You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inaccurate emulation of _PyType_Lookup() in the Descriptor HOWTO #95822
Thanks for the bug report. Here's a proposed update that replaces getattr(objtype, name) with find_name_in_mro(objtype, name, null):
def find_name_in_mro(cls, name, default):
"Emulate _PyType_Lookup() in Objects/typeobject.c"
for base in cls.__mro__:
if name in vars(base):
return vars(base)[name]
return default
def object_getattribute(obj, name):
"Emulate PyObject_GenericGetAttr() in Objects/object.c"
null = object()
objtype = type(obj)
cls_var = find_name_in_mro(objtype, name, null)
descr_get = getattr(type(cls_var), '__get__', null)
if descr_get is not null:
if (hasattr(type(cls_var), '__set__')
or hasattr(type(cls_var), '__delete__')):
return descr_get(cls_var, obj, objtype) # data descriptor
if hasattr(obj, '__dict__') and name in vars(obj):
return vars(obj)[name] # instance variable
if descr_get is not null:
return descr_get(cls_var, obj, objtype) # non-data descriptor
if cls_var is not null:
return cls_var # class variable
raise AttributeError(name)
Also, I want to add tests to cover your example and to cover why the fix proposed in the PR was incorrect.
Activity
rhettinger commentedon Aug 10, 2022
Thanks for the bug report. Here's a proposed update that replaces
getattr(objtype, name)withfind_name_in_mro(objtype, name, null):Also, I want to add tests to cover your example and to cover why the fix proposed in the PR was incorrect.
[-]Doc: descriptior HOWTO bad example[/-][+]Inaccurate emulation of _PyType_Lookup() in the Descriptor HOWTO[/+]pythonGH-95822: Need _PyType_Lookup() in descriptor howto code equiva…
pythonGH-95822: Need _PyType_Lookup() in descriptor howto code equiva…
GH-95822: Need _PyType_Lookup() in descriptor howto code equivalent. (G…
pythonGH-95822: Need _PyType_Lookup() in descriptor howto code equiva…
GH-95822: Need _PyType_Lookup() in descriptor howto code equivalent. (G…
GH-95822: Need _PyType_Lookup() in descriptor howto code equivalent. (G…
rhettinger commentedon Aug 19, 2022
Thanks for the report. This is fixed now.