summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-12-26 09:15:14 (GMT)
committerGitHub <noreply@github.com>2023-12-26 09:15:14 (GMT)
commite87cadc1ce194aae2c076e81298d6e8074f1bb45 (patch)
tree3cdba116c8e0f1aa0b93e41270968e932f4846f3
parentb5dc0f83adda0bde1b176c3a84cc31cd94254e73 (diff)
downloadcpython-e87cadc1ce194aae2c076e81298d6e8074f1bb45.zip
cpython-e87cadc1ce194aae2c076e81298d6e8074f1bb45.tar.gz
cpython-e87cadc1ce194aae2c076e81298d6e8074f1bb45.tar.bz2
gh-66515: mailbox.MH now supports folders withou the ".mh_sequences" file (GH-804)
(for example Claws Mail IMAP-cache folders).
-rw-r--r--Doc/library/mailbox.rst4
-rw-r--r--Lib/mailbox.py9
-rw-r--r--Lib/test/test_mailbox.py13
-rw-r--r--Misc/NEWS.d/next/Library/2023-04-09-21-05-43.gh-issue-66515.0DS8Ya.rst3
4 files changed, 26 insertions, 3 deletions
diff --git a/Doc/library/mailbox.rst b/Doc/library/mailbox.rst
index 3ffd098..fa5b273 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 574c014..0e1d49b 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 caa7eb3..8c350eb 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 0000000..b9c52f3
--- /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.