blob: 0ecccbd8740f55229823b7f3e28798c6b48553f1 [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 George97f9a282014-05-12 23:07:34 +010071 // call port specific initialization if any
stijn72521a12014-05-03 11:28:29 +020072#ifdef MICROPY_PORT_INIT_FUNC
73 MICROPY_PORT_INIT_FUNC;
74#endif
75
Paul Sokolovskyd3439d02014-06-02 19:37:55 +030076 // optimization disabled by default
Damien Georgeb4b10fd2015-01-01 23:30:53 +000077 MP_STATE_VM(mp_optimise_value) = 0;
Damien George97f9a282014-05-12 23:07:34 +010078
Damien Georgecaac5422014-03-25 14:18:18 +000079 // init global module stuff
80 mp_module_init();
Damien George0d028742014-01-22 23:59:20 +000081
Damien George7efc5b32014-04-05 22:36:42 +010082 // initialise the __main__ module
Damien Georgeb4b10fd2015-01-01 23:30:53 +000083 mp_obj_dict_init(&MP_STATE_VM(dict_main), 1);
Damien George999cedb2015-11-27 17:01:44 +000084 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 +030085
86 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien Georgeb4b10fd2015-01-01 23:30:53 +000087 MP_STATE_CTX(dict_locals) = MP_STATE_CTX(dict_globals) = &MP_STATE_VM(dict_main);
Damien George78d702c2014-12-09 16:19:48 +000088
89 #if MICROPY_CAN_OVERRIDE_BUILTINS
90 // start with no extensions to builtins
Damien Georgeb4b10fd2015-01-01 23:30:53 +000091 MP_STATE_VM(mp_module_builtins_override_dict) = NULL;
Damien George78d702c2014-12-09 16:19:48 +000092 #endif
Damien George4cec63a2016-05-26 10:42:53 +000093
94 #if MICROPY_PY_THREAD_GIL
95 mp_thread_mutex_init(&MP_STATE_VM(gil_mutex));
96 #endif
97
98 MP_THREAD_GIL_ENTER();
Damien429d7192013-10-04 19:53:11 +010099}
100
Damien Georged17926d2014-03-30 13:35:08 +0100101void mp_deinit(void) {
Damien George7efc5b32014-04-05 22:36:42 +0100102 //mp_obj_dict_free(&dict_main);
Damien George2326d522014-03-27 23:26:35 +0000103 mp_module_deinit();
stijn5ed284a2014-05-08 10:56:33 +0200104
105 // call port specific deinitialization if any
106#ifdef MICROPY_PORT_INIT_FUNC
107 MICROPY_PORT_DEINIT_FUNC;
108#endif
Damien429d7192013-10-04 19:53:11 +0100109}
110
Damien George50912e72015-01-20 11:55:10 +0000111mp_obj_t mp_load_name(qstr qst) {
Damien429d7192013-10-04 19:53:11 +0100112 // logic: search locals, globals, builtins
Damien George50912e72015-01-20 11:55:10 +0000113 DEBUG_OP_printf("load name %s\n", qstr_str(qst));
Paul Sokolovskya0d32992014-04-05 04:51:26 +0300114 // If we're at the outer scope (locals == globals), dispatch to load_global right away
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000115 if (MP_STATE_CTX(dict_locals) != MP_STATE_CTX(dict_globals)) {
Damien George50912e72015-01-20 11:55:10 +0000116 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 +0300117 if (elem != NULL) {
118 return elem->value;
119 }
Damiena3977762013-10-09 23:10:10 +0100120 }
Damien George50912e72015-01-20 11:55:10 +0000121 return mp_load_global(qst);
Damiena3977762013-10-09 23:10:10 +0100122}
123
Damien George50912e72015-01-20 11:55:10 +0000124mp_obj_t mp_load_global(qstr qst) {
Damiena3977762013-10-09 23:10:10 +0100125 // logic: search globals, builtins
Damien George50912e72015-01-20 11:55:10 +0000126 DEBUG_OP_printf("load global %s\n", qstr_str(qst));
127 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 +0100128 if (elem == NULL) {
Damien George78d702c2014-12-09 16:19:48 +0000129 #if MICROPY_CAN_OVERRIDE_BUILTINS
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000130 if (MP_STATE_VM(mp_module_builtins_override_dict) != NULL) {
Damien George78d702c2014-12-09 16:19:48 +0000131 // lookup in additional dynamic table of builtins first
Damien George50912e72015-01-20 11:55:10 +0000132 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 +0000133 if (elem != NULL) {
134 return elem->value;
135 }
136 }
137 #endif
Damien George50912e72015-01-20 11:55:10 +0000138 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 +0100139 if (elem == NULL) {
Damien George1e9a92f2014-11-06 17:36:16 +0000140 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100141 mp_raise_msg(&mp_type_NameError, "name not defined");
Damien George1e9a92f2014-11-06 17:36:16 +0000142 } else {
143 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NameError,
Damien George044c4732015-04-11 13:03:37 +0100144 "name '%q' is not defined", qst));
Damien George1e9a92f2014-11-06 17:36:16 +0000145 }
Damien429d7192013-10-04 19:53:11 +0100146 }
147 }
148 return elem->value;
149}
150
Damien Georged17926d2014-03-30 13:35:08 +0100151mp_obj_t mp_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100152 DEBUG_OP_printf("load_build_class\n");
Damien George78d702c2014-12-09 16:19:48 +0000153 #if MICROPY_CAN_OVERRIDE_BUILTINS
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000154 if (MP_STATE_VM(mp_module_builtins_override_dict) != NULL) {
Damien George78d702c2014-12-09 16:19:48 +0000155 // lookup in additional dynamic table of builtins first
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000156 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 +0000157 if (elem != NULL) {
158 return elem->value;
159 }
160 }
161 #endif
Damien George999cedb2015-11-27 17:01:44 +0000162 return MP_OBJ_FROM_PTR(&mp_builtin___build_class___obj);
Damien429d7192013-10-04 19:53:11 +0100163}
164
Damien George50912e72015-01-20 11:55:10 +0000165void mp_store_name(qstr qst, mp_obj_t obj) {
166 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qst), obj);
Damien George999cedb2015-11-27 17:01:44 +0000167 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 +0100168}
169
Damien George50912e72015-01-20 11:55:10 +0000170void mp_delete_name(qstr qst) {
171 DEBUG_OP_printf("delete name %s\n", qstr_str(qst));
172 // TODO convert KeyError to NameError if qst not found
Damien George999cedb2015-11-27 17:01:44 +0000173 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 +0200174}
175
Damien George50912e72015-01-20 11:55:10 +0000176void mp_store_global(qstr qst, mp_obj_t obj) {
177 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qst), obj);
Damien George999cedb2015-11-27 17:01:44 +0000178 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 +0100179}
180
Damien George50912e72015-01-20 11:55:10 +0000181void mp_delete_global(qstr qst) {
182 DEBUG_OP_printf("delete global %s\n", qstr_str(qst));
183 // TODO convert KeyError to NameError if qst not found
Damien George999cedb2015-11-27 17:01:44 +0000184 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 +0100185}
186
Damien George4abff752014-08-30 14:59:21 +0100187mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
Damien Georgeeaaebf32014-09-23 10:59:05 +0100188 DEBUG_OP_printf("unary " UINT_FMT " %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000189
Damien Georgebdbe8c92015-12-08 12:28:11 +0000190 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)) {
Damien George40f3c022014-07-03 13:25:24 +0100194 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000195 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100196 case MP_UNARY_OP_BOOL:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300197 return mp_obj_new_bool(val != 0);
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000198 case MP_UNARY_OP_HASH:
199 return arg;
Damien Georged17926d2014-03-30 13:35:08 +0100200 case MP_UNARY_OP_POSITIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000201 return arg;
Damien Georged17926d2014-03-30 13:35:08 +0100202 case MP_UNARY_OP_NEGATIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000203 // 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 }
Damien Georged17926d2014-03-30 13:35:08 +0100209 case MP_UNARY_OP_INVERT:
Damien George9d68e9c2014-03-12 15:38:15 +0000210 return MP_OBJ_NEW_SMALL_INT(~val);
211 default:
212 assert(0);
213 return arg;
Damien7410e442013-11-02 19:47:57 +0000214 }
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000215 } else if (op == MP_UNARY_OP_HASH && MP_OBJ_IS_STR_OR_BYTES(arg)) {
216 // fast path for hashing str/bytes
217 GET_STR_HASH(arg, h);
Damien George5f3bda42016-09-02 14:42:53 +1000218 if (h == 0) {
219 GET_STR_DATA_LEN(arg, data, len);
220 h = qstr_compute_hash(data, len);
221 }
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000222 return MP_OBJ_NEW_SMALL_INT(h);
Damien George1e708fe2014-01-23 18:27:51 +0000223 } else {
224 mp_obj_type_t *type = mp_obj_get_type(arg);
225 if (type->unary_op != NULL) {
226 mp_obj_t result = type->unary_op(op, arg);
Damien George6ac5dce2014-05-21 19:42:43 +0100227 if (result != MP_OBJ_NULL) {
Damiend99b0522013-12-21 18:17:45 +0000228 return result;
229 }
Damien7410e442013-11-02 19:47:57 +0000230 }
Damien George1e9a92f2014-11-06 17:36:16 +0000231 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100232 mp_raise_msg(&mp_type_TypeError, "unsupported type for operator");
Damien George1e9a92f2014-11-06 17:36:16 +0000233 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000234 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George044c4732015-04-11 13:03:37 +0100235 "unsupported type for %q: '%s'",
236 mp_unary_op_method_name[op], mp_obj_get_type_str(arg)));
Damien George1e9a92f2014-11-06 17:36:16 +0000237 }
Damien7410e442013-11-02 19:47:57 +0000238 }
Damien429d7192013-10-04 19:53:11 +0100239}
240
Damien George4abff752014-08-30 14:59:21 +0100241mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
Damien Georgeeaaebf32014-09-23 10:59:05 +0100242 DEBUG_OP_printf("binary " UINT_FMT " %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000243
244 // TODO correctly distinguish inplace operators for mutable objects
245 // lookup logic that CPython uses for +=:
246 // check for implemented +=
247 // then check for implemented +
248 // then check for implemented seq.inplace_concat
249 // then check for implemented seq.concat
250 // then fail
251 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
252
Damien George9aa2a522014-02-01 23:04:09 +0000253 // deal with is
Damien Georged17926d2014-03-30 13:35:08 +0100254 if (op == MP_BINARY_OP_IS) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300255 return mp_obj_new_bool(lhs == rhs);
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200256 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200257
Damien Georgebcbeea02014-01-11 10:47:22 +0000258 // deal with == and != for all types
Damien Georged17926d2014-03-30 13:35:08 +0100259 if (op == MP_BINARY_OP_EQUAL || op == MP_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000260 if (mp_obj_equal(lhs, rhs)) {
Damien Georged17926d2014-03-30 13:35:08 +0100261 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000262 return mp_const_true;
263 } else {
264 return mp_const_false;
265 }
266 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100267 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000268 return mp_const_false;
269 } else {
270 return mp_const_true;
271 }
272 }
273 }
274
275 // deal with exception_match for all types
Damien Georged17926d2014-03-30 13:35:08 +0100276 if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000277 // rhs must be issubclass(rhs, BaseException)
278 if (mp_obj_is_exception_type(rhs)) {
Damien George4bcd04b2014-09-24 14:05:40 +0100279 if (mp_obj_exception_match(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000280 return mp_const_true;
281 } else {
282 return mp_const_false;
283 }
Damien George4bcd04b2014-09-24 14:05:40 +0100284 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_tuple)) {
Damien George999cedb2015-11-27 17:01:44 +0000285 mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(rhs);
Damien George4bcd04b2014-09-24 14:05:40 +0100286 for (mp_uint_t i = 0; i < tuple->len; i++) {
287 rhs = tuple->items[i];
288 if (!mp_obj_is_exception_type(rhs)) {
289 goto unsupported_op;
290 }
291 if (mp_obj_exception_match(lhs, rhs)) {
292 return mp_const_true;
293 }
294 }
295 return mp_const_false;
Damien Georgebcbeea02014-01-11 10:47:22 +0000296 }
Damien George4bcd04b2014-09-24 14:05:40 +0100297 goto unsupported_op;
Damien Georgebcbeea02014-01-11 10:47:22 +0000298 }
299
Damien George1a9951d2014-01-06 22:13:00 +0000300 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damien George40f3c022014-07-03 13:25:24 +0100301 mp_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000302 if (MP_OBJ_IS_SMALL_INT(rhs)) {
Damien George40f3c022014-07-03 13:25:24 +0100303 mp_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000304 // This is a binary operation: lhs_val op rhs_val
305 // We need to be careful to handle overflow; see CERT INT32-C
306 // Operations that can overflow:
Damien George40f3c022014-07-03 13:25:24 +0100307 // + result always fits in mp_int_t, then handled by SMALL_INT check
308 // - result always fits in mp_int_t, then handled by SMALL_INT check
Damien George9d68e9c2014-03-12 15:38:15 +0000309 // * checked explicitly
Damien George40f3c022014-07-03 13:25:24 +0100310 // / if lhs=MIN and rhs=-1; result always fits in mp_int_t, then handled by SMALL_INT check
311 // % 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 +0000312 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000313 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100314 case MP_BINARY_OP_OR:
315 case MP_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
316 case MP_BINARY_OP_XOR:
317 case MP_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
318 case MP_BINARY_OP_AND:
319 case MP_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
320 case MP_BINARY_OP_LSHIFT:
321 case MP_BINARY_OP_INPLACE_LSHIFT: {
Damien George9d68e9c2014-03-12 15:38:15 +0000322 if (rhs_val < 0) {
323 // negative shift not allowed
Damien George7d0d7212016-10-17 12:17:37 +1100324 mp_raise_msg(&mp_type_ValueError, "negative shift count");
Damien George963a5a32015-01-16 17:47:07 +0000325 } 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 +0000326 // left-shift will overflow, so use higher precision integer
327 lhs = mp_obj_new_int_from_ll(lhs_val);
328 goto generic_binary_op;
329 } else {
330 // use standard precision
331 lhs_val <<= rhs_val;
332 }
333 break;
334 }
Damien Georged17926d2014-03-30 13:35:08 +0100335 case MP_BINARY_OP_RSHIFT:
336 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000337 if (rhs_val < 0) {
338 // negative shift not allowed
Damien George7d0d7212016-10-17 12:17:37 +1100339 mp_raise_msg(&mp_type_ValueError, "negative shift count");
Damien George9d68e9c2014-03-12 15:38:15 +0000340 } else {
341 // standard precision is enough for right-shift
Damien George963a5a32015-01-16 17:47:07 +0000342 if (rhs_val >= (mp_int_t)BITS_PER_WORD) {
Paul Sokolovsky039887a2014-11-02 02:39:41 +0200343 // Shifting to big amounts is underfined behavior
344 // in C and is CPU-dependent; propagate sign bit.
345 rhs_val = BITS_PER_WORD - 1;
346 }
Damien George9d68e9c2014-03-12 15:38:15 +0000347 lhs_val >>= rhs_val;
348 }
349 break;
Damien Georged17926d2014-03-30 13:35:08 +0100350 case MP_BINARY_OP_ADD:
351 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
352 case MP_BINARY_OP_SUBTRACT:
353 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
354 case MP_BINARY_OP_MULTIPLY:
355 case MP_BINARY_OP_INPLACE_MULTIPLY: {
Damien George9d68e9c2014-03-12 15:38:15 +0000356
Damien George40f3c022014-07-03 13:25:24 +0100357 // If long long type exists and is larger than mp_int_t, then
Damien George9d68e9c2014-03-12 15:38:15 +0000358 // we can use the following code to perform overflow-checked multiplication.
Damien Georgeecf5b772014-04-04 11:13:51 +0000359 // Otherwise (eg in x64 case) we must use mp_small_int_mul_overflow.
Damien George9d68e9c2014-03-12 15:38:15 +0000360 #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
Damien George40f3c022014-07-03 13:25:24 +0100368 lhs_val = (mp_int_t)res;
Damien George9d68e9c2014-03-12 15:38:15 +0000369 }
370 #endif
371
Damien Georgeecf5b772014-04-04 11:13:51 +0000372 if (mp_small_int_mul_overflow(lhs_val, rhs_val)) {
373 // use higher precision
374 lhs = mp_obj_new_int_from_ll(lhs_val);
375 goto generic_binary_op;
376 } else {
377 // use standard precision
378 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
379 }
Damien George9d68e9c2014-03-12 15:38:15 +0000380 break;
381 }
Damien Georged17926d2014-03-30 13:35:08 +0100382 case MP_BINARY_OP_FLOOR_DIVIDE:
383 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300384 if (rhs_val == 0) {
385 goto zero_division;
386 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000387 lhs_val = mp_small_int_floor_divide(lhs_val, rhs_val);
Rachel Dowdall56402792014-03-22 20:19:24 +0000388 break;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300389
Damien Georgefb510b32014-06-01 13:32:54 +0100390 #if MICROPY_PY_BUILTINS_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100391 case MP_BINARY_OP_TRUE_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300392 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
393 if (rhs_val == 0) {
Damien George70f33cd2014-04-02 17:06:05 +0100394 goto zero_division;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300395 }
396 return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000397 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000398
Damien Georged17926d2014-03-30 13:35:08 +0100399 case MP_BINARY_OP_MODULO:
Damien Georgeecf5b772014-04-04 11:13:51 +0000400 case MP_BINARY_OP_INPLACE_MODULO: {
Damien Georgee5635f42015-10-01 22:48:48 +0100401 if (rhs_val == 0) {
402 goto zero_division;
403 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000404 lhs_val = mp_small_int_modulo(lhs_val, rhs_val);
Rachel Dowdallcde86312014-03-22 17:29:27 +0000405 break;
406 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000407
Damien Georged17926d2014-03-30 13:35:08 +0100408 case MP_BINARY_OP_POWER:
409 case MP_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000410 if (rhs_val < 0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100411 #if MICROPY_PY_BUILTINS_FLOAT
Damien George9d68e9c2014-03-12 15:38:15 +0000412 lhs = mp_obj_new_float(lhs_val);
413 goto generic_binary_op;
414 #else
Damien George7d0d7212016-10-17 12:17:37 +1100415 mp_raise_msg(&mp_type_ValueError, "negative power with no float support");
Damien George9d68e9c2014-03-12 15:38:15 +0000416 #endif
417 } else {
Damien George40f3c022014-07-03 13:25:24 +0100418 mp_int_t ans = 1;
Damien George9d68e9c2014-03-12 15:38:15 +0000419 while (rhs_val > 0) {
420 if (rhs_val & 1) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000421 if (mp_small_int_mul_overflow(ans, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100422 goto power_overflow;
423 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000424 ans *= lhs_val;
Damien George9d68e9c2014-03-12 15:38:15 +0000425 }
Damien George5bf565e2014-04-04 00:16:32 +0100426 if (rhs_val == 1) {
427 break;
428 }
Damien George9d68e9c2014-03-12 15:38:15 +0000429 rhs_val /= 2;
Damien Georgeecf5b772014-04-04 11:13:51 +0000430 if (mp_small_int_mul_overflow(lhs_val, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100431 goto power_overflow;
432 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000433 lhs_val *= lhs_val;
Damien George1a9951d2014-01-06 22:13:00 +0000434 }
Damien George9d68e9c2014-03-12 15:38:15 +0000435 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000436 }
Damien George1a9951d2014-01-06 22:13:00 +0000437 break;
Damien George5bf565e2014-04-04 00:16:32 +0100438
439 power_overflow:
440 // use higher precision
441 lhs = mp_obj_new_int_from_ll(MP_OBJ_SMALL_INT_VALUE(lhs));
442 goto generic_binary_op;
443
Damien Georgec5029bc2015-06-13 22:00:10 +0100444 case MP_BINARY_OP_DIVMOD: {
445 if (rhs_val == 0) {
446 goto zero_division;
447 }
448 // to reduce stack usage we don't pass a temp array of the 2 items
Damien George999cedb2015-11-27 17:01:44 +0000449 mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
Damien Georgec5029bc2015-06-13 22:00:10 +0100450 tuple->items[0] = MP_OBJ_NEW_SMALL_INT(mp_small_int_floor_divide(lhs_val, rhs_val));
451 tuple->items[1] = MP_OBJ_NEW_SMALL_INT(mp_small_int_modulo(lhs_val, rhs_val));
Damien George999cedb2015-11-27 17:01:44 +0000452 return MP_OBJ_FROM_PTR(tuple);
Damien Georgec5029bc2015-06-13 22:00:10 +0100453 }
454
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300455 case MP_BINARY_OP_LESS: return mp_obj_new_bool(lhs_val < rhs_val); break;
456 case MP_BINARY_OP_MORE: return mp_obj_new_bool(lhs_val > rhs_val); break;
457 case MP_BINARY_OP_LESS_EQUAL: return mp_obj_new_bool(lhs_val <= rhs_val); break;
458 case MP_BINARY_OP_MORE_EQUAL: return mp_obj_new_bool(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000459
Damien George8bcb9862014-04-17 16:26:50 +0100460 default:
461 goto unsupported_op;
Damien George1a9951d2014-01-06 22:13:00 +0000462 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200463 // TODO: We just should make mp_obj_new_int() inline and use that
Damien Georged1e355e2014-05-28 14:51:12 +0100464 if (MP_SMALL_INT_FITS(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000465 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000466 } else {
467 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000468 }
Damien Georgefb510b32014-06-01 13:32:54 +0100469#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeaaef1852015-08-20 23:30:12 +0100470 } else if (mp_obj_is_float(rhs)) {
Damien Georgeae491052014-04-10 20:08:11 +0100471 mp_obj_t res = mp_obj_float_binary_op(op, lhs_val, rhs);
472 if (res == MP_OBJ_NULL) {
473 goto unsupported_op;
474 } else {
475 return res;
476 }
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300477#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0c36da02014-03-08 15:24:39 +0000478 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien Georgeae491052014-04-10 20:08:11 +0100479 mp_obj_t res = mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
480 if (res == MP_OBJ_NULL) {
481 goto unsupported_op;
482 } else {
483 return res;
484 }
Damien George3f759b72014-01-31 00:42:12 +0000485#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300486#endif
Damien429d7192013-10-04 19:53:11 +0100487 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000488 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000489
Damien George9aa2a522014-02-01 23:04:09 +0000490 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000491 *
492 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000493 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000494 */
Damien Georged17926d2014-03-30 13:35:08 +0100495 if (op == MP_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000496 mp_obj_type_t *type = mp_obj_get_type(rhs);
497 if (type->binary_op != NULL) {
498 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George6ac5dce2014-05-21 19:42:43 +0100499 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000500 return res;
501 }
502 }
503 if (type->getiter != NULL) {
504 /* second attempt, walk the iterator */
Damien Georged17926d2014-03-30 13:35:08 +0100505 mp_obj_t iter = mp_getiter(rhs);
Damien George3aa09f52014-10-23 12:06:53 +0100506 mp_obj_t next;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100507 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien George5fa93b62014-01-22 14:35:10 +0000508 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000509 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000510 }
Damien7410e442013-11-02 19:47:57 +0000511 }
Damien George9aa2a522014-02-01 23:04:09 +0000512 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000513 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000514
Damien George1e9a92f2014-11-06 17:36:16 +0000515 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100516 mp_raise_msg(&mp_type_TypeError, "object not iterable");
Damien George1e9a92f2014-11-06 17:36:16 +0000517 } else {
518 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
519 "'%s' object is not iterable", mp_obj_get_type_str(rhs)));
520 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000521 }
522
Damien George5fa93b62014-01-22 14:35:10 +0000523 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000524 mp_obj_type_t *type;
525generic_binary_op:
526 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000527 if (type->binary_op != NULL) {
528 mp_obj_t result = type->binary_op(op, lhs, rhs);
Damien George6ac5dce2014-05-21 19:42:43 +0100529 if (result != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000530 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000531 }
Damien429d7192013-10-04 19:53:11 +0100532 }
Damiend99b0522013-12-21 18:17:45 +0000533
Damien George5fa93b62014-01-22 14:35:10 +0000534 // TODO implement dispatch for reverse binary ops
535
Damien Georgeae491052014-04-10 20:08:11 +0100536unsupported_op:
Damien George1e9a92f2014-11-06 17:36:16 +0000537 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100538 mp_raise_msg(&mp_type_TypeError, "unsupported type for operator");
Damien George1e9a92f2014-11-06 17:36:16 +0000539 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000540 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George044c4732015-04-11 13:03:37 +0100541 "unsupported types for %q: '%s', '%s'",
542 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 +0000543 }
Damien George70f33cd2014-04-02 17:06:05 +0100544
545zero_division:
Damien George7d0d7212016-10-17 12:17:37 +1100546 mp_raise_msg(&mp_type_ZeroDivisionError, "division by zero");
Damien429d7192013-10-04 19:53:11 +0100547}
548
Damien Georged17926d2014-03-30 13:35:08 +0100549mp_obj_t mp_call_function_0(mp_obj_t fun) {
550 return mp_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100551}
552
Damien Georged17926d2014-03-30 13:35:08 +0100553mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg) {
554 return mp_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100555}
556
Damien Georged17926d2014-03-30 13:35:08 +0100557mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
Damiend99b0522013-12-21 18:17:45 +0000558 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000559 args[0] = arg1;
560 args[1] = arg2;
Damien Georged17926d2014-03-30 13:35:08 +0100561 return mp_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100562}
563
Damien George20006db2014-01-18 14:10:48 +0000564// args contains, eg: arg0 arg1 key0 value0 key1 value1
Damien George4abff752014-08-30 14:59:21 +0100565mp_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 +0000566 // TODO improve this: fun object can specify its type and we parse here the arguments,
567 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100568
Damien Georgeeaaebf32014-09-23 10:59:05 +0100569 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 +0000570
Damien George8b56beb2014-01-31 23:49:49 +0000571 // get the type
572 mp_obj_type_t *type = mp_obj_get_type(fun_in);
573
574 // do the call
575 if (type->call != NULL) {
Damien George3f522622014-06-03 13:40:16 +0100576 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000577 }
Paul Sokolovsky755565d2014-04-25 21:15:16 +0300578
Damien George1e9a92f2014-11-06 17:36:16 +0000579 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100580 mp_raise_msg(&mp_type_TypeError, "object not callable");
Damien George1e9a92f2014-11-06 17:36:16 +0000581 } else {
582 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
583 "'%s' object is not callable", mp_obj_get_type_str(fun_in)));
584 }
Damien86c7fc72013-11-26 15:16:41 +0000585}
586
Damien George20006db2014-01-18 14:10:48 +0000587// 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)
588// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damien George4abff752014-08-30 14:59:21 +0100589mp_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 +0100590 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 +0000591 int adjust = (args[1] == MP_OBJ_NULL) ? 0 : 1;
Damien Georged17926d2014-03-30 13:35:08 +0100592 return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000593}
594
Damien George12a5e172015-04-01 23:31:30 +0100595// This function only needs to be exposed externally when in stackless mode.
596#if !MICROPY_STACKLESS
597STATIC
598#endif
599void 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 +0100600 mp_obj_t fun = *args++;
601 mp_obj_t self = MP_OBJ_NULL;
602 if (have_self) {
603 self = *args++; // may be MP_OBJ_NULL
604 }
605 uint n_args = n_args_n_kw & 0xff;
606 uint n_kw = (n_args_n_kw >> 8) & 0xff;
Paul Sokolovskye104acd2015-03-03 21:37:37 +0200607 mp_obj_t pos_seq = args[n_args + 2 * n_kw]; // may be MP_OBJ_NULL
608 mp_obj_t kw_dict = args[n_args + 2 * n_kw + 1]; // may be MP_OBJ_NULL
Damien George230fec72014-03-30 21:21:24 +0100609
610 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);
611
612 // We need to create the following array of objects:
613 // args[0 .. n_args] unpacked(pos_seq) args[n_args .. n_args + 2 * n_kw] unpacked(kw_dict)
614 // TODO: optimize one day to avoid constructing new arg array? Will be hard.
615
616 // The new args array
617 mp_obj_t *args2;
618 uint args2_alloc;
619 uint args2_len = 0;
620
621 // Try to get a hint for the size of the kw_dict
622 uint kw_dict_len = 0;
623 if (kw_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
624 kw_dict_len = mp_obj_dict_len(kw_dict);
625 }
626
627 // Extract the pos_seq sequence to the new args array.
628 // Note that it can be arbitrary iterator.
629 if (pos_seq == MP_OBJ_NULL) {
630 // no sequence
631
632 // allocate memory for the new array of args
633 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len);
634 args2 = m_new(mp_obj_t, args2_alloc);
635
636 // copy the self
637 if (self != MP_OBJ_NULL) {
638 args2[args2_len++] = self;
639 }
640
641 // copy the fixed pos args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300642 mp_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100643 args2_len += n_args;
644
645 } else if (MP_OBJ_IS_TYPE(pos_seq, &mp_type_tuple) || MP_OBJ_IS_TYPE(pos_seq, &mp_type_list)) {
646 // optimise the case of a tuple and list
647
648 // get the items
Damien George9c4cbe22014-08-30 14:04:14 +0100649 mp_uint_t len;
Damien George230fec72014-03-30 21:21:24 +0100650 mp_obj_t *items;
651 mp_obj_get_array(pos_seq, &len, &items);
652
653 // allocate memory for the new array of args
654 args2_alloc = 1 + n_args + len + 2 * (n_kw + kw_dict_len);
655 args2 = m_new(mp_obj_t, args2_alloc);
656
657 // copy the self
658 if (self != MP_OBJ_NULL) {
659 args2[args2_len++] = self;
660 }
661
662 // copy the fixed and variable position args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300663 mp_seq_cat(args2 + args2_len, args, n_args, items, len, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100664 args2_len += n_args + len;
665
666 } else {
667 // generic iterator
668
669 // allocate memory for the new array of args
670 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len) + 3;
671 args2 = m_new(mp_obj_t, args2_alloc);
672
673 // copy the self
674 if (self != MP_OBJ_NULL) {
675 args2[args2_len++] = self;
676 }
677
678 // copy the fixed position args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300679 mp_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
Damien George7a996392015-12-03 17:59:49 +0000680 args2_len += n_args;
Damien George230fec72014-03-30 21:21:24 +0100681
682 // extract the variable position args from the iterator
683 mp_obj_t iterable = mp_getiter(pos_seq);
684 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100685 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George230fec72014-03-30 21:21:24 +0100686 if (args2_len >= args2_alloc) {
687 args2 = m_renew(mp_obj_t, args2, args2_alloc, args2_alloc * 2);
688 args2_alloc *= 2;
689 }
690 args2[args2_len++] = item;
691 }
692 }
693
694 // The size of the args2 array now is the number of positional args.
695 uint pos_args_len = args2_len;
696
697 // Copy the fixed kw args.
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300698 mp_seq_copy(args2 + args2_len, args + n_args, 2 * n_kw, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100699 args2_len += 2 * n_kw;
700
701 // Extract (key,value) pairs from kw_dict dictionary and append to args2.
702 // Note that it can be arbitrary iterator.
703 if (kw_dict == MP_OBJ_NULL) {
704 // pass
705 } else if (MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
706 // dictionary
707 mp_map_t *map = mp_obj_dict_get_map(kw_dict);
708 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 +0000709 for (mp_uint_t i = 0; i < map->alloc; i++) {
710 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
Damien Georgefea40ad2016-04-21 16:51:36 +0100711 // the key must be a qstr, so intern it if it's a string
712 mp_obj_t key = map->table[i].key;
713 if (MP_OBJ_IS_TYPE(key, &mp_type_str)) {
714 key = mp_obj_str_intern(key);
715 }
716 args2[args2_len++] = key;
Damien George230fec72014-03-30 21:21:24 +0100717 args2[args2_len++] = map->table[i].value;
718 }
719 }
720 } else {
Damien George470c4292016-05-07 22:02:46 +0100721 // generic mapping:
722 // - call keys() to get an iterable of all keys in the mapping
723 // - call __getitem__ for each key to get the corresponding value
724
725 // get the keys iterable
726 mp_obj_t dest[3];
727 mp_load_method(kw_dict, MP_QSTR_keys, dest);
Damien George230fec72014-03-30 21:21:24 +0100728 mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest));
Damien George470c4292016-05-07 22:02:46 +0100729
730 mp_obj_t key;
731 while ((key = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
732 // expand size of args array if needed
Damien George230fec72014-03-30 21:21:24 +0100733 if (args2_len + 1 >= args2_alloc) {
734 uint new_alloc = args2_alloc * 2;
735 if (new_alloc < 4) {
736 new_alloc = 4;
737 }
738 args2 = m_renew(mp_obj_t, args2, args2_alloc, new_alloc);
739 args2_alloc = new_alloc;
740 }
Damien George470c4292016-05-07 22:02:46 +0100741
Damien Georgefea40ad2016-04-21 16:51:36 +0100742 // the key must be a qstr, so intern it if it's a string
Damien Georgefea40ad2016-04-21 16:51:36 +0100743 if (MP_OBJ_IS_TYPE(key, &mp_type_str)) {
744 key = mp_obj_str_intern(key);
745 }
Damien George470c4292016-05-07 22:02:46 +0100746
747 // get the value corresponding to the key
748 mp_load_method(kw_dict, MP_QSTR___getitem__, dest);
749 dest[2] = key;
750 mp_obj_t value = mp_call_method_n_kw(1, 0, dest);
751
752 // store the key/value pair in the argument array
Damien Georgefea40ad2016-04-21 16:51:36 +0100753 args2[args2_len++] = key;
Damien George470c4292016-05-07 22:02:46 +0100754 args2[args2_len++] = value;
Damien George230fec72014-03-30 21:21:24 +0100755 }
756 }
757
Paul Sokolovskye6c6fe32015-03-28 01:14:45 +0200758 out_args->fun = fun;
759 out_args->args = args2;
760 out_args->n_args = pos_args_len;
761 out_args->n_kw = (args2_len - pos_args_len) / 2;
762 out_args->n_alloc = args2_alloc;
763}
764
765mp_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 +0100766 mp_call_args_t out_args;
Paul Sokolovskye6c6fe32015-03-28 01:14:45 +0200767 mp_call_prepare_args_n_kw_var(have_self, n_args_n_kw, args, &out_args);
768
769 mp_obj_t res = mp_call_function_n_kw(out_args.fun, out_args.n_args, out_args.n_kw, out_args.args);
770 m_del(mp_obj_t, out_args.args, out_args.n_alloc);
Damien George230fec72014-03-30 21:21:24 +0100771
772 return res;
773}
774
Damien George932bf1c2014-01-18 23:42:49 +0000775// unpacked items are stored in reverse order into the array pointed to by items
Damien George4abff752014-08-30 14:59:21 +0100776void mp_unpack_sequence(mp_obj_t seq_in, mp_uint_t num, mp_obj_t *items) {
Damien George9c4cbe22014-08-30 14:04:14 +0100777 mp_uint_t seq_len;
Damien George3e1a5c12014-03-29 13:43:38 +0000778 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 +0000779 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000780 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000781 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
782 } else {
783 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000784 }
Damiend99b0522013-12-21 18:17:45 +0000785 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200786 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000787 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200788 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000789 }
Damien George4abff752014-08-30 14:59:21 +0100790 for (mp_uint_t i = 0; i < num; i++) {
Damien George932bf1c2014-01-18 23:42:49 +0000791 items[i] = seq_items[num - 1 - i];
792 }
Damien86c7fc72013-11-26 15:16:41 +0000793 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100794 mp_obj_t iterable = mp_getiter(seq_in);
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200795
796 for (seq_len = 0; seq_len < num; seq_len++) {
Damien Georged17926d2014-03-30 13:35:08 +0100797 mp_obj_t el = mp_iternext(iterable);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100798 if (el == MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200799 goto too_short;
800 }
801 items[num - 1 - seq_len] = el;
802 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100803 if (mp_iternext(iterable) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200804 goto too_long;
805 }
Damien86c7fc72013-11-26 15:16:41 +0000806 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200807 return;
808
809too_short:
Damien George1e9a92f2014-11-06 17:36:16 +0000810 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100811 mp_raise_msg(&mp_type_ValueError, "wrong number of values to unpack");
Damien George1e9a92f2014-11-06 17:36:16 +0000812 } else {
813 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Damien George2a1cca22016-03-14 22:40:39 +0000814 "need more than %d values to unpack", (int)seq_len));
Damien George1e9a92f2014-11-06 17:36:16 +0000815 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200816too_long:
Damien George1e9a92f2014-11-06 17:36:16 +0000817 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100818 mp_raise_msg(&mp_type_ValueError, "wrong number of values to unpack");
Damien George1e9a92f2014-11-06 17:36:16 +0000819 } else {
820 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Damien George2a1cca22016-03-14 22:40:39 +0000821 "too many values to unpack (expected %d)", (int)num));
Damien George1e9a92f2014-11-06 17:36:16 +0000822 }
Damien86c7fc72013-11-26 15:16:41 +0000823}
824
Damien George495d7812014-04-08 17:51:47 +0100825// unpacked items are stored in reverse order into the array pointed to by items
Damien George4abff752014-08-30 14:59:21 +0100826void mp_unpack_ex(mp_obj_t seq_in, mp_uint_t num_in, mp_obj_t *items) {
827 mp_uint_t num_left = num_in & 0xff;
828 mp_uint_t num_right = (num_in >> 8) & 0xff;
Damien Georgeeaaebf32014-09-23 10:59:05 +0100829 DEBUG_OP_printf("unpack ex " UINT_FMT " " UINT_FMT "\n", num_left, num_right);
Damien George9c4cbe22014-08-30 14:04:14 +0100830 mp_uint_t seq_len;
Damien George495d7812014-04-08 17:51:47 +0100831 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
832 mp_obj_t *seq_items;
833 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
834 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
835 } else {
836 if (num_left == 0 && num_right == 0) {
837 // *a, = b # sets a to b if b is a list
838 items[0] = seq_in;
839 return;
840 }
841 mp_obj_list_get(seq_in, &seq_len, &seq_items);
842 }
843 if (seq_len < num_left + num_right) {
844 goto too_short;
845 }
Damien George4abff752014-08-30 14:59:21 +0100846 for (mp_uint_t i = 0; i < num_right; i++) {
Damien George495d7812014-04-08 17:51:47 +0100847 items[i] = seq_items[seq_len - 1 - i];
848 }
849 items[num_right] = mp_obj_new_list(seq_len - num_left - num_right, seq_items + num_left);
Damien George4abff752014-08-30 14:59:21 +0100850 for (mp_uint_t i = 0; i < num_left; i++) {
Damien George495d7812014-04-08 17:51:47 +0100851 items[num_right + 1 + i] = seq_items[num_left - 1 - i];
852 }
853 } else {
854 // Generic iterable; this gets a bit messy: we unpack known left length to the
855 // items destination array, then the rest to a dynamically created list. Once the
856 // iterable is exhausted, we take from this list for the right part of the items.
857 // TODO Improve to waste less memory in the dynamically created list.
858 mp_obj_t iterable = mp_getiter(seq_in);
859 mp_obj_t item;
860 for (seq_len = 0; seq_len < num_left; seq_len++) {
861 item = mp_iternext(iterable);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100862 if (item == MP_OBJ_STOP_ITERATION) {
Damien George495d7812014-04-08 17:51:47 +0100863 goto too_short;
864 }
865 items[num_left + num_right + 1 - 1 - seq_len] = item;
866 }
Damien George999cedb2015-11-27 17:01:44 +0000867 mp_obj_list_t *rest = MP_OBJ_TO_PTR(mp_obj_new_list(0, NULL));
Damien Georgeea8d06c2014-04-17 23:19:36 +0100868 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George999cedb2015-11-27 17:01:44 +0000869 mp_obj_list_append(MP_OBJ_FROM_PTR(rest), item);
Damien George495d7812014-04-08 17:51:47 +0100870 }
Damien Georgeca6d75f2014-08-30 15:17:47 +0100871 if (rest->len < num_right) {
Damien George495d7812014-04-08 17:51:47 +0100872 goto too_short;
873 }
Damien George999cedb2015-11-27 17:01:44 +0000874 items[num_right] = MP_OBJ_FROM_PTR(rest);
Damien George4abff752014-08-30 14:59:21 +0100875 for (mp_uint_t i = 0; i < num_right; i++) {
Damien Georgeca6d75f2014-08-30 15:17:47 +0100876 items[num_right - 1 - i] = rest->items[rest->len - num_right + i];
Damien George495d7812014-04-08 17:51:47 +0100877 }
Damien George999cedb2015-11-27 17:01:44 +0000878 mp_obj_list_set_len(MP_OBJ_FROM_PTR(rest), rest->len - num_right);
Damien George495d7812014-04-08 17:51:47 +0100879 }
880 return;
881
882too_short:
Damien George1e9a92f2014-11-06 17:36:16 +0000883 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100884 mp_raise_msg(&mp_type_ValueError, "wrong number of values to unpack");
Damien George1e9a92f2014-11-06 17:36:16 +0000885 } else {
886 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Damien George2a1cca22016-03-14 22:40:39 +0000887 "need more than %d values to unpack", (int)seq_len));
Damien George1e9a92f2014-11-06 17:36:16 +0000888 }
Damien George495d7812014-04-08 17:51:47 +0100889}
890
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300891mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000892 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300893 // use load_method
Damien George062478e2014-01-09 20:57:50 +0000894 mp_obj_t dest[2];
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300895 mp_load_method(base, attr, dest);
896 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000897 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000898 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000899 } else {
900 // load_method returned a method, so build a bound method object
901 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000902 }
Damiend99b0522013-12-21 18:17:45 +0000903}
904
Damien George06593fb2015-06-19 12:49:10 +0000905#if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
906
907// The following "checked fun" type is local to the mp_convert_member_lookup
908// function, and serves to check that the first argument to a builtin function
909// has the correct type.
910
911typedef struct _mp_obj_checked_fun_t {
912 mp_obj_base_t base;
913 const mp_obj_type_t *type;
914 mp_obj_t fun;
915} mp_obj_checked_fun_t;
916
Damien Georgea0c97812016-01-03 09:59:18 +0000917STATIC 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 +0000918 mp_obj_checked_fun_t *self = MP_OBJ_TO_PTR(self_in);
Damien George06593fb2015-06-19 12:49:10 +0000919 if (n_args > 0) {
920 const mp_obj_type_t *arg0_type = mp_obj_get_type(args[0]);
921 if (arg0_type != self->type) {
922 if (MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_DETAILED) {
Damien George7d0d7212016-10-17 12:17:37 +1100923 mp_raise_msg(&mp_type_TypeError, "argument has wrong type");
Damien George06593fb2015-06-19 12:49:10 +0000924 } else {
925 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
926 "argument should be a '%q' not a '%q'", self->type->name, arg0_type->name));
927 }
928 }
929 }
930 return mp_call_function_n_kw(self->fun, n_args, n_kw, args);
931}
932
933STATIC const mp_obj_type_t mp_type_checked_fun = {
934 { &mp_type_type },
935 .name = MP_QSTR_function,
936 .call = checked_fun_call,
937};
938
939STATIC mp_obj_t mp_obj_new_checked_fun(const mp_obj_type_t *type, mp_obj_t fun) {
940 mp_obj_checked_fun_t *o = m_new_obj(mp_obj_checked_fun_t);
941 o->base.type = &mp_type_checked_fun;
942 o->type = type;
943 o->fun = fun;
Damien George999cedb2015-11-27 17:01:44 +0000944 return MP_OBJ_FROM_PTR(o);
Damien George06593fb2015-06-19 12:49:10 +0000945}
946
947#endif // MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
948
Damien George55b74d12015-03-21 14:21:54 +0000949// Given a member that was extracted from an instance, convert it correctly
950// and put the result in the dest[] array for a possible method call.
951// Conversion means dealing with static/class methods, callables, and values.
952// see http://docs.python.org/3/howto/descriptor.html
953void mp_convert_member_lookup(mp_obj_t self, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest) {
954 if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
955 // return just the function
Damien George999cedb2015-11-27 17:01:44 +0000956 dest[0] = ((mp_obj_static_class_method_t*)MP_OBJ_TO_PTR(member))->fun;
Damien George55b74d12015-03-21 14:21:54 +0000957 } else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
958 // return a bound method, with self being the type of this object
Damien George3ff259a2015-12-09 17:30:01 +0000959 // this type should be the type of the original instance, not the base
960 // type (which is what is passed in the 'type' argument to this function)
961 if (self != MP_OBJ_NULL) {
962 type = mp_obj_get_type(self);
963 }
Damien George999cedb2015-11-27 17:01:44 +0000964 dest[0] = ((mp_obj_static_class_method_t*)MP_OBJ_TO_PTR(member))->fun;
965 dest[1] = MP_OBJ_FROM_PTR(type);
Damien George55b74d12015-03-21 14:21:54 +0000966 } else if (MP_OBJ_IS_TYPE(member, &mp_type_type)) {
967 // Don't try to bind types (even though they're callable)
968 dest[0] = member;
Damien George78913212015-12-26 12:41:31 +0000969 } else if (MP_OBJ_IS_FUN(member)
970 || (MP_OBJ_IS_OBJ(member)
971 && (((mp_obj_base_t*)MP_OBJ_TO_PTR(member))->type->name == MP_QSTR_closure
972 || ((mp_obj_base_t*)MP_OBJ_TO_PTR(member))->type->name == MP_QSTR_generator))) {
973 // only functions, closures and generators objects can be bound to self
Damien George06593fb2015-06-19 12:49:10 +0000974 #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
Damien George571e6f22016-10-18 11:49:27 +1100975 const mp_obj_type_t *m_type = ((mp_obj_base_t*)MP_OBJ_TO_PTR(member))->type;
976 if (self == MP_OBJ_NULL
977 && (m_type == &mp_type_fun_builtin_0
978 || m_type == &mp_type_fun_builtin_1
979 || m_type == &mp_type_fun_builtin_2
980 || m_type == &mp_type_fun_builtin_3
981 || m_type == &mp_type_fun_builtin_var)) {
Damien George06593fb2015-06-19 12:49:10 +0000982 // we extracted a builtin method without a first argument, so we must
983 // wrap this function in a type checker
984 dest[0] = mp_obj_new_checked_fun(type, member);
985 } else
986 #endif
987 {
988 // return a bound method, with self being this object
989 dest[0] = member;
990 dest[1] = self;
991 }
Damien George55b74d12015-03-21 14:21:54 +0000992 } else {
993 // class member is a value, so just return that value
994 dest[0] = member;
995 }
996}
997
Damien George7c9c6672014-01-25 00:17:36 +0000998// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
999// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
1000// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Paul Sokolovsky07b8dc62015-03-21 00:58:07 +02001001void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +00001002 // clear output to indicate no attribute/method found yet
1003 dest[0] = MP_OBJ_NULL;
1004 dest[1] = MP_OBJ_NULL;
1005
1006 // get the type
Paul Sokolovsky07b8dc62015-03-21 00:58:07 +02001007 mp_obj_type_t *type = mp_obj_get_type(obj);
Damien George062478e2014-01-09 20:57:50 +00001008
Damien Georgee44d26a2014-03-31 22:57:56 +01001009 // look for built-in names
1010 if (0) {
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +03001011#if MICROPY_CPYTHON_COMPAT
Damien Georgee44d26a2014-03-31 22:57:56 +01001012 } else if (attr == MP_QSTR___class__) {
1013 // a.__class__ is equivalent to type(a)
Damien George999cedb2015-11-27 17:01:44 +00001014 dest[0] = MP_OBJ_FROM_PTR(type);
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +03001015#endif
Damien Georgee44d26a2014-03-31 22:57:56 +01001016
1017 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George999cedb2015-11-27 17:01:44 +00001018 dest[0] = MP_OBJ_FROM_PTR(&mp_builtin_next_obj);
Paul Sokolovsky07b8dc62015-03-21 00:58:07 +02001019 dest[1] = obj;
Damien Georgee44d26a2014-03-31 22:57:56 +01001020
Damien Georgeb1bbe962015-04-01 14:10:50 +00001021 } else if (type->attr != NULL) {
Damien Georgee44d26a2014-03-31 22:57:56 +01001022 // this type can do its own load, so call it
Damien Georgeb1bbe962015-04-01 14:10:50 +00001023 type->attr(obj, attr, dest);
Damien Georgee44d26a2014-03-31 22:57:56 +01001024
1025 } else if (type->locals_dict != NULL) {
1026 // generic method lookup
1027 // this is a lookup in the object (ie not class or type)
Damien George999cedb2015-11-27 17:01:44 +00001028 assert(type->locals_dict->base.type == &mp_type_dict); // Micro Python restriction, for now
1029 mp_map_t *locals_map = &type->locals_dict->map;
Damien Georgee44d26a2014-03-31 22:57:56 +01001030 mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
1031 if (elem != NULL) {
Damien George55b74d12015-03-21 14:21:54 +00001032 mp_convert_member_lookup(obj, type, elem->value, dest);
Damiend57eba52013-11-02 23:58:14 +00001033 }
Damiena3977762013-10-09 23:10:10 +01001034 }
Damien George7c9c6672014-01-25 00:17:36 +00001035}
Damiena3977762013-10-09 23:10:10 +01001036
Damien Georged17926d2014-03-30 13:35:08 +01001037void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George7c9c6672014-01-25 00:17:36 +00001038 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
1039
Damien Georged17926d2014-03-30 13:35:08 +01001040 mp_load_method_maybe(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +00001041
1042 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +00001043 // no attribute/method called attr
Damien George1e9a92f2014-11-06 17:36:16 +00001044 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +11001045 mp_raise_msg(&mp_type_AttributeError, "no such attribute");
Damien George062478e2014-01-09 20:57:50 +00001046 } else {
Damien George1e9a92f2014-11-06 17:36:16 +00001047 // following CPython, we give a more detailed error message for type objects
1048 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
1049 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
Damien George044c4732015-04-11 13:03:37 +01001050 "type object '%q' has no attribute '%q'",
Damien George999cedb2015-11-27 17:01:44 +00001051 ((mp_obj_type_t*)MP_OBJ_TO_PTR(base))->name, attr));
Damien George1e9a92f2014-11-06 17:36:16 +00001052 } else {
1053 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
Damien George044c4732015-04-11 13:03:37 +01001054 "'%s' object has no attribute '%q'",
1055 mp_obj_get_type_str(base), attr));
Damien George1e9a92f2014-11-06 17:36:16 +00001056 }
Damien George062478e2014-01-09 20:57:50 +00001057 }
1058 }
Damiena3977762013-10-09 23:10:10 +01001059}
1060
Damien Georged17926d2014-03-30 13:35:08 +01001061void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +01001062 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +00001063 mp_obj_type_t *type = mp_obj_get_type(base);
Damien Georgeb1bbe962015-04-01 14:10:50 +00001064 if (type->attr != NULL) {
1065 mp_obj_t dest[2] = {MP_OBJ_SENTINEL, value};
1066 type->attr(base, attr, dest);
1067 if (dest[0] == MP_OBJ_NULL) {
1068 // success
Damien George062478e2014-01-09 20:57:50 +00001069 return;
1070 }
Damiena3977762013-10-09 23:10:10 +01001071 }
Damien George1e9a92f2014-11-06 17:36:16 +00001072 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +11001073 mp_raise_msg(&mp_type_AttributeError, "no such attribute");
Damien George1e9a92f2014-11-06 17:36:16 +00001074 } else {
1075 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
Damien George044c4732015-04-11 13:03:37 +01001076 "'%s' object has no attribute '%q'",
1077 mp_obj_get_type_str(base), attr));
Damien George1e9a92f2014-11-06 17:36:16 +00001078 }
Damiena3977762013-10-09 23:10:10 +01001079}
1080
Damien Georged17926d2014-03-30 13:35:08 +01001081mp_obj_t mp_getiter(mp_obj_t o_in) {
Paul Sokolovskyc48d6f72014-05-11 20:32:39 +03001082 assert(o_in);
Damien Georgef6532bb2015-02-15 01:10:13 +00001083
1084 // check for native getiter (corresponds to __iter__)
Damien George5fa93b62014-01-22 14:35:10 +00001085 mp_obj_type_t *type = mp_obj_get_type(o_in);
1086 if (type->getiter != NULL) {
Paul Sokolovskyc48d6f72014-05-11 20:32:39 +03001087 mp_obj_t iter = type->getiter(o_in);
Damien Georgef6532bb2015-02-15 01:10:13 +00001088 if (iter != MP_OBJ_NULL) {
1089 return iter;
Paul Sokolovskyc48d6f72014-05-11 20:32:39 +03001090 }
Damien Georgef6532bb2015-02-15 01:10:13 +00001091 }
1092
1093 // check for __getitem__
1094 mp_obj_t dest[2];
1095 mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
1096 if (dest[0] != MP_OBJ_NULL) {
1097 // __getitem__ exists, create and return an iterator
1098 return mp_obj_new_getitem_iter(dest);
1099 }
1100
1101 // object not iterable
1102 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +11001103 mp_raise_msg(&mp_type_TypeError, "object not iterable");
Damience89a212013-10-15 22:25:17 +01001104 } else {
Damien Georgef6532bb2015-02-15 01:10:13 +00001105 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
1106 "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
Damience89a212013-10-15 22:25:17 +01001107 }
1108}
1109
Damien Georgeea8d06c2014-04-17 23:19:36 +01001110// may return MP_OBJ_STOP_ITERATION as an optimisation instead of raise StopIteration()
Damien George66eaf842014-03-26 19:27:58 +00001111// may also raise StopIteration()
Damien Georged17926d2014-03-30 13:35:08 +01001112mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +00001113 mp_obj_type_t *type = mp_obj_get_type(o_in);
1114 if (type->iternext != NULL) {
1115 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +01001116 } else {
Damien George9e6e9352014-03-26 18:37:06 +00001117 // check for __next__ method
1118 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +01001119 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George9e6e9352014-03-26 18:37:06 +00001120 if (dest[0] != MP_OBJ_NULL) {
1121 // __next__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +01001122 return mp_call_method_n_kw(0, 0, dest);
Damien George9e6e9352014-03-26 18:37:06 +00001123 } else {
Damien George1e9a92f2014-11-06 17:36:16 +00001124 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +11001125 mp_raise_msg(&mp_type_TypeError, "object not an iterator");
Damien George1e9a92f2014-11-06 17:36:16 +00001126 } else {
1127 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
1128 "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
1129 }
Damien George9e6e9352014-03-26 18:37:06 +00001130 }
Damien Georgec5966122014-02-15 16:10:44 +00001131 }
1132}
1133
Damien Georgeea8d06c2014-04-17 23:19:36 +01001134// will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration() (or any subclass thereof)
Damien George66eaf842014-03-26 19:27:58 +00001135// may raise other exceptions
Damien Georged17926d2014-03-30 13:35:08 +01001136mp_obj_t mp_iternext(mp_obj_t o_in) {
Damien George953c23b2015-06-03 22:19:41 +01001137 MP_STACK_CHECK(); // enumerate, filter, map and zip can recursively call mp_iternext
Damien George66eaf842014-03-26 19:27:58 +00001138 mp_obj_type_t *type = mp_obj_get_type(o_in);
1139 if (type->iternext != NULL) {
1140 return type->iternext(o_in);
1141 } else {
1142 // check for __next__ method
1143 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +01001144 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George66eaf842014-03-26 19:27:58 +00001145 if (dest[0] != MP_OBJ_NULL) {
1146 // __next__ exists, call it and return its result
1147 nlr_buf_t nlr;
1148 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001149 mp_obj_t ret = mp_call_method_n_kw(0, 0, dest);
Damien George66eaf842014-03-26 19:27:58 +00001150 nlr_pop();
1151 return ret;
1152 } else {
Damien George999cedb2015-11-27 17:01:44 +00001153 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 +01001154 return MP_OBJ_STOP_ITERATION;
Damien George66eaf842014-03-26 19:27:58 +00001155 } else {
Damien George999cedb2015-11-27 17:01:44 +00001156 nlr_jump(nlr.ret_val);
Damien George66eaf842014-03-26 19:27:58 +00001157 }
1158 }
1159 } else {
Damien George1e9a92f2014-11-06 17:36:16 +00001160 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +11001161 mp_raise_msg(&mp_type_TypeError, "object not an iterator");
Damien George1e9a92f2014-11-06 17:36:16 +00001162 } else {
1163 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
1164 "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
1165 }
Damien George66eaf842014-03-26 19:27:58 +00001166 }
1167 }
1168}
1169
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001170// TODO: Unclear what to do with StopIterarion exception here.
1171mp_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 +03001172 assert((send_value != MP_OBJ_NULL) ^ (throw_value != MP_OBJ_NULL));
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001173 mp_obj_type_t *type = mp_obj_get_type(self_in);
1174
1175 if (type == &mp_type_gen_instance) {
1176 return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val);
1177 }
1178
1179 if (type->iternext != NULL && send_value == mp_const_none) {
1180 mp_obj_t ret = type->iternext(self_in);
Paul Sokolovsky4ed7b7f2015-05-10 00:41:34 +03001181 if (ret != MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001182 *ret_val = ret;
1183 return MP_VM_RETURN_YIELD;
1184 } else {
1185 // Emulate raise StopIteration()
1186 // Special case, handled in vm.c
1187 *ret_val = MP_OBJ_NULL;
1188 return MP_VM_RETURN_NORMAL;
1189 }
1190 }
1191
1192 mp_obj_t dest[3]; // Reserve slot for send() arg
1193
Paul Sokolovsky79d996a2016-11-15 01:10:34 +03001194 // Python instance iterator protocol
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001195 if (send_value == mp_const_none) {
1196 mp_load_method_maybe(self_in, MP_QSTR___next__, dest);
1197 if (dest[0] != MP_OBJ_NULL) {
Paul Sokolovsky79d996a2016-11-15 01:10:34 +03001198 nlr_buf_t nlr;
1199 if (nlr_push(&nlr) == 0) {
1200 *ret_val = mp_call_method_n_kw(0, 0, dest);
1201 nlr_pop();
1202 return MP_VM_RETURN_YIELD;
1203 } else {
Paul Sokolovskya0b2c6a2016-11-15 01:40:23 +03001204 *ret_val = MP_OBJ_FROM_PTR(nlr.ret_val);
Paul Sokolovsky79d996a2016-11-15 01:10:34 +03001205 return MP_VM_RETURN_EXCEPTION;
1206 }
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001207 }
1208 }
1209
Paul Sokolovsky79d996a2016-11-15 01:10:34 +03001210 // Either python instance generator protocol, or native object
1211 // generator protocol.
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001212 if (send_value != MP_OBJ_NULL) {
1213 mp_load_method(self_in, MP_QSTR_send, dest);
1214 dest[2] = send_value;
Paul Sokolovsky79d996a2016-11-15 01:10:34 +03001215 // TODO: This should have exception wrapping like __next__ case
1216 // above. Not done right away to think how to optimize native
1217 // generators better, see:
1218 // https://github.com/micropython/micropython/issues/2628
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001219 *ret_val = mp_call_method_n_kw(1, 0, dest);
1220 return MP_VM_RETURN_YIELD;
1221 }
1222
1223 if (throw_value != MP_OBJ_NULL) {
Damien George999cedb2015-11-27 17:01:44 +00001224 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 +03001225 mp_load_method_maybe(self_in, MP_QSTR_close, dest);
1226 if (dest[0] != MP_OBJ_NULL) {
Paul Sokolovsky4a60cac2015-05-10 02:39:45 +03001227 // TODO: Exceptions raised in close() are not propagated,
1228 // printed to sys.stderr
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001229 *ret_val = mp_call_method_n_kw(0, 0, dest);
1230 // We assume one can't "yield" from close()
1231 return MP_VM_RETURN_NORMAL;
1232 }
1233 }
Paul Sokolovskya2109d92014-03-31 04:14:30 +03001234 mp_load_method_maybe(self_in, MP_QSTR_throw, dest);
1235 if (dest[0] != MP_OBJ_NULL) {
1236 *ret_val = mp_call_method_n_kw(1, 0, &throw_value);
1237 // If .throw() method returned, we assume it's value to yield
Damien Georgeea13f402014-04-05 18:32:08 +01001238 // - any exception would be thrown with nlr_raise().
Paul Sokolovskya2109d92014-03-31 04:14:30 +03001239 return MP_VM_RETURN_YIELD;
1240 }
1241 // If there's nowhere to throw exception into, then we assume that object
1242 // is just incapable to handle it, so any exception thrown into it
1243 // will be propagated up. This behavior is approved by test_pep380.py
1244 // test_delegation_of_close_to_non_generator(),
1245 // test_delegating_throw_to_non_generator()
1246 *ret_val = throw_value;
1247 return MP_VM_RETURN_EXCEPTION;
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001248 }
1249
1250 assert(0);
1251 return MP_VM_RETURN_NORMAL; // Should be unreachable
1252}
1253
Damien Georged17926d2014-03-30 13:35:08 +01001254mp_obj_t mp_make_raise_obj(mp_obj_t o) {
Damien Georgec5966122014-02-15 16:10:44 +00001255 DEBUG_printf("raise %p\n", o);
1256 if (mp_obj_is_exception_type(o)) {
1257 // o is an exception type (it is derived from BaseException (or is BaseException))
1258 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +00001259 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
1260 // could have const instances in ROM which we return here instead
Damien Georged17926d2014-03-30 13:35:08 +01001261 return mp_call_function_n_kw(o, 0, 0, NULL);
Damien Georgec5966122014-02-15 16:10:44 +00001262 } else if (mp_obj_is_exception_instance(o)) {
1263 // o is an instance of an exception, so use it as the exception
1264 return o;
1265 } else {
1266 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
1267 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +01001268 }
1269}
1270
Damien Georged17926d2014-03-30 13:35:08 +01001271mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Paul Sokolovsky6557a092015-06-27 00:32:35 +03001272 DEBUG_printf("import name '%s' level=%d\n", qstr_str(name), MP_OBJ_SMALL_INT_VALUE(level));
Damien George64131f32014-02-06 20:31:44 +00001273
Damiendb4c3612013-12-10 17:27:24 +00001274 // build args array
Damiend99b0522013-12-21 18:17:45 +00001275 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +00001276 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +00001277 args[1] = mp_const_none; // TODO should be globals
1278 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +00001279 args[3] = fromlist;
1280 args[4] = level; // must be 0; we don't yet support other values
1281
1282 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +00001283 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001284}
1285
Damien Georged17926d2014-03-30 13:35:08 +01001286mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +00001287 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
1288
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001289 mp_obj_t dest[2];
1290
1291 mp_load_method_maybe(module, name, dest);
1292
1293 if (dest[1] != MP_OBJ_NULL) {
1294 // Hopefully we can't import bound method from an object
1295import_error:
Damien George044c4732015-04-11 13:03:37 +01001296 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "cannot import name %q", name));
Damiendb4c3612013-12-10 17:27:24 +00001297 }
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001298
1299 if (dest[0] != MP_OBJ_NULL) {
1300 return dest[0];
1301 }
1302
1303 // See if it's a package, then can try FS import
Paul Sokolovskye5a37592014-10-25 21:04:13 +03001304 if (!mp_obj_is_package(module)) {
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001305 goto import_error;
1306 }
1307
1308 mp_load_method_maybe(module, MP_QSTR___name__, dest);
Damien Georged182b982014-08-30 14:19:41 +01001309 mp_uint_t pkg_name_len;
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001310 const char *pkg_name = mp_obj_str_get_data(dest[0], &pkg_name_len);
1311
stijn01d6be42014-05-05 12:18:27 +02001312 const uint dot_name_len = pkg_name_len + 1 + qstr_len(name);
1313 char *dot_name = alloca(dot_name_len);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001314 memcpy(dot_name, pkg_name, pkg_name_len);
1315 dot_name[pkg_name_len] = '.';
1316 memcpy(dot_name + pkg_name_len + 1, qstr_str(name), qstr_len(name));
stijn01d6be42014-05-05 12:18:27 +02001317 qstr dot_name_q = qstr_from_strn(dot_name, dot_name_len);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001318
1319 mp_obj_t args[5];
1320 args[0] = MP_OBJ_NEW_QSTR(dot_name_q);
1321 args[1] = mp_const_none; // TODO should be globals
1322 args[2] = mp_const_none; // TODO should be locals
1323 args[3] = mp_const_true; // Pass sentinel "non empty" value to force returning of leaf module
Paul Sokolovsky69f18672014-04-12 02:47:46 +03001324 args[4] = MP_OBJ_NEW_SMALL_INT(0);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001325
1326 // TODO lookup __import__ and call that instead of going straight to builtin implementation
1327 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001328}
1329
Damien Georged17926d2014-03-30 13:35:08 +01001330void mp_import_all(mp_obj_t module) {
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001331 DEBUG_printf("import all %p\n", module);
1332
Paul Sokolovsky599bbc12014-04-18 04:11:19 +03001333 // TODO: Support __all__
Damien George999cedb2015-11-27 17:01:44 +00001334 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 +00001335 for (mp_uint_t i = 0; i < map->alloc; i++) {
Damien George8b0535e2014-04-05 21:53:54 +01001336 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
Paul Sokolovsky599bbc12014-04-18 04:11:19 +03001337 qstr name = MP_OBJ_QSTR_VALUE(map->table[i].key);
1338 if (*qstr_str(name) != '_') {
1339 mp_store_name(name, map->table[i].value);
1340 }
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001341 }
1342 }
1343}
1344
Damien Georgedd5353a2015-12-18 12:35:44 +00001345#if MICROPY_ENABLE_COMPILER
1346
Damien Georgec4d08682014-10-05 20:13:34 +01001347// this is implemented in this file so it can optimise access to locals/globals
1348mp_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 +00001349 // save context
1350 mp_obj_dict_t *volatile old_globals = mp_globals_get();
1351 mp_obj_dict_t *volatile old_locals = mp_locals_get();
Damien Georgec4d08682014-10-05 20:13:34 +01001352
Damien George0bfc7632015-02-07 18:33:58 +00001353 // set new context
Damien Georgec4d08682014-10-05 20:13:34 +01001354 mp_globals_set(globals);
1355 mp_locals_set(locals);
1356
Damien Georgec4d08682014-10-05 20:13:34 +01001357 nlr_buf_t nlr;
1358 if (nlr_push(&nlr) == 0) {
Damien George0bfc7632015-02-07 18:33:58 +00001359 qstr source_name = lex->source_name;
Damien George58e0f4a2015-09-23 10:50:43 +01001360 mp_parse_tree_t parse_tree = mp_parse(lex, parse_input_kind);
1361 mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, false);
Damien George0bfc7632015-02-07 18:33:58 +00001362
1363 mp_obj_t ret;
1364 if (MICROPY_PY_BUILTINS_COMPILE && globals == NULL) {
1365 // for compile only, return value is the module function
1366 ret = module_fun;
1367 } else {
1368 // execute module function and get return value
1369 ret = mp_call_function_0(module_fun);
1370 }
1371
1372 // finish nlr block, restore context and return value
Damien Georgec4d08682014-10-05 20:13:34 +01001373 nlr_pop();
1374 mp_globals_set(old_globals);
1375 mp_locals_set(old_locals);
1376 return ret;
1377 } else {
1378 // exception; restore context and re-raise same exception
1379 mp_globals_set(old_globals);
1380 mp_locals_set(old_locals);
Damien George999cedb2015-11-27 17:01:44 +00001381 nlr_jump(nlr.ret_val);
Damien Georgec4d08682014-10-05 20:13:34 +01001382 }
1383}
1384
Damien Georgedd5353a2015-12-18 12:35:44 +00001385#endif // MICROPY_ENABLE_COMPILER
1386
Damien Georgeb0261342014-09-23 18:10:17 +01001387void *m_malloc_fail(size_t num_bytes) {
Damien George978d2e52016-01-08 13:49:58 +00001388 DEBUG_printf("memory allocation failed, allocating %u bytes\n", (uint)num_bytes);
Damien George40914452014-10-09 16:44:43 +01001389 if (0) {
1390 // dummy
1391 #if MICROPY_ENABLE_GC
1392 } else if (gc_is_locked()) {
Damien George7d0d7212016-10-17 12:17:37 +11001393 mp_raise_msg(&mp_type_MemoryError, "memory allocation failed, heap is locked");
Damien George40914452014-10-09 16:44:43 +01001394 #endif
Dave Hylands3556e452014-10-07 00:50:20 -07001395 } else {
Damien George40914452014-10-09 16:44:43 +01001396 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError,
Damien George978d2e52016-01-08 13:49:58 +00001397 "memory allocation failed, allocating %u bytes", (uint)num_bytes));
Dave Hylands3556e452014-10-07 00:50:20 -07001398 }
Damien George6902eed2014-04-04 10:52:59 +00001399}
1400
Paul Sokolovsky9e1b61d2016-08-12 21:26:12 +03001401NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg) {
1402 nlr_raise(mp_obj_new_exception_msg(exc_type, msg));
1403}
1404
1405NORETURN void mp_raise_ValueError(const char *msg) {
1406 mp_raise_msg(&mp_type_ValueError, msg);
1407}
1408
1409NORETURN void mp_raise_TypeError(const char *msg) {
1410 mp_raise_msg(&mp_type_TypeError, msg);
1411}
1412
Damien George3a0a7712016-10-07 13:31:59 +11001413NORETURN void mp_raise_OSError(int errno_) {
1414 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_)));
1415}
1416
Paul Sokolovsky7e4a2b02014-06-07 23:22:41 +03001417NORETURN void mp_not_implemented(const char *msg) {
Paul Sokolovsky9e1b61d2016-08-12 21:26:12 +03001418 mp_raise_msg(&mp_type_NotImplementedError, msg);
Paul Sokolovsky7e4a2b02014-06-07 23:22:41 +03001419}