blob: 594f385f4c968f7fbf66424f55bbecd1ed813038 [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
Damien660365e2013-12-17 18:27:24 +000027#include <stdio.h>
Damien660365e2013-12-17 18:27:24 +000028#include <assert.h>
29
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/nlr.h"
31#include "py/smallint.h"
32#include "py/objstr.h"
33#include "py/runtime0.h"
34#include "py/runtime.h"
35#include "py/builtin.h"
36#include "py/stream.h"
37#include "py/pfenv.h"
Damien660365e2013-12-17 18:27:24 +000038
Damien Georgefb510b32014-06-01 13:32:54 +010039#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000040#include <math.h>
41#endif
42
Damien Georgeb97669a2014-01-08 11:47:55 +000043// args[0] is function from class body
44// args[1] is class name
45// args[2:] are base objects
Damien Georged182b982014-08-30 14:19:41 +010046STATIC mp_obj_t mp_builtin___build_class__(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgeb97669a2014-01-08 11:47:55 +000047 assert(2 <= n_args);
48
Damien George7efc5b32014-04-05 22:36:42 +010049 // set the new classes __locals__ object
50 mp_obj_dict_t *old_locals = mp_locals_get();
Damien George062478e2014-01-09 20:57:50 +000051 mp_obj_t class_locals = mp_obj_new_dict(0);
Damien George7efc5b32014-04-05 22:36:42 +010052 mp_locals_set(class_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000053
54 // call the class code
Damien George882b3632014-04-02 15:56:31 +010055 mp_obj_t cell = mp_call_function_0(args[0]);
Damiena3dcd9e2013-12-17 21:35:38 +000056
57 // restore old __locals__ object
Damien Georged17926d2014-03-30 13:35:08 +010058 mp_locals_set(old_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000059
Damien Georgeb97669a2014-01-08 11:47:55 +000060 // get the class type (meta object) from the base objects
61 mp_obj_t meta;
62 if (n_args == 2) {
63 // no explicit bases, so use 'type'
Damien Georgec5966122014-02-15 16:10:44 +000064 meta = (mp_obj_t)&mp_type_type;
Damien Georgeb97669a2014-01-08 11:47:55 +000065 } else {
66 // use type of first base object
67 meta = mp_obj_get_type(args[2]);
68 }
Damien Georgeb97669a2014-01-08 11:47:55 +000069
70 // TODO do proper metaclass resolution for multiple base objects
71
Damien Georgeb97669a2014-01-08 11:47:55 +000072 // create the new class using a call to the meta object
Damien Georgeb97669a2014-01-08 11:47:55 +000073 mp_obj_t meta_args[3];
Damien George20006db2014-01-18 14:10:48 +000074 meta_args[0] = args[1]; // class name
Damien Georgeb97669a2014-01-08 11:47:55 +000075 meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases
Damien George20006db2014-01-18 14:10:48 +000076 meta_args[2] = class_locals; // dict of members
Damien Georged17926d2014-03-30 13:35:08 +010077 mp_obj_t new_class = mp_call_function_n_kw(meta, 3, 0, meta_args);
Damien Georgeb97669a2014-01-08 11:47:55 +000078
79 // store into cell if neede
80 if (cell != mp_const_none) {
81 mp_obj_cell_set(cell, new_class);
82 }
83
84 return new_class;
Damiena3dcd9e2013-12-17 21:35:38 +000085}
Damien Georgeb97669a2014-01-08 11:47:55 +000086MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
87
Damien George69c5fe12014-08-12 18:13:44 +010088STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000089 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien George40f3c022014-07-03 13:25:24 +010090 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
Damien660365e2013-12-17 18:27:24 +000091 if (val < 0) {
92 val = -val;
93 }
Damiend99b0522013-12-21 18:17:45 +000094 return MP_OBJ_NEW_SMALL_INT(val);
Damien Georgefb510b32014-06-01 13:32:54 +010095#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000096 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +000097 mp_float_t value = mp_obj_float_get(o_in);
Damien660365e2013-12-17 18:27:24 +000098 // TODO check for NaN etc
Damiend99b0522013-12-21 18:17:45 +000099 if (value < 0) {
100 return mp_obj_new_float(-value);
Damien660365e2013-12-17 18:27:24 +0000101 } else {
102 return o_in;
103 }
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300104#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0c36da02014-03-08 15:24:39 +0000105 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000106 mp_float_t real, imag;
107 mp_obj_complex_get(o_in, &real, &imag);
Damien George0c36da02014-03-08 15:24:39 +0000108 return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag));
Damien660365e2013-12-17 18:27:24 +0000109#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300110#endif
Damien660365e2013-12-17 18:27:24 +0000111 } else {
112 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000113 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +0000114 }
115}
Damien George23005372014-01-13 19:39:01 +0000116MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
117
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200118STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100119 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000120 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100121 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100122 if (!mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000123 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000124 }
125 }
Damiend99b0522013-12-21 18:17:45 +0000126 return mp_const_true;
Damien660365e2013-12-17 18:27:24 +0000127}
Damien George23005372014-01-13 19:39:01 +0000128MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
129
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200130STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100131 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000132 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100133 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100134 if (mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000135 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000136 }
137 }
Damiend99b0522013-12-21 18:17:45 +0000138 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000139}
Damien George23005372014-01-13 19:39:01 +0000140MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
141
Damien George897fe0c2014-04-15 22:03:55 +0100142STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) {
143 mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_b_brace_close_), o_in };
Paul Sokolovskyed3b20a2015-01-04 13:26:43 +0200144 return mp_obj_str_format(MP_ARRAY_SIZE(args), args, NULL);
Damien George897fe0c2014-04-15 22:03:55 +0100145}
Damien George897fe0c2014-04-15 22:03:55 +0100146MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin);
147
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200148STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000149 if (mp_obj_is_callable(o_in)) {
150 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000151 } else {
Damiend99b0522013-12-21 18:17:45 +0000152 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000153 }
154}
Damien George23005372014-01-13 19:39:01 +0000155MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
156
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200157STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300158 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georgec9aa58e2014-07-31 13:41:43 +0000159 mp_uint_t c = mp_obj_get_int(o_in);
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000160 char str[4];
161 int len = 0;
162 if (c < 0x80) {
163 *str = c; len = 1;
164 } else if (c < 0x800) {
165 str[0] = (c >> 6) | 0xC0;
166 str[1] = (c & 0x3F) | 0x80;
167 len = 2;
168 } else if (c < 0x10000) {
169 str[0] = (c >> 12) | 0xE0;
170 str[1] = ((c >> 6) & 0x3F) | 0x80;
171 str[2] = (c & 0x3F) | 0x80;
172 len = 3;
173 } else if (c < 0x110000) {
174 str[0] = (c >> 18) | 0xF0;
175 str[1] = ((c >> 12) & 0x3F) | 0x80;
176 str[2] = ((c >> 6) & 0x3F) | 0x80;
177 str[3] = (c & 0x3F) | 0x80;
178 len = 4;
Damiena3dcd9e2013-12-17 21:35:38 +0000179 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100180 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
Damiena3dcd9e2013-12-17 21:35:38 +0000181 }
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000182 return mp_obj_new_str(str, len, true);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300183 #else
Damien George40f3c022014-07-03 13:25:24 +0100184 mp_int_t ord = mp_obj_get_int(o_in);
Damien George16677ce2015-01-28 14:07:11 +0000185 if (0 <= ord && ord <= 0xff) {
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300186 char str[1] = {ord};
187 return mp_obj_new_str(str, 1, true);
188 } else {
Damien George16677ce2015-01-28 14:07:11 +0000189 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(256)"));
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300190 }
191 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000192}
Damien George23005372014-01-13 19:39:01 +0000193MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
194
Damien Georged182b982014-08-30 14:19:41 +0100195STATIC mp_obj_t mp_builtin_dir(mp_uint_t n_args, const mp_obj_t *args) {
Damien George4acb2452014-02-02 22:07:44 +0000196 // TODO make this function more general and less of a hack
197
Damien George7efc5b32014-04-05 22:36:42 +0100198 mp_obj_dict_t *dict = NULL;
Damien George4acb2452014-02-02 22:07:44 +0000199 if (n_args == 0) {
200 // make a list of names in the local name space
Damien George7efc5b32014-04-05 22:36:42 +0100201 dict = mp_locals_get();
Damien George4acb2452014-02-02 22:07:44 +0000202 } else { // n_args == 1
203 // make a list of names in the given object
Damien George6022d9d2014-03-26 22:35:00 +0000204 if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
Damien George7efc5b32014-04-05 22:36:42 +0100205 dict = mp_obj_module_get_globals(args[0]);
Damien George6022d9d2014-03-26 22:35:00 +0000206 } else {
207 mp_obj_type_t *type;
208 if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
209 type = args[0];
210 } else {
211 type = mp_obj_get_type(args[0]);
212 }
Damien George3e1a5c12014-03-29 13:43:38 +0000213 if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
Damien George7efc5b32014-04-05 22:36:42 +0100214 dict = type->locals_dict;
Damien George6022d9d2014-03-26 22:35:00 +0000215 }
Damien Georgebadc9d42014-03-23 00:03:11 +0000216 }
Damien George4acb2452014-02-02 22:07:44 +0000217 }
218
219 mp_obj_t dir = mp_obj_new_list(0, NULL);
Damien George7efc5b32014-04-05 22:36:42 +0100220 if (dict != NULL) {
Damien Georged182b982014-08-30 14:19:41 +0100221 for (mp_uint_t i = 0; i < dict->map.alloc; i++) {
Damien George7efc5b32014-04-05 22:36:42 +0100222 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
223 mp_obj_list_append(dir, dict->map.table[i].key);
Damien Georgebadc9d42014-03-23 00:03:11 +0000224 }
225 }
226 }
Damien George9b196cd2014-03-26 21:47:19 +0000227
Damien George4acb2452014-02-02 22:07:44 +0000228 return dir;
229}
Damien George4acb2452014-02-02 22:07:44 +0000230MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir);
231
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200232STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
Damien George8594ce22014-09-13 18:43:09 +0100233 // TODO handle big int
Damiend99b0522013-12-21 18:17:45 +0000234 if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
Damien George40f3c022014-07-03 13:25:24 +0100235 mp_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
236 mp_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
Damien George8594ce22014-09-13 18:43:09 +0100237 if (i2 == 0) {
Damien George83695592014-09-13 19:58:18 +0100238 #if MICROPY_PY_BUILTINS_FLOAT
Damien George8594ce22014-09-13 18:43:09 +0100239 zero_division_error:
Damien George83695592014-09-13 19:58:18 +0100240 #endif
Damien George8594ce22014-09-13 18:43:09 +0100241 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
242 }
Damien George20006db2014-01-18 14:10:48 +0000243 mp_obj_t args[2];
Damien Georgee72be1b2014-10-22 23:05:50 +0100244 args[0] = MP_OBJ_NEW_SMALL_INT(mp_small_int_floor_divide(i1, i2));
245 args[1] = MP_OBJ_NEW_SMALL_INT(mp_small_int_modulo(i1, i2));
Damien George15d18062014-03-31 16:28:13 +0100246 return mp_obj_new_tuple(2, args);
Damien George83695592014-09-13 19:58:18 +0100247 #if MICROPY_PY_BUILTINS_FLOAT
Damien George8594ce22014-09-13 18:43:09 +0100248 } else if (MP_OBJ_IS_TYPE(o1_in, &mp_type_float) || MP_OBJ_IS_TYPE(o2_in, &mp_type_float)) {
249 mp_float_t f1 = mp_obj_get_float(o1_in);
250 mp_float_t f2 = mp_obj_get_float(o2_in);
251 if (f2 == 0.0) {
252 goto zero_division_error;
253 }
254 mp_obj_float_divmod(&f1, &f2);
255 mp_obj_t tuple[2] = {
256 mp_obj_new_float(f1),
257 mp_obj_new_float(f2),
258 };
259 return mp_obj_new_tuple(2, tuple);
Damien George83695592014-09-13 19:58:18 +0100260 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000261 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000262 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
263 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
264 "unsupported operand type(s) for divmod()"));
265 } else {
266 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
267 "unsupported operand type(s) for divmod(): '%s' and '%s'",
268 mp_obj_get_type_str(o1_in), mp_obj_get_type_str(o2_in)));
269 }
Damiena3dcd9e2013-12-17 21:35:38 +0000270 }
271}
Damien George23005372014-01-13 19:39:01 +0000272MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
273
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200274STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000275 // TODO hash will generally overflow small integer; can we safely truncate it?
Damiend99b0522013-12-21 18:17:45 +0000276 return mp_obj_new_int(mp_obj_hash(o_in));
Damiena3dcd9e2013-12-17 21:35:38 +0000277}
Damiend99b0522013-12-21 18:17:45 +0000278MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
279
Damien George58051112014-04-15 12:42:52 +0100280STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) {
Damien Georgeb013aea2014-04-15 12:50:21 +0100281 return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_x), o_in);
Damien George58051112014-04-15 12:42:52 +0100282}
Damien George58051112014-04-15 12:42:52 +0100283MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex);
284
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200285STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100286 return mp_getiter(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000287}
Damiend99b0522013-12-21 18:17:45 +0000288MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
Damiena3dcd9e2013-12-17 21:35:38 +0000289
Damien Georged182b982014-08-30 14:19:41 +0100290STATIC mp_obj_t mp_builtin_min_max(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs, mp_uint_t op) {
Damien George7310fd42014-08-24 19:14:09 +0100291 mp_map_elem_t *key_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP);
292 mp_obj_t key_fn = key_elem == NULL ? MP_OBJ_NULL : key_elem->value;
Damiena3dcd9e2013-12-17 21:35:38 +0000293 if (n_args == 1) {
294 // given an iterable
Damien Georged17926d2014-03-30 13:35:08 +0100295 mp_obj_t iterable = mp_getiter(args[0]);
Damien George7310fd42014-08-24 19:14:09 +0100296 mp_obj_t best_key = MP_OBJ_NULL;
297 mp_obj_t best_obj = MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000298 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100299 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George7310fd42014-08-24 19:14:09 +0100300 mp_obj_t key = key_fn == MP_OBJ_NULL ? item : mp_call_function_1(key_fn, item);
301 if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) {
302 best_key = key;
303 best_obj = item;
Damiena3dcd9e2013-12-17 21:35:38 +0000304 }
305 }
Damien George7310fd42014-08-24 19:14:09 +0100306 if (best_obj == MP_OBJ_NULL) {
307 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000308 }
Damien George7310fd42014-08-24 19:14:09 +0100309 return best_obj;
Damiena3dcd9e2013-12-17 21:35:38 +0000310 } else {
311 // given many args
Damien George7310fd42014-08-24 19:14:09 +0100312 mp_obj_t best_key = MP_OBJ_NULL;
313 mp_obj_t best_obj = MP_OBJ_NULL;
314 for (mp_uint_t i = 0; i < n_args; i++) {
315 mp_obj_t key = key_fn == MP_OBJ_NULL ? args[i] : mp_call_function_1(key_fn, args[i]);
316 if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) {
317 best_key = key;
318 best_obj = args[i];
Damiena3dcd9e2013-12-17 21:35:38 +0000319 }
320 }
Damien George7310fd42014-08-24 19:14:09 +0100321 return best_obj;
Damiena3dcd9e2013-12-17 21:35:38 +0000322 }
323}
324
Damien Georged182b982014-08-30 14:19:41 +0100325STATIC mp_obj_t mp_builtin_max(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien George7310fd42014-08-24 19:14:09 +0100326 return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_MORE);
Damiena3dcd9e2013-12-17 21:35:38 +0000327}
Damien George7310fd42014-08-24 19:14:09 +0100328MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_max_obj, 1, mp_builtin_max);
Damiena3dcd9e2013-12-17 21:35:38 +0000329
Damien Georged182b982014-08-30 14:19:41 +0100330STATIC mp_obj_t mp_builtin_min(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien George7310fd42014-08-24 19:14:09 +0100331 return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_LESS);
332}
333MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_min_obj, 1, mp_builtin_min);
Damien George23005372014-01-13 19:39:01 +0000334
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200335STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
Damien Georged17926d2014-03-30 13:35:08 +0100336 mp_obj_t ret = mp_iternext_allow_raise(o);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100337 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100338 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damiend9d62012013-12-21 18:38:03 +0000339 } else {
340 return ret;
341 }
Damiend99b0522013-12-21 18:17:45 +0000342}
Damiend99b0522013-12-21 18:17:45 +0000343MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
344
Damien George897fe0c2014-04-15 22:03:55 +0100345STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) {
346 return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in);
347}
Damien George897fe0c2014-04-15 22:03:55 +0100348MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
349
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200350STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
Damien Georged182b982014-08-30 14:19:41 +0100351 mp_uint_t len;
Damien George698ec212014-02-08 18:17:23 +0000352 const char *str = mp_obj_str_get_data(o_in, &len);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300353 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien George1e9a92f2014-11-06 17:36:16 +0000354 len = unichar_charlen(str, len);
355 if (len == 1) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000356 if (MP_OBJ_IS_STR(o_in) && UTF8_IS_NONASCII(*str)) {
Damien George40f3c022014-07-03 13:25:24 +0100357 mp_int_t ord = *str++ & 0x7F;
358 for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000359 ord &= ~mask;
360 }
361 while (UTF8_IS_CONT(*str)) {
362 ord = (ord << 6) | (*str++ & 0x3F);
363 }
364 return mp_obj_new_int(ord);
365 } else {
366 return mp_obj_new_int(((const byte*)str)[0]);
367 }
Damiena3dcd9e2013-12-17 21:35:38 +0000368 }
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300369 #else
370 if (len == 1) {
371 // don't sign extend when converting to ord
372 return mp_obj_new_int(((const byte*)str)[0]);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300373 }
374 #endif
Damien George1e9a92f2014-11-06 17:36:16 +0000375
376 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
377 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
378 "ord expects a character"));
379 } else {
380 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
381 "ord() expected a character, but string of length %d found", len));
382 }
Damiena3dcd9e2013-12-17 21:35:38 +0000383}
Damien George23005372014-01-13 19:39:01 +0000384MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
385
Damien Georged182b982014-08-30 14:19:41 +0100386STATIC mp_obj_t mp_builtin_pow(mp_uint_t n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000387 assert(2 <= n_args && n_args <= 3);
Damiena3dcd9e2013-12-17 21:35:38 +0000388 switch (n_args) {
Damien Georged17926d2014-03-30 13:35:08 +0100389 case 2: return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]);
390 default: return mp_binary_op(MP_BINARY_OP_MODULO, mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
Damiena3dcd9e2013-12-17 21:35:38 +0000391 }
392}
Damien George23005372014-01-13 19:39:01 +0000393MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
394
Damien Georged182b982014-08-30 14:19:41 +0100395STATIC mp_obj_t mp_builtin_print(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien George48815662014-04-02 10:34:44 +0100396 mp_map_elem_t *sep_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_MAP_LOOKUP);
397 mp_map_elem_t *end_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_end), MP_MAP_LOOKUP);
398 const char *sep_data = " ";
Damien Georged182b982014-08-30 14:19:41 +0100399 mp_uint_t sep_len = 1;
Damien George48815662014-04-02 10:34:44 +0100400 const char *end_data = "\n";
Damien Georged182b982014-08-30 14:19:41 +0100401 mp_uint_t end_len = 1;
Damien George48815662014-04-02 10:34:44 +0100402 if (sep_elem != NULL && sep_elem->value != mp_const_none) {
403 sep_data = mp_obj_str_get_data(sep_elem->value, &sep_len);
404 }
405 if (end_elem != NULL && end_elem->value != mp_const_none) {
406 end_data = mp_obj_str_get_data(end_elem->value, &end_len);
407 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300408 #if MICROPY_PY_IO
Damien Georgecd342072015-01-12 22:30:49 +0000409 extern mp_uint_t mp_sys_stdout_obj; // type is irrelevant, just need pointer
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300410 mp_obj_t stream_obj = &mp_sys_stdout_obj;
411 mp_map_elem_t *file_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_file), MP_MAP_LOOKUP);
412 if (file_elem != NULL && file_elem->value != mp_const_none) {
413 stream_obj = file_elem->value;
414 }
415
416 pfenv_t pfenv;
417 pfenv.data = stream_obj;
Damien George42f3de92014-10-03 17:44:14 +0000418 pfenv.print_strn = (void (*)(void *, const char *, mp_uint_t))mp_stream_write;
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300419 #endif
Damien George42f3de92014-10-03 17:44:14 +0000420 for (mp_uint_t i = 0; i < n_args; i++) {
Damiena3dcd9e2013-12-17 21:35:38 +0000421 if (i > 0) {
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300422 #if MICROPY_PY_IO
423 mp_stream_write(stream_obj, sep_data, sep_len);
424 #else
Damien George4abff752014-08-30 14:59:21 +0100425 printf("%.*s", (int)sep_len, sep_data);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300426 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000427 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300428 #if MICROPY_PY_IO
429 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))pfenv_printf, &pfenv, args[i], PRINT_STR);
430 #else
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200431 mp_obj_print(args[i], PRINT_STR);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300432 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000433 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300434 #if MICROPY_PY_IO
435 mp_stream_write(stream_obj, end_data, end_len);
436 #else
Damien George4abff752014-08-30 14:59:21 +0100437 printf("%.*s", (int)end_len, end_data);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300438 #endif
Damiend99b0522013-12-21 18:17:45 +0000439 return mp_const_none;
Damiena3dcd9e2013-12-17 21:35:38 +0000440}
Damien George48815662014-04-02 10:34:44 +0100441MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
Damien George23005372014-01-13 19:39:01 +0000442
Paul Sokolovsky1eca3282014-11-26 21:17:16 +0200443STATIC mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
444 if (o != mp_const_none) {
Damien George81e70a82015-01-28 23:53:13 +0000445 #if MICROPY_PY_IO
446 extern mp_uint_t mp_sys_stdout_obj; // type is irrelevant, just need pointer
447 pfenv_t pfenv;
448 pfenv.data = &mp_sys_stdout_obj;
449 pfenv.print_strn = (void (*)(void *, const char *, mp_uint_t))mp_stream_write;
450 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))pfenv_printf, &pfenv, o, PRINT_REPR);
451 mp_stream_write(&mp_sys_stdout_obj, "\n", 1);
452 #else
453 mp_obj_print(o, PRINT_REPR);
454 printf("\n");
455 #endif
Paul Sokolovsky1eca3282014-11-26 21:17:16 +0200456 }
457 return mp_const_none;
458}
459MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
460
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200461STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
Damien George0b9ee862015-01-21 19:14:25 +0000462 vstr_t vstr;
463 vstr_init(&vstr, 16);
464 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, &vstr, o_in, PRINT_REPR);
465 return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000466}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000467MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
468
Damien George1559a972014-10-31 11:28:50 +0000469STATIC mp_obj_t mp_builtin_round(mp_obj_t o_in) {
470 // TODO support second arg
471 if (MP_OBJ_IS_INT(o_in)) {
472 return o_in;
473 }
474#if MICROPY_PY_BUILTINS_FLOAT
475 mp_float_t val = mp_obj_get_float(o_in);
476 mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val);
477 mp_int_t r = rounded;
478 // make rounded value even if it was halfway between ints
479 if (val - rounded == 0.5) {
480 r = (r + 1) & (~1);
481 } else if (val - rounded == -0.5) {
482 r &= ~1;
483 }
484#else
485 mp_int_t r = mp_obj_get_int(o_in);
486#endif
487 return mp_obj_new_int(r);
488}
489MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_round_obj, mp_builtin_round);
490
Damien Georged182b982014-08-30 14:19:41 +0100491STATIC mp_obj_t mp_builtin_sum(mp_uint_t n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000492 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000493 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000494 switch (n_args) {
Damiend99b0522013-12-21 18:17:45 +0000495 case 1: value = mp_obj_new_int(0); break;
Damien George23005372014-01-13 19:39:01 +0000496 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000497 }
Damien Georged17926d2014-03-30 13:35:08 +0100498 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000499 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100500 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100501 value = mp_binary_op(MP_BINARY_OP_ADD, value, item);
Damiena3dcd9e2013-12-17 21:35:38 +0000502 }
503 return value;
504}
Damien George23005372014-01-13 19:39:01 +0000505MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000506
Damien Georged182b982014-08-30 14:19:41 +0100507STATIC mp_obj_t mp_builtin_sorted(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien George20006db2014-01-18 14:10:48 +0000508 assert(n_args >= 1);
509 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100510 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton5c768392014-01-13 05:12:50 +0000511 "must use keyword argument for key function"));
512 }
Damien George3e1a5c12014-03-29 13:43:38 +0000513 mp_obj_t self = mp_type_list.make_new((mp_obj_t)&mp_type_list, 1, 0, args);
Damien George20006db2014-01-18 14:10:48 +0000514 mp_obj_list_sort(1, &self, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000515
516 return self;
517}
John R. Lenton88cb1e62014-01-13 19:55:18 +0000518MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200519
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300520// See mp_load_attr() if making any changes
521STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) {
522 mp_obj_t dest[2];
523 // use load_method, raising or not raising exception
524 ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest);
525 if (dest[0] == MP_OBJ_NULL) {
526 return defval;
527 } else if (dest[1] == MP_OBJ_NULL) {
528 // load_method returned just a normal attribute
529 return dest[0];
530 } else {
531 // load_method returned a method, so build a bound method object
532 return mp_obj_new_bound_meth(dest[0], dest[1]);
533 }
534}
535
Damien Georged182b982014-08-30 14:19:41 +0100536STATIC mp_obj_t mp_builtin_getattr(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300537 mp_obj_t defval = MP_OBJ_NULL;
538 if (n_args > 2) {
539 defval = args[2];
540 }
stijnc1832fd2015-02-14 17:36:59 +0100541 return mp_load_attr_default(args[0], mp_obj_str_get_qstr(args[1]), defval);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200542}
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300543MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr);
Paul Sokolovskycc0af3d2014-04-06 01:00:46 +0300544
stijnc1832fd2015-02-14 17:36:59 +0100545STATIC mp_obj_t mp_builtin_setattr(mp_obj_t base, mp_obj_t attr, mp_obj_t value) {
546 mp_store_attr(base, mp_obj_str_get_qstr(attr), value);
547 return mp_const_none;
548}
549MP_DEFINE_CONST_FUN_OBJ_3(mp_builtin_setattr_obj, mp_builtin_setattr);
550
Paul Sokolovskyff306662014-05-11 19:05:42 +0300551STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
552 assert(MP_OBJ_IS_QSTR(attr_in));
553
554 mp_obj_t dest[2];
Chris Angelicodaf973a2014-06-06 03:51:03 +1000555 // TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
Paul Sokolovskyff306662014-05-11 19:05:42 +0300556 // explicitly says "This is implemented by calling getattr(object, name) and seeing
557 // whether it raises an AttributeError or not.", so we should explicitly wrap this
558 // in nlr_push and handle exception.
559 mp_load_method_maybe(object_in, MP_OBJ_QSTR_VALUE(attr_in), dest);
560
561 return MP_BOOL(dest[0] != MP_OBJ_NULL);
562}
Paul Sokolovskyff306662014-05-11 19:05:42 +0300563MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
564
Damien George4c03b3a2014-08-12 18:33:40 +0100565// These are defined in terms of MicroPython API functions right away
Damien Georgec7687ad2014-08-22 21:48:30 +0100566MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_obj_id);
Damien George4c03b3a2014-08-12 18:33:40 +0100567MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_obj_len);
Paul Sokolovsky080d99b2014-04-06 01:18:19 +0300568MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
569MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);
Damien George78d702c2014-12-09 16:19:48 +0000570
571STATIC const mp_map_elem_t mp_module_builtins_globals_table[] = {
572 // built-in core functions
573 { MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), (mp_obj_t)&mp_builtin___build_class___obj },
574 { MP_OBJ_NEW_QSTR(MP_QSTR___import__), (mp_obj_t)&mp_builtin___import___obj },
575 { MP_OBJ_NEW_QSTR(MP_QSTR___repl_print__), (mp_obj_t)&mp_builtin___repl_print___obj },
576
577 // built-in types
578 { MP_OBJ_NEW_QSTR(MP_QSTR_bool), (mp_obj_t)&mp_type_bool },
579 { MP_OBJ_NEW_QSTR(MP_QSTR_bytes), (mp_obj_t)&mp_type_bytes },
580#if MICROPY_PY_BUILTINS_BYTEARRAY
581 { MP_OBJ_NEW_QSTR(MP_QSTR_bytearray), (mp_obj_t)&mp_type_bytearray },
582#endif
583#if MICROPY_PY_BUILTINS_COMPLEX
584 { MP_OBJ_NEW_QSTR(MP_QSTR_complex), (mp_obj_t)&mp_type_complex },
585#endif
586 { MP_OBJ_NEW_QSTR(MP_QSTR_dict), (mp_obj_t)&mp_type_dict },
587 { MP_OBJ_NEW_QSTR(MP_QSTR_enumerate), (mp_obj_t)&mp_type_enumerate },
588 { MP_OBJ_NEW_QSTR(MP_QSTR_filter), (mp_obj_t)&mp_type_filter },
589#if MICROPY_PY_BUILTINS_FLOAT
590 { MP_OBJ_NEW_QSTR(MP_QSTR_float), (mp_obj_t)&mp_type_float },
591#endif
Damien Georgee37dcaa2014-12-27 17:07:16 +0000592#if MICROPY_PY_BUILTINS_SET && MICROPY_PY_BUILTINS_FROZENSET
Damien George78d702c2014-12-09 16:19:48 +0000593 { MP_OBJ_NEW_QSTR(MP_QSTR_frozenset), (mp_obj_t)&mp_type_frozenset },
594#endif
595 { MP_OBJ_NEW_QSTR(MP_QSTR_int), (mp_obj_t)&mp_type_int },
596 { MP_OBJ_NEW_QSTR(MP_QSTR_list), (mp_obj_t)&mp_type_list },
597 { MP_OBJ_NEW_QSTR(MP_QSTR_map), (mp_obj_t)&mp_type_map },
598#if MICROPY_PY_BUILTINS_MEMORYVIEW
599 { MP_OBJ_NEW_QSTR(MP_QSTR_memoryview), (mp_obj_t)&mp_type_memoryview },
600#endif
601 { MP_OBJ_NEW_QSTR(MP_QSTR_object), (mp_obj_t)&mp_type_object },
602#if MICROPY_PY_BUILTINS_PROPERTY
603 { MP_OBJ_NEW_QSTR(MP_QSTR_property), (mp_obj_t)&mp_type_property },
604#endif
605 { MP_OBJ_NEW_QSTR(MP_QSTR_range), (mp_obj_t)&mp_type_range },
606 { MP_OBJ_NEW_QSTR(MP_QSTR_reversed), (mp_obj_t)&mp_type_reversed },
607#if MICROPY_PY_BUILTINS_SET
608 { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&mp_type_set },
609#endif
610 { MP_OBJ_NEW_QSTR(MP_QSTR_str), (mp_obj_t)&mp_type_str },
611 { MP_OBJ_NEW_QSTR(MP_QSTR_super), (mp_obj_t)&mp_type_super },
612 { MP_OBJ_NEW_QSTR(MP_QSTR_tuple), (mp_obj_t)&mp_type_tuple },
613 { MP_OBJ_NEW_QSTR(MP_QSTR_type), (mp_obj_t)&mp_type_type },
614 { MP_OBJ_NEW_QSTR(MP_QSTR_zip), (mp_obj_t)&mp_type_zip },
615
616 { MP_OBJ_NEW_QSTR(MP_QSTR_classmethod), (mp_obj_t)&mp_type_classmethod },
617 { MP_OBJ_NEW_QSTR(MP_QSTR_staticmethod), (mp_obj_t)&mp_type_staticmethod },
618
619 // built-in objects
620 { MP_OBJ_NEW_QSTR(MP_QSTR_Ellipsis), (mp_obj_t)&mp_const_ellipsis_obj },
621
622 // built-in user functions
623 { MP_OBJ_NEW_QSTR(MP_QSTR_abs), (mp_obj_t)&mp_builtin_abs_obj },
624 { MP_OBJ_NEW_QSTR(MP_QSTR_all), (mp_obj_t)&mp_builtin_all_obj },
625 { MP_OBJ_NEW_QSTR(MP_QSTR_any), (mp_obj_t)&mp_builtin_any_obj },
626 { MP_OBJ_NEW_QSTR(MP_QSTR_bin), (mp_obj_t)&mp_builtin_bin_obj },
627 { MP_OBJ_NEW_QSTR(MP_QSTR_callable), (mp_obj_t)&mp_builtin_callable_obj },
628#if MICROPY_PY_BUILTINS_COMPILE
629 { MP_OBJ_NEW_QSTR(MP_QSTR_compile), (mp_obj_t)&mp_builtin_compile_obj },
630#endif
631 { MP_OBJ_NEW_QSTR(MP_QSTR_chr), (mp_obj_t)&mp_builtin_chr_obj },
632 { MP_OBJ_NEW_QSTR(MP_QSTR_dir), (mp_obj_t)&mp_builtin_dir_obj },
633 { MP_OBJ_NEW_QSTR(MP_QSTR_divmod), (mp_obj_t)&mp_builtin_divmod_obj },
634 { MP_OBJ_NEW_QSTR(MP_QSTR_eval), (mp_obj_t)&mp_builtin_eval_obj },
635 { MP_OBJ_NEW_QSTR(MP_QSTR_exec), (mp_obj_t)&mp_builtin_exec_obj },
Damien George2a3e2b92014-12-19 13:36:17 +0000636#if MICROPY_PY_BUILTINS_EXECFILE
637 { MP_OBJ_NEW_QSTR(MP_QSTR_execfile), (mp_obj_t)&mp_builtin_execfile_obj },
638#endif
Damien George78d702c2014-12-09 16:19:48 +0000639 { MP_OBJ_NEW_QSTR(MP_QSTR_getattr), (mp_obj_t)&mp_builtin_getattr_obj },
stijnc1832fd2015-02-14 17:36:59 +0100640 { MP_OBJ_NEW_QSTR(MP_QSTR_setattr), (mp_obj_t)&mp_builtin_setattr_obj },
Damien George78d702c2014-12-09 16:19:48 +0000641 { MP_OBJ_NEW_QSTR(MP_QSTR_globals), (mp_obj_t)&mp_builtin_globals_obj },
642 { MP_OBJ_NEW_QSTR(MP_QSTR_hasattr), (mp_obj_t)&mp_builtin_hasattr_obj },
643 { MP_OBJ_NEW_QSTR(MP_QSTR_hash), (mp_obj_t)&mp_builtin_hash_obj },
644 { MP_OBJ_NEW_QSTR(MP_QSTR_hex), (mp_obj_t)&mp_builtin_hex_obj },
645 { MP_OBJ_NEW_QSTR(MP_QSTR_id), (mp_obj_t)&mp_builtin_id_obj },
646 { MP_OBJ_NEW_QSTR(MP_QSTR_isinstance), (mp_obj_t)&mp_builtin_isinstance_obj },
647 { MP_OBJ_NEW_QSTR(MP_QSTR_issubclass), (mp_obj_t)&mp_builtin_issubclass_obj },
648 { MP_OBJ_NEW_QSTR(MP_QSTR_iter), (mp_obj_t)&mp_builtin_iter_obj },
649 { MP_OBJ_NEW_QSTR(MP_QSTR_len), (mp_obj_t)&mp_builtin_len_obj },
650 { MP_OBJ_NEW_QSTR(MP_QSTR_locals), (mp_obj_t)&mp_builtin_locals_obj },
651 { MP_OBJ_NEW_QSTR(MP_QSTR_max), (mp_obj_t)&mp_builtin_max_obj },
652 { MP_OBJ_NEW_QSTR(MP_QSTR_min), (mp_obj_t)&mp_builtin_min_obj },
653 { MP_OBJ_NEW_QSTR(MP_QSTR_next), (mp_obj_t)&mp_builtin_next_obj },
654 { MP_OBJ_NEW_QSTR(MP_QSTR_oct), (mp_obj_t)&mp_builtin_oct_obj },
655 { MP_OBJ_NEW_QSTR(MP_QSTR_ord), (mp_obj_t)&mp_builtin_ord_obj },
656 { MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_builtin_pow_obj },
657 { MP_OBJ_NEW_QSTR(MP_QSTR_print), (mp_obj_t)&mp_builtin_print_obj },
658 { MP_OBJ_NEW_QSTR(MP_QSTR_repr), (mp_obj_t)&mp_builtin_repr_obj },
659 { MP_OBJ_NEW_QSTR(MP_QSTR_round), (mp_obj_t)&mp_builtin_round_obj },
660 { MP_OBJ_NEW_QSTR(MP_QSTR_sorted), (mp_obj_t)&mp_builtin_sorted_obj },
661 { MP_OBJ_NEW_QSTR(MP_QSTR_sum), (mp_obj_t)&mp_builtin_sum_obj },
662
663 // built-in exceptions
664 { MP_OBJ_NEW_QSTR(MP_QSTR_BaseException), (mp_obj_t)&mp_type_BaseException },
665 { MP_OBJ_NEW_QSTR(MP_QSTR_ArithmeticError), (mp_obj_t)&mp_type_ArithmeticError },
666 { MP_OBJ_NEW_QSTR(MP_QSTR_AssertionError), (mp_obj_t)&mp_type_AssertionError },
667 { MP_OBJ_NEW_QSTR(MP_QSTR_AttributeError), (mp_obj_t)&mp_type_AttributeError },
668 { MP_OBJ_NEW_QSTR(MP_QSTR_EOFError), (mp_obj_t)&mp_type_EOFError },
669 { MP_OBJ_NEW_QSTR(MP_QSTR_Exception), (mp_obj_t)&mp_type_Exception },
670 { MP_OBJ_NEW_QSTR(MP_QSTR_GeneratorExit), (mp_obj_t)&mp_type_GeneratorExit },
671 { MP_OBJ_NEW_QSTR(MP_QSTR_ImportError), (mp_obj_t)&mp_type_ImportError },
672 { MP_OBJ_NEW_QSTR(MP_QSTR_IndentationError), (mp_obj_t)&mp_type_IndentationError },
673 { MP_OBJ_NEW_QSTR(MP_QSTR_IndexError), (mp_obj_t)&mp_type_IndexError },
674 { MP_OBJ_NEW_QSTR(MP_QSTR_KeyError), (mp_obj_t)&mp_type_KeyError },
675 { MP_OBJ_NEW_QSTR(MP_QSTR_LookupError), (mp_obj_t)&mp_type_LookupError },
676 { MP_OBJ_NEW_QSTR(MP_QSTR_MemoryError), (mp_obj_t)&mp_type_MemoryError },
677 { MP_OBJ_NEW_QSTR(MP_QSTR_NameError), (mp_obj_t)&mp_type_NameError },
678 { MP_OBJ_NEW_QSTR(MP_QSTR_NotImplementedError), (mp_obj_t)&mp_type_NotImplementedError },
679 { MP_OBJ_NEW_QSTR(MP_QSTR_OSError), (mp_obj_t)&mp_type_OSError },
680 { MP_OBJ_NEW_QSTR(MP_QSTR_OverflowError), (mp_obj_t)&mp_type_OverflowError },
681 { MP_OBJ_NEW_QSTR(MP_QSTR_RuntimeError), (mp_obj_t)&mp_type_RuntimeError },
682 { MP_OBJ_NEW_QSTR(MP_QSTR_StopIteration), (mp_obj_t)&mp_type_StopIteration },
683 { MP_OBJ_NEW_QSTR(MP_QSTR_SyntaxError), (mp_obj_t)&mp_type_SyntaxError },
684 { MP_OBJ_NEW_QSTR(MP_QSTR_SystemExit), (mp_obj_t)&mp_type_SystemExit },
685 { MP_OBJ_NEW_QSTR(MP_QSTR_TypeError), (mp_obj_t)&mp_type_TypeError },
686 { MP_OBJ_NEW_QSTR(MP_QSTR_ValueError), (mp_obj_t)&mp_type_ValueError },
687 { MP_OBJ_NEW_QSTR(MP_QSTR_ZeroDivisionError), (mp_obj_t)&mp_type_ZeroDivisionError },
688 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
689 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
690
691 // Extra builtins as defined by a port
692 MICROPY_PORT_BUILTINS
693};
694
695MP_DEFINE_CONST_DICT(mp_module_builtins_globals, mp_module_builtins_globals_table);
696
697const mp_obj_module_t mp_module_builtins = {
698 .base = { &mp_type_module },
699 .name = MP_QSTR_builtins,
700 .globals = (mp_obj_dict_t*)&mp_module_builtins_globals,
701};