Przeglądaj źródła

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

Regression in 97d7990abde3fe4b525ae83958fd0b52d6a1d13f.

Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Co-authored-by: Theo Alexiou <theofilosalexiou@gmail.com>
Matthias Kestenholz 3 lat temu
rodzic
commit
b2ed0d78f2

+ 3 - 0
django/utils/functional.py

@@ -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.

+ 8 - 0
tests/utils_tests/test_lazyobject.py

@@ -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"