]> granicus.if.org Git - python/commitdiff
#11019: Make BytesGenerator handle Message with None body.
authorR. David Murray <rdmurray@bitdance.com>
Wed, 26 Jan 2011 21:21:32 +0000 (21:21 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Wed, 26 Jan 2011 21:21:32 +0000 (21:21 +0000)
Bug discovery and initial patch by Victor Stinner.

Lib/email/generator.py
Lib/email/test/test_email.py
Misc/NEWS

index 9d33f1cb870a97b584b60b3ae77b9dd453f40caf..531fa9a7a4326b163c51a476d8a020753c519a6d 100644 (file)
@@ -377,6 +377,8 @@ class BytesGenerator(Generator):
     def _handle_text(self, msg):
         # If the string has surrogates the original source was bytes, so
         # just write it back out.
+        if msg._payload is None:
+            return
         if _has_surrogates(msg._payload):
             self.write(msg._payload)
         else:
index e4083ad63a62fa93f50d08a92d90554da426cb64..16772b1ee53b081bf7a2c979264b4f6fd9a389dd 100644 (file)
@@ -2989,6 +2989,13 @@ class Test8BitBytesHandling(unittest.TestCase):
         email.generator.BytesGenerator(out).flatten(msg)
         self.assertEqual(out.getvalue(), self.non_latin_bin_msg)
 
+    def test_bytes_generator_handles_None_body(self):
+        #Issue 11019
+        msg = email.message.Message()
+        out = BytesIO()
+        email.generator.BytesGenerator(out).flatten(msg)
+        self.assertEqual(out.getvalue(), b"\n")
+
     non_latin_bin_msg_as7bit_wrapped = textwrap.dedent("""\
         From: foo@bar.com
         To: =?unknown-8bit?q?b=C3=A1z?=
index f2e37b52a51ce6d9238c159a39d65c2fabba2fe7..28803f8c10a4fa796732d12c98729e11ec89f65f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -16,6 +16,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #11019: Fixed BytesGenerator so that it correctly handles a Message
+  with a None body.
+
 - Issue #11014: Make 'filter' argument in tarfile.Tarfile.add() into a
   keyword-only argument.  The preceding positional argument was deprecated,
   so it made no sense to add filter as a positional argument.