Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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
Claude Paroz authored August 11, 2012
8  django/http/__init__.py
@@ -61,14 +61,14 @@ def value_encode(self, val):
61 61
         if not _cookie_allows_colon_in_names:
62 62
             def load(self, rawdata):
63 63
                 self.bad_cookies = set()
64  
-                super(SimpleCookie, self).load(smart_bytes(rawdata))
  64
+                super(SimpleCookie, self).load(str(rawdata))
65 65
                 for key in self.bad_cookies:
66 66
                     del self[key]
67 67
 
68 68
             # override private __set() method:
69 69
             # (needed for using our Morsel, and for laxness with CookieError
70 70
             def _BaseCookie__set(self, key, real_value, coded_value):
71  
-                key = smart_bytes(key)
  71
+                key = str(key)
72 72
                 try:
73 73
                     M = self.get(key, Morsel())
74 74
                     M.set(key, real_value, coded_value)
@@ -137,7 +137,7 @@ def build_request_repr(request, path_override=None, GET_override=None,
137 137
     except:
138 138
         meta = '<could not parse>'
139 139
     path = path_override if path_override is not None else request.path
140  
-    return smart_bytes('<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
  140
+    return str('<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
141 141
                      (request.__class__.__name__,
142 142
                       path,
143 143
                       six.text_type(get),
@@ -294,7 +294,7 @@ def body(self):
294 294
             try:
295 295
                 self._body = self.read()
296 296
             except IOError as e:
297  
-                six.reraise(UnreadablePostError, e, sys.exc_traceback)
  297
+                six.reraise(UnreadablePostError, UnreadablePostError(*tuple(e.args)), sys.exc_info()[2])
298 298
             self._stream = BytesIO(self._body)
299 299
         return self._body
300 300
 
8  django/http/multipartparser.py
@@ -507,9 +507,11 @@ def _find_boundary(self, data, eof = False):
507 507
             end = index
508 508
             next = index + len(self._boundary)
509 509
             # backup over CRLF
510  
-            if data[max(0,end-1)] == b'\n':
  510
+            last = max(0, end-1)
  511
+            if data[last:last+1] == b'\n':
511 512
                 end -= 1
512  
-            if data[max(0,end-1)] == b'\r':
  513
+            last = max(0, end-1)
  514
+            if data[last:last+1] == b'\r':
513 515
                 end -= 1
514 516
             return end, next
515 517
 
@@ -613,7 +615,7 @@ def parse_header(line):
613 615
         if i >= 0:
614 616
             name = p[:i].strip().lower().decode('ascii')
615 617
             value = p[i+1:].strip()
616  
-            if len(value) >= 2 and value[0] == value[-1] == b'"':
  618
+            if len(value) >= 2 and value[:1] == value[-1:] == b'"':
617 619
                 value = value[1:-1]
618 620
                 value = value.replace(b'\\\\', b'\\').replace(b'\\"', b'"')
619 621
             pdict[name] = value

0 notes on commit f10a1b0

Please sign in to comment.
Something went wrong with that request. Please try again.