summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/utils/safestring.py2
-rw-r--r--tests/utils_tests/test_safestring.py18
2 files changed, 19 insertions, 1 deletions
diff --git a/django/utils/safestring.py b/django/utils/safestring.py
index ad8054fe0f..50b0c03686 100644
--- a/django/utils/safestring.py
+++ b/django/utils/safestring.py
@@ -144,4 +144,4 @@ def mark_for_escaping(s):
return EscapeBytes(s)
if isinstance(s, (six.text_type, Promise)):
return EscapeText(s)
- return EscapeBytes(bytes(s))
+ return EscapeString(str(s))
diff --git a/tests/utils_tests/test_safestring.py b/tests/utils_tests/test_safestring.py
index c953412430..053d9f42fa 100644
--- a/tests/utils_tests/test_safestring.py
+++ b/tests/utils_tests/test_safestring.py
@@ -33,6 +33,15 @@ class SafeStringTest(TestCase):
self.assertIsInstance(mark_safe(b), SafeData)
self.assertRenderEqual('{{ s }}', 'a&b', s=mark_safe(s))
+ def test_mark_safe_object_implementing_dunder_str(self):
+ class Obj(object):
+ def __str__(self):
+ return '<obj>'
+
+ s = mark_safe(Obj())
+
+ self.assertRenderEqual('{{ s }}', '<obj>', s=s)
+
def test_mark_for_escaping(self):
s = mark_for_escaping('a&b')
self.assertRenderEqual('{{ s }}', 'a&amp;b', s=s)
@@ -50,6 +59,15 @@ class SafeStringTest(TestCase):
s = '<h1>interop</h1>'
self.assertEqual(s, mark_safe(s).__html__())
+ def test_mark_for_escaping_object_implementing_dunder_str(self):
+ class Obj(object):
+ def __str__(self):
+ return '<obj>'
+
+ s = mark_for_escaping(Obj())
+
+ self.assertRenderEqual('{{ s }}', '&lt;obj&gt;', s=s)
+
def test_add_lazy_safe_text_and_safe_text(self):
s = html.escape(lazystr('a'))
s += mark_safe('&b')