From f10a1b06416e59a66cc9c3c8437fe045cb0fb03c Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 11 Aug 2012 14:44:34 +0200 Subject: [PATCH] [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 --- django/http/__init__.py | 8 ++++---- django/http/multipartparser.py | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/django/http/__init__.py b/django/http/__init__.py index 05824ad1d9..f2e1cda8ff 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -61,14 +61,14 @@ else: 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 = '' 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 @@ class HttpRequest(object): 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 diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py index 9c66827cbb..a324935d3d 100644 --- a/django/http/multipartparser.py +++ b/django/http/multipartparser.py @@ -507,9 +507,11 @@ class BoundaryIter(object): 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