1 | /* |
2 | * This file is part of the MicroPython project, http://micropython.org/ |
3 | * |
4 | * The MIT License (MIT) |
5 | * |
6 | * Copyright (c) 2013, 2014 Damien P. George |
7 | * |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal |
10 | * in the Software without restriction, including without limitation the rights |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12 | * copies of the Software, and to permit persons to whom the Software is |
13 | * furnished to do so, subject to the following conditions: |
14 | * |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. |
17 | * |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24 | * THE SOFTWARE. |
25 | */ |
26 | |
27 | #include <stdio.h> |
28 | #include <assert.h> |
29 | |
30 | #include "py/smallint.h" |
31 | #include "py/objint.h" |
32 | #include "py/objstr.h" |
33 | #include "py/objtype.h" |
34 | #include "py/runtime.h" |
35 | #include "py/builtin.h" |
36 | #include "py/stream.h" |
37 | |
38 | #if MICROPY_PY_BUILTINS_FLOAT |
39 | #include <math.h> |
40 | #endif |
41 | |
42 | #if MICROPY_PY_IO |
43 | extern struct _mp_dummy_t mp_sys_stdout_obj; // type is irrelevant, just need pointer |
44 | #endif |
45 | |
46 | // args[0] is function from class body |
47 | // args[1] is class name |
48 | // args[2:] are base objects |
49 | STATIC mp_obj_t mp_builtin___build_class__(size_t n_args, const mp_obj_t *args) { |
50 | assert(2 <= n_args); |
51 | |
52 | // set the new classes __locals__ object |
53 | mp_obj_dict_t *old_locals = mp_locals_get(); |
54 | mp_obj_t class_locals = mp_obj_new_dict(0); |
55 | mp_locals_set(MP_OBJ_TO_PTR(class_locals)); |
56 | |
57 | // call the class code |
58 | mp_obj_t cell = mp_call_function_0(args[0]); |
59 | |
60 | // restore old __locals__ object |
61 | mp_locals_set(old_locals); |
62 | |
63 | // get the class type (meta object) from the base objects |
64 | mp_obj_t meta; |
65 | if (n_args == 2) { |
66 | // no explicit bases, so use 'type' |
67 | meta = MP_OBJ_FROM_PTR(&mp_type_type); |
68 | } else { |
69 | // use type of first base object |
70 | meta = MP_OBJ_FROM_PTR(mp_obj_get_type(args[2])); |
71 | } |
72 | |
73 | // TODO do proper metaclass resolution for multiple base objects |
74 | |
75 | // create the new class using a call to the meta object |
76 | mp_obj_t meta_args[3]; |
77 | meta_args[0] = args[1]; // class name |
78 | meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases |
79 | meta_args[2] = class_locals; // dict of members |
80 | mp_obj_t new_class = mp_call_function_n_kw(meta, 3, 0, meta_args); |
81 | |
82 | // store into cell if neede |
83 | if (cell != mp_const_none) { |
84 | mp_obj_cell_set(cell, new_class); |
85 | } |
86 | |
87 | return new_class; |
88 | } |
89 | MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__); |
90 | |
91 | STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) { |
92 | return mp_unary_op(MP_UNARY_OP_ABS, o_in); |
93 | } |
94 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs); |
95 | |
96 | STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) { |
97 | mp_obj_iter_buf_t iter_buf; |
98 | mp_obj_t iterable = mp_getiter(o_in, &iter_buf); |
99 | mp_obj_t item; |
100 | while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { |
101 | if (!mp_obj_is_true(item)) { |
102 | return mp_const_false; |
103 | } |
104 | } |
105 | return mp_const_true; |
106 | } |
107 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all); |
108 | |
109 | STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) { |
110 | mp_obj_iter_buf_t iter_buf; |
111 | mp_obj_t iterable = mp_getiter(o_in, &iter_buf); |
112 | mp_obj_t item; |
113 | while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { |
114 | if (mp_obj_is_true(item)) { |
115 | return mp_const_true; |
116 | } |
117 | } |
118 | return mp_const_false; |
119 | } |
120 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any); |
121 | |
122 | STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) { |
123 | mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_b_brace_close_), o_in }; |
124 | return mp_obj_str_format(MP_ARRAY_SIZE(args), args, NULL); |
125 | } |
126 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin); |
127 | |
128 | STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) { |
129 | if (mp_obj_is_callable(o_in)) { |
130 | return mp_const_true; |
131 | } else { |
132 | return mp_const_false; |
133 | } |
134 | } |
135 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable); |
136 | |
137 | STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) { |
138 | #if MICROPY_PY_BUILTINS_STR_UNICODE |
139 | mp_uint_t c = mp_obj_get_int(o_in); |
140 | uint8_t str[4]; |
141 | int len = 0; |
142 | if (c < 0x80) { |
143 | *str = c; |
144 | len = 1; |
145 | } else if (c < 0x800) { |
146 | str[0] = (c >> 6) | 0xC0; |
147 | str[1] = (c & 0x3F) | 0x80; |
148 | len = 2; |
149 | } else if (c < 0x10000) { |
150 | str[0] = (c >> 12) | 0xE0; |
151 | str[1] = ((c >> 6) & 0x3F) | 0x80; |
152 | str[2] = (c & 0x3F) | 0x80; |
153 | len = 3; |
154 | } else if (c < 0x110000) { |
155 | str[0] = (c >> 18) | 0xF0; |
156 | str[1] = ((c >> 12) & 0x3F) | 0x80; |
157 | str[2] = ((c >> 6) & 0x3F) | 0x80; |
158 | str[3] = (c & 0x3F) | 0x80; |
159 | len = 4; |
160 | } else { |
161 | mp_raise_ValueError(MP_ERROR_TEXT("chr() arg not in range(0x110000)" )); |
162 | } |
163 | return mp_obj_new_str_via_qstr((char *)str, len); |
164 | #else |
165 | mp_int_t ord = mp_obj_get_int(o_in); |
166 | if (0 <= ord && ord <= 0xff) { |
167 | uint8_t str[1] = {ord}; |
168 | return mp_obj_new_str_via_qstr((char *)str, 1); |
169 | } else { |
170 | mp_raise_ValueError(MP_ERROR_TEXT("chr() arg not in range(256)" )); |
171 | } |
172 | #endif |
173 | } |
174 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr); |
175 | |
176 | STATIC mp_obj_t mp_builtin_dir(size_t n_args, const mp_obj_t *args) { |
177 | mp_obj_t dir = mp_obj_new_list(0, NULL); |
178 | if (n_args == 0) { |
179 | // Make a list of names in the local namespace |
180 | mp_obj_dict_t *dict = mp_locals_get(); |
181 | for (size_t i = 0; i < dict->map.alloc; i++) { |
182 | if (mp_map_slot_is_filled(&dict->map, i)) { |
183 | mp_obj_list_append(dir, dict->map.table[i].key); |
184 | } |
185 | } |
186 | } else { // n_args == 1 |
187 | // Make a list of names in the given object |
188 | // Implemented by probing all possible qstrs with mp_load_method_maybe |
189 | size_t nqstr = QSTR_TOTAL(); |
190 | for (size_t i = MP_QSTR_ + 1; i < nqstr; ++i) { |
191 | mp_obj_t dest[2]; |
192 | mp_load_method_protected(args[0], i, dest, false); |
193 | if (dest[0] != MP_OBJ_NULL) { |
194 | #if MICROPY_PY_ALL_SPECIAL_METHODS |
195 | // Support for __dir__: see if we can dispatch to this special method |
196 | // This relies on MP_QSTR__dir__ being first after MP_QSTR_ |
197 | if (i == MP_QSTR___dir__ && dest[1] != MP_OBJ_NULL) { |
198 | return mp_call_method_n_kw(0, 0, dest); |
199 | } |
200 | #endif |
201 | mp_obj_list_append(dir, MP_OBJ_NEW_QSTR(i)); |
202 | } |
203 | } |
204 | } |
205 | return dir; |
206 | } |
207 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir); |
208 | |
209 | STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { |
210 | return mp_binary_op(MP_BINARY_OP_DIVMOD, o1_in, o2_in); |
211 | } |
212 | MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod); |
213 | |
214 | STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) { |
215 | // result is guaranteed to be a (small) int |
216 | return mp_unary_op(MP_UNARY_OP_HASH, o_in); |
217 | } |
218 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash); |
219 | |
220 | STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) { |
221 | #if MICROPY_PY_BUILTINS_STR_OP_MODULO |
222 | return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_x), o_in); |
223 | #else |
224 | mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_x_brace_close_), o_in }; |
225 | return mp_obj_str_format(MP_ARRAY_SIZE(args), args, NULL); |
226 | #endif |
227 | } |
228 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex); |
229 | |
230 | #if MICROPY_PY_BUILTINS_INPUT |
231 | |
232 | #include "py/mphal.h" |
233 | #include "lib/mp-readline/readline.h" |
234 | |
235 | // A port can define mp_hal_readline if they want to use a custom function here |
236 | #ifndef mp_hal_readline |
237 | #define mp_hal_readline readline |
238 | #endif |
239 | |
240 | STATIC mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) { |
241 | if (n_args == 1) { |
242 | mp_obj_print(args[0], PRINT_STR); |
243 | } |
244 | vstr_t line; |
245 | vstr_init(&line, 16); |
246 | int ret = mp_hal_readline(&line, "" ); |
247 | if (ret == CHAR_CTRL_C) { |
248 | mp_raise_type(&mp_type_KeyboardInterrupt); |
249 | } |
250 | if (line.len == 0 && ret == CHAR_CTRL_D) { |
251 | mp_raise_type(&mp_type_EOFError); |
252 | } |
253 | return mp_obj_new_str_from_vstr(&mp_type_str, &line); |
254 | } |
255 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input); |
256 | |
257 | #endif |
258 | |
259 | STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) { |
260 | return mp_getiter(o_in, NULL); |
261 | } |
262 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter); |
263 | |
264 | #if MICROPY_PY_BUILTINS_MIN_MAX |
265 | |
266 | STATIC mp_obj_t mp_builtin_min_max(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs, mp_uint_t op) { |
267 | mp_map_elem_t *key_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP); |
268 | mp_map_elem_t *default_elem; |
269 | mp_obj_t key_fn = key_elem == NULL ? MP_OBJ_NULL : key_elem->value; |
270 | if (n_args == 1) { |
271 | // given an iterable |
272 | mp_obj_iter_buf_t iter_buf; |
273 | mp_obj_t iterable = mp_getiter(args[0], &iter_buf); |
274 | mp_obj_t best_key = MP_OBJ_NULL; |
275 | mp_obj_t best_obj = MP_OBJ_NULL; |
276 | mp_obj_t item; |
277 | while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { |
278 | mp_obj_t key = key_fn == MP_OBJ_NULL ? item : mp_call_function_1(key_fn, item); |
279 | if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) { |
280 | best_key = key; |
281 | best_obj = item; |
282 | } |
283 | } |
284 | if (best_obj == MP_OBJ_NULL) { |
285 | default_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_default), MP_MAP_LOOKUP); |
286 | if (default_elem != NULL) { |
287 | best_obj = default_elem->value; |
288 | } else { |
289 | mp_raise_ValueError(MP_ERROR_TEXT("arg is an empty sequence" )); |
290 | } |
291 | } |
292 | return best_obj; |
293 | } else { |
294 | // given many args |
295 | mp_obj_t best_key = MP_OBJ_NULL; |
296 | mp_obj_t best_obj = MP_OBJ_NULL; |
297 | for (size_t i = 0; i < n_args; i++) { |
298 | mp_obj_t key = key_fn == MP_OBJ_NULL ? args[i] : mp_call_function_1(key_fn, args[i]); |
299 | if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) { |
300 | best_key = key; |
301 | best_obj = args[i]; |
302 | } |
303 | } |
304 | return best_obj; |
305 | } |
306 | } |
307 | |
308 | STATIC mp_obj_t mp_builtin_max(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
309 | return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_MORE); |
310 | } |
311 | MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_max_obj, 1, mp_builtin_max); |
312 | |
313 | STATIC mp_obj_t mp_builtin_min(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
314 | return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_LESS); |
315 | } |
316 | MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_min_obj, 1, mp_builtin_min); |
317 | |
318 | #endif |
319 | |
320 | #if MICROPY_PY_BUILTINS_NEXT2 |
321 | STATIC mp_obj_t mp_builtin_next(size_t n_args, const mp_obj_t *args) { |
322 | if (n_args == 1) { |
323 | mp_obj_t ret = mp_iternext_allow_raise(args[0]); |
324 | if (ret == MP_OBJ_STOP_ITERATION) { |
325 | mp_raise_type(&mp_type_StopIteration); |
326 | } else { |
327 | return ret; |
328 | } |
329 | } else { |
330 | mp_obj_t ret = mp_iternext(args[0]); |
331 | return ret == MP_OBJ_STOP_ITERATION ? args[1] : ret; |
332 | } |
333 | } |
334 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_next_obj, 1, 2, mp_builtin_next); |
335 | #else |
336 | STATIC mp_obj_t mp_builtin_next(mp_obj_t o) { |
337 | mp_obj_t ret = mp_iternext_allow_raise(o); |
338 | if (ret == MP_OBJ_STOP_ITERATION) { |
339 | mp_raise_type(&mp_type_StopIteration); |
340 | } else { |
341 | return ret; |
342 | } |
343 | } |
344 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next); |
345 | #endif |
346 | |
347 | STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) { |
348 | #if MICROPY_PY_BUILTINS_STR_OP_MODULO |
349 | return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in); |
350 | #else |
351 | mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_o_brace_close_), o_in }; |
352 | return mp_obj_str_format(MP_ARRAY_SIZE(args), args, NULL); |
353 | #endif |
354 | } |
355 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct); |
356 | |
357 | STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) { |
358 | size_t len; |
359 | const byte *str = (const byte *)mp_obj_str_get_data(o_in, &len); |
360 | #if MICROPY_PY_BUILTINS_STR_UNICODE |
361 | if (mp_obj_is_str(o_in)) { |
362 | len = utf8_charlen(str, len); |
363 | if (len == 1) { |
364 | return mp_obj_new_int(utf8_get_char(str)); |
365 | } |
366 | } else |
367 | #endif |
368 | { |
369 | // a bytes object, or a str without unicode support (don't sign extend the char) |
370 | if (len == 1) { |
371 | return MP_OBJ_NEW_SMALL_INT(str[0]); |
372 | } |
373 | } |
374 | |
375 | #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
376 | mp_raise_TypeError(MP_ERROR_TEXT("ord expects a character" )); |
377 | #else |
378 | mp_raise_msg_varg(&mp_type_TypeError, |
379 | MP_ERROR_TEXT("ord() expected a character, but string of length %d found" ), (int)len); |
380 | #endif |
381 | } |
382 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord); |
383 | |
384 | STATIC mp_obj_t mp_builtin_pow(size_t n_args, const mp_obj_t *args) { |
385 | switch (n_args) { |
386 | case 2: |
387 | return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]); |
388 | default: |
389 | #if !MICROPY_PY_BUILTINS_POW3 |
390 | mp_raise_NotImplementedError(MP_ERROR_TEXT("3-arg pow() not supported" )); |
391 | #elif MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_MPZ |
392 | return mp_binary_op(MP_BINARY_OP_MODULO, mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]), args[2]); |
393 | #else |
394 | return mp_obj_int_pow3(args[0], args[1], args[2]); |
395 | #endif |
396 | } |
397 | } |
398 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow); |
399 | |
400 | STATIC mp_obj_t mp_builtin_print(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
401 | enum { ARG_sep, ARG_end, ARG_file }; |
402 | static const mp_arg_t allowed_args[] = { |
403 | { MP_QSTR_sep, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR__space_)} }, |
404 | { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR__0x0a_)} }, |
405 | #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES |
406 | { MP_QSTR_file, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_sys_stdout_obj)} }, |
407 | #endif |
408 | }; |
409 | |
410 | // parse args (a union is used to reduce the amount of C stack that is needed) |
411 | union { |
412 | mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
413 | size_t len[2]; |
414 | } u; |
415 | mp_arg_parse_all(0, NULL, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, u.args); |
416 | |
417 | #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES |
418 | mp_get_stream_raise(u.args[ARG_file].u_obj, MP_STREAM_OP_WRITE); |
419 | mp_print_t print = {MP_OBJ_TO_PTR(u.args[ARG_file].u_obj), mp_stream_write_adaptor}; |
420 | #endif |
421 | |
422 | // extract the objects first because we are going to use the other part of the union |
423 | mp_obj_t sep = u.args[ARG_sep].u_obj; |
424 | mp_obj_t end = u.args[ARG_end].u_obj; |
425 | const char *sep_data = mp_obj_str_get_data(sep, &u.len[0]); |
426 | const char *end_data = mp_obj_str_get_data(end, &u.len[1]); |
427 | |
428 | for (size_t i = 0; i < n_args; i++) { |
429 | if (i > 0) { |
430 | #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES |
431 | mp_stream_write_adaptor(print.data, sep_data, u.len[0]); |
432 | #else |
433 | mp_print_strn(&mp_plat_print, sep_data, u.len[0], 0, 0, 0); |
434 | #endif |
435 | } |
436 | #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES |
437 | mp_obj_print_helper(&print, pos_args[i], PRINT_STR); |
438 | #else |
439 | mp_obj_print_helper(&mp_plat_print, pos_args[i], PRINT_STR); |
440 | #endif |
441 | } |
442 | #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES |
443 | mp_stream_write_adaptor(print.data, end_data, u.len[1]); |
444 | #else |
445 | mp_print_strn(&mp_plat_print, end_data, u.len[1], 0, 0, 0); |
446 | #endif |
447 | return mp_const_none; |
448 | } |
449 | MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print); |
450 | |
451 | STATIC mp_obj_t mp_builtin___repl_print__(mp_obj_t o) { |
452 | if (o != mp_const_none) { |
453 | mp_obj_print_helper(MP_PYTHON_PRINTER, o, PRINT_REPR); |
454 | mp_print_str(MP_PYTHON_PRINTER, "\n" ); |
455 | #if MICROPY_CAN_OVERRIDE_BUILTINS |
456 | // Set "_" special variable |
457 | mp_obj_t dest[2] = {MP_OBJ_SENTINEL, o}; |
458 | mp_type_module.attr(MP_OBJ_FROM_PTR(&mp_module_builtins), MP_QSTR__, dest); |
459 | #endif |
460 | } |
461 | return mp_const_none; |
462 | } |
463 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__); |
464 | |
465 | STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) { |
466 | vstr_t vstr; |
467 | mp_print_t print; |
468 | vstr_init_print(&vstr, 16, &print); |
469 | mp_obj_print_helper(&print, o_in, PRINT_REPR); |
470 | return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); |
471 | } |
472 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr); |
473 | |
474 | STATIC mp_obj_t mp_builtin_round(size_t n_args, const mp_obj_t *args) { |
475 | mp_obj_t o_in = args[0]; |
476 | if (mp_obj_is_int(o_in)) { |
477 | if (n_args <= 1) { |
478 | return o_in; |
479 | } |
480 | |
481 | #if !MICROPY_PY_BUILTINS_ROUND_INT |
482 | mp_raise_NotImplementedError(NULL); |
483 | #else |
484 | mp_int_t num_dig = mp_obj_get_int(args[1]); |
485 | if (num_dig >= 0) { |
486 | return o_in; |
487 | } |
488 | |
489 | mp_obj_t mult = mp_binary_op(MP_BINARY_OP_POWER, MP_OBJ_NEW_SMALL_INT(10), MP_OBJ_NEW_SMALL_INT(-num_dig)); |
490 | mp_obj_t half_mult = mp_binary_op(MP_BINARY_OP_FLOOR_DIVIDE, mult, MP_OBJ_NEW_SMALL_INT(2)); |
491 | mp_obj_t modulo = mp_binary_op(MP_BINARY_OP_MODULO, o_in, mult); |
492 | mp_obj_t rounded = mp_binary_op(MP_BINARY_OP_SUBTRACT, o_in, modulo); |
493 | if (mp_obj_is_true(mp_binary_op(MP_BINARY_OP_MORE, half_mult, modulo))) { |
494 | return rounded; |
495 | } else if (mp_obj_is_true(mp_binary_op(MP_BINARY_OP_MORE, modulo, half_mult))) { |
496 | return mp_binary_op(MP_BINARY_OP_ADD, rounded, mult); |
497 | } else { |
498 | // round to even number |
499 | mp_obj_t floor = mp_binary_op(MP_BINARY_OP_FLOOR_DIVIDE, o_in, mult); |
500 | if (mp_obj_is_true(mp_binary_op(MP_BINARY_OP_AND, floor, MP_OBJ_NEW_SMALL_INT(1)))) { |
501 | return mp_binary_op(MP_BINARY_OP_ADD, rounded, mult); |
502 | } else { |
503 | return rounded; |
504 | } |
505 | } |
506 | #endif |
507 | } |
508 | #if MICROPY_PY_BUILTINS_FLOAT |
509 | mp_float_t val = mp_obj_get_float(o_in); |
510 | if (n_args > 1) { |
511 | mp_int_t num_dig = mp_obj_get_int(args[1]); |
512 | mp_float_t mult = MICROPY_FLOAT_C_FUN(pow)(10, (mp_float_t)num_dig); |
513 | // TODO may lead to overflow |
514 | mp_float_t rounded = MICROPY_FLOAT_C_FUN(nearbyint)(val * mult) / mult; |
515 | return mp_obj_new_float(rounded); |
516 | } |
517 | mp_float_t rounded = MICROPY_FLOAT_C_FUN(nearbyint)(val); |
518 | return mp_obj_new_int_from_float(rounded); |
519 | #else |
520 | mp_int_t r = mp_obj_get_int(o_in); |
521 | return mp_obj_new_int(r); |
522 | #endif |
523 | } |
524 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_round_obj, 1, 2, mp_builtin_round); |
525 | |
526 | STATIC mp_obj_t mp_builtin_sum(size_t n_args, const mp_obj_t *args) { |
527 | mp_obj_t value; |
528 | switch (n_args) { |
529 | case 1: |
530 | value = MP_OBJ_NEW_SMALL_INT(0); |
531 | break; |
532 | default: |
533 | value = args[1]; |
534 | break; |
535 | } |
536 | mp_obj_iter_buf_t iter_buf; |
537 | mp_obj_t iterable = mp_getiter(args[0], &iter_buf); |
538 | mp_obj_t item; |
539 | while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { |
540 | value = mp_binary_op(MP_BINARY_OP_ADD, value, item); |
541 | } |
542 | return value; |
543 | } |
544 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum); |
545 | |
546 | STATIC mp_obj_t mp_builtin_sorted(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
547 | if (n_args > 1) { |
548 | mp_raise_TypeError(MP_ERROR_TEXT("must use keyword argument for key function" )); |
549 | } |
550 | mp_obj_t self = mp_type_list.make_new(&mp_type_list, 1, 0, args); |
551 | mp_obj_list_sort(1, &self, kwargs); |
552 | |
553 | return self; |
554 | } |
555 | MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted); |
556 | |
557 | // See mp_load_attr() if making any changes |
558 | static inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) { |
559 | mp_obj_t dest[2]; |
560 | // use load_method, raising or not raising exception |
561 | if (defval == MP_OBJ_NULL) { |
562 | mp_load_method(base, attr, dest); |
563 | } else { |
564 | mp_load_method_protected(base, attr, dest, false); |
565 | } |
566 | if (dest[0] == MP_OBJ_NULL) { |
567 | return defval; |
568 | } else if (dest[1] == MP_OBJ_NULL) { |
569 | // load_method returned just a normal attribute |
570 | return dest[0]; |
571 | } else { |
572 | // load_method returned a method, so build a bound method object |
573 | return mp_obj_new_bound_meth(dest[0], dest[1]); |
574 | } |
575 | } |
576 | |
577 | STATIC mp_obj_t mp_builtin_getattr(size_t n_args, const mp_obj_t *args) { |
578 | mp_obj_t defval = MP_OBJ_NULL; |
579 | if (n_args > 2) { |
580 | defval = args[2]; |
581 | } |
582 | return mp_load_attr_default(args[0], mp_obj_str_get_qstr(args[1]), defval); |
583 | } |
584 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr); |
585 | |
586 | STATIC mp_obj_t mp_builtin_setattr(mp_obj_t base, mp_obj_t attr, mp_obj_t value) { |
587 | mp_store_attr(base, mp_obj_str_get_qstr(attr), value); |
588 | return mp_const_none; |
589 | } |
590 | MP_DEFINE_CONST_FUN_OBJ_3(mp_builtin_setattr_obj, mp_builtin_setattr); |
591 | |
592 | #if MICROPY_CPYTHON_COMPAT |
593 | STATIC mp_obj_t mp_builtin_delattr(mp_obj_t base, mp_obj_t attr) { |
594 | return mp_builtin_setattr(base, attr, MP_OBJ_NULL); |
595 | } |
596 | MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_delattr_obj, mp_builtin_delattr); |
597 | #endif |
598 | |
599 | STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) { |
600 | qstr attr = mp_obj_str_get_qstr(attr_in); |
601 | mp_obj_t dest[2]; |
602 | mp_load_method_protected(object_in, attr, dest, false); |
603 | return mp_obj_new_bool(dest[0] != MP_OBJ_NULL); |
604 | } |
605 | MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr); |
606 | |
607 | STATIC mp_obj_t mp_builtin_globals(void) { |
608 | return MP_OBJ_FROM_PTR(mp_globals_get()); |
609 | } |
610 | MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_builtin_globals); |
611 | |
612 | STATIC mp_obj_t mp_builtin_locals(void) { |
613 | return MP_OBJ_FROM_PTR(mp_locals_get()); |
614 | } |
615 | MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_builtin_locals); |
616 | |
617 | // These are defined in terms of MicroPython API functions right away |
618 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_obj_id); |
619 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_obj_len); |
620 | |
621 | STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = { |
622 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_builtins) }, |
623 | |
624 | // built-in core functions |
625 | { MP_ROM_QSTR(MP_QSTR___build_class__), MP_ROM_PTR(&mp_builtin___build_class___obj) }, |
626 | { MP_ROM_QSTR(MP_QSTR___import__), MP_ROM_PTR(&mp_builtin___import___obj) }, |
627 | { MP_ROM_QSTR(MP_QSTR___repl_print__), MP_ROM_PTR(&mp_builtin___repl_print___obj) }, |
628 | |
629 | // built-in types |
630 | { MP_ROM_QSTR(MP_QSTR_bool), MP_ROM_PTR(&mp_type_bool) }, |
631 | { MP_ROM_QSTR(MP_QSTR_bytes), MP_ROM_PTR(&mp_type_bytes) }, |
632 | #if MICROPY_PY_BUILTINS_BYTEARRAY |
633 | { MP_ROM_QSTR(MP_QSTR_bytearray), MP_ROM_PTR(&mp_type_bytearray) }, |
634 | #endif |
635 | #if MICROPY_PY_BUILTINS_COMPLEX |
636 | { MP_ROM_QSTR(MP_QSTR_complex), MP_ROM_PTR(&mp_type_complex) }, |
637 | #endif |
638 | { MP_ROM_QSTR(MP_QSTR_dict), MP_ROM_PTR(&mp_type_dict) }, |
639 | #if MICROPY_PY_BUILTINS_ENUMERATE |
640 | { MP_ROM_QSTR(MP_QSTR_enumerate), MP_ROM_PTR(&mp_type_enumerate) }, |
641 | #endif |
642 | #if MICROPY_PY_BUILTINS_FILTER |
643 | { MP_ROM_QSTR(MP_QSTR_filter), MP_ROM_PTR(&mp_type_filter) }, |
644 | #endif |
645 | #if MICROPY_PY_BUILTINS_FLOAT |
646 | { MP_ROM_QSTR(MP_QSTR_float), MP_ROM_PTR(&mp_type_float) }, |
647 | #endif |
648 | #if MICROPY_PY_BUILTINS_SET && MICROPY_PY_BUILTINS_FROZENSET |
649 | { MP_ROM_QSTR(MP_QSTR_frozenset), MP_ROM_PTR(&mp_type_frozenset) }, |
650 | #endif |
651 | { MP_ROM_QSTR(MP_QSTR_int), MP_ROM_PTR(&mp_type_int) }, |
652 | { MP_ROM_QSTR(MP_QSTR_list), MP_ROM_PTR(&mp_type_list) }, |
653 | { MP_ROM_QSTR(MP_QSTR_map), MP_ROM_PTR(&mp_type_map) }, |
654 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
655 | { MP_ROM_QSTR(MP_QSTR_memoryview), MP_ROM_PTR(&mp_type_memoryview) }, |
656 | #endif |
657 | { MP_ROM_QSTR(MP_QSTR_object), MP_ROM_PTR(&mp_type_object) }, |
658 | #if MICROPY_PY_BUILTINS_PROPERTY |
659 | { MP_ROM_QSTR(MP_QSTR_property), MP_ROM_PTR(&mp_type_property) }, |
660 | #endif |
661 | { MP_ROM_QSTR(MP_QSTR_range), MP_ROM_PTR(&mp_type_range) }, |
662 | #if MICROPY_PY_BUILTINS_REVERSED |
663 | { MP_ROM_QSTR(MP_QSTR_reversed), MP_ROM_PTR(&mp_type_reversed) }, |
664 | #endif |
665 | #if MICROPY_PY_BUILTINS_SET |
666 | { MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&mp_type_set) }, |
667 | #endif |
668 | #if MICROPY_PY_BUILTINS_SLICE |
669 | { MP_ROM_QSTR(MP_QSTR_slice), MP_ROM_PTR(&mp_type_slice) }, |
670 | #endif |
671 | { MP_ROM_QSTR(MP_QSTR_str), MP_ROM_PTR(&mp_type_str) }, |
672 | { MP_ROM_QSTR(MP_QSTR_super), MP_ROM_PTR(&mp_type_super) }, |
673 | { MP_ROM_QSTR(MP_QSTR_tuple), MP_ROM_PTR(&mp_type_tuple) }, |
674 | { MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&mp_type_type) }, |
675 | { MP_ROM_QSTR(MP_QSTR_zip), MP_ROM_PTR(&mp_type_zip) }, |
676 | |
677 | { MP_ROM_QSTR(MP_QSTR_classmethod), MP_ROM_PTR(&mp_type_classmethod) }, |
678 | { MP_ROM_QSTR(MP_QSTR_staticmethod), MP_ROM_PTR(&mp_type_staticmethod) }, |
679 | |
680 | // built-in objects |
681 | { MP_ROM_QSTR(MP_QSTR_Ellipsis), MP_ROM_PTR(&mp_const_ellipsis_obj) }, |
682 | #if MICROPY_PY_BUILTINS_NOTIMPLEMENTED |
683 | { MP_ROM_QSTR(MP_QSTR_NotImplemented), MP_ROM_PTR(&mp_const_notimplemented_obj) }, |
684 | #endif |
685 | |
686 | // built-in user functions |
687 | { MP_ROM_QSTR(MP_QSTR_abs), MP_ROM_PTR(&mp_builtin_abs_obj) }, |
688 | { MP_ROM_QSTR(MP_QSTR_all), MP_ROM_PTR(&mp_builtin_all_obj) }, |
689 | { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&mp_builtin_any_obj) }, |
690 | { MP_ROM_QSTR(MP_QSTR_bin), MP_ROM_PTR(&mp_builtin_bin_obj) }, |
691 | { MP_ROM_QSTR(MP_QSTR_callable), MP_ROM_PTR(&mp_builtin_callable_obj) }, |
692 | #if MICROPY_PY_BUILTINS_COMPILE |
693 | { MP_ROM_QSTR(MP_QSTR_compile), MP_ROM_PTR(&mp_builtin_compile_obj) }, |
694 | #endif |
695 | { MP_ROM_QSTR(MP_QSTR_chr), MP_ROM_PTR(&mp_builtin_chr_obj) }, |
696 | #if MICROPY_CPYTHON_COMPAT |
697 | { MP_ROM_QSTR(MP_QSTR_delattr), MP_ROM_PTR(&mp_builtin_delattr_obj) }, |
698 | #endif |
699 | { MP_ROM_QSTR(MP_QSTR_dir), MP_ROM_PTR(&mp_builtin_dir_obj) }, |
700 | { MP_ROM_QSTR(MP_QSTR_divmod), MP_ROM_PTR(&mp_builtin_divmod_obj) }, |
701 | #if MICROPY_PY_BUILTINS_EVAL_EXEC |
702 | { MP_ROM_QSTR(MP_QSTR_eval), MP_ROM_PTR(&mp_builtin_eval_obj) }, |
703 | { MP_ROM_QSTR(MP_QSTR_exec), MP_ROM_PTR(&mp_builtin_exec_obj) }, |
704 | #endif |
705 | #if MICROPY_PY_BUILTINS_EXECFILE |
706 | { MP_ROM_QSTR(MP_QSTR_execfile), MP_ROM_PTR(&mp_builtin_execfile_obj) }, |
707 | #endif |
708 | { MP_ROM_QSTR(MP_QSTR_getattr), MP_ROM_PTR(&mp_builtin_getattr_obj) }, |
709 | { MP_ROM_QSTR(MP_QSTR_setattr), MP_ROM_PTR(&mp_builtin_setattr_obj) }, |
710 | { MP_ROM_QSTR(MP_QSTR_globals), MP_ROM_PTR(&mp_builtin_globals_obj) }, |
711 | { MP_ROM_QSTR(MP_QSTR_hasattr), MP_ROM_PTR(&mp_builtin_hasattr_obj) }, |
712 | { MP_ROM_QSTR(MP_QSTR_hash), MP_ROM_PTR(&mp_builtin_hash_obj) }, |
713 | #if MICROPY_PY_BUILTINS_HELP |
714 | { MP_ROM_QSTR(MP_QSTR_help), MP_ROM_PTR(&mp_builtin_help_obj) }, |
715 | #endif |
716 | { MP_ROM_QSTR(MP_QSTR_hex), MP_ROM_PTR(&mp_builtin_hex_obj) }, |
717 | { MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&mp_builtin_id_obj) }, |
718 | #if MICROPY_PY_BUILTINS_INPUT |
719 | { MP_ROM_QSTR(MP_QSTR_input), MP_ROM_PTR(&mp_builtin_input_obj) }, |
720 | #endif |
721 | { MP_ROM_QSTR(MP_QSTR_isinstance), MP_ROM_PTR(&mp_builtin_isinstance_obj) }, |
722 | { MP_ROM_QSTR(MP_QSTR_issubclass), MP_ROM_PTR(&mp_builtin_issubclass_obj) }, |
723 | { MP_ROM_QSTR(MP_QSTR_iter), MP_ROM_PTR(&mp_builtin_iter_obj) }, |
724 | { MP_ROM_QSTR(MP_QSTR_len), MP_ROM_PTR(&mp_builtin_len_obj) }, |
725 | { MP_ROM_QSTR(MP_QSTR_locals), MP_ROM_PTR(&mp_builtin_locals_obj) }, |
726 | #if MICROPY_PY_BUILTINS_MIN_MAX |
727 | { MP_ROM_QSTR(MP_QSTR_max), MP_ROM_PTR(&mp_builtin_max_obj) }, |
728 | { MP_ROM_QSTR(MP_QSTR_min), MP_ROM_PTR(&mp_builtin_min_obj) }, |
729 | #endif |
730 | { MP_ROM_QSTR(MP_QSTR_next), MP_ROM_PTR(&mp_builtin_next_obj) }, |
731 | { MP_ROM_QSTR(MP_QSTR_oct), MP_ROM_PTR(&mp_builtin_oct_obj) }, |
732 | { MP_ROM_QSTR(MP_QSTR_ord), MP_ROM_PTR(&mp_builtin_ord_obj) }, |
733 | { MP_ROM_QSTR(MP_QSTR_pow), MP_ROM_PTR(&mp_builtin_pow_obj) }, |
734 | { MP_ROM_QSTR(MP_QSTR_print), MP_ROM_PTR(&mp_builtin_print_obj) }, |
735 | { MP_ROM_QSTR(MP_QSTR_repr), MP_ROM_PTR(&mp_builtin_repr_obj) }, |
736 | { MP_ROM_QSTR(MP_QSTR_round), MP_ROM_PTR(&mp_builtin_round_obj) }, |
737 | { MP_ROM_QSTR(MP_QSTR_sorted), MP_ROM_PTR(&mp_builtin_sorted_obj) }, |
738 | { MP_ROM_QSTR(MP_QSTR_sum), MP_ROM_PTR(&mp_builtin_sum_obj) }, |
739 | |
740 | // built-in exceptions |
741 | { MP_ROM_QSTR(MP_QSTR_BaseException), MP_ROM_PTR(&mp_type_BaseException) }, |
742 | { MP_ROM_QSTR(MP_QSTR_ArithmeticError), MP_ROM_PTR(&mp_type_ArithmeticError) }, |
743 | { MP_ROM_QSTR(MP_QSTR_AssertionError), MP_ROM_PTR(&mp_type_AssertionError) }, |
744 | { MP_ROM_QSTR(MP_QSTR_AttributeError), MP_ROM_PTR(&mp_type_AttributeError) }, |
745 | { MP_ROM_QSTR(MP_QSTR_EOFError), MP_ROM_PTR(&mp_type_EOFError) }, |
746 | { MP_ROM_QSTR(MP_QSTR_Exception), MP_ROM_PTR(&mp_type_Exception) }, |
747 | { MP_ROM_QSTR(MP_QSTR_GeneratorExit), MP_ROM_PTR(&mp_type_GeneratorExit) }, |
748 | { MP_ROM_QSTR(MP_QSTR_ImportError), MP_ROM_PTR(&mp_type_ImportError) }, |
749 | { MP_ROM_QSTR(MP_QSTR_IndentationError), MP_ROM_PTR(&mp_type_IndentationError) }, |
750 | { MP_ROM_QSTR(MP_QSTR_IndexError), MP_ROM_PTR(&mp_type_IndexError) }, |
751 | { MP_ROM_QSTR(MP_QSTR_KeyboardInterrupt), MP_ROM_PTR(&mp_type_KeyboardInterrupt) }, |
752 | { MP_ROM_QSTR(MP_QSTR_KeyError), MP_ROM_PTR(&mp_type_KeyError) }, |
753 | { MP_ROM_QSTR(MP_QSTR_LookupError), MP_ROM_PTR(&mp_type_LookupError) }, |
754 | { MP_ROM_QSTR(MP_QSTR_MemoryError), MP_ROM_PTR(&mp_type_MemoryError) }, |
755 | { MP_ROM_QSTR(MP_QSTR_NameError), MP_ROM_PTR(&mp_type_NameError) }, |
756 | { MP_ROM_QSTR(MP_QSTR_NotImplementedError), MP_ROM_PTR(&mp_type_NotImplementedError) }, |
757 | { MP_ROM_QSTR(MP_QSTR_OSError), MP_ROM_PTR(&mp_type_OSError) }, |
758 | { MP_ROM_QSTR(MP_QSTR_OverflowError), MP_ROM_PTR(&mp_type_OverflowError) }, |
759 | { MP_ROM_QSTR(MP_QSTR_RuntimeError), MP_ROM_PTR(&mp_type_RuntimeError) }, |
760 | #if MICROPY_PY_ASYNC_AWAIT |
761 | { MP_ROM_QSTR(MP_QSTR_StopAsyncIteration), MP_ROM_PTR(&mp_type_StopAsyncIteration) }, |
762 | #endif |
763 | { MP_ROM_QSTR(MP_QSTR_StopIteration), MP_ROM_PTR(&mp_type_StopIteration) }, |
764 | { MP_ROM_QSTR(MP_QSTR_SyntaxError), MP_ROM_PTR(&mp_type_SyntaxError) }, |
765 | { MP_ROM_QSTR(MP_QSTR_SystemExit), MP_ROM_PTR(&mp_type_SystemExit) }, |
766 | { MP_ROM_QSTR(MP_QSTR_TypeError), MP_ROM_PTR(&mp_type_TypeError) }, |
767 | #if MICROPY_PY_BUILTINS_STR_UNICODE |
768 | { MP_ROM_QSTR(MP_QSTR_UnicodeError), MP_ROM_PTR(&mp_type_UnicodeError) }, |
769 | #endif |
770 | { MP_ROM_QSTR(MP_QSTR_ValueError), MP_ROM_PTR(&mp_type_ValueError) }, |
771 | #if MICROPY_EMIT_NATIVE |
772 | { MP_ROM_QSTR(MP_QSTR_ViperTypeError), MP_ROM_PTR(&mp_type_ViperTypeError) }, |
773 | #endif |
774 | { MP_ROM_QSTR(MP_QSTR_ZeroDivisionError), MP_ROM_PTR(&mp_type_ZeroDivisionError) }, |
775 | |
776 | // Extra builtins as defined by a port |
777 | MICROPY_PORT_BUILTINS |
778 | }; |
779 | |
780 | MP_DEFINE_CONST_DICT(mp_module_builtins_globals, mp_module_builtins_globals_table); |
781 | |
782 | const mp_obj_module_t mp_module_builtins = { |
783 | .base = { &mp_type_module }, |
784 | .globals = (mp_obj_dict_t *)&mp_module_builtins_globals, |
785 | }; |
786 | |