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).
This commit is contained in:
Claude Paroz 2017-01-30 19:15:59 +01:00
parent 274ca99982
commit ccfd1295f9
2 changed files with 10 additions and 0 deletions

View File

@ -54,6 +54,9 @@ class SafeText(str, SafeData):
return SafeText(t)
return t
def __str__(self):
return self
SafeString = SafeText

View File

@ -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('<a&b>')
s = mark_safe(e)