From a70c177b0c4cf17128bf16f59f005fc24efa9abd Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Tue, 12 Feb 2019 16:13:59 -0700 Subject: [PATCH 1/3] bpo-21360: mailbox.Maildir now ignores files with a leading dot The maildir format specification states that files with a leading dot should be ignored. --- Lib/mailbox.py | 2 ++ Lib/test/test_mailbox.py | 9 +++++++++ .../Library/2019-02-12-16-12-54.bpo-21360.gkSSfx.rst | 1 + 3 files changed, 12 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-02-12-16-12-54.bpo-21360.gkSSfx.rst diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 5b4e86419f1173..5850f19fe86ac8 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -537,6 +537,8 @@ def _refresh(self): 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 a75c8cb00e806e..f0c458f361c09d 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -672,6 +672,15 @@ def test_initialize_existing(self): 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))) + with open(os.path.join(self._path, 'new', '.foo'), 'w') as f: + f.write("@") + self._box = mailbox.Maildir(self._path) + self.assertNotIn('.foo', self._box) + 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 00000000000000..bc32b9fe4199f9 --- /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. From bf695fa29e2db41060020ef4fbade01cf4a35df5 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 14 Dec 2023 21:49:26 +0200 Subject: [PATCH 2/3] Test in multiple subdirectories. --- Lib/test/test_mailbox.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 95e63f06c62183..caa7eb3d829c68 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -685,10 +685,15 @@ 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))) - with open(os.path.join(self._path, 'new', '.foo'), 'w') as f: - f.write("@") + 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('.foo', self._box) + 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().) From 6e137a36ce240de9b23c17d4271b6640ff0c5c10 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 14 Dec 2023 22:01:57 +0200 Subject: [PATCH 3/3] Document the change. --- Doc/library/mailbox.rst | 3 +++ Doc/whatsnew/3.13.rst | 3 +++ 2 files changed, 6 insertions(+) diff --git a/Doc/library/mailbox.rst b/Doc/library/mailbox.rst index fd60d163378f07..16ec3301a7bfb5 100644 --- a/Doc/library/mailbox.rst +++ b/Doc/library/mailbox.rst @@ -348,6 +348,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 e22257853d8333..5e0060df7e4975 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -1104,6 +1104,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 =============