]> granicus.if.org Git - python/commitdiff
#14062: fix BytesParser handling of linesep for Header objects
authorR David Murray <rdmurray@bitdance.com>
Wed, 14 Mar 2012 18:05:03 +0000 (14:05 -0400)
committerR David Murray <rdmurray@bitdance.com>
Wed, 14 Mar 2012 18:05:03 +0000 (14:05 -0400)
This also affected smtplib.SMTP.send_message, which calls BytesParser.

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

index f0e7a95477d61055b1e64f1f30b9f100375858c4..430ee73ea10c9168a6ce57cec111e2227b21e6ec 100644 (file)
@@ -360,7 +360,7 @@ class BytesGenerator(Generator):
         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
index f43bb38aa8d52a042b375e3fd1f79b89a1303b7d..5655938021c6e0b7b7574323f3fceab8aa1342c3 100644 (file)
@@ -1243,7 +1243,6 @@ List: List-Unsubscribe:
              =?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 @@ class Test8BitBytesHandling(unittest.TestCase):
         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
 
 
index b4dcf82608220a7354c785ee6975d063df6b2bc9..6d6268c4c8c1bba4e34a7f905cc3faf4024bfb8b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,9 @@ Core and Builtins
 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.