blob: c869a65465e2deba488a2802bda8042793ad2774 [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
Damien Georgeffe911d2014-07-24 14:21:37 +010027#include <stdint.h>
Damien660365e2013-12-17 18:27:24 +000028#include <stdio.h>
29#include <stdarg.h>
30#include <assert.h>
31
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030032#include "mpconfig.h"
Damien660365e2013-12-17 18:27:24 +000033#include "nlr.h"
34#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000035#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000036#include "obj.h"
Damien George0344fa12014-11-03 16:09:39 +000037#include "objtype.h"
Damien Georgeffe911d2014-07-24 14:21:37 +010038#include "mpz.h"
39#include "objint.h"
Damiend99b0522013-12-21 18:17:45 +000040#include "runtime0.h"
41#include "runtime.h"
Paul Sokolovsky8993fb62014-06-28 02:25:04 +030042#include "stackctrl.h"
Damien660365e2013-12-17 18:27:24 +000043
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +030044mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
Damien Georgeb97669a2014-01-08 11:47:55 +000045 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien George3e1a5c12014-03-29 13:43:38 +000046 return (mp_obj_t)&mp_type_int;
Damien George38a2da62014-01-08 17:33:12 +000047 } else if (MP_OBJ_IS_QSTR(o_in)) {
Damien George3e1a5c12014-03-29 13:43:38 +000048 return (mp_obj_t)&mp_type_str;
Damien Georgeb97669a2014-01-08 11:47:55 +000049 } else {
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +030050 const mp_obj_base_t *o = o_in;
Damien Georgeb97669a2014-01-08 11:47:55 +000051 return (mp_obj_t)o->type;
52 }
53}
54
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +030055const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
Damien Georgea71c83a2014-02-15 11:34:50 +000056 return qstr_str(mp_obj_get_type(o_in)->name);
Damien660365e2013-12-17 18:27:24 +000057}
58
59void printf_wrapper(void *env, const char *fmt, ...) {
60 va_list args;
61 va_start(args, fmt);
62 vprintf(fmt, args);
63 va_end(args);
64}
65
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020066void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Paul Sokolovsky8993fb62014-06-28 02:25:04 +030067 // There can be data structures nested too deep, or just recursive
Paul Sokolovskycaa73342014-07-01 02:13:42 +030068 MP_STACK_CHECK();
Paul Sokolovsky0bc15942014-05-10 21:05:45 +030069#if !NDEBUG
70 if (o_in == NULL) {
71 print(env, "(nil)");
72 return;
73 }
74#endif
Damien George5fa93b62014-01-22 14:35:10 +000075 mp_obj_type_t *type = mp_obj_get_type(o_in);
76 if (type->print != NULL) {
77 type->print(print, env, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000078 } else {
Damien Georgea71c83a2014-02-15 11:34:50 +000079 print(env, "<%s>", qstr_str(type->name));
Damien660365e2013-12-17 18:27:24 +000080 }
81}
82
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020083void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
84 mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000085}
86
Damien George136b1492014-01-19 12:38:49 +000087// helper function to print an exception with traceback
88void mp_obj_print_exception(mp_obj_t exc) {
Damien Georgec5966122014-02-15 16:10:44 +000089 if (mp_obj_is_exception_instance(exc)) {
Damien George40f3c022014-07-03 13:25:24 +010090 mp_uint_t n, *values;
Damien George136b1492014-01-19 12:38:49 +000091 mp_obj_exception_get_traceback(exc, &n, &values);
92 if (n > 0) {
Paul Sokolovsky0ae518f2014-03-30 02:08:36 +020093 assert(n % 3 == 0);
Damien George136b1492014-01-19 12:38:49 +000094 printf("Traceback (most recent call last):\n");
95 for (int i = n - 3; i >= 0; i -= 3) {
Damien George62ad1892014-01-29 21:51:51 +000096#if MICROPY_ENABLE_SOURCE_LINE
Damien George0e4ba252014-04-13 15:01:28 +010097 printf(" File \"%s\", line %d", qstr_str(values[i]), (int)values[i + 1]);
Damien George62ad1892014-01-29 21:51:51 +000098#else
Damien George0e4ba252014-04-13 15:01:28 +010099 printf(" File \"%s\"", qstr_str(values[i]));
Damien George62ad1892014-01-29 21:51:51 +0000100#endif
Damien George0e4ba252014-04-13 15:01:28 +0100101 // the block name can be NULL if it's unknown
102 qstr block = values[i + 2];
103 if (block == MP_QSTR_NULL) {
104 printf("\n");
105 } else {
106 printf(", in %s\n", qstr_str(block));
107 }
Damien George136b1492014-01-19 12:38:49 +0000108 }
109 }
110 }
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300111 mp_obj_print(exc, PRINT_EXC);
Damien George136b1492014-01-19 12:38:49 +0000112 printf("\n");
113}
114
Damien George4d917232014-08-30 14:28:06 +0100115bool mp_obj_is_true(mp_obj_t arg) {
Damien Georged17926d2014-03-30 13:35:08 +0100116 if (arg == mp_const_false) {
117 return 0;
118 } else if (arg == mp_const_true) {
119 return 1;
120 } else if (arg == mp_const_none) {
121 return 0;
122 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
123 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
124 return 0;
125 } else {
126 return 1;
127 }
128 } else {
129 mp_obj_type_t *type = mp_obj_get_type(arg);
130 if (type->unary_op != NULL) {
131 mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg);
Damien George6ac5dce2014-05-21 19:42:43 +0100132 if (result != MP_OBJ_NULL) {
Damien Georged17926d2014-03-30 13:35:08 +0100133 return result == mp_const_true;
134 }
135 }
136
137 mp_obj_t len = mp_obj_len_maybe(arg);
138 if (len != MP_OBJ_NULL) {
139 // obj has a length, truth determined if len != 0
140 return len != MP_OBJ_NEW_SMALL_INT(0);
141 } else {
142 // any other obj is true per Python semantics
143 return 1;
144 }
145 }
146}
147
Damiend99b0522013-12-21 18:17:45 +0000148bool mp_obj_is_callable(mp_obj_t o_in) {
Damien George0344fa12014-11-03 16:09:39 +0000149 mp_call_fun_t call = mp_obj_get_type(o_in)->call;
150 if (call != mp_obj_instance_call) {
151 return call != NULL;
152 }
153 return mp_obj_instance_is_callable(o_in);
Damien660365e2013-12-17 18:27:24 +0000154}
155
Damien George40f3c022014-07-03 13:25:24 +0100156mp_int_t mp_obj_hash(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000157 if (o_in == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000158 return 0; // needs to hash to same as the integer 0, since False==0
Damiend99b0522013-12-21 18:17:45 +0000159 } else if (o_in == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000160 return 1; // needs to hash to same as the integer 1, since True==1
Damiend99b0522013-12-21 18:17:45 +0000161 } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
162 return MP_OBJ_SMALL_INT_VALUE(o_in);
Damien Georgeffe911d2014-07-24 14:21:37 +0100163 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_int)) {
164 return mp_obj_int_hash(o_in);
Paul Sokolovskyf130ca12014-04-13 05:41:00 +0300165 } else if (MP_OBJ_IS_STR(o_in) || MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
Damien George5fa93b62014-01-22 14:35:10 +0000166 return mp_obj_str_get_hash(o_in);
Damien George07ddab52014-03-29 13:15:08 +0000167 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_NoneType)) {
Damien George40f3c022014-07-03 13:25:24 +0100168 return (mp_int_t)o_in;
Damien George3c658a42014-08-24 16:28:17 +0100169 } else if (MP_OBJ_IS_FUN(o_in)) {
Damien George40f3c022014-07-03 13:25:24 +0100170 return (mp_int_t)o_in;
Damien George07ddab52014-03-29 13:15:08 +0000171 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_tuple)) {
Damien George7f8be592014-03-20 19:20:59 +0000172 return mp_obj_tuple_hash(o_in);
Paul Sokolovsky91cbe602014-04-05 12:50:43 +0300173 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_type)) {
Damien George40f3c022014-07-03 13:25:24 +0100174 return (mp_int_t)o_in;
stijne00eeaf2014-11-12 14:57:34 +0100175 } else if (MP_OBJ_IS_OBJ(o_in)) {
176 // if a valid __hash__ method exists, use it
177 mp_obj_t hash_method[2];
178 mp_load_method_maybe(o_in, MP_QSTR___hash__, hash_method);
179 if (hash_method[0] != MP_OBJ_NULL) {
180 mp_obj_t hash_val = mp_call_method_n_kw(0, 0, hash_method);
181 if (MP_OBJ_IS_INT(hash_val)) {
Damien Georgebe6d8be2014-12-05 23:13:52 +0000182 return mp_obj_int_get_truncated(hash_val);
stijne00eeaf2014-11-12 14:57:34 +0100183 }
Damien George1e9a92f2014-11-06 17:36:16 +0000184 }
Damien660365e2013-12-17 18:27:24 +0000185 }
stijne00eeaf2014-11-12 14:57:34 +0100186
187 // TODO hash class and instances - in CPython by default user created classes' __hash__ resolves to their id
188
189 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
190 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "unhashable type"));
191 } else {
192 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
193 "unhashable type: '%s'", mp_obj_get_type_str(o_in)));
194 }
Damien660365e2013-12-17 18:27:24 +0000195}
196
197// this function implements the '==' operator (and so the inverse of '!=')
198// from the python language reference:
199// "The objects need not have the same type. If both are numbers, they are converted
200// to a common type. Otherwise, the == and != operators always consider objects of
201// different types to be unequal."
202// note also that False==0 and True==1 are true expressions
Damiend99b0522013-12-21 18:17:45 +0000203bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
Damien660365e2013-12-17 18:27:24 +0000204 if (o1 == o2) {
205 return true;
Damien Georgee22d76e2014-04-11 10:52:06 +0000206 }
207 if (o1 == mp_const_none || o2 == mp_const_none) {
Damien Georgee0f29792014-03-30 23:16:42 +0100208 return false;
Damien Georgee22d76e2014-04-11 10:52:06 +0000209 }
210
211 // fast path for small ints
212 if (MP_OBJ_IS_SMALL_INT(o1)) {
213 if (MP_OBJ_IS_SMALL_INT(o2)) {
214 // both SMALL_INT, and not equal if we get here
Damien660365e2013-12-17 18:27:24 +0000215 return false;
216 } else {
Damien Georgee22d76e2014-04-11 10:52:06 +0000217 mp_obj_t temp = o2; o2 = o1; o1 = temp;
218 // o2 is now the SMALL_INT, o1 is not
Damien Georgeb8a053a2014-04-11 10:10:37 +0100219 // fall through to generic op
Damien660365e2013-12-17 18:27:24 +0000220 }
Damien Georgee22d76e2014-04-11 10:52:06 +0000221 }
222
223 // fast path for strings
224 if (MP_OBJ_IS_STR(o1)) {
225 if (MP_OBJ_IS_STR(o2)) {
226 // both strings, use special function
227 return mp_obj_str_equal(o1, o2);
228 } else {
229 // a string is never equal to anything else
230 return false;
231 }
232 } else if (MP_OBJ_IS_STR(o2)) {
233 // o1 is not a string (else caught above), so the objects are not equal
234 return false;
Damien660365e2013-12-17 18:27:24 +0000235 }
Damien Georgeb8a053a2014-04-11 10:10:37 +0100236
237 // generic type, call binary_op(MP_BINARY_OP_EQUAL)
238 mp_obj_type_t *type = mp_obj_get_type(o1);
239 if (type->binary_op != NULL) {
240 mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
Damien George6ac5dce2014-05-21 19:42:43 +0100241 if (r != MP_OBJ_NULL) {
Damien Georgeb8a053a2014-04-11 10:10:37 +0100242 return r == mp_const_true ? true : false;
243 }
244 }
245
Damien George1e9a92f2014-11-06 17:36:16 +0000246 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
247 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
248 "equality for given types not yet implemented"));
249 } else {
250 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NotImplementedError,
251 "equality for '%s' and '%s' types not yet implemented",
252 mp_obj_get_type_str(o1), mp_obj_get_type_str(o2)));
253 }
Damien660365e2013-12-17 18:27:24 +0000254}
255
Damien George40f3c022014-07-03 13:25:24 +0100256mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
Paul Sokolovskye99841b2014-04-05 17:46:47 +0300257 // This function essentially performs implicit type conversion to int
258 // Note that Python does NOT provide implicit type conversion from
259 // float to int in the core expression language, try some_list[1.0].
Damiend99b0522013-12-21 18:17:45 +0000260 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000261 return 0;
Damiend99b0522013-12-21 18:17:45 +0000262 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000263 return 1;
Damiend99b0522013-12-21 18:17:45 +0000264 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
265 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George3e1a5c12014-03-29 13:43:38 +0000266 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200267 return mp_obj_int_get_checked(arg);
Damien660365e2013-12-17 18:27:24 +0000268 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000269 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
270 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
271 "can't convert to int"));
272 } else {
273 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
274 "can't convert %s to int", mp_obj_get_type_str(arg)));
275 }
Damien660365e2013-12-17 18:27:24 +0000276 }
277}
278
Damien George8270e382014-04-03 11:00:54 +0000279// returns false if arg is not of integral type
280// returns true and sets *value if it is of integral type
Damien George40f3c022014-07-03 13:25:24 +0100281// can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t
282bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) {
Damien George8270e382014-04-03 11:00:54 +0000283 if (arg == mp_const_false) {
284 *value = 0;
285 } else if (arg == mp_const_true) {
286 *value = 1;
287 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
288 *value = MP_OBJ_SMALL_INT_VALUE(arg);
289 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
290 *value = mp_obj_int_get_checked(arg);
291 } else {
292 return false;
293 }
294 return true;
295}
296
Damien Georgefb510b32014-06-01 13:32:54 +0100297#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000298mp_float_t mp_obj_get_float(mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +0000299 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000300 return 0;
Damiend99b0522013-12-21 18:17:45 +0000301 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000302 return 1;
Damiend99b0522013-12-21 18:17:45 +0000303 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
304 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George3e1a5c12014-03-29 13:43:38 +0000305 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
Damien Georgeeabdf672014-03-22 20:54:01 +0000306 return mp_obj_int_as_float(arg);
Damien George0c36da02014-03-08 15:24:39 +0000307 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000308 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000309 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000310 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
311 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
312 "can't convert to float"));
313 } else {
314 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
315 "can't convert %s to float", mp_obj_get_type_str(arg)));
316 }
Damien660365e2013-12-17 18:27:24 +0000317 }
318}
319
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300320#if MICROPY_PY_BUILTINS_COMPLEX
Damiend99b0522013-12-21 18:17:45 +0000321void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
322 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000323 *real = 0;
324 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000325 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000326 *real = 1;
327 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000328 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
329 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000330 *imag = 0;
Damien George0aa5d512014-03-29 17:28:20 +0000331 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
332 *real = mp_obj_int_as_float(arg);
333 *imag = 0;
Damien George0c36da02014-03-08 15:24:39 +0000334 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000335 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000336 *imag = 0;
Damien George0c36da02014-03-08 15:24:39 +0000337 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000338 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000339 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000340 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
341 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
342 "can't convert to complex"));
343 } else {
344 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
345 "can't convert %s to complex", mp_obj_get_type_str(arg)));
346 }
Damien660365e2013-12-17 18:27:24 +0000347 }
348}
349#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300350#endif
Damien660365e2013-12-17 18:27:24 +0000351
Damien George9c4cbe22014-08-30 14:04:14 +0100352void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items) {
Damien George07ddab52014-03-29 13:15:08 +0000353 if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
Damien George24ff0632014-03-24 10:47:13 +0000354 mp_obj_tuple_get(o, len, items);
Damien George3e1a5c12014-03-29 13:43:38 +0000355 } else if (MP_OBJ_IS_TYPE(o, &mp_type_list)) {
Damien George24ff0632014-03-24 10:47:13 +0000356 mp_obj_list_get(o, len, items);
Damien660365e2013-12-17 18:27:24 +0000357 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000358 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
359 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
360 "expected tuple/list"));
361 } else {
362 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
363 "object '%s' is not a tuple or list", mp_obj_get_type_str(o)));
364 }
Damien George24ff0632014-03-24 10:47:13 +0000365 }
366}
367
Damien George9c4cbe22014-08-30 14:04:14 +0100368void mp_obj_get_array_fixed_n(mp_obj_t o, mp_uint_t len, mp_obj_t **items) {
Damien Georgeca6d75f2014-08-30 15:17:47 +0100369 mp_uint_t seq_len;
370 mp_obj_get_array(o, &seq_len, items);
371 if (seq_len != len) {
Damien George1e9a92f2014-11-06 17:36:16 +0000372 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
373 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
374 "tuple/list has wrong length"));
375 } else {
376 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
377 "requested length %d but object has length %d", len, seq_len));
378 }
Damien660365e2013-12-17 18:27:24 +0000379 }
380}
381
xbe9e1e8cd2014-03-12 22:57:16 -0700382// is_slice determines whether the index is a slice index
Damien George9c4cbe22014-08-30 14:04:14 +0100383mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice) {
Damien George40f3c022014-07-03 13:25:24 +0100384 mp_int_t i;
Damien Georgea9ddd6d2014-04-11 10:40:38 +0000385 if (MP_OBJ_IS_SMALL_INT(index)) {
386 i = MP_OBJ_SMALL_INT_VALUE(index);
387 } else if (!mp_obj_get_int_maybe(index, &i)) {
Damien George1e9a92f2014-11-06 17:36:16 +0000388 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
389 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
390 "indices must be integers"));
391 } else {
392 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
393 "%s indices must be integers, not %s",
394 qstr_str(type->name), mp_obj_get_type_str(index)));
395 }
Damien660365e2013-12-17 18:27:24 +0000396 }
xbe9e1e8cd2014-03-12 22:57:16 -0700397
398 if (i < 0) {
xbec5d70ba2014-03-13 00:29:15 -0700399 i += len;
xbe9e1e8cd2014-03-12 22:57:16 -0700400 }
401 if (is_slice) {
xbec5d70ba2014-03-13 00:29:15 -0700402 if (i < 0) {
403 i = 0;
404 } else if (i > len) {
405 i = len;
406 }
xbe9e1e8cd2014-03-12 22:57:16 -0700407 } else {
xbec5d70ba2014-03-13 00:29:15 -0700408 if (i < 0 || i >= len) {
Damien George1e9a92f2014-11-06 17:36:16 +0000409 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
410 nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "index out of range"));
411 } else {
412 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError,
413 "%s index out of range", qstr_str(type->name)));
414 }
xbec5d70ba2014-03-13 00:29:15 -0700415 }
xbe9e1e8cd2014-03-12 22:57:16 -0700416 }
417 return i;
Damien660365e2013-12-17 18:27:24 +0000418}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000419
Damien Georgec7687ad2014-08-22 21:48:30 +0100420mp_obj_t mp_obj_id(mp_obj_t o_in) {
421 mp_int_t id = (mp_int_t)o_in;
422 if (!MP_OBJ_IS_OBJ(o_in)) {
423 return mp_obj_new_int(id);
424 } else if (id >= 0) {
425 // Many OSes and CPUs have affinity for putting "user" memories
426 // into low half of address space, and "system" into upper half.
427 // We're going to take advantage of that and return small int
428 // (signed) for such "user" addresses.
429 return MP_OBJ_NEW_SMALL_INT(id);
430 } else {
431 // If that didn't work, well, let's return long int, just as
432 // a (big) positve value, so it will never clash with the range
433 // of small int returned in previous case.
434 return mp_obj_new_int_from_uint((mp_uint_t)id);
435 }
436}
437
Damien George4c03b3a2014-08-12 18:33:40 +0100438// will raise a TypeError if object has no length
439mp_obj_t mp_obj_len(mp_obj_t o_in) {
440 mp_obj_t len = mp_obj_len_maybe(o_in);
441 if (len == MP_OBJ_NULL) {
Damien George1e9a92f2014-11-06 17:36:16 +0000442 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
443 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
444 "object has no len"));
445 } else {
446 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
447 "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
448 }
Damien George4c03b3a2014-08-12 18:33:40 +0100449 } else {
450 return len;
451 }
452}
453
Damien Georgeeae16442014-01-11 19:22:29 +0000454// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000455mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300456 if (
457#if !MICROPY_PY_BUILTINS_STR_UNICODE
458 // It's simple - unicode is slow, non-unicode is fast
459 MP_OBJ_IS_STR(o_in) ||
460#endif
461 MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
Damien Georgebb4c6f32014-07-31 10:49:14 +0100462 return MP_OBJ_NEW_SMALL_INT(mp_obj_str_get_len(o_in));
John R. Lenton4bee76e2014-01-10 11:25:03 +0000463 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200464 mp_obj_type_t *type = mp_obj_get_type(o_in);
465 if (type->unary_op != NULL) {
Damien George6ac5dce2014-05-21 19:42:43 +0100466 return type->unary_op(MP_UNARY_OP_LEN, o_in);
Damien George09a0c642014-01-30 10:05:33 +0000467 } else {
468 return MP_OBJ_NULL;
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200469 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000470 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000471}
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200472
Damien George729f7b42014-04-17 22:10:53 +0100473mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
474 mp_obj_type_t *type = mp_obj_get_type(base);
475 if (type->subscr != NULL) {
476 mp_obj_t ret = type->subscr(base, index, value);
Damien George6ac5dce2014-05-21 19:42:43 +0100477 if (ret != MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100478 return ret;
479 }
480 // TODO: call base classes here?
481 }
482 if (value == MP_OBJ_NULL) {
Damien George1e9a92f2014-11-06 17:36:16 +0000483 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
484 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
485 "object does not support item deletion"));
486 } else {
487 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
488 "'%s' object does not support item deletion", mp_obj_get_type_str(base)));
489 }
Damien George729f7b42014-04-17 22:10:53 +0100490 } else if (value == MP_OBJ_SENTINEL) {
Damien George1e9a92f2014-11-06 17:36:16 +0000491 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
492 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
493 "object is not subscriptable"));
494 } else {
495 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
496 "'%s' object is not subscriptable", mp_obj_get_type_str(base)));
497 }
Damien George729f7b42014-04-17 22:10:53 +0100498 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000499 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
500 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
501 "object does not support item assignment"));
502 } else {
503 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
504 "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
505 }
Damien George729f7b42014-04-17 22:10:53 +0100506 }
507}
508
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200509// Return input argument. Useful as .getiter for objects which are
510// their own iterators, etc.
511mp_obj_t mp_identity(mp_obj_t self) {
512 return self;
513}
Paul Sokolovsky557c9d52014-02-08 21:57:19 +0200514MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300515
Damien George4d917232014-08-30 14:28:06 +0100516bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
Damien George8a1cab92014-04-13 12:08:52 +0100517 mp_obj_type_t *type = mp_obj_get_type(obj);
518 if (type->buffer_p.get_buffer == NULL) {
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300519 return false;
520 }
Damien Georgeb11b85a2014-04-18 22:59:24 +0100521 int ret = type->buffer_p.get_buffer(obj, bufinfo, flags);
Paul Sokolovsky7133d912014-08-10 11:46:10 +0300522 if (ret != 0) {
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300523 return false;
524 }
525 return true;
526}
527
Damien George4d917232014-08-30 14:28:06 +0100528void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
Damien Georgeb11b85a2014-04-18 22:59:24 +0100529 if (!mp_get_buffer(obj, bufinfo, flags)) {
Damien George57a4b4f2014-04-18 22:29:21 +0100530 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object with buffer protocol required"));
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300531 }
532}