blob: b337af3c59658f9ce2f8b6a9917e23c851f1ff2a [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
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030030#include "mpconfig.h"
Damien660365e2013-12-17 18:27:24 +000031#include "nlr.h"
32#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000033#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000034#include "obj.h"
Damien George897fe0c2014-04-15 22:03:55 +010035#include "objstr.h"
Damien Georgee72be1b2014-10-22 23:05:50 +010036#include "smallint.h"
Damiend99b0522013-12-21 18:17:45 +000037#include "runtime0.h"
38#include "runtime.h"
Damien660365e2013-12-17 18:27:24 +000039#include "builtin.h"
Paul Sokolovskycb66f412014-07-13 23:07:42 +030040#include "stream.h"
41#include "pfenv.h"
Damien660365e2013-12-17 18:27:24 +000042
Damien Georgefb510b32014-06-01 13:32:54 +010043#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000044#include <math.h>
45#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
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020092STATIC mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
Damiend99b0522013-12-21 18:17:45 +000093 if (o != mp_const_none) {
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020094 mp_obj_print(o, PRINT_REPR);
Damien660365e2013-12-17 18:27:24 +000095 printf("\n");
96 }
Damiend99b0522013-12-21 18:17:45 +000097 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +000098}
Damien George23005372014-01-13 19:39:01 +000099MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
100
Damien George69c5fe12014-08-12 18:13:44 +0100101STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000102 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien George40f3c022014-07-03 13:25:24 +0100103 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
Damien660365e2013-12-17 18:27:24 +0000104 if (val < 0) {
105 val = -val;
106 }
Damiend99b0522013-12-21 18:17:45 +0000107 return MP_OBJ_NEW_SMALL_INT(val);
Damien Georgefb510b32014-06-01 13:32:54 +0100108#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000109 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000110 mp_float_t value = mp_obj_float_get(o_in);
Damien660365e2013-12-17 18:27:24 +0000111 // TODO check for NaN etc
Damiend99b0522013-12-21 18:17:45 +0000112 if (value < 0) {
113 return mp_obj_new_float(-value);
Damien660365e2013-12-17 18:27:24 +0000114 } else {
115 return o_in;
116 }
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300117#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0c36da02014-03-08 15:24:39 +0000118 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000119 mp_float_t real, imag;
120 mp_obj_complex_get(o_in, &real, &imag);
Damien George0c36da02014-03-08 15:24:39 +0000121 return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag));
Damien660365e2013-12-17 18:27:24 +0000122#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300123#endif
Damien660365e2013-12-17 18:27:24 +0000124 } else {
125 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000126 return mp_const_none;
Damien660365e2013-12-17 18:27:24 +0000127 }
128}
Damien George23005372014-01-13 19:39:01 +0000129MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
130
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200131STATIC mp_obj_t mp_builtin_all(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_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000137 }
138 }
Damiend99b0522013-12-21 18:17:45 +0000139 return mp_const_true;
Damien660365e2013-12-17 18:27:24 +0000140}
Damien George23005372014-01-13 19:39:01 +0000141MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
142
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200143STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100144 mp_obj_t iterable = mp_getiter(o_in);
Damiend99b0522013-12-21 18:17:45 +0000145 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100146 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100147 if (mp_obj_is_true(item)) {
Damiend99b0522013-12-21 18:17:45 +0000148 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000149 }
150 }
Damiend99b0522013-12-21 18:17:45 +0000151 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000152}
Damien George23005372014-01-13 19:39:01 +0000153MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
154
Damien George897fe0c2014-04-15 22:03:55 +0100155STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) {
156 mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_b_brace_close_), o_in };
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200157 return mp_obj_str_format(MP_ARRAY_SIZE(args), args);
Damien George897fe0c2014-04-15 22:03:55 +0100158}
Damien George897fe0c2014-04-15 22:03:55 +0100159MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin);
160
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200161STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000162 if (mp_obj_is_callable(o_in)) {
163 return mp_const_true;
Damiena3dcd9e2013-12-17 21:35:38 +0000164 } else {
Damiend99b0522013-12-21 18:17:45 +0000165 return mp_const_false;
Damiena3dcd9e2013-12-17 21:35:38 +0000166 }
167}
Damien George23005372014-01-13 19:39:01 +0000168MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
169
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200170STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300171 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georgec9aa58e2014-07-31 13:41:43 +0000172 mp_uint_t c = mp_obj_get_int(o_in);
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000173 char str[4];
174 int len = 0;
175 if (c < 0x80) {
176 *str = c; len = 1;
177 } else if (c < 0x800) {
178 str[0] = (c >> 6) | 0xC0;
179 str[1] = (c & 0x3F) | 0x80;
180 len = 2;
181 } else if (c < 0x10000) {
182 str[0] = (c >> 12) | 0xE0;
183 str[1] = ((c >> 6) & 0x3F) | 0x80;
184 str[2] = (c & 0x3F) | 0x80;
185 len = 3;
186 } else if (c < 0x110000) {
187 str[0] = (c >> 18) | 0xF0;
188 str[1] = ((c >> 12) & 0x3F) | 0x80;
189 str[2] = ((c >> 6) & 0x3F) | 0x80;
190 str[3] = (c & 0x3F) | 0x80;
191 len = 4;
Damiena3dcd9e2013-12-17 21:35:38 +0000192 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100193 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
Damiena3dcd9e2013-12-17 21:35:38 +0000194 }
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000195 return mp_obj_new_str(str, len, true);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300196 #else
Damien George40f3c022014-07-03 13:25:24 +0100197 mp_int_t ord = mp_obj_get_int(o_in);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300198 if (0 <= ord && ord <= 0x10ffff) {
199 char str[1] = {ord};
200 return mp_obj_new_str(str, 1, true);
201 } else {
202 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
203 }
204 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000205}
Damien George23005372014-01-13 19:39:01 +0000206MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
207
Damien Georged182b982014-08-30 14:19:41 +0100208STATIC mp_obj_t mp_builtin_dir(mp_uint_t n_args, const mp_obj_t *args) {
Damien George4acb2452014-02-02 22:07:44 +0000209 // TODO make this function more general and less of a hack
210
Damien George7efc5b32014-04-05 22:36:42 +0100211 mp_obj_dict_t *dict = NULL;
Damien George4acb2452014-02-02 22:07:44 +0000212 if (n_args == 0) {
213 // make a list of names in the local name space
Damien George7efc5b32014-04-05 22:36:42 +0100214 dict = mp_locals_get();
Damien George4acb2452014-02-02 22:07:44 +0000215 } else { // n_args == 1
216 // make a list of names in the given object
Damien George6022d9d2014-03-26 22:35:00 +0000217 if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
Damien George7efc5b32014-04-05 22:36:42 +0100218 dict = mp_obj_module_get_globals(args[0]);
Damien George6022d9d2014-03-26 22:35:00 +0000219 } else {
220 mp_obj_type_t *type;
221 if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
222 type = args[0];
223 } else {
224 type = mp_obj_get_type(args[0]);
225 }
Damien George3e1a5c12014-03-29 13:43:38 +0000226 if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
Damien George7efc5b32014-04-05 22:36:42 +0100227 dict = type->locals_dict;
Damien George6022d9d2014-03-26 22:35:00 +0000228 }
Damien Georgebadc9d42014-03-23 00:03:11 +0000229 }
Damien George4acb2452014-02-02 22:07:44 +0000230 }
231
232 mp_obj_t dir = mp_obj_new_list(0, NULL);
Damien George7efc5b32014-04-05 22:36:42 +0100233 if (dict != NULL) {
Damien Georged182b982014-08-30 14:19:41 +0100234 for (mp_uint_t i = 0; i < dict->map.alloc; i++) {
Damien George7efc5b32014-04-05 22:36:42 +0100235 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
236 mp_obj_list_append(dir, dict->map.table[i].key);
Damien Georgebadc9d42014-03-23 00:03:11 +0000237 }
238 }
239 }
Damien George9b196cd2014-03-26 21:47:19 +0000240
Damien George4acb2452014-02-02 22:07:44 +0000241 return dir;
242}
Damien George4acb2452014-02-02 22:07:44 +0000243MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir);
244
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200245STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
Damien George8594ce22014-09-13 18:43:09 +0100246 // TODO handle big int
Damiend99b0522013-12-21 18:17:45 +0000247 if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
Damien George40f3c022014-07-03 13:25:24 +0100248 mp_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
249 mp_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
Damien George8594ce22014-09-13 18:43:09 +0100250 if (i2 == 0) {
Damien George83695592014-09-13 19:58:18 +0100251 #if MICROPY_PY_BUILTINS_FLOAT
Damien George8594ce22014-09-13 18:43:09 +0100252 zero_division_error:
Damien George83695592014-09-13 19:58:18 +0100253 #endif
Damien George8594ce22014-09-13 18:43:09 +0100254 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
255 }
Damien George20006db2014-01-18 14:10:48 +0000256 mp_obj_t args[2];
Damien Georgee72be1b2014-10-22 23:05:50 +0100257 args[0] = MP_OBJ_NEW_SMALL_INT(mp_small_int_floor_divide(i1, i2));
258 args[1] = MP_OBJ_NEW_SMALL_INT(mp_small_int_modulo(i1, i2));
Damien George15d18062014-03-31 16:28:13 +0100259 return mp_obj_new_tuple(2, args);
Damien George83695592014-09-13 19:58:18 +0100260 #if MICROPY_PY_BUILTINS_FLOAT
Damien George8594ce22014-09-13 18:43:09 +0100261 } else if (MP_OBJ_IS_TYPE(o1_in, &mp_type_float) || MP_OBJ_IS_TYPE(o2_in, &mp_type_float)) {
262 mp_float_t f1 = mp_obj_get_float(o1_in);
263 mp_float_t f2 = mp_obj_get_float(o2_in);
264 if (f2 == 0.0) {
265 goto zero_division_error;
266 }
267 mp_obj_float_divmod(&f1, &f2);
268 mp_obj_t tuple[2] = {
269 mp_obj_new_float(f1),
270 mp_obj_new_float(f2),
271 };
272 return mp_obj_new_tuple(2, tuple);
Damien George83695592014-09-13 19:58:18 +0100273 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000274 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000275 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
276 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
277 "unsupported operand type(s) for divmod()"));
278 } else {
279 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
280 "unsupported operand type(s) for divmod(): '%s' and '%s'",
281 mp_obj_get_type_str(o1_in), mp_obj_get_type_str(o2_in)));
282 }
Damiena3dcd9e2013-12-17 21:35:38 +0000283 }
284}
Damien George23005372014-01-13 19:39:01 +0000285MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
286
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200287STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
Damiena3dcd9e2013-12-17 21:35:38 +0000288 // TODO hash will generally overflow small integer; can we safely truncate it?
Damiend99b0522013-12-21 18:17:45 +0000289 return mp_obj_new_int(mp_obj_hash(o_in));
Damiena3dcd9e2013-12-17 21:35:38 +0000290}
Damiend99b0522013-12-21 18:17:45 +0000291MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash);
292
Damien George58051112014-04-15 12:42:52 +0100293STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) {
Damien Georgeb013aea2014-04-15 12:50:21 +0100294 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 +0100295}
Damien George58051112014-04-15 12:42:52 +0100296MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex);
297
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200298STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
Damien Georged17926d2014-03-30 13:35:08 +0100299 return mp_getiter(o_in);
Damiena3dcd9e2013-12-17 21:35:38 +0000300}
Damiend99b0522013-12-21 18:17:45 +0000301MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
Damiena3dcd9e2013-12-17 21:35:38 +0000302
Damien Georged182b982014-08-30 14:19:41 +0100303STATIC 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 +0100304 mp_map_elem_t *key_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP);
305 mp_obj_t key_fn = key_elem == NULL ? MP_OBJ_NULL : key_elem->value;
Damiena3dcd9e2013-12-17 21:35:38 +0000306 if (n_args == 1) {
307 // given an iterable
Damien Georged17926d2014-03-30 13:35:08 +0100308 mp_obj_t iterable = mp_getiter(args[0]);
Damien George7310fd42014-08-24 19:14:09 +0100309 mp_obj_t best_key = MP_OBJ_NULL;
310 mp_obj_t best_obj = MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000311 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100312 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George7310fd42014-08-24 19:14:09 +0100313 mp_obj_t key = key_fn == MP_OBJ_NULL ? item : mp_call_function_1(key_fn, item);
314 if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) {
315 best_key = key;
316 best_obj = item;
Damiena3dcd9e2013-12-17 21:35:38 +0000317 }
318 }
Damien George7310fd42014-08-24 19:14:09 +0100319 if (best_obj == MP_OBJ_NULL) {
320 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "arg is an empty sequence"));
Damiena3dcd9e2013-12-17 21:35:38 +0000321 }
Damien George7310fd42014-08-24 19:14:09 +0100322 return best_obj;
Damiena3dcd9e2013-12-17 21:35:38 +0000323 } else {
324 // given many args
Damien George7310fd42014-08-24 19:14:09 +0100325 mp_obj_t best_key = MP_OBJ_NULL;
326 mp_obj_t best_obj = MP_OBJ_NULL;
327 for (mp_uint_t i = 0; i < n_args; i++) {
328 mp_obj_t key = key_fn == MP_OBJ_NULL ? args[i] : mp_call_function_1(key_fn, args[i]);
329 if (best_obj == MP_OBJ_NULL || (mp_binary_op(op, key, best_key) == mp_const_true)) {
330 best_key = key;
331 best_obj = args[i];
Damiena3dcd9e2013-12-17 21:35:38 +0000332 }
333 }
Damien George7310fd42014-08-24 19:14:09 +0100334 return best_obj;
Damiena3dcd9e2013-12-17 21:35:38 +0000335 }
336}
337
Damien Georged182b982014-08-30 14:19:41 +0100338STATIC 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 +0100339 return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_MORE);
Damiena3dcd9e2013-12-17 21:35:38 +0000340}
Damien George7310fd42014-08-24 19:14:09 +0100341MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_max_obj, 1, mp_builtin_max);
Damiena3dcd9e2013-12-17 21:35:38 +0000342
Damien Georged182b982014-08-30 14:19:41 +0100343STATIC 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 +0100344 return mp_builtin_min_max(n_args, args, kwargs, MP_BINARY_OP_LESS);
345}
346MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_min_obj, 1, mp_builtin_min);
Damien George23005372014-01-13 19:39:01 +0000347
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200348STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
Damien Georged17926d2014-03-30 13:35:08 +0100349 mp_obj_t ret = mp_iternext_allow_raise(o);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100350 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100351 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damiend9d62012013-12-21 18:38:03 +0000352 } else {
353 return ret;
354 }
Damiend99b0522013-12-21 18:17:45 +0000355}
Damiend99b0522013-12-21 18:17:45 +0000356MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
357
Damien George897fe0c2014-04-15 22:03:55 +0100358STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) {
359 return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in);
360}
Damien George897fe0c2014-04-15 22:03:55 +0100361MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
362
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200363STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
Damien Georged182b982014-08-30 14:19:41 +0100364 mp_uint_t len;
Damien George698ec212014-02-08 18:17:23 +0000365 const char *str = mp_obj_str_get_data(o_in, &len);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300366 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien George1e9a92f2014-11-06 17:36:16 +0000367 len = unichar_charlen(str, len);
368 if (len == 1) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000369 if (MP_OBJ_IS_STR(o_in) && UTF8_IS_NONASCII(*str)) {
Damien George40f3c022014-07-03 13:25:24 +0100370 mp_int_t ord = *str++ & 0x7F;
371 for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
Chris Angelico9a1a4be2014-06-04 05:28:12 +1000372 ord &= ~mask;
373 }
374 while (UTF8_IS_CONT(*str)) {
375 ord = (ord << 6) | (*str++ & 0x3F);
376 }
377 return mp_obj_new_int(ord);
378 } else {
379 return mp_obj_new_int(((const byte*)str)[0]);
380 }
Damiena3dcd9e2013-12-17 21:35:38 +0000381 }
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300382 #else
383 if (len == 1) {
384 // don't sign extend when converting to ord
385 return mp_obj_new_int(((const byte*)str)[0]);
Paul Sokolovsky42a52512014-06-13 02:39:37 +0300386 }
387 #endif
Damien George1e9a92f2014-11-06 17:36:16 +0000388
389 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
390 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
391 "ord expects a character"));
392 } else {
393 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
394 "ord() expected a character, but string of length %d found", len));
395 }
Damiena3dcd9e2013-12-17 21:35:38 +0000396}
Damien George23005372014-01-13 19:39:01 +0000397MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
398
Damien Georged182b982014-08-30 14:19:41 +0100399STATIC mp_obj_t mp_builtin_pow(mp_uint_t n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000400 assert(2 <= n_args && n_args <= 3);
Damiena3dcd9e2013-12-17 21:35:38 +0000401 switch (n_args) {
Damien Georged17926d2014-03-30 13:35:08 +0100402 case 2: return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]);
403 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 +0000404 }
405}
Damien George23005372014-01-13 19:39:01 +0000406MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
407
Damien Georged182b982014-08-30 14:19:41 +0100408STATIC 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 +0100409 mp_map_elem_t *sep_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_MAP_LOOKUP);
410 mp_map_elem_t *end_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_end), MP_MAP_LOOKUP);
411 const char *sep_data = " ";
Damien Georged182b982014-08-30 14:19:41 +0100412 mp_uint_t sep_len = 1;
Damien George48815662014-04-02 10:34:44 +0100413 const char *end_data = "\n";
Damien Georged182b982014-08-30 14:19:41 +0100414 mp_uint_t end_len = 1;
Damien George48815662014-04-02 10:34:44 +0100415 if (sep_elem != NULL && sep_elem->value != mp_const_none) {
416 sep_data = mp_obj_str_get_data(sep_elem->value, &sep_len);
417 }
418 if (end_elem != NULL && end_elem->value != mp_const_none) {
419 end_data = mp_obj_str_get_data(end_elem->value, &end_len);
420 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300421 #if MICROPY_PY_IO
422 mp_obj_t stream_obj = &mp_sys_stdout_obj;
423 mp_map_elem_t *file_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_file), MP_MAP_LOOKUP);
424 if (file_elem != NULL && file_elem->value != mp_const_none) {
425 stream_obj = file_elem->value;
426 }
427
428 pfenv_t pfenv;
429 pfenv.data = stream_obj;
Damien George42f3de92014-10-03 17:44:14 +0000430 pfenv.print_strn = (void (*)(void *, const char *, mp_uint_t))mp_stream_write;
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300431 #endif
Damien George42f3de92014-10-03 17:44:14 +0000432 for (mp_uint_t i = 0; i < n_args; i++) {
Damiena3dcd9e2013-12-17 21:35:38 +0000433 if (i > 0) {
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300434 #if MICROPY_PY_IO
435 mp_stream_write(stream_obj, sep_data, sep_len);
436 #else
Damien George4abff752014-08-30 14:59:21 +0100437 printf("%.*s", (int)sep_len, sep_data);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300438 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000439 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300440 #if MICROPY_PY_IO
441 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))pfenv_printf, &pfenv, args[i], PRINT_STR);
442 #else
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200443 mp_obj_print(args[i], PRINT_STR);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300444 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000445 }
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300446 #if MICROPY_PY_IO
447 mp_stream_write(stream_obj, end_data, end_len);
448 #else
Damien George4abff752014-08-30 14:59:21 +0100449 printf("%.*s", (int)end_len, end_data);
Paul Sokolovskycb66f412014-07-13 23:07:42 +0300450 #endif
Damiend99b0522013-12-21 18:17:45 +0000451 return mp_const_none;
Damiena3dcd9e2013-12-17 21:35:38 +0000452}
Damien George48815662014-04-02 10:34:44 +0100453MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
Damien George23005372014-01-13 19:39:01 +0000454
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200455STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000456 vstr_t *vstr = vstr_new();
Damien George4899ff92014-01-15 22:39:03 +0000457 mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in, PRINT_REPR);
Damien George2617eeb2014-05-25 22:27:57 +0100458 mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
Damien George5fa93b62014-01-22 14:35:10 +0000459 vstr_free(vstr);
460 return s;
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
Damien George1559a972014-10-31 11:28:50 +0000464STATIC mp_obj_t mp_builtin_round(mp_obj_t o_in) {
465 // TODO support second arg
466 if (MP_OBJ_IS_INT(o_in)) {
467 return o_in;
468 }
469#if MICROPY_PY_BUILTINS_FLOAT
470 mp_float_t val = mp_obj_get_float(o_in);
471 mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val);
472 mp_int_t r = rounded;
473 // make rounded value even if it was halfway between ints
474 if (val - rounded == 0.5) {
475 r = (r + 1) & (~1);
476 } else if (val - rounded == -0.5) {
477 r &= ~1;
478 }
479#else
480 mp_int_t r = mp_obj_get_int(o_in);
481#endif
482 return mp_obj_new_int(r);
483}
484MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_round_obj, mp_builtin_round);
485
Damien Georged182b982014-08-30 14:19:41 +0100486STATIC mp_obj_t mp_builtin_sum(mp_uint_t n_args, const mp_obj_t *args) {
Damien George23005372014-01-13 19:39:01 +0000487 assert(1 <= n_args && n_args <= 2);
Damiend99b0522013-12-21 18:17:45 +0000488 mp_obj_t value;
Damiena3dcd9e2013-12-17 21:35:38 +0000489 switch (n_args) {
Damiend99b0522013-12-21 18:17:45 +0000490 case 1: value = mp_obj_new_int(0); break;
Damien George23005372014-01-13 19:39:01 +0000491 default: value = args[1]; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000492 }
Damien Georged17926d2014-03-30 13:35:08 +0100493 mp_obj_t iterable = mp_getiter(args[0]);
Damiend99b0522013-12-21 18:17:45 +0000494 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100495 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien Georged17926d2014-03-30 13:35:08 +0100496 value = mp_binary_op(MP_BINARY_OP_ADD, value, item);
Damiena3dcd9e2013-12-17 21:35:38 +0000497 }
498 return value;
499}
Damien George23005372014-01-13 19:39:01 +0000500MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
John R. Lenton5c768392014-01-13 05:12:50 +0000501
Damien Georged182b982014-08-30 14:19:41 +0100502STATIC 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 +0000503 assert(n_args >= 1);
504 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100505 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lenton5c768392014-01-13 05:12:50 +0000506 "must use keyword argument for key function"));
507 }
Damien George3e1a5c12014-03-29 13:43:38 +0000508 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 +0000509 mp_obj_list_sort(1, &self, kwargs);
John R. Lenton5c768392014-01-13 05:12:50 +0000510
511 return self;
512}
John R. Lenton88cb1e62014-01-13 19:55:18 +0000513MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200514
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300515// See mp_load_attr() if making any changes
516STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) {
517 mp_obj_t dest[2];
518 // use load_method, raising or not raising exception
519 ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest);
520 if (dest[0] == MP_OBJ_NULL) {
521 return defval;
522 } else if (dest[1] == MP_OBJ_NULL) {
523 // load_method returned just a normal attribute
524 return dest[0];
525 } else {
526 // load_method returned a method, so build a bound method object
527 return mp_obj_new_bound_meth(dest[0], dest[1]);
528 }
529}
530
Damien Georged182b982014-08-30 14:19:41 +0100531STATIC mp_obj_t mp_builtin_getattr(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300532 mp_obj_t attr = args[1];
533 if (MP_OBJ_IS_TYPE(attr, &mp_type_str)) {
534 attr = mp_obj_str_intern(attr);
535 } else if (!MP_OBJ_IS_QSTR(attr)) {
536 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "string required"));
537 }
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300538 mp_obj_t defval = MP_OBJ_NULL;
539 if (n_args > 2) {
540 defval = args[2];
541 }
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300542 return mp_load_attr_default(args[0], MP_OBJ_QSTR_VALUE(attr), defval);
Paul Sokolovskye9137b92014-03-26 23:35:13 +0200543}
Paul Sokolovskybfb7d6a2014-04-05 13:33:04 +0300544MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr);
Paul Sokolovskycc0af3d2014-04-06 01:00:46 +0300545
Paul Sokolovskyff306662014-05-11 19:05:42 +0300546STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
547 assert(MP_OBJ_IS_QSTR(attr_in));
548
549 mp_obj_t dest[2];
Chris Angelicodaf973a2014-06-06 03:51:03 +1000550 // TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
Paul Sokolovskyff306662014-05-11 19:05:42 +0300551 // explicitly says "This is implemented by calling getattr(object, name) and seeing
552 // whether it raises an AttributeError or not.", so we should explicitly wrap this
553 // in nlr_push and handle exception.
554 mp_load_method_maybe(object_in, MP_OBJ_QSTR_VALUE(attr_in), dest);
555
556 return MP_BOOL(dest[0] != MP_OBJ_NULL);
557}
Paul Sokolovskyff306662014-05-11 19:05:42 +0300558MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
559
Damien George4c03b3a2014-08-12 18:33:40 +0100560// These are defined in terms of MicroPython API functions right away
Damien Georgec7687ad2014-08-22 21:48:30 +0100561MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_obj_id);
Damien George4c03b3a2014-08-12 18:33:40 +0100562MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_obj_len);
Paul Sokolovsky080d99b2014-04-06 01:18:19 +0300563MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
564MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);