Commit 962a5d50 authored by Damien George's avatar Damien George
Browse files

py: Implement __reversed__ slot.

Addresses issue #1073.
parent d7f19946
......@@ -39,6 +39,13 @@ typedef struct _mp_obj_reversed_t {
STATIC mp_obj_t reversed_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, 1, false);
// check if __reversed__ exists, and if so delegate to it
mp_obj_t dest[2];
mp_load_method_maybe(args[0], MP_QSTR___reversed__, dest);
if (dest[0] != MP_OBJ_NULL) {
return mp_call_method_n_kw(0, 0, dest);
}
mp_obj_reversed_t *o = m_new_obj(mp_obj_reversed_t);
o->base.type = type_in;
o->seq = args[0];
......
......@@ -74,6 +74,7 @@ Q(__gt__)
Q(__eq__)
Q(__le__)
Q(__ge__)
Q(__reversed__)
Q(micropython)
Q(bytecode)
......
......@@ -31,3 +31,9 @@ class A:
return pos + 1
for a in reversed(A()):
print(a)
# user object with __reversed__
class B:
def __reversed__(self):
return [1, 2, 3]
print(reversed(B()))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment