[issue19772] str serialization of Message object may mutate the payload and CTE.

Vajrasky Kok report at bugs.python.org
Sat Feb 8 09:14:43 CET 2014


Vajrasky Kok added the comment:

Actually, I am thinking of this approach (cloning the message just after entering the flatten method):

diff -r b541ecd32115 Lib/email/generator.py
--- a/Lib/email/generator.py	Fri Feb 07 16:11:17 2014 -0800
+++ b/Lib/email/generator.py	Sat Feb 08 15:55:01 2014 +0800
@@ -67,7 +67,7 @@
         # Just delegate to the file object
         self._fp.write(s)
 
-    def flatten(self, msg, unixfrom=False, linesep=None):
+    def flatten(self, msg_obj, unixfrom=False, linesep=None):
         r"""Print the message object tree rooted at msg to the output file
         specified when the Generator instance was created.
 
@@ -86,6 +86,8 @@
         # from the msg, and _encoded_XXX constants for operating on data that
         # has already been converted (to bytes in the BytesGenerator) and
         # inserted into a temporary buffer.
+        import copy
+        msg = copy.copy(msg_obj)
         policy = msg.policy if self.policy is None else self.policy
         if linesep is not None:
             policy = policy.clone(linesep=linesep)

It works for this bug but apparently it fails one test.

======================================================================
FAIL: test_make_boundary (__main__.TestMessageAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "Lib/test/test_email/test_email.py", line 199, in test_make_boundary
    'multipart/form-data; boundary="==')
AssertionError: 'multipart/form-data' != 'multipart/form-data; boundary="=='
- multipart/form-data
+ multipart/form-data; boundary="==

So somehow, according to the ultimate design of email library, msg can not leave flatten method unscratched. :)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19772>
_______________________________________


More information about the Python-bugs-list mailing list