From 45ae78c8d5407720927642211b7620766efaee5f Mon Sep 17 00:00:00 2001 From: Matthieu Caneill Date: Sun, 23 Jul 2023 12:28:43 +0200 Subject: [PATCH 1/4] Set unixfrom envelope in mailbox._mboxMMDF --- Lib/mailbox.py | 1 + Lib/test/test_mailbox.py | 2 ++ .../next/Library/2023-07-23-12-28-26.gh-issue-75705.aB2-Ww.rst | 1 + 3 files changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-07-23-12-28-26.gh-issue-75705.aB2-Ww.rst diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 59834a2b3b5243..e6d00b69fcb32b 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -782,6 +782,7 @@ def get_message(self, key): string = self._file.read(stop - self._file.tell()) msg = self._message_factory(string.replace(linesep, b'\n')) msg.set_from(from_line[5:].decode('ascii')) + msg.set_unixfrom(from_line.decode('ascii')) return msg def get_string(self, key, from_=False): diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 4977a9369ddf88..8114e26123b673 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -1027,12 +1027,14 @@ 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') self.assertEqual(self._box[key].get_from(), 'foo@bar blah') + self.assertEqual(self._box[key].get_unixfrom(), 'From foo@bar blah') self.assertEqual(self._box[key].get_payload(), '0\n') def test_add_from_bytes(self): # Add a byte string starting with 'From ' to the mailbox key = self._box.add(b'From foo@bar blah\nFrom: foo\n\n0\n') self.assertEqual(self._box[key].get_from(), 'foo@bar blah') + self.assertEqual(self._box[key].get_unixfrom(), 'From foo@bar blah') self.assertEqual(self._box[key].get_payload(), '0\n') def test_add_mbox_or_mmdf_message(self): diff --git a/Misc/NEWS.d/next/Library/2023-07-23-12-28-26.gh-issue-75705.aB2-Ww.rst b/Misc/NEWS.d/next/Library/2023-07-23-12-28-26.gh-issue-75705.aB2-Ww.rst new file mode 100644 index 00000000000000..d60d9db4ff053a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-23-12-28-26.gh-issue-75705.aB2-Ww.rst @@ -0,0 +1 @@ +Set unixfrom envelope in mailbox._mboxMMDF. From 14821b852924358cb7af55ea74e2bdba198eeaa9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 1 Feb 2024 14:49:21 +0200 Subject: [PATCH 2/4] Tiny optimization. --- Lib/mailbox.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 4267b45bcdc1f2..746811bd559412 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -830,11 +830,11 @@ def get_message(self, key): """Return a Message representation or raise a KeyError.""" start, stop = self._lookup(key) self._file.seek(start) - from_line = self._file.readline().replace(linesep, b'') + from_line = self._file.readline().replace(linesep, b'').decode('ascii') string = self._file.read(stop - self._file.tell()) msg = self._message_factory(string.replace(linesep, b'\n')) - msg.set_from(from_line[5:].decode('ascii')) - msg.set_unixfrom(from_line.decode('ascii')) + msg.set_unixfrom(from_line) + msg.set_from(from_line[5:]) return msg def get_string(self, key, from_=False): From 0982e7ad7fdceef75ace17956251662c4eecec1c Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 1 Feb 2024 14:58:15 +0200 Subject: [PATCH 3/4] Add more tests for get_unixfrom(). --- Lib/test/test_mailbox.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 7322bd464d928b..c52c014185bec7 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -1669,18 +1669,23 @@ def test_initialize_with_unixfrom(self): msg = mailbox.Message(_sample_message) msg.set_unixfrom('From foo@bar blah') msg = mailbox.mboxMessage(msg) - self.assertEqual(msg.get_from(), 'foo@bar blah', msg.get_from()) + self.assertEqual(msg.get_from(), 'foo@bar blah') + self.assertEqual(msg.get_unixfrom(), 'From foo@bar blah') def test_from(self): # Get and set "From " line msg = mailbox.mboxMessage(_sample_message) self._check_from(msg) + self.assertIsNone(msg.get_unixfrom()) msg.set_from('foo bar') self.assertEqual(msg.get_from(), 'foo bar') + self.assertIsNone(msg.get_unixfrom()) msg.set_from('foo@bar', True) self._check_from(msg, 'foo@bar') + self.assertIsNone(msg.get_unixfrom()) msg.set_from('blah@temp', time.localtime()) self._check_from(msg, 'blah@temp') + self.assertIsNone(msg.get_unixfrom()) def test_flags(self): # Use get_flags(), set_flags(), add_flag(), remove_flag() @@ -1868,6 +1873,7 @@ def test_maildir_to_mboxmmdf(self): self.assertEqual(msg.get_flags(), result) self.assertEqual(msg.get_from(), 'MAILER-DAEMON %s' % time.asctime(time.gmtime(0.0))) + self.assertIsNone(msg.get_unixfrom()) msg_maildir.set_subdir('cur') self.assertEqual(class_(msg_maildir).get_flags(), 'RODFA') @@ -1916,10 +1922,12 @@ def test_mboxmmdf_to_mboxmmdf(self): msg_mboxMMDF = class_(_sample_message) msg_mboxMMDF.set_flags('RODFA') msg_mboxMMDF.set_from('foo@bar') + self.assertIsNone(msg_mboxMMDF.get_unixfrom()) for class2_ in (mailbox.mboxMessage, mailbox.MMDFMessage): msg2 = class2_(msg_mboxMMDF) self.assertEqual(msg2.get_flags(), 'RODFA') self.assertEqual(msg2.get_from(), 'foo@bar') + self.assertIsNone(msg2.get_unixfrom()) def test_mboxmmdf_to_mh(self): # Convert mboxMessage and MMDFMessage to MHMessage From 9eccf7da70da59e81805b9011a56ec670105fb7d Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 1 Feb 2024 15:01:25 +0200 Subject: [PATCH 4/4] Update the NEWS entry. --- .../next/Library/2023-07-23-12-28-26.gh-issue-75705.aB2-Ww.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-07-23-12-28-26.gh-issue-75705.aB2-Ww.rst b/Misc/NEWS.d/next/Library/2023-07-23-12-28-26.gh-issue-75705.aB2-Ww.rst index d60d9db4ff053a..272e31d64cfbd9 100644 --- a/Misc/NEWS.d/next/Library/2023-07-23-12-28-26.gh-issue-75705.aB2-Ww.rst +++ b/Misc/NEWS.d/next/Library/2023-07-23-12-28-26.gh-issue-75705.aB2-Ww.rst @@ -1 +1 @@ -Set unixfrom envelope in mailbox._mboxMMDF. +Set unixfrom envelope in :class:`mailbox.mbox` and :class:`mailbox.MMDF`.