Skip to content

Subtle error in getattr_hook example in the Descriptor Guide #126090

@LunaRood

Description

@LunaRood

Documentation

The example below, from the Descriptor Guide, does not seem to reflect the behaviour of the actual implementation.

def getattr_hook(obj, name):
"Emulate slot_tp_getattr_hook() in Objects/typeobject.c"
try:
return obj.__getattribute__(name)
except AttributeError:
if not hasattr(type(obj), '__getattr__'):
raise
return type(obj).__getattr__(obj, name) # __getattr__

Unless I'm mistaken, line 736 return obj.__getattribute__(name) should actually read return type(obj).__getattribute__(obj, name).

It's easy to miss when thinking of class instances, but the issue becomes more evident when the example is applied to an attribute lookup on a class object instead. The example would call the __getattribute__ method of the class itself, while the actual implementation calls __getattribute__ on the metaclass (not to mention that in this case the call in the example would fail, due to a missing argument, as it is calling a plain function, rather than a bound method).

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions