py/objtype: Remove comment about catching exc from user __getattr__.
Any exception raised in a user __getattr__ should be propagated out. A test is added to verify these semantics.
This commit is contained in:
parent
4904663748
commit
7eb29c2000
|
@ -654,7 +654,6 @@ STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des
|
||||||
mp_load_method_maybe(self_in, MP_QSTR___getattr__, dest2);
|
mp_load_method_maybe(self_in, MP_QSTR___getattr__, dest2);
|
||||||
if (dest2[0] != MP_OBJ_NULL) {
|
if (dest2[0] != MP_OBJ_NULL) {
|
||||||
// __getattr__ exists, call it and return its result
|
// __getattr__ exists, call it and return its result
|
||||||
// XXX if this fails to load the requested attr, should we catch the attribute error and return silently?
|
|
||||||
dest2[2] = MP_OBJ_NEW_QSTR(attr);
|
dest2[2] = MP_OBJ_NEW_QSTR(attr);
|
||||||
dest[0] = mp_call_method_n_kw(1, 0, dest2);
|
dest[0] = mp_call_method_n_kw(1, 0, dest2);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -9,3 +9,20 @@ class A:
|
||||||
|
|
||||||
a = A({'a':1, 'b':2})
|
a = A({'a':1, 'b':2})
|
||||||
print(a.a, a.b)
|
print(a.a, a.b)
|
||||||
|
|
||||||
|
# test that any exception raised in __getattr__ propagates out
|
||||||
|
class A:
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
if attr == "value":
|
||||||
|
raise ValueError(123)
|
||||||
|
else:
|
||||||
|
raise AttributeError(456)
|
||||||
|
a = A()
|
||||||
|
try:
|
||||||
|
a.value
|
||||||
|
except ValueError as er:
|
||||||
|
print(er)
|
||||||
|
try:
|
||||||
|
a.attr
|
||||||
|
except AttributeError as er:
|
||||||
|
print(er)
|
||||||
|
|
Loading…
Reference in New Issue