From cd0ceaa102c1e12b016432db4d180bee7906131f Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 31 Jan 2015 16:05:16 +0100 Subject: [PATCH] Fixed #24252 -- Forced lazy __str__ to utf-8 on Python 2 Thanks Stanislas Guerra for the report and Tomas Ehrlich for the review. --- django/utils/functional.py | 4 ++++ tests/urlpatterns_reverse/tests.py | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/django/utils/functional.py b/django/utils/functional.py index 9e4df4b2f7..4c085d9c6d 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -95,6 +95,7 @@ def lazy(func, *resultclasses): cls.__str__ = cls.__text_cast else: cls.__unicode__ = cls.__text_cast + cls.__str__ = cls.__bytes_cast_encoded elif cls._delegate_bytes: if six.PY3: cls.__bytes__ = cls.__bytes_cast @@ -117,6 +118,9 @@ def lazy(func, *resultclasses): def __bytes_cast(self): return bytes(func(*self.__args, **self.__kw)) + def __bytes_cast_encoded(self): + return func(*self.__args, **self.__kw).encode('utf-8') + def __cast(self): if self._delegate_bytes: return self.__bytes_cast() diff --git a/tests/urlpatterns_reverse/tests.py b/tests/urlpatterns_reverse/tests.py index 310250152f..50a0881a41 100644 --- a/tests/urlpatterns_reverse/tests.py +++ b/tests/urlpatterns_reverse/tests.py @@ -318,6 +318,17 @@ class ReverseLazyTest(TestCase): response = self.client.get('/login_required_view/') self.assertEqual(response.status_code, 200) + def test_inserting_reverse_lazy_into_string(self): + self.assertEqual( + 'Some URL: %s' % reverse_lazy('some-login-page'), + 'Some URL: /login/' + ) + if six.PY2: + self.assertEqual( + b'Some URL: %s' % reverse_lazy('some-login-page'), + 'Some URL: /login/' + ) + class ReverseLazySettingsTest(AdminScriptTestCase): """