summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/builtin.c14
-rw-r--r--py/builtin.h1
-rw-r--r--py/builtintables.c1
-rw-r--r--py/obj.c10
-rw-r--r--py/obj.h2
-rw-r--r--py/objreversed.c74
-rw-r--r--py/py.mk1
-rw-r--r--py/qstrdefs.h1
8 files changed, 92 insertions, 12 deletions
diff --git a/py/builtin.c b/py/builtin.c
index 6a4ffdbd99..1924e6080f 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -284,17 +284,6 @@ STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
-STATIC mp_obj_t mp_builtin_len(mp_obj_t o_in) {
- mp_obj_t len = mp_obj_len_maybe(o_in);
- if (len == MP_OBJ_NULL) {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
- } else {
- return len;
- }
-}
-
-MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len);
-
STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
if (n_args == 1) {
// given an iterable
@@ -569,6 +558,7 @@ STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
-// These two are defined in terms of MicroPython API functions right away
+// These are defined in terms of MicroPython API functions right away
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_obj_len);
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);
diff --git a/py/builtin.h b/py/builtin.h
index 425cfec097..25edf32892 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -26,6 +26,7 @@
mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args);
mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args);
+mp_obj_t mp_builtin_len(mp_obj_t o);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin___import___obj);
diff --git a/py/builtintables.c b/py/builtintables.c
index ff530b93be..0f0cfcbc72 100644
--- a/py/builtintables.c
+++ b/py/builtintables.c
@@ -66,6 +66,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_property), (mp_obj_t)&mp_type_property },
#endif
{ MP_OBJ_NEW_QSTR(MP_QSTR_range), (mp_obj_t)&mp_type_range },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_reversed), (mp_obj_t)&mp_type_reversed },
#if MICROPY_PY_BUILTINS_SET
{ MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&mp_type_set },
#endif
diff --git a/py/obj.c b/py/obj.c
index 04716454d7..d8fccfb7b2 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -360,6 +360,16 @@ uint mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool
return i;
}
+// will raise a TypeError if object has no length
+mp_obj_t mp_obj_len(mp_obj_t o_in) {
+ mp_obj_t len = mp_obj_len_maybe(o_in);
+ if (len == MP_OBJ_NULL) {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
+ } else {
+ return len;
+ }
+}
+
// may return MP_OBJ_NULL
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
if (
diff --git a/py/obj.h b/py/obj.h
index 928402d44a..27d947a8ff 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -312,6 +312,7 @@ extern const mp_obj_type_t mp_type_classmethod;
extern const mp_obj_type_t mp_type_property;
extern const mp_obj_type_t mp_type_stringio;
extern const mp_obj_type_t mp_type_bytesio;
+extern const mp_obj_type_t mp_type_reversed;
// Exceptions
extern const mp_obj_type_t mp_type_BaseException;
@@ -424,6 +425,7 @@ void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items);
void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items);
uint mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice);
+mp_obj_t mp_obj_len(mp_obj_t o_in);
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return MP_OBJ_NULL */
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
diff --git a/py/objreversed.c b/py/objreversed.c
new file mode 100644
index 0000000000..3f758699d3
--- /dev/null
+++ b/py/objreversed.c
@@ -0,0 +1,74 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2014 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <assert.h>
+
+#include "mpconfig.h"
+#include "nlr.h"
+#include "misc.h"
+#include "qstr.h"
+#include "obj.h"
+#include "runtime.h"
+
+typedef struct _mp_obj_reversed_t {
+ mp_obj_base_t base;
+ mp_obj_t seq; // sequence object that we are reversing
+ mp_uint_t cur_index; // current index, plus 1; 0=no more, 1=last one (index 0)
+} mp_obj_reversed_t;
+
+STATIC mp_obj_t reversed_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
+ mp_arg_check_num(n_args, n_kw, 1, 1, false);
+
+ mp_obj_reversed_t *o = m_new_obj(mp_obj_reversed_t);
+ o->base.type = &mp_type_reversed;
+ o->seq = args[0];
+ o->cur_index = mp_obj_get_int(mp_obj_len(args[0])); // start at the end of the sequence
+
+ return o;
+}
+
+STATIC mp_obj_t reversed_iternext(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &mp_type_reversed));
+ mp_obj_reversed_t *self = self_in;
+
+ // "raise" stop iteration if we are at the end (the start) of the sequence
+ if (self->cur_index == 0) {
+ return MP_OBJ_STOP_ITERATION;
+ }
+
+ // pre-decrement and index sequence
+ self->cur_index -= 1;
+ return mp_obj_subscr(self->seq, MP_OBJ_NEW_SMALL_INT(self->cur_index), MP_OBJ_SENTINEL);
+}
+
+const mp_obj_type_t mp_type_reversed = {
+ { &mp_type_type },
+ .name = MP_QSTR_reversed,
+ .make_new = reversed_make_new,
+ .getiter = mp_identity,
+ .iternext = reversed_iternext,
+};
diff --git a/py/py.mk b/py/py.mk
index f1ae3c3f1e..5d62458812 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -72,6 +72,7 @@ PY_O_BASENAME = \
objnone.o \
objnamedtuple.o \
objrange.o \
+ objreversed.o \
objset.o \
objslice.o \
objstr.o \
diff --git a/py/qstrdefs.h b/py/qstrdefs.h
index 6470cb9509..9a6953d01f 100644
--- a/py/qstrdefs.h
+++ b/py/qstrdefs.h
@@ -175,6 +175,7 @@ Q(print)
Q(range)
Q(read)
Q(repr)
+Q(reversed)
Q(set)
Q(sorted)
Q(staticmethod)