py: Implement builtin reversed() function.
reversed function now implemented, and works for tuple, list, str, bytes and user objects with __len__ and __getitem__. Renamed mp_builtin_len to mp_obj_len to make it publically available (eg for reversed).
This commit is contained in:
parent
69c5fe1df6
commit
4c03b3a899
14
py/builtin.c
14
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);
|
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) {
|
STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
|
||||||
if (n_args == 1) {
|
if (n_args == 1) {
|
||||||
// given an iterable
|
// 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);
|
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_globals_obj, mp_globals_get);
|
||||||
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args);
|
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_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___build_class___obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin___import___obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin___import___obj);
|
||||||
|
@ -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 },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_property), (mp_obj_t)&mp_type_property },
|
||||||
#endif
|
#endif
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_range), (mp_obj_t)&mp_type_range },
|
{ 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
|
#if MICROPY_PY_BUILTINS_SET
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&mp_type_set },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&mp_type_set },
|
||||||
#endif
|
#endif
|
||||||
|
10
py/obj.c
10
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;
|
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
|
// may return MP_OBJ_NULL
|
||||||
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
|
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
|
||||||
if (
|
if (
|
||||||
|
2
py/obj.h
2
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_property;
|
||||||
extern const mp_obj_type_t mp_type_stringio;
|
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_bytesio;
|
||||||
|
extern const mp_obj_type_t mp_type_reversed;
|
||||||
|
|
||||||
// Exceptions
|
// Exceptions
|
||||||
extern const mp_obj_type_t mp_type_BaseException;
|
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(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);
|
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);
|
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_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);
|
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
|
||||||
|
|
||||||
|
74
py/objreversed.c
Normal file
74
py/objreversed.c
Normal file
@ -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,
|
||||||
|
};
|
1
py/py.mk
1
py/py.mk
@ -72,6 +72,7 @@ PY_O_BASENAME = \
|
|||||||
objnone.o \
|
objnone.o \
|
||||||
objnamedtuple.o \
|
objnamedtuple.o \
|
||||||
objrange.o \
|
objrange.o \
|
||||||
|
objreversed.o \
|
||||||
objset.o \
|
objset.o \
|
||||||
objslice.o \
|
objslice.o \
|
||||||
objstr.o \
|
objstr.o \
|
||||||
|
@ -175,6 +175,7 @@ Q(print)
|
|||||||
Q(range)
|
Q(range)
|
||||||
Q(read)
|
Q(read)
|
||||||
Q(repr)
|
Q(repr)
|
||||||
|
Q(reversed)
|
||||||
Q(set)
|
Q(set)
|
||||||
Q(sorted)
|
Q(sorted)
|
||||||
Q(staticmethod)
|
Q(staticmethod)
|
||||||
|
33
tests/basics/builtin_reversed.py
Normal file
33
tests/basics/builtin_reversed.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
# test the builtin reverse() function
|
||||||
|
|
||||||
|
# list
|
||||||
|
print(list(reversed([])))
|
||||||
|
print(list(reversed([1])))
|
||||||
|
print(list(reversed([1, 2, 3])))
|
||||||
|
|
||||||
|
# tuple
|
||||||
|
print(list(reversed(())))
|
||||||
|
print(list(reversed((1, 2, 3))))
|
||||||
|
|
||||||
|
# string
|
||||||
|
for c in reversed('ab'):
|
||||||
|
print(c)
|
||||||
|
|
||||||
|
# bytes
|
||||||
|
for b in reversed(b'1234'):
|
||||||
|
print(b)
|
||||||
|
|
||||||
|
# range
|
||||||
|
#for i in reversed(range(3)):
|
||||||
|
# print(i)
|
||||||
|
|
||||||
|
# user object
|
||||||
|
class A:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
def __len__(self):
|
||||||
|
return 3
|
||||||
|
def __getitem__(self, pos):
|
||||||
|
return pos + 1
|
||||||
|
for a in reversed(A()):
|
||||||
|
print(a)
|
Loading…
Reference in New Issue
Block a user