diff options
author | R David Murray <rdmurray@bitdance.com> | 2016-09-07 21:44:34 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2016-09-07 21:44:34 (GMT) |
commit | dc1650ca062a99d41a029a6645dc72fd7d820c94 (patch) | |
tree | 7719487f2ea0d6a95d2e024e365dbedacf697534 /Lib/email/policy.py | |
parent | 6b46ec7733ad7391b9e008d2b273c556f140f88e (diff) | |
download | cpython-dc1650ca062a99d41a029a6645dc72fd7d820c94.zip cpython-dc1650ca062a99d41a029a6645dc72fd7d820c94.tar.gz cpython-dc1650ca062a99d41a029a6645dc72fd7d820c94.tar.bz2 |
#22233: Only split headers on \r and/or \n, per email RFCs.
Original patch by Martin Panter, new policy fixes by me.
Diffstat (limited to 'Lib/email/policy.py')
-rw-r--r-- | Lib/email/policy.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/Lib/email/policy.py b/Lib/email/policy.py index 6ac64a5..35d0e69 100644 --- a/Lib/email/policy.py +++ b/Lib/email/policy.py @@ -2,6 +2,7 @@ code that adds all the email6 features. """ +import re from email._policybase import Policy, Compat32, compat32, _extend_docstrings from email.utils import _has_surrogates from email.headerregistry import HeaderRegistry as HeaderRegistry @@ -18,6 +19,8 @@ __all__ = [ 'HTTP', ] +linesep_splitter = re.compile(r'\n|\r') + @_extend_docstrings class EmailPolicy(Policy): @@ -135,6 +138,8 @@ class EmailPolicy(Policy): if hasattr(value, 'name') and value.name.lower() == name.lower(): return (name, value) if isinstance(value, str) and len(value.splitlines())>1: + # XXX this error message isn't quite right when we use splitlines + # (see issue 22233), but I'm not sure what should happen here. raise ValueError("Header values may not contain linefeed " "or carriage return characters") return (name, self.header_factory(name, value)) @@ -150,7 +155,9 @@ class EmailPolicy(Policy): """ if hasattr(value, 'name'): return value - return self.header_factory(name, ''.join(value.splitlines())) + # We can't use splitlines here because it splits on more than \r and \n. + value = ''.join(linesep_splitter.split(value)) + return self.header_factory(name, value) def fold(self, name, value): """+ |