diff -r 3ae2cd85a908 Doc/library/email.policy.rst --- a/Doc/library/email.policy.rst Sun Mar 09 11:18:16 2014 +0100 +++ b/Doc/library/email.policy.rst Sun Mar 09 23:42:42 2014 +0100 @@ -187,6 +187,16 @@ :const:`False` (the default), defects will be passed to the :meth:`register_defect` method. + + .. versionadded:: 3.4 + The *mangle_from_* parameter. + + .. attribute:: mangle_from\_ + + If :const:`True`, lines starting with *"From "* in the body are + escaped by putting a ``>`` in front of them. + Default: :const:`True`. + The following :class:`Policy` method is intended to be called by code using the email library to create policy instances with custom settings: diff -r 3ae2cd85a908 Lib/email/_policybase.py --- a/Lib/email/_policybase.py Sun Mar 09 11:18:16 2014 +0100 +++ b/Lib/email/_policybase.py Sun Mar 09 23:42:42 2014 +0100 @@ -149,12 +149,17 @@ 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. Default: True. + """ raise_on_defect = False linesep = '\n' cte_type = '8bit' max_line_length = 78 + mangle_from_ = True def handle_defect(self, obj, defect): """Based on policy, either raise defect or call register_defect. diff -r 3ae2cd85a908 Lib/email/generator.py --- a/Lib/email/generator.py Sun Mar 09 11:18:16 2014 +0100 +++ b/Lib/email/generator.py Sun Mar 09 23:42:42 2014 +0100 @@ -36,16 +36,16 @@ # 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 @@ -59,6 +59,9 @@ backward compatibility. """ + + 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 @@ -450,7 +453,7 @@ 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. diff -r 3ae2cd85a908 Lib/email/policy.py --- a/Lib/email/policy.py Sun Mar 09 11:18:16 2014 +0100 +++ b/Lib/email/policy.py Sun Mar 09 23:42:42 2014 +0100 @@ -75,6 +75,7 @@ refold_source = 'long' header_factory = HeaderRegistry() content_manager = raw_data_manager + mangle_from_ = False def __init__(self, **kw): # Ensure that each new instance gets a unique header factory diff -r 3ae2cd85a908 Lib/test/test_email/test_policy.py --- a/Lib/test/test_email/test_policy.py Sun Mar 09 11:18:16 2014 +0100 +++ b/Lib/test/test_email/test_policy.py Sun Mar 09 23:42:42 2014 +0100 @@ -22,6 +22,7 @@ 'linesep': '\n', 'cte_type': '8bit', 'raise_on_defect': False, + 'mangle_from_': True, } # These default values are the ones set on email.policy.default. # If any of these defaults change, the docs must be updated. @@ -31,6 +32,7 @@ 'header_factory': email.policy.EmailPolicy.header_factory, 'refold_source': 'long', 'content_manager': email.policy.EmailPolicy.content_manager, + 'mangle_from_': False, }) # For each policy under test, we give here what we expect the defaults to @@ -39,7 +41,8 @@ new_policy = email.policy.EmailPolicy() policies = { email.policy.compat32: make_defaults(compat32_defaults, {}), - email.policy.default: make_defaults(policy_defaults, {}), + email.policy.default: make_defaults(policy_defaults, + {'mangle_from_': False}), email.policy.SMTP: make_defaults(policy_defaults, {'linesep': '\r\n'}), email.policy.HTTP: make_defaults(policy_defaults,