From 4c03b3a899d49f0f4f2c54903403aaa9b384c315 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 12 Aug 2014 18:33:40 +0100 Subject: 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). --- py/obj.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'py/obj.c') diff --git a/py/obj.c b/py/obj.c index 04716454d..d8fccfb7b 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 ( -- cgit v1.2.3