mirror of
https://github.com/python/cpython.git
synced 2025-10-14 18:59:46 +00:00
Issue 9732: __class__ no longer checked on objects by getattr_static
This commit is contained in:
parent
e516265bbc
commit
35184edd3d
2 changed files with 13 additions and 2 deletions
|
@ -1080,6 +1080,13 @@ def _check_class(klass, attr):
|
||||||
pass
|
pass
|
||||||
return _sentinel
|
return _sentinel
|
||||||
|
|
||||||
|
def _is_type(obj):
|
||||||
|
try:
|
||||||
|
_static_getmro(obj)
|
||||||
|
except TypeError:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def getattr_static(obj, attr, default=_sentinel):
|
def getattr_static(obj, attr, default=_sentinel):
|
||||||
"""Retrieve attributes without triggering dynamic lookup via the
|
"""Retrieve attributes without triggering dynamic lookup via the
|
||||||
|
@ -1093,7 +1100,7 @@ def getattr_static(obj, attr, default=_sentinel):
|
||||||
documentation for details.
|
documentation for details.
|
||||||
"""
|
"""
|
||||||
instance_result = _sentinel
|
instance_result = _sentinel
|
||||||
if not isinstance(obj, type):
|
if not _is_type(obj):
|
||||||
instance_result = _check_instance(obj, attr)
|
instance_result = _check_instance(obj, attr)
|
||||||
klass = type(obj)
|
klass = type(obj)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -860,11 +860,15 @@ class TestGetattrStatic(unittest.TestCase):
|
||||||
foo = 3
|
foo = 3
|
||||||
|
|
||||||
class Something(Base):
|
class Something(Base):
|
||||||
|
executed = False
|
||||||
@property
|
@property
|
||||||
def __class__(self):
|
def __class__(self):
|
||||||
|
self.executed = True
|
||||||
return object
|
return object
|
||||||
|
|
||||||
self.assertEqual(inspect.getattr_static(Something(), 'foo'), 3)
|
instance = Something()
|
||||||
|
self.assertEqual(inspect.getattr_static(instance, 'foo'), 3)
|
||||||
|
self.assertFalse(instance.executed)
|
||||||
self.assertEqual(inspect.getattr_static(Something, 'foo'), 3)
|
self.assertEqual(inspect.getattr_static(Something, 'foo'), 3)
|
||||||
|
|
||||||
def test_mro_as_property(self):
|
def test_mro_as_property(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue