X Tutup
Skip to content

Commit 8d8f110

Browse files
committed
python#14062: fix BytesParser handling of Header objects
This is a different fix than the 3.2 fix, but the new tests are the same. This also affected smtplib.SMTP.send_message, which calls BytesParser.
2 parents 9d8c186 + 9fd170e commit 8d8f110

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Lib/email/generator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ def _write_headers(self, msg):
386386
h = Header(v, charset=_charset.UNKNOWN8BIT, header_name=h)
387387
else:
388388
h = Header(v, header_name=h)
389+
else:
390+
# Assume it is a Header-like object.
391+
h = v
389392
self.write(h.encode(linesep=self._NL,
390393
maxlinelen=self._maxheaderlen)+self._NL)
391394
# A blank line always separates headers from body

Lib/test/test_email/test_email.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3601,6 +3601,30 @@ def test_8bit_multipart(self):
36013601
g.flatten(msg)
36023602
self.assertEqual(s.getvalue(), source)
36033603

3604+
def test_bytes_generator_b_encoding_linesep(self):
3605+
# Issue 14062: b encoding was tacking on an extra \n.
3606+
m = Message()
3607+
# This has enough non-ascii that it should always end up b encoded.
3608+
m['Subject'] = Header('žluťoučký kůň')
3609+
s = BytesIO()
3610+
g = email.generator.BytesGenerator(s)
3611+
g.flatten(m, linesep='\r\n')
3612+
self.assertEqual(
3613+
s.getvalue(),
3614+
b'Subject: =?utf-8?b?xb5sdcWlb3XEjWvDvSBrxa/FiA==?=\r\n\r\n')
3615+
3616+
def test_generator_b_encoding_linesep(self):
3617+
# Since this broke in ByteGenerator, test Generator for completeness.
3618+
m = Message()
3619+
# This has enough non-ascii that it should always end up b encoded.
3620+
m['Subject'] = Header('žluťoučký kůň')
3621+
s = StringIO()
3622+
g = email.generator.Generator(s)
3623+
g.flatten(m, linesep='\r\n')
3624+
self.assertEqual(
3625+
s.getvalue(),
3626+
'Subject: =?utf-8?b?xb5sdcWlb3XEjWvDvSBrxa/FiA==?=\r\n\r\n')
3627+
36043628
def test_crlf_control_via_policy(self):
36053629
# msg_26 is crlf terminated
36063630
with openfile('msg_26.txt', 'rb') as fp:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #14062: BytesGenerator now correctly folds Header objects,
28+
including using linesep when folding.
29+
2730
- Issue #13839: When invoked on the command-line, the pstats module now
2831
accepts several filenames of profile stat files and merges them all.
2932
Patch by Matt Joiner.

0 commit comments

Comments
 (0)
X Tutup