summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVille Skyttä <ville.skytta@iki.fi>2016-04-28 10:41:02 +0300
committerVille Skyttä <ville.skytta@iki.fi>2016-05-02 10:19:27 +0300
commit7d3bf001d284ffbd9d555ce213241040c291f79b (patch)
tree71ffd2ac1521f4f1f57df31c7cb4943b95846f24
parent78e3e74deff9dddc54e22d1fbe9071383ab39ddc (diff)
downloadpython-mimeparse-7d3bf001d284ffbd9d555ce213241040c291f79b.tar.gz
Handle non-numeric q value gracefully
-rw-r--r--mimeparse.py5
-rw-r--r--testdata.json1
2 files changed, 5 insertions, 1 deletions
diff --git a/mimeparse.py b/mimeparse.py
index 4be4eb1..7f4aa3c 100644
--- a/mimeparse.py
+++ b/mimeparse.py
@@ -72,7 +72,10 @@ def parse_media_range(range):
necessary.
"""
(type, subtype, params) = parse_mime_type(range)
- if not params.get('q') or not 0 <= float(params['q']) <= 1:
+ try:
+ if not params.get('q') or not 0 <= float(params['q']) <= 1:
+ params['q'] = '1'
+ except ValueError: # from float()
params['q'] = '1'
return (type, subtype, params)
diff --git a/testdata.json b/testdata.json
index d5f079a..ab7693c 100644
--- a/testdata.json
+++ b/testdata.json
@@ -8,6 +8,7 @@
["application/xml ; q=1;b=other",["application", "xml", {"q": "1", "b":"other"}]],
["application/xml ; q=2;b=other",["application", "xml", {"q": "1", "b":"other"}]],
["application/xml ; q=0",["application", "xml", {"q": "0"}]],
+ ["application/xml ; q=foo", ["application", "xml", {"q": "1"}]],
[" *; q=.2",["*", "*", {"q": ".2"}]]
],