[Python-checkins] cpython (3.2): #14062: fix BytesParser handling of linesep for Header objects

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


http://hg.python.org/cpython/rev/d0bf40ff20ef
changeset:   75630:d0bf40ff20ef
branch:      3.2
parent:      75625:2800530b00d3
user:        R David Murray <rdmurray at bitdance.com>
date:        Wed Mar 14 14:05:03 2012 -0400
summary:
  #14062: fix BytesParser handling of linesep for Header objects

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

files:
  Lib/email/generator.py       |   2 +-
  Lib/email/test/test_email.py |  25 +++++++++++++++++++++++-
  Misc/NEWS                    |   3 ++
  3 files changed, 28 insertions(+), 2 deletions(-)


diff --git a/Lib/email/generator.py b/Lib/email/generator.py
--- a/Lib/email/generator.py
+++ b/Lib/email/generator.py
@@ -360,7 +360,7 @@
         for h, v in msg._headers:
             self.write('%s: ' % h)
             if isinstance(v, Header):
-                self.write(v.encode(maxlinelen=self._maxheaderlen)+NL)
+                self.write(v.encode(maxlinelen=self._maxheaderlen)+self._NL)
             elif _has_surrogates(v):
                 # If we have raw 8bit data in a byte string, we have no idea
                 # what the encoding is.  There is no safe way to split this
diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py
--- a/Lib/email/test/test_email.py
+++ b/Lib/email/test/test_email.py
@@ -1243,7 +1243,6 @@
              =?utf-8?q?_folding_white_space_works?=""")+'\n')
 
 
-
 # Test mangling of "From " lines in the body of a message
 class TestFromMangling(unittest.TestCase):
     def setUp(self):
@@ -3441,6 +3440,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')
+
     maxDiff = None
 
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,9 @@
 Library
 -------
 
+- Issue #14062: Header objects now correctly respect the 'linesep' setting
+  when processed by BytesParser (which smtplib.SMTP.send_message uses).
+
 - Issue #14291: Email now defaults to utf-8 for non-ASCII unicode headers
   instead of raising an error.  This fixes a regression relative to 2.7.
 

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


More information about the Python-checkins mailing list