Merged revisions 78274 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r78274 | r.david.murray | 2010-02-20 23:23:00 -0500 (Sat, 20 Feb 2010) | 9 lines

  Issue 7970: When email.Parser.Parser parses a MIME message of type
  message/rfc822 it turns it into an object whose body consists of
  a list containing a single Message object.  HeaderParser, on the
  other hand, just copies the body as a string.  Generator.flatten
  has a special handler for the message mime type that expected the
  body to be the one item list.  This fails if the message was parsed
  by HeaderParser.  So we now check to see if the body is a string
  first, and if so just we just emit it.
........
diff --git a/Lib/email/generator.py b/Lib/email/generator.py
index 61521b8..996f9dd 100644
--- a/Lib/email/generator.py
+++ b/Lib/email/generator.py
@@ -254,8 +254,16 @@
         # of length 1.  The zeroth element of the list should be the Message
         # object for the subpart.  Extract that object, stringify it, and
         # write it out.
-        g.flatten(msg.get_payload(0), unixfrom=False)
-        self._fp.write(s.getvalue())
+        # Except, it turns out, when it's a string instead, which happens when
+        # and only when HeaderParser is used on a message of mime type
+        # message/rfc822.  Such messages are generated by, for example,
+        # Groupwise when forwarding unadorned messages.  (Issue 7970.)  So
+        # in that case we just emit the string body.
+        payload = msg.get_payload()
+        if isinstance(payload, list):
+            g.flatten(msg.get_payload(0), unixfrom=False)
+            payload = s.getvalue()
+        self._fp.write(payload)
 
 
 
diff --git a/Lib/email/test/data/msg_46.txt b/Lib/email/test/data/msg_46.txt
new file mode 100644
index 0000000..1e22c4f
--- /dev/null
+++ b/Lib/email/test/data/msg_46.txt
@@ -0,0 +1,23 @@
+Return-Path: <sender@example.net>
+Delivery-Date: Mon, 08 Feb 2010 14:05:16 +0100
+Received: from example.org (example.org [64.5.53.58])
+    by example.net (node=mxbap2) with ESMTP (Nemesis)
+    id UNIQUE for someone@example.com; Mon, 08 Feb 2010 14:05:16 +0100
+Date: Mon, 01 Feb 2010 12:21:16 +0100
+From: "Sender" <sender@example.net>
+To: <someone@example.com>
+Subject: GroupwiseForwardingTest
+Mime-Version: 1.0
+Content-Type: message/rfc822
+
+Return-path: <sender@example.net>
+Message-ID: <4B66B890.4070408@teconcept.de>
+Date: Mon, 01 Feb 2010 12:18:40 +0100
+From: "Dr. Sender" <sender@example.net>
+MIME-Version: 1.0
+To: "Recipient" <recipient@example.com>
+Subject: GroupwiseForwardingTest
+Content-Type: text/plain; charset=ISO-8859-15
+Content-Transfer-Encoding: 7bit
+
+Testing email forwarding with Groupwise 1.2.2010
diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py
index 7736412..27ddc7c 100644
--- a/Lib/email/test/test_email.py
+++ b/Lib/email/test/test_email.py
@@ -178,6 +178,18 @@
         self.assertRaises(errors.HeaderParseError,
                           msg.set_boundary, 'BOUNDARY')
 
+    def test_message_rfc822_only(self):
+        # Issue 7970: message/rfc822 not in multipart parsed by
+        # HeaderParser caused an exception when flattened.
+        fp = openfile(findfile('msg_46.txt'))
+        msgdata = fp.read()
+        parser = HeaderParser()
+        msg = parser.parsestr(msgdata)
+        out = StringIO()
+        gen = Generator(out, True, 0)
+        gen.flatten(msg, False)
+        self.assertEqual(out.getvalue(), msgdata)
+
     def test_get_decoded_payload(self):
         eq = self.assertEqual
         msg = self._msgobj('msg_10.txt')
diff --git a/Misc/NEWS b/Misc/NEWS
index 7b9c8e5..67a9300 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -242,6 +242,9 @@
 Library
 -------
 
+- Issue #7970: email.Generator.flatten now correctly flattens message/rfc822
+  messages parsed by email.Parser.HeaderParser.
+
 - Issue #7361: Importlib was not properly checking the number of bytes in
   bytecode file when it was less then 8 bytes.