Skip to content

Commit aeb62f9

Browse files
pramasouldpgeorge
authored andcommitted
py/objslice: Make slice attributes (start/stop/step) readable.
Configurable with MICROPY_PY_BUILTINS_SLICE_ATTRS. Disabled by default.
1 parent d80174d commit aeb62f9

5 files changed

Lines changed: 44 additions & 0 deletions

File tree

py/mpconfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,12 @@ typedef double mp_float_t;
463463
#define MICROPY_PY_BUILTINS_SLICE (1)
464464
#endif
465465

466+
// Whether to support slice attribute read access,
467+
// i.e. slice.start, slice.stop, slice.step
468+
#ifndef MICROPY_PY_BUILTINS_SLICE_ATTRS
469+
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (0)
470+
#endif
471+
466472
// Whether to support frozenset object
467473
#ifndef MICROPY_PY_BUILTINS_FROZENSET
468474
#define MICROPY_PY_BUILTINS_FROZENSET (0)

py/objslice.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,30 @@ STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
5757
mp_print_str(print, ")");
5858
}
5959

60+
#if MICROPY_PY_BUILTINS_SLICE_ATTRS
61+
STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
62+
if (dest[0] != MP_OBJ_NULL) {
63+
// not load attribute
64+
return;
65+
}
66+
mp_obj_slice_t *self = self_in;
67+
if (attr == MP_QSTR_start) {
68+
dest[0] = self->start;
69+
} else if (attr == MP_QSTR_stop) {
70+
dest[0] = self->stop;
71+
} else if (attr == MP_QSTR_step) {
72+
dest[0] = self->step;
73+
}
74+
}
75+
#endif
76+
6077
const mp_obj_type_t mp_type_slice = {
6178
{ &mp_type_type },
6279
.name = MP_QSTR_slice,
6380
.print = slice_print,
81+
#if MICROPY_PY_BUILTINS_SLICE_ATTRS
82+
.attr = slice_attr,
83+
#endif
6484
};
6585

6686
mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {

stmhal/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#define MICROPY_PY_BUILTINS_EXECFILE (1)
6565
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
6666
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
67+
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
6768
#define MICROPY_PY_SYS_EXIT (1)
6869
#define MICROPY_PY_SYS_MAXSIZE (1)
6970
#define MICROPY_PY_SYS_STDFILES (1)

tests/basics/slice_attrs.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# test builtin slice attributes access
2+
3+
# print slice attributes
4+
class A:
5+
def __getitem__(self, idx):
6+
print(idx.start, idx.stop, idx.step)
7+
8+
try:
9+
t = A()[1:2]
10+
except:
11+
import sys
12+
print("SKIP")
13+
sys.exit()
14+
15+
16+
A()[1:2:3]

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
7474
#define MICROPY_PY_ALL_SPECIAL_METHODS (1)
7575
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
76+
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
7677
#define MICROPY_PY_SYS_EXIT (1)
7778
#if defined(__APPLE__) && defined(__MACH__)
7879
#define MICROPY_PY_SYS_PLATFORM "darwin"

0 commit comments

Comments
 (0)