runtime.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. * Copyright (c) 2014-2018 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <assert.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include "py/parsenum.h"
  33. #include "py/compile.h"
  34. #include "py/objstr.h"
  35. #include "py/objtuple.h"
  36. #include "py/objlist.h"
  37. #include "py/objtype.h"
  38. #include "py/objmodule.h"
  39. #include "py/objgenerator.h"
  40. #include "py/smallint.h"
  41. #include "py/stream.h"
  42. #include "py/runtime.h"
  43. #include "py/builtin.h"
  44. #include "py/stackctrl.h"
  45. #include "py/gc.h"
  46. #if MICROPY_DEBUG_VERBOSE // print debugging info
  47. #define DEBUG_PRINT (1)
  48. #define DEBUG_printf DEBUG_printf
  49. #define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
  50. #else // don't print debugging info
  51. #define DEBUG_printf(...) (void)0
  52. #define DEBUG_OP_printf(...) (void)0
  53. #endif
  54. const mp_obj_module_t mp_module___main__ = {
  55. .base = { &mp_type_module },
  56. .globals = (mp_obj_dict_t *)&MP_STATE_VM(dict_main),
  57. };
  58. MP_REGISTER_MODULE(MP_QSTR___main__, mp_module___main__);
  59. #define TYPE_HAS_ITERNEXT(type) (type->flags & (MP_TYPE_FLAG_ITER_IS_ITERNEXT | MP_TYPE_FLAG_ITER_IS_CUSTOM | MP_TYPE_FLAG_ITER_IS_STREAM))
  60. void mp_init(void) {
  61. qstr_init();
  62. // no pending exceptions to start with
  63. MP_STATE_THREAD(mp_pending_exception) = MP_OBJ_NULL;
  64. #if MICROPY_ENABLE_SCHEDULER
  65. // no pending callbacks to start with
  66. MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
  67. #if MICROPY_SCHEDULER_STATIC_NODES
  68. if (MP_STATE_VM(sched_head) != NULL) {
  69. // pending callbacks are on the list, eg from before a soft reset
  70. MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
  71. }
  72. #endif
  73. MP_STATE_VM(sched_idx) = 0;
  74. MP_STATE_VM(sched_len) = 0;
  75. #endif
  76. #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
  77. mp_init_emergency_exception_buf();
  78. #endif
  79. #if MICROPY_KBD_EXCEPTION
  80. // initialise the exception object for raising KeyboardInterrupt
  81. MP_STATE_VM(mp_kbd_exception).base.type = &mp_type_KeyboardInterrupt;
  82. MP_STATE_VM(mp_kbd_exception).traceback_alloc = 0;
  83. MP_STATE_VM(mp_kbd_exception).traceback_len = 0;
  84. MP_STATE_VM(mp_kbd_exception).traceback_data = NULL;
  85. MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj;
  86. #endif
  87. #if MICROPY_ENABLE_COMPILER
  88. // optimization disabled by default
  89. MP_STATE_VM(mp_optimise_value) = 0;
  90. #if MICROPY_EMIT_NATIVE
  91. MP_STATE_VM(default_emit_opt) = MP_EMIT_OPT_NONE;
  92. #endif
  93. #endif
  94. // init global module dict
  95. mp_obj_dict_init(&MP_STATE_VM(mp_loaded_modules_dict), MICROPY_LOADED_MODULES_DICT_SIZE);
  96. // initialise the __main__ module
  97. mp_obj_dict_init(&MP_STATE_VM(dict_main), 1);
  98. mp_obj_dict_store(MP_OBJ_FROM_PTR(&MP_STATE_VM(dict_main)), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
  99. // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
  100. mp_locals_set(&MP_STATE_VM(dict_main));
  101. mp_globals_set(&MP_STATE_VM(dict_main));
  102. #if MICROPY_CAN_OVERRIDE_BUILTINS
  103. // start with no extensions to builtins
  104. MP_STATE_VM(mp_module_builtins_override_dict) = NULL;
  105. #endif
  106. #if MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE
  107. MP_STATE_VM(track_reloc_code_list) = MP_OBJ_NULL;
  108. #endif
  109. #if MICROPY_PY_OS_DUPTERM
  110. for (size_t i = 0; i < MICROPY_PY_OS_DUPTERM; ++i) {
  111. MP_STATE_VM(dupterm_objs[i]) = MP_OBJ_NULL;
  112. }
  113. #endif
  114. #if MICROPY_VFS
  115. // initialise the VFS sub-system
  116. MP_STATE_VM(vfs_cur) = NULL;
  117. MP_STATE_VM(vfs_mount_table) = NULL;
  118. #endif
  119. #if MICROPY_PY_SYS_PATH_ARGV_DEFAULTS
  120. #if MICROPY_PY_SYS_PATH
  121. mp_sys_path = mp_obj_new_list(0, NULL);
  122. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
  123. #if MICROPY_MODULE_FROZEN
  124. mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
  125. #endif
  126. #endif
  127. #if MICROPY_PY_SYS_ARGV
  128. mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
  129. #endif
  130. #endif // MICROPY_PY_SYS_PATH_ARGV_DEFAULTS
  131. #if MICROPY_PY_SYS_ATEXIT
  132. MP_STATE_VM(sys_exitfunc) = mp_const_none;
  133. #endif
  134. #if MICROPY_PY_SYS_PS1_PS2
  135. MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_PS1]) = MP_OBJ_NEW_QSTR(MP_QSTR__gt__gt__gt__space_);
  136. MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_PS2]) = MP_OBJ_NEW_QSTR(MP_QSTR__dot__dot__dot__space_);
  137. #endif
  138. #if MICROPY_PY_SYS_SETTRACE
  139. MP_STATE_THREAD(prof_trace_callback) = MP_OBJ_NULL;
  140. MP_STATE_THREAD(prof_callback_is_executing) = false;
  141. MP_STATE_THREAD(current_code_state) = NULL;
  142. #endif
  143. #if MICROPY_PY_SYS_TRACEBACKLIMIT
  144. MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_TRACEBACKLIMIT]) = MP_OBJ_NEW_SMALL_INT(1000);
  145. #endif
  146. #if MICROPY_PY_BLUETOOTH
  147. MP_STATE_VM(bluetooth) = MP_OBJ_NULL;
  148. #endif
  149. #if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
  150. MP_STATE_VM(usbd) = MP_OBJ_NULL;
  151. #endif
  152. #if MICROPY_PY_THREAD_GIL
  153. mp_thread_mutex_init(&MP_STATE_VM(gil_mutex));
  154. #endif
  155. // call port specific initialization if any
  156. #ifdef MICROPY_PORT_INIT_FUNC
  157. MICROPY_PORT_INIT_FUNC;
  158. #endif
  159. MP_THREAD_GIL_ENTER();
  160. }
  161. void mp_deinit(void) {
  162. MP_THREAD_GIL_EXIT();
  163. // call port specific deinitialization if any
  164. #ifdef MICROPY_PORT_DEINIT_FUNC
  165. MICROPY_PORT_DEINIT_FUNC;
  166. #endif
  167. }
  168. void mp_globals_locals_set_from_nlr_jump_callback(void *ctx_in) {
  169. nlr_jump_callback_node_globals_locals_t *ctx = ctx_in;
  170. mp_globals_set(ctx->globals);
  171. mp_locals_set(ctx->locals);
  172. }
  173. void mp_call_function_1_from_nlr_jump_callback(void *ctx_in) {
  174. nlr_jump_callback_node_call_function_1_t *ctx = ctx_in;
  175. ctx->func(ctx->arg);
  176. }
  177. mp_obj_t MICROPY_WRAP_MP_LOAD_NAME(mp_load_name)(qstr qst) {
  178. // logic: search locals, globals, builtins
  179. DEBUG_OP_printf("load name %s\n", qstr_str(qst));
  180. // If we're at the outer scope (locals == globals), dispatch to load_global right away
  181. if (mp_locals_get() != mp_globals_get()) {
  182. mp_map_elem_t *elem = mp_map_lookup(&mp_locals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
  183. if (elem != NULL) {
  184. return elem->value;
  185. }
  186. }
  187. return mp_load_global(qst);
  188. }
  189. mp_obj_t MICROPY_WRAP_MP_LOAD_GLOBAL(mp_load_global)(qstr qst) {
  190. // logic: search globals, builtins
  191. DEBUG_OP_printf("load global %s\n", qstr_str(qst));
  192. mp_map_elem_t *elem = mp_map_lookup(&mp_globals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
  193. if (elem == NULL) {
  194. #if MICROPY_CAN_OVERRIDE_BUILTINS
  195. if (MP_STATE_VM(mp_module_builtins_override_dict) != NULL) {
  196. // lookup in additional dynamic table of builtins first
  197. elem = mp_map_lookup(&MP_STATE_VM(mp_module_builtins_override_dict)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
  198. if (elem != NULL) {
  199. return elem->value;
  200. }
  201. }
  202. #endif
  203. elem = mp_map_lookup((mp_map_t *)&mp_module_builtins_globals.map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
  204. if (elem == NULL) {
  205. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  206. mp_raise_msg(&mp_type_NameError, MP_ERROR_TEXT("name not defined"));
  207. #else
  208. mp_raise_msg_varg(&mp_type_NameError, MP_ERROR_TEXT("name '%q' isn't defined"), qst);
  209. #endif
  210. }
  211. }
  212. return elem->value;
  213. }
  214. mp_obj_t mp_load_build_class(void) {
  215. DEBUG_OP_printf("load_build_class\n");
  216. #if MICROPY_CAN_OVERRIDE_BUILTINS
  217. if (MP_STATE_VM(mp_module_builtins_override_dict) != NULL) {
  218. // lookup in additional dynamic table of builtins first
  219. mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_VM(mp_module_builtins_override_dict)->map, MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), MP_MAP_LOOKUP);
  220. if (elem != NULL) {
  221. return elem->value;
  222. }
  223. }
  224. #endif
  225. return MP_OBJ_FROM_PTR(&mp_builtin___build_class___obj);
  226. }
  227. void mp_store_name(qstr qst, mp_obj_t obj) {
  228. DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qst), obj);
  229. mp_obj_dict_store(MP_OBJ_FROM_PTR(mp_locals_get()), MP_OBJ_NEW_QSTR(qst), obj);
  230. }
  231. void mp_delete_name(qstr qst) {
  232. DEBUG_OP_printf("delete name %s\n", qstr_str(qst));
  233. // TODO convert KeyError to NameError if qst not found
  234. mp_obj_dict_delete(MP_OBJ_FROM_PTR(mp_locals_get()), MP_OBJ_NEW_QSTR(qst));
  235. }
  236. void mp_store_global(qstr qst, mp_obj_t obj) {
  237. DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qst), obj);
  238. mp_obj_dict_store(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(qst), obj);
  239. }
  240. void mp_delete_global(qstr qst) {
  241. DEBUG_OP_printf("delete global %s\n", qstr_str(qst));
  242. // TODO convert KeyError to NameError if qst not found
  243. mp_obj_dict_delete(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(qst));
  244. }
  245. mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
  246. DEBUG_OP_printf("unary " UINT_FMT " %q %p\n", op, mp_unary_op_method_name[op], arg);
  247. if (op == MP_UNARY_OP_NOT) {
  248. // "not x" is the negative of whether "x" is true per Python semantics
  249. return mp_obj_new_bool(mp_obj_is_true(arg) == 0);
  250. } else if (mp_obj_is_small_int(arg)) {
  251. mp_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
  252. switch (op) {
  253. case MP_UNARY_OP_BOOL:
  254. return mp_obj_new_bool(val != 0);
  255. case MP_UNARY_OP_HASH:
  256. return arg;
  257. case MP_UNARY_OP_POSITIVE:
  258. case MP_UNARY_OP_INT_MAYBE:
  259. return arg;
  260. case MP_UNARY_OP_NEGATIVE:
  261. // check for overflow
  262. if (val == MP_SMALL_INT_MIN) {
  263. return mp_obj_new_int(-val);
  264. } else {
  265. return MP_OBJ_NEW_SMALL_INT(-val);
  266. }
  267. case MP_UNARY_OP_ABS:
  268. if (val >= 0) {
  269. return arg;
  270. } else if (val == MP_SMALL_INT_MIN) {
  271. // check for overflow
  272. return mp_obj_new_int(-val);
  273. } else {
  274. return MP_OBJ_NEW_SMALL_INT(-val);
  275. }
  276. default:
  277. assert(op == MP_UNARY_OP_INVERT);
  278. return MP_OBJ_NEW_SMALL_INT(~val);
  279. }
  280. } else if (op == MP_UNARY_OP_HASH && mp_obj_is_str_or_bytes(arg)) {
  281. // fast path for hashing str/bytes
  282. GET_STR_HASH(arg, h);
  283. if (h == 0) {
  284. GET_STR_DATA_LEN(arg, data, len);
  285. h = qstr_compute_hash(data, len);
  286. }
  287. return MP_OBJ_NEW_SMALL_INT(h);
  288. } else {
  289. const mp_obj_type_t *type = mp_obj_get_type(arg);
  290. if (MP_OBJ_TYPE_HAS_SLOT(type, unary_op)) {
  291. mp_obj_t result = MP_OBJ_TYPE_GET_SLOT(type, unary_op)(op, arg);
  292. if (result != MP_OBJ_NULL) {
  293. return result;
  294. }
  295. } else if (op == MP_UNARY_OP_HASH) {
  296. // Type doesn't have unary_op so use hash of object instance.
  297. return MP_OBJ_NEW_SMALL_INT((mp_uint_t)arg);
  298. }
  299. if (op == MP_UNARY_OP_BOOL) {
  300. // Type doesn't have unary_op (or didn't handle MP_UNARY_OP_BOOL),
  301. // so is implicitly True as this code path is impossible to reach
  302. // if arg==mp_const_none.
  303. return mp_const_true;
  304. }
  305. if (op == MP_UNARY_OP_INT_MAYBE
  306. #if MICROPY_PY_BUILTINS_FLOAT
  307. || op == MP_UNARY_OP_FLOAT_MAYBE
  308. #if MICROPY_PY_BUILTINS_COMPLEX
  309. || op == MP_UNARY_OP_COMPLEX_MAYBE
  310. #endif
  311. #endif
  312. ) {
  313. // These operators may return MP_OBJ_NULL if they are not supported by the type.
  314. return MP_OBJ_NULL;
  315. }
  316. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  317. mp_raise_TypeError(MP_ERROR_TEXT("unsupported type for operator"));
  318. #else
  319. mp_raise_msg_varg(&mp_type_TypeError,
  320. MP_ERROR_TEXT("unsupported type for %q: '%s'"),
  321. mp_unary_op_method_name[op], mp_obj_get_type_str(arg));
  322. #endif
  323. }
  324. }
  325. mp_obj_t MICROPY_WRAP_MP_BINARY_OP(mp_binary_op)(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
  326. DEBUG_OP_printf("binary " UINT_FMT " %q %p %p\n", op, mp_binary_op_method_name[op], lhs, rhs);
  327. // TODO correctly distinguish inplace operators for mutable objects
  328. // lookup logic that CPython uses for +=:
  329. // check for implemented +=
  330. // then check for implemented +
  331. // then check for implemented seq.inplace_concat
  332. // then check for implemented seq.concat
  333. // then fail
  334. // note that list does not implement + or +=, so that inplace_concat is reached first for +=
  335. // deal with is
  336. if (op == MP_BINARY_OP_IS) {
  337. return mp_obj_new_bool(lhs == rhs);
  338. }
  339. // deal with == and != for all types
  340. if (op == MP_BINARY_OP_EQUAL || op == MP_BINARY_OP_NOT_EQUAL) {
  341. // mp_obj_equal_not_equal supports a bunch of shortcuts
  342. return mp_obj_equal_not_equal(op, lhs, rhs);
  343. }
  344. // deal with exception_match for all types
  345. if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
  346. // rhs must be issubclass(rhs, BaseException)
  347. if (mp_obj_is_exception_type(rhs)) {
  348. if (mp_obj_exception_match(lhs, rhs)) {
  349. return mp_const_true;
  350. } else {
  351. return mp_const_false;
  352. }
  353. } else if (mp_obj_is_type(rhs, &mp_type_tuple)) {
  354. mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(rhs);
  355. for (size_t i = 0; i < tuple->len; i++) {
  356. rhs = tuple->items[i];
  357. if (!mp_obj_is_exception_type(rhs)) {
  358. goto unsupported_op;
  359. }
  360. if (mp_obj_exception_match(lhs, rhs)) {
  361. return mp_const_true;
  362. }
  363. }
  364. return mp_const_false;
  365. }
  366. goto unsupported_op;
  367. }
  368. if (mp_obj_is_small_int(lhs)) {
  369. mp_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
  370. if (mp_obj_is_small_int(rhs)) {
  371. mp_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
  372. // This is a binary operation: lhs_val op rhs_val
  373. // We need to be careful to handle overflow; see CERT INT32-C
  374. // Operations that can overflow:
  375. // + result always fits in mp_int_t, then handled by SMALL_INT check
  376. // - result always fits in mp_int_t, then handled by SMALL_INT check
  377. // * checked explicitly
  378. // / if lhs=MIN and rhs=-1; result always fits in mp_int_t, then handled by SMALL_INT check
  379. // % if lhs=MIN and rhs=-1; result always fits in mp_int_t, then handled by SMALL_INT check
  380. // << checked explicitly
  381. switch (op) {
  382. case MP_BINARY_OP_OR:
  383. case MP_BINARY_OP_INPLACE_OR:
  384. lhs_val |= rhs_val;
  385. break;
  386. case MP_BINARY_OP_XOR:
  387. case MP_BINARY_OP_INPLACE_XOR:
  388. lhs_val ^= rhs_val;
  389. break;
  390. case MP_BINARY_OP_AND:
  391. case MP_BINARY_OP_INPLACE_AND:
  392. lhs_val &= rhs_val;
  393. break;
  394. case MP_BINARY_OP_LSHIFT:
  395. case MP_BINARY_OP_INPLACE_LSHIFT: {
  396. if (rhs_val < 0) {
  397. // negative shift not allowed
  398. mp_raise_ValueError(MP_ERROR_TEXT("negative shift count"));
  399. } else if (rhs_val >= (mp_int_t)(sizeof(lhs_val) * MP_BITS_PER_BYTE)
  400. || lhs_val > (MP_SMALL_INT_MAX >> rhs_val)
  401. || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
  402. // left-shift will overflow, so use higher precision integer
  403. lhs = mp_obj_new_int_from_ll(lhs_val);
  404. goto generic_binary_op;
  405. } else {
  406. // use standard precision
  407. lhs_val = (mp_uint_t)lhs_val << rhs_val;
  408. }
  409. break;
  410. }
  411. case MP_BINARY_OP_RSHIFT:
  412. case MP_BINARY_OP_INPLACE_RSHIFT:
  413. if (rhs_val < 0) {
  414. // negative shift not allowed
  415. mp_raise_ValueError(MP_ERROR_TEXT("negative shift count"));
  416. } else {
  417. // standard precision is enough for right-shift
  418. if (rhs_val >= (mp_int_t)(sizeof(lhs_val) * MP_BITS_PER_BYTE)) {
  419. // Shifting to big amounts is undefined behavior
  420. // in C and is CPU-dependent; propagate sign bit.
  421. rhs_val = sizeof(lhs_val) * MP_BITS_PER_BYTE - 1;
  422. }
  423. lhs_val >>= rhs_val;
  424. }
  425. break;
  426. case MP_BINARY_OP_ADD:
  427. case MP_BINARY_OP_INPLACE_ADD:
  428. lhs_val += rhs_val;
  429. break;
  430. case MP_BINARY_OP_SUBTRACT:
  431. case MP_BINARY_OP_INPLACE_SUBTRACT:
  432. lhs_val -= rhs_val;
  433. break;
  434. case MP_BINARY_OP_MULTIPLY:
  435. case MP_BINARY_OP_INPLACE_MULTIPLY: {
  436. // If long long type exists and is larger than mp_int_t, then
  437. // we can use the following code to perform overflow-checked multiplication.
  438. // Otherwise (eg in x64 case) we must use mp_small_int_mul_overflow.
  439. #if 0
  440. // compute result using long long precision
  441. long long res = (long long)lhs_val * (long long)rhs_val;
  442. if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
  443. // result overflowed SMALL_INT, so return higher precision integer
  444. return mp_obj_new_int_from_ll(res);
  445. } else {
  446. // use standard precision
  447. lhs_val = (mp_int_t)res;
  448. }
  449. #endif
  450. if (mp_small_int_mul_overflow(lhs_val, rhs_val)) {
  451. // use higher precision
  452. lhs = mp_obj_new_int_from_ll(lhs_val);
  453. goto generic_binary_op;
  454. } else {
  455. // use standard precision
  456. return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
  457. }
  458. }
  459. case MP_BINARY_OP_FLOOR_DIVIDE:
  460. case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
  461. if (rhs_val == 0) {
  462. goto zero_division;
  463. }
  464. lhs_val = mp_small_int_floor_divide(lhs_val, rhs_val);
  465. break;
  466. #if MICROPY_PY_BUILTINS_FLOAT
  467. case MP_BINARY_OP_TRUE_DIVIDE:
  468. case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
  469. if (rhs_val == 0) {
  470. goto zero_division;
  471. }
  472. return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
  473. #endif
  474. case MP_BINARY_OP_MODULO:
  475. case MP_BINARY_OP_INPLACE_MODULO: {
  476. if (rhs_val == 0) {
  477. goto zero_division;
  478. }
  479. lhs_val = mp_small_int_modulo(lhs_val, rhs_val);
  480. break;
  481. }
  482. case MP_BINARY_OP_POWER:
  483. case MP_BINARY_OP_INPLACE_POWER:
  484. if (rhs_val < 0) {
  485. #if MICROPY_PY_BUILTINS_FLOAT
  486. return mp_obj_float_binary_op(op, (mp_float_t)lhs_val, rhs);
  487. #else
  488. mp_raise_ValueError(MP_ERROR_TEXT("negative power with no float support"));
  489. #endif
  490. } else {
  491. mp_int_t ans = 1;
  492. while (rhs_val > 0) {
  493. if (rhs_val & 1) {
  494. if (mp_small_int_mul_overflow(ans, lhs_val)) {
  495. goto power_overflow;
  496. }
  497. ans *= lhs_val;
  498. }
  499. if (rhs_val == 1) {
  500. break;
  501. }
  502. rhs_val /= 2;
  503. if (mp_small_int_mul_overflow(lhs_val, lhs_val)) {
  504. goto power_overflow;
  505. }
  506. lhs_val *= lhs_val;
  507. }
  508. lhs_val = ans;
  509. }
  510. break;
  511. power_overflow:
  512. // use higher precision
  513. lhs = mp_obj_new_int_from_ll(MP_OBJ_SMALL_INT_VALUE(lhs));
  514. goto generic_binary_op;
  515. case MP_BINARY_OP_DIVMOD: {
  516. if (rhs_val == 0) {
  517. goto zero_division;
  518. }
  519. // to reduce stack usage we don't pass a temp array of the 2 items
  520. mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
  521. tuple->items[0] = MP_OBJ_NEW_SMALL_INT(mp_small_int_floor_divide(lhs_val, rhs_val));
  522. tuple->items[1] = MP_OBJ_NEW_SMALL_INT(mp_small_int_modulo(lhs_val, rhs_val));
  523. return MP_OBJ_FROM_PTR(tuple);
  524. }
  525. case MP_BINARY_OP_LESS:
  526. return mp_obj_new_bool(lhs_val < rhs_val);
  527. case MP_BINARY_OP_MORE:
  528. return mp_obj_new_bool(lhs_val > rhs_val);
  529. case MP_BINARY_OP_LESS_EQUAL:
  530. return mp_obj_new_bool(lhs_val <= rhs_val);
  531. case MP_BINARY_OP_MORE_EQUAL:
  532. return mp_obj_new_bool(lhs_val >= rhs_val);
  533. default:
  534. goto unsupported_op;
  535. }
  536. // This is an inlined version of mp_obj_new_int, for speed
  537. if (MP_SMALL_INT_FITS(lhs_val)) {
  538. return MP_OBJ_NEW_SMALL_INT(lhs_val);
  539. } else {
  540. return mp_obj_new_int_from_ll(lhs_val);
  541. }
  542. #if MICROPY_PY_BUILTINS_FLOAT
  543. } else if (mp_obj_is_float(rhs)) {
  544. mp_obj_t res = mp_obj_float_binary_op(op, (mp_float_t)lhs_val, rhs);
  545. if (res == MP_OBJ_NULL) {
  546. goto unsupported_op;
  547. } else {
  548. return res;
  549. }
  550. #endif
  551. #if MICROPY_PY_BUILTINS_COMPLEX
  552. } else if (mp_obj_is_type(rhs, &mp_type_complex)) {
  553. mp_obj_t res = mp_obj_complex_binary_op(op, (mp_float_t)lhs_val, 0, rhs);
  554. if (res == MP_OBJ_NULL) {
  555. goto unsupported_op;
  556. } else {
  557. return res;
  558. }
  559. #endif
  560. }
  561. }
  562. // Convert MP_BINARY_OP_IN to MP_BINARY_OP_CONTAINS with swapped args.
  563. if (op == MP_BINARY_OP_IN) {
  564. op = MP_BINARY_OP_CONTAINS;
  565. mp_obj_t temp = lhs;
  566. lhs = rhs;
  567. rhs = temp;
  568. }
  569. // generic binary_op supplied by type
  570. const mp_obj_type_t *type;
  571. generic_binary_op:
  572. type = mp_obj_get_type(lhs);
  573. if (MP_OBJ_TYPE_HAS_SLOT(type, binary_op)) {
  574. mp_obj_t result = MP_OBJ_TYPE_GET_SLOT(type, binary_op)(op, lhs, rhs);
  575. if (result != MP_OBJ_NULL) {
  576. return result;
  577. }
  578. }
  579. // If this was an inplace method, fallback to the corresponding normal method.
  580. // https://docs.python.org/3/reference/datamodel.html#object.__iadd__ :
  581. // "If a specific method is not defined, the augmented assignment falls back
  582. // to the normal methods."
  583. if (op >= MP_BINARY_OP_INPLACE_OR && op <= MP_BINARY_OP_INPLACE_POWER) {
  584. op += MP_BINARY_OP_OR - MP_BINARY_OP_INPLACE_OR;
  585. goto generic_binary_op;
  586. }
  587. #if MICROPY_PY_REVERSE_SPECIAL_METHODS
  588. if (op >= MP_BINARY_OP_OR && op <= MP_BINARY_OP_POWER) {
  589. mp_obj_t t = rhs;
  590. rhs = lhs;
  591. lhs = t;
  592. op += MP_BINARY_OP_REVERSE_OR - MP_BINARY_OP_OR;
  593. goto generic_binary_op;
  594. } else if (op >= MP_BINARY_OP_REVERSE_OR) {
  595. // Convert __rop__ back to __op__ for error message
  596. mp_obj_t t = rhs;
  597. rhs = lhs;
  598. lhs = t;
  599. op -= MP_BINARY_OP_REVERSE_OR - MP_BINARY_OP_OR;
  600. }
  601. #endif
  602. if (op == MP_BINARY_OP_CONTAINS) {
  603. // If type didn't support containment then explicitly walk the iterator.
  604. // mp_getiter will raise the appropriate exception if lhs is not iterable.
  605. mp_obj_iter_buf_t iter_buf;
  606. mp_obj_t iter = mp_getiter(lhs, &iter_buf);
  607. mp_obj_t next;
  608. while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
  609. if (mp_obj_equal(next, rhs)) {
  610. return mp_const_true;
  611. }
  612. }
  613. return mp_const_false;
  614. }
  615. unsupported_op:
  616. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  617. mp_raise_TypeError(MP_ERROR_TEXT("unsupported type for operator"));
  618. #else
  619. mp_raise_msg_varg(&mp_type_TypeError,
  620. MP_ERROR_TEXT("unsupported types for %q: '%s', '%s'"),
  621. mp_binary_op_method_name[op], mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs));
  622. #endif
  623. zero_division:
  624. mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("divide by zero"));
  625. }
  626. mp_obj_t mp_call_function_0(mp_obj_t fun) {
  627. return mp_call_function_n_kw(fun, 0, 0, NULL);
  628. }
  629. mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg) {
  630. return mp_call_function_n_kw(fun, 1, 0, &arg);
  631. }
  632. mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
  633. mp_obj_t args[2];
  634. args[0] = arg1;
  635. args[1] = arg2;
  636. return mp_call_function_n_kw(fun, 2, 0, args);
  637. }
  638. // args contains, eg: arg0 arg1 key0 value0 key1 value1
  639. mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  640. // TODO improve this: fun object can specify its type and we parse here the arguments,
  641. // passing to the function arrays of fixed and keyword arguments
  642. DEBUG_OP_printf("calling function %p(n_args=" UINT_FMT ", n_kw=" UINT_FMT ", args=%p)\n", fun_in, n_args, n_kw, args);
  643. // get the type
  644. const mp_obj_type_t *type = mp_obj_get_type(fun_in);
  645. // do the call
  646. if (MP_OBJ_TYPE_HAS_SLOT(type, call)) {
  647. return MP_OBJ_TYPE_GET_SLOT(type, call)(fun_in, n_args, n_kw, args);
  648. }
  649. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  650. mp_raise_TypeError(MP_ERROR_TEXT("object not callable"));
  651. #else
  652. mp_raise_msg_varg(&mp_type_TypeError,
  653. MP_ERROR_TEXT("'%s' object isn't callable"), mp_obj_get_type_str(fun_in));
  654. #endif
  655. }
  656. // args contains: fun self/NULL arg(0) ... arg(n_args-2) arg(n_args-1) kw_key(0) kw_val(0) ... kw_key(n_kw-1) kw_val(n_kw-1)
  657. // if n_args==0 and n_kw==0 then there are only fun and self/NULL
  658. mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args) {
  659. DEBUG_OP_printf("call method (fun=%p, self=%p, n_args=" UINT_FMT ", n_kw=" UINT_FMT ", args=%p)\n", args[0], args[1], n_args, n_kw, args);
  660. int adjust = (args[1] == MP_OBJ_NULL) ? 0 : 1;
  661. return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
  662. }
  663. // This function only needs to be exposed externally when in stackless mode.
  664. #if !MICROPY_STACKLESS
  665. static
  666. #endif
  667. void mp_call_prepare_args_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args, mp_call_args_t *out_args) {
  668. mp_obj_t fun = *args++;
  669. mp_obj_t self = MP_OBJ_NULL;
  670. if (have_self) {
  671. self = *args++; // may be MP_OBJ_NULL
  672. }
  673. size_t n_args = n_args_n_kw & 0xff;
  674. size_t n_kw = (n_args_n_kw >> 8) & 0xff;
  675. mp_uint_t star_args = MP_OBJ_SMALL_INT_VALUE(args[n_args + 2 * n_kw]);
  676. DEBUG_OP_printf("call method var (fun=%p, self=%p, n_args=%u, n_kw=%u, args=%p, map=%u)\n", fun, self, n_args, n_kw, args, star_args);
  677. // We need to create the following array of objects:
  678. // args[0 .. n_args] unpacked(pos_seq) args[n_args .. n_args + 2 * n_kw] unpacked(kw_dict)
  679. // TODO: optimize one day to avoid constructing new arg array? Will be hard.
  680. // The new args array
  681. mp_obj_t *args2;
  682. size_t args2_alloc;
  683. size_t args2_len = 0;
  684. // Try to get a hint for unpacked * args length
  685. ssize_t list_len = 0;
  686. if (star_args != 0) {
  687. for (size_t i = 0; i < n_args; i++) {
  688. if ((star_args >> i) & 1) {
  689. mp_obj_t len = mp_obj_len_maybe(args[i]);
  690. if (len != MP_OBJ_NULL) {
  691. // -1 accounts for 1 of n_args occupied by this arg
  692. list_len += mp_obj_get_int(len) - 1;
  693. }
  694. }
  695. }
  696. }
  697. // Try to get a hint for the size of the kw_dict
  698. ssize_t kw_dict_len = 0;
  699. for (size_t i = 0; i < n_kw; i++) {
  700. mp_obj_t key = args[n_args + i * 2];
  701. mp_obj_t value = args[n_args + i * 2 + 1];
  702. if (key == MP_OBJ_NULL && value != MP_OBJ_NULL && mp_obj_is_type(value, &mp_type_dict)) {
  703. // -1 accounts for 1 of n_kw occupied by this arg
  704. kw_dict_len += mp_obj_dict_len(value) - 1;
  705. }
  706. }
  707. // Extract the pos_seq sequence to the new args array.
  708. // Note that it can be arbitrary iterator.
  709. if (star_args == 0) {
  710. // no star args to unpack
  711. // allocate memory for the new array of args
  712. args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len);
  713. args2 = mp_nonlocal_alloc(args2_alloc * sizeof(mp_obj_t));
  714. // copy the self
  715. if (self != MP_OBJ_NULL) {
  716. args2[args2_len++] = self;
  717. }
  718. // copy the fixed pos args
  719. mp_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
  720. args2_len += n_args;
  721. } else {
  722. // at least one star arg to unpack
  723. // allocate memory for the new array of args
  724. args2_alloc = 1 + n_args + list_len + 2 * (n_kw + kw_dict_len);
  725. args2 = mp_nonlocal_alloc(args2_alloc * sizeof(mp_obj_t));
  726. // copy the self
  727. if (self != MP_OBJ_NULL) {
  728. args2[args2_len++] = self;
  729. }
  730. for (size_t i = 0; i < n_args; i++) {
  731. mp_obj_t arg = args[i];
  732. if ((star_args >> i) & 1) {
  733. // star arg
  734. if (mp_obj_is_type(arg, &mp_type_tuple) || mp_obj_is_type(arg, &mp_type_list)) {
  735. // optimise the case of a tuple and list
  736. // get the items
  737. size_t len;
  738. mp_obj_t *items;
  739. mp_obj_get_array(arg, &len, &items);
  740. // copy the items
  741. assert(args2_len + len <= args2_alloc);
  742. mp_seq_copy(args2 + args2_len, items, len, mp_obj_t);
  743. args2_len += len;
  744. } else {
  745. // generic iterator
  746. // extract the variable position args from the iterator
  747. mp_obj_iter_buf_t iter_buf;
  748. mp_obj_t iterable = mp_getiter(arg, &iter_buf);
  749. mp_obj_t item;
  750. while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
  751. if (args2_len + (n_args - i) >= args2_alloc) {
  752. args2 = mp_nonlocal_realloc(args2, args2_alloc * sizeof(mp_obj_t),
  753. args2_alloc * 2 * sizeof(mp_obj_t));
  754. args2_alloc *= 2;
  755. }
  756. args2[args2_len++] = item;
  757. }
  758. }
  759. } else {
  760. // normal argument
  761. assert(args2_len < args2_alloc);
  762. args2[args2_len++] = arg;
  763. }
  764. }
  765. }
  766. // The size of the args2 array now is the number of positional args.
  767. size_t pos_args_len = args2_len;
  768. // ensure there is still enough room for kw args
  769. if (args2_len + 2 * (n_kw + kw_dict_len) > args2_alloc) {
  770. size_t new_alloc = args2_len + 2 * (n_kw + kw_dict_len);
  771. args2 = mp_nonlocal_realloc(args2, args2_alloc * sizeof(mp_obj_t),
  772. new_alloc * sizeof(mp_obj_t));
  773. args2_alloc = new_alloc;
  774. }
  775. // Copy the kw args.
  776. for (size_t i = 0; i < n_kw; i++) {
  777. mp_obj_t kw_key = args[n_args + i * 2];
  778. mp_obj_t kw_value = args[n_args + i * 2 + 1];
  779. if (kw_key == MP_OBJ_NULL) {
  780. // double-star args
  781. if (mp_obj_is_type(kw_value, &mp_type_dict)) {
  782. // dictionary
  783. mp_map_t *map = mp_obj_dict_get_map(kw_value);
  784. // should have enough, since kw_dict_len is in this case hinted correctly above
  785. assert(args2_len + 2 * map->used <= args2_alloc);
  786. for (size_t j = 0; j < map->alloc; j++) {
  787. if (mp_map_slot_is_filled(map, j)) {
  788. // the key must be a qstr, so intern it if it's a string
  789. mp_obj_t key = map->table[j].key;
  790. if (!mp_obj_is_qstr(key)) {
  791. key = mp_obj_str_intern_checked(key);
  792. }
  793. args2[args2_len++] = key;
  794. args2[args2_len++] = map->table[j].value;
  795. }
  796. }
  797. } else {
  798. // generic mapping:
  799. // - call keys() to get an iterable of all keys in the mapping
  800. // - call __getitem__ for each key to get the corresponding value
  801. // get the keys iterable
  802. mp_obj_t dest[3];
  803. mp_load_method(kw_value, MP_QSTR_keys, dest);
  804. mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest), NULL);
  805. mp_obj_t key;
  806. while ((key = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
  807. // expand size of args array if needed
  808. if (args2_len + 1 >= args2_alloc) {
  809. size_t new_alloc = args2_alloc * 2;
  810. args2 = mp_nonlocal_realloc(args2, args2_alloc * sizeof(mp_obj_t), new_alloc * sizeof(mp_obj_t));
  811. args2_alloc = new_alloc;
  812. }
  813. // the key must be a qstr, so intern it if it's a string
  814. if (!mp_obj_is_qstr(key)) {
  815. key = mp_obj_str_intern_checked(key);
  816. }
  817. // get the value corresponding to the key
  818. mp_load_method(kw_value, MP_QSTR___getitem__, dest);
  819. dest[2] = key;
  820. mp_obj_t value = mp_call_method_n_kw(1, 0, dest);
  821. // store the key/value pair in the argument array
  822. args2[args2_len++] = key;
  823. args2[args2_len++] = value;
  824. }
  825. }
  826. } else {
  827. // normal kwarg
  828. assert(args2_len + 2 <= args2_alloc);
  829. args2[args2_len++] = kw_key;
  830. args2[args2_len++] = kw_value;
  831. }
  832. }
  833. out_args->fun = fun;
  834. out_args->args = args2;
  835. out_args->n_args = pos_args_len;
  836. out_args->n_kw = (args2_len - pos_args_len) / 2;
  837. out_args->n_alloc = args2_alloc;
  838. }
  839. mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args) {
  840. mp_call_args_t out_args;
  841. mp_call_prepare_args_n_kw_var(have_self, n_args_n_kw, args, &out_args);
  842. mp_obj_t res = mp_call_function_n_kw(out_args.fun, out_args.n_args, out_args.n_kw, out_args.args);
  843. mp_nonlocal_free(out_args.args, out_args.n_alloc * sizeof(mp_obj_t));
  844. return res;
  845. }
  846. // unpacked items are stored in reverse order into the array pointed to by items
  847. void mp_unpack_sequence(mp_obj_t seq_in, size_t num, mp_obj_t *items) {
  848. size_t seq_len;
  849. if (mp_obj_is_type(seq_in, &mp_type_tuple) || mp_obj_is_type(seq_in, &mp_type_list)) {
  850. mp_obj_t *seq_items;
  851. mp_obj_get_array(seq_in, &seq_len, &seq_items);
  852. if (seq_len < num) {
  853. goto too_short;
  854. } else if (seq_len > num) {
  855. goto too_long;
  856. }
  857. for (size_t i = 0; i < num; i++) {
  858. items[i] = seq_items[num - 1 - i];
  859. }
  860. } else {
  861. mp_obj_iter_buf_t iter_buf;
  862. mp_obj_t iterable = mp_getiter(seq_in, &iter_buf);
  863. for (seq_len = 0; seq_len < num; seq_len++) {
  864. mp_obj_t el = mp_iternext(iterable);
  865. if (el == MP_OBJ_STOP_ITERATION) {
  866. goto too_short;
  867. }
  868. items[num - 1 - seq_len] = el;
  869. }
  870. if (mp_iternext(iterable) != MP_OBJ_STOP_ITERATION) {
  871. goto too_long;
  872. }
  873. }
  874. return;
  875. too_short:
  876. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  877. mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack"));
  878. #else
  879. mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("need more than %d values to unpack"), (int)seq_len);
  880. #endif
  881. too_long:
  882. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  883. mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack"));
  884. #else
  885. mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("too many values to unpack (expected %d)"), (int)num);
  886. #endif
  887. }
  888. // unpacked items are stored in reverse order into the array pointed to by items
  889. void mp_unpack_ex(mp_obj_t seq_in, size_t num_in, mp_obj_t *items) {
  890. size_t num_left = num_in & 0xff;
  891. size_t num_right = (num_in >> 8) & 0xff;
  892. DEBUG_OP_printf("unpack ex " UINT_FMT " " UINT_FMT "\n", num_left, num_right);
  893. size_t seq_len;
  894. if (mp_obj_is_type(seq_in, &mp_type_tuple) || mp_obj_is_type(seq_in, &mp_type_list)) {
  895. // Make the seq variable volatile so the compiler keeps a reference to it,
  896. // since if it's a tuple then seq_items points to the interior of the GC cell
  897. // and mp_obj_new_list may trigger a GC which doesn't trace this and reclaims seq.
  898. volatile mp_obj_t seq = seq_in;
  899. mp_obj_t *seq_items;
  900. mp_obj_get_array(seq, &seq_len, &seq_items);
  901. if (seq_len < num_left + num_right) {
  902. goto too_short;
  903. }
  904. for (size_t i = 0; i < num_right; i++) {
  905. items[i] = seq_items[seq_len - 1 - i];
  906. }
  907. items[num_right] = mp_obj_new_list(seq_len - num_left - num_right, seq_items + num_left);
  908. for (size_t i = 0; i < num_left; i++) {
  909. items[num_right + 1 + i] = seq_items[num_left - 1 - i];
  910. }
  911. seq = MP_OBJ_NULL;
  912. } else {
  913. // Generic iterable; this gets a bit messy: we unpack known left length to the
  914. // items destination array, then the rest to a dynamically created list. Once the
  915. // iterable is exhausted, we take from this list for the right part of the items.
  916. // TODO Improve to waste less memory in the dynamically created list.
  917. mp_obj_t iterable = mp_getiter(seq_in, NULL);
  918. mp_obj_t item;
  919. for (seq_len = 0; seq_len < num_left; seq_len++) {
  920. item = mp_iternext(iterable);
  921. if (item == MP_OBJ_STOP_ITERATION) {
  922. goto too_short;
  923. }
  924. items[num_left + num_right + 1 - 1 - seq_len] = item;
  925. }
  926. mp_obj_list_t *rest = MP_OBJ_TO_PTR(mp_obj_new_list(0, NULL));
  927. while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
  928. mp_obj_list_append(MP_OBJ_FROM_PTR(rest), item);
  929. }
  930. if (rest->len < num_right) {
  931. goto too_short;
  932. }
  933. items[num_right] = MP_OBJ_FROM_PTR(rest);
  934. for (size_t i = 0; i < num_right; i++) {
  935. items[num_right - 1 - i] = rest->items[rest->len - num_right + i];
  936. }
  937. mp_obj_list_set_len(MP_OBJ_FROM_PTR(rest), rest->len - num_right);
  938. }
  939. return;
  940. too_short:
  941. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  942. mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack"));
  943. #else
  944. mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("need more than %d values to unpack"), (int)seq_len);
  945. #endif
  946. }
  947. mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
  948. DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
  949. // use load_method
  950. mp_obj_t dest[2];
  951. mp_load_method(base, attr, dest);
  952. if (dest[1] == MP_OBJ_NULL) {
  953. // load_method returned just a normal attribute
  954. return dest[0];
  955. } else {
  956. // load_method returned a method, so build a bound method object
  957. return mp_obj_new_bound_meth(dest[0], dest[1]);
  958. }
  959. }
  960. #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
  961. // The following "checked fun" type is local to the mp_convert_member_lookup
  962. // function, and serves to check that the first argument to a builtin function
  963. // has the correct type.
  964. typedef struct _mp_obj_checked_fun_t {
  965. mp_obj_base_t base;
  966. const mp_obj_type_t *type;
  967. mp_obj_t fun;
  968. } mp_obj_checked_fun_t;
  969. static mp_obj_t checked_fun_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  970. mp_obj_checked_fun_t *self = MP_OBJ_TO_PTR(self_in);
  971. if (n_args > 0) {
  972. const mp_obj_type_t *arg0_type = mp_obj_get_type(args[0]);
  973. if (arg0_type != self->type) {
  974. #if MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_DETAILED
  975. mp_raise_TypeError(MP_ERROR_TEXT("argument has wrong type"));
  976. #else
  977. mp_raise_msg_varg(&mp_type_TypeError,
  978. MP_ERROR_TEXT("argument should be a '%q' not a '%q'"), self->type->name, arg0_type->name);
  979. #endif
  980. }
  981. }
  982. return mp_call_function_n_kw(self->fun, n_args, n_kw, args);
  983. }
  984. static MP_DEFINE_CONST_OBJ_TYPE(
  985. mp_type_checked_fun,
  986. MP_QSTR_function,
  987. MP_TYPE_FLAG_BINDS_SELF,
  988. call, checked_fun_call
  989. );
  990. static mp_obj_t mp_obj_new_checked_fun(const mp_obj_type_t *type, mp_obj_t fun) {
  991. mp_obj_checked_fun_t *o = mp_obj_malloc(mp_obj_checked_fun_t, &mp_type_checked_fun);
  992. o->type = type;
  993. o->fun = fun;
  994. return MP_OBJ_FROM_PTR(o);
  995. }
  996. #endif // MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
  997. // Given a member that was extracted from an instance, convert it correctly
  998. // and put the result in the dest[] array for a possible method call.
  999. // Conversion means dealing with static/class methods, callables, and values.
  1000. // see http://docs.python.org/3/howto/descriptor.html
  1001. // and also https://mail.python.org/pipermail/python-dev/2015-March/138950.html
  1002. void mp_convert_member_lookup(mp_obj_t self, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest) {
  1003. if (mp_obj_is_obj(member)) {
  1004. const mp_obj_type_t *m_type = ((mp_obj_base_t *)MP_OBJ_TO_PTR(member))->type;
  1005. if (m_type->flags & MP_TYPE_FLAG_BINDS_SELF) {
  1006. // `member` is a function that binds self as its first argument.
  1007. if (m_type->flags & MP_TYPE_FLAG_BUILTIN_FUN) {
  1008. // `member` is a built-in function, which has special behaviour.
  1009. if (mp_obj_is_instance_type(type)) {
  1010. // Built-in functions on user types always behave like a staticmethod.
  1011. dest[0] = member;
  1012. }
  1013. #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
  1014. else if (self == MP_OBJ_NULL && type != &mp_type_object) {
  1015. // `member` is a built-in method without a first argument, so wrap
  1016. // it in a type checker that will check self when it's supplied.
  1017. // Note that object will do its own checking so shouldn't be wrapped.
  1018. dest[0] = mp_obj_new_checked_fun(type, member);
  1019. }
  1020. #endif
  1021. else {
  1022. // Return a (built-in) bound method, with self being this object.
  1023. dest[0] = member;
  1024. dest[1] = self;
  1025. }
  1026. } else {
  1027. // Return a bound method, with self being this object.
  1028. dest[0] = member;
  1029. dest[1] = self;
  1030. }
  1031. } else if (m_type == &mp_type_staticmethod) {
  1032. // `member` is a staticmethod, return the function that it wraps.
  1033. dest[0] = ((mp_obj_static_class_method_t *)MP_OBJ_TO_PTR(member))->fun;
  1034. } else if (m_type == &mp_type_classmethod) {
  1035. // `member` is a classmethod, return a bound method with self being the type of
  1036. // this object. This type should be the type of the original instance, not the
  1037. // base type (which is what is passed in the `type` argument to this function).
  1038. if (self != MP_OBJ_NULL) {
  1039. type = mp_obj_get_type(self);
  1040. }
  1041. dest[0] = ((mp_obj_static_class_method_t *)MP_OBJ_TO_PTR(member))->fun;
  1042. dest[1] = MP_OBJ_FROM_PTR(type);
  1043. } else {
  1044. // `member` is a value, so just return that value.
  1045. dest[0] = member;
  1046. }
  1047. } else {
  1048. // `member` is a value, so just return that value.
  1049. dest[0] = member;
  1050. }
  1051. }
  1052. // no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
  1053. // normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
  1054. // method attribute found, returns: dest[0] == <method>, dest[1] == <self>
  1055. void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest) {
  1056. // clear output to indicate no attribute/method found yet
  1057. dest[0] = MP_OBJ_NULL;
  1058. dest[1] = MP_OBJ_NULL;
  1059. // Note: the specific case of obj being an instance type is fast-path'ed in the VM
  1060. // for the MP_BC_LOAD_ATTR opcode. Instance types handle type->attr and look up directly
  1061. // in their member's map.
  1062. // get the type
  1063. const mp_obj_type_t *type = mp_obj_get_type(obj);
  1064. // look for built-in names
  1065. #if MICROPY_CPYTHON_COMPAT
  1066. if (attr == MP_QSTR___class__) {
  1067. // a.__class__ is equivalent to type(a)
  1068. dest[0] = MP_OBJ_FROM_PTR(type);
  1069. return;
  1070. }
  1071. #endif
  1072. if (attr == MP_QSTR___next__ && TYPE_HAS_ITERNEXT(type)) {
  1073. dest[0] = MP_OBJ_FROM_PTR(&mp_builtin_next_obj);
  1074. dest[1] = obj;
  1075. return;
  1076. }
  1077. if (MP_OBJ_TYPE_HAS_SLOT(type, attr)) {
  1078. // this type can do its own load, so call it
  1079. MP_OBJ_TYPE_GET_SLOT(type, attr)(obj, attr, dest);
  1080. // If type->attr has set dest[1] = MP_OBJ_SENTINEL, we should proceed
  1081. // with lookups below (i.e. in locals_dict). If not, return right away.
  1082. if (dest[1] != MP_OBJ_SENTINEL) {
  1083. return;
  1084. }
  1085. // Clear the fail flag set by type->attr so it's like it never ran.
  1086. dest[1] = MP_OBJ_NULL;
  1087. }
  1088. if (MP_OBJ_TYPE_HAS_SLOT(type, locals_dict)) {
  1089. // generic method lookup
  1090. // this is a lookup in the object (ie not class or type)
  1091. assert(MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->base.type == &mp_type_dict); // MicroPython restriction, for now
  1092. mp_map_t *locals_map = &MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->map;
  1093. mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
  1094. if (elem != NULL) {
  1095. mp_convert_member_lookup(obj, type, elem->value, dest);
  1096. }
  1097. return;
  1098. }
  1099. }
  1100. void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
  1101. DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
  1102. mp_load_method_maybe(base, attr, dest);
  1103. if (dest[0] == MP_OBJ_NULL) {
  1104. // no attribute/method called attr
  1105. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  1106. mp_raise_msg(&mp_type_AttributeError, MP_ERROR_TEXT("no such attribute"));
  1107. #else
  1108. // following CPython, we give a more detailed error message for type objects
  1109. if (mp_obj_is_type(base, &mp_type_type)) {
  1110. mp_raise_msg_varg(&mp_type_AttributeError,
  1111. MP_ERROR_TEXT("type object '%q' has no attribute '%q'"),
  1112. ((mp_obj_type_t *)MP_OBJ_TO_PTR(base))->name, attr);
  1113. } else {
  1114. mp_raise_msg_varg(&mp_type_AttributeError,
  1115. MP_ERROR_TEXT("'%s' object has no attribute '%q'"),
  1116. mp_obj_get_type_str(base), attr);
  1117. }
  1118. #endif
  1119. }
  1120. }
  1121. // Acts like mp_load_method_maybe but catches AttributeError, and all other exceptions if requested
  1122. void mp_load_method_protected(mp_obj_t obj, qstr attr, mp_obj_t *dest, bool catch_all_exc) {
  1123. nlr_buf_t nlr;
  1124. if (nlr_push(&nlr) == 0) {
  1125. mp_load_method_maybe(obj, attr, dest);
  1126. nlr_pop();
  1127. } else {
  1128. if (!catch_all_exc
  1129. && !mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t *)nlr.ret_val)->type),
  1130. MP_OBJ_FROM_PTR(&mp_type_AttributeError))) {
  1131. // Re-raise the exception
  1132. nlr_raise(MP_OBJ_FROM_PTR(nlr.ret_val));
  1133. }
  1134. }
  1135. }
  1136. void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
  1137. DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
  1138. const mp_obj_type_t *type = mp_obj_get_type(base);
  1139. if (MP_OBJ_TYPE_HAS_SLOT(type, attr)) {
  1140. mp_obj_t dest[2] = {MP_OBJ_SENTINEL, value};
  1141. MP_OBJ_TYPE_GET_SLOT(type, attr)(base, attr, dest);
  1142. if (dest[0] == MP_OBJ_NULL) {
  1143. // success
  1144. return;
  1145. }
  1146. }
  1147. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  1148. mp_raise_msg(&mp_type_AttributeError, MP_ERROR_TEXT("no such attribute"));
  1149. #else
  1150. mp_raise_msg_varg(&mp_type_AttributeError,
  1151. MP_ERROR_TEXT("'%s' object has no attribute '%q'"),
  1152. mp_obj_get_type_str(base), attr);
  1153. #endif
  1154. }
  1155. mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
  1156. assert(o_in);
  1157. const mp_obj_type_t *type = mp_obj_get_type(o_in);
  1158. // Most types that use iternext just use the identity getiter. We handle this case explicitly
  1159. // so we don't unnecessarily allocate any RAM for the iter_buf, which won't be used.
  1160. if ((type->flags & MP_TYPE_FLAG_ITER_IS_ITERNEXT) == MP_TYPE_FLAG_ITER_IS_ITERNEXT || (type->flags & MP_TYPE_FLAG_ITER_IS_STREAM) == MP_TYPE_FLAG_ITER_IS_STREAM) {
  1161. return o_in;
  1162. }
  1163. if (MP_OBJ_TYPE_HAS_SLOT(type, iter)) {
  1164. // check for native getiter (corresponds to __iter__)
  1165. if (iter_buf == NULL && MP_OBJ_TYPE_GET_SLOT(type, iter) != mp_obj_instance_getiter) {
  1166. // if caller did not provide a buffer then allocate one on the heap
  1167. // mp_obj_instance_getiter is special, it will allocate only if needed
  1168. iter_buf = m_new_obj(mp_obj_iter_buf_t);
  1169. }
  1170. mp_getiter_fun_t getiter;
  1171. if (type->flags & MP_TYPE_FLAG_ITER_IS_CUSTOM) {
  1172. getiter = ((mp_getiter_iternext_custom_t *)MP_OBJ_TYPE_GET_SLOT(type, iter))->getiter;
  1173. } else {
  1174. getiter = (mp_getiter_fun_t)MP_OBJ_TYPE_GET_SLOT(type, iter);
  1175. }
  1176. mp_obj_t iter = getiter(o_in, iter_buf);
  1177. if (iter != MP_OBJ_NULL) {
  1178. return iter;
  1179. }
  1180. }
  1181. // check for __getitem__
  1182. mp_obj_t dest[2];
  1183. mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
  1184. if (dest[0] != MP_OBJ_NULL) {
  1185. // __getitem__ exists, create and return an iterator
  1186. if (iter_buf == NULL) {
  1187. // if caller did not provide a buffer then allocate one on the heap
  1188. iter_buf = m_new_obj(mp_obj_iter_buf_t);
  1189. }
  1190. return mp_obj_new_getitem_iter(dest, iter_buf);
  1191. }
  1192. // object not iterable
  1193. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  1194. mp_raise_TypeError(MP_ERROR_TEXT("object not iterable"));
  1195. #else
  1196. mp_raise_msg_varg(&mp_type_TypeError,
  1197. MP_ERROR_TEXT("'%s' object isn't iterable"), mp_obj_get_type_str(o_in));
  1198. #endif
  1199. }
  1200. static mp_fun_1_t type_get_iternext(const mp_obj_type_t *type) {
  1201. if ((type->flags & MP_TYPE_FLAG_ITER_IS_STREAM) == MP_TYPE_FLAG_ITER_IS_STREAM) {
  1202. return mp_stream_unbuffered_iter;
  1203. } else if (type->flags & MP_TYPE_FLAG_ITER_IS_ITERNEXT) {
  1204. return (mp_fun_1_t)MP_OBJ_TYPE_GET_SLOT(type, iter);
  1205. } else if (type->flags & MP_TYPE_FLAG_ITER_IS_CUSTOM) {
  1206. return ((mp_getiter_iternext_custom_t *)MP_OBJ_TYPE_GET_SLOT(type, iter))->iternext;
  1207. } else {
  1208. return NULL;
  1209. }
  1210. }
  1211. // may return MP_OBJ_STOP_ITERATION as an optimisation instead of raise StopIteration()
  1212. // may also raise StopIteration()
  1213. mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
  1214. const mp_obj_type_t *type = mp_obj_get_type(o_in);
  1215. if (TYPE_HAS_ITERNEXT(type)) {
  1216. MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL;
  1217. return type_get_iternext(type)(o_in);
  1218. } else {
  1219. // check for __next__ method
  1220. mp_obj_t dest[2];
  1221. mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
  1222. if (dest[0] != MP_OBJ_NULL) {
  1223. // __next__ exists, call it and return its result
  1224. return mp_call_method_n_kw(0, 0, dest);
  1225. } else {
  1226. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  1227. mp_raise_TypeError(MP_ERROR_TEXT("object not an iterator"));
  1228. #else
  1229. mp_raise_msg_varg(&mp_type_TypeError,
  1230. MP_ERROR_TEXT("'%s' object isn't an iterator"), mp_obj_get_type_str(o_in));
  1231. #endif
  1232. }
  1233. }
  1234. }
  1235. // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration() (or any subclass thereof)
  1236. // may raise other exceptions
  1237. mp_obj_t mp_iternext(mp_obj_t o_in) {
  1238. MP_STACK_CHECK(); // enumerate, filter, map and zip can recursively call mp_iternext
  1239. const mp_obj_type_t *type = mp_obj_get_type(o_in);
  1240. if (TYPE_HAS_ITERNEXT(type)) {
  1241. MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL;
  1242. return type_get_iternext(type)(o_in);
  1243. } else {
  1244. // check for __next__ method
  1245. mp_obj_t dest[2];
  1246. mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
  1247. if (dest[0] != MP_OBJ_NULL) {
  1248. // __next__ exists, call it and return its result
  1249. nlr_buf_t nlr;
  1250. if (nlr_push(&nlr) == 0) {
  1251. mp_obj_t ret = mp_call_method_n_kw(0, 0, dest);
  1252. nlr_pop();
  1253. return ret;
  1254. } else {
  1255. if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t *)nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
  1256. return mp_make_stop_iteration(mp_obj_exception_get_value(MP_OBJ_FROM_PTR(nlr.ret_val)));
  1257. } else {
  1258. nlr_jump(nlr.ret_val);
  1259. }
  1260. }
  1261. } else {
  1262. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  1263. mp_raise_TypeError(MP_ERROR_TEXT("object not an iterator"));
  1264. #else
  1265. mp_raise_msg_varg(&mp_type_TypeError,
  1266. MP_ERROR_TEXT("'%s' object isn't an iterator"), mp_obj_get_type_str(o_in));
  1267. #endif
  1268. }
  1269. }
  1270. }
  1271. mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
  1272. assert((send_value != MP_OBJ_NULL) ^ (throw_value != MP_OBJ_NULL));
  1273. const mp_obj_type_t *type = mp_obj_get_type(self_in);
  1274. if (type == &mp_type_gen_instance) {
  1275. return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val);
  1276. }
  1277. if (TYPE_HAS_ITERNEXT(type) && send_value == mp_const_none) {
  1278. MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL;
  1279. mp_obj_t ret = type_get_iternext(type)(self_in);
  1280. *ret_val = ret;
  1281. if (ret != MP_OBJ_STOP_ITERATION) {
  1282. return MP_VM_RETURN_YIELD;
  1283. } else {
  1284. // The generator is finished.
  1285. // This is an optimised "raise StopIteration(*ret_val)".
  1286. *ret_val = MP_STATE_THREAD(stop_iteration_arg);
  1287. if (*ret_val == MP_OBJ_NULL) {
  1288. *ret_val = mp_const_none;
  1289. }
  1290. return MP_VM_RETURN_NORMAL;
  1291. }
  1292. }
  1293. mp_obj_t dest[3]; // Reserve slot for send() arg
  1294. // Python instance iterator protocol
  1295. if (send_value == mp_const_none) {
  1296. mp_load_method_maybe(self_in, MP_QSTR___next__, dest);
  1297. if (dest[0] != MP_OBJ_NULL) {
  1298. *ret_val = mp_call_method_n_kw(0, 0, dest);
  1299. return MP_VM_RETURN_YIELD;
  1300. }
  1301. }
  1302. // Either python instance generator protocol, or native object
  1303. // generator protocol.
  1304. if (send_value != MP_OBJ_NULL) {
  1305. mp_load_method(self_in, MP_QSTR_send, dest);
  1306. dest[2] = send_value;
  1307. *ret_val = mp_call_method_n_kw(1, 0, dest);
  1308. return MP_VM_RETURN_YIELD;
  1309. }
  1310. assert(throw_value != MP_OBJ_NULL);
  1311. {
  1312. if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(throw_value)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
  1313. mp_load_method_maybe(self_in, MP_QSTR_close, dest);
  1314. if (dest[0] != MP_OBJ_NULL) {
  1315. // TODO: Exceptions raised in close() are not propagated,
  1316. // printed to sys.stderr
  1317. *ret_val = mp_call_method_n_kw(0, 0, dest);
  1318. // We assume one can't "yield" from close()
  1319. return MP_VM_RETURN_NORMAL;
  1320. }
  1321. } else {
  1322. mp_load_method_maybe(self_in, MP_QSTR_throw, dest);
  1323. if (dest[0] != MP_OBJ_NULL) {
  1324. dest[2] = throw_value;
  1325. *ret_val = mp_call_method_n_kw(1, 0, dest);
  1326. // If .throw() method returned, we assume it's value to yield
  1327. // - any exception would be thrown with nlr_raise().
  1328. return MP_VM_RETURN_YIELD;
  1329. }
  1330. }
  1331. // If there's nowhere to throw exception into, then we assume that object
  1332. // is just incapable to handle it, so any exception thrown into it
  1333. // will be propagated up. This behavior is approved by test_pep380.py
  1334. // test_delegation_of_close_to_non_generator(),
  1335. // test_delegating_throw_to_non_generator()
  1336. if (mp_obj_exception_match(throw_value, MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
  1337. // PEP479: if StopIteration is raised inside a generator it is replaced with RuntimeError
  1338. *ret_val = mp_obj_new_exception_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("generator raised StopIteration"));
  1339. } else {
  1340. *ret_val = mp_make_raise_obj(throw_value);
  1341. }
  1342. return MP_VM_RETURN_EXCEPTION;
  1343. }
  1344. }
  1345. mp_obj_t mp_make_raise_obj(mp_obj_t o) {
  1346. DEBUG_printf("raise %p\n", o);
  1347. if (mp_obj_is_exception_type(o)) {
  1348. // o is an exception type (it is derived from BaseException (or is BaseException))
  1349. // create and return a new exception instance by calling o
  1350. // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
  1351. // could have const instances in ROM which we return here instead
  1352. o = mp_call_function_n_kw(o, 0, 0, NULL);
  1353. }
  1354. if (mp_obj_is_exception_instance(o)) {
  1355. // o is an instance of an exception, so use it as the exception
  1356. return o;
  1357. } else {
  1358. // o cannot be used as an exception, so return a type error (which will be raised by the caller)
  1359. return mp_obj_new_exception_msg(&mp_type_TypeError, MP_ERROR_TEXT("exceptions must derive from BaseException"));
  1360. }
  1361. }
  1362. mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
  1363. DEBUG_printf("import name '%s' level=%d\n", qstr_str(name), MP_OBJ_SMALL_INT_VALUE(level));
  1364. // build args array
  1365. mp_obj_t args[5];
  1366. args[0] = MP_OBJ_NEW_QSTR(name);
  1367. args[1] = mp_const_none; // TODO should be globals
  1368. args[2] = mp_const_none; // TODO should be locals
  1369. args[3] = fromlist;
  1370. args[4] = level;
  1371. #if MICROPY_CAN_OVERRIDE_BUILTINS
  1372. // Lookup __import__ and call that if it exists
  1373. mp_obj_dict_t *bo_dict = MP_STATE_VM(mp_module_builtins_override_dict);
  1374. if (bo_dict != NULL) {
  1375. mp_map_elem_t *import = mp_map_lookup(&bo_dict->map, MP_OBJ_NEW_QSTR(MP_QSTR___import__), MP_MAP_LOOKUP);
  1376. if (import != NULL) {
  1377. return mp_call_function_n_kw(import->value, 5, 0, args);
  1378. }
  1379. }
  1380. #endif
  1381. return mp_builtin___import__(5, args);
  1382. }
  1383. mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
  1384. DEBUG_printf("import from %p %s\n", module, qstr_str(name));
  1385. mp_obj_t dest[2];
  1386. mp_load_method_maybe(module, name, dest);
  1387. if (dest[1] != MP_OBJ_NULL) {
  1388. // Hopefully we can't import bound method from an object
  1389. import_error:
  1390. mp_raise_msg_varg(&mp_type_ImportError, MP_ERROR_TEXT("can't import name %q"), name);
  1391. }
  1392. if (dest[0] != MP_OBJ_NULL) {
  1393. return dest[0];
  1394. }
  1395. #if MICROPY_ENABLE_EXTERNAL_IMPORT
  1396. // See if it's a package, then can try FS import
  1397. mp_load_method_maybe(module, MP_QSTR___path__, dest);
  1398. if (dest[0] == MP_OBJ_NULL) {
  1399. goto import_error;
  1400. }
  1401. mp_load_method_maybe(module, MP_QSTR___name__, dest);
  1402. size_t pkg_name_len;
  1403. const char *pkg_name = mp_obj_str_get_data(dest[0], &pkg_name_len);
  1404. const uint dot_name_len = pkg_name_len + 1 + qstr_len(name);
  1405. char *dot_name = mp_local_alloc(dot_name_len);
  1406. memcpy(dot_name, pkg_name, pkg_name_len);
  1407. dot_name[pkg_name_len] = '.';
  1408. memcpy(dot_name + pkg_name_len + 1, qstr_str(name), qstr_len(name));
  1409. qstr dot_name_q = qstr_from_strn(dot_name, dot_name_len);
  1410. mp_local_free(dot_name);
  1411. // For fromlist, pass sentinel "non empty" value to force returning of leaf module
  1412. return mp_import_name(dot_name_q, mp_const_true, MP_OBJ_NEW_SMALL_INT(0));
  1413. #else
  1414. // Package import not supported with external imports disabled
  1415. goto import_error;
  1416. #endif
  1417. }
  1418. void mp_import_all(mp_obj_t module) {
  1419. DEBUG_printf("import all %p\n", module);
  1420. // TODO: Support __all__
  1421. mp_map_t *map = &mp_obj_module_get_globals(module)->map;
  1422. for (size_t i = 0; i < map->alloc; i++) {
  1423. if (mp_map_slot_is_filled(map, i)) {
  1424. // Entry in module global scope may be generated programmatically
  1425. // (and thus be not a qstr for longer names). Avoid turning it in
  1426. // qstr if it has '_' and was used exactly to save memory.
  1427. const char *name = mp_obj_str_get_str(map->table[i].key);
  1428. if (*name != '_') {
  1429. qstr qname = mp_obj_str_get_qstr(map->table[i].key);
  1430. mp_store_name(qname, map->table[i].value);
  1431. }
  1432. }
  1433. }
  1434. }
  1435. #if MICROPY_ENABLE_COMPILER
  1436. mp_obj_t mp_parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t parse_input_kind, mp_obj_dict_t *globals, mp_obj_dict_t *locals) {
  1437. // save context
  1438. nlr_jump_callback_node_globals_locals_t ctx;
  1439. ctx.globals = mp_globals_get();
  1440. ctx.locals = mp_locals_get();
  1441. // set new context
  1442. mp_globals_set(globals);
  1443. mp_locals_set(locals);
  1444. // set exception handler to restore context if an exception is raised
  1445. nlr_push_jump_callback(&ctx.callback, mp_globals_locals_set_from_nlr_jump_callback);
  1446. qstr source_name = lex->source_name;
  1447. mp_parse_tree_t parse_tree = mp_parse(lex, parse_input_kind);
  1448. mp_obj_t module_fun = mp_compile(&parse_tree, source_name, parse_input_kind == MP_PARSE_SINGLE_INPUT);
  1449. mp_obj_t ret;
  1450. if (MICROPY_PY_BUILTINS_COMPILE && globals == NULL) {
  1451. // for compile only, return value is the module function
  1452. ret = module_fun;
  1453. } else {
  1454. // execute module function and get return value
  1455. ret = mp_call_function_0(module_fun);
  1456. }
  1457. // deregister exception handler and restore context
  1458. nlr_pop_jump_callback(true);
  1459. // return value
  1460. return ret;
  1461. }
  1462. #endif // MICROPY_ENABLE_COMPILER
  1463. NORETURN void m_malloc_fail(size_t num_bytes) {
  1464. DEBUG_printf("memory allocation failed, allocating %u bytes\n", (uint)num_bytes);
  1465. #if MICROPY_ENABLE_GC
  1466. if (gc_is_locked()) {
  1467. mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("memory allocation failed, heap is locked"));
  1468. }
  1469. #endif
  1470. mp_raise_msg_varg(&mp_type_MemoryError,
  1471. MP_ERROR_TEXT("memory allocation failed, allocating %u bytes"), (uint)num_bytes);
  1472. }
  1473. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
  1474. NORETURN void mp_raise_type(const mp_obj_type_t *exc_type) {
  1475. nlr_raise(mp_obj_new_exception(exc_type));
  1476. }
  1477. NORETURN void mp_raise_ValueError_no_msg(void) {
  1478. mp_raise_type(&mp_type_ValueError);
  1479. }
  1480. NORETURN void mp_raise_TypeError_no_msg(void) {
  1481. mp_raise_type(&mp_type_TypeError);
  1482. }
  1483. NORETURN void mp_raise_NotImplementedError_no_msg(void) {
  1484. mp_raise_type(&mp_type_NotImplementedError);
  1485. }
  1486. #else
  1487. NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg) {
  1488. if (msg == NULL) {
  1489. nlr_raise(mp_obj_new_exception(exc_type));
  1490. } else {
  1491. nlr_raise(mp_obj_new_exception_msg(exc_type, msg));
  1492. }
  1493. }
  1494. NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...) {
  1495. va_list args;
  1496. va_start(args, fmt);
  1497. mp_obj_t exc = mp_obj_new_exception_msg_vlist(exc_type, fmt, args);
  1498. va_end(args);
  1499. nlr_raise(exc);
  1500. }
  1501. NORETURN void mp_raise_ValueError(mp_rom_error_text_t msg) {
  1502. mp_raise_msg(&mp_type_ValueError, msg);
  1503. }
  1504. NORETURN void mp_raise_TypeError(mp_rom_error_text_t msg) {
  1505. mp_raise_msg(&mp_type_TypeError, msg);
  1506. }
  1507. NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg) {
  1508. mp_raise_msg(&mp_type_NotImplementedError, msg);
  1509. }
  1510. #endif
  1511. NORETURN void mp_raise_type_arg(const mp_obj_type_t *exc_type, mp_obj_t arg) {
  1512. nlr_raise(mp_obj_new_exception_arg1(exc_type, arg));
  1513. }
  1514. NORETURN void mp_raise_StopIteration(mp_obj_t arg) {
  1515. if (arg == MP_OBJ_NULL) {
  1516. mp_raise_type(&mp_type_StopIteration);
  1517. } else {
  1518. mp_raise_type_arg(&mp_type_StopIteration, arg);
  1519. }
  1520. }
  1521. NORETURN void mp_raise_TypeError_int_conversion(mp_const_obj_t arg) {
  1522. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  1523. (void)arg;
  1524. mp_raise_TypeError(MP_ERROR_TEXT("can't convert to int"));
  1525. #else
  1526. mp_raise_msg_varg(&mp_type_TypeError,
  1527. MP_ERROR_TEXT("can't convert %s to int"), mp_obj_get_type_str(arg));
  1528. #endif
  1529. }
  1530. NORETURN void mp_raise_OSError(int errno_) {
  1531. mp_raise_type_arg(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_));
  1532. }
  1533. NORETURN void mp_raise_OSError_with_filename(int errno_, const char *filename) {
  1534. vstr_t vstr;
  1535. vstr_init(&vstr, 32);
  1536. vstr_printf(&vstr, "can't open %s", filename);
  1537. mp_obj_t o_str = mp_obj_new_str_from_vstr(&vstr);
  1538. mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(errno_), MP_OBJ_FROM_PTR(o_str)};
  1539. nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args));
  1540. }
  1541. #if MICROPY_STACK_CHECK || MICROPY_ENABLE_PYSTACK
  1542. NORETURN void mp_raise_recursion_depth(void) {
  1543. mp_raise_type_arg(&mp_type_RuntimeError, MP_OBJ_NEW_QSTR(MP_QSTR_maximum_space_recursion_space_depth_space_exceeded));
  1544. }
  1545. #endif