runtime.c 56 KB

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