From ef5bb25e2d6147cd44be9c9b166525fb30485be0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ta=C5=9Fkaya?= <47358913+isidentical@users.noreply.github.com> Date: Thu, 23 May 2019 04:13:16 +0300 Subject: [PATCH] bpo-27737: Allow whitespace only headers encoding (#13478) --- Lib/email/header.py | 2 +- Lib/test/test_email/test_email.py | 3 +++ .../next/Library/2019-05-22-02-25-31.bpo-27737.7bgKpa.rst | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-05-22-02-25-31.bpo-27737.7bgKpa.rst diff --git a/Lib/email/header.py b/Lib/email/header.py index 7b30a039da1..4ab0032bc66 100644 --- a/Lib/email/header.py +++ b/Lib/email/header.py @@ -431,7 +431,7 @@ class _ValueFormatter: if end_of_line != (' ', ''): self._current_line.push(*end_of_line) if len(self._current_line) > 0: - if self._current_line.is_onlyws(): + if self._current_line.is_onlyws() and self._lines: self._lines[-1] += str(self._current_line) else: self._lines.append(str(self._current_line)) diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index 621754cf753..dfb3be84384 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -4964,6 +4964,9 @@ A very long line that must get split to something other than at the msg['SomeHeader'] = ' value with leading ws' self.assertEqual(str(msg), "SomeHeader: value with leading ws\n\n") + def test_whitespace_header(self): + self.assertEqual(Header(' ').encode(), ' ') + # Test RFC 2231 header parameters (en/de)coding diff --git a/Misc/NEWS.d/next/Library/2019-05-22-02-25-31.bpo-27737.7bgKpa.rst b/Misc/NEWS.d/next/Library/2019-05-22-02-25-31.bpo-27737.7bgKpa.rst new file mode 100644 index 00000000000..02d0ef891be --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-05-22-02-25-31.bpo-27737.7bgKpa.rst @@ -0,0 +1,2 @@ +Allow whitespace only header encoding in ``email.header`` - by Batuhan +Taskaya