blob: 3ab522c3f4e381e6145b50a4cc01d92ca3ef3e95 [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"
Damien George6837d462015-03-14 22:07:30 +000032#include "py/objint.h"
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/objstr.h"
34#include "py/runtime0.h"
35#include "py/runtime.h"
36#include "py/builtin.h"
37#include "py/stream.h"
38#include "py/pfenv.h"
Damien660365e2013-12-17 18:27:24 +000039
Damien Georgefb510b32014-06-01 13:32:54 +010040#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000041#include <math.h>
42#endif
43
Damien George2e2e4042015-03-19 00:21:29 +000044#if MICROPY_PY_IO
45extern mp_uint_t mp_sys_stdout_obj; // type is irrelevant, just need pointer
46#endif
47
Damien Georgeb97669a2014-01-08 11:47:55 +000048// args[0] is function from class body
49// args[1] is class name
50// args[2:] are base objects
Damien Georged182b982014-08-30 14:19:41 +010051STATIC mp_obj_t mp_builtin___build_class__(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgeb97669a2014-01-08 11:47:55 +000052 assert(2 <= n_args);
53
Damien George7efc5b32014-04-05 22:36:42 +010054 // set the new classes __locals__ object
55 mp_obj_dict_t *old_locals = mp_locals_get();
Damien George062478e2014-01-09 20:57:50 +000056 mp_obj_t class_locals = mp_obj_new_dict(0);
Damien George7efc5b32014-04-05 22:36:42 +010057 mp_locals_set(class_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000058
59 // call the class code
Damien George882b3632014-04-02 15:56:31 +010060 mp_obj_t cell = mp_call_function_0(args[0]);
Damiena3dcd9e2013-12-17 21:35:38 +000061
62 // restore old __locals__ object
Damien Georged17926d2014-03-30 13:35:08 +010063 mp_locals_set(old_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000064
Damien Georgeb97669a2014-01-08 11:47:55 +000065 // get the class type (meta object) from the base objects
66 mp_obj_t meta;
67 if (n_args == 2) {
68 // no explicit bases, so use 'type'
Damien Georgec5966122014-02-15 16:10:44 +000069 meta = (mp_obj_t)&mp_type_type;
Damien Georgeb97669a2014-01-08 11:47:55 +000070 } else {
71 // use type of first base object
72 meta = mp_obj_get_type(args[2]);
73 }
Damien Georgeb97669a2014-01-08 11:47:55 +000074
75 // TODO do proper metaclass resolution for multiple base objects
76
Damien Georgeb97669a2014-01-08 11:47:55 +000077 // create the new class using a call to the meta object
Damien Georgeb97669a2014-01-08 11:47:55 +000078 mp_obj_t meta_args[3];
Damien George20006db2014-01-18 14:10:48 +000079 meta_args[0] = args[1]; // class name
Damien Georgeb97669a2014-01-08 11:47:55 +000080 meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases
Damien George20006db2014-01-18 14:10:48 +000081 meta_args[2] = class_locals; // dict of members
Damien Georged17926d2014-03-30 13:35:08 +010082 mp_obj_t new_class = mp_call_function_n_kw(meta, 3, 0, meta_args);
Damien Georgeb97669a2014-01-08 11:47:55 +000083
84 // store into cell if neede
85 if (cell != mp_const_none) {
86 mp_obj_cell_set(cell, new_class);
87 }
88
89 return new_class;
Damiena3dcd9e2013-12-17 21:35:38 +000090}
Damien Georgeb97669a2014-01-08 11:47:55 +000091MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
92
Damien George69c5fe12014-08-12 18:13:44 +010093STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
Damien George6837d462015-03-14 22:07:30 +000094 if (0) {
95 // dummy
Damien Georgefb510b32014-06-01 13:32:54 +010096#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000097 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +000098 mp_float_t value = mp_obj_float_get(o_in);
Damien660365e2013-12-17 18:27:24 +000099 // TODO check for NaN etc
Damiend99b0522013-12-21 18:17:45 +0000100 if (value < 0) {
101 return mp_obj_new_float(-value);
Damien660365e2013-12-17 18:27:24 +0000102 } else {
103 return o_in;
104 }
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300105#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0c36da02014-03-08 15:24:39 +0000106 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000107 mp_float_t real, imag;
108 mp_obj_complex_get(o_in, &real, &imag);
Damien George0c36da02014-03-08 15:24:39 +0000109 return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag));
Damien660365e2013-12-17 18:27:24 +0000110#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300111#endif
Damien660365e2013-12-17 18:27:24 +0000112 } else {
Damien George6837d462015-03-14 22:07:30 +0000113 // this will raise a TypeError if the argument is not integral
114 return mp_obj_int_abs(o_in);
Damien660365e2013-12-17 18:27:24 +0000115 }
116}
Damien George23005372014-01-13 19:39:01 +0000117MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
118
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200119STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100120 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000121 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100122 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100123 if (!mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000124 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000125 }
126 }
Damiend99b0522013-12-21 18:17:45 +0000127 return mp_const_true;
Damien660365e2013-12-17 18:27:24 +0000128}
Damien George23005372014-01-13 19:39:01 +0000129MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
130
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200131STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100132 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000133 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100134 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100135 if (mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000136 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000137 }
138 }
Damiend99b0522013-12-21 18:17:45 +0000139 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000140}
Damien George23005372014-01-13 19:39:01 +0000141MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
142
Damien George897fe0c2014-04-15 22:03:55 +0100143STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) {
144 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 +0200145 return mp_obj_str_format(MP_ARRAY_SIZE(args), args, NULL);
Damien George897fe0c2014-04-15 22:03:55 +0100146}
Damien George897fe0c2014-04-15 22:03:55 +0100147MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin);
148
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200149STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000150 if (mp_obj_is_callable(o_in)) {
151 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000152 } else {
Damiend99b0522013-12-21 18:17:45 +0000153 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000154 }
155}
Damien George23005372014-01-13 19:39:01 +0000156MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
157
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200158STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300159 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georgec9aa58e2014-07-31 13:41:43 +0000160 mp_uint_t c = mp_obj_get_int(o_in);
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000161 char str[4];
162 int len = 0;
163 if (c < 0x80) {
164 *str = c; len = 1;
165 } else if (c < 0x800) {
166 str[0] = (c >> 6) | 0xC0;
167 str[1] = (c & 0x3F) | 0x80;
168 len = 2;
169 } else if (c < 0x10000) {
170 str[0] = (c >> 12) | 0xE0;
171 str[1] = ((c >> 6) & 0x3F) | 0x80;
172 str[2] = (c & 0x3F) | 0x80;
173 len = 3;
174 } else if (c < 0x110000) {
175 str[0] = (c >> 18) | 0xF0;
176 str[1] = ((c >> 12) & 0x3F) | 0x80;
177 str[2] = ((c >> 6) & 0x3F) | 0x80;
178 str[3] = (c & 0x3F) | 0x80;
179 len = 4;
Damiena3dcd9e2013-12-17 21:35:38 +0000180 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100181 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
Damiena3dcd9e2013-12-17 21:35:38 +0000182 }
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000183 return mp_obj_new_str(str, len, true);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300184 #else
Damien George40f3c022014-07-03 13:25:24 +0100185 mp_int_t ord = mp_obj_get_int(o_in);
Damien George16677ce2015-01-28 14:07:11 +0000186 if (0 <= ord && ord <= 0xff) {
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300187 char str[1] = {ord};
188 return mp_obj_new_str(str, 1, true);
189 } else {
Damien George16677ce2015-01-28 14:07:11 +0000190 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(256)"));
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300191 }
192 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000193}
Damien George23005372014-01-13 19:39:01 +0000194MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
195
Damien Georged182b982014-08-30 14:19:41 +0100196STATIC mp_obj_t mp_builtin_dir(mp_uint_t n_args, const mp_obj_t *args) {
Damien George4acb2452014-02-02 22:07:44 +0000197 // TODO make this function more general and less of a hack
198
Damien George7efc5b32014-04-05 22:36:42 +0100199 mp_obj_dict_t *dict = NULL;
Damien George4acb2452014-02-02 22:07:44 +0000200 if (n_args == 0) {
201 // make a list of names in the local name space
Damien George7efc5b32014-04-05 22:36:42 +0100202 dict = mp_locals_get();
Damien George4acb2452014-02-02 22:07:44 +0000203 } else { // n_args == 1
204 // make a list of names in the given object
Damien George6022d9d2014-03-26 22:35:00 +0000205 if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
Damien George7efc5b32014-04-05 22:36:42 +0100206 dict = mp_obj_module_get_globals(args[0]);
Damien George6022d9d2014-03-26 22:35:00 +0000207 } else {
208 mp_obj_type_t *type;
209 if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
210 type = args[0];
211 } else {
212 type = mp_obj_get_type(args[0]);
213 }
Damien George3e1a5c12014-03-29 13:43:38 +0000214 if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
Damien George7efc5b32014-04-05 22:36:42 +0100215 dict = type->locals_dict;
Damien George6022d9d2014-03-26 22:35:00 +0000216 }
Damien Georgebadc9d42014-03-23 00:03:11 +0000217 }
Damien George4acb2452014-02-02 22:07:44 +0000218 }
219
220 mp_obj_t dir = mp_obj_new_list(0, NULL);
Damien George7efc5b32014-04-05 22:36:42 +0100221 if (dict != NULL) {
Damien Georged182b982014-08-30 14:19:41 +0100222 for (mp_uint_t i = 0; i < dict->map.alloc; i++) {
Damien George7efc5b32014-04-05 22:36:42 +0100223 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
224 mp_obj_list_append(dir, dict->map.table[i].key);
Damien Georgebadc9d42014-03-23 00:03:11 +0000225 }
226 }
227 }
Damien George9b196cd2014-03-26 21:47:19 +0000228
Damien George4acb2452014-02-02 22:07:44 +0000229 return dir;
230}
Damien George4acb2452014-02-02 22:07:44 +0000231MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir);
232
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200233STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
Damien George8594ce22014-09-13 18:43:09 +0100234 // TODO handle big int
Damiend99b0522013-12-21 18:17:45 +0000235 if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
Damien George40f3c022014-07-03 13:25:24 +0100236 mp_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
237 mp_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
Damien George8594ce22014-09-13 18:43:09 +0100238 if (i2 == 0) {
Damien George83695592014-09-13 19:58:18 +0100239 #if MICROPY_PY_BUILTINS_FLOAT
Damien George8594ce22014-09-13 18:43:09 +0100240 zero_division_error:
Damien George83695592014-09-13 19:58:18 +0100241 #endif
Damien George8594ce22014-09-13 18:43:09 +0100242 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
243 }
Damien George20006db2014-01-18 14:10:48 +0000244 mp_obj_t args[2];
Damien Georgee72be1b2014-10-22 23:05:50 +0100245 args[0] = MP_OBJ_NEW_SMALL_INT(mp_small_int_floor_divide(i1, i2));
246 args[1] = MP_OBJ_NEW_SMALL_INT(mp_small_int_modulo(i1, i2));
Damien George15d18062014-03-31 16:28:13 +0100247 return mp_obj_new_tuple(2, args);
Damien George83695592014-09-13 19:58:18 +0100248 #if MICROPY_PY_BUILTINS_FLOAT
Damien George8594ce22014-09-13 18:43:09 +0100249 } else if (MP_OBJ_IS_TYPE(o1_in, &mp_type_float) || MP_OBJ_IS_TYPE(o2_in, &mp_type_float)) {
250 mp_float_t f1 = mp_obj_get_float(o1_in);
251 mp_float_t f2 = mp_obj_get_float(o2_in);
252 if (f2 == 0.0) {
253 goto zero_division_error;
254 }
255 mp_obj_float_divmod(&f1, &f2);
256 mp_obj_t tuple[2] = {
257 mp_obj_new_float(f1),
258 mp_obj_new_float(f2),
259 };
260 return mp_obj_new_tuple(2, tuple);
Damien George83695592014-09-13 19:58:18 +0100261 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000262 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000263 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
264 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
265 "unsupported operand type(s) for divmod()"));
266 } else {
267 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
268 "unsupported operand type(s) for divmod(): '%s' and '%s'",
269 mp_obj_get_type_str(o1_in), mp_obj_get_type_str(o2_in)));
270 }
Damiena3dcd9e2013-12-17 21:35:38 +0000271 }
272}
Damien George23005372014-01-13 19:39:01 +0000273MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
274
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200275STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000276 // TODO hash will generally overflow small integer; can we safely truncate it?
Damiend99b0522013-12-21 18:17:45 +0000277 return mp_obj_new_int(mp_obj_hash(o_in));
Damiena3dcd9e2013-12-17 21:35:38 +0000278}
Damiend99b0522013-12-21 18:17:45 +0000279MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
280
Damien George58051112014-04-15 12:42:52 +0100281STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) {
Damien Georgeb013aea2014-04-15 12:50:21 +0100282 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 +0100283}
Damien George58051112014-04-15 12:42:52 +0100284MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex);
285
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200286STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100287 return mp_getiter(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000288}
Damiend99b0522013-12-21 18:17:45 +0000289MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
Damiena3dcd9e2013-12-17 21:35:38 +0000290
Damien Georged182b982014-08-30 14:19:41 +0100291STATIC 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 +0100292 mp_map_elem_t *key_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP);
293 mp_obj_t key_fn = key_elem == NULL ? MP_OBJ_NULL : key_elem->value;
Damiena3dcd9e2013-12-17 21:35:38 +0000294 if (n_args == 1) {
295 // given an iterable
Damien Georged17926d2014-03-30 13:35:08 +0100296 mp_obj_t iterable = mp_getiter(args[0]);
Damien George7310fd42014-08-24 19:14:09 +0100297 mp_obj_t best_key = MP_OBJ_NULL;
298 mp_obj_t best_obj = MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000299 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100300 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George7310fd42014-08-24 19:14:09 +0100301 mp_obj_t key = key_fn == MP_OBJ_NULL ? item : mp_call_function_1(key_fn, item);
302 if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) {
303 best_key = key;
304 best_obj = item;
Damiena3dcd9e2013-12-17 21:35:38 +0000305 }
306 }
Damien George7310fd42014-08-24 19:14:09 +0100307 if (best_obj == MP_OBJ_NULL) {
308 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000309 }
Damien George7310fd42014-08-24 19:14:09 +0100310 return best_obj;
Damiena3dcd9e2013-12-17 21:35:38 +0000311 } else {
312 // given many args
Damien George7310fd42014-08-24 19:14:09 +0100313 mp_obj_t best_key = MP_OBJ_NULL;
314 mp_obj_t best_obj = MP_OBJ_NULL;
315 for (mp_uint_t i = 0; i < n_args; i++) {
316 mp_obj_t key = key_fn == MP_OBJ_NULL ? args[i] : mp_call_function_1(key_fn, args[i]);
317 if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) {
318 best_key = key;
319 best_obj = args[i];
Damiena3dcd9e2013-12-17 21:35:38 +0000320 }
321 }
Damien George7310fd42014-08-24 19:14:09 +0100322 return best_obj;
Damiena3dcd9e2013-12-17 21:35:38 +0000323 }
324}
325
Damien Georged182b982014-08-30 14:19:41 +0100326STATIC 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 +0100327 return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_MORE);
Damiena3dcd9e2013-12-17 21:35:38 +0000328}
Damien George7310fd42014-08-24 19:14:09 +0100329MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_max_obj, 1, mp_builtin_max);
Damiena3dcd9e2013-12-17 21:35:38 +0000330
Damien Georged182b982014-08-30 14:19:41 +0100331STATIC 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 +0100332 return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_LESS);
333}
334MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_min_obj, 1, mp_builtin_min);
Damien George23005372014-01-13 19:39:01 +0000335
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200336STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
Damien Georged17926d2014-03-30 13:35:08 +0100337 mp_obj_t ret = mp_iternext_allow_raise(o);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100338 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100339 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damiend9d62012013-12-21 18:38:03 +0000340 } else {
341 return ret;
342 }
Damiend99b0522013-12-21 18:17:45 +0000343}
Damiend99b0522013-12-21 18:17:45 +0000344MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
345
Damien George897fe0c2014-04-15 22:03:55 +0100346STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) {
347 return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in);
348}
Damien George897fe0c2014-04-15 22:03:55 +0100349MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
350
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200351STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
Damien Georged182b982014-08-30 14:19:41 +0100352 mp_uint_t len;
Damien George698ec212014-02-08 18:17:23 +0000353 const char *str = mp_obj_str_get_data(o_in, &len);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300354 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien George1e9a92f2014-11-06 17:36:16 +0000355 len = unichar_charlen(str, len);
356 if (len == 1) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000357 if (MP_OBJ_IS_STR(o_in) && UTF8_IS_NONASCII(*str)) {
Damien George40f3c022014-07-03 13:25:24 +0100358 mp_int_t ord = *str++ & 0x7F;
359 for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000360 ord &= ~mask;
361 }
362 while (UTF8_IS_CONT(*str)) {
363 ord = (ord << 6) | (*str++ & 0x3F);
364 }
365 return mp_obj_new_int(ord);
366 } else {
367 return mp_obj_new_int(((const byte*)str)[0]);
368 }
Damiena3dcd9e2013-12-17 21:35:38 +0000369 }
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300370 #else
371 if (len == 1) {
372 // don't sign extend when converting to ord
373 return mp_obj_new_int(((const byte*)str)[0]);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300374 }
375 #endif
Damien George1e9a92f2014-11-06 17:36:16 +0000376
377 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
378 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
379 "ord expects a character"));
380 } else {
381 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
382 "ord() expected a character, but string of length %d found", len));
383 }
Damiena3dcd9e2013-12-17 21:35:38 +0000384}
Damien George23005372014-01-13 19:39:01 +0000385MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
386
Damien Georged182b982014-08-30 14:19:41 +0100387STATIC mp_obj_t mp_builtin_pow(mp_uint_t n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000388 assert(2 <= n_args && n_args <= 3);
Damiena3dcd9e2013-12-17 21:35:38 +0000389 switch (n_args) {
Damien Georged17926d2014-03-30 13:35:08 +0100390 case 2: return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]);
391 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 +0000392 }
393}
Damien George23005372014-01-13 19:39:01 +0000394MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
395
Damien Georged182b982014-08-30 14:19:41 +0100396STATIC 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 +0100397 mp_map_elem_t *sep_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_MAP_LOOKUP);
398 mp_map_elem_t *end_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_end), MP_MAP_LOOKUP);
399 const char *sep_data = " ";
Damien Georged182b982014-08-30 14:19:41 +0100400 mp_uint_t sep_len = 1;
Damien George48815662014-04-02 10:34:44 +0100401 const char *end_data = "\n";
Damien Georged182b982014-08-30 14:19:41 +0100402 mp_uint_t end_len = 1;
Damien George48815662014-04-02 10:34:44 +0100403 if (sep_elem != NULL && sep_elem->value != mp_const_none) {
404 sep_data = mp_obj_str_get_data(sep_elem->value, &sep_len);
405 }
406 if (end_elem != NULL && end_elem->value != mp_const_none) {
407 end_data = mp_obj_str_get_data(end_elem->value, &end_len);
408 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300409 #if MICROPY_PY_IO
410 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
Damien George81e70a82015-01-28 23:53:13 +0000446 pfenv_t pfenv;
447 pfenv.data = &mp_sys_stdout_obj;
448 pfenv.print_strn = (void (*)(void *, const char *, mp_uint_t))mp_stream_write;
449 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))pfenv_printf, &pfenv, o, PRINT_REPR);
450 mp_stream_write(&mp_sys_stdout_obj, "\n", 1);
451 #else
452 mp_obj_print(o, PRINT_REPR);
453 printf("\n");
454 #endif
Paul Sokolovsky1eca3282014-11-26 21:17:16 +0200455 }
456 return mp_const_none;
457}
458MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
459
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200460STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
Damien George0b9ee862015-01-21 19:14:25 +0000461 vstr_t vstr;
462 vstr_init(&vstr, 16);
463 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, &vstr, o_in, PRINT_REPR);
464 return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000465}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000466MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
467
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300468STATIC mp_obj_t mp_builtin_round(mp_uint_t n_args, const mp_obj_t *args) {
469 // TODO really support second arg
470 mp_obj_t o_in = args[0];
Damien George1559a972014-10-31 11:28:50 +0000471 if (MP_OBJ_IS_INT(o_in)) {
472 return o_in;
473 }
474#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300475 mp_int_t num_dig = 0;
476 if (n_args > 1) {
477 num_dig = mp_obj_get_int(args[1]);
478 if (num_dig > 0) {
479 mp_not_implemented("round(..., N>0)");
480 }
481 }
Damien George1559a972014-10-31 11:28:50 +0000482 mp_float_t val = mp_obj_get_float(o_in);
483 mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val);
484 mp_int_t r = rounded;
485 // make rounded value even if it was halfway between ints
486 if (val - rounded == 0.5) {
487 r = (r + 1) & (~1);
488 } else if (val - rounded == -0.5) {
489 r &= ~1;
490 }
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300491 if (n_args > 1) {
492 return mp_obj_new_float(r);
493 }
Damien George1559a972014-10-31 11:28:50 +0000494#else
495 mp_int_t r = mp_obj_get_int(o_in);
496#endif
497 return mp_obj_new_int(r);
498}
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300499MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_round_obj, 1, 2, mp_builtin_round);
Damien George1559a972014-10-31 11:28:50 +0000500
Damien Georged182b982014-08-30 14:19:41 +0100501STATIC mp_obj_t mp_builtin_sum(mp_uint_t n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000502 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000503 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000504 switch (n_args) {
Damien Georgedb1e10d2015-03-02 17:19:44 +0000505 case 1: value = MP_OBJ_NEW_SMALL_INT(0); break;
Damien George23005372014-01-13 19:39:01 +0000506 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000507 }
Damien Georged17926d2014-03-30 13:35:08 +0100508 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000509 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100510 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100511 value = mp_binary_op(MP_BINARY_OP_ADD, value, item);
Damiena3dcd9e2013-12-17 21:35:38 +0000512 }
513 return value;
514}
Damien George23005372014-01-13 19:39:01 +0000515MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000516
Damien Georged182b982014-08-30 14:19:41 +0100517STATIC 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 +0000518 assert(n_args >= 1);
519 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100520 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton5c768392014-01-13 05:12:50 +0000521 "must use keyword argument for key function"));
522 }
Damien George3e1a5c12014-03-29 13:43:38 +0000523 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 +0000524 mp_obj_list_sort(1, &self, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000525
526 return self;
527}
John R. Lenton88cb1e62014-01-13 19:55:18 +0000528MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200529
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300530// See mp_load_attr() if making any changes
531STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) {
532 mp_obj_t dest[2];
533 // use load_method, raising or not raising exception
534 ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest);
535 if (dest[0] == MP_OBJ_NULL) {
536 return defval;
537 } else if (dest[1] == MP_OBJ_NULL) {
538 // load_method returned just a normal attribute
539 return dest[0];
540 } else {
541 // load_method returned a method, so build a bound method object
542 return mp_obj_new_bound_meth(dest[0], dest[1]);
543 }
544}
545
Damien Georged182b982014-08-30 14:19:41 +0100546STATIC mp_obj_t mp_builtin_getattr(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300547 mp_obj_t defval = MP_OBJ_NULL;
548 if (n_args > 2) {
549 defval = args[2];
550 }
stijnc1832fd2015-02-14 17:36:59 +0100551 return mp_load_attr_default(args[0], mp_obj_str_get_qstr(args[1]), defval);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200552}
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300553MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr);
Paul Sokolovskycc0af3d2014-04-06 01:00:46 +0300554
stijnc1832fd2015-02-14 17:36:59 +0100555STATIC mp_obj_t mp_builtin_setattr(mp_obj_t base, mp_obj_t attr, mp_obj_t value) {
556 mp_store_attr(base, mp_obj_str_get_qstr(attr), value);
557 return mp_const_none;
558}
559MP_DEFINE_CONST_FUN_OBJ_3(mp_builtin_setattr_obj, mp_builtin_setattr);
560
Paul Sokolovskyff306662014-05-11 19:05:42 +0300561STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
562 assert(MP_OBJ_IS_QSTR(attr_in));
563
564 mp_obj_t dest[2];
Chris Angelicodaf973a2014-06-06 03:51:03 +1000565 // TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
Paul Sokolovskyff306662014-05-11 19:05:42 +0300566 // explicitly says "This is implemented by calling getattr(object, name) and seeing
567 // whether it raises an AttributeError or not.", so we should explicitly wrap this
568 // in nlr_push and handle exception.
569 mp_load_method_maybe(object_in, MP_OBJ_QSTR_VALUE(attr_in), dest);
570
571 return MP_BOOL(dest[0] != MP_OBJ_NULL);
572}
Paul Sokolovskyff306662014-05-11 19:05:42 +0300573MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
574
Damien George4c03b3a2014-08-12 18:33:40 +0100575// These are defined in terms of MicroPython API functions right away
Damien Georgec7687ad2014-08-22 21:48:30 +0100576MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_obj_id);
Damien George4c03b3a2014-08-12 18:33:40 +0100577MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_obj_len);
Paul Sokolovsky080d99b2014-04-06 01:18:19 +0300578MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
579MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);
Damien George78d702c2014-12-09 16:19:48 +0000580
581STATIC const mp_map_elem_t mp_module_builtins_globals_table[] = {
582 // built-in core functions
583 { MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), (mp_obj_t)&mp_builtin___build_class___obj },
584 { MP_OBJ_NEW_QSTR(MP_QSTR___import__), (mp_obj_t)&mp_builtin___import___obj },
585 { MP_OBJ_NEW_QSTR(MP_QSTR___repl_print__), (mp_obj_t)&mp_builtin___repl_print___obj },
586
587 // built-in types
588 { MP_OBJ_NEW_QSTR(MP_QSTR_bool), (mp_obj_t)&mp_type_bool },
589 { MP_OBJ_NEW_QSTR(MP_QSTR_bytes), (mp_obj_t)&mp_type_bytes },
590#if MICROPY_PY_BUILTINS_BYTEARRAY
591 { MP_OBJ_NEW_QSTR(MP_QSTR_bytearray), (mp_obj_t)&mp_type_bytearray },
592#endif
593#if MICROPY_PY_BUILTINS_COMPLEX
594 { MP_OBJ_NEW_QSTR(MP_QSTR_complex), (mp_obj_t)&mp_type_complex },
595#endif
596 { MP_OBJ_NEW_QSTR(MP_QSTR_dict), (mp_obj_t)&mp_type_dict },
597 { MP_OBJ_NEW_QSTR(MP_QSTR_enumerate), (mp_obj_t)&mp_type_enumerate },
598 { MP_OBJ_NEW_QSTR(MP_QSTR_filter), (mp_obj_t)&mp_type_filter },
599#if MICROPY_PY_BUILTINS_FLOAT
600 { MP_OBJ_NEW_QSTR(MP_QSTR_float), (mp_obj_t)&mp_type_float },
601#endif
Damien Georgee37dcaa2014-12-27 17:07:16 +0000602#if MICROPY_PY_BUILTINS_SET && MICROPY_PY_BUILTINS_FROZENSET
Damien George78d702c2014-12-09 16:19:48 +0000603 { MP_OBJ_NEW_QSTR(MP_QSTR_frozenset), (mp_obj_t)&mp_type_frozenset },
604#endif
605 { MP_OBJ_NEW_QSTR(MP_QSTR_int), (mp_obj_t)&mp_type_int },
606 { MP_OBJ_NEW_QSTR(MP_QSTR_list), (mp_obj_t)&mp_type_list },
607 { MP_OBJ_NEW_QSTR(MP_QSTR_map), (mp_obj_t)&mp_type_map },
608#if MICROPY_PY_BUILTINS_MEMORYVIEW
609 { MP_OBJ_NEW_QSTR(MP_QSTR_memoryview), (mp_obj_t)&mp_type_memoryview },
610#endif
611 { MP_OBJ_NEW_QSTR(MP_QSTR_object), (mp_obj_t)&mp_type_object },
612#if MICROPY_PY_BUILTINS_PROPERTY
613 { MP_OBJ_NEW_QSTR(MP_QSTR_property), (mp_obj_t)&mp_type_property },
614#endif
615 { MP_OBJ_NEW_QSTR(MP_QSTR_range), (mp_obj_t)&mp_type_range },
616 { MP_OBJ_NEW_QSTR(MP_QSTR_reversed), (mp_obj_t)&mp_type_reversed },
617#if MICROPY_PY_BUILTINS_SET
618 { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&mp_type_set },
619#endif
620 { MP_OBJ_NEW_QSTR(MP_QSTR_str), (mp_obj_t)&mp_type_str },
621 { MP_OBJ_NEW_QSTR(MP_QSTR_super), (mp_obj_t)&mp_type_super },
622 { MP_OBJ_NEW_QSTR(MP_QSTR_tuple), (mp_obj_t)&mp_type_tuple },
623 { MP_OBJ_NEW_QSTR(MP_QSTR_type), (mp_obj_t)&mp_type_type },
624 { MP_OBJ_NEW_QSTR(MP_QSTR_zip), (mp_obj_t)&mp_type_zip },
625
626 { MP_OBJ_NEW_QSTR(MP_QSTR_classmethod), (mp_obj_t)&mp_type_classmethod },
627 { MP_OBJ_NEW_QSTR(MP_QSTR_staticmethod), (mp_obj_t)&mp_type_staticmethod },
628
629 // built-in objects
630 { MP_OBJ_NEW_QSTR(MP_QSTR_Ellipsis), (mp_obj_t)&mp_const_ellipsis_obj },
631
632 // built-in user functions
633 { MP_OBJ_NEW_QSTR(MP_QSTR_abs), (mp_obj_t)&mp_builtin_abs_obj },
634 { MP_OBJ_NEW_QSTR(MP_QSTR_all), (mp_obj_t)&mp_builtin_all_obj },
635 { MP_OBJ_NEW_QSTR(MP_QSTR_any), (mp_obj_t)&mp_builtin_any_obj },
636 { MP_OBJ_NEW_QSTR(MP_QSTR_bin), (mp_obj_t)&mp_builtin_bin_obj },
637 { MP_OBJ_NEW_QSTR(MP_QSTR_callable), (mp_obj_t)&mp_builtin_callable_obj },
638#if MICROPY_PY_BUILTINS_COMPILE
639 { MP_OBJ_NEW_QSTR(MP_QSTR_compile), (mp_obj_t)&mp_builtin_compile_obj },
640#endif
641 { MP_OBJ_NEW_QSTR(MP_QSTR_chr), (mp_obj_t)&mp_builtin_chr_obj },
642 { MP_OBJ_NEW_QSTR(MP_QSTR_dir), (mp_obj_t)&mp_builtin_dir_obj },
643 { MP_OBJ_NEW_QSTR(MP_QSTR_divmod), (mp_obj_t)&mp_builtin_divmod_obj },
644 { MP_OBJ_NEW_QSTR(MP_QSTR_eval), (mp_obj_t)&mp_builtin_eval_obj },
645 { MP_OBJ_NEW_QSTR(MP_QSTR_exec), (mp_obj_t)&mp_builtin_exec_obj },
Damien George2a3e2b92014-12-19 13:36:17 +0000646#if MICROPY_PY_BUILTINS_EXECFILE
647 { MP_OBJ_NEW_QSTR(MP_QSTR_execfile), (mp_obj_t)&mp_builtin_execfile_obj },
648#endif
Damien George78d702c2014-12-09 16:19:48 +0000649 { MP_OBJ_NEW_QSTR(MP_QSTR_getattr), (mp_obj_t)&mp_builtin_getattr_obj },
stijnc1832fd2015-02-14 17:36:59 +0100650 { MP_OBJ_NEW_QSTR(MP_QSTR_setattr), (mp_obj_t)&mp_builtin_setattr_obj },
Damien George78d702c2014-12-09 16:19:48 +0000651 { MP_OBJ_NEW_QSTR(MP_QSTR_globals), (mp_obj_t)&mp_builtin_globals_obj },
652 { MP_OBJ_NEW_QSTR(MP_QSTR_hasattr), (mp_obj_t)&mp_builtin_hasattr_obj },
653 { MP_OBJ_NEW_QSTR(MP_QSTR_hash), (mp_obj_t)&mp_builtin_hash_obj },
654 { MP_OBJ_NEW_QSTR(MP_QSTR_hex), (mp_obj_t)&mp_builtin_hex_obj },
655 { MP_OBJ_NEW_QSTR(MP_QSTR_id), (mp_obj_t)&mp_builtin_id_obj },
656 { MP_OBJ_NEW_QSTR(MP_QSTR_isinstance), (mp_obj_t)&mp_builtin_isinstance_obj },
657 { MP_OBJ_NEW_QSTR(MP_QSTR_issubclass), (mp_obj_t)&mp_builtin_issubclass_obj },
658 { MP_OBJ_NEW_QSTR(MP_QSTR_iter), (mp_obj_t)&mp_builtin_iter_obj },
659 { MP_OBJ_NEW_QSTR(MP_QSTR_len), (mp_obj_t)&mp_builtin_len_obj },
660 { MP_OBJ_NEW_QSTR(MP_QSTR_locals), (mp_obj_t)&mp_builtin_locals_obj },
661 { MP_OBJ_NEW_QSTR(MP_QSTR_max), (mp_obj_t)&mp_builtin_max_obj },
662 { MP_OBJ_NEW_QSTR(MP_QSTR_min), (mp_obj_t)&mp_builtin_min_obj },
663 { MP_OBJ_NEW_QSTR(MP_QSTR_next), (mp_obj_t)&mp_builtin_next_obj },
664 { MP_OBJ_NEW_QSTR(MP_QSTR_oct), (mp_obj_t)&mp_builtin_oct_obj },
665 { MP_OBJ_NEW_QSTR(MP_QSTR_ord), (mp_obj_t)&mp_builtin_ord_obj },
666 { MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_builtin_pow_obj },
667 { MP_OBJ_NEW_QSTR(MP_QSTR_print), (mp_obj_t)&mp_builtin_print_obj },
668 { MP_OBJ_NEW_QSTR(MP_QSTR_repr), (mp_obj_t)&mp_builtin_repr_obj },
669 { MP_OBJ_NEW_QSTR(MP_QSTR_round), (mp_obj_t)&mp_builtin_round_obj },
670 { MP_OBJ_NEW_QSTR(MP_QSTR_sorted), (mp_obj_t)&mp_builtin_sorted_obj },
671 { MP_OBJ_NEW_QSTR(MP_QSTR_sum), (mp_obj_t)&mp_builtin_sum_obj },
672
673 // built-in exceptions
674 { MP_OBJ_NEW_QSTR(MP_QSTR_BaseException), (mp_obj_t)&mp_type_BaseException },
675 { MP_OBJ_NEW_QSTR(MP_QSTR_ArithmeticError), (mp_obj_t)&mp_type_ArithmeticError },
676 { MP_OBJ_NEW_QSTR(MP_QSTR_AssertionError), (mp_obj_t)&mp_type_AssertionError },
677 { MP_OBJ_NEW_QSTR(MP_QSTR_AttributeError), (mp_obj_t)&mp_type_AttributeError },
678 { MP_OBJ_NEW_QSTR(MP_QSTR_EOFError), (mp_obj_t)&mp_type_EOFError },
679 { MP_OBJ_NEW_QSTR(MP_QSTR_Exception), (mp_obj_t)&mp_type_Exception },
680 { MP_OBJ_NEW_QSTR(MP_QSTR_GeneratorExit), (mp_obj_t)&mp_type_GeneratorExit },
681 { MP_OBJ_NEW_QSTR(MP_QSTR_ImportError), (mp_obj_t)&mp_type_ImportError },
682 { MP_OBJ_NEW_QSTR(MP_QSTR_IndentationError), (mp_obj_t)&mp_type_IndentationError },
683 { MP_OBJ_NEW_QSTR(MP_QSTR_IndexError), (mp_obj_t)&mp_type_IndexError },
684 { MP_OBJ_NEW_QSTR(MP_QSTR_KeyError), (mp_obj_t)&mp_type_KeyError },
685 { MP_OBJ_NEW_QSTR(MP_QSTR_LookupError), (mp_obj_t)&mp_type_LookupError },
686 { MP_OBJ_NEW_QSTR(MP_QSTR_MemoryError), (mp_obj_t)&mp_type_MemoryError },
687 { MP_OBJ_NEW_QSTR(MP_QSTR_NameError), (mp_obj_t)&mp_type_NameError },
688 { MP_OBJ_NEW_QSTR(MP_QSTR_NotImplementedError), (mp_obj_t)&mp_type_NotImplementedError },
689 { MP_OBJ_NEW_QSTR(MP_QSTR_OSError), (mp_obj_t)&mp_type_OSError },
690 { MP_OBJ_NEW_QSTR(MP_QSTR_OverflowError), (mp_obj_t)&mp_type_OverflowError },
691 { MP_OBJ_NEW_QSTR(MP_QSTR_RuntimeError), (mp_obj_t)&mp_type_RuntimeError },
692 { MP_OBJ_NEW_QSTR(MP_QSTR_StopIteration), (mp_obj_t)&mp_type_StopIteration },
693 { MP_OBJ_NEW_QSTR(MP_QSTR_SyntaxError), (mp_obj_t)&mp_type_SyntaxError },
694 { MP_OBJ_NEW_QSTR(MP_QSTR_SystemExit), (mp_obj_t)&mp_type_SystemExit },
695 { MP_OBJ_NEW_QSTR(MP_QSTR_TypeError), (mp_obj_t)&mp_type_TypeError },
Paul Sokolovsky71ebd4b2015-02-23 23:18:36 +0200696 #if MICROPY_PY_BUILTINS_STR_UNICODE
697 { MP_OBJ_NEW_QSTR(MP_QSTR_UnicodeError), (mp_obj_t)&mp_type_UnicodeError },
698 #endif
Damien George78d702c2014-12-09 16:19:48 +0000699 { MP_OBJ_NEW_QSTR(MP_QSTR_ValueError), (mp_obj_t)&mp_type_ValueError },
700 { MP_OBJ_NEW_QSTR(MP_QSTR_ZeroDivisionError), (mp_obj_t)&mp_type_ZeroDivisionError },
701 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
702 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
703
704 // Extra builtins as defined by a port
705 MICROPY_PORT_BUILTINS
706};
707
708MP_DEFINE_CONST_DICT(mp_module_builtins_globals, mp_module_builtins_globals_table);
709
710const mp_obj_module_t mp_module_builtins = {
711 .base = { &mp_type_module },
712 .name = MP_QSTR_builtins,
713 .globals = (mp_obj_dict_t*)&mp_module_builtins_globals,
714};