]> granicus.if.org Git - python/commitdiff
bpo-31522: mailbox.get_string: pass `from_` parameter to `get_bytes` (#9857)
authorCheryl Sabella <cheryl.sabella@gmail.com>
Fri, 19 Oct 2018 00:21:47 +0000 (20:21 -0400)
committerR. David Murray <rdmurray@bitdance.com>
Fri, 19 Oct 2018 00:21:47 +0000 (20:21 -0400)
This allows *from_* to be successfully set to a non-default value when calling mbox.get_string.

Lib/mailbox.py
Lib/test/test_mailbox.py
Misc/NEWS.d/next/Library/2018-10-13-18-16-20.bpo-31522.rWBb43.rst [new file with mode: 0644]

index 056251dce0ada3c066f9c0b51272be0004de367b..5b4e86419f1173147ab4957af415ebcaef655b20 100644 (file)
@@ -784,7 +784,7 @@ class _mboxMMDF(_singlefileMailbox):
     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."""
index 3807b95b158263a7c162e9da7504dc4cd1f620cf..a75c8cb00e806e35ed1fd9c2763c288cd9e8edd8 100644 (file)
@@ -988,6 +988,34 @@ class _TestMboxMMDF(_TestSingleFile):
         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 (file)
index 0000000..5aea717
--- /dev/null
@@ -0,0 +1 @@
+The `mailbox.mbox.get_string` function *from_* parameter can now successfully be set to a non-default value.