MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_getattr_hooks

Method test_getattr_hooks

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

Source from the content-addressed store, hash-verified

4786 self.assertEqual(X.a, 42)
4787
4788 def test_getattr_hooks(self):
4789 # issue 4230
4790
4791 class Descriptor(object):
4792 counter = 0
4793 def __get__(self, obj, objtype=None):
4794 def getter(name):
4795 self.counter += 1
4796 raise AttributeError(name)
4797 return getter
4798
4799 descr = Descriptor()
4800 class A(object):
4801 __getattribute__ = descr
4802 class B(object):
4803 __getattr__ = descr
4804 class C(object):
4805 __getattribute__ = descr
4806 __getattr__ = descr
4807
4808 self.assertRaises(AttributeError, getattr, A(), "attr")
4809 self.assertEqual(descr.counter, 1)
4810 self.assertRaises(AttributeError, getattr, B(), "attr")
4811 self.assertEqual(descr.counter, 2)
4812 self.assertRaises(AttributeError, getattr, C(), "attr")
4813 self.assertEqual(descr.counter, 4)
4814
4815 class EvilGetattribute(object):
4816 # This used to segfault
4817 def __getattr__(self, name):
4818 raise AttributeError(name)
4819 def __getattribute__(self, name):
4820 del EvilGetattribute.__getattr__
4821 for i in range(5):
4822 gc.collect()
4823 raise AttributeError(name)
4824
4825 self.assertRaises(AttributeError, getattr, EvilGetattribute(), "attr")
4826
4827 def test_type___getattribute__(self):
4828 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