inconsistent exception when parsing a broken media type with debug enabled or disabled
The MIMEtype
class raises different errors depending on if Python interpreter has assertions enabled:
$ PYTHONPATH=. python3 test_bad.py
Traceback (most recent call last):
File "/home/hades/dev/pyxdg/test_bad.py", line 3, in <module>
MIMEtype("broken")
File "/home/hades/dev/pyxdg/xdg/Mime.py", line 70, in __new__
assert '/' not in subtype
TypeError: argument of type 'NoneType' is not iterable
$ PYTHONPATH=. python3 -O test_bad.py
Traceback (most recent call last):
File "/home/hades/dev/pyxdg/test_bad.py", line 3, in <module>
MIMEtype("broken")
File "/home/hades/dev/pyxdg/xdg/Mime.py", line 72, in __new__
subtype = subtype.lower()
AttributeError: 'NoneType' object has no attribute 'lower'
$ cat test_bad.py
from xdg.Mime import MIMEtype
MIMEtype("broken")
My suggestion is to raise ValueError, as per Python docs[1]. I'm attaching a patch for your consideration.0001-Mime-raise-consistent-errors-when-provided-an-invali.patch
[1] https://docs.python.org/3/library/exceptions.html#ValueError