summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2013-03-07 21:43:58 (GMT)
committerR David Murray <rdmurray@bitdance.com>2013-03-07 21:43:58 (GMT)
commitaddb0be63e82fd99d931c227b07a8f515ad3d181 (patch)
tree94a4f714faf6d318496d8b076c43625516286a10 /Lib
parent0e7ec44aa976dfbf2c8dd0f82078138646052959 (diff)
parente67c6c545b3b102fb84eb9317d8b83fb2ca2ca56 (diff)
downloadcpython-addb0be63e82fd99d931c227b07a8f515ad3d181.zip
cpython-addb0be63e82fd99d931c227b07a8f515ad3d181.tar.gz
cpython-addb0be63e82fd99d931c227b07a8f515ad3d181.tar.bz2
Merge: #14645: Generator now emits correct linesep for all parts.
Previously the parts of the message retained whatever linesep they had on read, which means if the messages weren't read in univeral newline mode, the line endings could well be inconsistent. In general sending it via smtplib would result in them getting fixed, but it is better to generate them correctly to begin with. Also, the new send_message method of smtplib does not do the fixup, so that method is producing rfc-invalid output without this fix.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/email/generator.py22
-rw-r--r--Lib/test/test_email/test_email.py34
2 files changed, 52 insertions, 4 deletions
diff --git a/Lib/email/generator.py b/Lib/email/generator.py
index de9da39..27dd0db 100644
--- a/Lib/email/generator.py
+++ b/Lib/email/generator.py
@@ -146,6 +146,19 @@ class Generator:
# BytesGenerator overrides this to encode strings to bytes.
return s
+ def _write_lines(self, lines):
+ # We have to transform the line endings.
+ if not lines:
+ return
+ lines = lines.splitlines(True)
+ 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(self._NL)
+
def _write(self, msg):
# We can't write the headers yet because of the following scenario:
# say a multipart message includes the boundary string somewhere in
@@ -217,7 +230,7 @@ class Generator:
payload = msg.get_payload()
if self._mangle_from_:
payload = fcre.sub('>From ', payload)
- self.write(payload)
+ self._write_lines(payload)
# Default body handler
_writeBody = _handle_text
@@ -256,7 +269,8 @@ class Generator:
preamble = fcre.sub('>From ', msg.preamble)
else:
preamble = msg.preamble
- self.write(preamble + self._NL)
+ self._write_lines(preamble)
+ self.write(self._NL)
# dash-boundary transport-padding CRLF
self.write('--' + boundary + self._NL)
# body-part
@@ -278,7 +292,7 @@ class Generator:
epilogue = fcre.sub('>From ', msg.epilogue)
else:
epilogue = msg.epilogue
- self.write(epilogue)
+ self._write_lines(epilogue)
def _handle_multipart_signed(self, msg):
# The contents of signed parts has to stay unmodified in order to keep
@@ -402,7 +416,7 @@ class BytesGenerator(Generator):
if _has_surrogates(msg._payload) and not self.policy.cte_type=='7bit':
if self._mangle_from_:
msg._payload = fcre.sub(">From ", msg._payload)
- self.write(msg._payload)
+ self._write_lines(msg._payload)
else:
super(BytesGenerator,self)._handle_text(msg)
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
index 593d27b..dd54a1c 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -3111,6 +3111,40 @@ multipart/report
email.utils.make_msgid(domain='testdomain-string')[-19:],
'@testdomain-string>')
+ def test_Generator_linend(self):
+ # Issue 14645.
+ with openfile('msg_26.txt', newline='\n') as f:
+ msgtxt = f.read()
+ msgtxt_nl = msgtxt.replace('\r\n', '\n')
+ msg = email.message_from_string(msgtxt)
+ s = StringIO()
+ g = email.generator.Generator(s)
+ g.flatten(msg)
+ self.assertEqual(s.getvalue(), msgtxt_nl)
+
+ def test_BytesGenerator_linend(self):
+ # Issue 14645.
+ with openfile('msg_26.txt', newline='\n') as f:
+ msgtxt = f.read()
+ msgtxt_nl = msgtxt.replace('\r\n', '\n')
+ msg = email.message_from_string(msgtxt_nl)
+ s = BytesIO()
+ g = email.generator.BytesGenerator(s)
+ g.flatten(msg, linesep='\r\n')
+ self.assertEqual(s.getvalue().decode('ascii'), msgtxt)
+
+ def test_BytesGenerator_linend_with_non_ascii(self):
+ # Issue 14645.
+ with openfile('msg_26.txt', 'rb') as f:
+ msgtxt = f.read()
+ msgtxt = msgtxt.replace(b'with attachment', b'fo\xf6')
+ msgtxt_nl = msgtxt.replace(b'\r\n', b'\n')
+ msg = email.message_from_bytes(msgtxt_nl)
+ s = BytesIO()
+ g = email.generator.BytesGenerator(s)
+ g.flatten(msg, linesep='\r\n')
+ self.assertEqual(s.getvalue(), msgtxt)
+
# Test the iterator/generators
class TestIterators(TestEmailBase):