diff options
author | R David Murray <rdmurray@bitdance.com> | 2015-05-17 18:24:33 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2015-05-17 18:24:33 (GMT) |
commit | fdb23c2fe5499d26701fa34873c1cdc347adcb80 (patch) | |
tree | 1f65d6f676b2feb7ee7c0e24068ca67160d91407 /Lib/email | |
parent | 224ef3ec3b0758956789c4c98e6cc93704304419 (diff) | |
download | cpython-fdb23c2fe5499d26701fa34873c1cdc347adcb80.zip cpython-fdb23c2fe5499d26701fa34873c1cdc347adcb80.tar.gz cpython-fdb23c2fe5499d26701fa34873c1cdc347adcb80.tar.bz2 |
#20098: add mangle_from_ policy option.
This defaults to True in the compat32 policy for backward compatibility,
but to False for all new policies.
Patch by Milan Oberkirch, with a few tweaks.
Diffstat (limited to 'Lib/email')
-rw-r--r-- | Lib/email/_policybase.py | 8 | ||||
-rw-r--r-- | Lib/email/generator.py | 13 |
2 files changed, 16 insertions, 5 deletions
diff --git a/Lib/email/_policybase.py b/Lib/email/_policybase.py index 8106114..c0d98a4 100644 --- a/Lib/email/_policybase.py +++ b/Lib/email/_policybase.py @@ -149,12 +149,18 @@ class Policy(_PolicyBase, metaclass=abc.ABCMeta): during serialization. None or 0 means no line wrapping is done. Default is 78. + mangle_from_ -- a flag that, when True escapes From_ lines in the + body of the message by putting a `>' in front of + them. This is used when the message is being + serialized by a generator. Default: True. + """ raise_on_defect = False linesep = '\n' cte_type = '8bit' max_line_length = 78 + mangle_from_ = False def handle_defect(self, obj, defect): """Based on policy, either raise defect or call register_defect. @@ -266,6 +272,8 @@ class Compat32(Policy): replicates the behavior of the email package version 5.1. """ + mangle_from_ = True + def _sanitize_header(self, name, value): # If the header value contains surrogates, return a Header using # the unknown-8bit charset to encode the bytes as encoded words. diff --git a/Lib/email/generator.py b/Lib/email/generator.py index 4735721..11ff16d 100644 --- a/Lib/email/generator.py +++ b/Lib/email/generator.py @@ -32,16 +32,16 @@ class Generator: # Public interface # - def __init__(self, outfp, mangle_from_=True, maxheaderlen=None, *, + def __init__(self, outfp, mangle_from_=None, maxheaderlen=None, *, policy=None): """Create the generator for message flattening. outfp is the output file-like object for writing the message to. It must have a write() method. - Optional mangle_from_ is a flag that, when True (the default), escapes - From_ lines in the body of the message by putting a `>' in front of - them. + Optional mangle_from_ is a flag that, when True (the default if policy + is not set), escapes From_ lines in the body of the message by putting + a `>' in front of them. Optional maxheaderlen specifies the longest length for a non-continued header. When a header line is longer (in characters, with tabs @@ -56,6 +56,9 @@ class Generator: flatten method is used. """ + + if mangle_from_ is None: + mangle_from_ = True if policy is None else policy.mangle_from_ self._fp = outfp self._mangle_from_ = mangle_from_ self.maxheaderlen = maxheaderlen @@ -449,7 +452,7 @@ class DecodedGenerator(Generator): Like the Generator base class, except that non-text parts are substituted with a format string representing the part. """ - def __init__(self, outfp, mangle_from_=True, maxheaderlen=78, fmt=None): + def __init__(self, outfp, mangle_from_=None, maxheaderlen=78, fmt=None): """Like Generator.__init__() except that an additional optional argument is allowed. |