From ccfd1295f986cdf628d774937d0b38a14584721f Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 30 Jan 2017 19:15:59 +0100 Subject: [PATCH] Refs #27795 -- Prevented SafeText from losing safe status on str() This will allow to replace force_text() by str() in several places (as one of the features of force_text is to keep the safe status). --- django/utils/safestring.py | 3 +++ tests/utils_tests/test_safestring.py | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/django/utils/safestring.py b/django/utils/safestring.py index 5f5e7098f2..5baaca2e08 100644 --- a/django/utils/safestring.py +++ b/django/utils/safestring.py @@ -54,6 +54,9 @@ class SafeText(str, SafeData): return SafeText(t) return t + def __str__(self): + return self + SafeString = SafeText diff --git a/tests/utils_tests/test_safestring.py b/tests/utils_tests/test_safestring.py index be20cea11e..bb4dfd1280 100644 --- a/tests/utils_tests/test_safestring.py +++ b/tests/utils_tests/test_safestring.py @@ -24,6 +24,13 @@ class SafeStringTest(SimpleTestCase): self.assertRenderEqual('{{ s }}', 'a&b', s=s) self.assertRenderEqual('{{ s|force_escape }}', 'a&b', s=s) + def test_mark_safe_str(self): + """ + Calling str() on a SafeText instance doesn't lose the safe status. + """ + s = mark_safe('a&b') + self.assertIsInstance(str(s), type(s)) + def test_mark_safe_object_implementing_dunder_html(self): e = customescape('') s = mark_safe(e)