blob: 8b4420926ce1bb34d68092bf11d0238cbb1cfafc [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python 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
Damien429d7192013-10-04 19:53:11 +010027#include <stdio.h>
28#include <string.h>
29#include <assert.h>
30
Damien Georgeb4b10fd2015-01-01 23:30:53 +000031#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/nlr.h"
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/parsenum.h"
34#include "py/compile.h"
Damien Georgec2a4e4e2015-05-11 12:25:19 +000035#include "py/objstr.h"
Damien George51dfcb42015-01-01 20:27:54 +000036#include "py/objtuple.h"
37#include "py/objlist.h"
38#include "py/objmodule.h"
39#include "py/objgenerator.h"
40#include "py/smallint.h"
41#include "py/runtime0.h"
42#include "py/runtime.h"
43#include "py/builtin.h"
44#include "py/stackctrl.h"
45#include "py/gc.h"
Damien660365e2013-12-17 18:27:24 +000046
Damien7f5dacf2013-10-10 11:24:39 +010047#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010048#define DEBUG_PRINT (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020049#define DEBUG_printf DEBUG_printf
Damien George41eb6082014-02-26 22:40:35 +000050#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
Damien7f5dacf2013-10-10 11:24:39 +010051#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000052#define DEBUG_printf(...) (void)0
53#define DEBUG_OP_printf(...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010054#endif
Damien429d7192013-10-04 19:53:11 +010055
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030056const mp_obj_module_t mp_module___main__ = {
57 .base = { &mp_type_module },
Damien Georgeb4b10fd2015-01-01 23:30:53 +000058 .globals = (mp_obj_dict_t*)&MP_STATE_VM(dict_main),
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030059};
60
Damien Georged17926d2014-03-30 13:35:08 +010061void mp_init(void) {
Damien George8dbbbbc2014-08-04 10:05:16 +010062 qstr_init();
Paul Sokolovsky8a96ebe2014-06-27 20:54:22 +030063
Damien George124df6f2014-10-25 18:19:55 +010064 // no pending exceptions to start with
Damien Georgeb4b10fd2015-01-01 23:30:53 +000065 MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
Damien George124df6f2014-10-25 18:19:55 +010066
Damien George8dbbbbc2014-08-04 10:05:16 +010067#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
68 mp_init_emergency_exception_buf();
69#endif
70
Damien George7f1da0a2016-12-15 13:00:19 +110071 #if MICROPY_KBD_EXCEPTION
72 // initialise the exception object for raising KeyboardInterrupt
73 MP_STATE_VM(mp_kbd_exception).base.type = &mp_type_KeyboardInterrupt;
74 MP_STATE_VM(mp_kbd_exception).traceback_alloc = 0;
75 MP_STATE_VM(mp_kbd_exception).traceback_len = 0;
76 MP_STATE_VM(mp_kbd_exception).traceback_data = NULL;
77 MP_STATE_VM(mp_kbd_exception).args = mp_const_empty_tuple;
78 #endif
79
Damien George97f9a282014-05-12 23:07:34 +010080 // call port specific initialization if any
stijn72521a12014-05-03 11:28:29 +020081#ifdef MICROPY_PORT_INIT_FUNC
82 MICROPY_PORT_INIT_FUNC;
83#endif
84
Paul Sokolovskyd3439d02014-06-02 19:37:55 +030085 // optimization disabled by default
Damien Georgeb4b10fd2015-01-01 23:30:53 +000086 MP_STATE_VM(mp_optimise_value) = 0;
Damien George97f9a282014-05-12 23:07:34 +010087
Damien Georgecaac5422014-03-25 14:18:18 +000088 // init global module stuff
89 mp_module_init();
Damien George0d028742014-01-22 23:59:20 +000090
Damien George7efc5b32014-04-05 22:36:42 +010091 // initialise the __main__ module
Damien Georgeb4b10fd2015-01-01 23:30:53 +000092 mp_obj_dict_init(&MP_STATE_VM(dict_main), 1);
Damien George999cedb2015-11-27 17:01:44 +000093 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__));
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030094
95 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien Georgeb4b10fd2015-01-01 23:30:53 +000096 MP_STATE_CTX(dict_locals) = MP_STATE_CTX(dict_globals) = &MP_STATE_VM(dict_main);
Damien George78d702c2014-12-09 16:19:48 +000097
98 #if MICROPY_CAN_OVERRIDE_BUILTINS
99 // start with no extensions to builtins
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000100 MP_STATE_VM(mp_module_builtins_override_dict) = NULL;
Damien George78d702c2014-12-09 16:19:48 +0000101 #endif
Damien George4cec63a2016-05-26 10:42:53 +0000102
Damien Georgee8f2db72016-12-14 11:40:11 +1100103 #if MICROPY_FSUSERMOUNT
104 // zero out the pointers to the user-mounted devices
105 memset(MP_STATE_VM(fs_user_mount), 0, sizeof(MP_STATE_VM(fs_user_mount)));
106 #endif
107
Damien George4cec63a2016-05-26 10:42:53 +0000108 #if MICROPY_PY_THREAD_GIL
109 mp_thread_mutex_init(&MP_STATE_VM(gil_mutex));
110 #endif
111
112 MP_THREAD_GIL_ENTER();
Damien429d7192013-10-04 19:53:11 +0100113}
114
Damien Georged17926d2014-03-30 13:35:08 +0100115void mp_deinit(void) {
Damien George7efc5b32014-04-05 22:36:42 +0100116 //mp_obj_dict_free(&dict_main);
Damien George2326d522014-03-27 23:26:35 +0000117 mp_module_deinit();
stijn5ed284a2014-05-08 10:56:33 +0200118
119 // call port specific deinitialization if any
120#ifdef MICROPY_PORT_INIT_FUNC
121 MICROPY_PORT_DEINIT_FUNC;
122#endif
Damien429d7192013-10-04 19:53:11 +0100123}
124
Damien George50912e72015-01-20 11:55:10 +0000125mp_obj_t mp_load_name(qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100126 // logic: search locals, globals, builtins
Damien George50912e72015-01-20 11:55:10 +0000127 DEBUG_OP_printf("load name %s\n", qstr_str(qst));
Paul Sokolovskya0d32992014-04-05 04:51:26 +0300128 // If we're at the outer scope (locals == globals), dispatch to load_global right away
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000129 if (MP_STATE_CTX(dict_locals) != MP_STATE_CTX(dict_globals)) {
Damien George50912e72015-01-20 11:55:10 +0000130 mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_CTX(dict_locals)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
Paul Sokolovskya0d32992014-04-05 04:51:26 +0300131 if (elem != NULL) {
132 return elem->value;
133 }
Damiena3977762013-10-09 23:10:10 +0100134 }
Damien George50912e72015-01-20 11:55:10 +0000135 return mp_load_global(qst);
Damiena3977762013-10-09 23:10:10 +0100136}
137
Damien George50912e72015-01-20 11:55:10 +0000138mp_obj_t mp_load_global(qstr qst) {
Damiena3977762013-10-09 23:10:10 +0100139 // logic: search globals, builtins
Damien George50912e72015-01-20 11:55:10 +0000140 DEBUG_OP_printf("load global %s\n", qstr_str(qst));
141 mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_CTX(dict_globals)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100142 if (elem == NULL) {
Damien George78d702c2014-12-09 16:19:48 +0000143 #if MICROPY_CAN_OVERRIDE_BUILTINS
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000144 if (MP_STATE_VM(mp_module_builtins_override_dict) != NULL) {
Damien George78d702c2014-12-09 16:19:48 +0000145 // lookup in additional dynamic table of builtins first
Damien George50912e72015-01-20 11:55:10 +0000146 elem = mp_map_lookup(&MP_STATE_VM(mp_module_builtins_override_dict)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
Damien George78d702c2014-12-09 16:19:48 +0000147 if (elem != NULL) {
148 return elem->value;
149 }
150 }
151 #endif
Damien George50912e72015-01-20 11:55:10 +0000152 elem = mp_map_lookup((mp_map_t*)&mp_module_builtins_globals.map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100153 if (elem == NULL) {
Damien George1e9a92f2014-11-06 17:36:16 +0000154 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100155 mp_raise_msg(&mp_type_NameError, "name not defined");
Damien George1e9a92f2014-11-06 17:36:16 +0000156 } else {
157 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NameError,
Damien George044c4732015-04-11 13:03:37 +0100158 "name '%q' is not defined", qst));
Damien George1e9a92f2014-11-06 17:36:16 +0000159 }
Damien429d7192013-10-04 19:53:11 +0100160 }
161 }
162 return elem->value;
163}
164
Damien Georged17926d2014-03-30 13:35:08 +0100165mp_obj_t mp_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100166 DEBUG_OP_printf("load_build_class\n");
Damien George78d702c2014-12-09 16:19:48 +0000167 #if MICROPY_CAN_OVERRIDE_BUILTINS
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000168 if (MP_STATE_VM(mp_module_builtins_override_dict) != NULL) {
Damien George78d702c2014-12-09 16:19:48 +0000169 // lookup in additional dynamic table of builtins first
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000170 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);
Damien George78d702c2014-12-09 16:19:48 +0000171 if (elem != NULL) {
172 return elem->value;
173 }
174 }
175 #endif
Damien George999cedb2015-11-27 17:01:44 +0000176 return MP_OBJ_FROM_PTR(&mp_builtin___build_class___obj);
Damien429d7192013-10-04 19:53:11 +0100177}
178
Damien George50912e72015-01-20 11:55:10 +0000179void mp_store_name(qstr qst, mp_obj_t obj) {
180 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qst), obj);
Damien George999cedb2015-11-27 17:01:44 +0000181 mp_obj_dict_store(MP_OBJ_FROM_PTR(MP_STATE_CTX(dict_locals)), MP_OBJ_NEW_QSTR(qst), obj);
Damiena3977762013-10-09 23:10:10 +0100182}
183
Damien George50912e72015-01-20 11:55:10 +0000184void mp_delete_name(qstr qst) {
185 DEBUG_OP_printf("delete name %s\n", qstr_str(qst));
186 // TODO convert KeyError to NameError if qst not found
Damien George999cedb2015-11-27 17:01:44 +0000187 mp_obj_dict_delete(MP_OBJ_FROM_PTR(MP_STATE_CTX(dict_locals)), MP_OBJ_NEW_QSTR(qst));
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200188}
189
Damien George50912e72015-01-20 11:55:10 +0000190void mp_store_global(qstr qst, mp_obj_t obj) {
191 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qst), obj);
Damien George999cedb2015-11-27 17:01:44 +0000192 mp_obj_dict_store(MP_OBJ_FROM_PTR(MP_STATE_CTX(dict_globals)), MP_OBJ_NEW_QSTR(qst), obj);
Damien429d7192013-10-04 19:53:11 +0100193}
194
Damien George50912e72015-01-20 11:55:10 +0000195void mp_delete_global(qstr qst) {
196 DEBUG_OP_printf("delete global %s\n", qstr_str(qst));
197 // TODO convert KeyError to NameError if qst not found
Damien George999cedb2015-11-27 17:01:44 +0000198 mp_obj_dict_delete(MP_OBJ_FROM_PTR(MP_STATE_CTX(dict_globals)), MP_OBJ_NEW_QSTR(qst));
Damien George1d24ea52014-04-08 21:11:49 +0100199}
200
Damien George4abff752014-08-30 14:59:21 +0100201mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
Damien Georgeeaaebf32014-09-23 10:59:05 +0100202 DEBUG_OP_printf("unary " UINT_FMT " %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000203
Damien Georgebdbe8c92015-12-08 12:28:11 +0000204 if (op == MP_UNARY_OP_NOT) {
205 // "not x" is the negative of whether "x" is true per Python semantics
206 return mp_obj_new_bool(mp_obj_is_true(arg) == 0);
207 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
Damien George40f3c022014-07-03 13:25:24 +0100208 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000209 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100210 case MP_UNARY_OP_BOOL:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300211 return mp_obj_new_bool(val != 0);
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000212 case MP_UNARY_OP_HASH:
213 return arg;
Damien Georged17926d2014-03-30 13:35:08 +0100214 case MP_UNARY_OP_POSITIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000215 return arg;
Damien Georged17926d2014-03-30 13:35:08 +0100216 case MP_UNARY_OP_NEGATIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000217 // check for overflow
218 if (val == MP_SMALL_INT_MIN) {
219 return mp_obj_new_int(-val);
220 } else {
221 return MP_OBJ_NEW_SMALL_INT(-val);
222 }
Damien Georged17926d2014-03-30 13:35:08 +0100223 case MP_UNARY_OP_INVERT:
Damien George9d68e9c2014-03-12 15:38:15 +0000224 return MP_OBJ_NEW_SMALL_INT(~val);
225 default:
226 assert(0);
227 return arg;
Damien7410e442013-11-02 19:47:57 +0000228 }
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000229 } else if (op == MP_UNARY_OP_HASH && MP_OBJ_IS_STR_OR_BYTES(arg)) {
230 // fast path for hashing str/bytes
231 GET_STR_HASH(arg, h);
Damien George5f3bda42016-09-02 14:42:53 +1000232 if (h == 0) {
233 GET_STR_DATA_LEN(arg, data, len);
234 h = qstr_compute_hash(data, len);
235 }
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000236 return MP_OBJ_NEW_SMALL_INT(h);
Damien George1e708fe2014-01-23 18:27:51 +0000237 } else {
238 mp_obj_type_t *type = mp_obj_get_type(arg);
239 if (type->unary_op != NULL) {
240 mp_obj_t result = type->unary_op(op, arg);
Damien George6ac5dce2014-05-21 19:42:43 +0100241 if (result != MP_OBJ_NULL) {
Damiend99b0522013-12-21 18:17:45 +0000242 return result;
243 }
Damien7410e442013-11-02 19:47:57 +0000244 }
Damien George1e9a92f2014-11-06 17:36:16 +0000245 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100246 mp_raise_msg(&mp_type_TypeError, "unsupported type for operator");
Damien George1e9a92f2014-11-06 17:36:16 +0000247 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000248 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George044c4732015-04-11 13:03:37 +0100249 "unsupported type for %q: '%s'",
250 mp_unary_op_method_name[op], mp_obj_get_type_str(arg)));
Damien George1e9a92f2014-11-06 17:36:16 +0000251 }
Damien7410e442013-11-02 19:47:57 +0000252 }
Damien429d7192013-10-04 19:53:11 +0100253}
254
Damien George4abff752014-08-30 14:59:21 +0100255mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
Damien Georgeeaaebf32014-09-23 10:59:05 +0100256 DEBUG_OP_printf("binary " UINT_FMT " %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000257
258 // TODO correctly distinguish inplace operators for mutable objects
259 // lookup logic that CPython uses for +=:
260 // check for implemented +=
261 // then check for implemented +
262 // then check for implemented seq.inplace_concat
263 // then check for implemented seq.concat
264 // then fail
265 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
266
Damien George9aa2a522014-02-01 23:04:09 +0000267 // deal with is
Damien Georged17926d2014-03-30 13:35:08 +0100268 if (op == MP_BINARY_OP_IS) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300269 return mp_obj_new_bool(lhs == rhs);
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200270 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200271
Damien Georgebcbeea02014-01-11 10:47:22 +0000272 // deal with == and != for all types
Damien Georged17926d2014-03-30 13:35:08 +0100273 if (op == MP_BINARY_OP_EQUAL || op == MP_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000274 if (mp_obj_equal(lhs, rhs)) {
Damien Georged17926d2014-03-30 13:35:08 +0100275 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000276 return mp_const_true;
277 } else {
278 return mp_const_false;
279 }
280 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100281 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000282 return mp_const_false;
283 } else {
284 return mp_const_true;
285 }
286 }
287 }
288
289 // deal with exception_match for all types
Damien Georged17926d2014-03-30 13:35:08 +0100290 if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000291 // rhs must be issubclass(rhs, BaseException)
292 if (mp_obj_is_exception_type(rhs)) {
Damien George4bcd04b2014-09-24 14:05:40 +0100293 if (mp_obj_exception_match(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000294 return mp_const_true;
295 } else {
296 return mp_const_false;
297 }
Damien George4bcd04b2014-09-24 14:05:40 +0100298 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_tuple)) {
Damien George999cedb2015-11-27 17:01:44 +0000299 mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(rhs);
Damien George4bcd04b2014-09-24 14:05:40 +0100300 for (mp_uint_t i = 0; i < tuple->len; i++) {
301 rhs = tuple->items[i];
302 if (!mp_obj_is_exception_type(rhs)) {
303 goto unsupported_op;
304 }
305 if (mp_obj_exception_match(lhs, rhs)) {
306 return mp_const_true;
307 }
308 }
309 return mp_const_false;
Damien Georgebcbeea02014-01-11 10:47:22 +0000310 }
Damien George4bcd04b2014-09-24 14:05:40 +0100311 goto unsupported_op;
Damien Georgebcbeea02014-01-11 10:47:22 +0000312 }
313
Damien George1a9951d2014-01-06 22:13:00 +0000314 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damien George40f3c022014-07-03 13:25:24 +0100315 mp_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000316 if (MP_OBJ_IS_SMALL_INT(rhs)) {
Damien George40f3c022014-07-03 13:25:24 +0100317 mp_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000318 // This is a binary operation: lhs_val op rhs_val
319 // We need to be careful to handle overflow; see CERT INT32-C
320 // Operations that can overflow:
Damien George40f3c022014-07-03 13:25:24 +0100321 // + result always fits in mp_int_t, then handled by SMALL_INT check
322 // - result always fits in mp_int_t, then handled by SMALL_INT check
Damien George9d68e9c2014-03-12 15:38:15 +0000323 // * checked explicitly
Damien George40f3c022014-07-03 13:25:24 +0100324 // / if lhs=MIN and rhs=-1; result always fits in mp_int_t, then handled by SMALL_INT check
325 // % if lhs=MIN and rhs=-1; result always fits in mp_int_t, then handled by SMALL_INT check
Damien George9d68e9c2014-03-12 15:38:15 +0000326 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000327 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100328 case MP_BINARY_OP_OR:
329 case MP_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
330 case MP_BINARY_OP_XOR:
331 case MP_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
332 case MP_BINARY_OP_AND:
333 case MP_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
334 case MP_BINARY_OP_LSHIFT:
335 case MP_BINARY_OP_INPLACE_LSHIFT: {
Damien George9d68e9c2014-03-12 15:38:15 +0000336 if (rhs_val < 0) {
337 // negative shift not allowed
Damien George7d0d7212016-10-17 12:17:37 +1100338 mp_raise_msg(&mp_type_ValueError, "negative shift count");
Damien George963a5a32015-01-16 17:47:07 +0000339 } 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)) {
Damien George9d68e9c2014-03-12 15:38:15 +0000340 // left-shift will overflow, so use higher precision integer
341 lhs = mp_obj_new_int_from_ll(lhs_val);
342 goto generic_binary_op;
343 } else {
344 // use standard precision
345 lhs_val <<= rhs_val;
346 }
347 break;
348 }
Damien Georged17926d2014-03-30 13:35:08 +0100349 case MP_BINARY_OP_RSHIFT:
350 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000351 if (rhs_val < 0) {
352 // negative shift not allowed
Damien George7d0d7212016-10-17 12:17:37 +1100353 mp_raise_msg(&mp_type_ValueError, "negative shift count");
Damien George9d68e9c2014-03-12 15:38:15 +0000354 } else {
355 // standard precision is enough for right-shift
Damien George963a5a32015-01-16 17:47:07 +0000356 if (rhs_val >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200357 // Shifting to big amounts is underfined behavior
358 // in C and is CPU-dependent; propagate sign bit.
359 rhs_val = BITS_PER_WORD - 1;
360 }
Damien George9d68e9c2014-03-12 15:38:15 +0000361 lhs_val >>= rhs_val;
362 }
363 break;
Damien Georged17926d2014-03-30 13:35:08 +0100364 case MP_BINARY_OP_ADD:
365 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
366 case MP_BINARY_OP_SUBTRACT:
367 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
368 case MP_BINARY_OP_MULTIPLY:
369 case MP_BINARY_OP_INPLACE_MULTIPLY: {
Damien George9d68e9c2014-03-12 15:38:15 +0000370
Damien George40f3c022014-07-03 13:25:24 +0100371 // If long long type exists and is larger than mp_int_t, then
Damien George9d68e9c2014-03-12 15:38:15 +0000372 // we can use the following code to perform overflow-checked multiplication.
Damien Georgeecf5b772014-04-04 11:13:51 +0000373 // Otherwise (eg in x64 case) we must use mp_small_int_mul_overflow.
Damien George9d68e9c2014-03-12 15:38:15 +0000374 #if 0
375 // compute result using long long precision
376 long long res = (long long)lhs_val * (long long)rhs_val;
377 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
378 // result overflowed SMALL_INT, so return higher precision integer
379 return mp_obj_new_int_from_ll(res);
380 } else {
381 // use standard precision
Damien George40f3c022014-07-03 13:25:24 +0100382 lhs_val = (mp_int_t)res;
Damien George9d68e9c2014-03-12 15:38:15 +0000383 }
384 #endif
385
Damien Georgeecf5b772014-04-04 11:13:51 +0000386 if (mp_small_int_mul_overflow(lhs_val, rhs_val)) {
387 // use higher precision
388 lhs = mp_obj_new_int_from_ll(lhs_val);
389 goto generic_binary_op;
390 } else {
391 // use standard precision
392 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
393 }
Damien George9d68e9c2014-03-12 15:38:15 +0000394 break;
395 }
Damien Georged17926d2014-03-30 13:35:08 +0100396 case MP_BINARY_OP_FLOOR_DIVIDE:
397 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300398 if (rhs_val == 0) {
399 goto zero_division;
400 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000401 lhs_val = mp_small_int_floor_divide(lhs_val, rhs_val);
Rachel Dowdall56402792014-03-22 20:19:24 +0000402 break;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300403
Damien Georgefb510b32014-06-01 13:32:54 +0100404 #if MICROPY_PY_BUILTINS_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100405 case MP_BINARY_OP_TRUE_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300406 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
407 if (rhs_val == 0) {
Damien George70f33cd2014-04-02 17:06:05 +0100408 goto zero_division;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300409 }
410 return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000411 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000412
Damien Georged17926d2014-03-30 13:35:08 +0100413 case MP_BINARY_OP_MODULO:
Damien Georgeecf5b772014-04-04 11:13:51 +0000414 case MP_BINARY_OP_INPLACE_MODULO: {
Damien Georgee5635f42015-10-01 22:48:48 +0100415 if (rhs_val == 0) {
416 goto zero_division;
417 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000418 lhs_val = mp_small_int_modulo(lhs_val, rhs_val);
Rachel Dowdallcde86312014-03-22 17:29:27 +0000419 break;
420 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000421
Damien Georged17926d2014-03-30 13:35:08 +0100422 case MP_BINARY_OP_POWER:
423 case MP_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000424 if (rhs_val < 0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100425 #if MICROPY_PY_BUILTINS_FLOAT
Damien George9d68e9c2014-03-12 15:38:15 +0000426 lhs = mp_obj_new_float(lhs_val);
427 goto generic_binary_op;
428 #else
Damien George7d0d7212016-10-17 12:17:37 +1100429 mp_raise_msg(&mp_type_ValueError, "negative power with no float support");
Damien George9d68e9c2014-03-12 15:38:15 +0000430 #endif
431 } else {
Damien George40f3c022014-07-03 13:25:24 +0100432 mp_int_t ans = 1;
Damien George9d68e9c2014-03-12 15:38:15 +0000433 while (rhs_val > 0) {
434 if (rhs_val & 1) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000435 if (mp_small_int_mul_overflow(ans, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100436 goto power_overflow;
437 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000438 ans *= lhs_val;
Damien George9d68e9c2014-03-12 15:38:15 +0000439 }
Damien George5bf565e2014-04-04 00:16:32 +0100440 if (rhs_val == 1) {
441 break;
442 }
Damien George9d68e9c2014-03-12 15:38:15 +0000443 rhs_val /= 2;
Damien Georgeecf5b772014-04-04 11:13:51 +0000444 if (mp_small_int_mul_overflow(lhs_val, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100445 goto power_overflow;
446 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000447 lhs_val *= lhs_val;
Damien George1a9951d2014-01-06 22:13:00 +0000448 }
Damien George9d68e9c2014-03-12 15:38:15 +0000449 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000450 }
Damien George1a9951d2014-01-06 22:13:00 +0000451 break;
Damien George5bf565e2014-04-04 00:16:32 +0100452
453 power_overflow:
454 // use higher precision
455 lhs = mp_obj_new_int_from_ll(MP_OBJ_SMALL_INT_VALUE(lhs));
456 goto generic_binary_op;
457
Damien Georgec5029bc2015-06-13 22:00:10 +0100458 case MP_BINARY_OP_DIVMOD: {
459 if (rhs_val == 0) {
460 goto zero_division;
461 }
462 // to reduce stack usage we don't pass a temp array of the 2 items
Damien George999cedb2015-11-27 17:01:44 +0000463 mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
Damien Georgec5029bc2015-06-13 22:00:10 +0100464 tuple->items[0] = MP_OBJ_NEW_SMALL_INT(mp_small_int_floor_divide(lhs_val, rhs_val));
465 tuple->items[1] = MP_OBJ_NEW_SMALL_INT(mp_small_int_modulo(lhs_val, rhs_val));
Damien George999cedb2015-11-27 17:01:44 +0000466 return MP_OBJ_FROM_PTR(tuple);
Damien Georgec5029bc2015-06-13 22:00:10 +0100467 }
468
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300469 case MP_BINARY_OP_LESS: return mp_obj_new_bool(lhs_val < rhs_val); break;
470 case MP_BINARY_OP_MORE: return mp_obj_new_bool(lhs_val > rhs_val); break;
471 case MP_BINARY_OP_LESS_EQUAL: return mp_obj_new_bool(lhs_val <= rhs_val); break;
472 case MP_BINARY_OP_MORE_EQUAL: return mp_obj_new_bool(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000473
Damien George8bcb9862014-04-17 16:26:50 +0100474 default:
475 goto unsupported_op;
Damien George1a9951d2014-01-06 22:13:00 +0000476 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200477 // TODO: We just should make mp_obj_new_int() inline and use that
Damien Georged1e355e2014-05-28 14:51:12 +0100478 if (MP_SMALL_INT_FITS(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000479 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000480 } else {
481 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000482 }
Damien Georgefb510b32014-06-01 13:32:54 +0100483#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeaaef1852015-08-20 23:30:12 +0100484 } else if (mp_obj_is_float(rhs)) {
Damien Georgeae491052014-04-10 20:08:11 +0100485 mp_obj_t res = mp_obj_float_binary_op(op, lhs_val, rhs);
486 if (res == MP_OBJ_NULL) {
487 goto unsupported_op;
488 } else {
489 return res;
490 }
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300491#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0c36da02014-03-08 15:24:39 +0000492 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien Georgeae491052014-04-10 20:08:11 +0100493 mp_obj_t res = mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
494 if (res == MP_OBJ_NULL) {
495 goto unsupported_op;
496 } else {
497 return res;
498 }
Damien George3f759b72014-01-31 00:42:12 +0000499#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300500#endif
Damien429d7192013-10-04 19:53:11 +0100501 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000502 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000503
Damien George9aa2a522014-02-01 23:04:09 +0000504 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000505 *
506 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000507 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000508 */
Damien Georged17926d2014-03-30 13:35:08 +0100509 if (op == MP_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000510 mp_obj_type_t *type = mp_obj_get_type(rhs);
511 if (type->binary_op != NULL) {
512 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George6ac5dce2014-05-21 19:42:43 +0100513 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000514 return res;
515 }
516 }
517 if (type->getiter != NULL) {
518 /* second attempt, walk the iterator */
Damien Georged17926d2014-03-30 13:35:08 +0100519 mp_obj_t iter = mp_getiter(rhs);
Damien George3aa09f52014-10-23 12:06:53 +0100520 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100521 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien George5fa93b62014-01-22 14:35:10 +0000522 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000523 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000524 }
Damien7410e442013-11-02 19:47:57 +0000525 }
Damien George9aa2a522014-02-01 23:04:09 +0000526 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000527 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000528
Damien George1e9a92f2014-11-06 17:36:16 +0000529 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100530 mp_raise_msg(&mp_type_TypeError, "object not iterable");
Damien George1e9a92f2014-11-06 17:36:16 +0000531 } else {
532 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
533 "'%s' object is not iterable", mp_obj_get_type_str(rhs)));
534 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000535 }
536
Damien George5fa93b62014-01-22 14:35:10 +0000537 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000538 mp_obj_type_t *type;
539generic_binary_op:
540 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000541 if (type->binary_op != NULL) {
542 mp_obj_t result = type->binary_op(op, lhs, rhs);
Damien George6ac5dce2014-05-21 19:42:43 +0100543 if (result != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000544 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000545 }
Damien429d7192013-10-04 19:53:11 +0100546 }
Damiend99b0522013-12-21 18:17:45 +0000547
Damien George5fa93b62014-01-22 14:35:10 +0000548 // TODO implement dispatch for reverse binary ops
549
Damien Georgeae491052014-04-10 20:08:11 +0100550unsupported_op:
Damien George1e9a92f2014-11-06 17:36:16 +0000551 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100552 mp_raise_msg(&mp_type_TypeError, "unsupported type for operator");
Damien George1e9a92f2014-11-06 17:36:16 +0000553 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000554 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George044c4732015-04-11 13:03:37 +0100555 "unsupported types for %q: '%s', '%s'",
556 mp_binary_op_method_name[op], mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
Damien George1e9a92f2014-11-06 17:36:16 +0000557 }
Damien George70f33cd2014-04-02 17:06:05 +0100558
559zero_division:
Damien George7d0d7212016-10-17 12:17:37 +1100560 mp_raise_msg(&mp_type_ZeroDivisionError, "division by zero");
Damien429d7192013-10-04 19:53:11 +0100561}
562
Damien Georged17926d2014-03-30 13:35:08 +0100563mp_obj_t mp_call_function_0(mp_obj_t fun) {
564 return mp_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100565}
566
Damien Georged17926d2014-03-30 13:35:08 +0100567mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg) {
568 return mp_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100569}
570
Damien Georged17926d2014-03-30 13:35:08 +0100571mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
Damiend99b0522013-12-21 18:17:45 +0000572 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000573 args[0] = arg1;
574 args[1] = arg2;
Damien Georged17926d2014-03-30 13:35:08 +0100575 return mp_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100576}
577
Damien George20006db2014-01-18 14:10:48 +0000578// args contains, eg: arg0 arg1 key0 value0 key1 value1
Damien George4abff752014-08-30 14:59:21 +0100579mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damiend99b0522013-12-21 18:17:45 +0000580 // TODO improve this: fun object can specify its type and we parse here the arguments,
581 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100582
Damien Georgeeaaebf32014-09-23 10:59:05 +0100583 DEBUG_OP_printf("calling function %p(n_args=" UINT_FMT ", n_kw=" UINT_FMT ", args=%p)\n", fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000584
Damien George8b56beb2014-01-31 23:49:49 +0000585 // get the type
586 mp_obj_type_t *type = mp_obj_get_type(fun_in);
587
588 // do the call
589 if (type->call != NULL) {
Damien George3f522622014-06-03 13:40:16 +0100590 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000591 }
Paul Sokolovsky755565d2014-04-25 21:15:16 +0300592
Damien George1e9a92f2014-11-06 17:36:16 +0000593 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100594 mp_raise_msg(&mp_type_TypeError, "object not callable");
Damien George1e9a92f2014-11-06 17:36:16 +0000595 } else {
596 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
597 "'%s' object is not callable", mp_obj_get_type_str(fun_in)));
598 }
Damien86c7fc72013-11-26 15:16:41 +0000599}
600
Damien George20006db2014-01-18 14:10:48 +0000601// 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)
602// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damien George4abff752014-08-30 14:59:21 +0100603mp_obj_t mp_call_method_n_kw(mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien Georgeeaaebf32014-09-23 10:59:05 +0100604 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);
Damien George999cedb2015-11-27 17:01:44 +0000605 int adjust = (args[1] == MP_OBJ_NULL) ? 0 : 1;
Damien Georged17926d2014-03-30 13:35:08 +0100606 return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000607}
608
Damien George12a5e172015-04-01 23:31:30 +0100609// This function only needs to be exposed externally when in stackless mode.
610#if !MICROPY_STACKLESS
611STATIC
612#endif
613void mp_call_prepare_args_n_kw_var(bool have_self, mp_uint_t n_args_n_kw, const mp_obj_t *args, mp_call_args_t *out_args) {
Damien George230fec72014-03-30 21:21:24 +0100614 mp_obj_t fun = *args++;
615 mp_obj_t self = MP_OBJ_NULL;
616 if (have_self) {
617 self = *args++; // may be MP_OBJ_NULL
618 }
619 uint n_args = n_args_n_kw & 0xff;
620 uint n_kw = (n_args_n_kw >> 8) & 0xff;
Paul Sokolovskye104acd2015-03-03 21:37:37 +0200621 mp_obj_t pos_seq = args[n_args + 2 * n_kw]; // may be MP_OBJ_NULL
622 mp_obj_t kw_dict = args[n_args + 2 * n_kw + 1]; // may be MP_OBJ_NULL
Damien George230fec72014-03-30 21:21:24 +0100623
624 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);
625
626 // We need to create the following array of objects:
627 // args[0 .. n_args] unpacked(pos_seq) args[n_args .. n_args + 2 * n_kw] unpacked(kw_dict)
628 // TODO: optimize one day to avoid constructing new arg array? Will be hard.
629
630 // The new args array
631 mp_obj_t *args2;
632 uint args2_alloc;
633 uint args2_len = 0;
634
635 // Try to get a hint for the size of the kw_dict
636 uint kw_dict_len = 0;
637 if (kw_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
638 kw_dict_len = mp_obj_dict_len(kw_dict);
639 }
640
641 // Extract the pos_seq sequence to the new args array.
642 // Note that it can be arbitrary iterator.
643 if (pos_seq == MP_OBJ_NULL) {
644 // no sequence
645
646 // allocate memory for the new array of args
647 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len);
648 args2 = m_new(mp_obj_t, args2_alloc);
649
650 // copy the self
651 if (self != MP_OBJ_NULL) {
652 args2[args2_len++] = self;
653 }
654
655 // copy the fixed pos args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300656 mp_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100657 args2_len += n_args;
658
659 } else if (MP_OBJ_IS_TYPE(pos_seq, &mp_type_tuple) || MP_OBJ_IS_TYPE(pos_seq, &mp_type_list)) {
660 // optimise the case of a tuple and list
661
662 // get the items
Damien George9c4cbe22014-08-30 14:04:14 +0100663 mp_uint_t len;
Damien George230fec72014-03-30 21:21:24 +0100664 mp_obj_t *items;
665 mp_obj_get_array(pos_seq, &len, &items);
666
667 // allocate memory for the new array of args
668 args2_alloc = 1 + n_args + len + 2 * (n_kw + kw_dict_len);
669 args2 = m_new(mp_obj_t, args2_alloc);
670
671 // copy the self
672 if (self != MP_OBJ_NULL) {
673 args2[args2_len++] = self;
674 }
675
676 // copy the fixed and variable position args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300677 mp_seq_cat(args2 + args2_len, args, n_args, items, len, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100678 args2_len += n_args + len;
679
680 } else {
681 // generic iterator
682
683 // allocate memory for the new array of args
684 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len) + 3;
685 args2 = m_new(mp_obj_t, args2_alloc);
686
687 // copy the self
688 if (self != MP_OBJ_NULL) {
689 args2[args2_len++] = self;
690 }
691
692 // copy the fixed position args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300693 mp_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
Damien George7a996392015-12-03 17:59:49 +0000694 args2_len += n_args;
Damien George230fec72014-03-30 21:21:24 +0100695
696 // extract the variable position args from the iterator
697 mp_obj_t iterable = mp_getiter(pos_seq);
698 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100699 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George230fec72014-03-30 21:21:24 +0100700 if (args2_len >= args2_alloc) {
701 args2 = m_renew(mp_obj_t, args2, args2_alloc, args2_alloc * 2);
702 args2_alloc *= 2;
703 }
704 args2[args2_len++] = item;
705 }
706 }
707
708 // The size of the args2 array now is the number of positional args.
709 uint pos_args_len = args2_len;
710
711 // Copy the fixed kw args.
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300712 mp_seq_copy(args2 + args2_len, args + n_args, 2 * n_kw, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100713 args2_len += 2 * n_kw;
714
715 // Extract (key,value) pairs from kw_dict dictionary and append to args2.
716 // Note that it can be arbitrary iterator.
717 if (kw_dict == MP_OBJ_NULL) {
718 // pass
719 } else if (MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
720 // dictionary
721 mp_map_t *map = mp_obj_dict_get_map(kw_dict);
722 assert(args2_len + 2 * map->used <= args2_alloc); // should have enough, since kw_dict_len is in this case hinted correctly above
Damien Georgeb063b9b2014-12-21 16:24:09 +0000723 for (mp_uint_t i = 0; i < map->alloc; i++) {
724 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
Damien Georgefea40ad2016-04-21 16:51:36 +0100725 // the key must be a qstr, so intern it if it's a string
726 mp_obj_t key = map->table[i].key;
727 if (MP_OBJ_IS_TYPE(key, &mp_type_str)) {
728 key = mp_obj_str_intern(key);
729 }
730 args2[args2_len++] = key;
Damien George230fec72014-03-30 21:21:24 +0100731 args2[args2_len++] = map->table[i].value;
732 }
733 }
734 } else {
Damien George470c4292016-05-07 22:02:46 +0100735 // generic mapping:
736 // - call keys() to get an iterable of all keys in the mapping
737 // - call __getitem__ for each key to get the corresponding value
738
739 // get the keys iterable
740 mp_obj_t dest[3];
741 mp_load_method(kw_dict, MP_QSTR_keys, dest);
Damien George230fec72014-03-30 21:21:24 +0100742 mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest));
Damien George470c4292016-05-07 22:02:46 +0100743
744 mp_obj_t key;
745 while ((key = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
746 // expand size of args array if needed
Damien George230fec72014-03-30 21:21:24 +0100747 if (args2_len + 1 >= args2_alloc) {
748 uint new_alloc = args2_alloc * 2;
749 if (new_alloc < 4) {
750 new_alloc = 4;
751 }
752 args2 = m_renew(mp_obj_t, args2, args2_alloc, new_alloc);
753 args2_alloc = new_alloc;
754 }
Damien George470c4292016-05-07 22:02:46 +0100755
Damien Georgefea40ad2016-04-21 16:51:36 +0100756 // the key must be a qstr, so intern it if it's a string
Damien Georgefea40ad2016-04-21 16:51:36 +0100757 if (MP_OBJ_IS_TYPE(key, &mp_type_str)) {
758 key = mp_obj_str_intern(key);
759 }
Damien George470c4292016-05-07 22:02:46 +0100760
761 // get the value corresponding to the key
762 mp_load_method(kw_dict, MP_QSTR___getitem__, dest);
763 dest[2] = key;
764 mp_obj_t value = mp_call_method_n_kw(1, 0, dest);
765
766 // store the key/value pair in the argument array
Damien Georgefea40ad2016-04-21 16:51:36 +0100767 args2[args2_len++] = key;
Damien George470c4292016-05-07 22:02:46 +0100768 args2[args2_len++] = value;
Damien George230fec72014-03-30 21:21:24 +0100769 }
770 }
771
Paul Sokolovskye6c6fe32015-03-28 01:14:45 +0200772 out_args->fun = fun;
773 out_args->args = args2;
774 out_args->n_args = pos_args_len;
775 out_args->n_kw = (args2_len - pos_args_len) / 2;
776 out_args->n_alloc = args2_alloc;
777}
778
779mp_obj_t mp_call_method_n_kw_var(bool have_self, mp_uint_t n_args_n_kw, const mp_obj_t *args) {
Damien George12a5e172015-04-01 23:31:30 +0100780 mp_call_args_t out_args;
Paul Sokolovskye6c6fe32015-03-28 01:14:45 +0200781 mp_call_prepare_args_n_kw_var(have_self, n_args_n_kw, args, &out_args);
782
783 mp_obj_t res = mp_call_function_n_kw(out_args.fun, out_args.n_args, out_args.n_kw, out_args.args);
784 m_del(mp_obj_t, out_args.args, out_args.n_alloc);
Damien George230fec72014-03-30 21:21:24 +0100785
786 return res;
787}
788
Damien George932bf1c2014-01-18 23:42:49 +0000789// unpacked items are stored in reverse order into the array pointed to by items
Damien George4abff752014-08-30 14:59:21 +0100790void mp_unpack_sequence(mp_obj_t seq_in, mp_uint_t num, mp_obj_t *items) {
Damien George9c4cbe22014-08-30 14:04:14 +0100791 mp_uint_t seq_len;
Damien George3e1a5c12014-03-29 13:43:38 +0000792 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
Damiend99b0522013-12-21 18:17:45 +0000793 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000794 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000795 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
796 } else {
797 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000798 }
Damiend99b0522013-12-21 18:17:45 +0000799 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200800 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000801 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200802 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000803 }
Damien George4abff752014-08-30 14:59:21 +0100804 for (mp_uint_t i = 0; i < num; i++) {
Damien George932bf1c2014-01-18 23:42:49 +0000805 items[i] = seq_items[num - 1 - i];
806 }
Damien86c7fc72013-11-26 15:16:41 +0000807 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100808 mp_obj_t iterable = mp_getiter(seq_in);
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200809
810 for (seq_len = 0; seq_len < num; seq_len++) {
Damien Georged17926d2014-03-30 13:35:08 +0100811 mp_obj_t el = mp_iternext(iterable);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100812 if (el == MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200813 goto too_short;
814 }
815 items[num - 1 - seq_len] = el;
816 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100817 if (mp_iternext(iterable) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200818 goto too_long;
819 }
Damien86c7fc72013-11-26 15:16:41 +0000820 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200821 return;
822
823too_short:
Damien George1e9a92f2014-11-06 17:36:16 +0000824 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100825 mp_raise_msg(&mp_type_ValueError, "wrong number of values to unpack");
Damien George1e9a92f2014-11-06 17:36:16 +0000826 } else {
827 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Damien George2a1cca22016-03-14 22:40:39 +0000828 "need more than %d values to unpack", (int)seq_len));
Damien George1e9a92f2014-11-06 17:36:16 +0000829 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200830too_long:
Damien George1e9a92f2014-11-06 17:36:16 +0000831 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100832 mp_raise_msg(&mp_type_ValueError, "wrong number of values to unpack");
Damien George1e9a92f2014-11-06 17:36:16 +0000833 } else {
834 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Damien George2a1cca22016-03-14 22:40:39 +0000835 "too many values to unpack (expected %d)", (int)num));
Damien George1e9a92f2014-11-06 17:36:16 +0000836 }
Damien86c7fc72013-11-26 15:16:41 +0000837}
838
Damien George495d7812014-04-08 17:51:47 +0100839// unpacked items are stored in reverse order into the array pointed to by items
Damien George4abff752014-08-30 14:59:21 +0100840void mp_unpack_ex(mp_obj_t seq_in, mp_uint_t num_in, mp_obj_t *items) {
841 mp_uint_t num_left = num_in & 0xff;
842 mp_uint_t num_right = (num_in >> 8) & 0xff;
Damien Georgeeaaebf32014-09-23 10:59:05 +0100843 DEBUG_OP_printf("unpack ex " UINT_FMT " " UINT_FMT "\n", num_left, num_right);
Damien George9c4cbe22014-08-30 14:04:14 +0100844 mp_uint_t seq_len;
Damien George495d7812014-04-08 17:51:47 +0100845 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
846 mp_obj_t *seq_items;
847 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
848 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
849 } else {
850 if (num_left == 0 && num_right == 0) {
851 // *a, = b # sets a to b if b is a list
852 items[0] = seq_in;
853 return;
854 }
855 mp_obj_list_get(seq_in, &seq_len, &seq_items);
856 }
857 if (seq_len < num_left + num_right) {
858 goto too_short;
859 }
Damien George4abff752014-08-30 14:59:21 +0100860 for (mp_uint_t i = 0; i < num_right; i++) {
Damien George495d7812014-04-08 17:51:47 +0100861 items[i] = seq_items[seq_len - 1 - i];
862 }
863 items[num_right] = mp_obj_new_list(seq_len - num_left - num_right, seq_items + num_left);
Damien George4abff752014-08-30 14:59:21 +0100864 for (mp_uint_t i = 0; i < num_left; i++) {
Damien George495d7812014-04-08 17:51:47 +0100865 items[num_right + 1 + i] = seq_items[num_left - 1 - i];
866 }
867 } else {
868 // Generic iterable; this gets a bit messy: we unpack known left length to the
869 // items destination array, then the rest to a dynamically created list. Once the
870 // iterable is exhausted, we take from this list for the right part of the items.
871 // TODO Improve to waste less memory in the dynamically created list.
872 mp_obj_t iterable = mp_getiter(seq_in);
873 mp_obj_t item;
874 for (seq_len = 0; seq_len < num_left; seq_len++) {
875 item = mp_iternext(iterable);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100876 if (item == MP_OBJ_STOP_ITERATION) {
Damien George495d7812014-04-08 17:51:47 +0100877 goto too_short;
878 }
879 items[num_left + num_right + 1 - 1 - seq_len] = item;
880 }
Damien George999cedb2015-11-27 17:01:44 +0000881 mp_obj_list_t *rest = MP_OBJ_TO_PTR(mp_obj_new_list(0, NULL));
Damien Georgeea8d06c2014-04-17 23:19:36 +0100882 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George999cedb2015-11-27 17:01:44 +0000883 mp_obj_list_append(MP_OBJ_FROM_PTR(rest), item);
Damien George495d7812014-04-08 17:51:47 +0100884 }
Damien Georgeca6d75f2014-08-30 15:17:47 +0100885 if (rest->len < num_right) {
Damien George495d7812014-04-08 17:51:47 +0100886 goto too_short;
887 }
Damien George999cedb2015-11-27 17:01:44 +0000888 items[num_right] = MP_OBJ_FROM_PTR(rest);
Damien George4abff752014-08-30 14:59:21 +0100889 for (mp_uint_t i = 0; i < num_right; i++) {
Damien Georgeca6d75f2014-08-30 15:17:47 +0100890 items[num_right - 1 - i] = rest->items[rest->len - num_right + i];
Damien George495d7812014-04-08 17:51:47 +0100891 }
Damien George999cedb2015-11-27 17:01:44 +0000892 mp_obj_list_set_len(MP_OBJ_FROM_PTR(rest), rest->len - num_right);
Damien George495d7812014-04-08 17:51:47 +0100893 }
894 return;
895
896too_short:
Damien George1e9a92f2014-11-06 17:36:16 +0000897 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100898 mp_raise_msg(&mp_type_ValueError, "wrong number of values to unpack");
Damien George1e9a92f2014-11-06 17:36:16 +0000899 } else {
900 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Damien George2a1cca22016-03-14 22:40:39 +0000901 "need more than %d values to unpack", (int)seq_len));
Damien George1e9a92f2014-11-06 17:36:16 +0000902 }
Damien George495d7812014-04-08 17:51:47 +0100903}
904
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300905mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000906 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300907 // use load_method
Damien George062478e2014-01-09 20:57:50 +0000908 mp_obj_t dest[2];
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300909 mp_load_method(base, attr, dest);
910 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000911 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000912 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000913 } else {
914 // load_method returned a method, so build a bound method object
915 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000916 }
Damiend99b0522013-12-21 18:17:45 +0000917}
918
Damien George06593fb2015-06-19 12:49:10 +0000919#if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
920
921// The following "checked fun" type is local to the mp_convert_member_lookup
922// function, and serves to check that the first argument to a builtin function
923// has the correct type.
924
925typedef struct _mp_obj_checked_fun_t {
926 mp_obj_base_t base;
927 const mp_obj_type_t *type;
928 mp_obj_t fun;
929} mp_obj_checked_fun_t;
930
Damien Georgea0c97812016-01-03 09:59:18 +0000931STATIC mp_obj_t checked_fun_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien George999cedb2015-11-27 17:01:44 +0000932 mp_obj_checked_fun_t *self = MP_OBJ_TO_PTR(self_in);
Damien George06593fb2015-06-19 12:49:10 +0000933 if (n_args > 0) {
934 const mp_obj_type_t *arg0_type = mp_obj_get_type(args[0]);
935 if (arg0_type != self->type) {
936 if (MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_DETAILED) {
Damien George7d0d7212016-10-17 12:17:37 +1100937 mp_raise_msg(&mp_type_TypeError, "argument has wrong type");
Damien George06593fb2015-06-19 12:49:10 +0000938 } else {
939 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
940 "argument should be a '%q' not a '%q'", self->type->name, arg0_type->name));
941 }
942 }
943 }
944 return mp_call_function_n_kw(self->fun, n_args, n_kw, args);
945}
946
947STATIC const mp_obj_type_t mp_type_checked_fun = {
948 { &mp_type_type },
949 .name = MP_QSTR_function,
950 .call = checked_fun_call,
951};
952
953STATIC mp_obj_t mp_obj_new_checked_fun(const mp_obj_type_t *type, mp_obj_t fun) {
954 mp_obj_checked_fun_t *o = m_new_obj(mp_obj_checked_fun_t);
955 o->base.type = &mp_type_checked_fun;
956 o->type = type;
957 o->fun = fun;
Damien George999cedb2015-11-27 17:01:44 +0000958 return MP_OBJ_FROM_PTR(o);
Damien George06593fb2015-06-19 12:49:10 +0000959}
960
961#endif // MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
962
Damien George55b74d12015-03-21 14:21:54 +0000963// Given a member that was extracted from an instance, convert it correctly
964// and put the result in the dest[] array for a possible method call.
965// Conversion means dealing with static/class methods, callables, and values.
966// see http://docs.python.org/3/howto/descriptor.html
967void mp_convert_member_lookup(mp_obj_t self, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest) {
968 if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
969 // return just the function
Damien George999cedb2015-11-27 17:01:44 +0000970 dest[0] = ((mp_obj_static_class_method_t*)MP_OBJ_TO_PTR(member))->fun;
Damien George55b74d12015-03-21 14:21:54 +0000971 } else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
972 // return a bound method, with self being the type of this object
Damien George3ff259a2015-12-09 17:30:01 +0000973 // this type should be the type of the original instance, not the base
974 // type (which is what is passed in the 'type' argument to this function)
975 if (self != MP_OBJ_NULL) {
976 type = mp_obj_get_type(self);
977 }
Damien George999cedb2015-11-27 17:01:44 +0000978 dest[0] = ((mp_obj_static_class_method_t*)MP_OBJ_TO_PTR(member))->fun;
979 dest[1] = MP_OBJ_FROM_PTR(type);
Damien George55b74d12015-03-21 14:21:54 +0000980 } else if (MP_OBJ_IS_TYPE(member, &mp_type_type)) {
981 // Don't try to bind types (even though they're callable)
982 dest[0] = member;
Damien George78913212015-12-26 12:41:31 +0000983 } else if (MP_OBJ_IS_FUN(member)
984 || (MP_OBJ_IS_OBJ(member)
985 && (((mp_obj_base_t*)MP_OBJ_TO_PTR(member))->type->name == MP_QSTR_closure
986 || ((mp_obj_base_t*)MP_OBJ_TO_PTR(member))->type->name == MP_QSTR_generator))) {
987 // only functions, closures and generators objects can be bound to self
Damien George06593fb2015-06-19 12:49:10 +0000988 #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
Damien George571e6f22016-10-18 11:49:27 +1100989 const mp_obj_type_t *m_type = ((mp_obj_base_t*)MP_OBJ_TO_PTR(member))->type;
990 if (self == MP_OBJ_NULL
991 && (m_type == &mp_type_fun_builtin_0
992 || m_type == &mp_type_fun_builtin_1
993 || m_type == &mp_type_fun_builtin_2
994 || m_type == &mp_type_fun_builtin_3
995 || m_type == &mp_type_fun_builtin_var)) {
Damien George06593fb2015-06-19 12:49:10 +0000996 // we extracted a builtin method without a first argument, so we must
997 // wrap this function in a type checker
998 dest[0] = mp_obj_new_checked_fun(type, member);
999 } else
1000 #endif
1001 {
1002 // return a bound method, with self being this object
1003 dest[0] = member;
1004 dest[1] = self;
1005 }
Damien George55b74d12015-03-21 14:21:54 +00001006 } else {
1007 // class member is a value, so just return that value
1008 dest[0] = member;
1009 }
1010}
1011
Damien George7c9c6672014-01-25 00:17:36 +00001012// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
1013// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
1014// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Paul Sokolovsky07b8dc62015-03-21 00:58:07 +02001015void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +00001016 // clear output to indicate no attribute/method found yet
1017 dest[0] = MP_OBJ_NULL;
1018 dest[1] = MP_OBJ_NULL;
1019
1020 // get the type
Paul Sokolovsky07b8dc62015-03-21 00:58:07 +02001021 mp_obj_type_t *type = mp_obj_get_type(obj);
Damien George062478e2014-01-09 20:57:50 +00001022
Damien Georgee44d26a2014-03-31 22:57:56 +01001023 // look for built-in names
1024 if (0) {
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +03001025#if MICROPY_CPYTHON_COMPAT
Damien Georgee44d26a2014-03-31 22:57:56 +01001026 } else if (attr == MP_QSTR___class__) {
1027 // a.__class__ is equivalent to type(a)
Damien George999cedb2015-11-27 17:01:44 +00001028 dest[0] = MP_OBJ_FROM_PTR(type);
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +03001029#endif
Damien Georgee44d26a2014-03-31 22:57:56 +01001030
1031 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George999cedb2015-11-27 17:01:44 +00001032 dest[0] = MP_OBJ_FROM_PTR(&mp_builtin_next_obj);
Paul Sokolovsky07b8dc62015-03-21 00:58:07 +02001033 dest[1] = obj;
Damien Georgee44d26a2014-03-31 22:57:56 +01001034
Damien Georgeb1bbe962015-04-01 14:10:50 +00001035 } else if (type->attr != NULL) {
Damien Georgee44d26a2014-03-31 22:57:56 +01001036 // this type can do its own load, so call it
Damien Georgeb1bbe962015-04-01 14:10:50 +00001037 type->attr(obj, attr, dest);
Damien Georgee44d26a2014-03-31 22:57:56 +01001038
1039 } else if (type->locals_dict != NULL) {
1040 // generic method lookup
1041 // this is a lookup in the object (ie not class or type)
Damien George999cedb2015-11-27 17:01:44 +00001042 assert(type->locals_dict->base.type == &mp_type_dict); // Micro Python restriction, for now
1043 mp_map_t *locals_map = &type->locals_dict->map;
Damien Georgee44d26a2014-03-31 22:57:56 +01001044 mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
1045 if (elem != NULL) {
Damien George55b74d12015-03-21 14:21:54 +00001046 mp_convert_member_lookup(obj, type, elem->value, dest);
Damiend57eba52013-11-02 23:58:14 +00001047 }
Damiena3977762013-10-09 23:10:10 +01001048 }
Damien George7c9c6672014-01-25 00:17:36 +00001049}
Damiena3977762013-10-09 23:10:10 +01001050
Damien Georged17926d2014-03-30 13:35:08 +01001051void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George7c9c6672014-01-25 00:17:36 +00001052 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
1053
Damien Georged17926d2014-03-30 13:35:08 +01001054 mp_load_method_maybe(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +00001055
1056 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +00001057 // no attribute/method called attr
Damien George1e9a92f2014-11-06 17:36:16 +00001058 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +11001059 mp_raise_msg(&mp_type_AttributeError, "no such attribute");
Damien George062478e2014-01-09 20:57:50 +00001060 } else {
Damien George1e9a92f2014-11-06 17:36:16 +00001061 // following CPython, we give a more detailed error message for type objects
1062 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
1063 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
Damien George044c4732015-04-11 13:03:37 +01001064 "type object '%q' has no attribute '%q'",
Damien George999cedb2015-11-27 17:01:44 +00001065 ((mp_obj_type_t*)MP_OBJ_TO_PTR(base))->name, attr));
Damien George1e9a92f2014-11-06 17:36:16 +00001066 } else {
1067 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
Damien George044c4732015-04-11 13:03:37 +01001068 "'%s' object has no attribute '%q'",
1069 mp_obj_get_type_str(base), attr));
Damien George1e9a92f2014-11-06 17:36:16 +00001070 }
Damien George062478e2014-01-09 20:57:50 +00001071 }
1072 }
Damiena3977762013-10-09 23:10:10 +01001073}
1074
Damien Georged17926d2014-03-30 13:35:08 +01001075void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +01001076 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +00001077 mp_obj_type_t *type = mp_obj_get_type(base);
Damien Georgeb1bbe962015-04-01 14:10:50 +00001078 if (type->attr != NULL) {
1079 mp_obj_t dest[2] = {MP_OBJ_SENTINEL, value};
1080 type->attr(base, attr, dest);
1081 if (dest[0] == MP_OBJ_NULL) {
1082 // success
Damien George062478e2014-01-09 20:57:50 +00001083 return;
1084 }
Damiena3977762013-10-09 23:10:10 +01001085 }
Damien George1e9a92f2014-11-06 17:36:16 +00001086 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +11001087 mp_raise_msg(&mp_type_AttributeError, "no such attribute");
Damien George1e9a92f2014-11-06 17:36:16 +00001088 } else {
1089 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
Damien George044c4732015-04-11 13:03:37 +01001090 "'%s' object has no attribute '%q'",
1091 mp_obj_get_type_str(base), attr));
Damien George1e9a92f2014-11-06 17:36:16 +00001092 }
Damiena3977762013-10-09 23:10:10 +01001093}
1094
Damien Georged17926d2014-03-30 13:35:08 +01001095mp_obj_t mp_getiter(mp_obj_t o_in) {
Paul Sokolovskyc48d6f72014-05-11 20:32:39 +03001096 assert(o_in);
Damien Georgef6532bb2015-02-15 01:10:13 +00001097
1098 // check for native getiter (corresponds to __iter__)
Damien George5fa93b62014-01-22 14:35:10 +00001099 mp_obj_type_t *type = mp_obj_get_type(o_in);
1100 if (type->getiter != NULL) {
Paul Sokolovskyc48d6f72014-05-11 20:32:39 +03001101 mp_obj_t iter = type->getiter(o_in);
Damien Georgef6532bb2015-02-15 01:10:13 +00001102 if (iter != MP_OBJ_NULL) {
1103 return iter;
Paul Sokolovskyc48d6f72014-05-11 20:32:39 +03001104 }
Damien Georgef6532bb2015-02-15 01:10:13 +00001105 }
1106
1107 // check for __getitem__
1108 mp_obj_t dest[2];
1109 mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
1110 if (dest[0] != MP_OBJ_NULL) {
1111 // __getitem__ exists, create and return an iterator
1112 return mp_obj_new_getitem_iter(dest);
1113 }
1114
1115 // object not iterable
1116 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +11001117 mp_raise_msg(&mp_type_TypeError, "object not iterable");
Damience89a212013-10-15 22:25:17 +01001118 } else {
Damien Georgef6532bb2015-02-15 01:10:13 +00001119 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
1120 "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
Damience89a212013-10-15 22:25:17 +01001121 }
1122}
1123
Damien Georgeea8d06c2014-04-17 23:19:36 +01001124// may return MP_OBJ_STOP_ITERATION as an optimisation instead of raise StopIteration()
Damien George66eaf842014-03-26 19:27:58 +00001125// may also raise StopIteration()
Damien Georged17926d2014-03-30 13:35:08 +01001126mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +00001127 mp_obj_type_t *type = mp_obj_get_type(o_in);
1128 if (type->iternext != NULL) {
1129 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +01001130 } else {
Damien George9e6e9352014-03-26 18:37:06 +00001131 // check for __next__ method
1132 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +01001133 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George9e6e9352014-03-26 18:37:06 +00001134 if (dest[0] != MP_OBJ_NULL) {
1135 // __next__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +01001136 return mp_call_method_n_kw(0, 0, dest);
Damien George9e6e9352014-03-26 18:37:06 +00001137 } else {
Damien George1e9a92f2014-11-06 17:36:16 +00001138 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +11001139 mp_raise_msg(&mp_type_TypeError, "object not an iterator");
Damien George1e9a92f2014-11-06 17:36:16 +00001140 } else {
1141 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
1142 "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
1143 }
Damien George9e6e9352014-03-26 18:37:06 +00001144 }
Damien Georgec5966122014-02-15 16:10:44 +00001145 }
1146}
1147
Damien Georgeea8d06c2014-04-17 23:19:36 +01001148// will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration() (or any subclass thereof)
Damien George66eaf842014-03-26 19:27:58 +00001149// may raise other exceptions
Damien Georged17926d2014-03-30 13:35:08 +01001150mp_obj_t mp_iternext(mp_obj_t o_in) {
Damien George953c23b2015-06-03 22:19:41 +01001151 MP_STACK_CHECK(); // enumerate, filter, map and zip can recursively call mp_iternext
Damien George66eaf842014-03-26 19:27:58 +00001152 mp_obj_type_t *type = mp_obj_get_type(o_in);
1153 if (type->iternext != NULL) {
1154 return type->iternext(o_in);
1155 } else {
1156 // check for __next__ method
1157 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +01001158 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George66eaf842014-03-26 19:27:58 +00001159 if (dest[0] != MP_OBJ_NULL) {
1160 // __next__ exists, call it and return its result
1161 nlr_buf_t nlr;
1162 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001163 mp_obj_t ret = mp_call_method_n_kw(0, 0, dest);
Damien George66eaf842014-03-26 19:27:58 +00001164 nlr_pop();
1165 return ret;
1166 } else {
Damien George999cedb2015-11-27 17:01:44 +00001167 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))) {
Damien Georgeea8d06c2014-04-17 23:19:36 +01001168 return MP_OBJ_STOP_ITERATION;
Damien George66eaf842014-03-26 19:27:58 +00001169 } else {
Damien George999cedb2015-11-27 17:01:44 +00001170 nlr_jump(nlr.ret_val);
Damien George66eaf842014-03-26 19:27:58 +00001171 }
1172 }
1173 } else {
Damien George1e9a92f2014-11-06 17:36:16 +00001174 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +11001175 mp_raise_msg(&mp_type_TypeError, "object not an iterator");
Damien George1e9a92f2014-11-06 17:36:16 +00001176 } else {
1177 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
1178 "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
1179 }
Damien George66eaf842014-03-26 19:27:58 +00001180 }
1181 }
1182}
1183
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001184// TODO: Unclear what to do with StopIterarion exception here.
1185mp_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) {
Paul Sokolovsky7da06602014-03-31 04:19:12 +03001186 assert((send_value != MP_OBJ_NULL) ^ (throw_value != MP_OBJ_NULL));
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001187 mp_obj_type_t *type = mp_obj_get_type(self_in);
1188
1189 if (type == &mp_type_gen_instance) {
1190 return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val);
1191 }
1192
1193 if (type->iternext != NULL && send_value == mp_const_none) {
1194 mp_obj_t ret = type->iternext(self_in);
Paul Sokolovsky4ed7b7f2015-05-10 00:41:34 +03001195 if (ret != MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001196 *ret_val = ret;
1197 return MP_VM_RETURN_YIELD;
1198 } else {
1199 // Emulate raise StopIteration()
1200 // Special case, handled in vm.c
1201 *ret_val = MP_OBJ_NULL;
1202 return MP_VM_RETURN_NORMAL;
1203 }
1204 }
1205
1206 mp_obj_t dest[3]; // Reserve slot for send() arg
1207
Paul Sokolovsky79d996a2016-11-15 01:10:34 +03001208 // Python instance iterator protocol
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001209 if (send_value == mp_const_none) {
1210 mp_load_method_maybe(self_in, MP_QSTR___next__, dest);
1211 if (dest[0] != MP_OBJ_NULL) {
Paul Sokolovsky79d996a2016-11-15 01:10:34 +03001212 nlr_buf_t nlr;
1213 if (nlr_push(&nlr) == 0) {
1214 *ret_val = mp_call_method_n_kw(0, 0, dest);
1215 nlr_pop();
1216 return MP_VM_RETURN_YIELD;
1217 } else {
Paul Sokolovskya0b2c6a2016-11-15 01:40:23 +03001218 *ret_val = MP_OBJ_FROM_PTR(nlr.ret_val);
Paul Sokolovsky79d996a2016-11-15 01:10:34 +03001219 return MP_VM_RETURN_EXCEPTION;
1220 }
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001221 }
1222 }
1223
Paul Sokolovsky79d996a2016-11-15 01:10:34 +03001224 // Either python instance generator protocol, or native object
1225 // generator protocol.
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001226 if (send_value != MP_OBJ_NULL) {
1227 mp_load_method(self_in, MP_QSTR_send, dest);
1228 dest[2] = send_value;
Paul Sokolovsky79d996a2016-11-15 01:10:34 +03001229 // TODO: This should have exception wrapping like __next__ case
1230 // above. Not done right away to think how to optimize native
1231 // generators better, see:
1232 // https://github.com/micropython/micropython/issues/2628
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001233 *ret_val = mp_call_method_n_kw(1, 0, dest);
1234 return MP_VM_RETURN_YIELD;
1235 }
1236
1237 if (throw_value != MP_OBJ_NULL) {
Damien George999cedb2015-11-27 17:01:44 +00001238 if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(throw_value)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001239 mp_load_method_maybe(self_in, MP_QSTR_close, dest);
1240 if (dest[0] != MP_OBJ_NULL) {
Paul Sokolovsky4a60cac2015-05-10 02:39:45 +03001241 // TODO: Exceptions raised in close() are not propagated,
1242 // printed to sys.stderr
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001243 *ret_val = mp_call_method_n_kw(0, 0, dest);
1244 // We assume one can't "yield" from close()
1245 return MP_VM_RETURN_NORMAL;
1246 }
1247 }
Paul Sokolovskya2109d92014-03-31 04:14:30 +03001248 mp_load_method_maybe(self_in, MP_QSTR_throw, dest);
1249 if (dest[0] != MP_OBJ_NULL) {
1250 *ret_val = mp_call_method_n_kw(1, 0, &throw_value);
1251 // If .throw() method returned, we assume it's value to yield
Damien Georgeea13f402014-04-05 18:32:08 +01001252 // - any exception would be thrown with nlr_raise().
Paul Sokolovskya2109d92014-03-31 04:14:30 +03001253 return MP_VM_RETURN_YIELD;
1254 }
1255 // If there's nowhere to throw exception into, then we assume that object
1256 // is just incapable to handle it, so any exception thrown into it
1257 // will be propagated up. This behavior is approved by test_pep380.py
1258 // test_delegation_of_close_to_non_generator(),
1259 // test_delegating_throw_to_non_generator()
1260 *ret_val = throw_value;
1261 return MP_VM_RETURN_EXCEPTION;
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001262 }
1263
1264 assert(0);
1265 return MP_VM_RETURN_NORMAL; // Should be unreachable
1266}
1267
Damien Georged17926d2014-03-30 13:35:08 +01001268mp_obj_t mp_make_raise_obj(mp_obj_t o) {
Damien Georgec5966122014-02-15 16:10:44 +00001269 DEBUG_printf("raise %p\n", o);
1270 if (mp_obj_is_exception_type(o)) {
1271 // o is an exception type (it is derived from BaseException (or is BaseException))
1272 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +00001273 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
1274 // could have const instances in ROM which we return here instead
Damien Georged17926d2014-03-30 13:35:08 +01001275 return mp_call_function_n_kw(o, 0, 0, NULL);
Damien Georgec5966122014-02-15 16:10:44 +00001276 } else if (mp_obj_is_exception_instance(o)) {
1277 // o is an instance of an exception, so use it as the exception
1278 return o;
1279 } else {
1280 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
1281 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +01001282 }
1283}
1284
Damien Georged17926d2014-03-30 13:35:08 +01001285mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Paul Sokolovsky6557a092015-06-27 00:32:35 +03001286 DEBUG_printf("import name '%s' level=%d\n", qstr_str(name), MP_OBJ_SMALL_INT_VALUE(level));
Damien George64131f32014-02-06 20:31:44 +00001287
Damiendb4c3612013-12-10 17:27:24 +00001288 // build args array
Damiend99b0522013-12-21 18:17:45 +00001289 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +00001290 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +00001291 args[1] = mp_const_none; // TODO should be globals
1292 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +00001293 args[3] = fromlist;
1294 args[4] = level; // must be 0; we don't yet support other values
1295
1296 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +00001297 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001298}
1299
Damien Georged17926d2014-03-30 13:35:08 +01001300mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +00001301 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
1302
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001303 mp_obj_t dest[2];
1304
1305 mp_load_method_maybe(module, name, dest);
1306
1307 if (dest[1] != MP_OBJ_NULL) {
1308 // Hopefully we can't import bound method from an object
1309import_error:
Damien George044c4732015-04-11 13:03:37 +01001310 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "cannot import name %q", name));
Damiendb4c3612013-12-10 17:27:24 +00001311 }
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001312
1313 if (dest[0] != MP_OBJ_NULL) {
1314 return dest[0];
1315 }
1316
1317 // See if it's a package, then can try FS import
Paul Sokolovskye5a37592014-10-25 21:04:13 +03001318 if (!mp_obj_is_package(module)) {
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001319 goto import_error;
1320 }
1321
1322 mp_load_method_maybe(module, MP_QSTR___name__, dest);
Damien Georged182b982014-08-30 14:19:41 +01001323 mp_uint_t pkg_name_len;
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001324 const char *pkg_name = mp_obj_str_get_data(dest[0], &pkg_name_len);
1325
stijn01d6be42014-05-05 12:18:27 +02001326 const uint dot_name_len = pkg_name_len + 1 + qstr_len(name);
1327 char *dot_name = alloca(dot_name_len);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001328 memcpy(dot_name, pkg_name, pkg_name_len);
1329 dot_name[pkg_name_len] = '.';
1330 memcpy(dot_name + pkg_name_len + 1, qstr_str(name), qstr_len(name));
stijn01d6be42014-05-05 12:18:27 +02001331 qstr dot_name_q = qstr_from_strn(dot_name, dot_name_len);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001332
1333 mp_obj_t args[5];
1334 args[0] = MP_OBJ_NEW_QSTR(dot_name_q);
1335 args[1] = mp_const_none; // TODO should be globals
1336 args[2] = mp_const_none; // TODO should be locals
1337 args[3] = mp_const_true; // Pass sentinel "non empty" value to force returning of leaf module
Paul Sokolovsky69f18672014-04-12 02:47:46 +03001338 args[4] = MP_OBJ_NEW_SMALL_INT(0);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001339
1340 // TODO lookup __import__ and call that instead of going straight to builtin implementation
1341 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001342}
1343
Damien Georged17926d2014-03-30 13:35:08 +01001344void mp_import_all(mp_obj_t module) {
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001345 DEBUG_printf("import all %p\n", module);
1346
Paul Sokolovsky599bbc12014-04-18 04:11:19 +03001347 // TODO: Support __all__
Damien George999cedb2015-11-27 17:01:44 +00001348 mp_map_t *map = mp_obj_dict_get_map(MP_OBJ_FROM_PTR(mp_obj_module_get_globals(module)));
Damien Georgeb063b9b2014-12-21 16:24:09 +00001349 for (mp_uint_t i = 0; i < map->alloc; i++) {
Damien George8b0535e2014-04-05 21:53:54 +01001350 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
Paul Sokolovsky599bbc12014-04-18 04:11:19 +03001351 qstr name = MP_OBJ_QSTR_VALUE(map->table[i].key);
1352 if (*qstr_str(name) != '_') {
1353 mp_store_name(name, map->table[i].value);
1354 }
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001355 }
1356 }
1357}
1358
Damien Georgedd5353a2015-12-18 12:35:44 +00001359#if MICROPY_ENABLE_COMPILER
1360
Damien Georgec4d08682014-10-05 20:13:34 +01001361// this is implemented in this file so it can optimise access to locals/globals
1362mp_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) {
Damien George0bfc7632015-02-07 18:33:58 +00001363 // save context
1364 mp_obj_dict_t *volatile old_globals = mp_globals_get();
1365 mp_obj_dict_t *volatile old_locals = mp_locals_get();
Damien Georgec4d08682014-10-05 20:13:34 +01001366
Damien George0bfc7632015-02-07 18:33:58 +00001367 // set new context
Damien Georgec4d08682014-10-05 20:13:34 +01001368 mp_globals_set(globals);
1369 mp_locals_set(locals);
1370
Damien Georgec4d08682014-10-05 20:13:34 +01001371 nlr_buf_t nlr;
1372 if (nlr_push(&nlr) == 0) {
Damien George0bfc7632015-02-07 18:33:58 +00001373 qstr source_name = lex->source_name;
Damien George58e0f4a2015-09-23 10:50:43 +01001374 mp_parse_tree_t parse_tree = mp_parse(lex, parse_input_kind);
1375 mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, false);
Damien George0bfc7632015-02-07 18:33:58 +00001376
1377 mp_obj_t ret;
1378 if (MICROPY_PY_BUILTINS_COMPILE && globals == NULL) {
1379 // for compile only, return value is the module function
1380 ret = module_fun;
1381 } else {
1382 // execute module function and get return value
1383 ret = mp_call_function_0(module_fun);
1384 }
1385
1386 // finish nlr block, restore context and return value
Damien Georgec4d08682014-10-05 20:13:34 +01001387 nlr_pop();
1388 mp_globals_set(old_globals);
1389 mp_locals_set(old_locals);
1390 return ret;
1391 } else {
1392 // exception; restore context and re-raise same exception
1393 mp_globals_set(old_globals);
1394 mp_locals_set(old_locals);
Damien George999cedb2015-11-27 17:01:44 +00001395 nlr_jump(nlr.ret_val);
Damien Georgec4d08682014-10-05 20:13:34 +01001396 }
1397}
1398
Damien Georgedd5353a2015-12-18 12:35:44 +00001399#endif // MICROPY_ENABLE_COMPILER
1400
Damien Georgeb0261342014-09-23 18:10:17 +01001401void *m_malloc_fail(size_t num_bytes) {
Damien George978d2e52016-01-08 13:49:58 +00001402 DEBUG_printf("memory allocation failed, allocating %u bytes\n", (uint)num_bytes);
Damien George40914452014-10-09 16:44:43 +01001403 if (0) {
1404 // dummy
1405 #if MICROPY_ENABLE_GC
1406 } else if (gc_is_locked()) {
Damien George7d0d7212016-10-17 12:17:37 +11001407 mp_raise_msg(&mp_type_MemoryError, "memory allocation failed, heap is locked");
Damien George40914452014-10-09 16:44:43 +01001408 #endif
Dave Hylands3556e452014-10-07 00:50:20 -07001409 } else {
Damien George40914452014-10-09 16:44:43 +01001410 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError,
Damien George978d2e52016-01-08 13:49:58 +00001411 "memory allocation failed, allocating %u bytes", (uint)num_bytes));
Dave Hylands3556e452014-10-07 00:50:20 -07001412 }
Damien George6902eed2014-04-04 10:52:59 +00001413}
1414
Paul Sokolovsky9e1b61d2016-08-12 21:26:12 +03001415NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg) {
1416 nlr_raise(mp_obj_new_exception_msg(exc_type, msg));
1417}
1418
1419NORETURN void mp_raise_ValueError(const char *msg) {
1420 mp_raise_msg(&mp_type_ValueError, msg);
1421}
1422
1423NORETURN void mp_raise_TypeError(const char *msg) {
1424 mp_raise_msg(&mp_type_TypeError, msg);
1425}
1426
Damien George3a0a7712016-10-07 13:31:59 +11001427NORETURN void mp_raise_OSError(int errno_) {
1428 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_)));
1429}
1430
Paul Sokolovsky7e4a2b02014-06-07 23:22:41 +03001431NORETURN void mp_not_implemented(const char *msg) {
Paul Sokolovsky9e1b61d2016-08-12 21:26:12 +03001432 mp_raise_msg(&mp_type_NotImplementedError, msg);
Paul Sokolovsky7e4a2b02014-06-07 23:22:41 +03001433}