blob: e58330a9d59da1e5e8b7f64b94ed65652e14834c [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
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/nlr.h"
33#include "py/obj.h"
34#include "py/objtype.h"
35#include "py/objint.h"
36#include "py/runtime0.h"
37#include "py/runtime.h"
38#include "py/stackctrl.h"
Damien660365e2013-12-17 18:27:24 +000039
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +030040mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
Damien Georgeb97669a2014-01-08 11:47:55 +000041 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien George3e1a5c12014-03-29 13:43:38 +000042 return (mp_obj_t)&mp_type_int;
Damien George38a2da62014-01-08 17:33:12 +000043 } else if (MP_OBJ_IS_QSTR(o_in)) {
Damien George3e1a5c12014-03-29 13:43:38 +000044 return (mp_obj_t)&mp_type_str;
Damien Georgeb97669a2014-01-08 11:47:55 +000045 } else {
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +030046 const mp_obj_base_t *o = o_in;
Damien Georgeb97669a2014-01-08 11:47:55 +000047 return (mp_obj_t)o->type;
48 }
49}
50
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +030051const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
Damien Georgea71c83a2014-02-15 11:34:50 +000052 return qstr_str(mp_obj_get_type(o_in)->name);
Damien660365e2013-12-17 18:27:24 +000053}
54
55void printf_wrapper(void *env, const char *fmt, ...) {
56 va_list args;
57 va_start(args, fmt);
58 vprintf(fmt, args);
59 va_end(args);
60}
61
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020062void 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 +030063 // There can be data structures nested too deep, or just recursive
Paul Sokolovskycaa73342014-07-01 02:13:42 +030064 MP_STACK_CHECK();
Paul Sokolovsky0bc15942014-05-10 21:05:45 +030065#if !NDEBUG
66 if (o_in == NULL) {
67 print(env, "(nil)");
68 return;
69 }
70#endif
Damien George5fa93b62014-01-22 14:35:10 +000071 mp_obj_type_t *type = mp_obj_get_type(o_in);
72 if (type->print != NULL) {
73 type->print(print, env, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000074 } else {
Damien Georgea71c83a2014-02-15 11:34:50 +000075 print(env, "<%s>", qstr_str(type->name));
Damien660365e2013-12-17 18:27:24 +000076 }
77}
78
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020079void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
80 mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000081}
82
Damien George136b1492014-01-19 12:38:49 +000083// helper function to print an exception with traceback
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +020084void mp_obj_print_exception(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t exc) {
Damien Georgec5966122014-02-15 16:10:44 +000085 if (mp_obj_is_exception_instance(exc)) {
Damien George40f3c022014-07-03 13:25:24 +010086 mp_uint_t n, *values;
Damien George136b1492014-01-19 12:38:49 +000087 mp_obj_exception_get_traceback(exc, &n, &values);
88 if (n > 0) {
Paul Sokolovsky0ae518f2014-03-30 02:08:36 +020089 assert(n % 3 == 0);
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +020090 print(env, "Traceback (most recent call last):\n");
Damien George136b1492014-01-19 12:38:49 +000091 for (int i = n - 3; i >= 0; i -= 3) {
Damien George62ad1892014-01-29 21:51:51 +000092#if MICROPY_ENABLE_SOURCE_LINE
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +020093 print(env, " File \"%s\", line %d", qstr_str(values[i]), (int)values[i + 1]);
Damien George62ad1892014-01-29 21:51:51 +000094#else
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +020095 print(env, " File \"%s\"", qstr_str(values[i]));
Damien George62ad1892014-01-29 21:51:51 +000096#endif
Damien George0e4ba252014-04-13 15:01:28 +010097 // the block name can be NULL if it's unknown
98 qstr block = values[i + 2];
99 if (block == MP_QSTR_NULL) {
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200100 print(env, "\n");
Damien George0e4ba252014-04-13 15:01:28 +0100101 } else {
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200102 print(env, ", in %s\n", qstr_str(block));
Damien George0e4ba252014-04-13 15:01:28 +0100103 }
Damien George136b1492014-01-19 12:38:49 +0000104 }
105 }
106 }
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200107 mp_obj_print_helper(print, env, exc, PRINT_EXC);
108 print(env, "\n");
Damien George136b1492014-01-19 12:38:49 +0000109}
110
Damien George4d917232014-08-30 14:28:06 +0100111bool mp_obj_is_true(mp_obj_t arg) {
Damien Georged17926d2014-03-30 13:35:08 +0100112 if (arg == mp_const_false) {
113 return 0;
114 } else if (arg == mp_const_true) {
115 return 1;
116 } else if (arg == mp_const_none) {
117 return 0;
118 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
119 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
120 return 0;
121 } else {
122 return 1;
123 }
124 } else {
125 mp_obj_type_t *type = mp_obj_get_type(arg);
126 if (type->unary_op != NULL) {
127 mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg);
Damien George6ac5dce2014-05-21 19:42:43 +0100128 if (result != MP_OBJ_NULL) {
Damien Georged17926d2014-03-30 13:35:08 +0100129 return result == mp_const_true;
130 }
131 }
132
133 mp_obj_t len = mp_obj_len_maybe(arg);
134 if (len != MP_OBJ_NULL) {
135 // obj has a length, truth determined if len != 0
136 return len != MP_OBJ_NEW_SMALL_INT(0);
137 } else {
138 // any other obj is true per Python semantics
139 return 1;
140 }
141 }
142}
143
Damiend99b0522013-12-21 18:17:45 +0000144bool mp_obj_is_callable(mp_obj_t o_in) {
Damien George0344fa12014-11-03 16:09:39 +0000145 mp_call_fun_t call = mp_obj_get_type(o_in)->call;
146 if (call != mp_obj_instance_call) {
147 return call != NULL;
148 }
149 return mp_obj_instance_is_callable(o_in);
Damien660365e2013-12-17 18:27:24 +0000150}
151
Damien George40f3c022014-07-03 13:25:24 +0100152mp_int_t mp_obj_hash(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000153 if (o_in == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000154 return 0; // needs to hash to same as the integer 0, since False==0
Damiend99b0522013-12-21 18:17:45 +0000155 } else if (o_in == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000156 return 1; // needs to hash to same as the integer 1, since True==1
Damiend99b0522013-12-21 18:17:45 +0000157 } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
158 return MP_OBJ_SMALL_INT_VALUE(o_in);
Damien Georgeffe911d2014-07-24 14:21:37 +0100159 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_int)) {
160 return mp_obj_int_hash(o_in);
Paul Sokolovskyf130ca12014-04-13 05:41:00 +0300161 } else if (MP_OBJ_IS_STR(o_in) || MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
Damien George5fa93b62014-01-22 14:35:10 +0000162 return mp_obj_str_get_hash(o_in);
Damien George07ddab52014-03-29 13:15:08 +0000163 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_NoneType)) {
Damien George40f3c022014-07-03 13:25:24 +0100164 return (mp_int_t)o_in;
Damien George3c658a42014-08-24 16:28:17 +0100165 } else if (MP_OBJ_IS_FUN(o_in)) {
Damien George40f3c022014-07-03 13:25:24 +0100166 return (mp_int_t)o_in;
Damien George07ddab52014-03-29 13:15:08 +0000167 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_tuple)) {
Damien George7f8be592014-03-20 19:20:59 +0000168 return mp_obj_tuple_hash(o_in);
Paul Sokolovsky91cbe602014-04-05 12:50:43 +0300169 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_type)) {
Damien George40f3c022014-07-03 13:25:24 +0100170 return (mp_int_t)o_in;
stijne00eeaf2014-11-12 14:57:34 +0100171 } else if (MP_OBJ_IS_OBJ(o_in)) {
172 // if a valid __hash__ method exists, use it
173 mp_obj_t hash_method[2];
174 mp_load_method_maybe(o_in, MP_QSTR___hash__, hash_method);
175 if (hash_method[0] != MP_OBJ_NULL) {
176 mp_obj_t hash_val = mp_call_method_n_kw(0, 0, hash_method);
177 if (MP_OBJ_IS_INT(hash_val)) {
Damien Georgebe6d8be2014-12-05 23:13:52 +0000178 return mp_obj_int_get_truncated(hash_val);
stijne00eeaf2014-11-12 14:57:34 +0100179 }
Damien George1e9a92f2014-11-06 17:36:16 +0000180 }
Damien660365e2013-12-17 18:27:24 +0000181 }
stijne00eeaf2014-11-12 14:57:34 +0100182
183 // TODO hash class and instances - in CPython by default user created classes' __hash__ resolves to their id
184
185 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
186 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "unhashable type"));
187 } else {
188 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
189 "unhashable type: '%s'", mp_obj_get_type_str(o_in)));
190 }
Damien660365e2013-12-17 18:27:24 +0000191}
192
Damien Georgec38dc3c2015-01-11 15:13:18 +0000193// This function implements the '==' operator (and so the inverse of '!=').
194//
195// From the Python language reference:
196// (https://docs.python.org/3/reference/expressions.html#not-in)
Damien660365e2013-12-17 18:27:24 +0000197// "The objects need not have the same type. If both are numbers, they are converted
198// to a common type. Otherwise, the == and != operators always consider objects of
199// different types to be unequal."
Damien Georgec38dc3c2015-01-11 15:13:18 +0000200//
201// This means that False==0 and True==1 are true expressions.
202//
203// Furthermore, from the v3.4.2 code for object.c: "Practical amendments: If rich
204// comparison returns NotImplemented, == and != are decided by comparing the object
205// pointer."
Damiend99b0522013-12-21 18:17:45 +0000206bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
Damien660365e2013-12-17 18:27:24 +0000207 if (o1 == o2) {
208 return true;
Damien Georgee22d76e2014-04-11 10:52:06 +0000209 }
210 if (o1 == mp_const_none || o2 == mp_const_none) {
Damien Georgee0f29792014-03-30 23:16:42 +0100211 return false;
Damien Georgee22d76e2014-04-11 10:52:06 +0000212 }
213
214 // fast path for small ints
215 if (MP_OBJ_IS_SMALL_INT(o1)) {
216 if (MP_OBJ_IS_SMALL_INT(o2)) {
217 // both SMALL_INT, and not equal if we get here
Damien660365e2013-12-17 18:27:24 +0000218 return false;
219 } else {
Damien Georgee22d76e2014-04-11 10:52:06 +0000220 mp_obj_t temp = o2; o2 = o1; o1 = temp;
221 // o2 is now the SMALL_INT, o1 is not
Damien Georgeb8a053a2014-04-11 10:10:37 +0100222 // fall through to generic op
Damien660365e2013-12-17 18:27:24 +0000223 }
Damien Georgee22d76e2014-04-11 10:52:06 +0000224 }
225
226 // fast path for strings
227 if (MP_OBJ_IS_STR(o1)) {
228 if (MP_OBJ_IS_STR(o2)) {
229 // both strings, use special function
230 return mp_obj_str_equal(o1, o2);
231 } else {
232 // a string is never equal to anything else
233 return false;
234 }
235 } else if (MP_OBJ_IS_STR(o2)) {
236 // o1 is not a string (else caught above), so the objects are not equal
237 return false;
Damien660365e2013-12-17 18:27:24 +0000238 }
Damien Georgeb8a053a2014-04-11 10:10:37 +0100239
240 // generic type, call binary_op(MP_BINARY_OP_EQUAL)
241 mp_obj_type_t *type = mp_obj_get_type(o1);
242 if (type->binary_op != NULL) {
243 mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
Damien George6ac5dce2014-05-21 19:42:43 +0100244 if (r != MP_OBJ_NULL) {
Damien Georgeb8a053a2014-04-11 10:10:37 +0100245 return r == mp_const_true ? true : false;
246 }
247 }
248
Damien Georgec38dc3c2015-01-11 15:13:18 +0000249 // equality not implemented, and objects are not the same object, so
250 // they are defined as not equal
251 return false;
Damien660365e2013-12-17 18:27:24 +0000252}
253
Damien George40f3c022014-07-03 13:25:24 +0100254mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
Paul Sokolovskye99841b2014-04-05 17:46:47 +0300255 // This function essentially performs implicit type conversion to int
256 // Note that Python does NOT provide implicit type conversion from
257 // float to int in the core expression language, try some_list[1.0].
Damiend99b0522013-12-21 18:17:45 +0000258 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000259 return 0;
Damiend99b0522013-12-21 18:17:45 +0000260 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000261 return 1;
Damiend99b0522013-12-21 18:17:45 +0000262 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
263 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George3e1a5c12014-03-29 13:43:38 +0000264 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200265 return mp_obj_int_get_checked(arg);
Damien660365e2013-12-17 18:27:24 +0000266 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000267 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
268 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
269 "can't convert to int"));
270 } else {
271 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
272 "can't convert %s to int", mp_obj_get_type_str(arg)));
273 }
Damien660365e2013-12-17 18:27:24 +0000274 }
275}
276
Damien George8270e382014-04-03 11:00:54 +0000277// returns false if arg is not of integral type
278// returns true and sets *value if it is of integral type
Damien George40f3c022014-07-03 13:25:24 +0100279// can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t
280bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) {
Damien George8270e382014-04-03 11:00:54 +0000281 if (arg == mp_const_false) {
282 *value = 0;
283 } else if (arg == mp_const_true) {
284 *value = 1;
285 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
286 *value = MP_OBJ_SMALL_INT_VALUE(arg);
287 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
288 *value = mp_obj_int_get_checked(arg);
289 } else {
290 return false;
291 }
292 return true;
293}
294
Damien Georgefb510b32014-06-01 13:32:54 +0100295#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000296mp_float_t mp_obj_get_float(mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +0000297 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000298 return 0;
Damiend99b0522013-12-21 18:17:45 +0000299 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000300 return 1;
Damiend99b0522013-12-21 18:17:45 +0000301 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
302 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George3e1a5c12014-03-29 13:43:38 +0000303 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
Damien Georgeeabdf672014-03-22 20:54:01 +0000304 return mp_obj_int_as_float(arg);
Damien George0c36da02014-03-08 15:24:39 +0000305 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000306 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000307 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000308 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
309 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
310 "can't convert to float"));
311 } else {
312 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
313 "can't convert %s to float", mp_obj_get_type_str(arg)));
314 }
Damien660365e2013-12-17 18:27:24 +0000315 }
316}
317
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300318#if MICROPY_PY_BUILTINS_COMPLEX
Damiend99b0522013-12-21 18:17:45 +0000319void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
320 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000321 *real = 0;
322 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000323 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000324 *real = 1;
325 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000326 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
327 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000328 *imag = 0;
Damien George0aa5d512014-03-29 17:28:20 +0000329 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
330 *real = mp_obj_int_as_float(arg);
331 *imag = 0;
Damien George0c36da02014-03-08 15:24:39 +0000332 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000333 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000334 *imag = 0;
Damien George0c36da02014-03-08 15:24:39 +0000335 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000336 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000337 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000338 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
339 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
340 "can't convert to complex"));
341 } else {
342 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
343 "can't convert %s to complex", mp_obj_get_type_str(arg)));
344 }
Damien660365e2013-12-17 18:27:24 +0000345 }
346}
347#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300348#endif
Damien660365e2013-12-17 18:27:24 +0000349
Damien George9c4cbe22014-08-30 14:04:14 +0100350void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items) {
Damien George07ddab52014-03-29 13:15:08 +0000351 if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
Damien George24ff0632014-03-24 10:47:13 +0000352 mp_obj_tuple_get(o, len, items);
Damien George3e1a5c12014-03-29 13:43:38 +0000353 } else if (MP_OBJ_IS_TYPE(o, &mp_type_list)) {
Damien George24ff0632014-03-24 10:47:13 +0000354 mp_obj_list_get(o, len, items);
Damien660365e2013-12-17 18:27:24 +0000355 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000356 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
357 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
358 "expected tuple/list"));
359 } else {
360 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
361 "object '%s' is not a tuple or list", mp_obj_get_type_str(o)));
362 }
Damien George24ff0632014-03-24 10:47:13 +0000363 }
364}
365
Damien George9c4cbe22014-08-30 14:04:14 +0100366void 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 +0100367 mp_uint_t seq_len;
368 mp_obj_get_array(o, &seq_len, items);
369 if (seq_len != len) {
Damien George1e9a92f2014-11-06 17:36:16 +0000370 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
371 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
372 "tuple/list has wrong length"));
373 } else {
374 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
375 "requested length %d but object has length %d", len, seq_len));
376 }
Damien660365e2013-12-17 18:27:24 +0000377 }
378}
379
xbe9e1e8cd2014-03-12 22:57:16 -0700380// is_slice determines whether the index is a slice index
Damien George9c4cbe22014-08-30 14:04:14 +0100381mp_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 +0100382 mp_int_t i;
Damien Georgea9ddd6d2014-04-11 10:40:38 +0000383 if (MP_OBJ_IS_SMALL_INT(index)) {
384 i = MP_OBJ_SMALL_INT_VALUE(index);
385 } else if (!mp_obj_get_int_maybe(index, &i)) {
Damien George1e9a92f2014-11-06 17:36:16 +0000386 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
387 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
388 "indices must be integers"));
389 } else {
390 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
391 "%s indices must be integers, not %s",
392 qstr_str(type->name), mp_obj_get_type_str(index)));
393 }
Damien660365e2013-12-17 18:27:24 +0000394 }
xbe9e1e8cd2014-03-12 22:57:16 -0700395
396 if (i < 0) {
xbec5d70ba2014-03-13 00:29:15 -0700397 i += len;
xbe9e1e8cd2014-03-12 22:57:16 -0700398 }
399 if (is_slice) {
xbec5d70ba2014-03-13 00:29:15 -0700400 if (i < 0) {
401 i = 0;
402 } else if (i > len) {
403 i = len;
404 }
xbe9e1e8cd2014-03-12 22:57:16 -0700405 } else {
xbec5d70ba2014-03-13 00:29:15 -0700406 if (i < 0 || i >= len) {
Damien George1e9a92f2014-11-06 17:36:16 +0000407 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
408 nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "index out of range"));
409 } else {
410 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError,
411 "%s index out of range", qstr_str(type->name)));
412 }
xbec5d70ba2014-03-13 00:29:15 -0700413 }
xbe9e1e8cd2014-03-12 22:57:16 -0700414 }
415 return i;
Damien660365e2013-12-17 18:27:24 +0000416}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000417
Damien Georgec7687ad2014-08-22 21:48:30 +0100418mp_obj_t mp_obj_id(mp_obj_t o_in) {
419 mp_int_t id = (mp_int_t)o_in;
420 if (!MP_OBJ_IS_OBJ(o_in)) {
421 return mp_obj_new_int(id);
422 } else if (id >= 0) {
423 // Many OSes and CPUs have affinity for putting "user" memories
424 // into low half of address space, and "system" into upper half.
425 // We're going to take advantage of that and return small int
426 // (signed) for such "user" addresses.
427 return MP_OBJ_NEW_SMALL_INT(id);
428 } else {
429 // If that didn't work, well, let's return long int, just as
430 // a (big) positve value, so it will never clash with the range
431 // of small int returned in previous case.
432 return mp_obj_new_int_from_uint((mp_uint_t)id);
433 }
434}
435
Damien George4c03b3a2014-08-12 18:33:40 +0100436// will raise a TypeError if object has no length
437mp_obj_t mp_obj_len(mp_obj_t o_in) {
438 mp_obj_t len = mp_obj_len_maybe(o_in);
439 if (len == MP_OBJ_NULL) {
Damien George1e9a92f2014-11-06 17:36:16 +0000440 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
441 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
442 "object has no len"));
443 } else {
444 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
445 "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
446 }
Damien George4c03b3a2014-08-12 18:33:40 +0100447 } else {
448 return len;
449 }
450}
451
Damien Georgeeae16442014-01-11 19:22:29 +0000452// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000453mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300454 if (
455#if !MICROPY_PY_BUILTINS_STR_UNICODE
456 // It's simple - unicode is slow, non-unicode is fast
457 MP_OBJ_IS_STR(o_in) ||
458#endif
459 MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
Damien Georgebb4c6f32014-07-31 10:49:14 +0100460 return MP_OBJ_NEW_SMALL_INT(mp_obj_str_get_len(o_in));
John R. Lenton4bee76e2014-01-10 11:25:03 +0000461 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200462 mp_obj_type_t *type = mp_obj_get_type(o_in);
463 if (type->unary_op != NULL) {
Damien George6ac5dce2014-05-21 19:42:43 +0100464 return type->unary_op(MP_UNARY_OP_LEN, o_in);
Damien George09a0c642014-01-30 10:05:33 +0000465 } else {
466 return MP_OBJ_NULL;
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200467 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000468 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000469}
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200470
Damien George729f7b42014-04-17 22:10:53 +0100471mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
472 mp_obj_type_t *type = mp_obj_get_type(base);
473 if (type->subscr != NULL) {
474 mp_obj_t ret = type->subscr(base, index, value);
Damien George6ac5dce2014-05-21 19:42:43 +0100475 if (ret != MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100476 return ret;
477 }
478 // TODO: call base classes here?
479 }
480 if (value == MP_OBJ_NULL) {
Damien George1e9a92f2014-11-06 17:36:16 +0000481 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
482 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
483 "object does not support item deletion"));
484 } else {
485 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
486 "'%s' object does not support item deletion", mp_obj_get_type_str(base)));
487 }
Damien George729f7b42014-04-17 22:10:53 +0100488 } else if (value == MP_OBJ_SENTINEL) {
Damien George1e9a92f2014-11-06 17:36:16 +0000489 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
490 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
491 "object is not subscriptable"));
492 } else {
493 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
494 "'%s' object is not subscriptable", mp_obj_get_type_str(base)));
495 }
Damien George729f7b42014-04-17 22:10:53 +0100496 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000497 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
498 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
499 "object does not support item assignment"));
500 } else {
501 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
502 "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
503 }
Damien George729f7b42014-04-17 22:10:53 +0100504 }
505}
506
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200507// Return input argument. Useful as .getiter for objects which are
508// their own iterators, etc.
509mp_obj_t mp_identity(mp_obj_t self) {
510 return self;
511}
Paul Sokolovsky557c9d52014-02-08 21:57:19 +0200512MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300513
Damien George4d917232014-08-30 14:28:06 +0100514bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
Damien George8a1cab92014-04-13 12:08:52 +0100515 mp_obj_type_t *type = mp_obj_get_type(obj);
516 if (type->buffer_p.get_buffer == NULL) {
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300517 return false;
518 }
Damien Georgeb11b85a2014-04-18 22:59:24 +0100519 int ret = type->buffer_p.get_buffer(obj, bufinfo, flags);
Paul Sokolovsky7133d912014-08-10 11:46:10 +0300520 if (ret != 0) {
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300521 return false;
522 }
523 return true;
524}
525
Damien George4d917232014-08-30 14:28:06 +0100526void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
Damien Georgeb11b85a2014-04-18 22:59:24 +0100527 if (!mp_get_buffer(obj, bufinfo, flags)) {
Damien George57a4b4f2014-04-18 22:29:21 +0100528 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object with buffer protocol required"));
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300529 }
530}