[Python-checkins] cpython (merge 3.2 -> default): #14062: fix BytesParser handling of Header objects

r.david.murray python-checkins at python.org
Wed Mar 14 19:24:46 CET 2012


http://hg.python.org/cpython/rev/7617f3071320
changeset:   75631:7617f3071320
parent:      75629:3a5a0e7d38c5
parent:      75630:d0bf40ff20ef
user:        R David Murray <rdmurray at bitdance.com>
date:        Wed Mar 14 14:24:22 2012 -0400
summary:
  #14062: fix BytesParser handling of Header objects

This is a different fix than the 3.2 fix, but the new tests are the same.

This also affected smtplib.SMTP.send_message, which calls BytesParser.

files:
  Lib/email/generator.py            |   3 ++
  Lib/test/test_email/test_email.py |  24 +++++++++++++++++++
  Misc/NEWS                         |   3 ++
  3 files changed, 30 insertions(+), 0 deletions(-)


diff --git a/Lib/email/generator.py b/Lib/email/generator.py
--- a/Lib/email/generator.py
+++ b/Lib/email/generator.py
@@ -386,6 +386,9 @@
                     h = Header(v, charset=_charset.UNKNOWN8BIT, header_name=h)
                 else:
                     h = Header(v, header_name=h)
+            else:
+                # Assume it is a Header-like object.
+                h = v
             self.write(h.encode(linesep=self._NL,
                                 maxlinelen=self._maxheaderlen)+self._NL)
         # A blank line always separates headers from body
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -3601,6 +3601,30 @@
         g.flatten(msg)
         self.assertEqual(s.getvalue(), source)
 
+    def test_bytes_generator_b_encoding_linesep(self):
+        # Issue 14062: b encoding was tacking on an extra \n.
+        m = Message()
+        # This has enough non-ascii that it should always end up b encoded.
+        m['Subject'] = Header('žluťoučký kůň')
+        s = BytesIO()
+        g = email.generator.BytesGenerator(s)
+        g.flatten(m, linesep='\r\n')
+        self.assertEqual(
+            s.getvalue(),
+            b'Subject: =?utf-8?b?xb5sdcWlb3XEjWvDvSBrxa/FiA==?=\r\n\r\n')
+
+    def test_generator_b_encoding_linesep(self):
+        # Since this broke in ByteGenerator, test Generator for completeness.
+        m = Message()
+        # This has enough non-ascii that it should always end up b encoded.
+        m['Subject'] = Header('žluťoučký kůň')
+        s = StringIO()
+        g = email.generator.Generator(s)
+        g.flatten(m, linesep='\r\n')
+        self.assertEqual(
+            s.getvalue(),
+            'Subject: =?utf-8?b?xb5sdcWlb3XEjWvDvSBrxa/FiA==?=\r\n\r\n')
+
     def test_crlf_control_via_policy(self):
         # msg_26 is crlf terminated
         with openfile('msg_26.txt', 'rb') as fp:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@
 Library
 -------
 
+- Issue #14062: BytesGenerator now correctly folds Header objects,
+  including using linesep when folding.
+
 - Issue #13839: When invoked on the command-line, the pstats module now
   accepts several filenames of profile stat files and merges them all.
   Patch by Matt Joiner.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list