Skip to content

Commit 7d3bf00

Browse files
committed
Handle non-numeric q value gracefully
1 parent 78e3e74 commit 7d3bf00

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

mimeparse.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ def parse_media_range(range):
7272
necessary.
7373
"""
7474
(type, subtype, params) = parse_mime_type(range)
75-
if not params.get('q') or not 0 <= float(params['q']) <= 1:
75+
try:
76+
if not params.get('q') or not 0 <= float(params['q']) <= 1:
77+
params['q'] = '1'
78+
except ValueError: # from float()
7679
params['q'] = '1'
7780

7881
return (type, subtype, params)

testdata.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
["application/xml ; q=1;b=other",["application", "xml", {"q": "1", "b":"other"}]],
99
["application/xml ; q=2;b=other",["application", "xml", {"q": "1", "b":"other"}]],
1010
["application/xml ; q=0",["application", "xml", {"q": "0"}]],
11+
["application/xml ; q=foo", ["application", "xml", {"q": "1"}]],
1112
[" *; q=.2",["*", "*", {"q": ".2"}]]
1213
],
1314

0 commit comments

Comments
 (0)