From 3f5eb3e6c75b874db1a83c9d53e824e56fcad02e Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 25 Dec 2023 07:07:51 -0800 Subject: bpo-21360: mailbox.Maildir now ignores files with a leading dot (GH-11833) The maildir format specification states that files with a leading dot should be ignored. Co-authored-by: Serhiy Storchaka --- Doc/library/mailbox.rst | 3 +++ Doc/whatsnew/3.13.rst | 3 +++ Lib/mailbox.py | 2 ++ Lib/test/test_mailbox.py | 14 ++++++++++++++ .../next/Library/2019-02-12-16-12-54.bpo-21360.gkSSfx.rst | 1 + 5 files changed, 23 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-02-12-16-12-54.bpo-21360.gkSSfx.rst diff --git a/Doc/library/mailbox.rst b/Doc/library/mailbox.rst index c98496d..3ffd098 100644 --- a/Doc/library/mailbox.rst +++ b/Doc/library/mailbox.rst @@ -364,6 +364,9 @@ Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF. The :attr:`!colon` attribute may also be set on a per-instance basis. + .. versionchanged:: 3.13 + :class:`Maildir` now ignores files with a leading dot. + :class:`!Maildir` instances have all of the methods of :class:`Mailbox` in addition to the following: diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index b4cd106..c905cdd 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -1163,6 +1163,9 @@ Changes in the Python API other "private" attributes. (See :gh:`112826`.) +* :class:`mailbox.Maildir` now ignores files with a leading dot. + (Contributed by Zackery Spytz in :gh:`65559`.) + Build Changes ============= diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 36afade..574c014 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -590,6 +590,8 @@ class Maildir(Mailbox): for subdir in self._toc_mtimes: path = self._paths[subdir] for entry in os.listdir(path): + if entry.startswith('.'): + continue p = os.path.join(path, entry) if os.path.isdir(p): continue diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 23fcbfa..caa7eb3 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -681,6 +681,20 @@ class TestMaildir(TestMailbox, unittest.TestCase): self._box = mailbox.Maildir(self._path) self._check_basics() + def test_filename_leading_dot(self): + self.tearDown() + for subdir in '', 'tmp', 'new', 'cur': + os.mkdir(os.path.normpath(os.path.join(self._path, subdir))) + for subdir in 'tmp', 'new', 'cur': + fname = os.path.join(self._path, subdir, '.foo' + subdir) + with open(fname, 'wb') as f: + f.write(b"@") + self._box = mailbox.Maildir(self._path) + self.assertNotIn('.footmp', self._box) + self.assertNotIn('.foonew', self._box) + self.assertNotIn('.foocur', self._box) + self.assertEqual(list(self._box.iterkeys()), []) + def _check_basics(self, factory=None): # (Used by test_open_new() and test_open_existing().) self.assertEqual(self._box._path, os.path.abspath(self._path)) diff --git a/Misc/NEWS.d/next/Library/2019-02-12-16-12-54.bpo-21360.gkSSfx.rst b/Misc/NEWS.d/next/Library/2019-02-12-16-12-54.bpo-21360.gkSSfx.rst new file mode 100644 index 0000000..bc32b9f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-02-12-16-12-54.bpo-21360.gkSSfx.rst @@ -0,0 +1 @@ +:class:`mailbox.Maildir` now ignores files with a leading dot. -- cgit v0.12