diff -r 5e978d499066 Doc/library/email.policy.rst --- a/Doc/library/email.policy.rst Wed Apr 23 08:39:02 2014 -0700 +++ b/Doc/library/email.policy.rst Thu Apr 24 14:07:14 2014 +0200 @@ -187,6 +187,18 @@ :const:`False` (the default), defects will be passed to the :meth:`register_defect` method. + + + .. attribute:: mangle_from\_ + + If :const:`True`, lines starting with *"From "* in the body are + escaped by putting a ``>`` in front of them. This parameter is used when + the message is being serialized by a generator. + Default: :const:`True`. + + .. versionadded:: 3.5 + The *mangle_from_* parameter. + 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 5e978d499066 Lib/email/_policybase.py --- a/Lib/email/_policybase.py Wed Apr 23 08:39:02 2014 -0700 +++ b/Lib/email/_policybase.py Thu Apr 24 14:07:14 2014 +0200 @@ -149,12 +149,18 @@ 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_ = True def handle_defect(self, obj, defect): """Based on policy, either raise defect or call register_defect. diff -r 5e978d499066 Lib/email/generator.py --- a/Lib/email/generator.py Wed Apr 23 08:39:02 2014 -0700 +++ b/Lib/email/generator.py Thu Apr 24 14:07:14 2014 +0200 @@ -32,16 +32,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 @@ -55,6 +55,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 @@ -446,7 +449,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 5e978d499066 Lib/email/policy.py --- a/Lib/email/policy.py Wed Apr 23 08:39:02 2014 -0700 +++ b/Lib/email/policy.py Thu Apr 24 14:07:14 2014 +0200 @@ -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 5e978d499066 Lib/test/test_email/test_generator.py --- a/Lib/test/test_email/test_generator.py Wed Apr 23 08:39:02 2014 -0700 +++ b/Lib/test/test_email/test_generator.py Thu Apr 24 14:07:14 2014 +0200 @@ -139,6 +139,21 @@ g.flatten(msg, linesep='\n') self.assertEqual(s.getvalue(), self.typ(expected)) + def test_set_mangle_from_via_policy(self): + source = textwrap.dedent("""\ + Subject: test that + from is mangeld in the body! + + From time to time I write a rhyme. + """) + for b in True, False: + expected = source.replace('From ', '>From ') if b else source + msg = self.msgmaker(self.typ(source)) + s = self.ioclass() + g = self.genclass(s, policy=policy.EmailPolicy(mangle_from_=b)) + g.flatten(msg) + self.assertEqual(s.getvalue(), self.typ(expected)) + class TestGenerator(TestGeneratorBase, TestEmailBase): diff -r 5e978d499066 Lib/test/test_email/test_policy.py --- a/Lib/test/test_email/test_policy.py Wed Apr 23 08:39:02 2014 -0700 +++ b/Lib/test/test_email/test_policy.py Thu Apr 24 14:07:14 2014 +0200 @@ -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