diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-12-23 21:49:05 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-12-27 18:17:18 +0100 |
| commit | b429a9796a162f9e12e6d34579468621ba9accc7 (patch) | |
| tree | 12262a0507b0b134e31ebe32b5ba0b2b8cee6427 | |
| parent | a79012f6d88a8cd67bd35c54b91d826f2b7a4cb2 (diff) | |
[1.7.x] Fixed an inconsistency introduced in 547b1810.
mark_safe and mark_for_escaping should have been kept similar.
On Python 2 this change has no effect. On Python 3 it fixes the use case
shown in the regression test for mark_for_escaping, which used to raise
a TypeError. The regression test for mark_safe is just for completeness.
Backport of 5c5eb5fe from master.
| -rw-r--r-- | django/utils/safestring.py | 2 | ||||
| -rw-r--r-- | docs/releases/1.7.2.txt | 3 | ||||
| -rw-r--r-- | tests/utils_tests/test_safestring.py | 18 |
3 files changed, 22 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/docs/releases/1.7.2.txt b/docs/releases/1.7.2.txt index 31a96a4ccd..84188cd494 100644 --- a/docs/releases/1.7.2.txt +++ b/docs/releases/1.7.2.txt @@ -175,3 +175,6 @@ Bugfixes * Corrected ``contrib.sites`` default site creation in a multiple database setup (:ticket:`24000`). + +* Restored support for objects that aren't :class:`str` or :class:`bytes` in + :func:`~django.utils.safestring.mark_for_escaping` on Python 3. diff --git a/tests/utils_tests/test_safestring.py b/tests/utils_tests/test_safestring.py index 65242e46fb..aaa4a07d1c 100644 --- a/tests/utils_tests/test_safestring.py +++ b/tests/utils_tests/test_safestring.py @@ -31,6 +31,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&b', s=s) @@ -47,3 +56,12 @@ class SafeStringTest(TestCase): def test_html(self): 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 }}', '<obj>', s=s) |
