Skip to content

Commit

Permalink
[py3] Supported integers in HttpResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
aaugustin committed Aug 19, 2012
1 parent 500fe9c commit 536b030
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
19 changes: 10 additions & 9 deletions django/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _BaseCookie__set(self, key, real_value, coded_value):
from django.http.multipartparser import MultiPartParser
from django.http.utils import *
from django.utils.datastructures import MultiValueDict, ImmutableList
from django.utils.encoding import smart_bytes, smart_str, iri_to_uri, force_text
from django.utils.encoding import force_bytes, force_text, iri_to_uri, smart_bytes, smart_str
from django.utils.http import cookie_date
from django.utils import six
from django.utils import timezone
Expand Down Expand Up @@ -670,10 +670,12 @@ def delete_cookie(self, key, path='/', domain=None):
expires='Thu, 01-Jan-1970 00:00:00 GMT')

def _get_content(self):
if self.has_header('Content-Encoding'):
# XXX this doesn't work under Python 3 when e is an integer (#18764)
return b''.join([bytes(e) for e in self._container])
return b''.join([smart_bytes(e, self._charset) for e in self._container])
# 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)

def _set_content(self, value):
if hasattr(value, '__iter__') and not isinstance(value, (bytes, six.string_types)):
Expand All @@ -690,10 +692,9 @@ def __iter__(self):
return self

def __next__(self):
chunk = next(self._iterator)
if isinstance(chunk, six.text_type):
chunk = chunk.encode(self._charset)
return bytes(chunk)
# 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)

next = __next__ # Python 2 compatibility

Expand Down
2 changes: 1 addition & 1 deletion tests/regressiontests/httpwrappers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def test_iter_content(self):
my_iter = r.__iter__()
result = list(my_iter)
#'\xde\x9e' == unichr(1950).encode('utf-8')
self.assertEqual(result, ['1', '2', '3', b'\xde\x9e'])
self.assertEqual(result, [b'1', b'2', b'3', b'\xde\x9e'])
self.assertEqual(r.content, b'123\xde\x9e')

#with Content-Encoding header
Expand Down

0 comments on commit 536b030

Please sign in to comment.