(type, subtype) = parts[0].split("/") ValueError: too many values to unpack
Real example of HEADERS:
{{{
!python
... 'DOCUMENT_URI': '/', 'GATEWAY_INTERFACE': 'CGI/1.1', 'HTTP_ACCEPT': 'text/html, image/gif, image/jpeg, ; q=.2, /*; q=.2', 'HTTP_ACCEPT_ENCODING': 'gzip,defalte', 'HTTP_ACCEPT_LANGUAGE': 'ru,en-us;q=0.7,en;q=0.3', ... }}}
{{{
!python
Traceback (most recent call last):
File "/var/www/movister.ru/web_site/django/core/handlers/base.py", line 178, in get_response response = middleware_method(request, response)
File "/var/www/movister.ru/web_site/django_authopenid/middleware.py", line 44, in process_response request.META['HTTP_ACCEPT']) == 'application/xrds+xml':
File "/var/www/movister.ru/web_site/django_authopenid/utils/mimeparse.py", line 111, in best_match parsed_header = [parse_media_range(r) for r in header.split(",")]
File "/var/www/movister.ru/web_site/django_authopenid/utils/mimeparse.py", line 54, in parse_media_range (type, subtype, params) = parse_mime_type(range)
File "/var/www/movister.ru/web_site/django_authopenid/utils/mimeparse.py", line 38, in parse_mime_type (type, subtype) = parts[0].split("/")
ValueError: need more than 1 value to unpack }}}
{{{
!python
In [4]: from django_authopenid.utils.mimeparse import parse_mime_type In [5]: parse_mime_type("text/html, image/gif, image/jpeg, ; q=.2, /*; q=.2")
ValueError Traceback (most recent call last)
/home/ram/workspace/movister/web_site/<ipython console> in <module>()
/home/ram/workspace/movister/web_site/django_authopenid/utils/mimeparse.pyc in parse_mime_type(mime_type) 36 params = dict([tuple([s.strip() for s in param.split("=")])\ 37 for param in parts[1:] ]) ---> 38 (type, subtype) = parts[0].split("/") 39 return (type.strip(), subtype.strip(), params) 40
ValueError: too many values to unpack }}}
Comments (5)
-
-
Patch-solution
@@ -32,9 +32,12 @@ ('application', 'xhtml', {'q', '0.5'}) """ - parts = mime_type.split(";") - params = dict([tuple([s.strip() for s in param.split("=")])\ - for param in parts[1:] ]) + + parts = [] + for part in mime_type.split(";"): + parts += part.split(",") if part.count(",") else [part] + params = dict([tuple([s.strip() for s in param.split("=")]) \ + for param in parts[1:] if param.count("=")]) (type, subtype) = parts[0].split("/") return (type.strip(), subtype.strip(), params)
-
@@ -23,6 +23,8 @@ __email__ = "joe@bitworking.org" __credits__ = "" +import re + def parse_mime_type(mime_type): """Carves up a mime_type and returns a tuple of the (type, subtype, params) where 'params' is a dictionary @@ -32,10 +34,10 @@ ('application', 'xhtml', {'q', '0.5'}) """ - parts = mime_type.split(";") - params = dict([tuple([s.strip() for s in param.split("=")])\ - for param in parts[1:] ]) - (type, subtype) = parts[0].split("/") + + parts = re.split(r'[,|;]', mime_type) + params = dict([tuple([s.strip() for s in param.split("=")]) for param in parts[1:] if param.count("=")]) + (type, subtype) = parts[0].split("/") if parts[0].count("/") else (parts[0], parts[0]) return (type.strip(), subtype.strip(), params) @@ -126,6 +128,7 @@ self.assertEqual(('application', 'xml', {'q': '1'}), parse_media_range('application/xml ; q=')) self.assertEqual(('application', 'xml', {'q': '1', 'b': 'other'}), parse_media_range('application/xml ; q=1;b=other')) self.assertEqual(('application', 'xml', {'q': '1', 'b': 'other'}), parse_media_range('application/xml ; q=2;b=other')) + self.assertEqual(('text', 'html', {'q': '.2'}), parse_media_range('text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2'))
-
The most recent code from @ramusus has fixed this for me - any chance of getting it pulled into the main distribution?
-
- changed status to resolved
- Log in to comment