cpython: 34fb36972f8d (original) (raw)
Mercurial > cpython
changeset 89053:34fb36972f8d 3.3
#19772: Do not mutate message when downcoding to 7bit. This is a bit of an ugly hack because of the way generator pieces together the output message. The deepcopys aren't too expensive, though, because we know it is only called on messages that are not multiparts, and the payload (the thing that could be large) is an immutable object. Test and preliminary work on patch by Vajrasky Kok. [#19772]
R David Murray rdmurray@bitdance.com | |
---|---|
date | Sat, 08 Feb 2014 11:48:20 -0500 |
parents | 58be80e7e653 |
children | 2e97d3500970 1dcb9d0d53a6 |
files | Lib/email/generator.py Lib/test/test_email/test_email.py Misc/NEWS |
diffstat | 3 files changed, 28 insertions(+), 1 deletions(-)[+] [-] Lib/email/generator.py 14 Lib/test/test_email/test_email.py 12 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/email/generator.py +++ b/Lib/email/generator.py @@ -12,6 +12,7 @@ import time import random import warnings +from copy import deepcopy from io import StringIO, BytesIO from email._policybase import compat32 from email.header import Header @@ -173,10 +174,18 @@ class Generator: # necessary. oldfp = self._fp try:
self._munge_cte = None[](#l1.15) self._fp = sfp = self._new_buffer()[](#l1.16) self._dispatch(msg)[](#l1.17) finally:[](#l1.18) self._fp = oldfp[](#l1.19)
munge_cte = self._munge_cte[](#l1.20)
del self._munge_cte[](#l1.21)
# If we munged the cte, copy the message again and re-fix the CTE.[](#l1.22)
if munge_cte:[](#l1.23)
msg = deepcopy(msg)[](#l1.24)
msg.replace_header('content-transfer-encoding', munge_cte[0])[](#l1.25)
msg.replace_header('content-type', munge_cte[1])[](#l1.26) # Write the headers. First we see if the message object wants to[](#l1.27) # handle that itself. If not, we'll do it generically.[](#l1.28) meth = getattr(msg, '_write_headers', None)[](#l1.29)
@@ -225,9 +234,14 @@ class Generator: if _has_surrogates(msg._payload): charset = msg.get_param('charset') if charset is not None:
# XXX: This copy stuff is an ugly hack to avoid modifying the[](#l1.34)
# existing message.[](#l1.35)
msg = deepcopy(msg)[](#l1.36) del msg['content-transfer-encoding'][](#l1.37) msg.set_payload(payload, charset)[](#l1.38) payload = msg.get_payload()[](#l1.39)
self._munge_cte = (msg['content-transfer-encoding'],[](#l1.40)
msg['content-type'])[](#l1.41) if self._mangle_from_:[](#l1.42) payload = fcre.sub('>From ', payload)[](#l1.43) self._write_lines(payload)[](#l1.44)
--- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -3495,7 +3495,7 @@ Here's the message body self.assertTrue(msg.get_payload(0).get_payload().endswith('\r\n')) -class Test8BitBytesHandling(unittest.TestCase): +class Test8BitBytesHandling(TestEmailBase): # In Python3 all input is string, but that doesn't work if the actual input # uses an 8bit transfer encoding. To hack around that, in email 5.1 we # decode byte streams using the surrogateescape error handler, and @@ -3748,6 +3748,16 @@ class Test8BitBytesHandling(unittest.Tes email.generator.Generator(out).flatten(msg) self.assertEqual(out.getvalue(), self.non_latin_bin_msg_as7bit_wrapped)
- def test_str_generator_should_not_mutate_msg_when_handling_8bit(self):
msg = email.message_from_bytes(self.non_latin_bin_msg)[](#l2.17)
out = BytesIO()[](#l2.18)
BytesGenerator(out).flatten(msg)[](#l2.19)
orig_value = out.getvalue()[](#l2.20)
Generator(StringIO()).flatten(msg) # Should not mutate msg
out = BytesIO()[](#l2.22)
BytesGenerator(out).flatten(msg)[](#l2.23)
self.assertEqual(out.getvalue(), orig_value)[](#l2.24)
+ def test_bytes_generator_with_unix_from(self): # The unixfrom contains a current date, so we can't check it # literally. Just make sure the first word is 'From' and the
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -48,6 +48,9 @@ Core and Builtins Library ------- +- Issue #19772: email.generator no longer mutates the message object when