[multipart] decode form data before calling parse_qs in python 3
authorJulien Cristau <julien.cristau@logilab.fr>
Thu, 01 Oct 2015 11:42:29 +0200
changeset 10746 407385314c0d
parent 10745 5318337e7128
child 10747 cf711d1c951f
[multipart] decode form data before calling parse_qs in python 3 parse_qs eats and returns 'str' on both python versions.
multipart.py
--- a/multipart.py	Thu Oct 01 11:41:20 2015 +0200
+++ b/multipart.py	Thu Oct 01 11:42:29 2015 +0200
@@ -45,7 +45,7 @@
 except ImportError: # pragma: no cover (fallback for Python 2.5)
     from StringIO import StringIO as BytesIO
 
-from six import text_type
+from six import PY3, text_type
 from six.moves.urllib.parse import parse_qs
 
 ##############################################################################
@@ -396,14 +396,19 @@
                               'application/x-url-encoded'):
             mem_limit = kw.get('mem_limit', 2**20)
             if content_length > mem_limit:
-                raise MultipartError("Request to big. Increase MAXMEM.")
+                raise MultipartError("Request too big. Increase MAXMEM.")
             data = stream.read(mem_limit)
             if stream.read(1): # These is more that does not fit mem_limit
-                raise MultipartError("Request to big. Increase MAXMEM.")
+                raise MultipartError("Request too big. Increase MAXMEM.")
+            if PY3:
+                data = data.decode('ascii')
             data = parse_qs(data, keep_blank_values=True)
             for key, values in data.items():
                 for value in values:
-                    forms[key.decode(charset)] = value.decode(charset)
+                    if PY3:
+                        forms[key] = value
+                    else:
+                        forms[key.decode(charset)] = value.decode(charset)
         else:
             raise MultipartError("Unsupported content type.")
     except MultipartError: