Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[py3] Fixed Python 3 compatibility of http handling

* Using str() when Python 2 expects bytes and Python 3 Unicode
* Fixed reraise-ing syntax
* Fixed slicing of byte strings
  • Loading branch information...
commit f10a1b06416e59a66cc9c3c8437fe045cb0fb03c 1 parent 22527a8
@claudep claudep authored
Showing with 9 additions and 7 deletions.
  1. +4 −4 django/http/__init__.py
  2. +5 −3 django/http/multipartparser.py
View
8 django/http/__init__.py
@@ -61,14 +61,14 @@ def value_encode(self, val):
if not _cookie_allows_colon_in_names:
def load(self, rawdata):
self.bad_cookies = set()
- super(SimpleCookie, self).load(smart_bytes(rawdata))
+ super(SimpleCookie, self).load(str(rawdata))
for key in self.bad_cookies:
del self[key]
# override private __set() method:
# (needed for using our Morsel, and for laxness with CookieError
def _BaseCookie__set(self, key, real_value, coded_value):
- key = smart_bytes(key)
+ key = str(key)
try:
M = self.get(key, Morsel())
M.set(key, real_value, coded_value)
@@ -137,7 +137,7 @@ def build_request_repr(request, path_override=None, GET_override=None,
except:
meta = '<could not parse>'
path = path_override if path_override is not None else request.path
- return smart_bytes('<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
+ return str('<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
(request.__class__.__name__,
path,
six.text_type(get),
@@ -294,7 +294,7 @@ def body(self):
try:
self._body = self.read()
except IOError as e:
- six.reraise(UnreadablePostError, e, sys.exc_traceback)
+ six.reraise(UnreadablePostError, UnreadablePostError(*tuple(e.args)), sys.exc_info()[2])
self._stream = BytesIO(self._body)
return self._body
View
8 django/http/multipartparser.py
@@ -507,9 +507,11 @@ def _find_boundary(self, data, eof = False):
end = index
next = index + len(self._boundary)
# backup over CRLF
- if data[max(0,end-1)] == b'\n':
+ last = max(0, end-1)
+ if data[last:last+1] == b'\n':
end -= 1
- if data[max(0,end-1)] == b'\r':
+ last = max(0, end-1)
+ if data[last:last+1] == b'\r':
end -= 1
return end, next
@@ -613,7 +615,7 @@ def parse_header(line):
if i >= 0:
name = p[:i].strip().lower().decode('ascii')
value = p[i+1:].strip()
- if len(value) >= 2 and value[0] == value[-1] == b'"':
+ if len(value) >= 2 and value[:1] == value[-1:] == b'"':
value = value[1:-1]
value = value.replace(b'\\\\', b'\\').replace(b'\\"', b'"')
pdict[name] = value
Please sign in to comment.
Something went wrong with that request. Please try again.