[Python-checkins] cpython (merge 3.3 -> default): Merge #16811: Fix folding of headers with no value in provisional policies.

r.david.murray python-checkins at python.org
Mon Feb 4 21:25:25 CET 2013


http://hg.python.org/cpython/rev/fe7f3e2e49ce
changeset:   82011:fe7f3e2e49ce
parent:      82009:c4e6e560e6f5
parent:      82010:e64b74227198
user:        R David Murray <rdmurray at bitdance.com>
date:        Mon Feb 04 15:25:06 2013 -0500
summary:
  Merge #16811: Fix folding of headers with no value in provisional policies.

files:
  Lib/email/policy.py                   |   2 +-
  Lib/test/test_email/test_inversion.py |  45 +++++++++++++++
  2 files changed, 46 insertions(+), 1 deletions(-)


diff --git a/Lib/email/policy.py b/Lib/email/policy.py
--- a/Lib/email/policy.py
+++ b/Lib/email/policy.py
@@ -173,7 +173,7 @@
         lines = value.splitlines()
         refold = (self.refold_source == 'all' or
                   self.refold_source == 'long' and
-                    (len(lines[0])+len(name)+2 > maxlen or
+                    (lines and len(lines[0])+len(name)+2 > maxlen or
                      any(len(x) > maxlen for x in lines[1:])))
         if refold or refold_binary and _has_surrogates(value):
             return self.header_factory(name, ''.join(lines)).fold(policy=self)
diff --git a/Lib/test/test_email/test_inversion.py b/Lib/test/test_email/test_inversion.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/test_email/test_inversion.py
@@ -0,0 +1,45 @@
+"""Test the parser and generator are inverses.
+
+Note that this is only strictly true if we are parsing RFC valid messages and
+producing RFC valid messages.
+"""
+
+import io
+import unittest
+from email import policy, message_from_bytes
+from email.generator import BytesGenerator
+from test.test_email import TestEmailBase, parameterize
+
+# This is like textwrap.dedent for bytes, except that it uses \r\n for the line
+# separators on the rebuilt string.
+def dedent(bstr):
+    lines = bstr.splitlines()
+    if not lines[0].strip():
+        raise ValueError("First line must contain text")
+    stripamt = len(lines[0]) - len(lines[0].lstrip())
+    return b'\r\n'.join(
+        [x[stripamt:] if len(x)>=stripamt else b''
+            for x in lines])
+
+
+ at parameterize
+class TestInversion(TestEmailBase, unittest.TestCase):
+
+    def msg_as_input(self, msg):
+        m = message_from_bytes(msg, policy=policy.SMTP)
+        b = io.BytesIO()
+        g = BytesGenerator(b)
+        g.flatten(m)
+        self.assertEqual(b.getvalue(), msg)
+
+    # XXX: spaces are not preserved correctly here yet in the general case.
+    msg_params = {
+        'header_with_one_space_body': (dedent(b"""\
+            From: abc at xyz.com
+            X-Status:\x20
+            Subject: test
+
+            foo
+            """),),
+
+            }

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


More information about the Python-checkins mailing list