diff --git a/django/utils/encoding.py b/django/utils/encoding.py
index 1ac14a66d7..d551909b47 100644
a
|
b
|
from urllib.parse import quote
|
6 | 6 | |
7 | 7 | from django.utils import six |
8 | 8 | from django.utils.functional import Promise |
| 9 | from django.utils.safestring import SafeData, SafeText |
9 | 10 | |
10 | 11 | |
11 | 12 | class DjangoUnicodeDecodeError(UnicodeDecodeError): |
… |
… |
def force_text(s, encoding='utf-8', strings_only=False, errors='strict'):
|
62 | 63 | return s |
63 | 64 | try: |
64 | 65 | if isinstance(s, bytes): |
65 | | s = str(s, encoding, errors) |
| 66 | if isinstance(s, SafeData): |
| 67 | s = SafeText(str(s, encoding, errors)) |
| 68 | else: |
| 69 | s = str(s, encoding, errors) |
66 | 70 | else: |
67 | 71 | s = str(s) |
68 | 72 | except UnicodeDecodeError as e: |
diff --git a/tests/utils_tests/test_encoding.py b/tests/utils_tests/test_encoding.py
index bca6549fe7..1dde0407ec 100644
a
|
b
|
from django.utils.encoding import (
|
10 | 10 | uri_to_iri, |
11 | 11 | ) |
12 | 12 | from django.utils.functional import SimpleLazyObject |
| 13 | from django.utils.safestring import SafeBytes, SafeText |
13 | 14 | from django.utils.translation import gettext_lazy |
14 | 15 | |
15 | 16 | |
… |
… |
class TestEncodingUtils(SimpleTestCase):
|
30 | 31 | s = SimpleLazyObject(lambda: 'x') |
31 | 32 | self.assertTrue(type(force_text(s)), str) |
32 | 33 | |
| 34 | def test_force_text_safe_bytes(self): |
| 35 | s = SafeBytes(b'') |
| 36 | self.assertIsInstance(force_text(s), SafeText) |
| 37 | |
33 | 38 | def test_force_text_DjangoUnicodeDecodeError(self): |
34 | 39 | msg = ( |
35 | 40 | "'utf-8' codec can't decode byte 0xff in position 0: invalid " |