Skip to content

Commit

Permalink
Drop certain CircuitPython workarounds that are no longer needed
Browse files Browse the repository at this point in the history
 * ndarray_set_value: in CircuitPython 9
 * mp_obj_slice_indices: ditto
 * Use modern MP_REGISTER_MODULE calls: ditto
 * use MP_OBJ_SENTINEL to forward to locals dict (was never necessary?)
  • Loading branch information
jepler committed Sep 22, 2023
1 parent 84f99f1 commit 2df210f
Show file tree
Hide file tree
Showing 15 changed files with 3 additions and 259 deletions.
92 changes: 0 additions & 92 deletions code/ndarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,98 +61,6 @@ void ndarray_set_complex_value(void *p, size_t index, mp_obj_t value) {
}
}

#ifdef CIRCUITPY
void ndarray_set_value(char typecode, void *p, size_t index, mp_obj_t val_in) {
switch (typecode) {
case NDARRAY_INT8:
((signed char *)p)[index] = mp_obj_get_int(val_in);
break;
case NDARRAY_UINT8:
((unsigned char *)p)[index] = mp_obj_get_int(val_in);
break;
case NDARRAY_INT16:
((short *)p)[index] = mp_obj_get_int(val_in);
break;
case NDARRAY_UINT16:
((unsigned short *)p)[index] = mp_obj_get_int(val_in);
break;
case NDARRAY_FLOAT:
((mp_float_t *)p)[index] = mp_obj_get_float(val_in);
break;
#if ULAB_SUPPORTS_COMPLEX
case NDARRAY_COMPLEX:
ndarray_set_complex_value(p, index, val_in);
break;
#endif
}
}
#endif

#if defined(MICROPY_VERSION_MAJOR) && MICROPY_VERSION_MAJOR == 1 && MICROPY_VERSION_MINOR == 11

void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result) {
mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
mp_int_t start, stop, step;

if (self->step == mp_const_none) {
step = 1;
} else {
step = mp_obj_get_int(self->step);
if (step == 0) {
mp_raise_ValueError(translate("slice step can't be zero"));
}
}

if (step > 0) {
// Positive step
if (self->start == mp_const_none) {
start = 0;
} else {
start = mp_obj_get_int(self->start);
if (start < 0) {
start += length;
}
start = MIN(length, MAX(start, 0));
}

if (self->stop == mp_const_none) {
stop = length;
} else {
stop = mp_obj_get_int(self->stop);
if (stop < 0) {
stop += length;
}
stop = MIN(length, MAX(stop, 0));
}
} else {
// Negative step
if (self->start == mp_const_none) {
start = length - 1;
} else {
start = mp_obj_get_int(self->start);
if (start < 0) {
start += length;
}
start = MIN(length - 1, MAX(start, -1));
}

if (self->stop == mp_const_none) {
stop = -1;
} else {
stop = mp_obj_get_int(self->stop);
if (stop < 0) {
stop += length;
}
stop = MIN(length - 1, MAX(stop, -1));
}
}

result->start = start;
result->stop = stop;
result->step = step;
}
#endif /* MICROPY_VERSION v1.11 */

void ndarray_fill_array_iterable(mp_float_t *array, mp_obj_t iterable) {
mp_obj_iter_buf_t x_buf;
mp_obj_t x_item, x_iterable = mp_getiter(iterable, &x_buf);
Expand Down
4 changes: 1 addition & 3 deletions code/ndarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ typedef struct _mp_obj_slice_t {

#if !CIRCUITPY
#define translate(x) MP_ERROR_TEXT(x)
#define ndarray_set_value(a, b, c, d) mp_binary_set_val_array(a, b, c, d)
#else
void ndarray_set_value(char , void *, size_t , mp_obj_t );
#endif

#define ndarray_set_value(a, b, c, d) mp_binary_set_val_array(a, b, c, d)
void ndarray_set_complex_value(void *, size_t , mp_obj_t );

#define NDARRAY_NUMERIC 0
Expand Down
28 changes: 2 additions & 26 deletions code/ndarray_properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,6 @@
#include "numpy/carray/carray.h"
#endif

#ifndef CIRCUITPY

// a somewhat hackish implementation of property getters/setters;
// this functions is hooked into the attr member of ndarray

STATIC void call_local_method(mp_obj_t obj, qstr attr, mp_obj_t *dest) {
const mp_obj_type_t *type = mp_obj_get_type(obj);
while (MP_OBJ_TYPE_HAS_SLOT(type, locals_dict)) {
assert(MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->base.type == &mp_type_dict); // MicroPython restriction, for now
mp_map_t *locals_map = &MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->map;
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
if (elem != NULL) {
mp_convert_member_lookup(obj, type, elem->value, dest);
break;
}
if (!MP_OBJ_TYPE_HAS_SLOT(type, parent)) {
break;
}
type = MP_OBJ_TYPE_GET_SLOT(type, parent);
}
}


void ndarray_properties_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
if (dest[0] == MP_OBJ_NULL) {
switch(attr) {
Expand Down Expand Up @@ -98,7 +75,8 @@ void ndarray_properties_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
#endif
#endif /* ULAB_SUPPORTS_COMPLEX */
default:
call_local_method(self_in, attr, dest);
// forward to locals dict
dest[1] = MP_OBJ_SENTINEL;
break;
}
} else {
Expand All @@ -119,5 +97,3 @@ void ndarray_properties_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
}
}
}

#endif /* CIRCUITPY */
70 changes: 0 additions & 70 deletions code/ndarray_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,74 +22,6 @@
#include "ndarray.h"
#include "numpy/ndarray/ndarray_iter.h"

#if CIRCUITPY
typedef struct _mp_obj_property_t {
mp_obj_base_t base;
mp_obj_t proxy[3]; // getter, setter, deleter
} mp_obj_property_t;

#if NDARRAY_HAS_DTYPE
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_get_dtype_obj, ndarray_dtype);
STATIC const mp_obj_property_t ndarray_dtype_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&ndarray_get_dtype_obj,
mp_const_none,
mp_const_none },
};
#endif /* NDARRAY_HAS_DTYPE */

#if NDARRAY_HAS_FLATITER
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_flatiter_make_new_obj, ndarray_flatiter_make_new);
STATIC const mp_obj_property_t ndarray_flat_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&ndarray_flatiter_make_new_obj,
mp_const_none,
mp_const_none },
};
#endif /* NDARRAY_HAS_FLATITER */

#if NDARRAY_HAS_ITEMSIZE
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_get_itemsize_obj, ndarray_itemsize);
STATIC const mp_obj_property_t ndarray_itemsize_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&ndarray_get_itemsize_obj,
mp_const_none,
mp_const_none },
};
#endif /* NDARRAY_HAS_ITEMSIZE */

#if NDARRAY_HAS_SHAPE
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_get_shape_obj, ndarray_shape);
STATIC const mp_obj_property_t ndarray_shape_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&ndarray_get_shape_obj,
mp_const_none,
mp_const_none },
};
#endif /* NDARRAY_HAS_SHAPE */

#if NDARRAY_HAS_SIZE
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_get_size_obj, ndarray_size);
STATIC const mp_obj_property_t ndarray_size_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&ndarray_get_size_obj,
mp_const_none,
mp_const_none },
};
#endif /* NDARRAY_HAS_SIZE */

#if NDARRAY_HAS_STRIDES
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_get_strides_obj, ndarray_strides);
STATIC const mp_obj_property_t ndarray_strides_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&ndarray_get_strides_obj,
mp_const_none,
mp_const_none },
};
#endif /* NDARRAY_HAS_STRIDES */

#else

void ndarray_properties_attr(mp_obj_t , qstr , mp_obj_t *);

#if NDARRAY_HAS_DTYPE
Expand All @@ -116,6 +48,4 @@ MP_DEFINE_CONST_FUN_OBJ_1(ndarray_size_obj, ndarray_size);
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_strides_obj, ndarray_strides);
#endif

#endif /* CIRCUITPY */

#endif
4 changes: 0 additions & 4 deletions code/numpy/fft/fft.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,5 @@ const mp_obj_module_t ulab_fft_module = {
.globals = (mp_obj_dict_t*)&mp_module_ulab_fft_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy_dot_fft, ulab_fft_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy_dot_fft, ulab_fft_module);
#endif
#endif
4 changes: 0 additions & 4 deletions code/numpy/linalg/linalg.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,6 @@ const mp_obj_module_t ulab_linalg_module = {
.globals = (mp_obj_dict_t*)&mp_module_ulab_linalg_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy_dot_linalg, ulab_linalg_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy_dot_linalg, ulab_linalg_module);
#endif
#endif
#endif
4 changes: 0 additions & 4 deletions code/numpy/numpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,5 @@ const mp_obj_module_t ulab_numpy_module = {
};

#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy, ulab_numpy_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_numpy, ulab_numpy_module);
#endif
#endif
4 changes: 0 additions & 4 deletions code/scipy/linalg/linalg.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,6 @@ const mp_obj_module_t ulab_scipy_linalg_module = {
.globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_linalg_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_linalg, ulab_scipy_linalg_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_linalg, ulab_scipy_linalg_module);
#endif
#endif
#endif
4 changes: 0 additions & 4 deletions code/scipy/optimize/optimize.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,5 @@ const mp_obj_module_t ulab_scipy_optimize_module = {
.globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_optimize_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_optimize, ulab_scipy_optimize_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_optimize, ulab_scipy_optimize_module);
#endif
#endif
4 changes: 0 additions & 4 deletions code/scipy/scipy.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ const mp_obj_module_t ulab_scipy_module = {
.globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy, ulab_scipy_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy, ulab_scipy_module);
#endif
#endif
#endif /* ULAB_HAS_SCIPY */
4 changes: 0 additions & 4 deletions code/scipy/signal/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,5 @@ const mp_obj_module_t ulab_scipy_signal_module = {
.globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_signal_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_signal, ulab_scipy_signal_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_signal, ulab_scipy_signal_module);
#endif
#endif
4 changes: 0 additions & 4 deletions code/scipy/special/special.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,5 @@ const mp_obj_module_t ulab_scipy_special_module = {
.globals = (mp_obj_dict_t*)&mp_module_ulab_scipy_special_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_special, ulab_scipy_special_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_scipy_dot_special, ulab_scipy_special_module);
#endif
#endif
28 changes: 0 additions & 28 deletions code/ulab.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,6 @@ STATIC const mp_rom_map_elem_t ulab_ndarray_locals_dict_table[] = {
#if NDARRAY_HAS_SORT
{ MP_ROM_QSTR(MP_QSTR_sort), MP_ROM_PTR(&numerical_sort_inplace_obj) },
#endif
#ifdef CIRCUITPY
#if NDARRAY_HAS_DTYPE
{ MP_ROM_QSTR(MP_QSTR_dtype), MP_ROM_PTR(&ndarray_dtype_obj) },
#endif
#if NDARRAY_HAS_FLATITER
{ MP_ROM_QSTR(MP_QSTR_flat), MP_ROM_PTR(&ndarray_flat_obj) },
#endif
#if NDARRAY_HAS_ITEMSIZE
{ MP_ROM_QSTR(MP_QSTR_itemsize), MP_ROM_PTR(&ndarray_itemsize_obj) },
#endif
#if NDARRAY_HAS_SHAPE
{ MP_ROM_QSTR(MP_QSTR_shape), MP_ROM_PTR(&ndarray_shape_obj) },
#endif
#if NDARRAY_HAS_SIZE
{ MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&ndarray_size_obj) },
#endif
#if NDARRAY_HAS_STRIDES
{ MP_ROM_QSTR(MP_QSTR_strides), MP_ROM_PTR(&ndarray_strides_obj) },
#endif
#endif /* CIRCUITPY */
};

STATIC MP_DEFINE_CONST_DICT(ulab_ndarray_locals_dict, ulab_ndarray_locals_dict_table);
Expand Down Expand Up @@ -167,9 +147,7 @@ const mp_obj_type_t ulab_ndarray_type = {
#if NDARRAY_HAS_BINARY_OPS
.binary_op = ndarray_binary_op,
#endif
#ifndef CIRCUITPY
.attr = ndarray_properties_attr,
#endif
.buffer_p = { .get_buffer = ndarray_get_buffer, },
)
};
Expand Down Expand Up @@ -253,10 +231,4 @@ const mp_obj_module_t ulab_user_cmodule = {
.globals = (mp_obj_dict_t*)&mp_module_ulab_globals,
};

// Use old three-argument MP_REGISTER_MODULE for
// MicroPython <= v1.18.0: (1 << 16) | (18 << 8) | 0
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab, ulab_user_cmodule, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab, ulab_user_cmodule);
#endif
4 changes: 0 additions & 4 deletions code/user/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ const mp_obj_module_t ulab_user_module = {
.globals = (mp_obj_dict_t*)&mp_module_ulab_user_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_user, ulab_user_module, ULAB_HAS_USER_MODULE);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_user, ulab_user_module);
#endif
#endif

#endif
4 changes: 0 additions & 4 deletions code/utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,7 @@ const mp_obj_module_t ulab_utils_module = {
.globals = (mp_obj_dict_t*)&mp_module_ulab_utils_globals,
};
#if CIRCUITPY_ULAB
#if !defined(MICROPY_VERSION) || MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_utils, ulab_utils_module, MODULE_ULAB_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_ulab_dot_utils, ulab_utils_module);
#endif
#endif

#endif /* ULAB_HAS_UTILS_MODULE */

0 comments on commit 2df210f

Please sign in to comment.