From e87cadc1ce194aae2c076e81298d6e8074f1bb45 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 26 Dec 2023 11:15:14 +0200 Subject: [PATCH] gh-66515: mailbox.MH now supports folders withou the ".mh_sequences" file (GH-804) (for example Claws Mail IMAP-cache folders). --- Doc/library/mailbox.rst | 4 ++++ Lib/mailbox.py | 9 ++++++--- Lib/test/test_mailbox.py | 13 +++++++++++++ .../2023-04-09-21-05-43.gh-issue-66515.0DS8Ya.rst | 3 +++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-04-09-21-05-43.gh-issue-66515.0DS8Ya.rst diff --git a/Doc/library/mailbox.rst b/Doc/library/mailbox.rst index 3ffd0981a4a..fa5b273093f 100644 --- a/Doc/library/mailbox.rst +++ b/Doc/library/mailbox.rst @@ -644,6 +644,10 @@ Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF. :class:`!MH` instances have all of the methods of :class:`Mailbox` in addition to the following: + .. versionchanged:: 3.13 + + Supported folders that don't contain a :file:`.mh_sequences` file. + .. method:: list_folders() diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 574c01475de..0e1d49b399d 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -1198,7 +1198,11 @@ class MH(Mailbox): def get_sequences(self): """Return a name-to-key-list dictionary to define each sequence.""" results = {} - with open(os.path.join(self._path, '.mh_sequences'), 'r', encoding='ASCII') as f: + try: + f = open(os.path.join(self._path, '.mh_sequences'), 'r', encoding='ASCII') + except FileNotFoundError: + return results + with f: all_keys = set(self.keys()) for line in f: try: @@ -1221,9 +1225,8 @@ class MH(Mailbox): def set_sequences(self, sequences): """Set sequences using the given name-to-key-list dictionary.""" - f = open(os.path.join(self._path, '.mh_sequences'), 'r+', encoding='ASCII') + f = open(os.path.join(self._path, '.mh_sequences'), 'w', encoding='ASCII') try: - os.close(os.open(f.name, os.O_WRONLY | os.O_TRUNC)) for name, keys in sequences.items(): if len(keys) == 0: continue diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index caa7eb3d829..8c350eb02cc 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -1347,6 +1347,19 @@ class TestMH(TestMailbox, unittest.TestCase): self._box.remove(key1) self.assertEqual(self._box.get_sequences(), {'flagged':[key0]}) + self._box.set_sequences({'foo':[key0]}) + self.assertEqual(self._box.get_sequences(), {'foo':[key0]}) + + def test_no_dot_mh_sequences_file(self): + path = os.path.join(self._path, 'foo.bar') + os.mkdir(path) + box = self._factory(path) + self.assertEqual(os.listdir(path), []) + self.assertEqual(box.get_sequences(), {}) + self.assertEqual(os.listdir(path), []) + box.set_sequences({}) + self.assertEqual(os.listdir(path), ['.mh_sequences']) + def test_issue2625(self): msg0 = mailbox.MHMessage(self._template % 0) msg0.add_sequence('foo') diff --git a/Misc/NEWS.d/next/Library/2023-04-09-21-05-43.gh-issue-66515.0DS8Ya.rst b/Misc/NEWS.d/next/Library/2023-04-09-21-05-43.gh-issue-66515.0DS8Ya.rst new file mode 100644 index 00000000000..b9c52f3b8db --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-09-21-05-43.gh-issue-66515.0DS8Ya.rst @@ -0,0 +1,3 @@ +:class:`mailbox.MH` now supports folders that do not contain a +``.mh_sequences`` file (e.g. Claws Mail IMAP-cache folders). Patch by Serhiy +Storchaka.