diff --git a/django/http/__init__.py b/django/http/__init__.py
index b9c5caa..9ec2afd 100644
a
|
b
|
from django.core.files import uploadhandler
|
85 | 85 | from django.http.multipartparser import MultiPartParser |
86 | 86 | from django.http.utils import * |
87 | 87 | from django.utils.datastructures import MultiValueDict, ImmutableList |
88 | | from django.utils.encoding import smart_bytes, smart_str, iri_to_uri, force_text |
| 88 | from django.utils.encoding import force_bytes, force_text, iri_to_uri, smart_bytes, smart_str |
89 | 89 | from django.utils.http import cookie_date |
90 | 90 | from django.utils import six |
91 | 91 | from django.utils import timezone |
… |
… |
class HttpResponse(object):
|
670 | 670 | expires='Thu, 01-Jan-1970 00:00:00 GMT') |
671 | 671 | |
672 | 672 | 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) |
677 | 679 | |
678 | 680 | def _set_content(self, value): |
679 | 681 | if hasattr(value, '__iter__') and not isinstance(value, (bytes, six.string_types)): |
… |
… |
class HttpResponse(object):
|
690 | 692 | return self |
691 | 693 | |
692 | 694 | 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) |
697 | 698 | |
698 | 699 | next = __next__ # Python 2 compatibility |
699 | 700 | |
diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
index 332f25d..bbb2518 100644
a
|
b
|
class HttpResponseTests(unittest.TestCase):
|
296 | 296 | my_iter = r.__iter__() |
297 | 297 | result = list(my_iter) |
298 | 298 | #'\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']) |
300 | 300 | self.assertEqual(r.content, b'123\xde\x9e') |
301 | 301 | |
302 | 302 | #with Content-Encoding header |