support __getattr__

This commit is contained in:
blueloveTH 2024-08-16 16:52:04 +08:00
parent daf974657c
commit 317a37a851
3 changed files with 33 additions and 0 deletions

View File

@ -59,6 +59,7 @@ MAGIC_METHOD(__package__)
MAGIC_METHOD(__path__)
MAGIC_METHOD(__class__)
MAGIC_METHOD(__abs__)
MAGIC_METHOD(__getattr__)
MAGIC_METHOD(__missing__)
#endif

View File

@ -151,6 +151,14 @@ bool py_getattr(py_Ref self, py_Name name) {
}
}
py_Ref fallback = py_tpfindmagic(type, __getattr__);
if(fallback){
py_push(fallback);
py_push(self);
py_newstr(py_pushtmp(), py_name2str(name));
return py_vectorcall(1, 0);
}
if(self->type == tp_module) {
py_Ref path = py_getdict(self, __path__);
c11_sbuf buf;

View File

@ -26,3 +26,27 @@ except AttributeError:
pass
assert getattr(a, 'xxx', 1) == 1
class A:
def __init__(self, x):
self.x = x
def __getattr__(self, name):
if not name:
raise AttributeError
return name, None
a = A(1)
assert a.x == 1
assert a.y == ('y', None)
assert a.zzz == ('zzz', None)
assert getattr(a, 'x') == 1
assert getattr(a, 'zzz') == ('zzz', None)
assert hasattr(a, 'x')
assert hasattr(a, 'y')
assert hasattr(a, 'zzz')
assert not hasattr(a, '')