Branch data Line data Source code
1 : : /*
2 : : * This file is part of the MicroPython 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 : :
27 : : #include <stdint.h>
28 : : #include <stdio.h>
29 : : #include <stdarg.h>
30 : : #include <assert.h>
31 : :
32 : : #include "py/obj.h"
33 : : #include "py/objtype.h"
34 : : #include "py/objint.h"
35 : : #include "py/objstr.h"
36 : : #include "py/runtime.h"
37 : : #include "py/stackctrl.h"
38 : : #include "py/stream.h" // for mp_obj_print
39 : :
40 : : // Allocates an object and also sets type, for mp_obj_malloc{,_var} macros.
41 : 548597 : MP_NOINLINE void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type) {
42 : 548597 : mp_obj_base_t *base = (mp_obj_base_t *)m_malloc(num_bytes);
43 : 548578 : base->type = type;
44 : 548578 : return base;
45 : : }
46 : :
47 : 50194721 : const mp_obj_type_t *MICROPY_WRAP_MP_OBJ_GET_TYPE(mp_obj_get_type)(mp_const_obj_t o_in) {
48 : : #if MICROPY_OBJ_IMMEDIATE_OBJS && MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
49 : :
50 [ + + ]: 50194721 : if (mp_obj_is_obj(o_in)) {
51 : 49668151 : const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
52 : 49668151 : return o->type;
53 : : } else {
54 : 526570 : static const mp_obj_type_t *const types[] = {
55 : : NULL, &mp_type_int, &mp_type_str, &mp_type_int,
56 : : NULL, &mp_type_int, &mp_type_NoneType, &mp_type_int,
57 : : NULL, &mp_type_int, &mp_type_str, &mp_type_int,
58 : : NULL, &mp_type_int, &mp_type_bool, &mp_type_int,
59 : : };
60 : 526570 : return types[(uintptr_t)o_in & 0xf];
61 : : }
62 : :
63 : : #elif MICROPY_OBJ_IMMEDIATE_OBJS && MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
64 : :
65 : : if (mp_obj_is_small_int(o_in)) {
66 : : return &mp_type_int;
67 : : } else if (mp_obj_is_obj(o_in)) {
68 : : const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
69 : : return o->type;
70 : : #if MICROPY_PY_BUILTINS_FLOAT
71 : : } else if ((((mp_uint_t)(o_in)) & 0xff800007) != 0x00000006) {
72 : : return &mp_type_float;
73 : : #endif
74 : : } else {
75 : : static const mp_obj_type_t *const types[] = {
76 : : &mp_type_str, &mp_type_NoneType, &mp_type_str, &mp_type_bool,
77 : : };
78 : : return types[((uintptr_t)o_in >> 3) & 3];
79 : : }
80 : :
81 : : #else
82 : :
83 : : if (mp_obj_is_small_int(o_in)) {
84 : : return &mp_type_int;
85 : : } else if (mp_obj_is_qstr(o_in)) {
86 : : return &mp_type_str;
87 : : #if MICROPY_PY_BUILTINS_FLOAT && ( \
88 : : MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D)
89 : : } else if (mp_obj_is_float(o_in)) {
90 : : return &mp_type_float;
91 : : #endif
92 : : #if MICROPY_OBJ_IMMEDIATE_OBJS
93 : : } else if (mp_obj_is_immediate_obj(o_in)) {
94 : : static const mp_obj_type_t *const types[2] = {&mp_type_NoneType, &mp_type_bool};
95 : : return types[MP_OBJ_IMMEDIATE_OBJ_VALUE(o_in) & 1];
96 : : #endif
97 : : } else {
98 : : const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
99 : : return o->type;
100 : : }
101 : :
102 : : #endif
103 : : }
104 : :
105 : 740 : const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
106 : 740 : return qstr_str(mp_obj_get_type(o_in)->name);
107 : : }
108 : :
109 : 365647 : void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
110 : : // There can be data structures nested too deep, or just recursive
111 : 365647 : MP_STACK_CHECK();
112 : : #ifndef NDEBUG
113 [ - + ]: 365645 : if (o_in == MP_OBJ_NULL) {
114 : 0 : mp_print_str(print, "(nil)");
115 : 0 : return;
116 : : }
117 : : #endif
118 : 365645 : const mp_obj_type_t *type = mp_obj_get_type(o_in);
119 [ + + ]: 365645 : if (MP_OBJ_TYPE_HAS_SLOT(type, print)) {
120 : 365555 : MP_OBJ_TYPE_GET_SLOT(type, print)((mp_print_t *)print, o_in, kind);
121 : : } else {
122 : 90 : mp_printf(print, "<%q>", type->name);
123 : : }
124 : : }
125 : :
126 : 160 : void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
127 : 160 : mp_obj_print_helper(MP_PYTHON_PRINTER, o_in, kind);
128 : 160 : }
129 : :
130 : : // helper function to print an exception with traceback
131 : 45 : void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
132 [ + - ]: 45 : if (mp_obj_is_exception_instance(exc)) {
133 : 45 : size_t n, *values;
134 : 45 : mp_obj_exception_get_traceback(exc, &n, &values);
135 [ + + ]: 45 : if (n > 0) {
136 [ - + ]: 23 : assert(n % 3 == 0);
137 : 23 : mp_print_str(print, "Traceback (most recent call last):\n");
138 [ + + ]: 75 : for (int i = n - 3; i >= 0; i -= 3) {
139 : : #if MICROPY_ENABLE_SOURCE_LINE
140 : 52 : mp_printf(print, " File \"%q\", line %d", values[i], (int)values[i + 1]);
141 : : #else
142 : : mp_printf(print, " File \"%q\"", values[i]);
143 : : #endif
144 : : // the block name can be NULL if it's unknown
145 : 52 : qstr block = values[i + 2];
146 [ - + ]: 52 : if (block == MP_QSTRnull) {
147 : 0 : mp_print_str(print, "\n");
148 : : } else {
149 : 52 : mp_printf(print, ", in %q\n", block);
150 : : }
151 : : }
152 : : }
153 : : }
154 : 45 : mp_obj_print_helper(print, exc, PRINT_EXC);
155 : 45 : mp_print_str(print, "\n");
156 : 45 : }
157 : :
158 : 15976277 : bool mp_obj_is_true(mp_obj_t arg) {
159 [ + + ]: 15976277 : if (arg == mp_const_false) {
160 : : return 0;
161 [ + + ]: 14014622 : } else if (arg == mp_const_true) {
162 : : return 1;
163 [ + + ]: 5406909 : } else if (arg == mp_const_none) {
164 : : return 0;
165 [ + + ]: 5256843 : } else if (mp_obj_is_small_int(arg)) {
166 [ + + ]: 4965328 : if (arg == MP_OBJ_NEW_SMALL_INT(0)) {
167 : : return 0;
168 : : } else {
169 : 2473670 : return 1;
170 : : }
171 : : } else {
172 : 291515 : const mp_obj_type_t *type = mp_obj_get_type(arg);
173 [ + + ]: 291515 : if (MP_OBJ_TYPE_HAS_SLOT(type, unary_op)) {
174 : 141277 : mp_obj_t result = MP_OBJ_TYPE_GET_SLOT(type, unary_op)(MP_UNARY_OP_BOOL, arg);
175 [ + + ]: 141277 : if (result != MP_OBJ_NULL) {
176 : 140862 : return result == mp_const_true;
177 : : }
178 : : }
179 : :
180 : 150653 : mp_obj_t len = mp_obj_len_maybe(arg);
181 [ + + ]: 150653 : if (len != MP_OBJ_NULL) {
182 : : // obj has a length, truth determined if len != 0
183 : 26 : return len != MP_OBJ_NEW_SMALL_INT(0);
184 : : } else {
185 : : // any other obj is true per Python semantics
186 : : return 1;
187 : : }
188 : : }
189 : : }
190 : :
191 : 3436 : bool mp_obj_is_callable(mp_obj_t o_in) {
192 [ + + ]: 3436 : const mp_call_fun_t call = MP_OBJ_TYPE_GET_SLOT_OR_NULL(mp_obj_get_type(o_in), call);
193 [ + + ]: 82 : if (call != mp_obj_instance_call) {
194 : 3424 : return call != NULL;
195 : : }
196 : 12 : return mp_obj_instance_is_callable(o_in);
197 : : }
198 : :
199 : : // This function implements the '==' and '!=' operators.
200 : : //
201 : : // From the Python language reference:
202 : : // (https://docs.python.org/3/reference/expressions.html#not-in)
203 : : // "The objects need not have the same type. If both are numbers, they are converted
204 : : // to a common type. Otherwise, the == and != operators always consider objects of
205 : : // different types to be unequal."
206 : : //
207 : : // This means that False==0 and True==1 are true expressions.
208 : : //
209 : : // Furthermore, from the v3.4.2 code for object.c: "Practical amendments: If rich
210 : : // comparison returns NotImplemented, == and != are decided by comparing the object
211 : : // pointer."
212 : 930697 : mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2) {
213 [ + + ]: 930697 : mp_obj_t local_true = (op == MP_BINARY_OP_NOT_EQUAL) ? mp_const_false : mp_const_true;
214 : 722359 : mp_obj_t local_false = (op == MP_BINARY_OP_NOT_EQUAL) ? mp_const_true : mp_const_false;
215 : 930697 : int pass_number = 0;
216 : :
217 : : // Shortcut for very common cases
218 [ + + ]: 930697 : if (o1 == o2 &&
219 [ + + + + ]: 236223 : (mp_obj_is_small_int(o1) || !(mp_obj_get_type(o1)->flags & MP_TYPE_FLAG_EQ_NOT_REFLEXIVE))) {
220 : : return local_true;
221 : : }
222 : :
223 : : // fast path for strings
224 [ + + + + : 694546 : if (mp_obj_is_str(o1)) {
+ + ]
225 [ + + + + : 269179 : if (mp_obj_is_str(o2)) {
+ + ]
226 : : // both strings, use special function
227 [ + + ]: 269131 : return mp_obj_str_equal(o1, o2) ? local_true : local_false;
228 : : #if MICROPY_PY_STR_BYTES_CMP_WARN
229 [ - + - + : 48 : } else if (mp_obj_is_type(o2, &mp_type_bytes)) {
- + - + +
+ + + ]
230 : 4 : str_bytes_cmp:
231 : 8 : mp_warning(MP_WARN_CAT(BytesWarning), "Comparison between bytes and str");
232 : 8 : return local_false;
233 : : #endif
234 : : } else {
235 : 44 : goto skip_one_pass;
236 : : }
237 : : #if MICROPY_PY_STR_BYTES_CMP_WARN
238 [ + + + + : 425367 : } else if (mp_obj_is_str(o2) && mp_obj_is_type(o1, &mp_type_bytes)) {
+ + - + -
+ - + - +
+ + + + ]
239 : : // o1 is not a string (else caught above), so the objects are not equal
240 : 4 : goto str_bytes_cmp;
241 : : #endif
242 : : }
243 : :
244 : : // fast path for small ints
245 [ + + ]: 425363 : if (mp_obj_is_small_int(o1)) {
246 [ + + ]: 328638 : if (mp_obj_is_small_int(o2)) {
247 : : // both SMALL_INT, and not equal if we get here
248 : : return local_false;
249 : : } else {
250 : 902 : goto skip_one_pass;
251 : : }
252 : : }
253 : :
254 : : // generic type, call binary_op(MP_BINARY_OP_EQUAL)
255 [ + + ]: 106446 : while (pass_number < 2) {
256 : 102318 : const mp_obj_type_t *type = mp_obj_get_type(o1);
257 : : // If a full equality test is not needed and the other object is a different
258 : : // type then we don't need to bother trying the comparison.
259 [ + + ]: 102318 : if (MP_OBJ_TYPE_HAS_SLOT(type, binary_op) &&
260 [ + + + + ]: 94971 : ((type->flags & MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE) || mp_obj_get_type(o2) == type)) {
261 : : // CPython is asymmetric: it will try __eq__ if there's no __ne__ but not the
262 : : // other way around. If the class doesn't need a full test we can skip __ne__.
263 [ + + + + ]: 93687 : if (op == MP_BINARY_OP_NOT_EQUAL && (type->flags & MP_TYPE_FLAG_EQ_HAS_NEQ_TEST)) {
264 : 152 : mp_obj_t r = MP_OBJ_TYPE_GET_SLOT(type, binary_op)(MP_BINARY_OP_NOT_EQUAL, o1, o2);
265 [ + + ]: 152 : if (r != MP_OBJ_NULL) {
266 : : return r;
267 : : }
268 : : }
269 : :
270 : : // Try calling __eq__.
271 : 93603 : mp_obj_t r = MP_OBJ_TYPE_GET_SLOT(type, binary_op)(MP_BINARY_OP_EQUAL, o1, o2);
272 [ + + ]: 93235 : if (r != MP_OBJ_NULL) {
273 [ + + ]: 93091 : if (op == MP_BINARY_OP_EQUAL) {
274 : : return r;
275 : : } else {
276 [ + + ]: 1997 : return mp_obj_is_true(r) ? local_true : local_false;
277 : : }
278 : : }
279 : : }
280 : :
281 : 8775 : skip_one_pass:
282 : : // Try the other way around if none of the above worked
283 : 9721 : ++pass_number;
284 : 9721 : mp_obj_t temp = o1;
285 : 9721 : o1 = o2;
286 : 9721 : o2 = temp;
287 : : }
288 : :
289 : : // equality not implemented, so fall back to pointer conparison
290 [ + + ]: 4128 : return (o1 == o2) ? local_true : local_false;
291 : : }
292 : :
293 : 563246 : bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
294 : 563246 : return mp_obj_is_true(mp_obj_equal_not_equal(MP_BINARY_OP_EQUAL, o1, o2));
295 : : }
296 : :
297 : 9535969 : mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
298 : : // This function essentially performs implicit type conversion to int
299 : : // Note that Python does NOT provide implicit type conversion from
300 : : // float to int in the core expression language, try some_list[1.0].
301 [ + + ]: 9535969 : if (arg == mp_const_false) {
302 : : return 0;
303 [ + + ]: 9535953 : } else if (arg == mp_const_true) {
304 : : return 1;
305 [ + + ]: 9535917 : } else if (mp_obj_is_small_int(arg)) {
306 : 9535877 : return MP_OBJ_SMALL_INT_VALUE(arg);
307 [ + + + + ]: 40 : } else if (mp_obj_is_exact_type(arg, &mp_type_int)) {
308 : 14 : return mp_obj_int_get_checked(arg);
309 : : } else {
310 : 26 : mp_obj_t res = mp_unary_op(MP_UNARY_OP_INT, (mp_obj_t)arg);
311 : 0 : return mp_obj_int_get_checked(res);
312 : : }
313 : : }
314 : :
315 : 1570 : mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg) {
316 [ + + + + : 1570 : if (mp_obj_is_int(arg)) {
+ + ]
317 : 1554 : return mp_obj_int_get_truncated(arg);
318 : : } else {
319 : 16 : return mp_obj_get_int(arg);
320 : : }
321 : : }
322 : :
323 : : // returns false if arg is not of integral type
324 : : // returns true and sets *value if it is of integral type
325 : : // can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t
326 : 842 : bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) {
327 [ + + ]: 842 : if (arg == mp_const_false) {
328 : 20 : *value = 0;
329 [ + + ]: 822 : } else if (arg == mp_const_true) {
330 : 44 : *value = 1;
331 [ + + ]: 778 : } else if (mp_obj_is_small_int(arg)) {
332 : 754 : *value = MP_OBJ_SMALL_INT_VALUE(arg);
333 [ + + + + ]: 24 : } else if (mp_obj_is_exact_type(arg, &mp_type_int)) {
334 : 4 : *value = mp_obj_int_get_checked(arg);
335 : : } else {
336 : : return false;
337 : : }
338 : : return true;
339 : : }
340 : :
341 : : #if MICROPY_PY_BUILTINS_FLOAT
342 : 518718 : bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) {
343 : 518718 : mp_float_t val;
344 : :
345 [ + + ]: 518718 : if (arg == mp_const_false) {
346 : : val = 0;
347 [ + + ]: 518690 : } else if (arg == mp_const_true) {
348 : : val = 1;
349 [ + + ]: 518670 : } else if (mp_obj_is_small_int(arg)) {
350 : 244378 : val = (mp_float_t)MP_OBJ_SMALL_INT_VALUE(arg);
351 : : #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
352 [ + - + + ]: 274292 : } else if (mp_obj_is_exact_type(arg, &mp_type_int)) {
353 : 21624 : val = mp_obj_int_as_float_impl(arg);
354 : : #endif
355 [ - + - + : 252668 : } else if (mp_obj_is_float(arg)) {
- + - + +
- + + ]
356 : 249728 : val = mp_obj_float_get(arg);
357 : : } else {
358 : 2940 : arg = mp_unary_op(MP_UNARY_OP_FLOAT_MAYBE, (mp_obj_t)arg);
359 [ + + + + : 2940 : if (arg != MP_OBJ_NULL && mp_obj_is_float(arg)) {
+ - ]
360 : 20 : val = mp_obj_float_get(arg);
361 : : } else {
362 : : return false;
363 : : }
364 : : }
365 : 515798 : *value = val;
366 : 515798 : return true;
367 : : }
368 : :
369 : 93598 : mp_float_t mp_obj_get_float(mp_obj_t arg) {
370 : 93598 : mp_float_t val;
371 : :
372 [ + + ]: 93598 : if (!mp_obj_get_float_maybe(arg, &val)) {
373 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
374 : : mp_raise_TypeError(MP_ERROR_TEXT("can't convert to float"));
375 : : #else
376 : 16 : mp_raise_msg_varg(&mp_type_TypeError,
377 : 16 : MP_ERROR_TEXT("can't convert %s to float"), mp_obj_get_type_str(arg));
378 : : #endif
379 : : }
380 : :
381 : 93582 : return val;
382 : : }
383 : :
384 : : #if MICROPY_PY_BUILTINS_COMPLEX
385 : 2984 : bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
386 [ + + ]: 2984 : if (mp_obj_get_float_maybe(arg, real)) {
387 : 104 : *imag = 0;
388 [ - + - + : 2880 : } else if (mp_obj_is_type(arg, &mp_type_complex)) {
- + - + +
- + + ]
389 : 2848 : mp_obj_complex_get(arg, real, imag);
390 : : } else {
391 : 32 : arg = mp_unary_op(MP_UNARY_OP_COMPLEX_MAYBE, (mp_obj_t)arg);
392 [ + + + + : 32 : if (arg != MP_OBJ_NULL && mp_obj_is_type(arg, &mp_type_complex)) {
+ - ]
393 : 8 : mp_obj_complex_get(arg, real, imag);
394 : : } else {
395 : : return false;
396 : : }
397 : : }
398 : : return true;
399 : : }
400 : :
401 : 1600 : void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
402 [ + + ]: 1600 : if (!mp_obj_get_complex_maybe(arg, real, imag)) {
403 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
404 : : mp_raise_TypeError(MP_ERROR_TEXT("can't convert to complex"));
405 : : #else
406 : 16 : mp_raise_msg_varg(&mp_type_TypeError,
407 : 16 : MP_ERROR_TEXT("can't convert %s to complex"), mp_obj_get_type_str(arg));
408 : : #endif
409 : : }
410 : 1584 : }
411 : : #endif
412 : : #endif
413 : :
414 : : // note: returned value in *items may point to the interior of a GC block
415 : 148990 : void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) {
416 [ - + - + : 148990 : if (mp_obj_is_type(o, &mp_type_tuple)) {
- + - + +
+ + + ]
417 : 144432 : mp_obj_tuple_get(o, len, items);
418 [ - + - + : 4558 : } else if (mp_obj_is_type(o, &mp_type_list)) {
- + - + +
+ + + ]
419 : 4538 : mp_obj_list_get(o, len, items);
420 : : } else {
421 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
422 : : mp_raise_TypeError(MP_ERROR_TEXT("expected tuple/list"));
423 : : #else
424 : 20 : mp_raise_msg_varg(&mp_type_TypeError,
425 : 20 : MP_ERROR_TEXT("object '%s' isn't a tuple or list"), mp_obj_get_type_str(o));
426 : : #endif
427 : : }
428 : 148970 : }
429 : :
430 : : // note: returned value in *items may point to the interior of a GC block
431 : 384 : void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) {
432 : 384 : size_t seq_len;
433 : 384 : mp_obj_get_array(o, &seq_len, items);
434 [ + + ]: 372 : if (seq_len != len) {
435 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
436 : : mp_raise_ValueError(MP_ERROR_TEXT("tuple/list has wrong length"));
437 : : #else
438 : 16 : mp_raise_msg_varg(&mp_type_ValueError,
439 : 16 : MP_ERROR_TEXT("requested length %d but object has length %d"), (int)len, (int)seq_len);
440 : : #endif
441 : : }
442 : 356 : }
443 : :
444 : : // is_slice determines whether the index is a slice index
445 : 21843834 : size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice) {
446 : 21843834 : mp_int_t i;
447 [ + + ]: 21843834 : if (mp_obj_is_small_int(index)) {
448 : 21843786 : i = MP_OBJ_SMALL_INT_VALUE(index);
449 [ + + ]: 48 : } else if (!mp_obj_get_int_maybe(index, &i)) {
450 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
451 : : mp_raise_TypeError(MP_ERROR_TEXT("indices must be integers"));
452 : : #else
453 : 4 : mp_raise_msg_varg(&mp_type_TypeError,
454 : 4 : MP_ERROR_TEXT("%q indices must be integers, not %s"),
455 : 4 : type->name, mp_obj_get_type_str(index));
456 : : #endif
457 : : }
458 : :
459 [ + + ]: 21843830 : if (i < 0) {
460 : 100174 : i += len;
461 : : }
462 [ + + ]: 21843830 : if (is_slice) {
463 [ + + ]: 280 : if (i < 0) {
464 : 12 : i = 0;
465 [ + + ]: 268 : } else if ((mp_uint_t)i > len) {
466 : 24 : i = len;
467 : : }
468 : : } else {
469 [ + - + + ]: 21843550 : if (i < 0 || (mp_uint_t)i >= len) {
470 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
471 : : mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("index out of range"));
472 : : #else
473 : 8 : mp_raise_msg_varg(&mp_type_IndexError, MP_ERROR_TEXT("%q index out of range"), type->name);
474 : : #endif
475 : : }
476 : : }
477 : :
478 : : // By this point 0 <= i <= len and so fits in a size_t
479 : 21843822 : return (size_t)i;
480 : : }
481 : :
482 : 56 : mp_obj_t mp_obj_id(mp_obj_t o_in) {
483 : 56 : mp_int_t id = (mp_int_t)o_in;
484 [ + + ]: 56 : if (!mp_obj_is_obj(o_in)) {
485 : 24 : return mp_obj_new_int(id);
486 [ + - ]: 32 : } else if (id >= 0) {
487 : : // Many OSes and CPUs have affinity for putting "user" memories
488 : : // into low half of address space, and "system" into upper half.
489 : : // We're going to take advantage of that and return small int
490 : : // (signed) for such "user" addresses.
491 : 32 : return MP_OBJ_NEW_SMALL_INT(id);
492 : : } else {
493 : : // If that didn't work, well, let's return long int, just as
494 : : // a (big) positive value, so it will never clash with the range
495 : : // of small int returned in previous case.
496 : 0 : return mp_obj_new_int_from_uint((mp_uint_t)id);
497 : : }
498 : : }
499 : :
500 : : // will raise a TypeError if object has no length
501 : 30088 : mp_obj_t mp_obj_len(mp_obj_t o_in) {
502 : 30088 : mp_obj_t len = mp_obj_len_maybe(o_in);
503 [ + + ]: 30088 : if (len == MP_OBJ_NULL) {
504 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
505 : : mp_raise_TypeError(MP_ERROR_TEXT("object has no len"));
506 : : #else
507 : 8 : mp_raise_msg_varg(&mp_type_TypeError,
508 : 8 : MP_ERROR_TEXT("object of type '%s' has no len()"), mp_obj_get_type_str(o_in));
509 : : #endif
510 : : } else {
511 : 30080 : return len;
512 : : }
513 : : }
514 : :
515 : : // may return MP_OBJ_NULL
516 : 189662 : mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
517 : 189662 : if (
518 : : #if !MICROPY_PY_BUILTINS_STR_UNICODE
519 : : // It's simple - unicode is slow, non-unicode is fast
520 : : mp_obj_is_str(o_in) ||
521 : : #endif
522 [ - + - + : 189662 : mp_obj_is_type(o_in, &mp_type_bytes)) {
- + - + +
+ + + ]
523 [ - + ]: 174 : GET_STR_LEN(o_in, l);
524 : 174 : return MP_OBJ_NEW_SMALL_INT(l);
525 : : } else {
526 : 189488 : const mp_obj_type_t *type = mp_obj_get_type(o_in);
527 [ + + ]: 189488 : if (MP_OBJ_TYPE_HAS_SLOT(type, unary_op)) {
528 : 39248 : return MP_OBJ_TYPE_GET_SLOT(type, unary_op)(MP_UNARY_OP_LEN, o_in);
529 : : } else {
530 : : return MP_OBJ_NULL;
531 : : }
532 : : }
533 : : }
534 : :
535 : 22362302 : mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
536 : 22362302 : const mp_obj_type_t *type = mp_obj_get_type(base);
537 [ + + ]: 22362302 : if (MP_OBJ_TYPE_HAS_SLOT(type, subscr)) {
538 : 22362290 : mp_obj_t ret = MP_OBJ_TYPE_GET_SLOT(type, subscr)(base, index, value);
539 [ + + ]: 22446989 : if (ret != MP_OBJ_NULL) {
540 : 22446945 : return ret;
541 : : }
542 : : // TODO: call base classes here?
543 : : }
544 [ + + ]: 56 : if (value == MP_OBJ_NULL) {
545 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
546 : : mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item deletion"));
547 : : #else
548 : 14 : mp_raise_msg_varg(&mp_type_TypeError,
549 : 14 : MP_ERROR_TEXT("'%s' object doesn't support item deletion"), mp_obj_get_type_str(base));
550 : : #endif
551 [ + + ]: 42 : } else if (value == MP_OBJ_SENTINEL) {
552 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
553 : : mp_raise_TypeError(MP_ERROR_TEXT("object isn't subscriptable"));
554 : : #else
555 : 8 : mp_raise_msg_varg(&mp_type_TypeError,
556 : 8 : MP_ERROR_TEXT("'%s' object isn't subscriptable"), mp_obj_get_type_str(base));
557 : : #endif
558 : : } else {
559 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
560 : : mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item assignment"));
561 : : #else
562 : 34 : mp_raise_msg_varg(&mp_type_TypeError,
563 : 34 : MP_ERROR_TEXT("'%s' object doesn't support item assignment"), mp_obj_get_type_str(base));
564 : : #endif
565 : : }
566 : : }
567 : :
568 : : // Return input argument. Useful as .getiter for objects which are
569 : : // their own iterators, etc.
570 : 124 : mp_obj_t mp_identity(mp_obj_t self) {
571 : 124 : return self;
572 : : }
573 : : MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
574 : :
575 : : // mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) {
576 : : // (void)iter_buf;
577 : : // return self;
578 : : // }
579 : :
580 : 78136 : bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
581 : 78136 : const mp_obj_type_t *type = mp_obj_get_type(obj);
582 [ + + ]: 78136 : if (!MP_OBJ_TYPE_HAS_SLOT(type, buffer)) {
583 : : return false;
584 : : }
585 : 77865 : int ret = MP_OBJ_TYPE_GET_SLOT(type, buffer)(obj, bufinfo, flags);
586 [ + + ]: 77865 : if (ret != 0) {
587 : 226 : return false;
588 : : }
589 : : return true;
590 : : }
591 : :
592 : 53777 : void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
593 [ + + ]: 53777 : if (!mp_get_buffer(obj, bufinfo, flags)) {
594 : 4 : mp_raise_TypeError(MP_ERROR_TEXT("object with buffer protocol required"));
595 : : }
596 : 53773 : }
597 : :
598 : 1020 : mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
599 [ + + ]: 1020 : switch (op) {
600 : 76 : case MP_UNARY_OP_HASH:
601 : 76 : return MP_OBJ_NEW_SMALL_INT((mp_uint_t)o_in);
602 : : default:
603 : : return MP_OBJ_NULL; // op not supported
604 : : }
605 : : }
|