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.
This commit is contained in:
Damien George 2018-12-06 18:02:41 +11:00
parent da7355e213
commit 113f00a9ab
2 changed files with 10 additions and 4 deletions

View File

@ -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

View File

@ -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