mirror of
https://github.com/django/django.git
synced 2024-12-26 02:56:25 +00:00
[py3] Fixed regression introduced in 536b030363
.
Refs #18764.
Reverted 536b030363
and switched to a more explicit way of avoiding
calling bytes(<int>).
This definitely deserves a refactoring. Specifically, _get_content
should just return b''.join(self). Unfortunately that's impossible
with the current tests.
This commit is contained in:
parent
536b030363
commit
2892cb0ec4
@ -85,7 +85,7 @@ from django.core.files import uploadhandler
|
||||
from django.http.multipartparser import MultiPartParser
|
||||
from django.http.utils import *
|
||||
from django.utils.datastructures import MultiValueDict, ImmutableList
|
||||
from django.utils.encoding import force_bytes, force_text, iri_to_uri, smart_bytes, smart_str
|
||||
from django.utils.encoding import smart_bytes, smart_str, iri_to_uri, force_text
|
||||
from django.utils.http import cookie_date
|
||||
from django.utils import six
|
||||
from django.utils import timezone
|
||||
@ -670,12 +670,16 @@ class HttpResponse(object):
|
||||
expires='Thu, 01-Jan-1970 00:00:00 GMT')
|
||||
|
||||
def _get_content(self):
|
||||
# The logic below obeys the following constraints:
|
||||
# - do not perform any encoding if a Content-Encoding is set (#4969)
|
||||
# - force string conversion of non-string types (#16494)
|
||||
# - avoid simply calling bytes() with Python 3 (#18764)
|
||||
encoding = 'ascii' if self.has_header('Content-Encoding') else self._charset
|
||||
return b''.join(force_bytes(e, encoding) for e in self._container)
|
||||
if self.has_header('Content-Encoding'):
|
||||
def make_bytes(value):
|
||||
if isinstance(value, int):
|
||||
return six.text_type(value).encode()
|
||||
elif isinstance(value, six.text_type):
|
||||
return value.encode('ascii')
|
||||
else:
|
||||
return bytes(value)
|
||||
return b''.join(make_bytes(e) for e in self._container)
|
||||
return b''.join(smart_bytes(e, self._charset) for e in self._container)
|
||||
|
||||
def _set_content(self, value):
|
||||
if hasattr(value, '__iter__') and not isinstance(value, (bytes, six.string_types)):
|
||||
@ -692,9 +696,12 @@ class HttpResponse(object):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
# Use the same logic as _get_content.
|
||||
encoding = 'ascii' if self.has_header('Content-Encoding') else self._charset
|
||||
return force_bytes(next(self._iterator), encoding)
|
||||
chunk = next(self._iterator)
|
||||
if isinstance(chunk, int):
|
||||
return six.text_type(chunk).encode()
|
||||
if isinstance(chunk, six.text_type):
|
||||
return chunk.encode(self._charset)
|
||||
return bytes(chunk)
|
||||
|
||||
next = __next__ # Python 2 compatibility
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user