summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2020-01-09 11:01:14 +1100
committerDamien George <damien.p.george@gmail.com>2020-01-09 11:25:26 +1100
commitbfbd94401d9cf658fc50b2e45896aba300a7af71 (patch)
tree21aec2207f432658083a26ed5172f9ce0e57ee43 /py
parente3187b052f14872fdb5e2d2338d359013a544fae (diff)
py: Make mp_obj_get_type() return a const ptr to mp_obj_type_t.
Most types are in rodata/ROM, and mp_obj_base_t.type is a constant pointer, so enforce this const-ness throughout the code base. If a type ever needs to be modified (eg a user type) then a simple cast can be used.
Diffstat (limited to 'py')
-rw-r--r--py/builtinhelp.c2
-rw-r--r--py/nativeglue.h2
-rw-r--r--py/obj.c24
-rw-r--r--py/obj.h2
-rw-r--r--py/objfun.c2
-rw-r--r--py/objstr.c6
-rw-r--r--py/objstrunicode.c2
-rw-r--r--py/objtuple.c2
-rw-r--r--py/objtype.c6
-rw-r--r--py/opmethods.c8
-rw-r--r--py/runtime.c18
-rw-r--r--py/sequence.c2
-rw-r--r--py/stream.c2
13 files changed, 39 insertions, 39 deletions
diff --git a/py/builtinhelp.c b/py/builtinhelp.c
index 8f162d885..033242b3d 100644
--- a/py/builtinhelp.c
+++ b/py/builtinhelp.c
@@ -134,7 +134,7 @@ STATIC void mp_help_print_obj(const mp_obj_t obj) {
}
#endif
- mp_obj_type_t *type = mp_obj_get_type(obj);
+ const mp_obj_type_t *type = mp_obj_get_type(obj);
// try to print something sensible about the given object
mp_print_str(MP_PYTHON_PRINTER, "object ");
diff --git a/py/nativeglue.h b/py/nativeglue.h
index 021e7a8ec..1b6d9cc7a 100644
--- a/py/nativeglue.h
+++ b/py/nativeglue.h
@@ -146,7 +146,7 @@ typedef struct _mp_fun_table_t {
NORETURN // Only certain compilers support no-return attributes in function pointer declarations
#endif
void (*raise_msg)(const mp_obj_type_t *exc_type, const char *msg);
- mp_obj_type_t *(*obj_get_type)(mp_const_obj_t o_in);
+ const mp_obj_type_t *(*obj_get_type)(mp_const_obj_t o_in);
mp_obj_t (*obj_new_str)(const char* data, size_t len);
mp_obj_t (*obj_new_bytes)(const byte* data, size_t len);
mp_obj_t (*obj_new_bytearray_by_ref)(size_t n, void *items);
diff --git a/py/obj.c b/py/obj.c
index 4588d896a..f2a884754 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -37,18 +37,18 @@
#include "py/stackctrl.h"
#include "py/stream.h" // for mp_obj_print
-mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
+const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
if (mp_obj_is_small_int(o_in)) {
- return (mp_obj_type_t*)&mp_type_int;
+ return &mp_type_int;
} else if (mp_obj_is_qstr(o_in)) {
- return (mp_obj_type_t*)&mp_type_str;
+ return &mp_type_str;
#if MICROPY_PY_BUILTINS_FLOAT
} else if (mp_obj_is_float(o_in)) {
- return (mp_obj_type_t*)&mp_type_float;
+ return &mp_type_float;
#endif
} else {
const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
- return (mp_obj_type_t*)o->type;
+ return o->type;
}
}
@@ -65,7 +65,7 @@ void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
return;
}
#endif
- mp_obj_type_t *type = mp_obj_get_type(o_in);
+ const mp_obj_type_t *type = mp_obj_get_type(o_in);
if (type->print != NULL) {
type->print((mp_print_t*)print, o_in, kind);
} else {
@@ -119,7 +119,7 @@ bool mp_obj_is_true(mp_obj_t arg) {
return 1;
}
} else {
- mp_obj_type_t *type = mp_obj_get_type(arg);
+ const mp_obj_type_t *type = mp_obj_get_type(arg);
if (type->unary_op != NULL) {
mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg);
if (result != MP_OBJ_NULL) {
@@ -139,7 +139,7 @@ bool mp_obj_is_true(mp_obj_t arg) {
}
bool mp_obj_is_callable(mp_obj_t o_in) {
- mp_call_fun_t call = mp_obj_get_type(o_in)->call;
+ const mp_call_fun_t call = mp_obj_get_type(o_in)->call;
if (call != mp_obj_instance_call) {
return call != NULL;
}
@@ -209,7 +209,7 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
}
// generic type, call binary_op(MP_BINARY_OP_EQUAL)
- mp_obj_type_t *type = mp_obj_get_type(o1);
+ const mp_obj_type_t *type = mp_obj_get_type(o1);
if (type->binary_op != NULL) {
mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
if (r != MP_OBJ_NULL) {
@@ -451,7 +451,7 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
GET_STR_LEN(o_in, l);
return MP_OBJ_NEW_SMALL_INT(l);
} else {
- mp_obj_type_t *type = mp_obj_get_type(o_in);
+ const mp_obj_type_t *type = mp_obj_get_type(o_in);
if (type->unary_op != NULL) {
return type->unary_op(MP_UNARY_OP_LEN, o_in);
} else {
@@ -461,7 +461,7 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
}
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
- mp_obj_type_t *type = mp_obj_get_type(base);
+ const mp_obj_type_t *type = mp_obj_get_type(base);
if (type->subscr != NULL) {
mp_obj_t ret = type->subscr(base, index, value);
if (ret != MP_OBJ_NULL) {
@@ -506,7 +506,7 @@ mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) {
}
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
- mp_obj_type_t *type = mp_obj_get_type(obj);
+ const mp_obj_type_t *type = mp_obj_get_type(obj);
if (type->buffer_p.get_buffer == NULL) {
return false;
}
diff --git a/py/obj.h b/py/obj.h
index f14447491..d98a0470d 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -669,7 +669,7 @@ mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf);
mp_obj_t mp_obj_new_module(qstr module_name);
mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items);
-mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
+const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
const char *mp_obj_get_type_str(mp_const_obj_t o_in);
bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type);
diff --git a/py/objfun.c b/py/objfun.c
index 7051f3476..984d2000a 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -455,7 +455,7 @@ STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
size_t l;
return (mp_uint_t)mp_obj_str_get_data(obj, &l);
} else {
- mp_obj_type_t *type = mp_obj_get_type(obj);
+ const mp_obj_type_t *type = mp_obj_get_type(obj);
#if MICROPY_PY_BUILTINS_FLOAT
if (type == &mp_type_float) {
// convert float to int (could also pass in float registers)
diff --git a/py/objstr.c b/py/objstr.c
index 822c9fc41..454d1cbcc 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -320,7 +320,7 @@ mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i
}
// from now on we need lhs type and data, so extract them
- mp_obj_type_t *lhs_type = mp_obj_get_type(lhs_in);
+ const mp_obj_type_t *lhs_type = mp_obj_get_type(lhs_in);
GET_STR_DATA_LEN(lhs_in, lhs_data, lhs_len);
// check for multiply
@@ -420,7 +420,7 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s
// This is used for both bytes and 8-bit strings. This is not used for unicode strings.
STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
- mp_obj_type_t *type = mp_obj_get_type(self_in);
+ const mp_obj_type_t *type = mp_obj_get_type(self_in);
GET_STR_DATA_LEN(self_in, self_data, self_len);
if (value == MP_OBJ_SENTINEL) {
// load
@@ -1743,7 +1743,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
#if MICROPY_PY_BUILTINS_STR_PARTITION
STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, int direction) {
mp_check_self(mp_obj_is_str_or_bytes(self_in));
- mp_obj_type_t *self_type = mp_obj_get_type(self_in);
+ const mp_obj_type_t *self_type = mp_obj_get_type(self_in);
if (self_type != mp_obj_get_type(arg)) {
bad_implicit_conversion(arg);
}
diff --git a/py/objstrunicode.c b/py/objstrunicode.c
index 5a127243b..e5a57ab94 100644
--- a/py/objstrunicode.c
+++ b/py/objstrunicode.c
@@ -176,7 +176,7 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s
}
STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
- mp_obj_type_t *type = mp_obj_get_type(self_in);
+ const mp_obj_type_t *type = mp_obj_get_type(self_in);
assert(type == &mp_type_str);
GET_STR_DATA_LEN(self_in, self_data, self_len);
if (value == MP_OBJ_SENTINEL) {
diff --git a/py/objtuple.c b/py/objtuple.c
index 740e0795b..10a5e586f 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -105,7 +105,7 @@ STATIC mp_obj_t mp_obj_tuple_make_new(const mp_obj_type_t *type_in, size_t n_arg
// Don't pass MP_BINARY_OP_NOT_EQUAL here
STATIC mp_obj_t tuple_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in) {
mp_check_self(mp_obj_is_tuple_compatible(self_in));
- mp_obj_type_t *another_type = mp_obj_get_type(another_in);
+ const mp_obj_type_t *another_type = mp_obj_get_type(another_in);
mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
if (another_type->getiter != mp_obj_tuple_getiter) {
// Slow path for user subclasses
diff --git a/py/objtype.c b/py/objtype.c
index 5d4dd7d2f..2ea3f1c18 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -897,7 +897,7 @@ mp_obj_t mp_obj_instance_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf)
if (member[0] == MP_OBJ_NULL) {
return MP_OBJ_NULL;
} else if (member[0] == MP_OBJ_SENTINEL) {
- mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
+ const mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
if (iter_buf == NULL) {
iter_buf = m_new_obj(mp_obj_iter_buf_t);
}
@@ -919,7 +919,7 @@ STATIC mp_int_t instance_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo,
};
mp_obj_class_lookup(&lookup, self->base.type);
if (member[0] == MP_OBJ_SENTINEL) {
- mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
+ const mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
return type->buffer_p.get_buffer(self->subobj[0], bufinfo, flags);
} else {
return 1; // object does not support buffer protocol
@@ -1393,7 +1393,7 @@ STATIC mp_obj_t mp_builtin_isinstance(mp_obj_t object, mp_obj_t classinfo) {
MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_isinstance_obj, mp_builtin_isinstance);
mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type) {
- mp_obj_type_t *self_type = mp_obj_get_type(self_in);
+ const mp_obj_type_t *self_type = mp_obj_get_type(self_in);
if (!mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(self_type), native_type)) {
return MP_OBJ_NULL;
}
diff --git a/py/opmethods.c b/py/opmethods.c
index 247fa5bbc..595cc088b 100644
--- a/py/opmethods.c
+++ b/py/opmethods.c
@@ -28,25 +28,25 @@
#include "py/builtin.h"
STATIC mp_obj_t op_getitem(mp_obj_t self_in, mp_obj_t key_in) {
- mp_obj_type_t *type = mp_obj_get_type(self_in);
+ const mp_obj_type_t *type = mp_obj_get_type(self_in);
return type->subscr(self_in, key_in, MP_OBJ_SENTINEL);
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_op_getitem_obj, op_getitem);
STATIC mp_obj_t op_setitem(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) {
- mp_obj_type_t *type = mp_obj_get_type(self_in);
+ const mp_obj_type_t *type = mp_obj_get_type(self_in);
return type->subscr(self_in, key_in, value_in);
}
MP_DEFINE_CONST_FUN_OBJ_3(mp_op_setitem_obj, op_setitem);
STATIC mp_obj_t op_delitem(mp_obj_t self_in, mp_obj_t key_in) {
- mp_obj_type_t *type = mp_obj_get_type(self_in);
+ const mp_obj_type_t *type = mp_obj_get_type(self_in);
return type->subscr(self_in, key_in, MP_OBJ_NULL);
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_op_delitem_obj, op_delitem);
STATIC mp_obj_t op_contains(mp_obj_t lhs_in, mp_obj_t rhs_in) {
- mp_obj_type_t *type = mp_obj_get_type(lhs_in);
+ const mp_obj_type_t *type = mp_obj_get_type(lhs_in);
return type->binary_op(MP_BINARY_OP_CONTAINS, lhs_in, rhs_in);
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_op_contains_obj, op_contains);
diff --git a/py/runtime.c b/py/runtime.c
index d5511236d..26b473bbe 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -276,7 +276,7 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
}
return MP_OBJ_NEW_SMALL_INT(h);
} else {
- mp_obj_type_t *type = mp_obj_get_type(arg);
+ const mp_obj_type_t *type = mp_obj_get_type(arg);
if (type->unary_op != NULL) {
mp_obj_t result = type->unary_op(op, arg);
if (result != MP_OBJ_NULL) {
@@ -560,7 +560,7 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
}
// generic binary_op supplied by type
- mp_obj_type_t *type;
+ const mp_obj_type_t *type;
generic_binary_op:
type = mp_obj_get_type(lhs);
if (type->binary_op != NULL) {
@@ -636,7 +636,7 @@ mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, size_t n_args, size_t n_kw, cons
DEBUG_OP_printf("calling function %p(n_args=" UINT_FMT ", n_kw=" UINT_FMT ", args=%p)\n", fun_in, n_args, n_kw, args);
// get the type
- mp_obj_type_t *type = mp_obj_get_type(fun_in);
+ const mp_obj_type_t *type = mp_obj_get_type(fun_in);
// do the call
if (type->call != NULL) {
@@ -1067,7 +1067,7 @@ void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest) {
dest[1] = MP_OBJ_NULL;
// get the type
- mp_obj_type_t *type = mp_obj_get_type(obj);
+ const mp_obj_type_t *type = mp_obj_get_type(obj);
// look for built-in names
#if MICROPY_CPYTHON_COMPAT
@@ -1138,7 +1138,7 @@ void mp_load_method_protected(mp_obj_t obj, qstr attr, mp_obj_t *dest, bool catc
void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
- mp_obj_type_t *type = mp_obj_get_type(base);
+ const mp_obj_type_t *type = mp_obj_get_type(base);
if (type->attr != NULL) {
mp_obj_t dest[2] = {MP_OBJ_SENTINEL, value};
type->attr(base, attr, dest);
@@ -1158,7 +1158,7 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
assert(o_in);
- mp_obj_type_t *type = mp_obj_get_type(o_in);
+ const mp_obj_type_t *type = mp_obj_get_type(o_in);
// Check for native getiter which is the identity. We handle this case explicitly
// so we don't unnecessarily allocate any RAM for the iter_buf, which won't be used.
@@ -1203,7 +1203,7 @@ mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
// may return MP_OBJ_STOP_ITERATION as an optimisation instead of raise StopIteration()
// may also raise StopIteration()
mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
- mp_obj_type_t *type = mp_obj_get_type(o_in);
+ const mp_obj_type_t *type = mp_obj_get_type(o_in);
if (type->iternext != NULL) {
return type->iternext(o_in);
} else {
@@ -1228,7 +1228,7 @@ mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
// may raise other exceptions
mp_obj_t mp_iternext(mp_obj_t o_in) {
MP_STACK_CHECK(); // enumerate, filter, map and zip can recursively call mp_iternext
- mp_obj_type_t *type = mp_obj_get_type(o_in);
+ const mp_obj_type_t *type = mp_obj_get_type(o_in);
if (type->iternext != NULL) {
return type->iternext(o_in);
} else {
@@ -1263,7 +1263,7 @@ mp_obj_t mp_iternext(mp_obj_t o_in) {
// TODO: Unclear what to do with StopIterarion exception here.
mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
assert((send_value != MP_OBJ_NULL) ^ (throw_value != MP_OBJ_NULL));
- mp_obj_type_t *type = mp_obj_get_type(self_in);
+ const mp_obj_type_t *type = mp_obj_get_type(self_in);
if (type == &mp_type_gen_instance) {
return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val);
diff --git a/py/sequence.c b/py/sequence.c
index 15e925000..94a9bb9d5 100644
--- a/py/sequence.c
+++ b/py/sequence.c
@@ -183,7 +183,7 @@ bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, size_t len1, const mp
// Special-case of index() which searches for mp_obj_t
mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args) {
- mp_obj_type_t *type = mp_obj_get_type(args[0]);
+ const mp_obj_type_t *type = mp_obj_get_type(args[0]);
mp_obj_t value = args[1];
size_t start = 0;
size_t stop = len;
diff --git a/py/stream.c b/py/stream.c
index 762cd0fc0..d795681cb 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -85,7 +85,7 @@ mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf_, mp_uint_t size, int *errcode
}
const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
- mp_obj_type_t *type = mp_obj_get_type(self_in);
+ const mp_obj_type_t *type = mp_obj_get_type(self_in);
const mp_stream_p_t *stream_p = type->protocol;
if (stream_p == NULL
|| ((flags & MP_STREAM_OP_READ) && stream_p->read == NULL)