Skip to content

Commit 45b2f88

Browse files
jenstroegerbitdancer
authored andcommitted
bpo-34424: Handle different policy.linesep lengths correctly. (#8803)
1 parent 8da5ebe commit 45b2f88

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Lib/email/_header_value_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,7 @@ def _refold_parse_tree(parse_tree, *, policy):
26252625
want_encoding = False
26262626
last_ew = None
26272627
if part.syntactic_break:
2628-
encoded_part = part.fold(policy=policy)[:-1] # strip nl
2628+
encoded_part = part.fold(policy=policy)[:-len(policy.linesep)]
26292629
if policy.linesep not in encoded_part:
26302630
# It fits on a single line
26312631
if len(encoded_part) > maxlen - len(lines[-1]):

Lib/test/test_email/test_generator.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from email import message_from_string, message_from_bytes
55
from email.message import EmailMessage
66
from email.generator import Generator, BytesGenerator
7+
from email.headerregistry import Address
78
from email import policy
89
from test.test_email import TestEmailBase, parameterize
910

@@ -291,6 +292,27 @@ def test_smtputf8_policy(self):
291292
g.flatten(msg)
292293
self.assertEqual(s.getvalue(), expected)
293294

295+
def test_smtp_policy(self):
296+
msg = EmailMessage()
297+
msg["From"] = Address(addr_spec="[email protected]", display_name="Páolo")
298+
msg["To"] = Address(addr_spec="[email protected]", display_name="Dinsdale")
299+
msg["Subject"] = "Nudge nudge, wink, wink"
300+
msg.set_content("oh boy, know what I mean, know what I mean?")
301+
expected = textwrap.dedent("""\
302+
From: =?utf-8?q?P=C3=A1olo?= <[email protected]>
303+
To: Dinsdale <[email protected]>
304+
Subject: Nudge nudge, wink, wink
305+
Content-Type: text/plain; charset="utf-8"
306+
Content-Transfer-Encoding: 7bit
307+
MIME-Version: 1.0
308+
309+
oh boy, know what I mean, know what I mean?
310+
""").encode().replace(b"\n", b"\r\n")
311+
s = io.BytesIO()
312+
g = BytesGenerator(s, policy=policy.SMTP)
313+
g.flatten(msg)
314+
self.assertEqual(s.getvalue(), expected)
315+
294316

295317
if __name__ == '__main__':
296318
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix serialization of messages containing encoded strings when the
2+
policy.linesep is set to a multi-character string. Patch by Jens Troeger.

0 commit comments

Comments
 (0)