mirror of
https://github.com/micropython/micropython.git
synced 2026-01-07 04:30:24 +01:00
py: Wrap all obj-ptr conversions in MP_OBJ_TO_PTR/MP_OBJ_FROM_PTR.
This allows the mp_obj_t type to be configured to something other than a pointer-sized primitive type. This patch also includes additional changes to allow the code to compile when sizeof(mp_uint_t) != sizeof(void*), such as using size_t instead of mp_uint_t, and various casts.
This commit is contained in:
@@ -78,7 +78,7 @@ STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_ui
|
||||
#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
|
||||
STATIC void array_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||
(void)kind;
|
||||
mp_obj_array_t *o = o_in;
|
||||
mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
|
||||
if (o->typecode == BYTEARRAY_TYPECODE) {
|
||||
mp_print_str(print, "bytearray(b");
|
||||
mp_str_print_quoted(print, o->items, o->len, true);
|
||||
@@ -138,7 +138,7 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
|
||||
mp_uint_t len = bufinfo.len / sz;
|
||||
mp_obj_array_t *o = array_new(typecode, len);
|
||||
memcpy(o->items, bufinfo.buf, len * sz);
|
||||
return o;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
mp_uint_t len;
|
||||
@@ -157,13 +157,13 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
|
||||
mp_uint_t i = 0;
|
||||
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
|
||||
if (len == 0) {
|
||||
array_append(array, item);
|
||||
array_append(MP_OBJ_FROM_PTR(array), item);
|
||||
} else {
|
||||
mp_binary_set_val_array(typecode, array->items, i++, item);
|
||||
}
|
||||
}
|
||||
|
||||
return array;
|
||||
return MP_OBJ_FROM_PTR(array);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -178,7 +178,7 @@ STATIC mp_obj_t array_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
|
||||
|
||||
if (n_args == 1) {
|
||||
// 1 arg: make an empty array
|
||||
return array_new(*typecode, 0);
|
||||
return MP_OBJ_FROM_PTR(array_new(*typecode, 0));
|
||||
} else {
|
||||
// 2 args: construct the array from the given object
|
||||
return array_construct(*typecode, args[1]);
|
||||
@@ -193,13 +193,13 @@ STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
|
||||
|
||||
if (n_args == 0) {
|
||||
// no args: construct an empty bytearray
|
||||
return array_new(BYTEARRAY_TYPECODE, 0);
|
||||
return MP_OBJ_FROM_PTR(array_new(BYTEARRAY_TYPECODE, 0));
|
||||
} else if (MP_OBJ_IS_INT(args[0])) {
|
||||
// 1 arg, an integer: construct a blank bytearray of that length
|
||||
mp_uint_t len = mp_obj_get_int(args[0]);
|
||||
mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
|
||||
memset(o->items, 0, len);
|
||||
return o;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
} else {
|
||||
// 1 arg: construct the bytearray from that
|
||||
return array_construct(BYTEARRAY_TYPECODE, args[0]);
|
||||
@@ -216,7 +216,7 @@ mp_obj_t mp_obj_new_memoryview(byte typecode, mp_uint_t nitems, void *items) {
|
||||
self->free = 0;
|
||||
self->len = nitems;
|
||||
self->items = items;
|
||||
return self;
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
STATIC mp_obj_t memoryview_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||
@@ -230,21 +230,21 @@ STATIC mp_obj_t memoryview_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
|
||||
|
||||
mp_obj_array_t *self = mp_obj_new_memoryview(bufinfo.typecode,
|
||||
mp_obj_array_t *self = MP_OBJ_TO_PTR(mp_obj_new_memoryview(bufinfo.typecode,
|
||||
bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL),
|
||||
bufinfo.buf);
|
||||
bufinfo.buf));
|
||||
|
||||
// test if the object can be written to
|
||||
if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
|
||||
self->typecode |= 0x80; // used to indicate writable buffer
|
||||
}
|
||||
|
||||
return self;
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
#endif
|
||||
|
||||
STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
|
||||
mp_obj_array_t *o = o_in;
|
||||
mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
|
||||
switch (op) {
|
||||
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->len != 0);
|
||||
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
|
||||
@@ -253,7 +253,7 @@ STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
|
||||
}
|
||||
|
||||
STATIC mp_obj_t array_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
||||
mp_obj_array_t *lhs = lhs_in;
|
||||
mp_obj_array_t *lhs = MP_OBJ_TO_PTR(lhs_in);
|
||||
switch (op) {
|
||||
case MP_BINARY_OP_ADD: {
|
||||
// allow to add anything that has the buffer protocol (extension to CPython)
|
||||
@@ -270,7 +270,7 @@ STATIC mp_obj_t array_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in)
|
||||
// note: lhs->len is element count of lhs, lhs_bufinfo.len is byte count
|
||||
mp_obj_array_t *res = array_new(lhs_bufinfo.typecode, lhs->len + rhs_len);
|
||||
mp_seq_cat((byte*)res->items, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_len * sz, byte);
|
||||
return res;
|
||||
return MP_OBJ_FROM_PTR(res);
|
||||
}
|
||||
|
||||
case MP_BINARY_OP_INPLACE_ADD: {
|
||||
@@ -279,8 +279,8 @@ STATIC mp_obj_t array_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in)
|
||||
return MP_OBJ_NULL; // op not supported
|
||||
}
|
||||
#endif
|
||||
array_extend(lhs, rhs_in);
|
||||
return lhs;
|
||||
array_extend(lhs_in, rhs_in);
|
||||
return lhs_in;
|
||||
}
|
||||
|
||||
case MP_BINARY_OP_EQUAL: {
|
||||
@@ -303,7 +303,7 @@ STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
|
||||
// self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
|
||||
assert((MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray))
|
||||
|| (MICROPY_PY_ARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_array)));
|
||||
mp_obj_array_t *self = self_in;
|
||||
mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
if (self->free == 0) {
|
||||
size_t item_sz = mp_binary_get_size('@', self->typecode, NULL);
|
||||
@@ -322,7 +322,7 @@ STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) {
|
||||
// self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
|
||||
assert((MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray))
|
||||
|| (MICROPY_PY_ARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_array)));
|
||||
mp_obj_array_t *self = self_in;
|
||||
mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
// allow to extend by anything that has the buffer protocol (extension to CPython)
|
||||
mp_buffer_info_t arg_bufinfo;
|
||||
@@ -359,7 +359,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
|
||||
// slice deletion
|
||||
return MP_OBJ_NULL; // op not supported
|
||||
} else {
|
||||
mp_obj_array_t *o = self_in;
|
||||
mp_obj_array_t *o = MP_OBJ_TO_PTR(self_in);
|
||||
if (0) {
|
||||
#if MICROPY_PY_BUILTINS_SLICE
|
||||
} else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
|
||||
@@ -373,9 +373,9 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
|
||||
mp_uint_t src_len;
|
||||
void *src_items;
|
||||
size_t item_sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
|
||||
if (MP_OBJ_IS_OBJ(value) && ((mp_obj_base_t*)value)->type->subscr == array_subscr) {
|
||||
if (MP_OBJ_IS_OBJ(value) && ((mp_obj_base_t*)MP_OBJ_TO_PTR(value))->type->subscr == array_subscr) {
|
||||
// value is array, bytearray or memoryview
|
||||
mp_obj_array_t *src_slice = value;
|
||||
mp_obj_array_t *src_slice = MP_OBJ_TO_PTR(value);
|
||||
if (item_sz != mp_binary_get_size('@', src_slice->typecode & TYPECODE_MASK, NULL)) {
|
||||
compat_error:
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "lhs and rhs should be compatible"));
|
||||
@@ -449,7 +449,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
|
||||
res = array_new(o->typecode, slice.stop - slice.start);
|
||||
memcpy(res->items, (uint8_t*)o->items + slice.start * sz, (slice.stop - slice.start) * sz);
|
||||
}
|
||||
return res;
|
||||
return MP_OBJ_FROM_PTR(res);
|
||||
#endif
|
||||
} else {
|
||||
mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false);
|
||||
@@ -475,7 +475,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
|
||||
}
|
||||
|
||||
STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
|
||||
mp_obj_array_t *o = o_in;
|
||||
mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
|
||||
size_t sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
|
||||
bufinfo->buf = o->items;
|
||||
bufinfo->len = o->len * sz;
|
||||
@@ -514,7 +514,7 @@ const mp_obj_type_t mp_type_array = {
|
||||
.binary_op = array_binary_op,
|
||||
.subscr = array_subscr,
|
||||
.buffer_p = { .get_buffer = array_get_buffer },
|
||||
.locals_dict = (mp_obj_t)&array_locals_dict,
|
||||
.locals_dict = (mp_obj_dict_t*)&array_locals_dict,
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -529,7 +529,7 @@ const mp_obj_type_t mp_type_bytearray = {
|
||||
.binary_op = array_binary_op,
|
||||
.subscr = array_subscr,
|
||||
.buffer_p = { .get_buffer = array_get_buffer },
|
||||
.locals_dict = (mp_obj_t)&array_locals_dict,
|
||||
.locals_dict = (mp_obj_dict_t*)&array_locals_dict,
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -556,7 +556,7 @@ mp_uint_t mp_obj_array_len(mp_obj_t self_in) {
|
||||
mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
|
||||
mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
|
||||
memcpy(o->items, items, n);
|
||||
return o;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
// Create bytearray which references specified memory area
|
||||
@@ -567,7 +567,7 @@ mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
|
||||
o->free = 0;
|
||||
o->len = n;
|
||||
o->items = items;
|
||||
return o;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -582,7 +582,7 @@ typedef struct _mp_obj_array_it_t {
|
||||
} mp_obj_array_it_t;
|
||||
|
||||
STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
|
||||
mp_obj_array_it_t *self = self_in;
|
||||
mp_obj_array_it_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
if (self->cur < self->array->len) {
|
||||
return mp_binary_get_val_array(self->array->typecode & TYPECODE_MASK, self->array->items, self->offset + self->cur++);
|
||||
} else {
|
||||
@@ -598,7 +598,7 @@ STATIC const mp_obj_type_t array_it_type = {
|
||||
};
|
||||
|
||||
STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
|
||||
mp_obj_array_t *array = array_in;
|
||||
mp_obj_array_t *array = MP_OBJ_TO_PTR(array_in);
|
||||
mp_obj_array_it_t *o = m_new0(mp_obj_array_it_t, 1);
|
||||
o->base.type = &array_it_type;
|
||||
o->array = array;
|
||||
@@ -607,7 +607,7 @@ STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
|
||||
o->offset = array->free;
|
||||
}
|
||||
#endif
|
||||
return o;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
|
||||
#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW
|
||||
|
||||
Reference in New Issue
Block a user