[Python-checkins] bpo-34424: Handle different policy.linesep lengths correctly. (#8803)

R. David Murray webhook-mailer at python.org
Mon May 13 21:07:50 EDT 2019


https://github.com/python/cpython/commit/45b2f8893c1b7ab3b3981a966f82e42beea82106
commit: 45b2f8893c1b7ab3b3981a966f82e42beea82106
branch: master
author: Jens Troeger <jenstroeger at users.noreply.github.com>
committer: R. David Murray <rdmurray at bitdance.com>
date: 2019-05-13T21:07:39-04:00
summary:

bpo-34424: Handle different policy.linesep lengths correctly. (#8803)

files:
A Misc/NEWS.d/next/Library/2018-08-18-14-47-00.bpo-34424.wAlRuS.rst
M Lib/email/_header_value_parser.py
M Lib/test/test_email/test_generator.py

diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py
index 922daa2560f0..bb26d5a556db 100644
--- a/Lib/email/_header_value_parser.py
+++ b/Lib/email/_header_value_parser.py
@@ -2625,7 +2625,7 @@ def _refold_parse_tree(parse_tree, *, policy):
                 want_encoding = False
                 last_ew = None
                 if part.syntactic_break:
-                    encoded_part = part.fold(policy=policy)[:-1] # strip nl
+                    encoded_part = part.fold(policy=policy)[:-len(policy.linesep)]
                     if policy.linesep not in encoded_part:
                         # It fits on a single line
                         if len(encoded_part) > maxlen - len(lines[-1]):
diff --git a/Lib/test/test_email/test_generator.py b/Lib/test/test_email/test_generator.py
index c1aeaefab775..89e7edeb63a8 100644
--- a/Lib/test/test_email/test_generator.py
+++ b/Lib/test/test_email/test_generator.py
@@ -4,6 +4,7 @@
 from email import message_from_string, message_from_bytes
 from email.message import EmailMessage
 from email.generator import Generator, BytesGenerator
+from email.headerregistry import Address
 from email import policy
 from test.test_email import TestEmailBase, parameterize
 
@@ -291,6 +292,27 @@ def test_smtputf8_policy(self):
         g.flatten(msg)
         self.assertEqual(s.getvalue(), expected)
 
+    def test_smtp_policy(self):
+        msg = EmailMessage()
+        msg["From"] = Address(addr_spec="foo at bar.com", display_name="Páolo")
+        msg["To"] = Address(addr_spec="bar at foo.com", display_name="Dinsdale")
+        msg["Subject"] = "Nudge nudge, wink, wink"
+        msg.set_content("oh boy, know what I mean, know what I mean?")
+        expected = textwrap.dedent("""\
+            From: =?utf-8?q?P=C3=A1olo?= <foo at bar.com>
+            To: Dinsdale <bar at foo.com>
+            Subject: Nudge nudge, wink, wink
+            Content-Type: text/plain; charset="utf-8"
+            Content-Transfer-Encoding: 7bit
+            MIME-Version: 1.0
+
+            oh boy, know what I mean, know what I mean?
+            """).encode().replace(b"\n", b"\r\n")
+        s = io.BytesIO()
+        g = BytesGenerator(s, policy=policy.SMTP)
+        g.flatten(msg)
+        self.assertEqual(s.getvalue(), expected)
+
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2018-08-18-14-47-00.bpo-34424.wAlRuS.rst b/Misc/NEWS.d/next/Library/2018-08-18-14-47-00.bpo-34424.wAlRuS.rst
new file mode 100644
index 000000000000..2b384cd5513f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-08-18-14-47-00.bpo-34424.wAlRuS.rst
@@ -0,0 +1,2 @@
+Fix serialization of messages containing encoded strings when the
+policy.linesep is set to a multi-character string. Patch by Jens Troeger.



More information about the Python-checkins mailing list