Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 35184ed

Browse files
author
Michael Foord
committed
Issue 9732: __class__ no longer checked on objects by getattr_static
1 parent e516265 commit 35184ed

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

Lib/inspect.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,13 @@ def _check_class(klass, attr):
10801080
pass
10811081
return _sentinel
10821082

1083+
def _is_type(obj):
1084+
try:
1085+
_static_getmro(obj)
1086+
except TypeError:
1087+
return False
1088+
return True
1089+
10831090

10841091
def getattr_static(obj, attr, default=_sentinel):
10851092
"""Retrieve attributes without triggering dynamic lookup via the
@@ -1093,7 +1100,7 @@ def getattr_static(obj, attr, default=_sentinel):
10931100
documentation for details.
10941101
"""
10951102
instance_result = _sentinel
1096-
if not isinstance(obj, type):
1103+
if not _is_type(obj):
10971104
instance_result = _check_instance(obj, attr)
10981105
klass = type(obj)
10991106
else:

Lib/test/test_inspect.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,11 +860,15 @@ class Base(object):
860860
foo = 3
861861

862862
class Something(Base):
863+
executed = False
863864
@property
864865
def __class__(self):
866+
self.executed = True
865867
return object
866868

867-
self.assertEqual(inspect.getattr_static(Something(), 'foo'), 3)
869+
instance = Something()
870+
self.assertEqual(inspect.getattr_static(instance, 'foo'), 3)
871+
self.assertFalse(instance.executed)
868872
self.assertEqual(inspect.getattr_static(Something, 'foo'), 3)
869873

870874
def test_mro_as_property(self):

0 commit comments

Comments
 (0)