[issue3609] does parse_header really belong in CGI module?
R. David Murray
report at bugs.python.org
Thu Feb 19 00:06:05 CET 2015
R. David Murray added the comment:
There is no reason to move this to the email package. email can already parse headers just fine. It might be useful to have a parse_header utility method, though, since currently the easiest way to parse a single header using the email package is:
>>> from email.policy import HTTP
>>> from email.parser import Parser
>>> m = Parser(policy=HTTP).parsestr('Content-Type: text/plain; filename="foo"\n\n')
>>> m['Content-Type'].content_type
'text/plain'
>>> m['Content-type'].params
mappingproxy({'filename': 'foo'})
Which isn't as straightforward as the parse_header API when you are only interested in a single header.
It might also be useful to have the email MIME headers grow a 'value' attribute to return whatever the value is (in this case, text/plain), so it can be accessed regardless of the header type.
I would make parse_header be a utility method of the policy, I think. Then the email API would be:
from email.policy import HTTP
h = HTTP.parse_header('Content-Type: ...')
value, params = h.value, h.params
It would then be trivial to implement the backward compatibility shim for the CGI parse_header method using the above.
----------
stage: patch review -> needs patch
versions: +Python 3.5 -Python 3.4
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3609>
_______________________________________
More information about the Python-bugs-list
mailing list