2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-07-24 09:21:37 -04:00
|
|
|
#include <stdint.h>
|
2013-12-17 13:27:24 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/objtype.h"
|
|
|
|
#include "py/objint.h"
|
2015-09-03 18:01:07 -04:00
|
|
|
#include "py/objstr.h"
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stackctrl.h"
|
2015-02-16 17:12:42 -05:00
|
|
|
#include "py/stream.h" // for mp_obj_print
|
2013-12-17 13:27:24 -05:00
|
|
|
|
2022-04-22 01:32:22 -04:00
|
|
|
// Allocates an object and also sets type, for mp_obj_malloc{,_var} macros.
|
|
|
|
void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type) {
|
|
|
|
mp_obj_base_t *base = (mp_obj_base_t *)m_malloc(num_bytes);
|
|
|
|
base->type = type;
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2021-09-23 22:49:51 -04:00
|
|
|
const mp_obj_type_t *MICROPY_WRAP_MP_OBJ_GET_TYPE(mp_obj_get_type)(mp_const_obj_t o_in) {
|
2020-01-08 19:19:26 -05:00
|
|
|
#if MICROPY_OBJ_IMMEDIATE_OBJS && MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
|
|
|
|
|
|
|
|
if (mp_obj_is_obj(o_in)) {
|
|
|
|
const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
|
|
|
|
return o->type;
|
|
|
|
} else {
|
|
|
|
static const mp_obj_type_t *const types[] = {
|
|
|
|
NULL, &mp_type_int, &mp_type_str, &mp_type_int,
|
|
|
|
NULL, &mp_type_int, &mp_type_NoneType, &mp_type_int,
|
|
|
|
NULL, &mp_type_int, &mp_type_str, &mp_type_int,
|
|
|
|
NULL, &mp_type_int, &mp_type_bool, &mp_type_int,
|
|
|
|
};
|
|
|
|
return types[(uintptr_t)o_in & 0xf];
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif MICROPY_OBJ_IMMEDIATE_OBJS && MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
|
|
|
|
|
|
|
|
if (mp_obj_is_small_int(o_in)) {
|
|
|
|
return &mp_type_int;
|
|
|
|
} else if (mp_obj_is_obj(o_in)) {
|
|
|
|
const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
|
|
|
|
return o->type;
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
} else if ((((mp_uint_t)(o_in)) & 0xff800007) != 0x00000006) {
|
|
|
|
return &mp_type_float;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
static const mp_obj_type_t *const types[] = {
|
|
|
|
&mp_type_str, &mp_type_NoneType, &mp_type_str, &mp_type_bool,
|
|
|
|
};
|
|
|
|
return types[((uintptr_t)o_in >> 3) & 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2019-01-30 02:49:52 -05:00
|
|
|
if (mp_obj_is_small_int(o_in)) {
|
2020-01-08 19:01:14 -05:00
|
|
|
return &mp_type_int;
|
2019-01-30 02:49:52 -05:00
|
|
|
} else if (mp_obj_is_qstr(o_in)) {
|
2020-01-08 19:01:14 -05:00
|
|
|
return &mp_type_str;
|
2020-01-08 19:19:26 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT && ( \
|
|
|
|
MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D)
|
2015-10-17 17:57:34 -04:00
|
|
|
} else if (mp_obj_is_float(o_in)) {
|
2020-01-08 19:01:14 -05:00
|
|
|
return &mp_type_float;
|
2015-10-17 17:57:34 -04:00
|
|
|
#endif
|
2020-01-08 08:00:27 -05:00
|
|
|
#if MICROPY_OBJ_IMMEDIATE_OBJS
|
|
|
|
} else if (mp_obj_is_immediate_obj(o_in)) {
|
|
|
|
static const mp_obj_type_t *const types[2] = {&mp_type_NoneType, &mp_type_bool};
|
|
|
|
return types[MP_OBJ_IMMEDIATE_OBJ_VALUE(o_in) & 1];
|
|
|
|
#endif
|
2014-01-08 06:47:55 -05:00
|
|
|
} else {
|
2015-11-27 12:01:44 -05:00
|
|
|
const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
|
2020-01-08 19:01:14 -05:00
|
|
|
return o->type;
|
2014-01-08 06:47:55 -05:00
|
|
|
}
|
2020-01-08 19:19:26 -05:00
|
|
|
|
|
|
|
#endif
|
2014-01-08 06:47:55 -05:00
|
|
|
}
|
|
|
|
|
2014-05-10 19:26:42 -04:00
|
|
|
const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
|
2014-02-15 06:34:50 -05:00
|
|
|
return qstr_str(mp_obj_get_type(o_in)->name);
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
|
2014-06-27 19:25:04 -04:00
|
|
|
// There can be data structures nested too deep, or just recursive
|
2014-06-30 19:13:42 -04:00
|
|
|
MP_STACK_CHECK();
|
2015-01-25 13:35:54 -05:00
|
|
|
#ifndef NDEBUG
|
2015-11-27 12:01:44 -05:00
|
|
|
if (o_in == MP_OBJ_NULL) {
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "(nil)");
|
2014-05-10 14:05:45 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2020-01-08 19:01:14 -05:00
|
|
|
const mp_obj_type_t *type = mp_obj_get_type(o_in);
|
2014-01-22 09:35:10 -05:00
|
|
|
if (type->print != NULL) {
|
2015-04-09 18:56:15 -04:00
|
|
|
type->print((mp_print_t *)print, o_in, kind);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2015-04-11 08:03:37 -04:00
|
|
|
mp_printf(print, "<%q>", type->name);
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-13 12:19:16 -05:00
|
|
|
void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
|
2016-10-21 18:07:07 -04:00
|
|
|
mp_obj_print_helper(MP_PYTHON_PRINTER, o_in, kind);
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2014-01-19 07:38:49 -05:00
|
|
|
// helper function to print an exception with traceback
|
2015-04-09 18:56:15 -04:00
|
|
|
void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
|
2014-02-15 11:10:44 -05:00
|
|
|
if (mp_obj_is_exception_instance(exc)) {
|
2016-01-02 17:04:12 -05:00
|
|
|
size_t n, *values;
|
2014-01-19 07:38:49 -05:00
|
|
|
mp_obj_exception_get_traceback(exc, &n, &values);
|
|
|
|
if (n > 0) {
|
2014-03-29 20:08:36 -04:00
|
|
|
assert(n % 3 == 0);
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "Traceback (most recent call last):\n");
|
2014-01-19 07:38:49 -05:00
|
|
|
for (int i = n - 3; i >= 0; i -= 3) {
|
2014-01-29 16:51:51 -05:00
|
|
|
#if MICROPY_ENABLE_SOURCE_LINE
|
2015-04-11 08:03:37 -04:00
|
|
|
mp_printf(print, " File \"%q\", line %d", values[i], (int)values[i + 1]);
|
2014-01-29 16:51:51 -05:00
|
|
|
#else
|
2015-04-11 08:03:37 -04:00
|
|
|
mp_printf(print, " File \"%q\"", values[i]);
|
2014-01-29 16:51:51 -05:00
|
|
|
#endif
|
2014-04-13 10:01:28 -04:00
|
|
|
// the block name can be NULL if it's unknown
|
|
|
|
qstr block = values[i + 2];
|
2019-09-25 01:53:30 -04:00
|
|
|
if (block == MP_QSTRnull) {
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_str(print, "\n");
|
2014-04-13 10:01:28 -04:00
|
|
|
} else {
|
2015-04-11 08:03:37 -04:00
|
|
|
mp_printf(print, ", in %q\n", block);
|
2014-04-13 10:01:28 -04:00
|
|
|
}
|
2014-01-19 07:38:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_obj_print_helper(print, exc, PRINT_EXC);
|
|
|
|
mp_print_str(print, "\n");
|
2014-01-19 07:38:49 -05:00
|
|
|
}
|
|
|
|
|
2014-08-30 09:28:06 -04:00
|
|
|
bool mp_obj_is_true(mp_obj_t arg) {
|
2014-03-30 08:35:08 -04:00
|
|
|
if (arg == mp_const_false) {
|
|
|
|
return 0;
|
|
|
|
} else if (arg == mp_const_true) {
|
|
|
|
return 1;
|
|
|
|
} else if (arg == mp_const_none) {
|
|
|
|
return 0;
|
2019-01-30 02:49:52 -05:00
|
|
|
} else if (mp_obj_is_small_int(arg)) {
|
2019-06-04 13:14:41 -04:00
|
|
|
if (arg == MP_OBJ_NEW_SMALL_INT(0)) {
|
2014-03-30 08:35:08 -04:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-08 19:01:14 -05:00
|
|
|
const mp_obj_type_t *type = mp_obj_get_type(arg);
|
2014-03-30 08:35:08 -04:00
|
|
|
if (type->unary_op != NULL) {
|
|
|
|
mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg);
|
2014-05-21 14:42:43 -04:00
|
|
|
if (result != MP_OBJ_NULL) {
|
2014-03-30 08:35:08 -04:00
|
|
|
return result == mp_const_true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t len = mp_obj_len_maybe(arg);
|
|
|
|
if (len != MP_OBJ_NULL) {
|
|
|
|
// obj has a length, truth determined if len != 0
|
|
|
|
return len != MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
} else {
|
|
|
|
// any other obj is true per Python semantics
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
bool mp_obj_is_callable(mp_obj_t o_in) {
|
2020-01-08 19:01:14 -05:00
|
|
|
const mp_call_fun_t call = mp_obj_get_type(o_in)->call;
|
2014-11-03 11:09:39 -05:00
|
|
|
if (call != mp_obj_instance_call) {
|
|
|
|
return call != NULL;
|
|
|
|
}
|
|
|
|
return mp_obj_instance_is_callable(o_in);
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2019-12-31 17:19:12 -05:00
|
|
|
// This function implements the '==' and '!=' operators.
|
2015-01-11 10:13:18 -05:00
|
|
|
//
|
|
|
|
// From the Python language reference:
|
|
|
|
// (https://docs.python.org/3/reference/expressions.html#not-in)
|
2013-12-17 13:27:24 -05:00
|
|
|
// "The objects need not have the same type. If both are numbers, they are converted
|
|
|
|
// to a common type. Otherwise, the == and != operators always consider objects of
|
|
|
|
// different types to be unequal."
|
2015-01-11 10:13:18 -05:00
|
|
|
//
|
|
|
|
// This means that False==0 and True==1 are true expressions.
|
|
|
|
//
|
|
|
|
// Furthermore, from the v3.4.2 code for object.c: "Practical amendments: If rich
|
|
|
|
// comparison returns NotImplemented, == and != are decided by comparing the object
|
|
|
|
// pointer."
|
2019-12-31 17:19:12 -05:00
|
|
|
mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2) {
|
|
|
|
mp_obj_t local_true = (op == MP_BINARY_OP_NOT_EQUAL) ? mp_const_false : mp_const_true;
|
|
|
|
mp_obj_t local_false = (op == MP_BINARY_OP_NOT_EQUAL) ? mp_const_true : mp_const_false;
|
|
|
|
int pass_number = 0;
|
2014-04-11 06:52:06 -04:00
|
|
|
|
2019-12-31 17:19:12 -05:00
|
|
|
// Shortcut for very common cases
|
|
|
|
if (o1 == o2 &&
|
2020-02-10 05:41:22 -05:00
|
|
|
(mp_obj_is_small_int(o1) || !(mp_obj_get_type(o1)->flags & MP_TYPE_FLAG_EQ_NOT_REFLEXIVE))) {
|
2019-12-31 17:19:12 -05:00
|
|
|
return local_true;
|
2014-04-11 06:52:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// fast path for strings
|
2019-01-30 02:49:52 -05:00
|
|
|
if (mp_obj_is_str(o1)) {
|
|
|
|
if (mp_obj_is_str(o2)) {
|
2014-04-11 06:52:06 -04:00
|
|
|
// both strings, use special function
|
2019-12-31 17:19:12 -05:00
|
|
|
return mp_obj_str_equal(o1, o2) ? local_true : local_false;
|
|
|
|
#if MICROPY_PY_STR_BYTES_CMP_WARN
|
|
|
|
} else if (mp_obj_is_type(o2, &mp_type_bytes)) {
|
|
|
|
str_bytes_cmp:
|
|
|
|
mp_warning(MP_WARN_CAT(BytesWarning), "Comparison between bytes and str");
|
|
|
|
return local_false;
|
|
|
|
#endif
|
2014-04-11 06:52:06 -04:00
|
|
|
} else {
|
2019-12-31 17:19:12 -05:00
|
|
|
goto skip_one_pass;
|
2014-04-11 06:52:06 -04:00
|
|
|
}
|
2019-12-31 17:19:12 -05:00
|
|
|
#if MICROPY_PY_STR_BYTES_CMP_WARN
|
|
|
|
} else if (mp_obj_is_str(o2) && mp_obj_is_type(o1, &mp_type_bytes)) {
|
2014-04-11 06:52:06 -04:00
|
|
|
// o1 is not a string (else caught above), so the objects are not equal
|
2019-12-31 17:19:12 -05:00
|
|
|
goto str_bytes_cmp;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// fast path for small ints
|
|
|
|
if (mp_obj_is_small_int(o1)) {
|
|
|
|
if (mp_obj_is_small_int(o2)) {
|
|
|
|
// both SMALL_INT, and not equal if we get here
|
|
|
|
return local_false;
|
|
|
|
} else {
|
|
|
|
goto skip_one_pass;
|
2016-07-21 17:34:34 -04:00
|
|
|
}
|
2014-04-11 05:10:37 -04:00
|
|
|
}
|
2014-02-10 12:43:41 -05:00
|
|
|
|
2014-04-11 05:10:37 -04:00
|
|
|
// generic type, call binary_op(MP_BINARY_OP_EQUAL)
|
2019-12-31 17:19:12 -05:00
|
|
|
while (pass_number < 2) {
|
|
|
|
const mp_obj_type_t *type = mp_obj_get_type(o1);
|
|
|
|
// If a full equality test is not needed and the other object is a different
|
|
|
|
// type then we don't need to bother trying the comparison.
|
|
|
|
if (type->binary_op != NULL &&
|
2020-02-10 05:41:22 -05:00
|
|
|
((type->flags & MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE) || mp_obj_get_type(o2) == type)) {
|
2019-12-31 17:19:12 -05:00
|
|
|
// CPython is asymmetric: it will try __eq__ if there's no __ne__ but not the
|
|
|
|
// other way around. If the class doesn't need a full test we can skip __ne__.
|
2020-02-10 05:41:22 -05:00
|
|
|
if (op == MP_BINARY_OP_NOT_EQUAL && (type->flags & MP_TYPE_FLAG_EQ_HAS_NEQ_TEST)) {
|
2019-12-31 17:19:12 -05:00
|
|
|
mp_obj_t r = type->binary_op(MP_BINARY_OP_NOT_EQUAL, o1, o2);
|
|
|
|
if (r != MP_OBJ_NULL) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try calling __eq__.
|
|
|
|
mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
|
|
|
|
if (r != MP_OBJ_NULL) {
|
|
|
|
if (op == MP_BINARY_OP_EQUAL) {
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
return mp_obj_is_true(r) ? local_true : local_false;
|
|
|
|
}
|
|
|
|
}
|
2014-04-11 05:10:37 -04:00
|
|
|
}
|
2019-12-31 17:19:12 -05:00
|
|
|
|
|
|
|
skip_one_pass:
|
|
|
|
// Try the other way around if none of the above worked
|
|
|
|
++pass_number;
|
|
|
|
mp_obj_t temp = o1;
|
|
|
|
o1 = o2;
|
|
|
|
o2 = temp;
|
2013-12-17 16:35:38 -05:00
|
|
|
}
|
2014-04-11 05:10:37 -04:00
|
|
|
|
2019-12-31 17:19:12 -05:00
|
|
|
// equality not implemented, so fall back to pointer conparison
|
|
|
|
return (o1 == o2) ? local_true : local_false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
|
|
|
|
return mp_obj_is_true(mp_obj_equal_not_equal(MP_BINARY_OP_EQUAL, o1, o2));
|
2013-12-17 16:35:38 -05:00
|
|
|
}
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
|
2014-04-05 10:46:47 -04:00
|
|
|
// This function essentially performs implicit type conversion to int
|
|
|
|
// Note that Python does NOT provide implicit type conversion from
|
|
|
|
// float to int in the core expression language, try some_list[1.0].
|
2013-12-21 13:17:45 -05:00
|
|
|
if (arg == mp_const_false) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (arg == mp_const_true) {
|
2013-12-17 13:27:24 -05:00
|
|
|
return 1;
|
2019-01-30 02:49:52 -05:00
|
|
|
} else if (mp_obj_is_small_int(arg)) {
|
2013-12-21 13:17:45 -05:00
|
|
|
return MP_OBJ_SMALL_INT_VALUE(arg);
|
py/obj: Add static safety checks to mp_obj_is_type().
Commit d96cfd13e3a464862c introduced a regression by breaking existing
users of mp_obj_is_type(.., &mp_obj_bool). This function (and associated
helpers like mp_obj_is_int()) have some specific nuances, and mistakes like
this one can happen again.
This commit adds mp_obj_is_exact_type() which behaves like the the old
mp_obj_is_type(). The new mp_obj_is_type() has the same prototype but it
attempts to statically assert that it's not called with types which should
be checked using mp_obj_is_type(). If called with any of these types: int,
str, bool, NoneType - it will cause a compilation error. Additional
checked types (e.g function types) can be added in the future.
Existing users of mp_obj_is_type() with the now "invalid" types, were
translated to use mp_obj_is_exact_type().
The use of MP_STATIC_ASSERT() is not bulletproof - usually GCC (and other
compilers) can't statically check conditions that are only known during
link-time (like variables' addresses comparison). However, in this case,
GCC is able to statically detect these conditions, probably because it's
the exact same object - `&mp_type_int == &mp_type_int` is detected.
Misuses of this function with runtime-chosen types (e.g:
`mp_obj_type_t *x = ...; mp_obj_is_type(..., x);` won't be detected. MSC
is unable to detect this, so we use MP_STATIC_ASSERT_NOT_MSC().
Compiling with this commit and without the fix for d96cfd13e3a464862c shows
that it detects the problem.
Signed-off-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
2020-01-22 07:34:19 -05:00
|
|
|
} else if (mp_obj_is_exact_type(arg, &mp_type_int)) {
|
2014-01-18 09:07:16 -05:00
|
|
|
return mp_obj_int_get_checked(arg);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2018-08-29 10:59:36 -04:00
|
|
|
mp_obj_t res = mp_unary_op(MP_UNARY_OP_INT, (mp_obj_t)arg);
|
|
|
|
return mp_obj_int_get_checked(res);
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 18:05:53 -04:00
|
|
|
mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg) {
|
2019-01-30 02:49:52 -05:00
|
|
|
if (mp_obj_is_int(arg)) {
|
2015-05-12 18:05:53 -04:00
|
|
|
return mp_obj_int_get_truncated(arg);
|
|
|
|
} else {
|
|
|
|
return mp_obj_get_int(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 07:00:54 -04:00
|
|
|
// returns false if arg is not of integral type
|
|
|
|
// returns true and sets *value if it is of integral type
|
2014-07-03 08:25:24 -04:00
|
|
|
// can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t
|
|
|
|
bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) {
|
2014-04-03 07:00:54 -04:00
|
|
|
if (arg == mp_const_false) {
|
|
|
|
*value = 0;
|
|
|
|
} else if (arg == mp_const_true) {
|
|
|
|
*value = 1;
|
2019-01-30 02:49:52 -05:00
|
|
|
} else if (mp_obj_is_small_int(arg)) {
|
2014-04-03 07:00:54 -04:00
|
|
|
*value = MP_OBJ_SMALL_INT_VALUE(arg);
|
py/obj: Add static safety checks to mp_obj_is_type().
Commit d96cfd13e3a464862c introduced a regression by breaking existing
users of mp_obj_is_type(.., &mp_obj_bool). This function (and associated
helpers like mp_obj_is_int()) have some specific nuances, and mistakes like
this one can happen again.
This commit adds mp_obj_is_exact_type() which behaves like the the old
mp_obj_is_type(). The new mp_obj_is_type() has the same prototype but it
attempts to statically assert that it's not called with types which should
be checked using mp_obj_is_type(). If called with any of these types: int,
str, bool, NoneType - it will cause a compilation error. Additional
checked types (e.g function types) can be added in the future.
Existing users of mp_obj_is_type() with the now "invalid" types, were
translated to use mp_obj_is_exact_type().
The use of MP_STATIC_ASSERT() is not bulletproof - usually GCC (and other
compilers) can't statically check conditions that are only known during
link-time (like variables' addresses comparison). However, in this case,
GCC is able to statically detect these conditions, probably because it's
the exact same object - `&mp_type_int == &mp_type_int` is detected.
Misuses of this function with runtime-chosen types (e.g:
`mp_obj_type_t *x = ...; mp_obj_is_type(..., x);` won't be detected. MSC
is unable to detect this, so we use MP_STATIC_ASSERT_NOT_MSC().
Compiling with this commit and without the fix for d96cfd13e3a464862c shows
that it detects the problem.
Signed-off-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
2020-01-22 07:34:19 -05:00
|
|
|
} else if (mp_obj_is_exact_type(arg, &mp_type_int)) {
|
2014-04-03 07:00:54 -04:00
|
|
|
*value = mp_obj_int_get_checked(arg);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2017-09-02 14:19:01 -04:00
|
|
|
bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) {
|
|
|
|
mp_float_t val;
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
if (arg == mp_const_false) {
|
2017-09-02 14:19:01 -04:00
|
|
|
val = 0;
|
2013-12-21 13:17:45 -05:00
|
|
|
} else if (arg == mp_const_true) {
|
2017-09-02 14:19:01 -04:00
|
|
|
val = 1;
|
2019-01-30 02:49:52 -05:00
|
|
|
} else if (mp_obj_is_small_int(arg)) {
|
2020-04-13 14:56:31 -04:00
|
|
|
val = (mp_float_t)MP_OBJ_SMALL_INT_VALUE(arg);
|
2016-12-20 19:46:27 -05:00
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
py/obj: Add static safety checks to mp_obj_is_type().
Commit d96cfd13e3a464862c introduced a regression by breaking existing
users of mp_obj_is_type(.., &mp_obj_bool). This function (and associated
helpers like mp_obj_is_int()) have some specific nuances, and mistakes like
this one can happen again.
This commit adds mp_obj_is_exact_type() which behaves like the the old
mp_obj_is_type(). The new mp_obj_is_type() has the same prototype but it
attempts to statically assert that it's not called with types which should
be checked using mp_obj_is_type(). If called with any of these types: int,
str, bool, NoneType - it will cause a compilation error. Additional
checked types (e.g function types) can be added in the future.
Existing users of mp_obj_is_type() with the now "invalid" types, were
translated to use mp_obj_is_exact_type().
The use of MP_STATIC_ASSERT() is not bulletproof - usually GCC (and other
compilers) can't statically check conditions that are only known during
link-time (like variables' addresses comparison). However, in this case,
GCC is able to statically detect these conditions, probably because it's
the exact same object - `&mp_type_int == &mp_type_int` is detected.
Misuses of this function with runtime-chosen types (e.g:
`mp_obj_type_t *x = ...; mp_obj_is_type(..., x);` won't be detected. MSC
is unable to detect this, so we use MP_STATIC_ASSERT_NOT_MSC().
Compiling with this commit and without the fix for d96cfd13e3a464862c shows
that it detects the problem.
Signed-off-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
2020-01-22 07:34:19 -05:00
|
|
|
} else if (mp_obj_is_exact_type(arg, &mp_type_int)) {
|
2017-09-02 14:19:01 -04:00
|
|
|
val = mp_obj_int_as_float_impl(arg);
|
2016-12-20 19:46:27 -05:00
|
|
|
#endif
|
2015-08-20 18:30:12 -04:00
|
|
|
} else if (mp_obj_is_float(arg)) {
|
2017-09-02 14:19:01 -04:00
|
|
|
val = mp_obj_float_get(arg);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2022-07-04 03:35:46 -04:00
|
|
|
arg = mp_unary_op(MP_UNARY_OP_FLOAT_MAYBE, (mp_obj_t)arg);
|
|
|
|
if (arg != MP_OBJ_NULL && mp_obj_is_float(arg)) {
|
|
|
|
val = mp_obj_float_get(arg);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-02 14:19:01 -04:00
|
|
|
}
|
|
|
|
*value = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_float_t mp_obj_get_float(mp_obj_t arg) {
|
|
|
|
mp_float_t val;
|
|
|
|
|
|
|
|
if (!mp_obj_get_float_maybe(arg, &val)) {
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("can't convert to float"));
|
2019-09-26 08:52:04 -04:00
|
|
|
#else
|
|
|
|
mp_raise_msg_varg(&mp_type_TypeError,
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("can't convert %s to float"), mp_obj_get_type_str(arg));
|
2019-09-26 08:52:04 -04:00
|
|
|
#endif
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2017-09-02 14:19:01 -04:00
|
|
|
|
|
|
|
return val;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
|
2014-06-19 18:48:35 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_COMPLEX
|
2020-06-21 20:21:02 -04:00
|
|
|
bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
|
2022-07-25 01:23:48 -04:00
|
|
|
if (mp_obj_get_float_maybe(arg, real)) {
|
2013-12-17 13:27:24 -05:00
|
|
|
*imag = 0;
|
2019-01-30 02:49:52 -05:00
|
|
|
} else if (mp_obj_is_type(arg, &mp_type_complex)) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_complex_get(arg, real, imag);
|
2013-12-17 13:27:24 -05:00
|
|
|
} else {
|
2022-07-04 03:35:46 -04:00
|
|
|
arg = mp_unary_op(MP_UNARY_OP_COMPLEX_MAYBE, (mp_obj_t)arg);
|
|
|
|
if (arg != MP_OBJ_NULL && mp_obj_is_type(arg, &mp_type_complex)) {
|
|
|
|
mp_obj_complex_get(arg, real, imag);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-21 20:21:02 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
|
|
|
|
if (!mp_obj_get_complex_maybe(arg, real, imag)) {
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("can't convert to complex"));
|
2019-09-26 08:52:04 -04:00
|
|
|
#else
|
|
|
|
mp_raise_msg_varg(&mp_type_TypeError,
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("can't convert %s to complex"), mp_obj_get_type_str(arg));
|
2019-09-26 08:52:04 -04:00
|
|
|
#endif
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-06-19 18:48:35 -04:00
|
|
|
#endif
|
2013-12-17 13:27:24 -05:00
|
|
|
|
2016-05-04 05:19:08 -04:00
|
|
|
// note: returned value in *items may point to the interior of a GC block
|
2017-03-25 04:35:08 -04:00
|
|
|
void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) {
|
2019-01-30 02:49:52 -05:00
|
|
|
if (mp_obj_is_type(o, &mp_type_tuple)) {
|
2014-03-24 06:47:13 -04:00
|
|
|
mp_obj_tuple_get(o, len, items);
|
2019-01-30 02:49:52 -05:00
|
|
|
} else if (mp_obj_is_type(o, &mp_type_list)) {
|
2014-03-24 06:47:13 -04:00
|
|
|
mp_obj_list_get(o, len, items);
|
|
|
|
} else {
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("expected tuple/list"));
|
2019-09-26 08:52:04 -04:00
|
|
|
#else
|
|
|
|
mp_raise_msg_varg(&mp_type_TypeError,
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("object '%s' isn't a tuple or list"), mp_obj_get_type_str(o));
|
2019-09-26 08:52:04 -04:00
|
|
|
#endif
|
2014-03-24 06:47:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 05:19:08 -04:00
|
|
|
// note: returned value in *items may point to the interior of a GC block
|
2017-03-26 04:20:06 -04:00
|
|
|
void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) {
|
2017-03-25 04:35:08 -04:00
|
|
|
size_t seq_len;
|
2014-08-30 10:17:47 -04:00
|
|
|
mp_obj_get_array(o, &seq_len, items);
|
|
|
|
if (seq_len != len) {
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("tuple/list has wrong length"));
|
2019-09-26 08:52:04 -04:00
|
|
|
#else
|
|
|
|
mp_raise_msg_varg(&mp_type_ValueError,
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("requested length %d but object has length %d"), (int)len, (int)seq_len);
|
2019-09-26 08:52:04 -04:00
|
|
|
#endif
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 01:57:16 -04:00
|
|
|
// is_slice determines whether the index is a slice index
|
2017-03-23 01:17:40 -04:00
|
|
|
size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice) {
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t i;
|
2019-01-30 02:49:52 -05:00
|
|
|
if (mp_obj_is_small_int(index)) {
|
2014-04-11 06:40:38 -04:00
|
|
|
i = MP_OBJ_SMALL_INT_VALUE(index);
|
|
|
|
} else if (!mp_obj_get_int_maybe(index, &i)) {
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("indices must be integers"));
|
2019-09-26 08:52:04 -04:00
|
|
|
#else
|
|
|
|
mp_raise_msg_varg(&mp_type_TypeError,
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("%q indices must be integers, not %s"),
|
2019-09-26 08:52:04 -04:00
|
|
|
type->name, mp_obj_get_type_str(index));
|
|
|
|
#endif
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-03-13 01:57:16 -04:00
|
|
|
|
|
|
|
if (i < 0) {
|
2014-03-13 03:29:15 -04:00
|
|
|
i += len;
|
2014-03-13 01:57:16 -04:00
|
|
|
}
|
|
|
|
if (is_slice) {
|
2014-03-13 03:29:15 -04:00
|
|
|
if (i < 0) {
|
|
|
|
i = 0;
|
2015-01-16 12:47:07 -05:00
|
|
|
} else if ((mp_uint_t)i > len) {
|
2014-03-13 03:29:15 -04:00
|
|
|
i = len;
|
|
|
|
}
|
2014-03-13 01:57:16 -04:00
|
|
|
} else {
|
2015-01-16 12:47:07 -05:00
|
|
|
if (i < 0 || (mp_uint_t)i >= len) {
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("index out of range"));
|
2019-09-26 08:52:04 -04:00
|
|
|
#else
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg_varg(&mp_type_IndexError, MP_ERROR_TEXT("%q index out of range"), type->name);
|
2019-09-26 08:52:04 -04:00
|
|
|
#endif
|
2014-03-13 03:29:15 -04:00
|
|
|
}
|
2014-03-13 01:57:16 -04:00
|
|
|
}
|
2017-03-23 01:17:40 -04:00
|
|
|
|
|
|
|
// By this point 0 <= i <= len and so fits in a size_t
|
|
|
|
return (size_t)i;
|
2013-12-17 13:27:24 -05:00
|
|
|
}
|
2014-01-10 06:25:03 -05:00
|
|
|
|
2014-08-22 16:48:30 -04:00
|
|
|
mp_obj_t mp_obj_id(mp_obj_t o_in) {
|
|
|
|
mp_int_t id = (mp_int_t)o_in;
|
2019-01-30 02:49:52 -05:00
|
|
|
if (!mp_obj_is_obj(o_in)) {
|
2014-08-22 16:48:30 -04:00
|
|
|
return mp_obj_new_int(id);
|
|
|
|
} else if (id >= 0) {
|
|
|
|
// Many OSes and CPUs have affinity for putting "user" memories
|
|
|
|
// into low half of address space, and "system" into upper half.
|
|
|
|
// We're going to take advantage of that and return small int
|
|
|
|
// (signed) for such "user" addresses.
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(id);
|
|
|
|
} else {
|
|
|
|
// If that didn't work, well, let's return long int, just as
|
2017-05-29 03:08:14 -04:00
|
|
|
// a (big) positive value, so it will never clash with the range
|
2014-08-22 16:48:30 -04:00
|
|
|
// of small int returned in previous case.
|
|
|
|
return mp_obj_new_int_from_uint((mp_uint_t)id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-12 13:33:40 -04:00
|
|
|
// will raise a TypeError if object has no length
|
|
|
|
mp_obj_t mp_obj_len(mp_obj_t o_in) {
|
|
|
|
mp_obj_t len = mp_obj_len_maybe(o_in);
|
|
|
|
if (len == MP_OBJ_NULL) {
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("object has no len"));
|
2019-09-26 08:52:04 -04:00
|
|
|
#else
|
|
|
|
mp_raise_msg_varg(&mp_type_TypeError,
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("object of type '%s' has no len()"), mp_obj_get_type_str(o_in));
|
2019-09-26 08:52:04 -04:00
|
|
|
#endif
|
2014-08-12 13:33:40 -04:00
|
|
|
} else {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-11 14:22:29 -05:00
|
|
|
// may return MP_OBJ_NULL
|
2014-01-10 06:25:03 -05:00
|
|
|
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
|
2014-06-13 16:37:18 -04:00
|
|
|
if (
|
|
|
|
#if !MICROPY_PY_BUILTINS_STR_UNICODE
|
|
|
|
// It's simple - unicode is slow, non-unicode is fast
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_obj_is_str(o_in) ||
|
2014-06-13 16:37:18 -04:00
|
|
|
#endif
|
2019-01-30 02:49:52 -05:00
|
|
|
mp_obj_is_type(o_in, &mp_type_bytes)) {
|
2015-09-03 18:01:07 -04:00
|
|
|
GET_STR_LEN(o_in, l);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(l);
|
2014-01-10 06:25:03 -05:00
|
|
|
} else {
|
2020-01-08 19:01:14 -05:00
|
|
|
const mp_obj_type_t *type = mp_obj_get_type(o_in);
|
2014-01-29 21:37:19 -05:00
|
|
|
if (type->unary_op != NULL) {
|
2014-05-21 14:42:43 -04:00
|
|
|
return type->unary_op(MP_UNARY_OP_LEN, o_in);
|
2014-01-30 05:05:33 -05:00
|
|
|
} else {
|
|
|
|
return MP_OBJ_NULL;
|
2014-01-29 21:37:19 -05:00
|
|
|
}
|
2014-01-10 06:25:03 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-20 11:37:30 -05:00
|
|
|
|
2014-04-17 17:10:53 -04:00
|
|
|
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
|
2020-01-08 19:01:14 -05:00
|
|
|
const mp_obj_type_t *type = mp_obj_get_type(base);
|
2014-04-17 17:10:53 -04:00
|
|
|
if (type->subscr != NULL) {
|
|
|
|
mp_obj_t ret = type->subscr(base, index, value);
|
2014-05-21 14:42:43 -04:00
|
|
|
if (ret != MP_OBJ_NULL) {
|
2014-04-17 17:10:53 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
// TODO: call base classes here?
|
|
|
|
}
|
|
|
|
if (value == MP_OBJ_NULL) {
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item deletion"));
|
2019-09-26 08:52:04 -04:00
|
|
|
#else
|
|
|
|
mp_raise_msg_varg(&mp_type_TypeError,
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("'%s' object doesn't support item deletion"), mp_obj_get_type_str(base));
|
2019-09-26 08:52:04 -04:00
|
|
|
#endif
|
2014-04-17 17:10:53 -04:00
|
|
|
} else if (value == MP_OBJ_SENTINEL) {
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("object isn't subscriptable"));
|
2019-09-26 08:52:04 -04:00
|
|
|
#else
|
|
|
|
mp_raise_msg_varg(&mp_type_TypeError,
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("'%s' object isn't subscriptable"), mp_obj_get_type_str(base));
|
2019-09-26 08:52:04 -04:00
|
|
|
#endif
|
2014-04-17 17:10:53 -04:00
|
|
|
} else {
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item assignment"));
|
2019-09-26 08:52:04 -04:00
|
|
|
#else
|
|
|
|
mp_raise_msg_varg(&mp_type_TypeError,
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("'%s' object doesn't support item assignment"), mp_obj_get_type_str(base));
|
2019-09-26 08:52:04 -04:00
|
|
|
#endif
|
2014-04-17 17:10:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 11:37:30 -05:00
|
|
|
// Return input argument. Useful as .getiter for objects which are
|
|
|
|
// their own iterators, etc.
|
|
|
|
mp_obj_t mp_identity(mp_obj_t self) {
|
|
|
|
return self;
|
|
|
|
}
|
2014-02-08 14:57:19 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
|
2014-04-08 17:25:28 -04:00
|
|
|
|
2016-01-09 18:14:54 -05:00
|
|
|
mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) {
|
|
|
|
(void)iter_buf;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-08-30 09:28:06 -04:00
|
|
|
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
|
2020-01-08 19:01:14 -05:00
|
|
|
const mp_obj_type_t *type = mp_obj_get_type(obj);
|
2021-07-15 00:31:06 -04:00
|
|
|
if (type->buffer == NULL) {
|
2014-04-08 17:25:28 -04:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-15 00:31:06 -04:00
|
|
|
int ret = type->buffer(obj, bufinfo, flags);
|
2014-08-10 04:46:10 -04:00
|
|
|
if (ret != 0) {
|
2014-04-08 17:25:28 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-30 09:28:06 -04:00
|
|
|
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
|
2014-04-18 17:59:24 -04:00
|
|
|
if (!mp_get_buffer(obj, bufinfo, flags)) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("object with buffer protocol required"));
|
2014-04-08 17:25:28 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-11 08:25:19 -04:00
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
|
2015-05-11 08:25:19 -04:00
|
|
|
switch (op) {
|
|
|
|
case MP_UNARY_OP_HASH:
|
|
|
|
return MP_OBJ_NEW_SMALL_INT((mp_uint_t)o_in);
|
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
|
|
|
}
|
|
|
|
}
|