Skip to content

Commit 8e3c28e

Browse files
committed
Fix memoryview.cast in micropython-coverage
`locals_dict` and `attr` are incompatible, so just use circuitpython-style properties so that a property and a method are both available. this makes no difference in circuitpython, where `MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE` is never enabled
1 parent d078bc3 commit 8e3c28e

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

py/objarray.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "py/runtime.h"
3333
#include "py/binary.h"
34+
#include "py/objproperty.h"
3435
#include "py/objstr.h"
3536
#include "py/objarray.h"
3637

@@ -270,15 +271,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(memoryview_cast_obj, memoryview_cast);
270271
#endif
271272

272273
#if MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE
273-
STATIC void memoryview_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
274-
if (dest[0] != MP_OBJ_NULL) {
275-
return;
276-
}
277-
if (attr == MP_QSTR_itemsize) {
278-
mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in);
279-
dest[0] = MP_OBJ_NEW_SMALL_INT(mp_binary_get_size('@', self->typecode & TYPECODE_MASK, NULL));
280-
}
274+
STATIC mp_obj_t memoryview_itemsize_get(mp_obj_t self_in) {
275+
mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in);
276+
return MP_OBJ_NEW_SMALL_INT(mp_binary_get_size('@', self->typecode & TYPECODE_MASK, NULL));
281277
}
278+
MP_DEFINE_CONST_FUN_OBJ_1(memoryview_itemsize_get_obj, memoryview_itemsize_get);
279+
280+
MP_PROPERTY_GETTER(memoryview_itemsize_obj, (mp_obj_t)&memoryview_itemsize_get_obj);
282281
#endif
283282

284283
#endif
@@ -785,9 +784,14 @@ const mp_obj_type_t mp_type_bytearray = {
785784

786785
#if MICROPY_PY_BUILTINS_MEMORYVIEW
787786

788-
#if MICROPY_CPYTHON_COMPAT
787+
#if MICROPY_CPYTHON_COMPAT || MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE
789788
STATIC const mp_rom_map_elem_t memoryview_locals_dict_table[] = {
789+
#if MICROPY_CPYTHON_COMPAT
790790
{ MP_ROM_QSTR(MP_QSTR_cast), MP_ROM_PTR(&memoryview_cast_obj) },
791+
#endif
792+
#if MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE
793+
{ MP_ROM_QSTR(MP_QSTR_itemsize), MP_ROM_PTR(&memoryview_itemsize_obj) },
794+
#endif
791795
};
792796

793797
STATIC MP_DEFINE_CONST_DICT(memoryview_locals_dict, memoryview_locals_dict_table);
@@ -798,12 +802,9 @@ const mp_obj_type_t mp_type_memoryview = {
798802
.flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE | MP_TYPE_FLAG_EXTENDED,
799803
.name = MP_QSTR_memoryview,
800804
.make_new = memoryview_make_new,
801-
#if MICROPY_CPYTHON_COMPAT
805+
#if MICROPY_CPYTHON_COMPAT || MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE
802806
.locals_dict = (mp_obj_dict_t *)&memoryview_locals_dict,
803807
#endif
804-
#if MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE
805-
.attr = memoryview_attr,
806-
#endif
807808
MP_TYPE_EXTENDED_FIELDS(
808809
.getiter = array_iterator_new,
809810
.unary_op = array_unary_op,

0 commit comments

Comments
 (0)