Skip to content

Commit d16f012

Browse files
csabellabitdancer
authored andcommitted
bpo-31522: mailbox.get_string: pass from_ parameter to get_bytes (#9857)
This allows *from_* to be successfully set to a non-default value when calling mbox.get_string.
1 parent 5be0024 commit d16f012

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

Lib/mailbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ def get_message(self, key):
784784
def get_string(self, key, from_=False):
785785
"""Return a string representation or raise a KeyError."""
786786
return email.message_from_bytes(
787-
self.get_bytes(key)).as_string(unixfrom=from_)
787+
self.get_bytes(key, from_)).as_string(unixfrom=from_)
788788

789789
def get_bytes(self, key, from_=False):
790790
"""Return a string representation or raise a KeyError."""

Lib/test/test_mailbox.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,34 @@ def assertMailboxEmpty(self):
988988
with open(self._path) as f:
989989
self.assertEqual(f.readlines(), [])
990990

991+
def test_get_bytes_from(self):
992+
# Get bytes representations of messages with _unixfrom.
993+
unixfrom = 'From foo@bar blah\n'
994+
key0 = self._box.add(unixfrom + self._template % 0)
995+
key1 = self._box.add(unixfrom + _sample_message)
996+
self.assertEqual(self._box.get_bytes(key0, from_=False),
997+
(self._template % 0).encode('ascii'))
998+
self.assertEqual(self._box.get_bytes(key1, from_=False),
999+
_bytes_sample_message)
1000+
self.assertEqual(self._box.get_bytes(key0, from_=True),
1001+
(unixfrom + self._template % 0).encode('ascii'))
1002+
self.assertEqual(self._box.get_bytes(key1, from_=True),
1003+
unixfrom.encode('ascii') + _bytes_sample_message)
1004+
1005+
def test_get_string_from(self):
1006+
# Get string representations of messages with _unixfrom.
1007+
unixfrom = 'From foo@bar blah\n'
1008+
key0 = self._box.add(unixfrom + self._template % 0)
1009+
key1 = self._box.add(unixfrom + _sample_message)
1010+
self.assertEqual(self._box.get_string(key0, from_=False),
1011+
self._template % 0)
1012+
self.assertEqual(self._box.get_string(key1, from_=False).split('\n'),
1013+
_sample_message.split('\n'))
1014+
self.assertEqual(self._box.get_string(key0, from_=True),
1015+
unixfrom + self._template % 0)
1016+
self.assertEqual(self._box.get_string(key1, from_=True).split('\n'),
1017+
(unixfrom + _sample_message).split('\n'))
1018+
9911019
def test_add_from_string(self):
9921020
# Add a string starting with 'From ' to the mailbox
9931021
key = self._box.add('From foo@bar blah\nFrom: foo\n\n0\n')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The `mailbox.mbox.get_string` function *from_* parameter can now successfully be set to a non-default value.

0 commit comments

Comments
 (0)