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 : 553610 : MP_NOINLINE void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type) {
42 : 553610 : mp_obj_base_t *base = (mp_obj_base_t *)m_malloc(num_bytes);
43 : 553591 : base->type = type;
44 : 553591 : return base;
45 : : }
46 : :
47 : 46417271 : 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 [ + + ]: 46417271 : if (mp_obj_is_obj(o_in)) {
51 : 45883612 : const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
52 : 45883612 : return o->type;
53 : : } else {
54 : 533659 : 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 : 533659 : 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 : 780 : const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
106 : 780 : return qstr_str(mp_obj_get_type(o_in)->name);
107 : : }
108 : :
109 : 369333 : 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 : 369333 : MP_STACK_CHECK();
112 : : #ifndef NDEBUG
113 [ - + ]: 369331 : if (o_in == MP_OBJ_NULL) {
114 : 0 : mp_print_str(print, "(nil)");
115 : 0 : return;
116 : : }
117 : : #endif
118 : 369331 : const mp_obj_type_t *type = mp_obj_get_type(o_in);
119 [ + + ]: 369331 : if (MP_OBJ_TYPE_HAS_SLOT(type, print)) {
120 : 369235 : MP_OBJ_TYPE_GET_SLOT(type, print)((mp_print_t *)print, o_in, kind);
121 : : } else {
122 : 96 : 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 : 14501165 : bool mp_obj_is_true(mp_obj_t arg) {
159 [ + + ]: 14501165 : if (arg == mp_const_false) {
160 : : return 0;
161 [ + + ]: 12529267 : } else if (arg == mp_const_true) {
162 : : return 1;
163 [ + + ]: 5314873 : } else if (arg == mp_const_none) {
164 : : return 0;
165 [ + + ]: 5169656 : } else if (mp_obj_is_small_int(arg)) {
166 [ + + ]: 4883072 : if (arg == MP_OBJ_NEW_SMALL_INT(0)) {
167 : : return 0;
168 : : } else {
169 : 2454722 : return 1;
170 : : }
171 : : } else {
172 : 286584 : const mp_obj_type_t *type = mp_obj_get_type(arg);
173 [ + + ]: 286584 : if (MP_OBJ_TYPE_HAS_SLOT(type, unary_op)) {
174 : 140825 : mp_obj_t result = MP_OBJ_TYPE_GET_SLOT(type, unary_op)(MP_UNARY_OP_BOOL, arg);
175 [ + + ]: 140825 : if (result != MP_OBJ_NULL) {
176 : 140796 : return result == mp_const_true;
177 : : }
178 : : }
179 : :
180 : 145788 : mp_obj_t len = mp_obj_len_maybe(arg);
181 [ + + ]: 145788 : 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 : 3479 : bool mp_obj_is_callable(mp_obj_t o_in) {
192 [ + + ]: 3479 : 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 : 3467 : 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 : 898360 : mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2) {
213 [ + + ]: 898360 : mp_obj_t local_true = (op == MP_BINARY_OP_NOT_EQUAL) ? mp_const_false : mp_const_true;
214 : 690015 : mp_obj_t local_false = (op == MP_BINARY_OP_NOT_EQUAL) ? mp_const_true : mp_const_false;
215 : 898360 : int pass_number = 0;
216 : :
217 : : // Shortcut for very common cases
218 [ + + ]: 898360 : if (o1 == o2 &&
219 [ + + + + ]: 236167 : (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 [ + + + + : 662265 : if (mp_obj_is_str(o1)) {
+ + ]
225 [ + + + + : 269149 : if (mp_obj_is_str(o2)) {
+ + ]
226 : : // both strings, use special function
227 [ + + ]: 516981 : 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 [ + + + + : 393116 : } 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 [ + + ]: 393112 : if (mp_obj_is_small_int(o1)) {
246 [ + + ]: 294587 : 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 : 918 : goto skip_one_pass;
251 : : }
252 : : }
253 : :
254 : : // generic type, call binary_op(MP_BINARY_OP_EQUAL)
255 [ + + ]: 108286 : while (pass_number < 2) {
256 : 104142 : 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 [ + + ]: 104142 : if (MP_OBJ_TYPE_HAS_SLOT(type, binary_op) &&
260 [ + + + + ]: 96795 : ((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 [ + + + + ]: 95487 : 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 : 95403 : mp_obj_t r = MP_OBJ_TYPE_GET_SLOT(type, binary_op)(MP_BINARY_OP_EQUAL, o1, o2);
272 [ + + ]: 95035 : if (r != MP_OBJ_NULL) {
273 [ + + ]: 94891 : if (op == MP_BINARY_OP_EQUAL) {
274 : : return r;
275 : : } else {
276 [ + + ]: 3678 : return mp_obj_is_true(r) ? local_true : local_false;
277 : : }
278 : : }
279 : : }
280 : :
281 : 8799 : skip_one_pass:
282 : : // Try the other way around if none of the above worked
283 : 9761 : ++pass_number;
284 : 9761 : mp_obj_t temp = o1;
285 : 9761 : o1 = o2;
286 : 9761 : o2 = temp;
287 : : }
288 : :
289 : : // equality not implemented, so fall back to pointer conparison
290 [ + + ]: 4144 : return (o1 == o2) ? local_true : local_false;
291 : : }
292 : :
293 : 598544 : bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
294 : 598544 : return mp_obj_is_true(mp_obj_equal_not_equal(MP_BINARY_OP_EQUAL, o1, o2));
295 : : }
296 : :
297 : 8462156 : 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 : 8462156 : mp_int_t val;
302 [ + + ]: 8462156 : if (!mp_obj_get_int_maybe(arg, &val)) {
303 : 26 : mp_raise_TypeError_int_conversion(arg);
304 : : }
305 : 8397291 : return val;
306 : : }
307 : :
308 : 1582 : mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg) {
309 [ + + + + : 1582 : if (mp_obj_is_int(arg)) {
+ + ]
310 : 1566 : return mp_obj_int_get_truncated(arg);
311 : : } else {
312 : 16 : return mp_obj_get_int(arg);
313 : : }
314 : : }
315 : :
316 : : // returns false if arg is not of integral type
317 : : // returns true and sets *value if it is of integral type
318 : : // can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t
319 : 8317638 : bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) {
320 [ + + ]: 8317638 : if (arg == mp_const_false) {
321 : 36 : *value = 0;
322 [ + + ]: 8317602 : } else if (arg == mp_const_true) {
323 : 80 : *value = 1;
324 [ + + ]: 8317522 : } else if (mp_obj_is_small_int(arg)) {
325 : 8317446 : *value = MP_OBJ_SMALL_INT_VALUE(arg);
326 [ + + + + ]: 76 : } else if (mp_obj_is_exact_type(arg, &mp_type_int)) {
327 : 18 : *value = mp_obj_int_get_checked(arg);
328 : : } else {
329 : 58 : arg = mp_unary_op(MP_UNARY_OP_INT_MAYBE, (mp_obj_t)arg);
330 [ + + ]: 58 : if (arg != MP_OBJ_NULL) {
331 : 4 : *value = mp_obj_int_get_checked(arg);
332 : : } else {
333 : : return false;
334 : : }
335 : : }
336 : : return true;
337 : : }
338 : :
339 : : #if MICROPY_PY_BUILTINS_FLOAT
340 : 517172 : bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) {
341 : 517172 : mp_float_t val;
342 : :
343 [ + + ]: 517172 : if (arg == mp_const_false) {
344 : : val = 0;
345 [ + + ]: 517144 : } else if (arg == mp_const_true) {
346 : : val = 1;
347 [ + + ]: 517124 : } else if (mp_obj_is_small_int(arg)) {
348 : 240548 : val = (mp_float_t)MP_OBJ_SMALL_INT_VALUE(arg);
349 : : #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
350 [ + - + + ]: 276576 : } else if (mp_obj_is_exact_type(arg, &mp_type_int)) {
351 : 21624 : val = mp_obj_int_as_float_impl(arg);
352 : : #endif
353 [ - + - + : 254952 : } else if (mp_obj_is_float(arg)) {
- + - + +
- + + ]
354 : 252012 : val = mp_obj_float_get(arg);
355 : : } else {
356 : 2940 : arg = mp_unary_op(MP_UNARY_OP_FLOAT_MAYBE, (mp_obj_t)arg);
357 [ + + + + : 2940 : if (arg != MP_OBJ_NULL && mp_obj_is_float(arg)) {
+ - ]
358 : 20 : val = mp_obj_float_get(arg);
359 : : } else {
360 : : return false;
361 : : }
362 : : }
363 : 514252 : *value = val;
364 : 514252 : return true;
365 : : }
366 : :
367 : 96816 : mp_float_t mp_obj_get_float(mp_obj_t arg) {
368 : 96816 : mp_float_t val;
369 : :
370 [ + + ]: 96816 : if (!mp_obj_get_float_maybe(arg, &val)) {
371 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
372 : : mp_raise_TypeError(MP_ERROR_TEXT("can't convert to float"));
373 : : #else
374 : 16 : mp_raise_msg_varg(&mp_type_TypeError,
375 : 16 : MP_ERROR_TEXT("can't convert %s to float"), mp_obj_get_type_str(arg));
376 : : #endif
377 : : }
378 : :
379 : 96800 : return val;
380 : : }
381 : :
382 : : #if MICROPY_PY_BUILTINS_COMPLEX
383 : 2984 : bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
384 [ + + ]: 2984 : if (mp_obj_get_float_maybe(arg, real)) {
385 : 104 : *imag = 0;
386 [ - + - + : 2880 : } else if (mp_obj_is_type(arg, &mp_type_complex)) {
- + - + +
- + + ]
387 : 2848 : mp_obj_complex_get(arg, real, imag);
388 : : } else {
389 : 32 : arg = mp_unary_op(MP_UNARY_OP_COMPLEX_MAYBE, (mp_obj_t)arg);
390 [ + + + + : 32 : if (arg != MP_OBJ_NULL && mp_obj_is_type(arg, &mp_type_complex)) {
+ - ]
391 : 8 : mp_obj_complex_get(arg, real, imag);
392 : : } else {
393 : : return false;
394 : : }
395 : : }
396 : : return true;
397 : : }
398 : :
399 : 1600 : void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
400 [ + + ]: 1600 : if (!mp_obj_get_complex_maybe(arg, real, imag)) {
401 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
402 : : mp_raise_TypeError(MP_ERROR_TEXT("can't convert to complex"));
403 : : #else
404 : 16 : mp_raise_msg_varg(&mp_type_TypeError,
405 : 16 : MP_ERROR_TEXT("can't convert %s to complex"), mp_obj_get_type_str(arg));
406 : : #endif
407 : : }
408 : 1584 : }
409 : : #endif
410 : : #endif
411 : :
412 : : // note: returned value in *items may point to the interior of a GC block
413 : 155431 : void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) {
414 [ - + - + : 155431 : if (mp_obj_is_type(o, &mp_type_tuple)) {
- + - + +
+ + + ]
415 : 145312 : mp_obj_tuple_get(o, len, items);
416 [ - + - + : 10119 : } else if (mp_obj_is_type(o, &mp_type_list)) {
- + - + +
+ + + ]
417 : 10099 : mp_obj_list_get(o, len, items);
418 : : } else {
419 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
420 : : mp_raise_TypeError(MP_ERROR_TEXT("expected tuple/list"));
421 : : #else
422 : 20 : mp_raise_msg_varg(&mp_type_TypeError,
423 : 20 : MP_ERROR_TEXT("object '%s' isn't a tuple or list"), mp_obj_get_type_str(o));
424 : : #endif
425 : : }
426 : 155411 : }
427 : :
428 : : // note: returned value in *items may point to the interior of a GC block
429 : 388 : void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) {
430 : 388 : size_t seq_len;
431 : 388 : mp_obj_get_array(o, &seq_len, items);
432 [ + + ]: 376 : if (seq_len != len) {
433 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
434 : : mp_raise_ValueError(MP_ERROR_TEXT("tuple/list has wrong length"));
435 : : #else
436 : 16 : mp_raise_msg_varg(&mp_type_ValueError,
437 : 16 : MP_ERROR_TEXT("requested length %d but object has length %d"), (int)len, (int)seq_len);
438 : : #endif
439 : : }
440 : 360 : }
441 : :
442 : : // is_slice determines whether the index is a slice index
443 : 18864035 : size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice) {
444 : 18864035 : mp_int_t i;
445 [ + + ]: 18864035 : if (mp_obj_is_small_int(index)) {
446 : 18863987 : i = MP_OBJ_SMALL_INT_VALUE(index);
447 [ + + ]: 48 : } else if (!mp_obj_get_int_maybe(index, &i)) {
448 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
449 : : mp_raise_TypeError(MP_ERROR_TEXT("indices must be integers"));
450 : : #else
451 : 4 : mp_raise_msg_varg(&mp_type_TypeError,
452 : 4 : MP_ERROR_TEXT("%q indices must be integers, not %s"),
453 : 4 : type->name, mp_obj_get_type_str(index));
454 : : #endif
455 : : }
456 : :
457 [ + + ]: 18864031 : if (i < 0) {
458 : 100061 : i += len;
459 : : }
460 [ + + ]: 18864031 : if (is_slice) {
461 [ + + ]: 280 : if (i < 0) {
462 : 12 : i = 0;
463 [ + + ]: 268 : } else if ((mp_uint_t)i > len) {
464 : 24 : i = len;
465 : : }
466 : : } else {
467 [ + - + + ]: 18863751 : if (i < 0 || (mp_uint_t)i >= len) {
468 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
469 : : mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("index out of range"));
470 : : #else
471 : 8 : mp_raise_msg_varg(&mp_type_IndexError, MP_ERROR_TEXT("%q index out of range"), type->name);
472 : : #endif
473 : : }
474 : : }
475 : :
476 : : // By this point 0 <= i <= len and so fits in a size_t
477 : 18864023 : return (size_t)i;
478 : : }
479 : :
480 : 12140 : mp_obj_t mp_obj_id(mp_obj_t o_in) {
481 : 12140 : mp_int_t id = (mp_int_t)o_in;
482 [ + + ]: 12140 : if (!mp_obj_is_obj(o_in)) {
483 : 12036 : return mp_obj_new_int(id);
484 [ + - ]: 104 : } else if (id >= 0) {
485 : : // Many OSes and CPUs have affinity for putting "user" memories
486 : : // into low half of address space, and "system" into upper half.
487 : : // We're going to take advantage of that and return small int
488 : : // (signed) for such "user" addresses.
489 : 104 : return MP_OBJ_NEW_SMALL_INT(id);
490 : : } else {
491 : : // If that didn't work, well, let's return long int, just as
492 : : // a (big) positive value, so it will never clash with the range
493 : : // of small int returned in previous case.
494 : 0 : return mp_obj_new_int_from_uint((mp_uint_t)id);
495 : : }
496 : : }
497 : :
498 : : // will raise a TypeError if object has no length
499 : 30296 : mp_obj_t mp_obj_len(mp_obj_t o_in) {
500 : 30296 : mp_obj_t len = mp_obj_len_maybe(o_in);
501 [ + + ]: 30296 : if (len == MP_OBJ_NULL) {
502 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
503 : : mp_raise_TypeError(MP_ERROR_TEXT("object has no len"));
504 : : #else
505 : 8 : mp_raise_msg_varg(&mp_type_TypeError,
506 : 8 : MP_ERROR_TEXT("object of type '%s' has no len()"), mp_obj_get_type_str(o_in));
507 : : #endif
508 : : } else {
509 : 30288 : return len;
510 : : }
511 : : }
512 : :
513 : : // may return MP_OBJ_NULL
514 : 185823 : mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
515 : 185823 : if (
516 : : #if !MICROPY_PY_BUILTINS_STR_UNICODE
517 : : // It's simple - unicode is slow, non-unicode is fast
518 : : mp_obj_is_str(o_in) ||
519 : : #endif
520 [ - + - + : 185823 : mp_obj_is_type(o_in, &mp_type_bytes)) {
- + - + +
+ + + ]
521 [ - + ]: 308 : GET_STR_LEN(o_in, l);
522 : 308 : return MP_OBJ_NEW_SMALL_INT(l);
523 : : } else {
524 : 185515 : const mp_obj_type_t *type = mp_obj_get_type(o_in);
525 [ + + ]: 185515 : if (MP_OBJ_TYPE_HAS_SLOT(type, unary_op)) {
526 : 39648 : return MP_OBJ_TYPE_GET_SLOT(type, unary_op)(MP_UNARY_OP_LEN, o_in);
527 : : } else {
528 : : return MP_OBJ_NULL;
529 : : }
530 : : }
531 : : }
532 : :
533 : 20292445 : mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
534 : 20292445 : const mp_obj_type_t *type = mp_obj_get_type(base);
535 [ + + ]: 20292445 : if (MP_OBJ_TYPE_HAS_SLOT(type, subscr)) {
536 : 20292433 : mp_obj_t ret = MP_OBJ_TYPE_GET_SLOT(type, subscr)(base, index, value);
537 [ + + ]: 19626801 : if (ret != MP_OBJ_NULL) {
538 : 19626757 : return ret;
539 : : }
540 : : // TODO: call base classes here?
541 : : }
542 [ + + ]: 56 : if (value == MP_OBJ_NULL) {
543 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
544 : : mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item deletion"));
545 : : #else
546 : 14 : mp_raise_msg_varg(&mp_type_TypeError,
547 : 14 : MP_ERROR_TEXT("'%s' object doesn't support item deletion"), mp_obj_get_type_str(base));
548 : : #endif
549 [ + + ]: 42 : } else if (value == MP_OBJ_SENTINEL) {
550 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
551 : : mp_raise_TypeError(MP_ERROR_TEXT("object isn't subscriptable"));
552 : : #else
553 : 8 : mp_raise_msg_varg(&mp_type_TypeError,
554 : 8 : MP_ERROR_TEXT("'%s' object isn't subscriptable"), mp_obj_get_type_str(base));
555 : : #endif
556 : : } else {
557 : : #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
558 : : mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item assignment"));
559 : : #else
560 : 34 : mp_raise_msg_varg(&mp_type_TypeError,
561 : 34 : MP_ERROR_TEXT("'%s' object doesn't support item assignment"), mp_obj_get_type_str(base));
562 : : #endif
563 : : }
564 : : }
565 : :
566 : : // Return input argument. Useful as .getiter for objects which are
567 : : // their own iterators, etc.
568 : 316 : mp_obj_t mp_identity(mp_obj_t self) {
569 : 316 : return self;
570 : : }
571 : : MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
572 : :
573 : : // mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) {
574 : : // (void)iter_buf;
575 : : // return self;
576 : : // }
577 : :
578 : 156597 : bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
579 : 156597 : const mp_obj_type_t *type = mp_obj_get_type(obj);
580 [ + + ]: 156597 : if (!MP_OBJ_TYPE_HAS_SLOT(type, buffer)) {
581 : : return false;
582 : : }
583 : 78693 : int ret = MP_OBJ_TYPE_GET_SLOT(type, buffer)(obj, bufinfo, flags);
584 [ + + ]: 78692 : if (ret != 0) {
585 : 240 : return false;
586 : : }
587 : : return true;
588 : : }
589 : :
590 : 54046 : void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
591 [ + + ]: 54046 : if (!mp_get_buffer(obj, bufinfo, flags)) {
592 : 4 : mp_raise_TypeError(MP_ERROR_TEXT("object with buffer protocol required"));
593 : : }
594 : 54042 : }
|