Refs #28358 -- Fixed infinite recursion in LazyObject.__getattribute__().

Regression in 97d7990abd.

Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Co-authored-by: Theo Alexiou <theofilosalexiou@gmail.com>
This commit is contained in:
Matthias Kestenholz 2022-02-17 09:45:34 +01:00 committed by Mariusz Felisiak
parent a94ae4cb11
commit b2ed0d78f2
2 changed files with 11 additions and 0 deletions

View File

@ -288,6 +288,9 @@ class LazyObject:
self._wrapped = empty
def __getattribute__(self, name):
if name == "_wrapped":
# Avoid recursion when getting wrapped object.
return super().__getattribute__(name)
value = super().__getattribute__(name)
# If attribute is a proxy method, raise an AttributeError to call
# __getattr__() and use the wrapped object method.

View File

@ -58,6 +58,14 @@ class LazyObjectTestCase(TestCase):
obj = self.lazy_wrap(Foo())
self.assertEqual(obj.foo, "bar")
def test_getattr_falsey(self):
class Thing:
def __getattr__(self, key):
return []
obj = self.lazy_wrap(Thing())
self.assertEqual(obj.main, [])
def test_setattr(self):
obj = self.lazy_wrap(Foo())
obj.foo = "BAR"