Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9fd170e

Browse files
committed
#14062: fix BytesParser handling of linesep for Header objects
This also affected smtplib.SMTP.send_message, which calls BytesParser.
1 parent 525fd54 commit 9fd170e

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

Lib/email/generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def _write_headers(self, msg):
360360
for h, v in msg._headers:
361361
self.write('%s: ' % h)
362362
if isinstance(v, Header):
363-
self.write(v.encode(maxlinelen=self._maxheaderlen)+NL)
363+
self.write(v.encode(maxlinelen=self._maxheaderlen)+self._NL)
364364
elif _has_surrogates(v):
365365
# If we have raw 8bit data in a byte string, we have no idea
366366
# what the encoding is. There is no safe way to split this

Lib/email/test/test_email.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,6 @@ def test_long_rfc2047_header_with_embedded_fws(self):
12431243
=?utf-8?q?_folding_white_space_works?=""")+'\n')
12441244

12451245

1246-
12471246
# Test mangling of "From " lines in the body of a message
12481247
class TestFromMangling(unittest.TestCase):
12491248
def setUp(self):
@@ -3441,6 +3440,30 @@ def test_8bit_multipart(self):
34413440
g.flatten(msg)
34423441
self.assertEqual(s.getvalue(), source)
34433442

3443+
def test_bytes_generator_b_encoding_linesep(self):
3444+
# Issue 14062: b encoding was tacking on an extra \n.
3445+
m = Message()
3446+
# This has enough non-ascii that it should always end up b encoded.
3447+
m['Subject'] = Header('žluťoučký kůň')
3448+
s = BytesIO()
3449+
g = email.generator.BytesGenerator(s)
3450+
g.flatten(m, linesep='\r\n')
3451+
self.assertEqual(
3452+
s.getvalue(),
3453+
b'Subject: =?utf-8?b?xb5sdcWlb3XEjWvDvSBrxa/FiA==?=\r\n\r\n')
3454+
3455+
def test_generator_b_encoding_linesep(self):
3456+
# Since this broke in ByteGenerator, test Generator for completeness.
3457+
m = Message()
3458+
# This has enough non-ascii that it should always end up b encoded.
3459+
m['Subject'] = Header('žluťoučký kůň')
3460+
s = StringIO()
3461+
g = email.generator.Generator(s)
3462+
g.flatten(m, linesep='\r\n')
3463+
self.assertEqual(
3464+
s.getvalue(),
3465+
'Subject: =?utf-8?b?xb5sdcWlb3XEjWvDvSBrxa/FiA==?=\r\n\r\n')
3466+
34443467
maxDiff = None
34453468

34463469

Misc/NEWS

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

25+
- Issue #14062: Header objects now correctly respect the 'linesep' setting
26+
when processed by BytesParser (which smtplib.SMTP.send_message uses).
27+
2528
- Issue #14291: Email now defaults to utf-8 for non-ASCII unicode headers
2629
instead of raising an error. This fixes a regression relative to 2.7.
2730

0 commit comments

Comments
 (0)