Port of MicroPython to the mbed platform. See micropython-repl for an interactive program.
This a port of MicroPython to the mbed Classic platform.
This provides an interpreter running on the board's USB serial connection.
Getting Started
Import the micropython-repl program into your IDE workspace on developer.mbed.org. Compile and download to your board. Connect to the USB serial port in your usual manner. You should get a startup message similar to the following:
MicroPython v1.7-155-gdddcdd8 on 2016-04-23; K64F with ARM Type "help()" for more information. >>>
Then you can start using micropython. For example:
>>> from mbed import DigitalOut >>> from pins import LED1 >>> led = DigitalOut(LED1) >>> led.write(1)
Requirements
You need approximately 100K of flash memory, so this will be no good for boards with smaller amounts of storage.
Caveats
This can be considered an alpha release of the port; things may not work; APIs may change in later releases. It is NOT an official part part the micropython project, so if anything doesn't work, blame me. If it does work, most of the credit is due to micropython.
- Only a few of the mbed classes are available in micropython so far, and not all methods of those that are.
- Only a few boards have their full range of pin names available; for others, only a few standard ones (USBTX, USBRX, LED1) are implemented.
- The garbage collector is not yet implemented. The interpreter will gradually consume memory and then fail.
- Exceptions from the mbed classes are not yet handled.
- Asynchronous processing (e.g. events on inputs) is not supported.
Credits
- Damien P. George and other contributors who created micropython.
- Colin Hogben, author of this port.
py/modbuiltins.c@2:c89e95946844, 2016-04-16 (annotated)
- Committer:
- Colin Hogben
- Date:
- Sat Apr 16 22:43:41 2016 +0100
- Revision:
- 2:c89e95946844
- Parent:
- 0:5868e8752d44
py: update to upstream master
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pythontech | 0:5868e8752d44 | 1 | /* |
pythontech | 0:5868e8752d44 | 2 | * This file is part of the Micro Python project, http://micropython.org/ |
pythontech | 0:5868e8752d44 | 3 | * |
pythontech | 0:5868e8752d44 | 4 | * The MIT License (MIT) |
pythontech | 0:5868e8752d44 | 5 | * |
pythontech | 0:5868e8752d44 | 6 | * Copyright (c) 2013, 2014 Damien P. George |
pythontech | 0:5868e8752d44 | 7 | * |
pythontech | 0:5868e8752d44 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
pythontech | 0:5868e8752d44 | 9 | * of this software and associated documentation files (the "Software"), to deal |
pythontech | 0:5868e8752d44 | 10 | * in the Software without restriction, including without limitation the rights |
pythontech | 0:5868e8752d44 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
pythontech | 0:5868e8752d44 | 12 | * copies of the Software, and to permit persons to whom the Software is |
pythontech | 0:5868e8752d44 | 13 | * furnished to do so, subject to the following conditions: |
pythontech | 0:5868e8752d44 | 14 | * |
pythontech | 0:5868e8752d44 | 15 | * The above copyright notice and this permission notice shall be included in |
pythontech | 0:5868e8752d44 | 16 | * all copies or substantial portions of the Software. |
pythontech | 0:5868e8752d44 | 17 | * |
pythontech | 0:5868e8752d44 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
pythontech | 0:5868e8752d44 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
pythontech | 0:5868e8752d44 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
pythontech | 0:5868e8752d44 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
pythontech | 0:5868e8752d44 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
pythontech | 0:5868e8752d44 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
pythontech | 0:5868e8752d44 | 24 | * THE SOFTWARE. |
pythontech | 0:5868e8752d44 | 25 | */ |
pythontech | 0:5868e8752d44 | 26 | |
pythontech | 0:5868e8752d44 | 27 | #include <stdio.h> |
pythontech | 0:5868e8752d44 | 28 | #include <assert.h> |
pythontech | 0:5868e8752d44 | 29 | |
pythontech | 0:5868e8752d44 | 30 | #include "py/nlr.h" |
pythontech | 0:5868e8752d44 | 31 | #include "py/smallint.h" |
pythontech | 0:5868e8752d44 | 32 | #include "py/objint.h" |
pythontech | 0:5868e8752d44 | 33 | #include "py/objstr.h" |
pythontech | 0:5868e8752d44 | 34 | #include "py/objtype.h" |
pythontech | 0:5868e8752d44 | 35 | #include "py/runtime0.h" |
pythontech | 0:5868e8752d44 | 36 | #include "py/runtime.h" |
pythontech | 0:5868e8752d44 | 37 | #include "py/builtin.h" |
pythontech | 0:5868e8752d44 | 38 | #include "py/stream.h" |
pythontech | 0:5868e8752d44 | 39 | |
pythontech | 0:5868e8752d44 | 40 | #if MICROPY_PY_BUILTINS_FLOAT |
pythontech | 0:5868e8752d44 | 41 | #include <math.h> |
pythontech | 0:5868e8752d44 | 42 | #endif |
pythontech | 0:5868e8752d44 | 43 | |
pythontech | 0:5868e8752d44 | 44 | #if MICROPY_PY_IO |
pythontech | 0:5868e8752d44 | 45 | extern struct _mp_dummy_t mp_sys_stdout_obj; // type is irrelevant, just need pointer |
pythontech | 0:5868e8752d44 | 46 | #endif |
pythontech | 0:5868e8752d44 | 47 | |
pythontech | 0:5868e8752d44 | 48 | // args[0] is function from class body |
pythontech | 0:5868e8752d44 | 49 | // args[1] is class name |
pythontech | 0:5868e8752d44 | 50 | // args[2:] are base objects |
pythontech | 0:5868e8752d44 | 51 | STATIC mp_obj_t mp_builtin___build_class__(size_t n_args, const mp_obj_t *args) { |
pythontech | 0:5868e8752d44 | 52 | assert(2 <= n_args); |
pythontech | 0:5868e8752d44 | 53 | |
pythontech | 0:5868e8752d44 | 54 | // set the new classes __locals__ object |
pythontech | 0:5868e8752d44 | 55 | mp_obj_dict_t *old_locals = mp_locals_get(); |
pythontech | 0:5868e8752d44 | 56 | mp_obj_t class_locals = mp_obj_new_dict(0); |
pythontech | 0:5868e8752d44 | 57 | mp_locals_set(MP_OBJ_TO_PTR(class_locals)); |
pythontech | 0:5868e8752d44 | 58 | |
pythontech | 0:5868e8752d44 | 59 | // call the class code |
pythontech | 0:5868e8752d44 | 60 | mp_obj_t cell = mp_call_function_0(args[0]); |
pythontech | 0:5868e8752d44 | 61 | |
pythontech | 0:5868e8752d44 | 62 | // restore old __locals__ object |
pythontech | 0:5868e8752d44 | 63 | mp_locals_set(old_locals); |
pythontech | 0:5868e8752d44 | 64 | |
pythontech | 0:5868e8752d44 | 65 | // get the class type (meta object) from the base objects |
pythontech | 0:5868e8752d44 | 66 | mp_obj_t meta; |
pythontech | 0:5868e8752d44 | 67 | if (n_args == 2) { |
pythontech | 0:5868e8752d44 | 68 | // no explicit bases, so use 'type' |
pythontech | 0:5868e8752d44 | 69 | meta = MP_OBJ_FROM_PTR(&mp_type_type); |
pythontech | 0:5868e8752d44 | 70 | } else { |
pythontech | 0:5868e8752d44 | 71 | // use type of first base object |
pythontech | 0:5868e8752d44 | 72 | meta = MP_OBJ_FROM_PTR(mp_obj_get_type(args[2])); |
pythontech | 0:5868e8752d44 | 73 | } |
pythontech | 0:5868e8752d44 | 74 | |
pythontech | 0:5868e8752d44 | 75 | // TODO do proper metaclass resolution for multiple base objects |
pythontech | 0:5868e8752d44 | 76 | |
pythontech | 0:5868e8752d44 | 77 | // create the new class using a call to the meta object |
pythontech | 0:5868e8752d44 | 78 | mp_obj_t meta_args[3]; |
pythontech | 0:5868e8752d44 | 79 | meta_args[0] = args[1]; // class name |
pythontech | 0:5868e8752d44 | 80 | meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases |
pythontech | 0:5868e8752d44 | 81 | meta_args[2] = class_locals; // dict of members |
pythontech | 0:5868e8752d44 | 82 | mp_obj_t new_class = mp_call_function_n_kw(meta, 3, 0, meta_args); |
pythontech | 0:5868e8752d44 | 83 | |
pythontech | 0:5868e8752d44 | 84 | // store into cell if neede |
pythontech | 0:5868e8752d44 | 85 | if (cell != mp_const_none) { |
pythontech | 0:5868e8752d44 | 86 | mp_obj_cell_set(cell, new_class); |
pythontech | 0:5868e8752d44 | 87 | } |
pythontech | 0:5868e8752d44 | 88 | |
pythontech | 0:5868e8752d44 | 89 | return new_class; |
pythontech | 0:5868e8752d44 | 90 | } |
pythontech | 0:5868e8752d44 | 91 | MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__); |
pythontech | 0:5868e8752d44 | 92 | |
pythontech | 0:5868e8752d44 | 93 | STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 94 | if (0) { |
pythontech | 0:5868e8752d44 | 95 | // dummy |
pythontech | 0:5868e8752d44 | 96 | #if MICROPY_PY_BUILTINS_FLOAT |
pythontech | 0:5868e8752d44 | 97 | } else if (mp_obj_is_float(o_in)) { |
pythontech | 0:5868e8752d44 | 98 | mp_float_t value = mp_obj_float_get(o_in); |
pythontech | 0:5868e8752d44 | 99 | // TODO check for NaN etc |
pythontech | 0:5868e8752d44 | 100 | if (value < 0) { |
pythontech | 0:5868e8752d44 | 101 | return mp_obj_new_float(-value); |
pythontech | 0:5868e8752d44 | 102 | } else { |
pythontech | 0:5868e8752d44 | 103 | return o_in; |
pythontech | 0:5868e8752d44 | 104 | } |
pythontech | 0:5868e8752d44 | 105 | #if MICROPY_PY_BUILTINS_COMPLEX |
pythontech | 0:5868e8752d44 | 106 | } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) { |
pythontech | 0:5868e8752d44 | 107 | mp_float_t real, imag; |
pythontech | 0:5868e8752d44 | 108 | mp_obj_complex_get(o_in, &real, &imag); |
pythontech | 0:5868e8752d44 | 109 | return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)); |
pythontech | 0:5868e8752d44 | 110 | #endif |
pythontech | 0:5868e8752d44 | 111 | #endif |
pythontech | 0:5868e8752d44 | 112 | } else { |
pythontech | 0:5868e8752d44 | 113 | // this will raise a TypeError if the argument is not integral |
pythontech | 0:5868e8752d44 | 114 | return mp_obj_int_abs(o_in); |
pythontech | 0:5868e8752d44 | 115 | } |
pythontech | 0:5868e8752d44 | 116 | } |
pythontech | 0:5868e8752d44 | 117 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs); |
pythontech | 0:5868e8752d44 | 118 | |
pythontech | 0:5868e8752d44 | 119 | STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 120 | mp_obj_t iterable = mp_getiter(o_in); |
pythontech | 0:5868e8752d44 | 121 | mp_obj_t item; |
pythontech | 0:5868e8752d44 | 122 | while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { |
pythontech | 0:5868e8752d44 | 123 | if (!mp_obj_is_true(item)) { |
pythontech | 0:5868e8752d44 | 124 | return mp_const_false; |
pythontech | 0:5868e8752d44 | 125 | } |
pythontech | 0:5868e8752d44 | 126 | } |
pythontech | 0:5868e8752d44 | 127 | return mp_const_true; |
pythontech | 0:5868e8752d44 | 128 | } |
pythontech | 0:5868e8752d44 | 129 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all); |
pythontech | 0:5868e8752d44 | 130 | |
pythontech | 0:5868e8752d44 | 131 | STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 132 | mp_obj_t iterable = mp_getiter(o_in); |
pythontech | 0:5868e8752d44 | 133 | mp_obj_t item; |
pythontech | 0:5868e8752d44 | 134 | while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { |
pythontech | 0:5868e8752d44 | 135 | if (mp_obj_is_true(item)) { |
pythontech | 0:5868e8752d44 | 136 | return mp_const_true; |
pythontech | 0:5868e8752d44 | 137 | } |
pythontech | 0:5868e8752d44 | 138 | } |
pythontech | 0:5868e8752d44 | 139 | return mp_const_false; |
pythontech | 0:5868e8752d44 | 140 | } |
pythontech | 0:5868e8752d44 | 141 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any); |
pythontech | 0:5868e8752d44 | 142 | |
pythontech | 0:5868e8752d44 | 143 | STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 144 | mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_b_brace_close_), o_in }; |
pythontech | 0:5868e8752d44 | 145 | return mp_obj_str_format(MP_ARRAY_SIZE(args), args, NULL); |
pythontech | 0:5868e8752d44 | 146 | } |
pythontech | 0:5868e8752d44 | 147 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin); |
pythontech | 0:5868e8752d44 | 148 | |
pythontech | 0:5868e8752d44 | 149 | STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 150 | if (mp_obj_is_callable(o_in)) { |
pythontech | 0:5868e8752d44 | 151 | return mp_const_true; |
pythontech | 0:5868e8752d44 | 152 | } else { |
pythontech | 0:5868e8752d44 | 153 | return mp_const_false; |
pythontech | 0:5868e8752d44 | 154 | } |
pythontech | 0:5868e8752d44 | 155 | } |
pythontech | 0:5868e8752d44 | 156 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable); |
pythontech | 0:5868e8752d44 | 157 | |
pythontech | 0:5868e8752d44 | 158 | STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 159 | #if MICROPY_PY_BUILTINS_STR_UNICODE |
pythontech | 0:5868e8752d44 | 160 | mp_uint_t c = mp_obj_get_int(o_in); |
pythontech | 0:5868e8752d44 | 161 | char str[4]; |
pythontech | 0:5868e8752d44 | 162 | int len = 0; |
pythontech | 0:5868e8752d44 | 163 | if (c < 0x80) { |
pythontech | 0:5868e8752d44 | 164 | *str = c; len = 1; |
pythontech | 0:5868e8752d44 | 165 | } else if (c < 0x800) { |
pythontech | 0:5868e8752d44 | 166 | str[0] = (c >> 6) | 0xC0; |
pythontech | 0:5868e8752d44 | 167 | str[1] = (c & 0x3F) | 0x80; |
pythontech | 0:5868e8752d44 | 168 | len = 2; |
pythontech | 0:5868e8752d44 | 169 | } else if (c < 0x10000) { |
pythontech | 0:5868e8752d44 | 170 | str[0] = (c >> 12) | 0xE0; |
pythontech | 0:5868e8752d44 | 171 | str[1] = ((c >> 6) & 0x3F) | 0x80; |
pythontech | 0:5868e8752d44 | 172 | str[2] = (c & 0x3F) | 0x80; |
pythontech | 0:5868e8752d44 | 173 | len = 3; |
pythontech | 0:5868e8752d44 | 174 | } else if (c < 0x110000) { |
pythontech | 0:5868e8752d44 | 175 | str[0] = (c >> 18) | 0xF0; |
pythontech | 0:5868e8752d44 | 176 | str[1] = ((c >> 12) & 0x3F) | 0x80; |
pythontech | 0:5868e8752d44 | 177 | str[2] = ((c >> 6) & 0x3F) | 0x80; |
pythontech | 0:5868e8752d44 | 178 | str[3] = (c & 0x3F) | 0x80; |
pythontech | 0:5868e8752d44 | 179 | len = 4; |
pythontech | 0:5868e8752d44 | 180 | } else { |
pythontech | 0:5868e8752d44 | 181 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)")); |
pythontech | 0:5868e8752d44 | 182 | } |
pythontech | 0:5868e8752d44 | 183 | return mp_obj_new_str(str, len, true); |
pythontech | 0:5868e8752d44 | 184 | #else |
pythontech | 0:5868e8752d44 | 185 | mp_int_t ord = mp_obj_get_int(o_in); |
pythontech | 0:5868e8752d44 | 186 | if (0 <= ord && ord <= 0xff) { |
pythontech | 0:5868e8752d44 | 187 | char str[1] = {ord}; |
pythontech | 0:5868e8752d44 | 188 | return mp_obj_new_str(str, 1, true); |
pythontech | 0:5868e8752d44 | 189 | } else { |
pythontech | 0:5868e8752d44 | 190 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(256)")); |
pythontech | 0:5868e8752d44 | 191 | } |
pythontech | 0:5868e8752d44 | 192 | #endif |
pythontech | 0:5868e8752d44 | 193 | } |
pythontech | 0:5868e8752d44 | 194 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr); |
pythontech | 0:5868e8752d44 | 195 | |
pythontech | 0:5868e8752d44 | 196 | STATIC mp_obj_t mp_builtin_dir(size_t n_args, const mp_obj_t *args) { |
pythontech | 0:5868e8752d44 | 197 | // TODO make this function more general and less of a hack |
pythontech | 0:5868e8752d44 | 198 | |
pythontech | 0:5868e8752d44 | 199 | mp_obj_dict_t *dict = NULL; |
pythontech | 0:5868e8752d44 | 200 | mp_map_t *members = NULL; |
pythontech | 0:5868e8752d44 | 201 | if (n_args == 0) { |
pythontech | 0:5868e8752d44 | 202 | // make a list of names in the local name space |
pythontech | 0:5868e8752d44 | 203 | dict = mp_locals_get(); |
pythontech | 0:5868e8752d44 | 204 | } else { // n_args == 1 |
pythontech | 0:5868e8752d44 | 205 | // make a list of names in the given object |
pythontech | 0:5868e8752d44 | 206 | if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) { |
pythontech | 0:5868e8752d44 | 207 | dict = mp_obj_module_get_globals(args[0]); |
pythontech | 0:5868e8752d44 | 208 | } else { |
pythontech | 0:5868e8752d44 | 209 | mp_obj_type_t *type; |
pythontech | 0:5868e8752d44 | 210 | if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) { |
pythontech | 0:5868e8752d44 | 211 | type = MP_OBJ_TO_PTR(args[0]); |
pythontech | 0:5868e8752d44 | 212 | } else { |
pythontech | 0:5868e8752d44 | 213 | type = mp_obj_get_type(args[0]); |
pythontech | 0:5868e8752d44 | 214 | } |
pythontech | 0:5868e8752d44 | 215 | if (type->locals_dict != NULL && type->locals_dict->base.type == &mp_type_dict) { |
pythontech | 0:5868e8752d44 | 216 | dict = type->locals_dict; |
pythontech | 0:5868e8752d44 | 217 | } |
pythontech | 0:5868e8752d44 | 218 | } |
pythontech | 0:5868e8752d44 | 219 | if (mp_obj_is_instance_type(mp_obj_get_type(args[0]))) { |
pythontech | 0:5868e8752d44 | 220 | mp_obj_instance_t *inst = MP_OBJ_TO_PTR(args[0]); |
pythontech | 0:5868e8752d44 | 221 | members = &inst->members; |
pythontech | 0:5868e8752d44 | 222 | } |
pythontech | 0:5868e8752d44 | 223 | } |
pythontech | 0:5868e8752d44 | 224 | |
pythontech | 0:5868e8752d44 | 225 | mp_obj_t dir = mp_obj_new_list(0, NULL); |
pythontech | 0:5868e8752d44 | 226 | if (dict != NULL) { |
pythontech | 0:5868e8752d44 | 227 | for (mp_uint_t i = 0; i < dict->map.alloc; i++) { |
pythontech | 0:5868e8752d44 | 228 | if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) { |
pythontech | 0:5868e8752d44 | 229 | mp_obj_list_append(dir, dict->map.table[i].key); |
pythontech | 0:5868e8752d44 | 230 | } |
pythontech | 0:5868e8752d44 | 231 | } |
pythontech | 0:5868e8752d44 | 232 | } |
pythontech | 0:5868e8752d44 | 233 | if (members != NULL) { |
pythontech | 0:5868e8752d44 | 234 | for (mp_uint_t i = 0; i < members->alloc; i++) { |
pythontech | 0:5868e8752d44 | 235 | if (MP_MAP_SLOT_IS_FILLED(members, i)) { |
pythontech | 0:5868e8752d44 | 236 | mp_obj_list_append(dir, members->table[i].key); |
pythontech | 0:5868e8752d44 | 237 | } |
pythontech | 0:5868e8752d44 | 238 | } |
pythontech | 0:5868e8752d44 | 239 | } |
pythontech | 0:5868e8752d44 | 240 | return dir; |
pythontech | 0:5868e8752d44 | 241 | } |
pythontech | 0:5868e8752d44 | 242 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir); |
pythontech | 0:5868e8752d44 | 243 | |
pythontech | 0:5868e8752d44 | 244 | STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { |
pythontech | 0:5868e8752d44 | 245 | return mp_binary_op(MP_BINARY_OP_DIVMOD, o1_in, o2_in); |
pythontech | 0:5868e8752d44 | 246 | } |
pythontech | 0:5868e8752d44 | 247 | MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod); |
pythontech | 0:5868e8752d44 | 248 | |
pythontech | 0:5868e8752d44 | 249 | STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 250 | // result is guaranteed to be a (small) int |
pythontech | 0:5868e8752d44 | 251 | return mp_unary_op(MP_UNARY_OP_HASH, o_in); |
pythontech | 0:5868e8752d44 | 252 | } |
pythontech | 0:5868e8752d44 | 253 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash); |
pythontech | 0:5868e8752d44 | 254 | |
pythontech | 0:5868e8752d44 | 255 | STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 256 | return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_x), o_in); |
pythontech | 0:5868e8752d44 | 257 | } |
pythontech | 0:5868e8752d44 | 258 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex); |
pythontech | 0:5868e8752d44 | 259 | |
pythontech | 0:5868e8752d44 | 260 | STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 261 | return mp_getiter(o_in); |
pythontech | 0:5868e8752d44 | 262 | } |
pythontech | 0:5868e8752d44 | 263 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter); |
pythontech | 0:5868e8752d44 | 264 | |
pythontech | 0:5868e8752d44 | 265 | #if MICROPY_PY_BUILTINS_MIN_MAX |
pythontech | 0:5868e8752d44 | 266 | |
pythontech | 0:5868e8752d44 | 267 | 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) { |
pythontech | 0:5868e8752d44 | 268 | mp_map_elem_t *key_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP); |
pythontech | 0:5868e8752d44 | 269 | mp_map_elem_t *default_elem; |
pythontech | 0:5868e8752d44 | 270 | mp_obj_t key_fn = key_elem == NULL ? MP_OBJ_NULL : key_elem->value; |
pythontech | 0:5868e8752d44 | 271 | if (n_args == 1) { |
pythontech | 0:5868e8752d44 | 272 | // given an iterable |
pythontech | 0:5868e8752d44 | 273 | mp_obj_t iterable = mp_getiter(args[0]); |
pythontech | 0:5868e8752d44 | 274 | mp_obj_t best_key = MP_OBJ_NULL; |
pythontech | 0:5868e8752d44 | 275 | mp_obj_t best_obj = MP_OBJ_NULL; |
pythontech | 0:5868e8752d44 | 276 | mp_obj_t item; |
pythontech | 0:5868e8752d44 | 277 | while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { |
pythontech | 0:5868e8752d44 | 278 | mp_obj_t key = key_fn == MP_OBJ_NULL ? item : mp_call_function_1(key_fn, item); |
pythontech | 0:5868e8752d44 | 279 | if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) { |
pythontech | 0:5868e8752d44 | 280 | best_key = key; |
pythontech | 0:5868e8752d44 | 281 | best_obj = item; |
pythontech | 0:5868e8752d44 | 282 | } |
pythontech | 0:5868e8752d44 | 283 | } |
pythontech | 0:5868e8752d44 | 284 | if (best_obj == MP_OBJ_NULL) { |
pythontech | 0:5868e8752d44 | 285 | default_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_default), MP_MAP_LOOKUP); |
pythontech | 0:5868e8752d44 | 286 | if (default_elem != NULL) { |
pythontech | 0:5868e8752d44 | 287 | best_obj = default_elem->value; |
pythontech | 0:5868e8752d44 | 288 | } else { |
pythontech | 0:5868e8752d44 | 289 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "arg is an empty sequence")); |
pythontech | 0:5868e8752d44 | 290 | } |
pythontech | 0:5868e8752d44 | 291 | } |
pythontech | 0:5868e8752d44 | 292 | return best_obj; |
pythontech | 0:5868e8752d44 | 293 | } else { |
pythontech | 0:5868e8752d44 | 294 | // given many args |
pythontech | 0:5868e8752d44 | 295 | mp_obj_t best_key = MP_OBJ_NULL; |
pythontech | 0:5868e8752d44 | 296 | mp_obj_t best_obj = MP_OBJ_NULL; |
pythontech | 0:5868e8752d44 | 297 | for (mp_uint_t i = 0; i < n_args; i++) { |
pythontech | 0:5868e8752d44 | 298 | mp_obj_t key = key_fn == MP_OBJ_NULL ? args[i] : mp_call_function_1(key_fn, args[i]); |
pythontech | 0:5868e8752d44 | 299 | if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) { |
pythontech | 0:5868e8752d44 | 300 | best_key = key; |
pythontech | 0:5868e8752d44 | 301 | best_obj = args[i]; |
pythontech | 0:5868e8752d44 | 302 | } |
pythontech | 0:5868e8752d44 | 303 | } |
pythontech | 0:5868e8752d44 | 304 | return best_obj; |
pythontech | 0:5868e8752d44 | 305 | } |
pythontech | 0:5868e8752d44 | 306 | } |
pythontech | 0:5868e8752d44 | 307 | |
pythontech | 0:5868e8752d44 | 308 | STATIC mp_obj_t mp_builtin_max(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
pythontech | 0:5868e8752d44 | 309 | return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_MORE); |
pythontech | 0:5868e8752d44 | 310 | } |
pythontech | 0:5868e8752d44 | 311 | MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_max_obj, 1, mp_builtin_max); |
pythontech | 0:5868e8752d44 | 312 | |
pythontech | 0:5868e8752d44 | 313 | STATIC mp_obj_t mp_builtin_min(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
pythontech | 0:5868e8752d44 | 314 | return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_LESS); |
pythontech | 0:5868e8752d44 | 315 | } |
pythontech | 0:5868e8752d44 | 316 | MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_min_obj, 1, mp_builtin_min); |
pythontech | 0:5868e8752d44 | 317 | |
pythontech | 0:5868e8752d44 | 318 | #endif |
pythontech | 0:5868e8752d44 | 319 | |
pythontech | 0:5868e8752d44 | 320 | STATIC mp_obj_t mp_builtin_next(mp_obj_t o) { |
pythontech | 0:5868e8752d44 | 321 | mp_obj_t ret = mp_iternext_allow_raise(o); |
pythontech | 0:5868e8752d44 | 322 | if (ret == MP_OBJ_STOP_ITERATION) { |
pythontech | 0:5868e8752d44 | 323 | nlr_raise(mp_obj_new_exception(&mp_type_StopIteration)); |
pythontech | 0:5868e8752d44 | 324 | } else { |
pythontech | 0:5868e8752d44 | 325 | return ret; |
pythontech | 0:5868e8752d44 | 326 | } |
pythontech | 0:5868e8752d44 | 327 | } |
pythontech | 0:5868e8752d44 | 328 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next); |
pythontech | 0:5868e8752d44 | 329 | |
pythontech | 0:5868e8752d44 | 330 | STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 331 | return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in); |
pythontech | 0:5868e8752d44 | 332 | } |
pythontech | 0:5868e8752d44 | 333 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct); |
pythontech | 0:5868e8752d44 | 334 | |
pythontech | 0:5868e8752d44 | 335 | STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 336 | mp_uint_t len; |
pythontech | 0:5868e8752d44 | 337 | const char *str = mp_obj_str_get_data(o_in, &len); |
pythontech | 0:5868e8752d44 | 338 | #if MICROPY_PY_BUILTINS_STR_UNICODE |
pythontech | 0:5868e8752d44 | 339 | if (MP_OBJ_IS_STR(o_in)) { |
pythontech | 0:5868e8752d44 | 340 | len = unichar_charlen(str, len); |
pythontech | 0:5868e8752d44 | 341 | if (len == 1) { |
pythontech | 0:5868e8752d44 | 342 | if (!UTF8_IS_NONASCII(*str)) { |
pythontech | 0:5868e8752d44 | 343 | goto return_first_byte; |
pythontech | 0:5868e8752d44 | 344 | } |
pythontech | 0:5868e8752d44 | 345 | mp_int_t ord = *str++ & 0x7F; |
pythontech | 0:5868e8752d44 | 346 | for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) { |
pythontech | 0:5868e8752d44 | 347 | ord &= ~mask; |
pythontech | 0:5868e8752d44 | 348 | } |
pythontech | 0:5868e8752d44 | 349 | while (UTF8_IS_CONT(*str)) { |
pythontech | 0:5868e8752d44 | 350 | ord = (ord << 6) | (*str++ & 0x3F); |
pythontech | 0:5868e8752d44 | 351 | } |
pythontech | 0:5868e8752d44 | 352 | return mp_obj_new_int(ord); |
pythontech | 0:5868e8752d44 | 353 | } |
pythontech | 0:5868e8752d44 | 354 | } else { |
pythontech | 0:5868e8752d44 | 355 | // a bytes object |
pythontech | 0:5868e8752d44 | 356 | if (len == 1) { |
pythontech | 0:5868e8752d44 | 357 | return_first_byte: |
pythontech | 0:5868e8752d44 | 358 | return MP_OBJ_NEW_SMALL_INT(((const byte*)str)[0]); |
pythontech | 0:5868e8752d44 | 359 | } |
pythontech | 0:5868e8752d44 | 360 | } |
pythontech | 0:5868e8752d44 | 361 | #else |
pythontech | 0:5868e8752d44 | 362 | if (len == 1) { |
pythontech | 0:5868e8752d44 | 363 | // don't sign extend when converting to ord |
pythontech | 0:5868e8752d44 | 364 | return mp_obj_new_int(((const byte*)str)[0]); |
pythontech | 0:5868e8752d44 | 365 | } |
pythontech | 0:5868e8752d44 | 366 | #endif |
pythontech | 0:5868e8752d44 | 367 | |
pythontech | 0:5868e8752d44 | 368 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
pythontech | 0:5868e8752d44 | 369 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
pythontech | 0:5868e8752d44 | 370 | "ord expects a character")); |
pythontech | 0:5868e8752d44 | 371 | } else { |
pythontech | 0:5868e8752d44 | 372 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
pythontech | 0:5868e8752d44 | 373 | "ord() expected a character, but string of length %d found", (int)len)); |
pythontech | 0:5868e8752d44 | 374 | } |
pythontech | 0:5868e8752d44 | 375 | } |
pythontech | 0:5868e8752d44 | 376 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord); |
pythontech | 0:5868e8752d44 | 377 | |
pythontech | 0:5868e8752d44 | 378 | STATIC mp_obj_t mp_builtin_pow(size_t n_args, const mp_obj_t *args) { |
pythontech | 0:5868e8752d44 | 379 | assert(2 <= n_args && n_args <= 3); |
pythontech | 0:5868e8752d44 | 380 | switch (n_args) { |
pythontech | 0:5868e8752d44 | 381 | case 2: return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]); |
pythontech | 0:5868e8752d44 | 382 | default: return mp_binary_op(MP_BINARY_OP_MODULO, mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise... |
pythontech | 0:5868e8752d44 | 383 | } |
pythontech | 0:5868e8752d44 | 384 | } |
pythontech | 0:5868e8752d44 | 385 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow); |
pythontech | 0:5868e8752d44 | 386 | |
pythontech | 0:5868e8752d44 | 387 | STATIC mp_obj_t mp_builtin_print(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
pythontech | 0:5868e8752d44 | 388 | mp_map_elem_t *sep_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_MAP_LOOKUP); |
pythontech | 0:5868e8752d44 | 389 | mp_map_elem_t *end_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_end), MP_MAP_LOOKUP); |
pythontech | 0:5868e8752d44 | 390 | const char *sep_data = " "; |
pythontech | 0:5868e8752d44 | 391 | mp_uint_t sep_len = 1; |
pythontech | 0:5868e8752d44 | 392 | const char *end_data = "\n"; |
pythontech | 0:5868e8752d44 | 393 | mp_uint_t end_len = 1; |
pythontech | 0:5868e8752d44 | 394 | if (sep_elem != NULL && sep_elem->value != mp_const_none) { |
pythontech | 0:5868e8752d44 | 395 | sep_data = mp_obj_str_get_data(sep_elem->value, &sep_len); |
pythontech | 0:5868e8752d44 | 396 | } |
pythontech | 0:5868e8752d44 | 397 | if (end_elem != NULL && end_elem->value != mp_const_none) { |
pythontech | 0:5868e8752d44 | 398 | end_data = mp_obj_str_get_data(end_elem->value, &end_len); |
pythontech | 0:5868e8752d44 | 399 | } |
pythontech | 0:5868e8752d44 | 400 | #if MICROPY_PY_IO |
pythontech | 0:5868e8752d44 | 401 | void *stream_obj = &mp_sys_stdout_obj; |
pythontech | 0:5868e8752d44 | 402 | mp_map_elem_t *file_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_file), MP_MAP_LOOKUP); |
pythontech | 0:5868e8752d44 | 403 | if (file_elem != NULL && file_elem->value != mp_const_none) { |
pythontech | 0:5868e8752d44 | 404 | stream_obj = MP_OBJ_TO_PTR(file_elem->value); // XXX may not be a concrete object |
pythontech | 0:5868e8752d44 | 405 | } |
pythontech | 0:5868e8752d44 | 406 | |
pythontech | 0:5868e8752d44 | 407 | mp_print_t print = {stream_obj, mp_stream_write_adaptor}; |
pythontech | 0:5868e8752d44 | 408 | #endif |
pythontech | 0:5868e8752d44 | 409 | for (mp_uint_t i = 0; i < n_args; i++) { |
pythontech | 0:5868e8752d44 | 410 | if (i > 0) { |
pythontech | 0:5868e8752d44 | 411 | #if MICROPY_PY_IO |
pythontech | 0:5868e8752d44 | 412 | mp_stream_write_adaptor(stream_obj, sep_data, sep_len); |
pythontech | 0:5868e8752d44 | 413 | #else |
pythontech | 0:5868e8752d44 | 414 | mp_print_strn(&mp_plat_print, sep_data, sep_len, 0, 0, 0); |
pythontech | 0:5868e8752d44 | 415 | #endif |
pythontech | 0:5868e8752d44 | 416 | } |
pythontech | 0:5868e8752d44 | 417 | #if MICROPY_PY_IO |
pythontech | 0:5868e8752d44 | 418 | mp_obj_print_helper(&print, args[i], PRINT_STR); |
pythontech | 0:5868e8752d44 | 419 | #else |
pythontech | 0:5868e8752d44 | 420 | mp_obj_print_helper(&mp_plat_print, args[i], PRINT_STR); |
pythontech | 0:5868e8752d44 | 421 | #endif |
pythontech | 0:5868e8752d44 | 422 | } |
pythontech | 0:5868e8752d44 | 423 | #if MICROPY_PY_IO |
pythontech | 0:5868e8752d44 | 424 | mp_stream_write_adaptor(stream_obj, end_data, end_len); |
pythontech | 0:5868e8752d44 | 425 | #else |
pythontech | 0:5868e8752d44 | 426 | mp_print_strn(&mp_plat_print, end_data, end_len, 0, 0, 0); |
pythontech | 0:5868e8752d44 | 427 | #endif |
pythontech | 0:5868e8752d44 | 428 | return mp_const_none; |
pythontech | 0:5868e8752d44 | 429 | } |
pythontech | 0:5868e8752d44 | 430 | MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print); |
pythontech | 0:5868e8752d44 | 431 | |
pythontech | 0:5868e8752d44 | 432 | STATIC mp_obj_t mp_builtin___repl_print__(mp_obj_t o) { |
pythontech | 0:5868e8752d44 | 433 | if (o != mp_const_none) { |
pythontech | 0:5868e8752d44 | 434 | #if MICROPY_PY_IO |
pythontech | 0:5868e8752d44 | 435 | mp_obj_print_helper(&mp_sys_stdout_print, o, PRINT_REPR); |
pythontech | 0:5868e8752d44 | 436 | mp_print_str(&mp_sys_stdout_print, "\n"); |
pythontech | 0:5868e8752d44 | 437 | #else |
pythontech | 0:5868e8752d44 | 438 | mp_obj_print_helper(&mp_plat_print, o, PRINT_REPR); |
pythontech | 0:5868e8752d44 | 439 | mp_print_str(&mp_plat_print, "\n"); |
pythontech | 0:5868e8752d44 | 440 | #endif |
pythontech | 0:5868e8752d44 | 441 | #if MICROPY_CAN_OVERRIDE_BUILTINS |
Colin Hogben |
2:c89e95946844 | 442 | // Set "_" special variable |
pythontech | 0:5868e8752d44 | 443 | mp_obj_t dest[2] = {MP_OBJ_SENTINEL, o}; |
pythontech | 0:5868e8752d44 | 444 | mp_type_module.attr(MP_OBJ_FROM_PTR(&mp_module_builtins), MP_QSTR__, dest); |
pythontech | 0:5868e8752d44 | 445 | #endif |
pythontech | 0:5868e8752d44 | 446 | } |
pythontech | 0:5868e8752d44 | 447 | return mp_const_none; |
pythontech | 0:5868e8752d44 | 448 | } |
pythontech | 0:5868e8752d44 | 449 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__); |
pythontech | 0:5868e8752d44 | 450 | |
pythontech | 0:5868e8752d44 | 451 | STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) { |
pythontech | 0:5868e8752d44 | 452 | vstr_t vstr; |
pythontech | 0:5868e8752d44 | 453 | mp_print_t print; |
pythontech | 0:5868e8752d44 | 454 | vstr_init_print(&vstr, 16, &print); |
pythontech | 0:5868e8752d44 | 455 | mp_obj_print_helper(&print, o_in, PRINT_REPR); |
pythontech | 0:5868e8752d44 | 456 | return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); |
pythontech | 0:5868e8752d44 | 457 | } |
pythontech | 0:5868e8752d44 | 458 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr); |
pythontech | 0:5868e8752d44 | 459 | |
pythontech | 0:5868e8752d44 | 460 | STATIC mp_obj_t mp_builtin_round(size_t n_args, const mp_obj_t *args) { |
pythontech | 0:5868e8752d44 | 461 | mp_obj_t o_in = args[0]; |
pythontech | 0:5868e8752d44 | 462 | if (MP_OBJ_IS_INT(o_in)) { |
pythontech | 0:5868e8752d44 | 463 | return o_in; |
pythontech | 0:5868e8752d44 | 464 | } |
pythontech | 0:5868e8752d44 | 465 | #if MICROPY_PY_BUILTINS_FLOAT |
pythontech | 0:5868e8752d44 | 466 | mp_int_t num_dig = 0; |
pythontech | 0:5868e8752d44 | 467 | if (n_args > 1) { |
pythontech | 0:5868e8752d44 | 468 | num_dig = mp_obj_get_int(args[1]); |
pythontech | 0:5868e8752d44 | 469 | mp_float_t val = mp_obj_get_float(o_in); |
pythontech | 0:5868e8752d44 | 470 | mp_float_t mult = MICROPY_FLOAT_C_FUN(pow)(10, num_dig); |
pythontech | 0:5868e8752d44 | 471 | // TODO may lead to overflow |
pythontech | 0:5868e8752d44 | 472 | mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val * mult) / mult; |
pythontech | 0:5868e8752d44 | 473 | return mp_obj_new_float(rounded); |
pythontech | 0:5868e8752d44 | 474 | } |
pythontech | 0:5868e8752d44 | 475 | mp_float_t val = mp_obj_get_float(o_in); |
pythontech | 0:5868e8752d44 | 476 | mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val); |
pythontech | 0:5868e8752d44 | 477 | mp_int_t r = rounded; |
pythontech | 0:5868e8752d44 | 478 | // make rounded value even if it was halfway between ints |
pythontech | 0:5868e8752d44 | 479 | if (val - rounded == 0.5) { |
pythontech | 0:5868e8752d44 | 480 | r = (r + 1) & (~1); |
pythontech | 0:5868e8752d44 | 481 | } else if (val - rounded == -0.5) { |
pythontech | 0:5868e8752d44 | 482 | r &= ~1; |
pythontech | 0:5868e8752d44 | 483 | } |
pythontech | 0:5868e8752d44 | 484 | if (n_args > 1) { |
pythontech | 0:5868e8752d44 | 485 | return mp_obj_new_float(r); |
pythontech | 0:5868e8752d44 | 486 | } |
pythontech | 0:5868e8752d44 | 487 | #else |
pythontech | 0:5868e8752d44 | 488 | mp_int_t r = mp_obj_get_int(o_in); |
pythontech | 0:5868e8752d44 | 489 | #endif |
pythontech | 0:5868e8752d44 | 490 | return mp_obj_new_int(r); |
pythontech | 0:5868e8752d44 | 491 | } |
pythontech | 0:5868e8752d44 | 492 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_round_obj, 1, 2, mp_builtin_round); |
pythontech | 0:5868e8752d44 | 493 | |
pythontech | 0:5868e8752d44 | 494 | STATIC mp_obj_t mp_builtin_sum(size_t n_args, const mp_obj_t *args) { |
pythontech | 0:5868e8752d44 | 495 | assert(1 <= n_args && n_args <= 2); |
pythontech | 0:5868e8752d44 | 496 | mp_obj_t value; |
pythontech | 0:5868e8752d44 | 497 | switch (n_args) { |
pythontech | 0:5868e8752d44 | 498 | case 1: value = MP_OBJ_NEW_SMALL_INT(0); break; |
pythontech | 0:5868e8752d44 | 499 | default: value = args[1]; break; |
pythontech | 0:5868e8752d44 | 500 | } |
pythontech | 0:5868e8752d44 | 501 | mp_obj_t iterable = mp_getiter(args[0]); |
pythontech | 0:5868e8752d44 | 502 | mp_obj_t item; |
pythontech | 0:5868e8752d44 | 503 | while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { |
pythontech | 0:5868e8752d44 | 504 | value = mp_binary_op(MP_BINARY_OP_ADD, value, item); |
pythontech | 0:5868e8752d44 | 505 | } |
pythontech | 0:5868e8752d44 | 506 | return value; |
pythontech | 0:5868e8752d44 | 507 | } |
pythontech | 0:5868e8752d44 | 508 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum); |
pythontech | 0:5868e8752d44 | 509 | |
pythontech | 0:5868e8752d44 | 510 | STATIC mp_obj_t mp_builtin_sorted(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
pythontech | 0:5868e8752d44 | 511 | assert(n_args >= 1); |
pythontech | 0:5868e8752d44 | 512 | if (n_args > 1) { |
pythontech | 0:5868e8752d44 | 513 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
pythontech | 0:5868e8752d44 | 514 | "must use keyword argument for key function")); |
pythontech | 0:5868e8752d44 | 515 | } |
pythontech | 0:5868e8752d44 | 516 | mp_obj_t self = mp_type_list.make_new(&mp_type_list, 1, 0, args); |
pythontech | 0:5868e8752d44 | 517 | mp_obj_list_sort(1, &self, kwargs); |
pythontech | 0:5868e8752d44 | 518 | |
pythontech | 0:5868e8752d44 | 519 | return self; |
pythontech | 0:5868e8752d44 | 520 | } |
pythontech | 0:5868e8752d44 | 521 | MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted); |
pythontech | 0:5868e8752d44 | 522 | |
pythontech | 0:5868e8752d44 | 523 | // See mp_load_attr() if making any changes |
pythontech | 0:5868e8752d44 | 524 | STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) { |
pythontech | 0:5868e8752d44 | 525 | mp_obj_t dest[2]; |
pythontech | 0:5868e8752d44 | 526 | // use load_method, raising or not raising exception |
pythontech | 0:5868e8752d44 | 527 | ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest); |
pythontech | 0:5868e8752d44 | 528 | if (dest[0] == MP_OBJ_NULL) { |
pythontech | 0:5868e8752d44 | 529 | return defval; |
pythontech | 0:5868e8752d44 | 530 | } else if (dest[1] == MP_OBJ_NULL) { |
pythontech | 0:5868e8752d44 | 531 | // load_method returned just a normal attribute |
pythontech | 0:5868e8752d44 | 532 | return dest[0]; |
pythontech | 0:5868e8752d44 | 533 | } else { |
pythontech | 0:5868e8752d44 | 534 | // load_method returned a method, so build a bound method object |
pythontech | 0:5868e8752d44 | 535 | return mp_obj_new_bound_meth(dest[0], dest[1]); |
pythontech | 0:5868e8752d44 | 536 | } |
pythontech | 0:5868e8752d44 | 537 | } |
pythontech | 0:5868e8752d44 | 538 | |
pythontech | 0:5868e8752d44 | 539 | STATIC mp_obj_t mp_builtin_getattr(size_t n_args, const mp_obj_t *args) { |
pythontech | 0:5868e8752d44 | 540 | mp_obj_t defval = MP_OBJ_NULL; |
pythontech | 0:5868e8752d44 | 541 | if (n_args > 2) { |
pythontech | 0:5868e8752d44 | 542 | defval = args[2]; |
pythontech | 0:5868e8752d44 | 543 | } |
pythontech | 0:5868e8752d44 | 544 | return mp_load_attr_default(args[0], mp_obj_str_get_qstr(args[1]), defval); |
pythontech | 0:5868e8752d44 | 545 | } |
pythontech | 0:5868e8752d44 | 546 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr); |
pythontech | 0:5868e8752d44 | 547 | |
pythontech | 0:5868e8752d44 | 548 | STATIC mp_obj_t mp_builtin_setattr(mp_obj_t base, mp_obj_t attr, mp_obj_t value) { |
pythontech | 0:5868e8752d44 | 549 | mp_store_attr(base, mp_obj_str_get_qstr(attr), value); |
pythontech | 0:5868e8752d44 | 550 | return mp_const_none; |
pythontech | 0:5868e8752d44 | 551 | } |
pythontech | 0:5868e8752d44 | 552 | MP_DEFINE_CONST_FUN_OBJ_3(mp_builtin_setattr_obj, mp_builtin_setattr); |
pythontech | 0:5868e8752d44 | 553 | |
pythontech | 0:5868e8752d44 | 554 | STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) { |
pythontech | 0:5868e8752d44 | 555 | qstr attr = mp_obj_str_get_qstr(attr_in); |
pythontech | 0:5868e8752d44 | 556 | |
pythontech | 0:5868e8752d44 | 557 | mp_obj_t dest[2]; |
pythontech | 0:5868e8752d44 | 558 | // TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr |
pythontech | 0:5868e8752d44 | 559 | // explicitly says "This is implemented by calling getattr(object, name) and seeing |
pythontech | 0:5868e8752d44 | 560 | // whether it raises an AttributeError or not.", so we should explicitly wrap this |
pythontech | 0:5868e8752d44 | 561 | // in nlr_push and handle exception. |
pythontech | 0:5868e8752d44 | 562 | mp_load_method_maybe(object_in, attr, dest); |
pythontech | 0:5868e8752d44 | 563 | |
pythontech | 0:5868e8752d44 | 564 | return mp_obj_new_bool(dest[0] != MP_OBJ_NULL); |
pythontech | 0:5868e8752d44 | 565 | } |
pythontech | 0:5868e8752d44 | 566 | MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr); |
pythontech | 0:5868e8752d44 | 567 | |
pythontech | 0:5868e8752d44 | 568 | STATIC mp_obj_t mp_builtin_globals(void) { |
pythontech | 0:5868e8752d44 | 569 | return MP_OBJ_FROM_PTR(mp_globals_get()); |
pythontech | 0:5868e8752d44 | 570 | } |
pythontech | 0:5868e8752d44 | 571 | MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_builtin_globals); |
pythontech | 0:5868e8752d44 | 572 | |
pythontech | 0:5868e8752d44 | 573 | STATIC mp_obj_t mp_builtin_locals(void) { |
pythontech | 0:5868e8752d44 | 574 | return MP_OBJ_FROM_PTR(mp_locals_get()); |
pythontech | 0:5868e8752d44 | 575 | } |
pythontech | 0:5868e8752d44 | 576 | MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_builtin_locals); |
pythontech | 0:5868e8752d44 | 577 | |
pythontech | 0:5868e8752d44 | 578 | // These are defined in terms of MicroPython API functions right away |
pythontech | 0:5868e8752d44 | 579 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_obj_id); |
pythontech | 0:5868e8752d44 | 580 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_obj_len); |
pythontech | 0:5868e8752d44 | 581 | |
pythontech | 0:5868e8752d44 | 582 | STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = { |
pythontech | 0:5868e8752d44 | 583 | // built-in core functions |
pythontech | 0:5868e8752d44 | 584 | { MP_ROM_QSTR(MP_QSTR___build_class__), MP_ROM_PTR(&mp_builtin___build_class___obj) }, |
pythontech | 0:5868e8752d44 | 585 | { MP_ROM_QSTR(MP_QSTR___import__), MP_ROM_PTR(&mp_builtin___import___obj) }, |
pythontech | 0:5868e8752d44 | 586 | { MP_ROM_QSTR(MP_QSTR___repl_print__), MP_ROM_PTR(&mp_builtin___repl_print___obj) }, |
pythontech | 0:5868e8752d44 | 587 | |
pythontech | 0:5868e8752d44 | 588 | // built-in types |
pythontech | 0:5868e8752d44 | 589 | { MP_ROM_QSTR(MP_QSTR_bool), MP_ROM_PTR(&mp_type_bool) }, |
pythontech | 0:5868e8752d44 | 590 | { MP_ROM_QSTR(MP_QSTR_bytes), MP_ROM_PTR(&mp_type_bytes) }, |
pythontech | 0:5868e8752d44 | 591 | #if MICROPY_PY_BUILTINS_BYTEARRAY |
pythontech | 0:5868e8752d44 | 592 | { MP_ROM_QSTR(MP_QSTR_bytearray), MP_ROM_PTR(&mp_type_bytearray) }, |
pythontech | 0:5868e8752d44 | 593 | #endif |
pythontech | 0:5868e8752d44 | 594 | #if MICROPY_PY_BUILTINS_COMPLEX |
pythontech | 0:5868e8752d44 | 595 | { MP_ROM_QSTR(MP_QSTR_complex), MP_ROM_PTR(&mp_type_complex) }, |
pythontech | 0:5868e8752d44 | 596 | #endif |
pythontech | 0:5868e8752d44 | 597 | { MP_ROM_QSTR(MP_QSTR_dict), MP_ROM_PTR(&mp_type_dict) }, |
pythontech | 0:5868e8752d44 | 598 | #if MICROPY_PY_BUILTINS_ENUMERATE |
pythontech | 0:5868e8752d44 | 599 | { MP_ROM_QSTR(MP_QSTR_enumerate), MP_ROM_PTR(&mp_type_enumerate) }, |
pythontech | 0:5868e8752d44 | 600 | #endif |
pythontech | 0:5868e8752d44 | 601 | #if MICROPY_PY_BUILTINS_FILTER |
pythontech | 0:5868e8752d44 | 602 | { MP_ROM_QSTR(MP_QSTR_filter), MP_ROM_PTR(&mp_type_filter) }, |
pythontech | 0:5868e8752d44 | 603 | #endif |
pythontech | 0:5868e8752d44 | 604 | #if MICROPY_PY_BUILTINS_FLOAT |
pythontech | 0:5868e8752d44 | 605 | { MP_ROM_QSTR(MP_QSTR_float), MP_ROM_PTR(&mp_type_float) }, |
pythontech | 0:5868e8752d44 | 606 | #endif |
pythontech | 0:5868e8752d44 | 607 | #if MICROPY_PY_BUILTINS_SET && MICROPY_PY_BUILTINS_FROZENSET |
pythontech | 0:5868e8752d44 | 608 | { MP_ROM_QSTR(MP_QSTR_frozenset), MP_ROM_PTR(&mp_type_frozenset) }, |
pythontech | 0:5868e8752d44 | 609 | #endif |
pythontech | 0:5868e8752d44 | 610 | { MP_ROM_QSTR(MP_QSTR_int), MP_ROM_PTR(&mp_type_int) }, |
pythontech | 0:5868e8752d44 | 611 | { MP_ROM_QSTR(MP_QSTR_list), MP_ROM_PTR(&mp_type_list) }, |
pythontech | 0:5868e8752d44 | 612 | { MP_ROM_QSTR(MP_QSTR_map), MP_ROM_PTR(&mp_type_map) }, |
pythontech | 0:5868e8752d44 | 613 | #if MICROPY_PY_BUILTINS_MEMORYVIEW |
pythontech | 0:5868e8752d44 | 614 | { MP_ROM_QSTR(MP_QSTR_memoryview), MP_ROM_PTR(&mp_type_memoryview) }, |
pythontech | 0:5868e8752d44 | 615 | #endif |
pythontech | 0:5868e8752d44 | 616 | { MP_ROM_QSTR(MP_QSTR_object), MP_ROM_PTR(&mp_type_object) }, |
pythontech | 0:5868e8752d44 | 617 | #if MICROPY_PY_BUILTINS_PROPERTY |
pythontech | 0:5868e8752d44 | 618 | { MP_ROM_QSTR(MP_QSTR_property), MP_ROM_PTR(&mp_type_property) }, |
pythontech | 0:5868e8752d44 | 619 | #endif |
pythontech | 0:5868e8752d44 | 620 | { MP_ROM_QSTR(MP_QSTR_range), MP_ROM_PTR(&mp_type_range) }, |
pythontech | 0:5868e8752d44 | 621 | #if MICROPY_PY_BUILTINS_REVERSED |
pythontech | 0:5868e8752d44 | 622 | { MP_ROM_QSTR(MP_QSTR_reversed), MP_ROM_PTR(&mp_type_reversed) }, |
pythontech | 0:5868e8752d44 | 623 | #endif |
pythontech | 0:5868e8752d44 | 624 | #if MICROPY_PY_BUILTINS_SET |
pythontech | 0:5868e8752d44 | 625 | { MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&mp_type_set) }, |
pythontech | 0:5868e8752d44 | 626 | #endif |
pythontech | 0:5868e8752d44 | 627 | { MP_ROM_QSTR(MP_QSTR_str), MP_ROM_PTR(&mp_type_str) }, |
pythontech | 0:5868e8752d44 | 628 | { MP_ROM_QSTR(MP_QSTR_super), MP_ROM_PTR(&mp_type_super) }, |
pythontech | 0:5868e8752d44 | 629 | { MP_ROM_QSTR(MP_QSTR_tuple), MP_ROM_PTR(&mp_type_tuple) }, |
pythontech | 0:5868e8752d44 | 630 | { MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&mp_type_type) }, |
pythontech | 0:5868e8752d44 | 631 | { MP_ROM_QSTR(MP_QSTR_zip), MP_ROM_PTR(&mp_type_zip) }, |
pythontech | 0:5868e8752d44 | 632 | |
pythontech | 0:5868e8752d44 | 633 | { MP_ROM_QSTR(MP_QSTR_classmethod), MP_ROM_PTR(&mp_type_classmethod) }, |
pythontech | 0:5868e8752d44 | 634 | { MP_ROM_QSTR(MP_QSTR_staticmethod), MP_ROM_PTR(&mp_type_staticmethod) }, |
pythontech | 0:5868e8752d44 | 635 | |
pythontech | 0:5868e8752d44 | 636 | // built-in objects |
pythontech | 0:5868e8752d44 | 637 | { MP_ROM_QSTR(MP_QSTR_Ellipsis), MP_ROM_PTR(&mp_const_ellipsis_obj) }, |
pythontech | 0:5868e8752d44 | 638 | #if MICROPY_PY_BUILTINS_NOTIMPLEMENTED |
pythontech | 0:5868e8752d44 | 639 | { MP_ROM_QSTR(MP_QSTR_NotImplemented), MP_ROM_PTR(&mp_const_notimplemented_obj) }, |
pythontech | 0:5868e8752d44 | 640 | #endif |
pythontech | 0:5868e8752d44 | 641 | |
pythontech | 0:5868e8752d44 | 642 | // built-in user functions |
pythontech | 0:5868e8752d44 | 643 | { MP_ROM_QSTR(MP_QSTR_abs), MP_ROM_PTR(&mp_builtin_abs_obj) }, |
pythontech | 0:5868e8752d44 | 644 | { MP_ROM_QSTR(MP_QSTR_all), MP_ROM_PTR(&mp_builtin_all_obj) }, |
pythontech | 0:5868e8752d44 | 645 | { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&mp_builtin_any_obj) }, |
pythontech | 0:5868e8752d44 | 646 | { MP_ROM_QSTR(MP_QSTR_bin), MP_ROM_PTR(&mp_builtin_bin_obj) }, |
pythontech | 0:5868e8752d44 | 647 | { MP_ROM_QSTR(MP_QSTR_callable), MP_ROM_PTR(&mp_builtin_callable_obj) }, |
pythontech | 0:5868e8752d44 | 648 | #if MICROPY_PY_BUILTINS_COMPILE |
pythontech | 0:5868e8752d44 | 649 | { MP_ROM_QSTR(MP_QSTR_compile), MP_ROM_PTR(&mp_builtin_compile_obj) }, |
pythontech | 0:5868e8752d44 | 650 | #endif |
pythontech | 0:5868e8752d44 | 651 | { MP_ROM_QSTR(MP_QSTR_chr), MP_ROM_PTR(&mp_builtin_chr_obj) }, |
pythontech | 0:5868e8752d44 | 652 | { MP_ROM_QSTR(MP_QSTR_dir), MP_ROM_PTR(&mp_builtin_dir_obj) }, |
pythontech | 0:5868e8752d44 | 653 | { MP_ROM_QSTR(MP_QSTR_divmod), MP_ROM_PTR(&mp_builtin_divmod_obj) }, |
pythontech | 0:5868e8752d44 | 654 | #if MICROPY_PY_BUILTINS_EVAL_EXEC |
pythontech | 0:5868e8752d44 | 655 | { MP_ROM_QSTR(MP_QSTR_eval), MP_ROM_PTR(&mp_builtin_eval_obj) }, |
pythontech | 0:5868e8752d44 | 656 | { MP_ROM_QSTR(MP_QSTR_exec), MP_ROM_PTR(&mp_builtin_exec_obj) }, |
pythontech | 0:5868e8752d44 | 657 | #endif |
pythontech | 0:5868e8752d44 | 658 | #if MICROPY_PY_BUILTINS_EXECFILE |
pythontech | 0:5868e8752d44 | 659 | { MP_ROM_QSTR(MP_QSTR_execfile), MP_ROM_PTR(&mp_builtin_execfile_obj) }, |
pythontech | 0:5868e8752d44 | 660 | #endif |
pythontech | 0:5868e8752d44 | 661 | { MP_ROM_QSTR(MP_QSTR_getattr), MP_ROM_PTR(&mp_builtin_getattr_obj) }, |
pythontech | 0:5868e8752d44 | 662 | { MP_ROM_QSTR(MP_QSTR_setattr), MP_ROM_PTR(&mp_builtin_setattr_obj) }, |
pythontech | 0:5868e8752d44 | 663 | { MP_ROM_QSTR(MP_QSTR_globals), MP_ROM_PTR(&mp_builtin_globals_obj) }, |
pythontech | 0:5868e8752d44 | 664 | { MP_ROM_QSTR(MP_QSTR_hasattr), MP_ROM_PTR(&mp_builtin_hasattr_obj) }, |
pythontech | 0:5868e8752d44 | 665 | { MP_ROM_QSTR(MP_QSTR_hash), MP_ROM_PTR(&mp_builtin_hash_obj) }, |
pythontech | 0:5868e8752d44 | 666 | { MP_ROM_QSTR(MP_QSTR_hex), MP_ROM_PTR(&mp_builtin_hex_obj) }, |
pythontech | 0:5868e8752d44 | 667 | { MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&mp_builtin_id_obj) }, |
pythontech | 0:5868e8752d44 | 668 | { MP_ROM_QSTR(MP_QSTR_isinstance), MP_ROM_PTR(&mp_builtin_isinstance_obj) }, |
pythontech | 0:5868e8752d44 | 669 | { MP_ROM_QSTR(MP_QSTR_issubclass), MP_ROM_PTR(&mp_builtin_issubclass_obj) }, |
pythontech | 0:5868e8752d44 | 670 | { MP_ROM_QSTR(MP_QSTR_iter), MP_ROM_PTR(&mp_builtin_iter_obj) }, |
pythontech | 0:5868e8752d44 | 671 | { MP_ROM_QSTR(MP_QSTR_len), MP_ROM_PTR(&mp_builtin_len_obj) }, |
pythontech | 0:5868e8752d44 | 672 | { MP_ROM_QSTR(MP_QSTR_locals), MP_ROM_PTR(&mp_builtin_locals_obj) }, |
pythontech | 0:5868e8752d44 | 673 | #if MICROPY_PY_BUILTINS_MIN_MAX |
pythontech | 0:5868e8752d44 | 674 | { MP_ROM_QSTR(MP_QSTR_max), MP_ROM_PTR(&mp_builtin_max_obj) }, |
pythontech | 0:5868e8752d44 | 675 | { MP_ROM_QSTR(MP_QSTR_min), MP_ROM_PTR(&mp_builtin_min_obj) }, |
pythontech | 0:5868e8752d44 | 676 | #endif |
pythontech | 0:5868e8752d44 | 677 | { MP_ROM_QSTR(MP_QSTR_next), MP_ROM_PTR(&mp_builtin_next_obj) }, |
pythontech | 0:5868e8752d44 | 678 | { MP_ROM_QSTR(MP_QSTR_oct), MP_ROM_PTR(&mp_builtin_oct_obj) }, |
pythontech | 0:5868e8752d44 | 679 | { MP_ROM_QSTR(MP_QSTR_ord), MP_ROM_PTR(&mp_builtin_ord_obj) }, |
pythontech | 0:5868e8752d44 | 680 | { MP_ROM_QSTR(MP_QSTR_pow), MP_ROM_PTR(&mp_builtin_pow_obj) }, |
pythontech | 0:5868e8752d44 | 681 | { MP_ROM_QSTR(MP_QSTR_print), MP_ROM_PTR(&mp_builtin_print_obj) }, |
pythontech | 0:5868e8752d44 | 682 | { MP_ROM_QSTR(MP_QSTR_repr), MP_ROM_PTR(&mp_builtin_repr_obj) }, |
pythontech | 0:5868e8752d44 | 683 | { MP_ROM_QSTR(MP_QSTR_round), MP_ROM_PTR(&mp_builtin_round_obj) }, |
pythontech | 0:5868e8752d44 | 684 | { MP_ROM_QSTR(MP_QSTR_sorted), MP_ROM_PTR(&mp_builtin_sorted_obj) }, |
pythontech | 0:5868e8752d44 | 685 | { MP_ROM_QSTR(MP_QSTR_sum), MP_ROM_PTR(&mp_builtin_sum_obj) }, |
pythontech | 0:5868e8752d44 | 686 | |
pythontech | 0:5868e8752d44 | 687 | // built-in exceptions |
pythontech | 0:5868e8752d44 | 688 | { MP_ROM_QSTR(MP_QSTR_BaseException), MP_ROM_PTR(&mp_type_BaseException) }, |
pythontech | 0:5868e8752d44 | 689 | { MP_ROM_QSTR(MP_QSTR_ArithmeticError), MP_ROM_PTR(&mp_type_ArithmeticError) }, |
pythontech | 0:5868e8752d44 | 690 | { MP_ROM_QSTR(MP_QSTR_AssertionError), MP_ROM_PTR(&mp_type_AssertionError) }, |
pythontech | 0:5868e8752d44 | 691 | { MP_ROM_QSTR(MP_QSTR_AttributeError), MP_ROM_PTR(&mp_type_AttributeError) }, |
pythontech | 0:5868e8752d44 | 692 | { MP_ROM_QSTR(MP_QSTR_EOFError), MP_ROM_PTR(&mp_type_EOFError) }, |
pythontech | 0:5868e8752d44 | 693 | { MP_ROM_QSTR(MP_QSTR_Exception), MP_ROM_PTR(&mp_type_Exception) }, |
pythontech | 0:5868e8752d44 | 694 | { MP_ROM_QSTR(MP_QSTR_GeneratorExit), MP_ROM_PTR(&mp_type_GeneratorExit) }, |
pythontech | 0:5868e8752d44 | 695 | { MP_ROM_QSTR(MP_QSTR_ImportError), MP_ROM_PTR(&mp_type_ImportError) }, |
pythontech | 0:5868e8752d44 | 696 | { MP_ROM_QSTR(MP_QSTR_IndentationError), MP_ROM_PTR(&mp_type_IndentationError) }, |
pythontech | 0:5868e8752d44 | 697 | { MP_ROM_QSTR(MP_QSTR_IndexError), MP_ROM_PTR(&mp_type_IndexError) }, |
pythontech | 0:5868e8752d44 | 698 | { MP_ROM_QSTR(MP_QSTR_KeyboardInterrupt), MP_ROM_PTR(&mp_type_KeyboardInterrupt) }, |
pythontech | 0:5868e8752d44 | 699 | { MP_ROM_QSTR(MP_QSTR_KeyError), MP_ROM_PTR(&mp_type_KeyError) }, |
pythontech | 0:5868e8752d44 | 700 | { MP_ROM_QSTR(MP_QSTR_LookupError), MP_ROM_PTR(&mp_type_LookupError) }, |
pythontech | 0:5868e8752d44 | 701 | { MP_ROM_QSTR(MP_QSTR_MemoryError), MP_ROM_PTR(&mp_type_MemoryError) }, |
pythontech | 0:5868e8752d44 | 702 | { MP_ROM_QSTR(MP_QSTR_NameError), MP_ROM_PTR(&mp_type_NameError) }, |
pythontech | 0:5868e8752d44 | 703 | { MP_ROM_QSTR(MP_QSTR_NotImplementedError), MP_ROM_PTR(&mp_type_NotImplementedError) }, |
pythontech | 0:5868e8752d44 | 704 | { MP_ROM_QSTR(MP_QSTR_OSError), MP_ROM_PTR(&mp_type_OSError) }, |
pythontech | 0:5868e8752d44 | 705 | { MP_ROM_QSTR(MP_QSTR_OverflowError), MP_ROM_PTR(&mp_type_OverflowError) }, |
pythontech | 0:5868e8752d44 | 706 | { MP_ROM_QSTR(MP_QSTR_RuntimeError), MP_ROM_PTR(&mp_type_RuntimeError) }, |
Colin Hogben |
2:c89e95946844 | 707 | #if MICROPY_PY_ASYNC_AWAIT |
Colin Hogben |
2:c89e95946844 | 708 | { MP_ROM_QSTR(MP_QSTR_StopAsyncIteration), MP_ROM_PTR(&mp_type_StopAsyncIteration) }, |
Colin Hogben |
2:c89e95946844 | 709 | #endif |
pythontech | 0:5868e8752d44 | 710 | { MP_ROM_QSTR(MP_QSTR_StopIteration), MP_ROM_PTR(&mp_type_StopIteration) }, |
pythontech | 0:5868e8752d44 | 711 | { MP_ROM_QSTR(MP_QSTR_SyntaxError), MP_ROM_PTR(&mp_type_SyntaxError) }, |
pythontech | 0:5868e8752d44 | 712 | { MP_ROM_QSTR(MP_QSTR_SystemExit), MP_ROM_PTR(&mp_type_SystemExit) }, |
pythontech | 0:5868e8752d44 | 713 | { MP_ROM_QSTR(MP_QSTR_TypeError), MP_ROM_PTR(&mp_type_TypeError) }, |
pythontech | 0:5868e8752d44 | 714 | #if MICROPY_PY_BUILTINS_STR_UNICODE |
pythontech | 0:5868e8752d44 | 715 | { MP_ROM_QSTR(MP_QSTR_UnicodeError), MP_ROM_PTR(&mp_type_UnicodeError) }, |
pythontech | 0:5868e8752d44 | 716 | #endif |
pythontech | 0:5868e8752d44 | 717 | { MP_ROM_QSTR(MP_QSTR_ValueError), MP_ROM_PTR(&mp_type_ValueError) }, |
pythontech | 0:5868e8752d44 | 718 | #if MICROPY_EMIT_NATIVE |
pythontech | 0:5868e8752d44 | 719 | { MP_ROM_QSTR(MP_QSTR_ViperTypeError), MP_ROM_PTR(&mp_type_ViperTypeError) }, |
pythontech | 0:5868e8752d44 | 720 | #endif |
pythontech | 0:5868e8752d44 | 721 | { MP_ROM_QSTR(MP_QSTR_ZeroDivisionError), MP_ROM_PTR(&mp_type_ZeroDivisionError) }, |
pythontech | 0:5868e8752d44 | 722 | // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/ |
pythontech | 0:5868e8752d44 | 723 | // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation |
pythontech | 0:5868e8752d44 | 724 | |
pythontech | 0:5868e8752d44 | 725 | // Extra builtins as defined by a port |
pythontech | 0:5868e8752d44 | 726 | MICROPY_PORT_BUILTINS |
pythontech | 0:5868e8752d44 | 727 | }; |
pythontech | 0:5868e8752d44 | 728 | |
pythontech | 0:5868e8752d44 | 729 | MP_DEFINE_CONST_DICT(mp_module_builtins_globals, mp_module_builtins_globals_table); |
pythontech | 0:5868e8752d44 | 730 | |
pythontech | 0:5868e8752d44 | 731 | const mp_obj_module_t mp_module_builtins = { |
pythontech | 0:5868e8752d44 | 732 | .base = { &mp_type_module }, |
pythontech | 0:5868e8752d44 | 733 | .name = MP_QSTR_builtins, |
pythontech | 0:5868e8752d44 | 734 | .globals = (mp_obj_dict_t*)&mp_module_builtins_globals, |
pythontech | 0:5868e8752d44 | 735 | }; |