Commit aeb62f9a authored by Tom Soulanille's avatar Tom Soulanille Committed by Damien George

py/objslice: Make slice attributes (start/stop/step) readable.

Configurable with MICROPY_PY_BUILTINS_SLICE_ATTRS.  Disabled by default.
parent d80174d7
...@@ -463,6 +463,12 @@ typedef double mp_float_t; ...@@ -463,6 +463,12 @@ typedef double mp_float_t;
#define MICROPY_PY_BUILTINS_SLICE (1) #define MICROPY_PY_BUILTINS_SLICE (1)
#endif #endif
// Whether to support slice attribute read access,
// i.e. slice.start, slice.stop, slice.step
#ifndef MICROPY_PY_BUILTINS_SLICE_ATTRS
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (0)
#endif
// Whether to support frozenset object // Whether to support frozenset object
#ifndef MICROPY_PY_BUILTINS_FROZENSET #ifndef MICROPY_PY_BUILTINS_FROZENSET
#define MICROPY_PY_BUILTINS_FROZENSET (0) #define MICROPY_PY_BUILTINS_FROZENSET (0)
......
...@@ -57,10 +57,30 @@ STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t ...@@ -57,10 +57,30 @@ STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
mp_print_str(print, ")"); mp_print_str(print, ")");
} }
#if MICROPY_PY_BUILTINS_SLICE_ATTRS
STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
if (dest[0] != MP_OBJ_NULL) {
// not load attribute
return;
}
mp_obj_slice_t *self = self_in;
if (attr == MP_QSTR_start) {
dest[0] = self->start;
} else if (attr == MP_QSTR_stop) {
dest[0] = self->stop;
} else if (attr == MP_QSTR_step) {
dest[0] = self->step;
}
}
#endif
const mp_obj_type_t mp_type_slice = { 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_ATTRS
.attr = slice_attr,
#endif
}; };
mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) { mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
......
...@@ -64,6 +64,7 @@ ...@@ -64,6 +64,7 @@
#define MICROPY_PY_BUILTINS_EXECFILE (1) #define MICROPY_PY_BUILTINS_EXECFILE (1)
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
#define MICROPY_PY_SYS_EXIT (1) #define MICROPY_PY_SYS_EXIT (1)
#define MICROPY_PY_SYS_MAXSIZE (1) #define MICROPY_PY_SYS_MAXSIZE (1)
#define MICROPY_PY_SYS_STDFILES (1) #define MICROPY_PY_SYS_STDFILES (1)
......
# test builtin slice attributes access
# print slice attributes
class A:
def __getitem__(self, idx):
print(idx.start, idx.stop, idx.step)
try:
t = A()[1:2]
except:
import sys
print("SKIP")
sys.exit()
A()[1:2:3]
...@@ -73,6 +73,7 @@ ...@@ -73,6 +73,7 @@
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
#define MICROPY_PY_ALL_SPECIAL_METHODS (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (1)
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
#define MICROPY_PY_SYS_EXIT (1) #define MICROPY_PY_SYS_EXIT (1)
#if defined(__APPLE__) && defined(__MACH__) #if defined(__APPLE__) && defined(__MACH__)
#define MICROPY_PY_SYS_PLATFORM "darwin" #define MICROPY_PY_SYS_PLATFORM "darwin"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment