summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--py/mpconfig.h6
-rw-r--r--py/objslice.c20
-rw-r--r--stmhal/mpconfigport.h1
-rw-r--r--tests/basics/slice_attrs.py16
-rw-r--r--unix/mpconfigport.h1
5 files changed, 44 insertions, 0 deletions
diff --git a/py/mpconfig.h b/py/mpconfig.h
index eb5b7243fe..57fc227abb 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -463,6 +463,12 @@ typedef double mp_float_t;
#define MICROPY_PY_BUILTINS_SLICE (1)
#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
#ifndef MICROPY_PY_BUILTINS_FROZENSET
#define MICROPY_PY_BUILTINS_FROZENSET (0)
diff --git a/py/objslice.c b/py/objslice.c
index 5b99c992b6..1ef16ec233 100644
--- a/py/objslice.c
+++ b/py/objslice.c
@@ -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, ")");
}
+#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 = {
{ &mp_type_type },
.name = MP_QSTR_slice,
.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) {
diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h
index eec492a15b..100c66f982 100644
--- a/stmhal/mpconfigport.h
+++ b/stmhal/mpconfigport.h
@@ -64,6 +64,7 @@
#define MICROPY_PY_BUILTINS_EXECFILE (1)
#define MICROPY_PY_MICROPYTHON_MEM_INFO (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_MAXSIZE (1)
#define MICROPY_PY_SYS_STDFILES (1)
diff --git a/tests/basics/slice_attrs.py b/tests/basics/slice_attrs.py
new file mode 100644
index 0000000000..76368a78c6
--- /dev/null
+++ b/tests/basics/slice_attrs.py
@@ -0,0 +1,16 @@
+# 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]
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index f51d1aeae0..21df224861 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -73,6 +73,7 @@
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
#define MICROPY_PY_ALL_SPECIAL_METHODS (1)
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
+#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
#define MICROPY_PY_SYS_EXIT (1)
#if defined(__APPLE__) && defined(__MACH__)
#define MICROPY_PY_SYS_PLATFORM "darwin"