summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorTom Soulanille <soul@prama.com>2015-09-11 14:31:32 -0700
committerDamien George <damien.p.george@gmail.com>2015-09-15 21:59:20 +0100
commitaeb62f9ae3169d789800591eea390e020c7532bf (patch)
tree6b4524e14756f3cdd2c41ad98b72ab88e40f72ec /py
parentd80174d7c3fe36f139c639d6a820cc4cbb88ddf9 (diff)
py/objslice: Make slice attributes (start/stop/step) readable.
Configurable with MICROPY_PY_BUILTINS_SLICE_ATTRS. Disabled by default.
Diffstat (limited to 'py')
-rw-r--r--py/mpconfig.h6
-rw-r--r--py/objslice.c20
2 files changed, 26 insertions, 0 deletions
diff --git a/py/mpconfig.h b/py/mpconfig.h
index eb5b7243f..57fc227ab 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 5b99c992b..1ef16ec23 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) {