blob: 1f33ab75ecc5f545b9efcd0021b4b446567cbf63 [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 Georgec5029bc2015-06-13 22:00:10 +0100233 return mp_binary_op(MP_BINARY_OP_DIVMOD, o1_in, o2_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000234}
Damien George23005372014-01-13 19:39:01 +0000235MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
236
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200237STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000238 // result is guaranteed to be a (small) int
239 return mp_unary_op(MP_UNARY_OP_HASH, o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000240}
Damiend99b0522013-12-21 18:17:45 +0000241MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
242
Damien George58051112014-04-15 12:42:52 +0100243STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) {
Damien Georgeb013aea2014-04-15 12:50:21 +0100244 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 +0100245}
Damien George58051112014-04-15 12:42:52 +0100246MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex);
247
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200248STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100249 return mp_getiter(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000250}
Damiend99b0522013-12-21 18:17:45 +0000251MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
Damiena3dcd9e2013-12-17 21:35:38 +0000252
Damien Georged182b982014-08-30 14:19:41 +0100253STATIC 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 +0100254 mp_map_elem_t *key_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP);
255 mp_obj_t key_fn = key_elem == NULL ? MP_OBJ_NULL : key_elem->value;
Damiena3dcd9e2013-12-17 21:35:38 +0000256 if (n_args == 1) {
257 // given an iterable
Damien Georged17926d2014-03-30 13:35:08 +0100258 mp_obj_t iterable = mp_getiter(args[0]);
Damien George7310fd42014-08-24 19:14:09 +0100259 mp_obj_t best_key = MP_OBJ_NULL;
260 mp_obj_t best_obj = MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000261 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100262 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George7310fd42014-08-24 19:14:09 +0100263 mp_obj_t key = key_fn == MP_OBJ_NULL ? item : mp_call_function_1(key_fn, item);
264 if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) {
265 best_key = key;
266 best_obj = item;
Damiena3dcd9e2013-12-17 21:35:38 +0000267 }
268 }
Damien George7310fd42014-08-24 19:14:09 +0100269 if (best_obj == MP_OBJ_NULL) {
270 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000271 }
Damien George7310fd42014-08-24 19:14:09 +0100272 return best_obj;
Damiena3dcd9e2013-12-17 21:35:38 +0000273 } else {
274 // given many args
Damien George7310fd42014-08-24 19:14:09 +0100275 mp_obj_t best_key = MP_OBJ_NULL;
276 mp_obj_t best_obj = MP_OBJ_NULL;
277 for (mp_uint_t i = 0; i < n_args; i++) {
278 mp_obj_t key = key_fn == MP_OBJ_NULL ? args[i] : mp_call_function_1(key_fn, args[i]);
279 if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) {
280 best_key = key;
281 best_obj = args[i];
Damiena3dcd9e2013-12-17 21:35:38 +0000282 }
283 }
Damien George7310fd42014-08-24 19:14:09 +0100284 return best_obj;
Damiena3dcd9e2013-12-17 21:35:38 +0000285 }
286}
287
Damien Georged182b982014-08-30 14:19:41 +0100288STATIC 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 +0100289 return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_MORE);
Damiena3dcd9e2013-12-17 21:35:38 +0000290}
Damien George7310fd42014-08-24 19:14:09 +0100291MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_max_obj, 1, mp_builtin_max);
Damiena3dcd9e2013-12-17 21:35:38 +0000292
Damien Georged182b982014-08-30 14:19:41 +0100293STATIC 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 +0100294 return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_LESS);
295}
296MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_min_obj, 1, mp_builtin_min);
Damien George23005372014-01-13 19:39:01 +0000297
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200298STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
Damien Georged17926d2014-03-30 13:35:08 +0100299 mp_obj_t ret = mp_iternext_allow_raise(o);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100300 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100301 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damiend9d62012013-12-21 18:38:03 +0000302 } else {
303 return ret;
304 }
Damiend99b0522013-12-21 18:17:45 +0000305}
Damiend99b0522013-12-21 18:17:45 +0000306MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
307
Damien George897fe0c2014-04-15 22:03:55 +0100308STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) {
309 return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in);
310}
Damien George897fe0c2014-04-15 22:03:55 +0100311MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
312
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200313STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
Damien Georged182b982014-08-30 14:19:41 +0100314 mp_uint_t len;
Damien George698ec212014-02-08 18:17:23 +0000315 const char *str = mp_obj_str_get_data(o_in, &len);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300316 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georged8cbbca2015-04-19 12:26:46 +0100317 if (MP_OBJ_IS_STR(o_in)) {
318 len = unichar_charlen(str, len);
319 if (len == 1) {
320 if (!UTF8_IS_NONASCII(*str)) {
321 goto return_first_byte;
322 }
Damien George40f3c022014-07-03 13:25:24 +0100323 mp_int_t ord = *str++ & 0x7F;
324 for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000325 ord &= ~mask;
326 }
327 while (UTF8_IS_CONT(*str)) {
328 ord = (ord << 6) | (*str++ & 0x3F);
329 }
330 return mp_obj_new_int(ord);
Damien Georged8cbbca2015-04-19 12:26:46 +0100331 }
332 } else {
333 // a bytes object
334 if (len == 1) {
335 return_first_byte:
336 return MP_OBJ_NEW_SMALL_INT(((const byte*)str)[0]);
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000337 }
Damiena3dcd9e2013-12-17 21:35:38 +0000338 }
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300339 #else
340 if (len == 1) {
341 // don't sign extend when converting to ord
342 return mp_obj_new_int(((const byte*)str)[0]);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300343 }
344 #endif
Damien George1e9a92f2014-11-06 17:36:16 +0000345
346 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
347 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
348 "ord expects a character"));
349 } else {
350 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
351 "ord() expected a character, but string of length %d found", len));
352 }
Damiena3dcd9e2013-12-17 21:35:38 +0000353}
Damien George23005372014-01-13 19:39:01 +0000354MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
355
Damien Georged182b982014-08-30 14:19:41 +0100356STATIC mp_obj_t mp_builtin_pow(mp_uint_t n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000357 assert(2 <= n_args && n_args <= 3);
Damiena3dcd9e2013-12-17 21:35:38 +0000358 switch (n_args) {
Damien Georged17926d2014-03-30 13:35:08 +0100359 case 2: return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]);
360 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 +0000361 }
362}
Damien George23005372014-01-13 19:39:01 +0000363MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
364
Damien Georged182b982014-08-30 14:19:41 +0100365STATIC 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 +0100366 mp_map_elem_t *sep_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_MAP_LOOKUP);
367 mp_map_elem_t *end_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_end), MP_MAP_LOOKUP);
368 const char *sep_data = " ";
Damien Georged182b982014-08-30 14:19:41 +0100369 mp_uint_t sep_len = 1;
Damien George48815662014-04-02 10:34:44 +0100370 const char *end_data = "\n";
Damien Georged182b982014-08-30 14:19:41 +0100371 mp_uint_t end_len = 1;
Damien George48815662014-04-02 10:34:44 +0100372 if (sep_elem != NULL && sep_elem->value != mp_const_none) {
373 sep_data = mp_obj_str_get_data(sep_elem->value, &sep_len);
374 }
375 if (end_elem != NULL && end_elem->value != mp_const_none) {
376 end_data = mp_obj_str_get_data(end_elem->value, &end_len);
377 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300378 #if MICROPY_PY_IO
379 mp_obj_t stream_obj = &mp_sys_stdout_obj;
380 mp_map_elem_t *file_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_file), MP_MAP_LOOKUP);
381 if (file_elem != NULL && file_elem->value != mp_const_none) {
382 stream_obj = file_elem->value;
383 }
384
Damien George7f9d1d62015-04-09 23:56:15 +0100385 mp_print_t print = {stream_obj, (mp_print_strn_t)mp_stream_write};
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300386 #endif
Damien George42f3de92014-10-03 17:44:14 +0000387 for (mp_uint_t i = 0; i < n_args; i++) {
Damiena3dcd9e2013-12-17 21:35:38 +0000388 if (i > 0) {
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300389 #if MICROPY_PY_IO
390 mp_stream_write(stream_obj, sep_data, sep_len);
391 #else
Damien Georgee72cda92015-04-11 12:15:47 +0100392 mp_print_strn(&mp_plat_print, sep_data, sep_len, 0, 0, 0);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300393 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000394 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300395 #if MICROPY_PY_IO
Damien George7f9d1d62015-04-09 23:56:15 +0100396 mp_obj_print_helper(&print, args[i], PRINT_STR);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300397 #else
Damien Georgee72cda92015-04-11 12:15:47 +0100398 mp_obj_print_helper(&mp_plat_print, args[i], PRINT_STR);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300399 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000400 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300401 #if MICROPY_PY_IO
402 mp_stream_write(stream_obj, end_data, end_len);
403 #else
Damien Georgee72cda92015-04-11 12:15:47 +0100404 mp_print_strn(&mp_plat_print, end_data, end_len, 0, 0, 0);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300405 #endif
Damiend99b0522013-12-21 18:17:45 +0000406 return mp_const_none;
Damiena3dcd9e2013-12-17 21:35:38 +0000407}
Damien George48815662014-04-02 10:34:44 +0100408MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
Damien George23005372014-01-13 19:39:01 +0000409
Paul Sokolovsky1eca3282014-11-26 21:17:16 +0200410STATIC mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
411 if (o != mp_const_none) {
Damien George81e70a82015-01-28 23:53:13 +0000412 #if MICROPY_PY_IO
Damien George5ae5ec92015-04-11 12:01:39 +0100413 mp_obj_print_helper(&mp_sys_stdout_print, o, PRINT_REPR);
414 mp_print_str(&mp_sys_stdout_print, "\n");
Damien George81e70a82015-01-28 23:53:13 +0000415 #else
Damien Georgee72cda92015-04-11 12:15:47 +0100416 mp_obj_print_helper(&mp_plat_print, o, PRINT_REPR);
417 mp_print_str(&mp_plat_print, "\n");
Damien George81e70a82015-01-28 23:53:13 +0000418 #endif
Paul Sokolovsky1eca3282014-11-26 21:17:16 +0200419 }
420 return mp_const_none;
421}
422MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
423
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200424STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
Damien George0b9ee862015-01-21 19:14:25 +0000425 vstr_t vstr;
Damien George7f9d1d62015-04-09 23:56:15 +0100426 mp_print_t print;
427 vstr_init_print(&vstr, 16, &print);
428 mp_obj_print_helper(&print, o_in, PRINT_REPR);
Damien George0b9ee862015-01-21 19:14:25 +0000429 return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000430}
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000431MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
432
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300433STATIC mp_obj_t mp_builtin_round(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300434 mp_obj_t o_in = args[0];
Damien George1559a972014-10-31 11:28:50 +0000435 if (MP_OBJ_IS_INT(o_in)) {
436 return o_in;
437 }
438#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300439 mp_int_t num_dig = 0;
440 if (n_args > 1) {
441 num_dig = mp_obj_get_int(args[1]);
Sebastian Plamauer1e8ca3a2015-07-14 14:44:31 +0200442 mp_float_t val = mp_obj_get_float(o_in);
443 mp_float_t mult = MICROPY_FLOAT_C_FUN(pow)(10, num_dig);
444 // TODO may lead to overflow
445 mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val * mult) / mult;
446 return mp_obj_new_float(rounded);
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300447 }
Damien George1559a972014-10-31 11:28:50 +0000448 mp_float_t val = mp_obj_get_float(o_in);
449 mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val);
450 mp_int_t r = rounded;
451 // make rounded value even if it was halfway between ints
452 if (val - rounded == 0.5) {
453 r = (r + 1) & (~1);
454 } else if (val - rounded == -0.5) {
455 r &= ~1;
456 }
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300457 if (n_args > 1) {
458 return mp_obj_new_float(r);
459 }
Damien George1559a972014-10-31 11:28:50 +0000460#else
461 mp_int_t r = mp_obj_get_int(o_in);
462#endif
463 return mp_obj_new_int(r);
464}
Paul Sokolovskyfdaac1d2015-03-30 02:20:23 +0300465MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_round_obj, 1, 2, mp_builtin_round);
Damien George1559a972014-10-31 11:28:50 +0000466
Damien Georged182b982014-08-30 14:19:41 +0100467STATIC mp_obj_t mp_builtin_sum(mp_uint_t n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000468 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000469 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000470 switch (n_args) {
Damien Georgedb1e10d2015-03-02 17:19:44 +0000471 case 1: value = MP_OBJ_NEW_SMALL_INT(0); break;
Damien George23005372014-01-13 19:39:01 +0000472 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000473 }
Damien Georged17926d2014-03-30 13:35:08 +0100474 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000475 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100476 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100477 value = mp_binary_op(MP_BINARY_OP_ADD, value, item);
Damiena3dcd9e2013-12-17 21:35:38 +0000478 }
479 return value;
480}
Damien George23005372014-01-13 19:39:01 +0000481MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000482
Damien Georged182b982014-08-30 14:19:41 +0100483STATIC 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 +0000484 assert(n_args >= 1);
485 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100486 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton5c768392014-01-13 05:12:50 +0000487 "must use keyword argument for key function"));
488 }
Damien George3e1a5c12014-03-29 13:43:38 +0000489 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 +0000490 mp_obj_list_sort(1, &self, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000491
492 return self;
493}
John R. Lenton88cb1e62014-01-13 19:55:18 +0000494MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200495
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300496// See mp_load_attr() if making any changes
497STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) {
498 mp_obj_t dest[2];
499 // use load_method, raising or not raising exception
500 ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest);
501 if (dest[0] == MP_OBJ_NULL) {
502 return defval;
503 } else if (dest[1] == MP_OBJ_NULL) {
504 // load_method returned just a normal attribute
505 return dest[0];
506 } else {
507 // load_method returned a method, so build a bound method object
508 return mp_obj_new_bound_meth(dest[0], dest[1]);
509 }
510}
511
Damien Georged182b982014-08-30 14:19:41 +0100512STATIC mp_obj_t mp_builtin_getattr(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300513 mp_obj_t defval = MP_OBJ_NULL;
514 if (n_args > 2) {
515 defval = args[2];
516 }
stijnc1832fd2015-02-14 17:36:59 +0100517 return mp_load_attr_default(args[0], mp_obj_str_get_qstr(args[1]), defval);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200518}
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300519MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr);
Paul Sokolovskycc0af3d2014-04-06 01:00:46 +0300520
stijnc1832fd2015-02-14 17:36:59 +0100521STATIC mp_obj_t mp_builtin_setattr(mp_obj_t base, mp_obj_t attr, mp_obj_t value) {
522 mp_store_attr(base, mp_obj_str_get_qstr(attr), value);
523 return mp_const_none;
524}
525MP_DEFINE_CONST_FUN_OBJ_3(mp_builtin_setattr_obj, mp_builtin_setattr);
526
Paul Sokolovskyff306662014-05-11 19:05:42 +0300527STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
528 assert(MP_OBJ_IS_QSTR(attr_in));
529
530 mp_obj_t dest[2];
Chris Angelicodaf973a2014-06-06 03:51:03 +1000531 // TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
Paul Sokolovskyff306662014-05-11 19:05:42 +0300532 // explicitly says "This is implemented by calling getattr(object, name) and seeing
533 // whether it raises an AttributeError or not.", so we should explicitly wrap this
534 // in nlr_push and handle exception.
535 mp_load_method_maybe(object_in, MP_OBJ_QSTR_VALUE(attr_in), dest);
536
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300537 return mp_obj_new_bool(dest[0] != MP_OBJ_NULL);
Paul Sokolovskyff306662014-05-11 19:05:42 +0300538}
Paul Sokolovskyff306662014-05-11 19:05:42 +0300539MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
540
Damien George4c03b3a2014-08-12 18:33:40 +0100541// These are defined in terms of MicroPython API functions right away
Damien Georgec7687ad2014-08-22 21:48:30 +0100542MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_obj_id);
Damien George4c03b3a2014-08-12 18:33:40 +0100543MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_obj_len);
Paul Sokolovsky080d99b2014-04-06 01:18:19 +0300544MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
545MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);
Damien George78d702c2014-12-09 16:19:48 +0000546
547STATIC const mp_map_elem_t mp_module_builtins_globals_table[] = {
548 // built-in core functions
549 { MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), (mp_obj_t)&mp_builtin___build_class___obj },
550 { MP_OBJ_NEW_QSTR(MP_QSTR___import__), (mp_obj_t)&mp_builtin___import___obj },
551 { MP_OBJ_NEW_QSTR(MP_QSTR___repl_print__), (mp_obj_t)&mp_builtin___repl_print___obj },
552
553 // built-in types
554 { MP_OBJ_NEW_QSTR(MP_QSTR_bool), (mp_obj_t)&mp_type_bool },
555 { MP_OBJ_NEW_QSTR(MP_QSTR_bytes), (mp_obj_t)&mp_type_bytes },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300556 #if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George78d702c2014-12-09 16:19:48 +0000557 { MP_OBJ_NEW_QSTR(MP_QSTR_bytearray), (mp_obj_t)&mp_type_bytearray },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300558 #endif
559 #if MICROPY_PY_BUILTINS_COMPLEX
Damien George78d702c2014-12-09 16:19:48 +0000560 { MP_OBJ_NEW_QSTR(MP_QSTR_complex), (mp_obj_t)&mp_type_complex },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300561 #endif
Damien George78d702c2014-12-09 16:19:48 +0000562 { MP_OBJ_NEW_QSTR(MP_QSTR_dict), (mp_obj_t)&mp_type_dict },
Paul Sokolovskye2d44e32015-04-06 23:50:37 +0300563 #if MICROPY_PY_BUILTINS_ENUMERATE
Damien George78d702c2014-12-09 16:19:48 +0000564 { MP_OBJ_NEW_QSTR(MP_QSTR_enumerate), (mp_obj_t)&mp_type_enumerate },
Paul Sokolovskye2d44e32015-04-06 23:50:37 +0300565 #endif
Paul Sokolovsky22ff3972015-08-20 01:01:56 +0300566 #if MICROPY_PY_BUILTINS_FILTER
Damien George78d702c2014-12-09 16:19:48 +0000567 { MP_OBJ_NEW_QSTR(MP_QSTR_filter), (mp_obj_t)&mp_type_filter },
Paul Sokolovsky22ff3972015-08-20 01:01:56 +0300568 #endif
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300569 #if MICROPY_PY_BUILTINS_FLOAT
Damien George78d702c2014-12-09 16:19:48 +0000570 { MP_OBJ_NEW_QSTR(MP_QSTR_float), (mp_obj_t)&mp_type_float },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300571 #endif
572 #if MICROPY_PY_BUILTINS_SET && MICROPY_PY_BUILTINS_FROZENSET
Damien George78d702c2014-12-09 16:19:48 +0000573 { MP_OBJ_NEW_QSTR(MP_QSTR_frozenset), (mp_obj_t)&mp_type_frozenset },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300574 #endif
Damien George78d702c2014-12-09 16:19:48 +0000575 { MP_OBJ_NEW_QSTR(MP_QSTR_int), (mp_obj_t)&mp_type_int },
576 { MP_OBJ_NEW_QSTR(MP_QSTR_list), (mp_obj_t)&mp_type_list },
577 { MP_OBJ_NEW_QSTR(MP_QSTR_map), (mp_obj_t)&mp_type_map },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300578 #if MICROPY_PY_BUILTINS_MEMORYVIEW
Damien George78d702c2014-12-09 16:19:48 +0000579 { MP_OBJ_NEW_QSTR(MP_QSTR_memoryview), (mp_obj_t)&mp_type_memoryview },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300580 #endif
Damien George78d702c2014-12-09 16:19:48 +0000581 { MP_OBJ_NEW_QSTR(MP_QSTR_object), (mp_obj_t)&mp_type_object },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300582 #if MICROPY_PY_BUILTINS_PROPERTY
Damien George78d702c2014-12-09 16:19:48 +0000583 { MP_OBJ_NEW_QSTR(MP_QSTR_property), (mp_obj_t)&mp_type_property },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300584 #endif
Damien George78d702c2014-12-09 16:19:48 +0000585 { MP_OBJ_NEW_QSTR(MP_QSTR_range), (mp_obj_t)&mp_type_range },
Paul Sokolovsky282ca092015-04-07 00:16:51 +0300586 #if MICROPY_PY_BUILTINS_REVERSED
Damien George78d702c2014-12-09 16:19:48 +0000587 { MP_OBJ_NEW_QSTR(MP_QSTR_reversed), (mp_obj_t)&mp_type_reversed },
Paul Sokolovsky282ca092015-04-07 00:16:51 +0300588 #endif
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300589 #if MICROPY_PY_BUILTINS_SET
Damien George78d702c2014-12-09 16:19:48 +0000590 { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&mp_type_set },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300591 #endif
Damien George78d702c2014-12-09 16:19:48 +0000592 { MP_OBJ_NEW_QSTR(MP_QSTR_str), (mp_obj_t)&mp_type_str },
593 { MP_OBJ_NEW_QSTR(MP_QSTR_super), (mp_obj_t)&mp_type_super },
594 { MP_OBJ_NEW_QSTR(MP_QSTR_tuple), (mp_obj_t)&mp_type_tuple },
595 { MP_OBJ_NEW_QSTR(MP_QSTR_type), (mp_obj_t)&mp_type_type },
596 { MP_OBJ_NEW_QSTR(MP_QSTR_zip), (mp_obj_t)&mp_type_zip },
597
598 { MP_OBJ_NEW_QSTR(MP_QSTR_classmethod), (mp_obj_t)&mp_type_classmethod },
599 { MP_OBJ_NEW_QSTR(MP_QSTR_staticmethod), (mp_obj_t)&mp_type_staticmethod },
600
601 // built-in objects
602 { MP_OBJ_NEW_QSTR(MP_QSTR_Ellipsis), (mp_obj_t)&mp_const_ellipsis_obj },
Paul Sokolovsky5ab5ac52015-05-04 19:45:53 +0300603 #if MICROPY_PY_BUILTINS_NOTIMPLEMENTED
Paul Sokolovsky76677272015-05-05 22:18:07 +0300604 { MP_OBJ_NEW_QSTR(MP_QSTR_NotImplemented), (mp_obj_t)&mp_const_notimplemented_obj },
Paul Sokolovsky5ab5ac52015-05-04 19:45:53 +0300605 #endif
Damien George78d702c2014-12-09 16:19:48 +0000606
607 // built-in user functions
608 { MP_OBJ_NEW_QSTR(MP_QSTR_abs), (mp_obj_t)&mp_builtin_abs_obj },
609 { MP_OBJ_NEW_QSTR(MP_QSTR_all), (mp_obj_t)&mp_builtin_all_obj },
610 { MP_OBJ_NEW_QSTR(MP_QSTR_any), (mp_obj_t)&mp_builtin_any_obj },
611 { MP_OBJ_NEW_QSTR(MP_QSTR_bin), (mp_obj_t)&mp_builtin_bin_obj },
612 { MP_OBJ_NEW_QSTR(MP_QSTR_callable), (mp_obj_t)&mp_builtin_callable_obj },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300613 #if MICROPY_PY_BUILTINS_COMPILE
Damien George78d702c2014-12-09 16:19:48 +0000614 { MP_OBJ_NEW_QSTR(MP_QSTR_compile), (mp_obj_t)&mp_builtin_compile_obj },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300615 #endif
Damien George78d702c2014-12-09 16:19:48 +0000616 { MP_OBJ_NEW_QSTR(MP_QSTR_chr), (mp_obj_t)&mp_builtin_chr_obj },
617 { MP_OBJ_NEW_QSTR(MP_QSTR_dir), (mp_obj_t)&mp_builtin_dir_obj },
618 { MP_OBJ_NEW_QSTR(MP_QSTR_divmod), (mp_obj_t)&mp_builtin_divmod_obj },
619 { MP_OBJ_NEW_QSTR(MP_QSTR_eval), (mp_obj_t)&mp_builtin_eval_obj },
620 { MP_OBJ_NEW_QSTR(MP_QSTR_exec), (mp_obj_t)&mp_builtin_exec_obj },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300621 #if MICROPY_PY_BUILTINS_EXECFILE
Damien George2a3e2b92014-12-19 13:36:17 +0000622 { MP_OBJ_NEW_QSTR(MP_QSTR_execfile), (mp_obj_t)&mp_builtin_execfile_obj },
Paul Sokolovsky21ffa7c2015-08-31 00:22:11 +0300623 #endif
Damien George78d702c2014-12-09 16:19:48 +0000624 { MP_OBJ_NEW_QSTR(MP_QSTR_getattr), (mp_obj_t)&mp_builtin_getattr_obj },
stijnc1832fd2015-02-14 17:36:59 +0100625 { MP_OBJ_NEW_QSTR(MP_QSTR_setattr), (mp_obj_t)&mp_builtin_setattr_obj },
Damien George78d702c2014-12-09 16:19:48 +0000626 { MP_OBJ_NEW_QSTR(MP_QSTR_globals), (mp_obj_t)&mp_builtin_globals_obj },
627 { MP_OBJ_NEW_QSTR(MP_QSTR_hasattr), (mp_obj_t)&mp_builtin_hasattr_obj },
628 { MP_OBJ_NEW_QSTR(MP_QSTR_hash), (mp_obj_t)&mp_builtin_hash_obj },
629 { MP_OBJ_NEW_QSTR(MP_QSTR_hex), (mp_obj_t)&mp_builtin_hex_obj },
630 { MP_OBJ_NEW_QSTR(MP_QSTR_id), (mp_obj_t)&mp_builtin_id_obj },
631 { MP_OBJ_NEW_QSTR(MP_QSTR_isinstance), (mp_obj_t)&mp_builtin_isinstance_obj },
632 { MP_OBJ_NEW_QSTR(MP_QSTR_issubclass), (mp_obj_t)&mp_builtin_issubclass_obj },
633 { MP_OBJ_NEW_QSTR(MP_QSTR_iter), (mp_obj_t)&mp_builtin_iter_obj },
634 { MP_OBJ_NEW_QSTR(MP_QSTR_len), (mp_obj_t)&mp_builtin_len_obj },
635 { MP_OBJ_NEW_QSTR(MP_QSTR_locals), (mp_obj_t)&mp_builtin_locals_obj },
636 { MP_OBJ_NEW_QSTR(MP_QSTR_max), (mp_obj_t)&mp_builtin_max_obj },
637 { MP_OBJ_NEW_QSTR(MP_QSTR_min), (mp_obj_t)&mp_builtin_min_obj },
638 { MP_OBJ_NEW_QSTR(MP_QSTR_next), (mp_obj_t)&mp_builtin_next_obj },
639 { MP_OBJ_NEW_QSTR(MP_QSTR_oct), (mp_obj_t)&mp_builtin_oct_obj },
640 { MP_OBJ_NEW_QSTR(MP_QSTR_ord), (mp_obj_t)&mp_builtin_ord_obj },
641 { MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_builtin_pow_obj },
642 { MP_OBJ_NEW_QSTR(MP_QSTR_print), (mp_obj_t)&mp_builtin_print_obj },
643 { MP_OBJ_NEW_QSTR(MP_QSTR_repr), (mp_obj_t)&mp_builtin_repr_obj },
644 { MP_OBJ_NEW_QSTR(MP_QSTR_round), (mp_obj_t)&mp_builtin_round_obj },
645 { MP_OBJ_NEW_QSTR(MP_QSTR_sorted), (mp_obj_t)&mp_builtin_sorted_obj },
646 { MP_OBJ_NEW_QSTR(MP_QSTR_sum), (mp_obj_t)&mp_builtin_sum_obj },
647
648 // built-in exceptions
649 { MP_OBJ_NEW_QSTR(MP_QSTR_BaseException), (mp_obj_t)&mp_type_BaseException },
650 { MP_OBJ_NEW_QSTR(MP_QSTR_ArithmeticError), (mp_obj_t)&mp_type_ArithmeticError },
651 { MP_OBJ_NEW_QSTR(MP_QSTR_AssertionError), (mp_obj_t)&mp_type_AssertionError },
652 { MP_OBJ_NEW_QSTR(MP_QSTR_AttributeError), (mp_obj_t)&mp_type_AttributeError },
653 { MP_OBJ_NEW_QSTR(MP_QSTR_EOFError), (mp_obj_t)&mp_type_EOFError },
654 { MP_OBJ_NEW_QSTR(MP_QSTR_Exception), (mp_obj_t)&mp_type_Exception },
655 { MP_OBJ_NEW_QSTR(MP_QSTR_GeneratorExit), (mp_obj_t)&mp_type_GeneratorExit },
656 { MP_OBJ_NEW_QSTR(MP_QSTR_ImportError), (mp_obj_t)&mp_type_ImportError },
657 { MP_OBJ_NEW_QSTR(MP_QSTR_IndentationError), (mp_obj_t)&mp_type_IndentationError },
658 { MP_OBJ_NEW_QSTR(MP_QSTR_IndexError), (mp_obj_t)&mp_type_IndexError },
Damien Georged7192fe2015-06-05 10:46:22 +0100659 { MP_OBJ_NEW_QSTR(MP_QSTR_KeyboardInterrupt), (mp_obj_t)&mp_type_KeyboardInterrupt },
Damien George78d702c2014-12-09 16:19:48 +0000660 { MP_OBJ_NEW_QSTR(MP_QSTR_KeyError), (mp_obj_t)&mp_type_KeyError },
661 { MP_OBJ_NEW_QSTR(MP_QSTR_LookupError), (mp_obj_t)&mp_type_LookupError },
662 { MP_OBJ_NEW_QSTR(MP_QSTR_MemoryError), (mp_obj_t)&mp_type_MemoryError },
663 { MP_OBJ_NEW_QSTR(MP_QSTR_NameError), (mp_obj_t)&mp_type_NameError },
664 { MP_OBJ_NEW_QSTR(MP_QSTR_NotImplementedError), (mp_obj_t)&mp_type_NotImplementedError },
665 { MP_OBJ_NEW_QSTR(MP_QSTR_OSError), (mp_obj_t)&mp_type_OSError },
666 { MP_OBJ_NEW_QSTR(MP_QSTR_OverflowError), (mp_obj_t)&mp_type_OverflowError },
667 { MP_OBJ_NEW_QSTR(MP_QSTR_RuntimeError), (mp_obj_t)&mp_type_RuntimeError },
668 { MP_OBJ_NEW_QSTR(MP_QSTR_StopIteration), (mp_obj_t)&mp_type_StopIteration },
669 { MP_OBJ_NEW_QSTR(MP_QSTR_SyntaxError), (mp_obj_t)&mp_type_SyntaxError },
670 { MP_OBJ_NEW_QSTR(MP_QSTR_SystemExit), (mp_obj_t)&mp_type_SystemExit },
671 { MP_OBJ_NEW_QSTR(MP_QSTR_TypeError), (mp_obj_t)&mp_type_TypeError },
Paul Sokolovsky71ebd4b2015-02-23 23:18:36 +0200672 #if MICROPY_PY_BUILTINS_STR_UNICODE
673 { MP_OBJ_NEW_QSTR(MP_QSTR_UnicodeError), (mp_obj_t)&mp_type_UnicodeError },
674 #endif
Damien George78d702c2014-12-09 16:19:48 +0000675 { MP_OBJ_NEW_QSTR(MP_QSTR_ValueError), (mp_obj_t)&mp_type_ValueError },
Damien Georgec8b60f02015-04-20 13:29:31 +0000676 #if MICROPY_EMIT_NATIVE
677 { MP_OBJ_NEW_QSTR(MP_QSTR_ViperTypeError), (mp_obj_t)&mp_type_ViperTypeError },
678 #endif
Damien George78d702c2014-12-09 16:19:48 +0000679 { MP_OBJ_NEW_QSTR(MP_QSTR_ZeroDivisionError), (mp_obj_t)&mp_type_ZeroDivisionError },
680 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
681 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
682
683 // Extra builtins as defined by a port
684 MICROPY_PORT_BUILTINS
685};
686
687MP_DEFINE_CONST_DICT(mp_module_builtins_globals, mp_module_builtins_globals_table);
688
689const mp_obj_module_t mp_module_builtins = {
690 .base = { &mp_type_module },
691 .name = MP_QSTR_builtins,
692 .globals = (mp_obj_dict_t*)&mp_module_builtins_globals,
693};