[issue11133] inspect.getattr_static code execution

Andreas Stührk report at bugs.python.org
Tue Feb 22 02:40:41 CET 2011


Andreas Stührk <andy-python at hammerhartes.de> added the comment:

> The patch is not sufficient - instances may have a class member "__dict__" whilst still having an instance __dict__.

Sure, but I don't think there is a way how you can access the instance
__dict__ in that case inside Python code. At least I can't think of
one.

>Alternatively the "__dict__" property may be provided by a base class and so not available in "type(obj).__dict__" but still be provided by a property.
>
> I don't think there is any general way to tell whether fetching obj.__dict__ will get an instance dictionary or fetch a "__dict__" member from the class or a base-class... (Hence the documented exception.)

Why not? ``obj.__dict__`` will fetch the instance dictionary iff there
is no class attribute "__dict__" in any of the base classes. In the
patch,``type.__dict__["__dict__"].__get__()`` is used to get (without
any doubt) the class dictionary. By looking inside that dictionary, we
can now tell whether "__dict__" is overwritten: If it isn't
overwritten, the dictionary either doesn't have a "__dict__" entry at
all or the value is a getset_descriptor. So we just need to iterate
over a type's mro, look inside each entries' dictionary and stop when
a "__dict__" entry is found.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11133>
_______________________________________


More information about the Python-bugs-list mailing list