slice: restore ability to construct a slice

This commit is contained in:
Jeff Epler 2021-05-05 08:47:15 -05:00
parent 92d63b40cc
commit 2c0664ea3f
2 changed files with 40 additions and 2 deletions

View File

@ -663,6 +663,10 @@ msgstr ""
msgid "Cannot specify RTS or CTS in RS485 mode" msgid "Cannot specify RTS or CTS in RS485 mode"
msgstr "" msgstr ""
#: py/objslice.c
msgid "Cannot subclass slice"
msgstr ""
#: shared-module/bitbangio/SPI.c #: shared-module/bitbangio/SPI.c
msgid "Cannot transfer without MOSI and MISO pins." msgid "Cannot transfer without MOSI and MISO pins."
msgstr "" msgstr ""
@ -3886,10 +3890,14 @@ msgstr ""
msgid "sleep length must be non-negative" msgid "sleep length must be non-negative"
msgstr "" msgstr ""
#: extmod/ulab/code/ndarray.c py/objslice.c #: extmod/ulab/code/ndarray.c
msgid "slice step can't be zero" msgid "slice step can't be zero"
msgstr "" msgstr ""
#: py/objslice.c
msgid "slice step cannot be zero"
msgstr ""
#: py/nativeglue.c #: py/nativeglue.c
msgid "slice unsupported" msgid "slice unsupported"
msgstr "" msgstr ""

View File

@ -88,6 +88,33 @@ STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
} }
#endif #endif
#if MICROPY_PY_BUILTINS_SLICE_ATTRS
STATIC mp_obj_t slice_make_new(const mp_obj_type_t *type,
size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
if (type != &mp_type_slice) {
mp_raise_NotImplementedError(translate("Cannot subclass slice"));
}
// check number of arguments
mp_arg_check_num(n_args, kw_args, 1, 3, false);
// 1st argument is the pin
mp_obj_t start = mp_const_none;
mp_obj_t stop = mp_const_none;
mp_obj_t step = mp_const_none;
if (n_args == 1) {
stop = args[0];
} else {
start = args[0];
stop = args[1];
if (n_args == 3) {
step = args[2];
}
}
return mp_obj_new_slice(start, stop, step);
}
#endif
#if MICROPY_PY_BUILTINS_SLICE_INDICES && !MICROPY_PY_BUILTINS_SLICE_ATTRS #if MICROPY_PY_BUILTINS_SLICE_INDICES && !MICROPY_PY_BUILTINS_SLICE_ATTRS
STATIC const mp_rom_map_elem_t slice_locals_dict_table[] = { STATIC const mp_rom_map_elem_t slice_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_indices), MP_ROM_PTR(&slice_indices_obj) }, { MP_ROM_QSTR(MP_QSTR_indices), MP_ROM_PTR(&slice_indices_obj) },
@ -99,6 +126,9 @@ const mp_obj_type_t mp_type_slice = {
{ &mp_type_type }, { &mp_type_type },
.name = MP_QSTR_slice, .name = MP_QSTR_slice,
.print = slice_print, .print = slice_print,
#if MICROPY_PY_BUILTINS_SLICE_INDICES
.make_new = slice_make_new,
#endif
#if MICROPY_PY_BUILTINS_SLICE_ATTRS #if MICROPY_PY_BUILTINS_SLICE_ATTRS
.attr = slice_attr, .attr = slice_attr,
#elif MICROPY_PY_BUILTINS_SLICE_INDICES #elif MICROPY_PY_BUILTINS_SLICE_INDICES
@ -127,7 +157,7 @@ void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *r
} else { } else {
step = mp_obj_get_int(self->step); step = mp_obj_get_int(self->step);
if (step == 0) { if (step == 0) {
mp_raise_ValueError(MP_ERROR_TEXT("slice step can't be zero")); mp_raise_ValueError(MP_ERROR_TEXT("slice step cannot be zero"));
} }
} }