From 113f00a9ab469a83d1004dac502077af0a8f4847 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 6 Dec 2018 18:02:41 +1100 Subject: [PATCH] py/objboundmeth: Support loading generic attrs from the method. Instead of assuming that the method is a bytecode object, and only supporting load of __name__, make the operation generic by delegating the load to the method object itself. Saves a bit of code size and fixes the case of attempting to load __name__ on a native method, see issue #4028. --- py/objboundmeth.c | 7 +++---- tests/basics/fun_name.py | 7 +++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/py/objboundmeth.c b/py/objboundmeth.c index b0df6a68a7..8fc44f1637 100644 --- a/py/objboundmeth.c +++ b/py/objboundmeth.c @@ -89,10 +89,9 @@ STATIC void bound_meth_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { // not load attribute return; } - if (attr == MP_QSTR___name__) { - mp_obj_bound_meth_t *o = MP_OBJ_TO_PTR(self_in); - dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(o->meth)); - } + // Delegate the load to the method object + mp_obj_bound_meth_t *self = MP_OBJ_TO_PTR(self_in); + mp_load_method_maybe(self->meth, attr, dest); } #endif diff --git a/tests/basics/fun_name.py b/tests/basics/fun_name.py index a724f41118..53ca935616 100644 --- a/tests/basics/fun_name.py +++ b/tests/basics/fun_name.py @@ -15,3 +15,10 @@ try: except AttributeError: print('SKIP') raise SystemExit + +# __name__ of a bound native method is not implemented in uPy +# the test here is to make sure it doesn't crash +try: + str((1).to_bytes.__name__) +except AttributeError: + pass