diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 2db71c8c47..0955d389c4 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -663,6 +663,10 @@ msgstr "" msgid "Cannot specify RTS or CTS in RS485 mode" msgstr "" +#: py/objslice.c +msgid "Cannot subclass slice" +msgstr "" + #: shared-module/bitbangio/SPI.c msgid "Cannot transfer without MOSI and MISO pins." msgstr "" @@ -3886,10 +3890,14 @@ msgstr "" msgid "sleep length must be non-negative" msgstr "" -#: extmod/ulab/code/ndarray.c py/objslice.c +#: extmod/ulab/code/ndarray.c msgid "slice step can't be zero" msgstr "" +#: py/objslice.c +msgid "slice step cannot be zero" +msgstr "" + #: py/nativeglue.c msgid "slice unsupported" msgstr "" diff --git a/py/objslice.c b/py/objslice.c index 2201e01751..230125a8c3 100644 --- a/py/objslice.c +++ b/py/objslice.c @@ -88,6 +88,33 @@ STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } #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 STATIC const mp_rom_map_elem_t slice_locals_dict_table[] = { { 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 }, .name = MP_QSTR_slice, .print = slice_print, + #if MICROPY_PY_BUILTINS_SLICE_INDICES + .make_new = slice_make_new, + #endif #if MICROPY_PY_BUILTINS_SLICE_ATTRS .attr = slice_attr, #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 { step = mp_obj_get_int(self->step); 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")); } }