From 613b93d91f1d269e7cae89c322e4a7e1da8e7dd2 Mon Sep 17 00:00:00 2001 From: Cheryl Sabella Date: Sat, 13 Oct 2018 18:18:29 -0400 Subject: [PATCH 1/2] bpo-31522: mailbox: pass `from_` parameter from `get_string` to `get_bytes` --- Lib/mailbox.py | 2 +- Lib/test/test_mailbox.py | 28 +++++++++++++++++++ .../2018-10-13-18-16-20.bpo-31522.rWBb43.rst | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2018-10-13-18-16-20.bpo-31522.rWBb43.rst diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 056251dce0ada3c..5b4e86419f11731 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -784,7 +784,7 @@ def get_message(self, key): def get_string(self, key, from_=False): """Return a string representation or raise a KeyError.""" return email.message_from_bytes( - self.get_bytes(key)).as_string(unixfrom=from_) + self.get_bytes(key, from_)).as_string(unixfrom=from_) def get_bytes(self, key, from_=False): """Return a string representation or raise a KeyError.""" diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 3807b95b158263a..a75c8cb00e806e3 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -988,6 +988,34 @@ def assertMailboxEmpty(self): with open(self._path) as f: self.assertEqual(f.readlines(), []) + def test_get_bytes_from(self): + # Get bytes representations of messages with _unixfrom. + unixfrom = 'From foo@bar blah\n' + key0 = self._box.add(unixfrom + self._template % 0) + key1 = self._box.add(unixfrom + _sample_message) + self.assertEqual(self._box.get_bytes(key0, from_=False), + (self._template % 0).encode('ascii')) + self.assertEqual(self._box.get_bytes(key1, from_=False), + _bytes_sample_message) + self.assertEqual(self._box.get_bytes(key0, from_=True), + (unixfrom + self._template % 0).encode('ascii')) + self.assertEqual(self._box.get_bytes(key1, from_=True), + unixfrom.encode('ascii') + _bytes_sample_message) + + def test_get_string_from(self): + # Get string representations of messages with _unixfrom. + unixfrom = 'From foo@bar blah\n' + key0 = self._box.add(unixfrom + self._template % 0) + key1 = self._box.add(unixfrom + _sample_message) + self.assertEqual(self._box.get_string(key0, from_=False), + self._template % 0) + self.assertEqual(self._box.get_string(key1, from_=False).split('\n'), + _sample_message.split('\n')) + self.assertEqual(self._box.get_string(key0, from_=True), + unixfrom + self._template % 0) + self.assertEqual(self._box.get_string(key1, from_=True).split('\n'), + (unixfrom + _sample_message).split('\n')) + def test_add_from_string(self): # Add a string starting with 'From ' to the mailbox key = self._box.add('From foo@bar blah\nFrom: foo\n\n0\n') diff --git a/Misc/NEWS.d/next/Library/2018-10-13-18-16-20.bpo-31522.rWBb43.rst b/Misc/NEWS.d/next/Library/2018-10-13-18-16-20.bpo-31522.rWBb43.rst new file mode 100644 index 000000000000000..83b78b8d2d07c6c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-10-13-18-16-20.bpo-31522.rWBb43.rst @@ -0,0 +1 @@ +In `mailbox._mboxMMDF.get_string`, pass *from_* parameter to ``get_bytes``. From 6e014c0cf961b3a31d616e577d42d622fb317d76 Mon Sep 17 00:00:00 2001 From: Cheryl Sabella Date: Tue, 16 Oct 2018 18:24:54 -0400 Subject: [PATCH 2/2] Update blurb. --- .../next/Library/2018-10-13-18-16-20.bpo-31522.rWBb43.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2018-10-13-18-16-20.bpo-31522.rWBb43.rst b/Misc/NEWS.d/next/Library/2018-10-13-18-16-20.bpo-31522.rWBb43.rst index 83b78b8d2d07c6c..5aea7173fc6d023 100644 --- a/Misc/NEWS.d/next/Library/2018-10-13-18-16-20.bpo-31522.rWBb43.rst +++ b/Misc/NEWS.d/next/Library/2018-10-13-18-16-20.bpo-31522.rWBb43.rst @@ -1 +1 @@ -In `mailbox._mboxMMDF.get_string`, pass *from_* parameter to ``get_bytes``. +The `mailbox.mbox.get_string` function *from_* parameter can now successfully be set to a non-default value.