Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The `mailbox.mbox.get_string` function *from_* parameter can now successfully be set to a non-default value.