From 7451dd6b656929ef25baedf64a8176b448813c5f Mon Sep 17 00:00:00 2001 From: Mircea Cosbuc Date: Fri, 10 Mar 2017 13:09:39 +0200 Subject: [PATCH] [3.5] bpo-29478: Fix passing max_line_length=None from Compat32 policy (cherry picked from commit 03a9c7a0d2a27e91b540f92f0f019ef47d3fce6a) --- Lib/email/_policybase.py | 8 ++++++-- Lib/test/test_email/test_generator.py | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Lib/email/_policybase.py b/Lib/email/_policybase.py index c0d98a4f5429357..7326d3a2b1671ea 100644 --- a/Lib/email/_policybase.py +++ b/Lib/email/_policybase.py @@ -357,8 +357,12 @@ def _fold(self, name, value, sanitize): # Assume it is a Header-like object. h = value if h is not None: - parts.append(h.encode(linesep=self.linesep, - maxlinelen=self.max_line_length)) + # The Header class interprets a value of None for maxlinelen as the + # default value of 78, as recommended by RFC 2822. + maxlinelen = 0 + if self.max_line_length is not None: + maxlinelen = self.max_line_length + parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen)) parts.append(self.linesep) return ''.join(parts) diff --git a/Lib/test/test_email/test_generator.py b/Lib/test/test_email/test_generator.py index b1cbce26d5c1fb9..226c5f98c4039cf 100644 --- a/Lib/test/test_email/test_generator.py +++ b/Lib/test/test_email/test_generator.py @@ -162,6 +162,13 @@ def test_set_mangle_from_via_policy(self): g.flatten(msg) self.assertEqual(s.getvalue(), self.typ(expected)) + def test_compat32_max_line_length_does_not_fold_when_none(self): + msg = self.msgmaker(self.typ(self.refold_long_expected[0])) + s = self.ioclass() + g = self.genclass(s, policy=policy.compat32.clone(max_line_length=None)) + g.flatten(msg) + self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[0])) + class TestGenerator(TestGeneratorBase, TestEmailBase):