MCPcopy Index your code
hub / github.com/python/cpython / test_getattr_hooks

Method test_getattr_hooks

Lib/test/test_descr.py:5006–5043  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

5004 self.assertEqual(X.a, 42)
5005
5006 def test_getattr_hooks(self):
5007 # issue 4230
5008
5009 class Descriptor(object):
5010 counter = 0
5011 def __get__(self, obj, objtype=None):
5012 def getter(name):
5013 self.counter += 1
5014 raise AttributeError(name)
5015 return getter
5016
5017 descr = Descriptor()
5018 class A(object):
5019 __getattribute__ = descr
5020 class B(object):
5021 __getattr__ = descr
5022 class C(object):
5023 __getattribute__ = descr
5024 __getattr__ = descr
5025
5026 self.assertRaises(AttributeError, getattr, A(), "attr")
5027 self.assertEqual(descr.counter, 1)
5028 self.assertRaises(AttributeError, getattr, B(), "attr")
5029 self.assertEqual(descr.counter, 2)
5030 self.assertRaises(AttributeError, getattr, C(), "attr")
5031 self.assertEqual(descr.counter, 4)
5032
5033 class EvilGetattribute(object):
5034 # This used to segfault
5035 def __getattr__(self, name):
5036 raise AttributeError(name)
5037 def __getattribute__(self, name):
5038 del EvilGetattribute.__getattr__
5039 for i in range(5):
5040 gc.collect()
5041 raise AttributeError(name)
5042
5043 self.assertRaises(AttributeError, getattr, EvilGetattribute(), "attr")
5044
5045 def test_type___getattribute__(self):
5046 self.assertRaises(TypeError, type.__getattribute__, list, type)

Callers

nothing calls this directly

Calls 7

EvilGetattributeClass · 0.85
DescriptorClass · 0.70
AClass · 0.70
BClass · 0.70
CClass · 0.70
assertRaisesMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected