Ticket #18764: 18764.patch

File 18764.patch, 2.7 KB (added by aaugustin, 3 years ago)
  • django/http/__init__.py

    diff --git a/django/http/__init__.py b/django/http/__init__.py
    index b9c5caa..9ec2afd 100644
    a b from django.core.files import uploadhandler 
    8585from django.http.multipartparser import MultiPartParser
    8686from django.http.utils import *
    8787from django.utils.datastructures import MultiValueDict, ImmutableList
    88 from django.utils.encoding import smart_bytes, smart_str, iri_to_uri, force_text
     88from django.utils.encoding import force_bytes, force_text, iri_to_uri, smart_bytes, smart_str
    8989from django.utils.http import cookie_date
    9090from django.utils import six
    9191from django.utils import timezone
    class HttpResponse(object): 
    670670                        expires='Thu, 01-Jan-1970 00:00:00 GMT')
    671671
    672672    def _get_content(self):
    673         if self.has_header('Content-Encoding'):
    674             # XXX this doesn't work under Python 3 when e is an integer (#18764)
    675             return b''.join([bytes(e) for e in self._container])
    676         return b''.join([smart_bytes(e, self._charset) for e in self._container])
     673        # The logic below obeys the following constraints:
     674        # - do not perform any encoding if a Content-Encoding is set (#4969)
     675        # - force string conversion of non-string types (#16494)
     676        # - avoid simply calling bytes() with Python 3 (#18764)
     677        encoding = 'ascii' if self.has_header('Content-Encoding') else self._charset
     678        return b''.join(force_bytes(e, encoding) for e in self._container)
    677679
    678680    def _set_content(self, value):
    679681        if hasattr(value, '__iter__') and not isinstance(value, (bytes, six.string_types)):
    class HttpResponse(object): 
    690692        return self
    691693
    692694    def __next__(self):
    693         chunk = next(self._iterator)
    694         if isinstance(chunk, six.text_type):
    695             chunk = chunk.encode(self._charset)
    696         return bytes(chunk)
     695        # Use the same logic as _get_content.
     696        encoding = 'ascii' if self.has_header('Content-Encoding') else self._charset
     697        return force_bytes(next(self._iterator), encoding)
    697698
    698699    next = __next__             # Python 2 compatibility
    699700
  • tests/regressiontests/httpwrappers/tests.py

    diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
    index 332f25d..bbb2518 100644
    a b class HttpResponseTests(unittest.TestCase): 
    296296        my_iter = r.__iter__()
    297297        result = list(my_iter)
    298298        #'\xde\x9e' == unichr(1950).encode('utf-8')
    299         self.assertEqual(result, ['1', '2', '3', b'\xde\x9e'])
     299        self.assertEqual(result, [b'1', b'2', b'3', b'\xde\x9e'])
    300300        self.assertEqual(r.content, b'123\xde\x9e')
    301301
    302302        #with Content-Encoding header
Back to Top