Ticket #19543: implemented_repr.diff

File implemented_repr.diff, 2.0 KB (added by fhahn, 2 years ago)

implemented repr and updated test

  • django/utils/functional.py

    diff --git a/django/utils/functional.py b/django/utils/functional.py
    index 1b5200c..773214d 100644
    a b class SimpleLazyObject(LazyObject): 
    302302    def __reduce__(self):
    303303        return (self.__newobj__, (self.__class__,), self.__getstate__())
    304304
     305    def __repr__(self):
     306        if self._wrapped is empty:
     307            repr_attr = self._setupfunc
     308        else:
     309            repr_attr = self._wrapped
     310        return '<SimpleLazyObject: %r>' % repr_attr
     311
    305312    # Need to pretend to be the wrapped class, for the sake of objects that care
    306313    # about this (especially in equality tests)
    307314    __class__ = property(new_method_proxy(operator.attrgetter("__class__")))
  • tests/regressiontests/utils/simplelazyobject.py

    diff --git a/tests/regressiontests/utils/simplelazyobject.py b/tests/regressiontests/utils/simplelazyobject.py
    index 3f81e8f..e0d5d70 100644
    a b class TestUtilsSimpleLazyObject(TestCase): 
    5959                         hash(SimpleLazyObject(complex_object)))
    6060
    6161    def test_repr(self):
    62         # For debugging, it will really confuse things if there is no clue that
    63         # SimpleLazyObject is actually a proxy object. So we don't
    64         # proxy __repr__
    65         self.assertTrue("SimpleLazyObject" in repr(SimpleLazyObject(complex_object)))
     62        # First, for an unevaluated SimpleLazyObject
     63        x = SimpleLazyObject(complex_object)
     64        # __repr__ contains __repr__ of setup function and does not evaluate
     65        # the SimpleLazyObject
     66        self.assertEqual("<SimpleLazyObject: %r>" % complex_object, repr(x))
     67        self.assertEqual(empty, x._wrapped)
     68
     69        # Second, for an evaluated SimpleLazyObject
     70        name = x.name # evaluate
     71        # __repr__ contains __repr__ of wrapped object
     72        self.assertEqual("<SimpleLazyObject: %r>" % complex_object(), repr(x))
    6673
    6774    def test_bytes(self):
    6875        self.assertEqual(b"I am _ComplexObject('joe')",
Back to Top