blob: f080f457b635174d48f724a1161ebcd704dd4e8d [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"
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 George2e2e4042015-03-19 00:21:29 +000043#if MICROPY_PY_IO
stijne50cff62015-04-09 12:27:15 +020044extern struct _mp_dummy_t mp_sys_stdout_obj; // type is irrelevant, just need pointer
Damien George2e2e4042015-03-19 00:21:29 +000045#endif
46
Damien Georgeb97669a2014-01-08 11:47:55 +000047// args[0] is function from class body
48// args[1] is class name
49// args[2:] are base objects
Damien Georged182b982014-08-30 14:19:41 +010050STATIC mp_obj_t mp_builtin___build_class__(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgeb97669a2014-01-08 11:47:55 +000051 assert(2 <= n_args);
52
Damien George7efc5b32014-04-05 22:36:42 +010053 // set the new classes __locals__ object
54 mp_obj_dict_t *old_locals = mp_locals_get();
Damien George062478e2014-01-09 20:57:50 +000055 mp_obj_t class_locals = mp_obj_new_dict(0);
Damien George7efc5b32014-04-05 22:36:42 +010056 mp_locals_set(class_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000057
58 // call the class code
Damien George882b3632014-04-02 15:56:31 +010059 mp_obj_t cell = mp_call_function_0(args[0]);
Damiena3dcd9e2013-12-17 21:35:38 +000060
61 // restore old __locals__ object
Damien Georged17926d2014-03-30 13:35:08 +010062 mp_locals_set(old_locals);
Damiena3dcd9e2013-12-17 21:35:38 +000063
Damien Georgeb97669a2014-01-08 11:47:55 +000064 // get the class type (meta object) from the base objects
65 mp_obj_t meta;
66 if (n_args == 2) {
67 // no explicit bases, so use 'type'
Damien Georgec5966122014-02-15 16:10:44 +000068 meta = (mp_obj_t)&mp_type_type;
Damien Georgeb97669a2014-01-08 11:47:55 +000069 } else {
70 // use type of first base object
71 meta = mp_obj_get_type(args[2]);
72 }
Damien Georgeb97669a2014-01-08 11:47:55 +000073
74 // TODO do proper metaclass resolution for multiple base objects
75
Damien Georgeb97669a2014-01-08 11:47:55 +000076 // create the new class using a call to the meta object
Damien Georgeb97669a2014-01-08 11:47:55 +000077 mp_obj_t meta_args[3];
Damien George20006db2014-01-18 14:10:48 +000078 meta_args[0] = args[1]; // class name
Damien Georgeb97669a2014-01-08 11:47:55 +000079 meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases
Damien George20006db2014-01-18 14:10:48 +000080 meta_args[2] = class_locals; // dict of members
Damien Georged17926d2014-03-30 13:35:08 +010081 mp_obj_t new_class = mp_call_function_n_kw(meta, 3, 0, meta_args);
Damien Georgeb97669a2014-01-08 11:47:55 +000082
83 // store into cell if neede
84 if (cell != mp_const_none) {
85 mp_obj_cell_set(cell, new_class);
86 }
87
88 return new_class;
Damiena3dcd9e2013-12-17 21:35:38 +000089}
Damien Georgeb97669a2014-01-08 11:47:55 +000090MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
91
Damien George69c5fe12014-08-12 18:13:44 +010092STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
Damien George6837d462015-03-14 22:07:30 +000093 if (0) {
94 // dummy
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 {
Damien George6837d462015-03-14 22:07:30 +0000112 // this will raise a TypeError if the argument is not integral
113 return mp_obj_int_abs(o_in);
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
409 mp_obj_t stream_obj = &mp_sys_stdout_obj;
410 mp_map_elem_t *file_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_file), MP_MAP_LOOKUP);
411 if (file_elem != NULL && file_elem->value != mp_const_none) {
412 stream_obj = file_elem->value;
413 }
414
Damien George7f9d1d62015-04-09 23:56:15 +0100415 mp_print_t print = {stream_obj, (mp_print_strn_t)mp_stream_write};
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300416 #endif
Damien George42f3de92014-10-03 17:44:14 +0000417 for (mp_uint_t i = 0; i < n_args; i++) {
Damiena3dcd9e2013-12-17 21:35:38 +0000418 if (i > 0) {
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300419 #if MICROPY_PY_IO
420 mp_stream_write(stream_obj, sep_data, sep_len);
421 #else
Damien George4abff752014-08-30 14:59:21 +0100422 printf("%.*s", (int)sep_len, sep_data);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300423 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000424 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300425 #if MICROPY_PY_IO
Damien George7f9d1d62015-04-09 23:56:15 +0100426 mp_obj_print_helper(&print, args[i], PRINT_STR);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300427 #else
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200428 mp_obj_print(args[i], PRINT_STR);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300429 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000430 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300431 #if MICROPY_PY_IO
432 mp_stream_write(stream_obj, end_data, end_len);
433 #else
Damien George4abff752014-08-30 14:59:21 +0100434 printf("%.*s", (int)end_len, end_data);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300435 #endif
Damiend99b0522013-12-21 18:17:45 +0000436 return mp_const_none;
Damiena3dcd9e2013-12-17 21:35:38 +0000437}
Damien George48815662014-04-02 10:34:44 +0100438MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
Damien George23005372014-01-13 19:39:01 +0000439
Paul Sokolovsky1eca3282014-11-26 21:17:16 +0200440STATIC mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
441 if (o != mp_const_none) {
Damien George81e70a82015-01-28 23:53:13 +0000442 #if MICROPY_PY_IO
Damien George7f9d1d62015-04-09 23:56:15 +0100443 mp_print_t print = {&mp_sys_stdout_obj, (mp_print_strn_t)mp_stream_write};
444 mp_obj_print_helper(&print, o, PRINT_REPR);
Damien George81e70a82015-01-28 23:53:13 +0000445 mp_stream_write(&mp_sys_stdout_obj, "\n", 1);
446 #else
447 mp_obj_print(o, PRINT_REPR);
448 printf("\n");
449 #endif
Paul Sokolovsky1eca3282014-11-26 21:17:16 +0200450 }
451 return mp_const_none;
452}
453MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
454
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200455STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
Damien George0b9ee862015-01-21 19:14:25 +0000456 vstr_t vstr;
Damien George7f9d1d62015-04-09 23:56:15 +0100457 mp_print_t print;
458 vstr_init_print(&vstr, 16, &print);
459 mp_obj_print_helper(&print, o_in, PRINT_REPR);
Damien George0b9ee862015-01-21 19:14:25 +0000460 return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000461}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000462MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
463
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300464STATIC mp_obj_t mp_builtin_round(mp_uint_t n_args, const mp_obj_t *args) {
465 // TODO really support second arg
466 mp_obj_t o_in = args[0];
Damien George1559a972014-10-31 11:28:50 +0000467 if (MP_OBJ_IS_INT(o_in)) {
468 return o_in;
469 }
470#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300471 mp_int_t num_dig = 0;
472 if (n_args > 1) {
473 num_dig = mp_obj_get_int(args[1]);
474 if (num_dig > 0) {
475 mp_not_implemented("round(..., N>0)");
476 }
477 }
Damien George1559a972014-10-31 11:28:50 +0000478 mp_float_t val = mp_obj_get_float(o_in);
479 mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val);
480 mp_int_t r = rounded;
481 // make rounded value even if it was halfway between ints
482 if (val - rounded == 0.5) {
483 r = (r + 1) & (~1);
484 } else if (val - rounded == -0.5) {
485 r &= ~1;
486 }
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300487 if (n_args > 1) {
488 return mp_obj_new_float(r);
489 }
Damien George1559a972014-10-31 11:28:50 +0000490#else
491 mp_int_t r = mp_obj_get_int(o_in);
492#endif
493 return mp_obj_new_int(r);
494}
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300495MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_round_obj, 1, 2, mp_builtin_round);
Damien George1559a972014-10-31 11:28:50 +0000496
Damien Georged182b982014-08-30 14:19:41 +0100497STATIC mp_obj_t mp_builtin_sum(mp_uint_t n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000498 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000499 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000500 switch (n_args) {
Damien Georgedb1e10d2015-03-02 17:19:44 +0000501 case 1: value = MP_OBJ_NEW_SMALL_INT(0); break;
Damien George23005372014-01-13 19:39:01 +0000502 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000503 }
Damien Georged17926d2014-03-30 13:35:08 +0100504 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000505 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100506 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100507 value = mp_binary_op(MP_BINARY_OP_ADD, value, item);
Damiena3dcd9e2013-12-17 21:35:38 +0000508 }
509 return value;
510}
Damien George23005372014-01-13 19:39:01 +0000511MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000512
Damien Georged182b982014-08-30 14:19:41 +0100513STATIC 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 +0000514 assert(n_args >= 1);
515 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100516 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton5c768392014-01-13 05:12:50 +0000517 "must use keyword argument for key function"));
518 }
Damien George3e1a5c12014-03-29 13:43:38 +0000519 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 +0000520 mp_obj_list_sort(1, &self, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000521
522 return self;
523}
John R. Lenton88cb1e62014-01-13 19:55:18 +0000524MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200525
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300526// See mp_load_attr() if making any changes
527STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) {
528 mp_obj_t dest[2];
529 // use load_method, raising or not raising exception
530 ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest);
531 if (dest[0] == MP_OBJ_NULL) {
532 return defval;
533 } else if (dest[1] == MP_OBJ_NULL) {
534 // load_method returned just a normal attribute
535 return dest[0];
536 } else {
537 // load_method returned a method, so build a bound method object
538 return mp_obj_new_bound_meth(dest[0], dest[1]);
539 }
540}
541
Damien Georged182b982014-08-30 14:19:41 +0100542STATIC mp_obj_t mp_builtin_getattr(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300543 mp_obj_t defval = MP_OBJ_NULL;
544 if (n_args > 2) {
545 defval = args[2];
546 }
stijnc1832fd2015-02-14 17:36:59 +0100547 return mp_load_attr_default(args[0], mp_obj_str_get_qstr(args[1]), defval);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200548}
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300549MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr);
Paul Sokolovskycc0af3d2014-04-06 01:00:46 +0300550
stijnc1832fd2015-02-14 17:36:59 +0100551STATIC mp_obj_t mp_builtin_setattr(mp_obj_t base, mp_obj_t attr, mp_obj_t value) {
552 mp_store_attr(base, mp_obj_str_get_qstr(attr), value);
553 return mp_const_none;
554}
555MP_DEFINE_CONST_FUN_OBJ_3(mp_builtin_setattr_obj, mp_builtin_setattr);
556
Paul Sokolovskyff306662014-05-11 19:05:42 +0300557STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
558 assert(MP_OBJ_IS_QSTR(attr_in));
559
560 mp_obj_t dest[2];
Chris Angelicodaf973a2014-06-06 03:51:03 +1000561 // TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
Paul Sokolovskyff306662014-05-11 19:05:42 +0300562 // explicitly says "This is implemented by calling getattr(object, name) and seeing
563 // whether it raises an AttributeError or not.", so we should explicitly wrap this
564 // in nlr_push and handle exception.
565 mp_load_method_maybe(object_in, MP_OBJ_QSTR_VALUE(attr_in), dest);
566
567 return MP_BOOL(dest[0] != MP_OBJ_NULL);
568}
Paul Sokolovskyff306662014-05-11 19:05:42 +0300569MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
570
Damien George4c03b3a2014-08-12 18:33:40 +0100571// These are defined in terms of MicroPython API functions right away
Damien Georgec7687ad2014-08-22 21:48:30 +0100572MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_obj_id);
Damien George4c03b3a2014-08-12 18:33:40 +0100573MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_obj_len);
Paul Sokolovsky080d99b2014-04-06 01:18:19 +0300574MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
575MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);
Damien George78d702c2014-12-09 16:19:48 +0000576
577STATIC const mp_map_elem_t mp_module_builtins_globals_table[] = {
578 // built-in core functions
579 { MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), (mp_obj_t)&mp_builtin___build_class___obj },
580 { MP_OBJ_NEW_QSTR(MP_QSTR___import__), (mp_obj_t)&mp_builtin___import___obj },
581 { MP_OBJ_NEW_QSTR(MP_QSTR___repl_print__), (mp_obj_t)&mp_builtin___repl_print___obj },
582
583 // built-in types
584 { MP_OBJ_NEW_QSTR(MP_QSTR_bool), (mp_obj_t)&mp_type_bool },
585 { MP_OBJ_NEW_QSTR(MP_QSTR_bytes), (mp_obj_t)&mp_type_bytes },
586#if MICROPY_PY_BUILTINS_BYTEARRAY
587 { MP_OBJ_NEW_QSTR(MP_QSTR_bytearray), (mp_obj_t)&mp_type_bytearray },
588#endif
589#if MICROPY_PY_BUILTINS_COMPLEX
590 { MP_OBJ_NEW_QSTR(MP_QSTR_complex), (mp_obj_t)&mp_type_complex },
591#endif
592 { MP_OBJ_NEW_QSTR(MP_QSTR_dict), (mp_obj_t)&mp_type_dict },
Paul Sokolovskye2d44e32015-04-06 23:50:37 +0300593 #if MICROPY_PY_BUILTINS_ENUMERATE
Damien George78d702c2014-12-09 16:19:48 +0000594 { MP_OBJ_NEW_QSTR(MP_QSTR_enumerate), (mp_obj_t)&mp_type_enumerate },
Paul Sokolovskye2d44e32015-04-06 23:50:37 +0300595 #endif
Damien George78d702c2014-12-09 16:19:48 +0000596 { MP_OBJ_NEW_QSTR(MP_QSTR_filter), (mp_obj_t)&mp_type_filter },
597#if MICROPY_PY_BUILTINS_FLOAT
598 { MP_OBJ_NEW_QSTR(MP_QSTR_float), (mp_obj_t)&mp_type_float },
599#endif
Damien Georgee37dcaa2014-12-27 17:07:16 +0000600#if MICROPY_PY_BUILTINS_SET && MICROPY_PY_BUILTINS_FROZENSET
Damien George78d702c2014-12-09 16:19:48 +0000601 { MP_OBJ_NEW_QSTR(MP_QSTR_frozenset), (mp_obj_t)&mp_type_frozenset },
602#endif
603 { MP_OBJ_NEW_QSTR(MP_QSTR_int), (mp_obj_t)&mp_type_int },
604 { MP_OBJ_NEW_QSTR(MP_QSTR_list), (mp_obj_t)&mp_type_list },
605 { MP_OBJ_NEW_QSTR(MP_QSTR_map), (mp_obj_t)&mp_type_map },
606#if MICROPY_PY_BUILTINS_MEMORYVIEW
607 { MP_OBJ_NEW_QSTR(MP_QSTR_memoryview), (mp_obj_t)&mp_type_memoryview },
608#endif
609 { MP_OBJ_NEW_QSTR(MP_QSTR_object), (mp_obj_t)&mp_type_object },
610#if MICROPY_PY_BUILTINS_PROPERTY
611 { MP_OBJ_NEW_QSTR(MP_QSTR_property), (mp_obj_t)&mp_type_property },
612#endif
613 { MP_OBJ_NEW_QSTR(MP_QSTR_range), (mp_obj_t)&mp_type_range },
Paul Sokolovsky282ca092015-04-07 00:16:51 +0300614 #if MICROPY_PY_BUILTINS_REVERSED
Damien George78d702c2014-12-09 16:19:48 +0000615 { MP_OBJ_NEW_QSTR(MP_QSTR_reversed), (mp_obj_t)&mp_type_reversed },
Paul Sokolovsky282ca092015-04-07 00:16:51 +0300616 #endif
Damien George78d702c2014-12-09 16:19:48 +0000617#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};