[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
This commit is contained in:
Claude Paroz 2012-08-11 14:44:34 +02:00
parent 22527a821b
commit f10a1b0641
2 changed files with 9 additions and 7 deletions

View File

@ -61,14 +61,14 @@ else:
if not _cookie_allows_colon_in_names: if not _cookie_allows_colon_in_names:
def load(self, rawdata): def load(self, rawdata):
self.bad_cookies = set() self.bad_cookies = set()
super(SimpleCookie, self).load(smart_bytes(rawdata)) super(SimpleCookie, self).load(str(rawdata))
for key in self.bad_cookies: for key in self.bad_cookies:
del self[key] del self[key]
# override private __set() method: # override private __set() method:
# (needed for using our Morsel, and for laxness with CookieError # (needed for using our Morsel, and for laxness with CookieError
def _BaseCookie__set(self, key, real_value, coded_value): def _BaseCookie__set(self, key, real_value, coded_value):
key = smart_bytes(key) key = str(key)
try: try:
M = self.get(key, Morsel()) M = self.get(key, Morsel())
M.set(key, real_value, coded_value) M.set(key, real_value, coded_value)
@ -137,7 +137,7 @@ def build_request_repr(request, path_override=None, GET_override=None,
except: except:
meta = '<could not parse>' meta = '<could not parse>'
path = path_override if path_override is not None else request.path 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__, (request.__class__.__name__,
path, path,
six.text_type(get), six.text_type(get),
@ -294,7 +294,7 @@ class HttpRequest(object):
try: try:
self._body = self.read() self._body = self.read()
except IOError as e: 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) self._stream = BytesIO(self._body)
return self._body return self._body

View File

@ -507,9 +507,11 @@ class BoundaryIter(object):
end = index end = index
next = index + len(self._boundary) next = index + len(self._boundary)
# backup over CRLF # 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 end -= 1
if data[max(0,end-1)] == b'\r': last = max(0, end-1)
if data[last:last+1] == b'\r':
end -= 1 end -= 1
return end, next return end, next
@ -613,7 +615,7 @@ def parse_header(line):
if i >= 0: if i >= 0:
name = p[:i].strip().lower().decode('ascii') name = p[:i].strip().lower().decode('ascii')
value = p[i+1:].strip() 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[1:-1]
value = value.replace(b'\\\\', b'\\').replace(b'\\"', b'"') value = value.replace(b'\\\\', b'\\').replace(b'\\"', b'"')
pdict[name] = value pdict[name] = value