Skip to content

Commit

Permalink
python#19003: Only replace \r and/or \n line endings in email.generator.
Browse files Browse the repository at this point in the history
This is a further restoration of backward compatibility, as well as
being correct per the RFCs.
  • Loading branch information
bitdancer committed Sep 11, 2016
1 parent e4946e7 commit 0e0cfd7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
16 changes: 10 additions & 6 deletions Lib/email/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
UNDERSCORE = '_'
NL = '\n' # XXX: no longer used by the code below.

NLCRE = re.compile(r'\r\n|\r|\n')
fcre = re.compile(r'^From ', re.MULTILINE)


Expand Down Expand Up @@ -149,14 +150,17 @@ def _write_lines(self, lines):
# We have to transform the line endings.
if not lines:
return
lines = lines.splitlines(True)
lines = NLCRE.split(lines)
for line in lines[:-1]:
self.write(line.rstrip('\r\n'))
self.write(self._NL)
laststripped = lines[-1].rstrip('\r\n')
self.write(laststripped)
if len(lines[-1]) != len(laststripped):
self.write(line)
self.write(self._NL)
if lines[-1]:
self.write(lines[-1])
# XXX logic tells me this else should be needed, but the tests fail
# with it and pass without it. (NLCRE.split ends with a blank element
# if and only if there was a trailing newline.)
#else:
# self.write(self._NL)

def _write(self, msg):
# We can't write the headers yet because of the following scenario:
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,18 @@ def test_binary_body_with_encode_noop(self):
self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata))
self.assertEqual(msg2.get_payload(decode=True), bytesdata)

def test_binary_body_with_unicode_linend_encode_noop(self):
# Issue 19003: This is a variation on #16564.
bytesdata = b'\x0b\xfa\xfb\xfc\xfd\xfe\xff'
msg = MIMEApplication(bytesdata, _encoder=encoders.encode_noop)
self.assertEqual(msg.get_payload(decode=True), bytesdata)
s = BytesIO()
g = BytesGenerator(s)
g.flatten(msg)
wireform = s.getvalue()
msg2 = email.message_from_bytes(wireform)
self.assertEqual(msg2.get_payload(decode=True), bytesdata)

def test_binary_body_with_encode_quopri(self):
# Issue 14360.
bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff '
Expand Down
4 changes: 4 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ Core and Builtins
Library
-------


- Issue #19003:m email.generator now replaces only \r and/or \n line
endings, per the RFC, instead of all unicode line endings.

- Issue #28019: itertools.count() no longer rounds non-integer step in range
between 1.0 and 2.0 to 1.

Expand Down

0 comments on commit 0e0cfd7

Please sign in to comment.