blob: 493945a227a9e3505c0c0255097270fa820b60dd [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"
Damien George51698222015-09-03 23:01:07 +010036#include "py/objstr.h"
Damien George51dfcb42015-01-01 20:27:54 +000037#include "py/runtime0.h"
38#include "py/runtime.h"
39#include "py/stackctrl.h"
Paul Sokolovsky1f91e922015-02-17 00:12:42 +020040#include "py/stream.h" // for mp_obj_print
Damien660365e2013-12-17 18:27:24 +000041
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +030042mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
Damien Georgeb97669a2014-01-08 11:47:55 +000043 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien George999cedb2015-11-27 17:01:44 +000044 return (mp_obj_type_t*)&mp_type_int;
Damien George38a2da62014-01-08 17:33:12 +000045 } else if (MP_OBJ_IS_QSTR(o_in)) {
Damien George999cedb2015-11-27 17:01:44 +000046 return (mp_obj_type_t*)&mp_type_str;
Damien Georgeaedb8592015-10-17 22:57:34 +010047 #if MICROPY_PY_BUILTINS_FLOAT
48 } else if (mp_obj_is_float(o_in)) {
Damien George999cedb2015-11-27 17:01:44 +000049 return (mp_obj_type_t*)&mp_type_float;
Damien Georgeaedb8592015-10-17 22:57:34 +010050 #endif
Damien Georgeb97669a2014-01-08 11:47:55 +000051 } else {
Damien George999cedb2015-11-27 17:01:44 +000052 const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
53 return (mp_obj_type_t*)o->type;
Damien Georgeb97669a2014-01-08 11:47:55 +000054 }
55}
56
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +030057const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
Damien Georgea71c83a2014-02-15 11:34:50 +000058 return qstr_str(mp_obj_get_type(o_in)->name);
Damien660365e2013-12-17 18:27:24 +000059}
60
Damien George7f9d1d62015-04-09 23:56:15 +010061void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Paul Sokolovsky8993fb62014-06-28 02:25:04 +030062 // There can be data structures nested too deep, or just recursive
Paul Sokolovskycaa73342014-07-01 02:13:42 +030063 MP_STACK_CHECK();
Damien George8788b132015-01-25 18:35:54 +000064#ifndef NDEBUG
Damien George999cedb2015-11-27 17:01:44 +000065 if (o_in == MP_OBJ_NULL) {
Damien George7f9d1d62015-04-09 23:56:15 +010066 mp_print_str(print, "(nil)");
Paul Sokolovsky0bc15942014-05-10 21:05:45 +030067 return;
68 }
69#endif
Damien George5fa93b62014-01-22 14:35:10 +000070 mp_obj_type_t *type = mp_obj_get_type(o_in);
71 if (type->print != NULL) {
Damien George7f9d1d62015-04-09 23:56:15 +010072 type->print((mp_print_t*)print, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000073 } else {
Damien George044c4732015-04-11 13:03:37 +010074 mp_printf(print, "<%q>", type->name);
Damien660365e2013-12-17 18:27:24 +000075 }
76}
77
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020078void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
Paul Sokolovsky37300902016-10-22 01:07:07 +030079 mp_obj_print_helper(MP_PYTHON_PRINTER, o_in, kind);
Damien660365e2013-12-17 18:27:24 +000080}
81
Damien George136b1492014-01-19 12:38:49 +000082// helper function to print an exception with traceback
Damien George7f9d1d62015-04-09 23:56:15 +010083void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
Damien Georgec5966122014-02-15 16:10:44 +000084 if (mp_obj_is_exception_instance(exc)) {
Damien George3d2daa22016-01-02 22:04:12 +000085 size_t n, *values;
Damien George136b1492014-01-19 12:38:49 +000086 mp_obj_exception_get_traceback(exc, &n, &values);
87 if (n > 0) {
Paul Sokolovsky0ae518f2014-03-30 02:08:36 +020088 assert(n % 3 == 0);
Damien George7f9d1d62015-04-09 23:56:15 +010089 mp_print_str(print, "Traceback (most recent call last):\n");
Damien George136b1492014-01-19 12:38:49 +000090 for (int i = n - 3; i >= 0; i -= 3) {
Damien George62ad1892014-01-29 21:51:51 +000091#if MICROPY_ENABLE_SOURCE_LINE
Damien George044c4732015-04-11 13:03:37 +010092 mp_printf(print, " File \"%q\", line %d", values[i], (int)values[i + 1]);
Damien George62ad1892014-01-29 21:51:51 +000093#else
Damien George044c4732015-04-11 13:03:37 +010094 mp_printf(print, " File \"%q\"", values[i]);
Damien George62ad1892014-01-29 21:51:51 +000095#endif
Damien George0e4ba252014-04-13 15:01:28 +010096 // the block name can be NULL if it's unknown
97 qstr block = values[i + 2];
98 if (block == MP_QSTR_NULL) {
Damien George7f9d1d62015-04-09 23:56:15 +010099 mp_print_str(print, "\n");
Damien George0e4ba252014-04-13 15:01:28 +0100100 } else {
Damien George044c4732015-04-11 13:03:37 +0100101 mp_printf(print, ", in %q\n", block);
Damien George0e4ba252014-04-13 15:01:28 +0100102 }
Damien George136b1492014-01-19 12:38:49 +0000103 }
104 }
105 }
Damien George7f9d1d62015-04-09 23:56:15 +0100106 mp_obj_print_helper(print, exc, PRINT_EXC);
107 mp_print_str(print, "\n");
Damien George136b1492014-01-19 12:38:49 +0000108}
109
Damien George4d917232014-08-30 14:28:06 +0100110bool mp_obj_is_true(mp_obj_t arg) {
Damien Georged17926d2014-03-30 13:35:08 +0100111 if (arg == mp_const_false) {
112 return 0;
113 } else if (arg == mp_const_true) {
114 return 1;
115 } else if (arg == mp_const_none) {
116 return 0;
117 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
118 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
119 return 0;
120 } else {
121 return 1;
122 }
123 } else {
124 mp_obj_type_t *type = mp_obj_get_type(arg);
125 if (type->unary_op != NULL) {
126 mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg);
Damien George6ac5dce2014-05-21 19:42:43 +0100127 if (result != MP_OBJ_NULL) {
Damien Georged17926d2014-03-30 13:35:08 +0100128 return result == mp_const_true;
129 }
130 }
131
132 mp_obj_t len = mp_obj_len_maybe(arg);
133 if (len != MP_OBJ_NULL) {
134 // obj has a length, truth determined if len != 0
135 return len != MP_OBJ_NEW_SMALL_INT(0);
136 } else {
137 // any other obj is true per Python semantics
138 return 1;
139 }
140 }
141}
142
Damiend99b0522013-12-21 18:17:45 +0000143bool mp_obj_is_callable(mp_obj_t o_in) {
Damien George0344fa12014-11-03 16:09:39 +0000144 mp_call_fun_t call = mp_obj_get_type(o_in)->call;
145 if (call != mp_obj_instance_call) {
146 return call != NULL;
147 }
148 return mp_obj_instance_is_callable(o_in);
Damien660365e2013-12-17 18:27:24 +0000149}
150
Damien Georgec38dc3c2015-01-11 15:13:18 +0000151// This function implements the '==' operator (and so the inverse of '!=').
152//
153// From the Python language reference:
154// (https://docs.python.org/3/reference/expressions.html#not-in)
Damien660365e2013-12-17 18:27:24 +0000155// "The objects need not have the same type. If both are numbers, they are converted
156// to a common type. Otherwise, the == and != operators always consider objects of
157// different types to be unequal."
Damien Georgec38dc3c2015-01-11 15:13:18 +0000158//
159// This means that False==0 and True==1 are true expressions.
160//
161// Furthermore, from the v3.4.2 code for object.c: "Practical amendments: If rich
162// comparison returns NotImplemented, == and != are decided by comparing the object
163// pointer."
Damiend99b0522013-12-21 18:17:45 +0000164bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
Damien660365e2013-12-17 18:27:24 +0000165 if (o1 == o2) {
166 return true;
Damien Georgee22d76e2014-04-11 10:52:06 +0000167 }
168 if (o1 == mp_const_none || o2 == mp_const_none) {
Damien Georgee0f29792014-03-30 23:16:42 +0100169 return false;
Damien Georgee22d76e2014-04-11 10:52:06 +0000170 }
171
172 // fast path for small ints
173 if (MP_OBJ_IS_SMALL_INT(o1)) {
174 if (MP_OBJ_IS_SMALL_INT(o2)) {
175 // both SMALL_INT, and not equal if we get here
Damien660365e2013-12-17 18:27:24 +0000176 return false;
177 } else {
Damien Georgee22d76e2014-04-11 10:52:06 +0000178 mp_obj_t temp = o2; o2 = o1; o1 = temp;
179 // o2 is now the SMALL_INT, o1 is not
Damien Georgeb8a053a2014-04-11 10:10:37 +0100180 // fall through to generic op
Damien660365e2013-12-17 18:27:24 +0000181 }
Damien Georgee22d76e2014-04-11 10:52:06 +0000182 }
183
184 // fast path for strings
185 if (MP_OBJ_IS_STR(o1)) {
186 if (MP_OBJ_IS_STR(o2)) {
187 // both strings, use special function
188 return mp_obj_str_equal(o1, o2);
189 } else {
190 // a string is never equal to anything else
Paul Sokolovsky707cae72016-07-22 00:34:34 +0300191 goto str_cmp_err;
Damien Georgee22d76e2014-04-11 10:52:06 +0000192 }
193 } else if (MP_OBJ_IS_STR(o2)) {
194 // o1 is not a string (else caught above), so the objects are not equal
Paul Sokolovsky707cae72016-07-22 00:34:34 +0300195 str_cmp_err:
196 #if MICROPY_PY_STR_BYTES_CMP_WARN
197 if (MP_OBJ_IS_TYPE(o1, &mp_type_bytes) || MP_OBJ_IS_TYPE(o2, &mp_type_bytes)) {
Paul Sokolovsky918851e2016-07-22 00:52:07 +0300198 mp_warning("Comparison between bytes and str");
Paul Sokolovsky707cae72016-07-22 00:34:34 +0300199 }
200 #endif
Damien Georgee22d76e2014-04-11 10:52:06 +0000201 return false;
Damien660365e2013-12-17 18:27:24 +0000202 }
Damien Georgeb8a053a2014-04-11 10:10:37 +0100203
204 // generic type, call binary_op(MP_BINARY_OP_EQUAL)
205 mp_obj_type_t *type = mp_obj_get_type(o1);
206 if (type->binary_op != NULL) {
207 mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
Damien George6ac5dce2014-05-21 19:42:43 +0100208 if (r != MP_OBJ_NULL) {
Damien Georgeb8a053a2014-04-11 10:10:37 +0100209 return r == mp_const_true ? true : false;
210 }
211 }
212
Damien Georgec38dc3c2015-01-11 15:13:18 +0000213 // equality not implemented, and objects are not the same object, so
214 // they are defined as not equal
215 return false;
Damien660365e2013-12-17 18:27:24 +0000216}
217
Damien George40f3c022014-07-03 13:25:24 +0100218mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
Paul Sokolovskye99841b2014-04-05 17:46:47 +0300219 // This function essentially performs implicit type conversion to int
220 // Note that Python does NOT provide implicit type conversion from
221 // float to int in the core expression language, try some_list[1.0].
Damiend99b0522013-12-21 18:17:45 +0000222 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000223 return 0;
Damiend99b0522013-12-21 18:17:45 +0000224 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000225 return 1;
Damiend99b0522013-12-21 18:17:45 +0000226 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
227 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien George3e1a5c12014-03-29 13:43:38 +0000228 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200229 return mp_obj_int_get_checked(arg);
Damien660365e2013-12-17 18:27:24 +0000230 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000231 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George94c41bb2017-03-28 22:37:26 +1100232 mp_raise_TypeError("can't convert to int");
Damien George1e9a92f2014-11-06 17:36:16 +0000233 } else {
234 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
235 "can't convert %s to int", mp_obj_get_type_str(arg)));
236 }
Damien660365e2013-12-17 18:27:24 +0000237 }
238}
239
Damien Georgec50772d2015-05-12 23:05:53 +0100240mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg) {
241 if (MP_OBJ_IS_INT(arg)) {
242 return mp_obj_int_get_truncated(arg);
243 } else {
244 return mp_obj_get_int(arg);
245 }
246}
247
Damien George8270e382014-04-03 11:00:54 +0000248// returns false if arg is not of integral type
249// returns true and sets *value if it is of integral type
Damien George40f3c022014-07-03 13:25:24 +0100250// can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t
251bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) {
Damien George8270e382014-04-03 11:00:54 +0000252 if (arg == mp_const_false) {
253 *value = 0;
254 } else if (arg == mp_const_true) {
255 *value = 1;
256 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
257 *value = MP_OBJ_SMALL_INT_VALUE(arg);
258 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
259 *value = mp_obj_int_get_checked(arg);
260 } else {
261 return false;
262 }
263 return true;
264}
265
Damien Georgefb510b32014-06-01 13:32:54 +0100266#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000267mp_float_t mp_obj_get_float(mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +0000268 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000269 return 0;
Damiend99b0522013-12-21 18:17:45 +0000270 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000271 return 1;
Damiend99b0522013-12-21 18:17:45 +0000272 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
273 return MP_OBJ_SMALL_INT_VALUE(arg);
Damien Georgee4af7122016-12-21 11:46:27 +1100274 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
Damien George3e1a5c12014-03-29 13:43:38 +0000275 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
Damien Georgee4af7122016-12-21 11:46:27 +1100276 return mp_obj_int_as_float_impl(arg);
277 #endif
Damien Georgeaaef1852015-08-20 23:30:12 +0100278 } else if (mp_obj_is_float(arg)) {
Damiend99b0522013-12-21 18:17:45 +0000279 return mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000280 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000281 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George94c41bb2017-03-28 22:37:26 +1100282 mp_raise_TypeError("can't convert to float");
Damien George1e9a92f2014-11-06 17:36:16 +0000283 } else {
284 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
285 "can't convert %s to float", mp_obj_get_type_str(arg)));
286 }
Damien660365e2013-12-17 18:27:24 +0000287 }
288}
289
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300290#if MICROPY_PY_BUILTINS_COMPLEX
Damiend99b0522013-12-21 18:17:45 +0000291void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
292 if (arg == mp_const_false) {
Damien660365e2013-12-17 18:27:24 +0000293 *real = 0;
294 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000295 } else if (arg == mp_const_true) {
Damien660365e2013-12-17 18:27:24 +0000296 *real = 1;
297 *imag = 0;
Damiend99b0522013-12-21 18:17:45 +0000298 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
299 *real = MP_OBJ_SMALL_INT_VALUE(arg);
Damien660365e2013-12-17 18:27:24 +0000300 *imag = 0;
Damien Georgee4af7122016-12-21 11:46:27 +1100301 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
Damien George0aa5d512014-03-29 17:28:20 +0000302 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
Damien Georgee4af7122016-12-21 11:46:27 +1100303 *real = mp_obj_int_as_float_impl(arg);
Damien George0aa5d512014-03-29 17:28:20 +0000304 *imag = 0;
Damien Georgee4af7122016-12-21 11:46:27 +1100305 #endif
Damien Georgeaaef1852015-08-20 23:30:12 +0100306 } else if (mp_obj_is_float(arg)) {
Damiend99b0522013-12-21 18:17:45 +0000307 *real = mp_obj_float_get(arg);
Damien660365e2013-12-17 18:27:24 +0000308 *imag = 0;
Damien George0c36da02014-03-08 15:24:39 +0000309 } else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) {
Damiend99b0522013-12-21 18:17:45 +0000310 mp_obj_complex_get(arg, real, imag);
Damien660365e2013-12-17 18:27:24 +0000311 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000312 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George94c41bb2017-03-28 22:37:26 +1100313 mp_raise_TypeError("can't convert to complex");
Damien George1e9a92f2014-11-06 17:36:16 +0000314 } else {
315 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
316 "can't convert %s to complex", mp_obj_get_type_str(arg)));
317 }
Damien660365e2013-12-17 18:27:24 +0000318 }
319}
320#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300321#endif
Damien660365e2013-12-17 18:27:24 +0000322
Damien Georgeeb54e4d2016-05-04 10:19:08 +0100323// note: returned value in *items may point to the interior of a GC block
Damien George6213ad72017-03-25 19:35:08 +1100324void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) {
Damien George07ddab52014-03-29 13:15:08 +0000325 if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
Damien George24ff0632014-03-24 10:47:13 +0000326 mp_obj_tuple_get(o, len, items);
Damien George3e1a5c12014-03-29 13:43:38 +0000327 } else if (MP_OBJ_IS_TYPE(o, &mp_type_list)) {
Damien George24ff0632014-03-24 10:47:13 +0000328 mp_obj_list_get(o, len, items);
Damien660365e2013-12-17 18:27:24 +0000329 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000330 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George94c41bb2017-03-28 22:37:26 +1100331 mp_raise_TypeError("expected tuple/list");
Damien George1e9a92f2014-11-06 17:36:16 +0000332 } else {
333 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
334 "object '%s' is not a tuple or list", mp_obj_get_type_str(o)));
335 }
Damien George24ff0632014-03-24 10:47:13 +0000336 }
337}
338
Damien Georgeeb54e4d2016-05-04 10:19:08 +0100339// note: returned value in *items may point to the interior of a GC block
Damien George3f810da2017-03-26 19:20:06 +1100340void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) {
Damien George6213ad72017-03-25 19:35:08 +1100341 size_t seq_len;
Damien Georgeca6d75f2014-08-30 15:17:47 +0100342 mp_obj_get_array(o, &seq_len, items);
343 if (seq_len != len) {
Damien George1e9a92f2014-11-06 17:36:16 +0000344 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George94c41bb2017-03-28 22:37:26 +1100345 mp_raise_ValueError("tuple/list has wrong length");
Damien George1e9a92f2014-11-06 17:36:16 +0000346 } else {
347 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Damien George2a1cca22016-03-14 22:40:39 +0000348 "requested length %d but object has length %d", (int)len, (int)seq_len));
Damien George1e9a92f2014-11-06 17:36:16 +0000349 }
Damien660365e2013-12-17 18:27:24 +0000350 }
351}
352
xbe9e1e8cd2014-03-12 22:57:16 -0700353// is_slice determines whether the index is a slice index
Damien Georgec88cfe12017-03-23 16:17:40 +1100354size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice) {
Damien George40f3c022014-07-03 13:25:24 +0100355 mp_int_t i;
Damien Georgea9ddd6d2014-04-11 10:40:38 +0000356 if (MP_OBJ_IS_SMALL_INT(index)) {
357 i = MP_OBJ_SMALL_INT_VALUE(index);
358 } else if (!mp_obj_get_int_maybe(index, &i)) {
Damien George1e9a92f2014-11-06 17:36:16 +0000359 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George94c41bb2017-03-28 22:37:26 +1100360 mp_raise_TypeError("indices must be integers");
Damien George1e9a92f2014-11-06 17:36:16 +0000361 } else {
362 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George044c4732015-04-11 13:03:37 +0100363 "%q indices must be integers, not %s",
364 type->name, mp_obj_get_type_str(index)));
Damien George1e9a92f2014-11-06 17:36:16 +0000365 }
Damien660365e2013-12-17 18:27:24 +0000366 }
xbe9e1e8cd2014-03-12 22:57:16 -0700367
368 if (i < 0) {
xbec5d70ba2014-03-13 00:29:15 -0700369 i += len;
xbe9e1e8cd2014-03-12 22:57:16 -0700370 }
371 if (is_slice) {
xbec5d70ba2014-03-13 00:29:15 -0700372 if (i < 0) {
373 i = 0;
Damien George963a5a32015-01-16 17:47:07 +0000374 } else if ((mp_uint_t)i > len) {
xbec5d70ba2014-03-13 00:29:15 -0700375 i = len;
376 }
xbe9e1e8cd2014-03-12 22:57:16 -0700377 } else {
Damien George963a5a32015-01-16 17:47:07 +0000378 if (i < 0 || (mp_uint_t)i >= len) {
Damien George1e9a92f2014-11-06 17:36:16 +0000379 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7d0d7212016-10-17 12:17:37 +1100380 mp_raise_msg(&mp_type_IndexError, "index out of range");
Damien George1e9a92f2014-11-06 17:36:16 +0000381 } else {
382 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError,
Damien George044c4732015-04-11 13:03:37 +0100383 "%q index out of range", type->name));
Damien George1e9a92f2014-11-06 17:36:16 +0000384 }
xbec5d70ba2014-03-13 00:29:15 -0700385 }
xbe9e1e8cd2014-03-12 22:57:16 -0700386 }
Damien Georgec88cfe12017-03-23 16:17:40 +1100387
388 // By this point 0 <= i <= len and so fits in a size_t
389 return (size_t)i;
Damien660365e2013-12-17 18:27:24 +0000390}
John R. Lenton4bee76e2014-01-10 11:25:03 +0000391
Damien Georgec7687ad2014-08-22 21:48:30 +0100392mp_obj_t mp_obj_id(mp_obj_t o_in) {
393 mp_int_t id = (mp_int_t)o_in;
394 if (!MP_OBJ_IS_OBJ(o_in)) {
395 return mp_obj_new_int(id);
396 } else if (id >= 0) {
397 // Many OSes and CPUs have affinity for putting "user" memories
398 // into low half of address space, and "system" into upper half.
399 // We're going to take advantage of that and return small int
400 // (signed) for such "user" addresses.
401 return MP_OBJ_NEW_SMALL_INT(id);
402 } else {
403 // If that didn't work, well, let's return long int, just as
Ville Skyttäca16c382017-05-29 10:08:14 +0300404 // a (big) positive value, so it will never clash with the range
Damien Georgec7687ad2014-08-22 21:48:30 +0100405 // of small int returned in previous case.
406 return mp_obj_new_int_from_uint((mp_uint_t)id);
407 }
408}
409
Damien George4c03b3a2014-08-12 18:33:40 +0100410// will raise a TypeError if object has no length
411mp_obj_t mp_obj_len(mp_obj_t o_in) {
412 mp_obj_t len = mp_obj_len_maybe(o_in);
413 if (len == MP_OBJ_NULL) {
Damien George1e9a92f2014-11-06 17:36:16 +0000414 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George94c41bb2017-03-28 22:37:26 +1100415 mp_raise_TypeError("object has no len");
Damien George1e9a92f2014-11-06 17:36:16 +0000416 } else {
417 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
418 "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
419 }
Damien George4c03b3a2014-08-12 18:33:40 +0100420 } else {
421 return len;
422 }
423}
424
Damien Georgeeae16442014-01-11 19:22:29 +0000425// may return MP_OBJ_NULL
John R. Lenton4bee76e2014-01-10 11:25:03 +0000426mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
Paul Sokolovskye7f2b4c2014-06-13 23:37:18 +0300427 if (
428#if !MICROPY_PY_BUILTINS_STR_UNICODE
429 // It's simple - unicode is slow, non-unicode is fast
430 MP_OBJ_IS_STR(o_in) ||
431#endif
432 MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
Damien George51698222015-09-03 23:01:07 +0100433 GET_STR_LEN(o_in, l);
434 return MP_OBJ_NEW_SMALL_INT(l);
John R. Lenton4bee76e2014-01-10 11:25:03 +0000435 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200436 mp_obj_type_t *type = mp_obj_get_type(o_in);
437 if (type->unary_op != NULL) {
Damien George6ac5dce2014-05-21 19:42:43 +0100438 return type->unary_op(MP_UNARY_OP_LEN, o_in);
Damien George09a0c642014-01-30 10:05:33 +0000439 } else {
440 return MP_OBJ_NULL;
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200441 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000442 }
John R. Lenton4bee76e2014-01-10 11:25:03 +0000443}
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200444
Damien George729f7b42014-04-17 22:10:53 +0100445mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
446 mp_obj_type_t *type = mp_obj_get_type(base);
447 if (type->subscr != NULL) {
448 mp_obj_t ret = type->subscr(base, index, value);
Damien George6ac5dce2014-05-21 19:42:43 +0100449 if (ret != MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100450 return ret;
451 }
452 // TODO: call base classes here?
453 }
454 if (value == MP_OBJ_NULL) {
Damien George1e9a92f2014-11-06 17:36:16 +0000455 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George94c41bb2017-03-28 22:37:26 +1100456 mp_raise_TypeError("object does not support item deletion");
Damien George1e9a92f2014-11-06 17:36:16 +0000457 } else {
458 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
459 "'%s' object does not support item deletion", mp_obj_get_type_str(base)));
460 }
Damien George729f7b42014-04-17 22:10:53 +0100461 } else if (value == MP_OBJ_SENTINEL) {
Damien George1e9a92f2014-11-06 17:36:16 +0000462 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
463 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
464 "object is not subscriptable"));
465 } else {
466 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
467 "'%s' object is not subscriptable", mp_obj_get_type_str(base)));
468 }
Damien George729f7b42014-04-17 22:10:53 +0100469 } else {
Damien George1e9a92f2014-11-06 17:36:16 +0000470 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George94c41bb2017-03-28 22:37:26 +1100471 mp_raise_TypeError("object does not support item assignment");
Damien George1e9a92f2014-11-06 17:36:16 +0000472 } else {
473 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
474 "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
475 }
Damien George729f7b42014-04-17 22:10:53 +0100476 }
477}
478
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200479// Return input argument. Useful as .getiter for objects which are
480// their own iterators, etc.
481mp_obj_t mp_identity(mp_obj_t self) {
482 return self;
483}
Paul Sokolovsky557c9d52014-02-08 21:57:19 +0200484MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300485
Damien Georgeae8d8672016-01-09 23:14:54 +0000486mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) {
487 (void)iter_buf;
488 return self;
489}
490
Damien George4d917232014-08-30 14:28:06 +0100491bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
Damien George8a1cab92014-04-13 12:08:52 +0100492 mp_obj_type_t *type = mp_obj_get_type(obj);
493 if (type->buffer_p.get_buffer == NULL) {
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300494 return false;
495 }
Damien Georgeb11b85a2014-04-18 22:59:24 +0100496 int ret = type->buffer_p.get_buffer(obj, bufinfo, flags);
Paul Sokolovsky7133d912014-08-10 11:46:10 +0300497 if (ret != 0) {
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300498 return false;
499 }
500 return true;
501}
502
Damien George4d917232014-08-30 14:28:06 +0100503void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
Damien Georgeb11b85a2014-04-18 22:59:24 +0100504 if (!mp_get_buffer(obj, bufinfo, flags)) {
Damien George94c41bb2017-03-28 22:37:26 +1100505 mp_raise_TypeError("object with buffer protocol required");
Paul Sokolovsky3aa8ee72014-04-09 00:25:28 +0300506 }
507}
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000508
509mp_obj_t mp_generic_unary_op(mp_uint_t op, mp_obj_t o_in) {
510 switch (op) {
511 case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT((mp_uint_t)o_in);
512 default: return MP_OBJ_NULL; // op not supported
513 }
514}