]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-66515: mailbox.MH now supports folders withou the ".mh_sequences" file (GH-804)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 26 Dec 2023 09:15:14 +0000 (11:15 +0200)
committerGitHub <noreply@github.com>
Tue, 26 Dec 2023 09:15:14 +0000 (11:15 +0200)
(for example Claws Mail IMAP-cache folders).

Doc/library/mailbox.rst
Lib/mailbox.py
Lib/test/test_mailbox.py
Misc/NEWS.d/next/Library/2023-04-09-21-05-43.gh-issue-66515.0DS8Ya.rst [new file with mode: 0644]

index 3ffd0981a4a4f2bb5ffcdbd4a466773512fe0165..fa5b273093f583f31378e58ec84f7befa9da4e51 100644 (file)
@@ -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()
 
index 574c01475dece60a1ae3705a5b61759fd72f826b..0e1d49b399d0776aaa398df91662b7863256aee5 100644 (file)
@@ -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
index caa7eb3d829c68254853dda6a9a463d24551c118..8c350eb02ccc179ffc239a4c18e203c936d90eb0 100644 (file)
@@ -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 (file)
index 0000000..b9c52f3
--- /dev/null
@@ -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.