Ticket #28121: 28121_master.patch

File 28121_master.patch, 1.7 KB (added by Thomas Achtemichuk, 8 years ago)

Test and patch for master

  • django/utils/encoding.py

    diff --git a/django/utils/encoding.py b/django/utils/encoding.py
    index 1ac14a66d7..d551909b47 100644
    a b from urllib.parse import quote  
    66
    77from django.utils import six
    88from django.utils.functional import Promise
     9from django.utils.safestring import SafeData, SafeText
    910
    1011
    1112class DjangoUnicodeDecodeError(UnicodeDecodeError):
    def force_text(s, encoding='utf-8', strings_only=False, errors='strict'):  
    6263        return s
    6364    try:
    6465        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)
    6670        else:
    6771            s = str(s)
    6872    except UnicodeDecodeError as e:
  • tests/utils_tests/test_encoding.py

    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 (  
    1010    uri_to_iri,
    1111)
    1212from django.utils.functional import SimpleLazyObject
     13from django.utils.safestring import SafeBytes, SafeText
    1314from django.utils.translation import gettext_lazy
    1415
    1516
    class TestEncodingUtils(SimpleTestCase):  
    3031        s = SimpleLazyObject(lambda: 'x')
    3132        self.assertTrue(type(force_text(s)), str)
    3233
     34    def test_force_text_safe_bytes(self):
     35        s = SafeBytes(b'')
     36        self.assertIsInstance(force_text(s), SafeText)
     37
    3338    def test_force_text_DjangoUnicodeDecodeError(self):
    3439        msg = (
    3540            "'utf-8' codec can't decode byte 0xff in position 0: invalid "
Back to Top