From f5817c24f44aa90188757ddca1e5e63ba6f5df75 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 3 May 2019 13:12:54 +0300 Subject: [PATCH] Refs #34445 -- Fixed string-casting of non-string lazy objects when value may be bytes. If the result type is bytes, then calling bytes() on it does nothing. If the result type is not bytes, we should not cast to bytes, just because the return value may be bytes. --- django/utils/functional.py | 5 +---- tests/utils_tests/test_functional.py | 4 ++++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/django/utils/functional.py b/django/utils/functional.py index 666651bd2e..2771851eae 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -144,10 +144,7 @@ def lazy(func, *resultclasses): return bytes(func(*self.__args, **self.__kw)) def __cast(self): - if self._delegate_bytes: - return self.__bytes_cast() - else: - return func(*self.__args, **self.__kw) + return func(*self.__args, **self.__kw) def __str__(self): # object defines __str__(), so __prepare_class__() won't overload diff --git a/tests/utils_tests/test_functional.py b/tests/utils_tests/test_functional.py index f8763b3db6..cc4409d893 100644 --- a/tests/utils_tests/test_functional.py +++ b/tests/utils_tests/test_functional.py @@ -242,6 +242,10 @@ class FunctionalTests(SimpleTestCase): lazy_value = lazy(lambda: [1], str, list)() self.assertEqual(str(lazy_value), "[1]") + def test_lazy_str_cast_mixed_bytes_result_types(self): + lazy_value = lazy(lambda: [1], bytes, list)() + self.assertEqual(str(lazy_value), "[1]") + def test_classproperty_getter(self): class Foo: foo_attr = 123