summaryrefslogtreecommitdiff
path: root/django/http/multipartparser.py
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/multipartparser.py
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/multipartparser.py')
-rw-r--r--django/http/multipartparser.py14
1 files changed, 10 insertions, 4 deletions
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]