blob: a0f55d65db783cdb0903cf8ac509102856514d42 [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>
28#include <stdarg.h>
29#include <assert.h>
30
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030031#include "mpconfig.h"
Damien660365e2013-12-17 18:27:24 +000032#include "nlr.h"
33#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000034#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000035#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +000036#include "runtime0.h"
37#include "runtime.h"
Damien660365e2013-12-17 18:27:24 +000038
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +030039mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
Damien Georgeb97669a2014-01-08 11:47:55 +000040 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien George3e1a5c12014-03-29 13:43:38 +000041 return (mp_obj_t)&mp_type_int;
Damien George38a2da62014-01-08 17:33:12 +000042 } else if (MP_OBJ_IS_QSTR(o_in)) {
Damien George3e1a5c12014-03-29 13:43:38 +000043 return (mp_obj_t)&mp_type_str;
Damien Georgeb97669a2014-01-08 11:47:55 +000044 } else {
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +030045 const mp_obj_base_t *o = o_in;
Damien Georgeb97669a2014-01-08 11:47:55 +000046 return (mp_obj_t)o->type;
47 }
48}
49
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +030050const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
Damien Georgea71c83a2014-02-15 11:34:50 +000051 return qstr_str(mp_obj_get_type(o_in)->name);
Damien660365e2013-12-17 18:27:24 +000052}
53
54void printf_wrapper(void *env, const char *fmt, ...) {
55 va_list args;
56 va_start(args, fmt);
57 vprintf(fmt, args);
58 va_end(args);
59}
60
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020061void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Paul Sokolovsky0bc15942014-05-10 21:05:45 +030062#if !NDEBUG
63 if (o_in == NULL) {
64 print(env, "(nil)");
65 return;
66 }
67#endif
Damien George5fa93b62014-01-22 14:35:10 +000068 mp_obj_type_t *type = mp_obj_get_type(o_in);
69 if (type->print != NULL) {
70 type->print(print, env, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000071 } else {
Damien Georgea71c83a2014-02-15 11:34:50 +000072 print(env, "<%s>", qstr_str(type->name));
Damien660365e2013-12-17 18:27:24 +000073 }
74}
75
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020076void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
77 mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000078}
79
Damien George136b1492014-01-19 12:38:49 +000080// helper function to print an exception with traceback
81void mp_obj_print_exception(mp_obj_t exc) {
Damien Georgec5966122014-02-15 16:10:44 +000082 if (mp_obj_is_exception_instance(exc)) {
Damien George136b1492014-01-19 12:38:49 +000083 machine_uint_t n, *values;
84 mp_obj_exception_get_traceback(exc, &n, &values);
85 if (n > 0) {
Paul Sokolovsky0ae518f2014-03-30 02:08:36 +020086 assert(n % 3 == 0);
Damien George136b1492014-01-19 12:38:49 +000087 printf("Traceback (most recent call last):\n");
88 for (int i = n - 3; i >= 0; i -= 3) {
Damien George62ad1892014-01-29 21:51:51 +000089#if MICROPY_ENABLE_SOURCE_LINE
Damien George0e4ba252014-04-13 15:01:28 +010090 printf(" File \"%s\", line %d", qstr_str(values[i]), (int)values[i + 1]);
Damien George62ad1892014-01-29 21:51:51 +000091#else
Damien George0e4ba252014-04-13 15:01:28 +010092 printf(" File \"%s\"", qstr_str(values[i]));
Damien George62ad1892014-01-29 21:51:51 +000093#endif
Damien George0e4ba252014-04-13 15:01:28 +010094 // the block name can be NULL if it's unknown
95 qstr block = values[i + 2];
96 if (block == MP_QSTR_NULL) {
97 printf("\n");
98 } else {
99 printf(", in %s\n", qstr_str(block));
100 }
Damien George136b1492014-01-19 12:38:49 +0000101 }
102 }
103 }
Paul Sokolovskya96d3d02014-03-31 01:10:10 +0300104 mp_obj_print(exc, PRINT_EXC);
Damien George136b1492014-01-19 12:38:49 +0000105 printf("\n");
106}
107
Damien Georged17926d2014-03-30 13:35:08 +0100108int mp_obj_is_true(mp_obj_t arg) {
109 if (arg == mp_const_false) {
110 return 0;
111 } else if (arg == mp_const_true) {
112 return 1;
113 } else if (arg == mp_const_none) {
114 return 0;
115 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
116 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
117 return 0;
118 } else {
119 return 1;
120 }
121 } else {
122 mp_obj_type_t *type = mp_obj_get_type(arg);
123 if (type->unary_op != NULL) {
124 mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg);
Damien George6ac5dce2014-05-21 19:42:43 +0100125 if (result != MP_OBJ_NULL) {
Damien Georged17926d2014-03-30 13:35:08 +0100126 return result == mp_const_true;
127 }
128 }
129
130 mp_obj_t len = mp_obj_len_maybe(arg);
131 if (len != MP_OBJ_NULL) {
132 // obj has a length, truth determined if len != 0
133 return len != MP_OBJ_NEW_SMALL_INT(0);
134 } else {
135 // any other obj is true per Python semantics
136 return 1;
137 }
138 }
139}
140
Damiend99b0522013-12-21 18:17:45 +0000141bool mp_obj_is_callable(mp_obj_t o_in) {
Damien Georgeb051e7d2014-01-23 18:13:53 +0000142 return mp_obj_get_type(o_in)->call != NULL;
Damien660365e2013-12-17 18:27:24 +0000143}
144
Damiend99b0522013-12-21 18:17:45 +0000145machine_int_t mp_obj_hash(mp_obj_t o_in) {
146 if (o_in == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000147 return 0; // needs to hash to same as the integer 0, since False==0
Damiend99b0522013-12-21 18:17:45 +0000148 } else if (o_in == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000149 return 1; // needs to hash to same as the integer 1, since True==1
Damiend99b0522013-12-21 18:17:45 +0000150 } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
151 return MP_OBJ_SMALL_INT_VALUE(o_in);
Paul Sokolovskyf130ca12014-04-13 05:41:00 +0300152 } else if (MP_OBJ_IS_STR(o_in) || MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
Damien George5fa93b62014-01-22 14:35:10 +0000153 return mp_obj_str_get_hash(o_in);
Damien George07ddab52014-03-29 13:15:08 +0000154 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_NoneType)) {
Damien660365e2013-12-17 18:27:24 +0000155 return (machine_int_t)o_in;
Damien George3e1a5c12014-03-29 13:43:38 +0000156 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_fun_native) || MP_OBJ_IS_TYPE(o_in, &mp_type_fun_bc)) {
Damien George7f8be592014-03-20 19:20:59 +0000157 return (machine_int_t)o_in;
Damien George07ddab52014-03-29 13:15:08 +0000158 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_tuple)) {
Damien George7f8be592014-03-20 19:20:59 +0000159 return mp_obj_tuple_hash(o_in);
Paul Sokolovsky91cbe602014-04-05 12:50:43 +0300160 } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_type)) {
161 return (machine_int_t)o_in;
Damien George7f8be592014-03-20 19:20:59 +0000162
163 // TODO hash class and instances
164 // TODO delegate to __hash__ method if it exists
165
Damien660365e2013-12-17 18:27:24 +0000166 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100167 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "unhashable type: '%s'", mp_obj_get_type_str(o_in)));
Damien660365e2013-12-17 18:27:24 +0000168 }
169}
170
171// this function implements the '==' operator (and so the inverse of '!=')
172// from the python language reference:
173// "The objects need not have the same type. If both are numbers, they are converted
174// to a common type. Otherwise, the == and != operators always consider objects of
175// different types to be unequal."
176// note also that False==0 and True==1 are true expressions
Damiend99b0522013-12-21 18:17:45 +0000177bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
Damien660365e2013-12-17 18:27:24 +0000178 if (o1 == o2) {
179 return true;
Damien Georgee22d76e2014-04-11 10:52:06 +0000180 }
181 if (o1 == mp_const_none || o2 == mp_const_none) {
Damien Georgee0f29792014-03-30 23:16:42 +0100182 return false;
Damien Georgee22d76e2014-04-11 10:52:06 +0000183 }
184
185 // fast path for small ints
186 if (MP_OBJ_IS_SMALL_INT(o1)) {
187 if (MP_OBJ_IS_SMALL_INT(o2)) {
188 // both SMALL_INT, and not equal if we get here
Damien660365e2013-12-17 18:27:24 +0000189 return false;
190 } else {
Damien Georgee22d76e2014-04-11 10:52:06 +0000191 mp_obj_t temp = o2; o2 = o1; o1 = temp;
192 // o2 is now the SMALL_INT, o1 is not
Damien Georgeb8a053a2014-04-11 10:10:37 +0100193 // fall through to generic op
Damien660365e2013-12-17 18:27:24 +0000194 }
Damien Georgee22d76e2014-04-11 10:52:06 +0000195 }
196
197 // fast path for strings
198 if (MP_OBJ_IS_STR(o1)) {
199 if (MP_OBJ_IS_STR(o2)) {
200 // both strings, use special function
201 return mp_obj_str_equal(o1, o2);
202 } else {
203 // a string is never equal to anything else
204 return false;
205 }
206 } else if (MP_OBJ_IS_STR(o2)) {
207 // o1 is not a string (else caught above), so the objects are not equal
208 return false;
Damien660365e2013-12-17 18:27:24 +0000209 }
Damien Georgeb8a053a2014-04-11 10:10:37 +0100210
211 // generic type, call binary_op(MP_BINARY_OP_EQUAL)
212 mp_obj_type_t *type = mp_obj_get_type(o1);
213 if (type->binary_op != NULL) {
214 mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
Damien George6ac5dce2014-05-21 19:42:43 +0100215 if (r != MP_OBJ_NULL) {
Damien Georgeb8a053a2014-04-11 10:10:37 +0100216 return r == mp_const_true ? true : false;
217 }
218 }
219
220 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NotImplementedError,
221 "Equality for '%s' and '%s' types not yet implemented", mp_obj_get_type_str(o1), mp_obj_get_type_str(o2)));
222 return false;
Damien660365e2013-12-17 18:27:24 +0000223}
224
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300225machine_int_t mp_obj_get_int(mp_const_obj_t arg) {
Paul Sokolovskye99841b2014-04-05 17:46:47 +0300226 // This function essentially performs implicit type conversion to int
227 // Note that Python does NOT provide implicit type conversion from
228 // float to int in the core expression language, try some_list[1.0].
Damiend99b0522013-12-21 18:17:45 +0000229 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000230 return 0;
Damiend99b0522013-12-21 18:17:45 +0000231 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000232 return 1;
Damiend99b0522013-12-21 18:17:45 +0000233 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
234 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George3e1a5c12014-03-29 13:43:38 +0000235 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200236 return mp_obj_int_get_checked(arg);
Damien660365e2013-12-17 18:27:24 +0000237 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100238 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000239 }
240}
241
Damien George8270e382014-04-03 11:00:54 +0000242// returns false if arg is not of integral type
243// returns true and sets *value if it is of integral type
244// can throw OverflowError if arg is of integral type, but doesn't fit in a machine_int_t
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300245bool mp_obj_get_int_maybe(mp_const_obj_t arg, machine_int_t *value) {
Damien George8270e382014-04-03 11:00:54 +0000246 if (arg == mp_const_false) {
247 *value = 0;
248 } else if (arg == mp_const_true) {
249 *value = 1;
250 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
251 *value = MP_OBJ_SMALL_INT_VALUE(arg);
252 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
253 *value = mp_obj_int_get_checked(arg);
254 } else {
255 return false;
256 }
257 return true;
258}
259
Damien Georgefb510b32014-06-01 13:32:54 +0100260#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000261mp_float_t mp_obj_get_float(mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +0000262 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000263 return 0;
Damiend99b0522013-12-21 18:17:45 +0000264 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000265 return 1;
Damiend99b0522013-12-21 18:17:45 +0000266 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
267 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George3e1a5c12014-03-29 13:43:38 +0000268 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
Damien Georgeeabdf672014-03-22 20:54:01 +0000269 return mp_obj_int_as_float(arg);
Damien George0c36da02014-03-08 15:24:39 +0000270 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000271 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000272 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100273 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to float", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000274 }
275}
276
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300277#if MICROPY_PY_BUILTINS_COMPLEX
Damiend99b0522013-12-21 18:17:45 +0000278void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
279 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000280 *real = 0;
281 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000282 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000283 *real = 1;
284 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000285 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
286 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000287 *imag = 0;
Damien George0aa5d512014-03-29 17:28:20 +0000288 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
289 *real = mp_obj_int_as_float(arg);
290 *imag = 0;
Damien George0c36da02014-03-08 15:24:39 +0000291 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
Damiend99b0522013-12-21 18:17:45 +0000292 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000293 *imag = 0;
Damien George0c36da02014-03-08 15:24:39 +0000294 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000295 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000296 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100297 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't convert %s to complex", mp_obj_get_type_str(arg)));
Damien660365e2013-12-17 18:27:24 +0000298 }
299}
300#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300301#endif
Damien660365e2013-12-17 18:27:24 +0000302
Damien George24ff0632014-03-24 10:47:13 +0000303void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items) {
Damien George07ddab52014-03-29 13:15:08 +0000304 if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
Damien George24ff0632014-03-24 10:47:13 +0000305 mp_obj_tuple_get(o, len, items);
Damien George3e1a5c12014-03-29 13:43:38 +0000306 } else if (MP_OBJ_IS_TYPE(o, &mp_type_list)) {
Damien George24ff0632014-03-24 10:47:13 +0000307 mp_obj_list_get(o, len, items);
Damien660365e2013-12-17 18:27:24 +0000308 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100309 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "object '%s' is not a tuple or list", mp_obj_get_type_str(o)));
Damien George24ff0632014-03-24 10:47:13 +0000310 }
311}
312
313void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items) {
Damien George3e1a5c12014-03-29 13:43:38 +0000314 if (MP_OBJ_IS_TYPE(o, &mp_type_tuple) || MP_OBJ_IS_TYPE(o, &mp_type_list)) {
Damien George24ff0632014-03-24 10:47:13 +0000315 uint seq_len;
Damien George07ddab52014-03-29 13:15:08 +0000316 if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
Damien George24ff0632014-03-24 10:47:13 +0000317 mp_obj_tuple_get(o, &seq_len, items);
318 } else {
319 mp_obj_list_get(o, &seq_len, items);
320 }
321 if (seq_len != len) {
Damien George686afc52014-04-11 09:13:30 +0100322 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "requested length %d but object has length %d", len, seq_len));
Damien George24ff0632014-03-24 10:47:13 +0000323 }
324 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100325 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "object '%s' is not a tuple or list", mp_obj_get_type_str(o)));
Damien660365e2013-12-17 18:27:24 +0000326 }
327}
328
xbe9e1e8cd2014-03-12 22:57:16 -0700329// is_slice determines whether the index is a slice index
330uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice) {
Damien Georgea9ddd6d2014-04-11 10:40:38 +0000331 machine_int_t i;
332 if (MP_OBJ_IS_SMALL_INT(index)) {
333 i = MP_OBJ_SMALL_INT_VALUE(index);
334 } else if (!mp_obj_get_int_maybe(index, &i)) {
Damien Georgeea13f402014-04-05 18:32:08 +0100335 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "%s indices must be integers, not %s", qstr_str(type->name), mp_obj_get_type_str(index)));
Damien660365e2013-12-17 18:27:24 +0000336 }
xbe9e1e8cd2014-03-12 22:57:16 -0700337
338 if (i < 0) {
xbec5d70ba2014-03-13 00:29:15 -0700339 i += len;
xbe9e1e8cd2014-03-12 22:57:16 -0700340 }
341 if (is_slice) {
xbec5d70ba2014-03-13 00:29:15 -0700342 if (i < 0) {
343 i = 0;
344 } else if (i > len) {
345 i = len;
346 }
xbe9e1e8cd2014-03-12 22:57:16 -0700347 } else {
xbec5d70ba2014-03-13 00:29:15 -0700348 if (i < 0 || i >= len) {
Damien Georgeea13f402014-04-05 18:32:08 +0100349 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "%s index out of range", qstr_str(type->name)));
xbec5d70ba2014-03-13 00:29:15 -0700350 }
xbe9e1e8cd2014-03-12 22:57:16 -0700351 }
352 return i;
Damien660365e2013-12-17 18:27:24 +0000353}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000354
Damien Georgeeae16442014-01-11 19:22:29 +0000355// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000356mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
Damien Georgeee014112014-04-15 23:10:00 +0100357 if (MP_OBJ_IS_STR(o_in) || MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
Damien George09a0c642014-01-30 10:05:33 +0000358 return MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_str_get_len(o_in));
John R. Lenton4bee76e2014-01-10 11:25:03 +0000359 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200360 mp_obj_type_t *type = mp_obj_get_type(o_in);
361 if (type->unary_op != NULL) {
Damien George6ac5dce2014-05-21 19:42:43 +0100362 return type->unary_op(MP_UNARY_OP_LEN, o_in);
Damien George09a0c642014-01-30 10:05:33 +0000363 } else {
364 return MP_OBJ_NULL;
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200365 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000366 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000367}
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200368
Damien George729f7b42014-04-17 22:10:53 +0100369mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
370 mp_obj_type_t *type = mp_obj_get_type(base);
371 if (type->subscr != NULL) {
372 mp_obj_t ret = type->subscr(base, index, value);
Damien George6ac5dce2014-05-21 19:42:43 +0100373 if (ret != MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100374 return ret;
375 }
376 // TODO: call base classes here?
377 }
378 if (value == MP_OBJ_NULL) {
379 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object does not support item deletion", mp_obj_get_type_str(base)));
380 } else if (value == MP_OBJ_SENTINEL) {
381 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not subscriptable", mp_obj_get_type_str(base)));
382 } else {
383 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
384 }
385}
386
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200387// Return input argument. Useful as .getiter for objects which are
388// their own iterators, etc.
389mp_obj_t mp_identity(mp_obj_t self) {
390 return self;
391}
Paul Sokolovsky557c9d52014-02-08 21:57:19 +0200392MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300393
Damien Georgeb11b85a2014-04-18 22:59:24 +0100394bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags) {
Damien George8a1cab92014-04-13 12:08:52 +0100395 mp_obj_type_t *type = mp_obj_get_type(obj);
396 if (type->buffer_p.get_buffer == NULL) {
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300397 return false;
398 }
Damien Georgeb11b85a2014-04-18 22:59:24 +0100399 int ret = type->buffer_p.get_buffer(obj, bufinfo, flags);
Damien George57a4b4f2014-04-18 22:29:21 +0100400 if (ret != 0 || bufinfo->buf == NULL) {
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300401 return false;
402 }
403 return true;
404}
405
Damien Georgeb11b85a2014-04-18 22:59:24 +0100406void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags) {
407 if (!mp_get_buffer(obj, bufinfo, flags)) {
Damien George57a4b4f2014-04-18 22:29:21 +0100408 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object with buffer protocol required"));
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300409 }
410}