summaryrefslogtreecommitdiff
path: root/django/http
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2012-06-07 18:08:47 +0200
committerClaude Paroz <claude@2xlibre.net>2012-06-07 18:08:47 +0200
commit4a103086d5c67fa4fcc53c106c9fdf644c742dd8 (patch)
tree3df00600c27f6369f7561c3b8ddf2f97d2d341d9 /django/http
parent706fd9adc0b6587c7f96a834c757708e64fcf615 (diff)
Fixed #18269 -- Applied unicode_literals for Python 3 compatibility.
Thanks Vinay Sajip for the support of his django3 branch and Jannis Leidel for the review.
Diffstat (limited to 'django/http')
-rw-r--r--django/http/__init__.py8
-rw-r--r--django/http/multipartparser.py14
2 files changed, 15 insertions, 7 deletions
diff --git a/django/http/__init__.py b/django/http/__init__.py
index bf848deb38..30d7e5dc2f 100644
--- a/django/http/__init__.py
+++ b/django/http/__init__.py
@@ -1,4 +1,4 @@
-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals
import datetime
import os
@@ -57,13 +57,14 @@ else:
if not _cookie_allows_colon_in_names:
def load(self, rawdata):
self.bad_cookies = set()
- super(SimpleCookie, self).load(rawdata)
+ super(SimpleCookie, self).load(smart_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_str(key)
try:
M = self.get(key, Morsel())
M.set(key, real_value, coded_value)
@@ -131,7 +132,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_str(u'<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
+ return smart_str('<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
(request.__class__.__name__,
path,
unicode(get),
@@ -488,6 +489,7 @@ class QueryDict(MultiValueDict):
"""
output = []
if safe:
+ safe = smart_str(safe, self.encoding)
encode = lambda k, v: '%s=%s' % ((quote(k, safe), quote(v, safe)))
else:
encode = lambda k, v: urlencode({k: v})
diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py
index 162c08e38d..bbe4b052b7 100644
--- a/django/http/multipartparser.py
+++ b/django/http/multipartparser.py
@@ -4,6 +4,7 @@ Multi-part parsing for file uploads.
Exposes one class, ``MultiPartParser``, which feeds chunks of uploaded data to
file upload handlers for processing.
"""
+from __future__ import unicode_literals
import cgi
from django.conf import settings
@@ -59,7 +60,7 @@ class MultiPartParser(object):
raise MultiPartParserError('Invalid Content-Type: %s' % content_type)
# Parse the header to get the boundary to split the parts.
- ctypes, opts = parse_header(content_type)
+ ctypes, opts = parse_header(content_type.encode('ascii'))
boundary = opts.get('boundary')
if not boundary or not cgi.valid_boundary(boundary):
raise MultiPartParserError('Invalid boundary in multipart: %s' % boundary)
@@ -76,6 +77,8 @@ class MultiPartParser(object):
# This means we shouldn't continue...raise an error.
raise MultiPartParserError("Invalid content length: %r" % content_length)
+ if isinstance(boundary, unicode):
+ boundary = boundary.encode('ascii')
self._boundary = boundary
self._input_data = input_data
@@ -589,14 +592,17 @@ class Parser(object):
yield parse_boundary_stream(sub_stream, 1024)
def parse_header(line):
- """ Parse the header into a key-value. """
+ """ Parse the header into a key-value.
+ Input (line): bytes, output: unicode for key/name, bytes for value which
+ will be decoded later
+ """
plist = _parse_header_params(b';' + line)
- key = plist.pop(0).lower()
+ key = plist.pop(0).lower().decode('ascii')
pdict = {}
for p in plist:
i = p.find(b'=')
if i >= 0:
- name = p[:i].strip().lower()
+ name = p[:i].strip().lower().decode('ascii')
value = p[i+1:].strip()
if len(value) >= 2 and value[0] == value[-1] == b'"':
value = value[1:-1]