From 55f16702a6f867ea589be1ea6389a5365d34dccb Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sun, 16 Apr 2017 18:16:39 +0300 Subject: [PATCH 01/15] Add test for creation of maildir subdirs Make sure that if base maildir directory exists, all mandatory subfolders are also created if mailbox.Maildir initialized with create=True argument --- Lib/test/test_mailbox.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 0995b1e386d00b..d69fbea84858be 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -672,6 +672,11 @@ def test_initialize_existing(self): self._box = mailbox.Maildir(self._path) self._check_basics() + self._delete_recursively(self._path) + os.mkdir(self._path) # maildir exists, but there's no subdirs in it + self._box = self._factory(self._path, factory=None) + self._check_basics() + 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)) From a451dc71a5e9f854ae32e2e83f2c4999a07039ae Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sun, 16 Apr 2017 18:19:31 +0300 Subject: [PATCH 02/15] Ensure maildir's subdirs exist upon initialization --- Lib/mailbox.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 5b4e86419f1173..9299c24b7a4533 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -273,14 +273,18 @@ def __init__(self, dirname, factory=None, create=True): 'tmp': os.path.join(self._path, 'tmp'), 'new': os.path.join(self._path, 'new'), 'cur': os.path.join(self._path, 'cur'), - } - if not os.path.exists(self._path): - if create: - os.mkdir(self._path, 0o700) - for path in self._paths.values(): - os.mkdir(path, 0o700) - else: - raise NoSuchMailboxError(self._path) + } + + def _mkdir_or_error(path): + if not os.path.exists(path): + if not create: + raise NoSuchMailboxError(path) + os.mkdir(path, 0o700) + + _mkdir_or_error(self._path) + for path in self._paths.values(): + _mkdir_or_error(path) + self._toc = {} self._toc_mtimes = {'cur': 0, 'new': 0} self._last_read = 0 # Records last time we read cur/new From f4de9065f2fcc2d58a5b62db9de4edffe73a6deb Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sun, 16 Apr 2017 19:18:00 +0300 Subject: [PATCH 03/15] Add Sviatoslav Sydorenko to Misc/ACKS --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index c0119992cffe30..4bd7e404a14589 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1612,6 +1612,7 @@ Kalle Svensson Andrew Svetlov Paul Swartz Al Sweigart +Sviatoslav Sydorenko Thenault Sylvain Péter Szabó John Szakmeister From 26f16a228b2beeb73ae9d9262514a7aeb5d17e7e Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Mon, 17 Apr 2017 23:57:11 +0300 Subject: [PATCH 04/15] Add test for raising error if maildir is invalid --- Lib/test/test_mailbox.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index d69fbea84858be..d76af259c28bcc 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -587,7 +587,8 @@ def test_notimplemented(self): class TestMaildir(TestMailbox, unittest.TestCase): - _factory = lambda self, path, factory=None: mailbox.Maildir(path, factory) + def _factory(self, path, factory=None, create=True): + return mailbox.Maildir(dirname=path, factory=factory, create=create) def setUp(self): TestMailbox.setUp(self) @@ -677,6 +678,32 @@ def test_initialize_existing(self): self._box = self._factory(self._path, factory=None) self._check_basics() + def test_fail_initialize_if_no_folder(self): + """ + Check that trying to initialize mailbox with create=False raises + mailbox.NoSuchMailboxError if any of required folders is missing. + """ + subdirs = '', 'tmp', 'new', 'cur' + def _create_subfolders_except(dir_): + self._delete_recursively(self._path) + if not dir_: + return + for _subdir in subdirs: + if dir_ == _subdir: + continue + subpath = os.path.normpath(os.path.join(self._path, _subdir)) + os.mkdir(subpath) + + # Initialize a non-existent mailbox + self.tearDown() + for subdir in subdirs: + with self.subTest(subdir=subdir): + subpath = os.path.normpath(os.path.join(self._path, subdir)) + _create_subfolders_except(subdir) + with self.assertRaisesRegexp( + mailbox.NoSuchMailboxError, subpath): + self._factory(subpath, factory=None, create=False) + 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)) From 9a428673b8b2fe94902b4a729925efd6f84ecbe4 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 18 Apr 2017 00:23:33 +0300 Subject: [PATCH 05/15] Fix mailbox instantiation in test * Bonus: use new assertRaisesRegex instead of deprecated name --- Lib/test/test_mailbox.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index d76af259c28bcc..7262ca5f93fc59 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -700,9 +700,9 @@ def _create_subfolders_except(dir_): with self.subTest(subdir=subdir): subpath = os.path.normpath(os.path.join(self._path, subdir)) _create_subfolders_except(subdir) - with self.assertRaisesRegexp( + with self.assertRaisesRegex( mailbox.NoSuchMailboxError, subpath): - self._factory(subpath, factory=None, create=False) + self._factory(self._path, factory=None, create=False) def _check_basics(self, factory=None): # (Used by test_open_new() and test_open_existing().) From 3059564a4bcd624534220fcf1104f7572cb66c92 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Wed, 19 Apr 2017 00:40:58 +0300 Subject: [PATCH 06/15] Make checked subpath absolute in test --- Lib/test/test_mailbox.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 7262ca5f93fc59..799f60550c0a7c 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -699,6 +699,7 @@ def _create_subfolders_except(dir_): for subdir in subdirs: with self.subTest(subdir=subdir): subpath = os.path.normpath(os.path.join(self._path, subdir)) + subpath = os.path.abspath(subpath) _create_subfolders_except(subdir) with self.assertRaisesRegex( mailbox.NoSuchMailboxError, subpath): From aff2caa7e04331fa8459504cd7534cb0f7961690 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Thu, 20 Apr 2017 00:31:32 +0300 Subject: [PATCH 07/15] Turn assertRaisesRegex into assertRaises --- Lib/test/test_mailbox.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 799f60550c0a7c..47a5d5446df05f 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -701,9 +701,9 @@ def _create_subfolders_except(dir_): subpath = os.path.normpath(os.path.join(self._path, subdir)) subpath = os.path.abspath(subpath) _create_subfolders_except(subdir) - with self.assertRaisesRegex( - mailbox.NoSuchMailboxError, subpath): + with self.assertRaises(mailbox.NoSuchMailboxError) as error: self._factory(self._path, factory=None, create=False) + self.assertEqual(str(error.exception), subpath) def _check_basics(self, factory=None): # (Used by test_open_new() and test_open_existing().) From 07c2e76f34e58bf1aa30d95d46245739b59f5497 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 13 Jul 2019 14:30:55 +0200 Subject: [PATCH 08/15] Revert "Turn assertRaisesRegex into assertRaises" This reverts commit aff2caa7e04331fa8459504cd7534cb0f7961690. --- Lib/test/test_mailbox.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 47a5d5446df05f..799f60550c0a7c 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -701,9 +701,9 @@ def _create_subfolders_except(dir_): subpath = os.path.normpath(os.path.join(self._path, subdir)) subpath = os.path.abspath(subpath) _create_subfolders_except(subdir) - with self.assertRaises(mailbox.NoSuchMailboxError) as error: + with self.assertRaisesRegex( + mailbox.NoSuchMailboxError, subpath): self._factory(self._path, factory=None, create=False) - self.assertEqual(str(error.exception), subpath) def _check_basics(self, factory=None): # (Used by test_open_new() and test_open_existing().) From d9f791d29b454504c8bbaf40eface0e64de1bf62 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 13 Jul 2019 14:30:58 +0200 Subject: [PATCH 09/15] Revert "Make checked subpath absolute in test" This reverts commit 3059564a4bcd624534220fcf1104f7572cb66c92. --- Lib/test/test_mailbox.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 799f60550c0a7c..7262ca5f93fc59 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -699,7 +699,6 @@ def _create_subfolders_except(dir_): for subdir in subdirs: with self.subTest(subdir=subdir): subpath = os.path.normpath(os.path.join(self._path, subdir)) - subpath = os.path.abspath(subpath) _create_subfolders_except(subdir) with self.assertRaisesRegex( mailbox.NoSuchMailboxError, subpath): From e0d4f8c92ae69137d5fef4ff7fade5ecc108c63b Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 13 Jul 2019 14:30:59 +0200 Subject: [PATCH 10/15] Revert "Fix mailbox instantiation in test" This reverts commit 9a428673b8b2fe94902b4a729925efd6f84ecbe4. --- Lib/test/test_mailbox.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 7262ca5f93fc59..d76af259c28bcc 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -700,9 +700,9 @@ def _create_subfolders_except(dir_): with self.subTest(subdir=subdir): subpath = os.path.normpath(os.path.join(self._path, subdir)) _create_subfolders_except(subdir) - with self.assertRaisesRegex( + with self.assertRaisesRegexp( mailbox.NoSuchMailboxError, subpath): - self._factory(self._path, factory=None, create=False) + self._factory(subpath, factory=None, create=False) def _check_basics(self, factory=None): # (Used by test_open_new() and test_open_existing().) From 48dd6d82b54e87a0be0222449917796d1bb5868f Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 13 Jul 2019 14:31:01 +0200 Subject: [PATCH 11/15] Revert "Add test for raising error if maildir is invalid" This reverts commit 26f16a228b2beeb73ae9d9262514a7aeb5d17e7e. --- Lib/test/test_mailbox.py | 29 +---------------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index d76af259c28bcc..d69fbea84858be 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -587,8 +587,7 @@ def test_notimplemented(self): class TestMaildir(TestMailbox, unittest.TestCase): - def _factory(self, path, factory=None, create=True): - return mailbox.Maildir(dirname=path, factory=factory, create=create) + _factory = lambda self, path, factory=None: mailbox.Maildir(path, factory) def setUp(self): TestMailbox.setUp(self) @@ -678,32 +677,6 @@ def test_initialize_existing(self): self._box = self._factory(self._path, factory=None) self._check_basics() - def test_fail_initialize_if_no_folder(self): - """ - Check that trying to initialize mailbox with create=False raises - mailbox.NoSuchMailboxError if any of required folders is missing. - """ - subdirs = '', 'tmp', 'new', 'cur' - def _create_subfolders_except(dir_): - self._delete_recursively(self._path) - if not dir_: - return - for _subdir in subdirs: - if dir_ == _subdir: - continue - subpath = os.path.normpath(os.path.join(self._path, _subdir)) - os.mkdir(subpath) - - # Initialize a non-existent mailbox - self.tearDown() - for subdir in subdirs: - with self.subTest(subdir=subdir): - subpath = os.path.normpath(os.path.join(self._path, subdir)) - _create_subfolders_except(subdir) - with self.assertRaisesRegexp( - mailbox.NoSuchMailboxError, subpath): - self._factory(subpath, factory=None, create=False) - 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)) From 3ff5052d5553b298616f6ea16853f3ca318e85db Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 13 Jul 2019 14:31:02 +0200 Subject: [PATCH 12/15] Revert "Ensure maildir's subdirs exist upon initialization" This reverts commit a451dc71a5e9f854ae32e2e83f2c4999a07039ae. --- Lib/mailbox.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 9299c24b7a4533..5b4e86419f1173 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -273,18 +273,14 @@ def __init__(self, dirname, factory=None, create=True): 'tmp': os.path.join(self._path, 'tmp'), 'new': os.path.join(self._path, 'new'), 'cur': os.path.join(self._path, 'cur'), - } - - def _mkdir_or_error(path): - if not os.path.exists(path): - if not create: - raise NoSuchMailboxError(path) - os.mkdir(path, 0o700) - - _mkdir_or_error(self._path) - for path in self._paths.values(): - _mkdir_or_error(path) - + } + if not os.path.exists(self._path): + if create: + os.mkdir(self._path, 0o700) + for path in self._paths.values(): + os.mkdir(path, 0o700) + else: + raise NoSuchMailboxError(self._path) self._toc = {} self._toc_mtimes = {'cur': 0, 'new': 0} self._last_read = 0 # Records last time we read cur/new From 01042607e8cbd00490f70685832769aec22e87a1 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 13 Jul 2019 14:31:04 +0200 Subject: [PATCH 13/15] Revert "Add test for creation of maildir subdirs" This reverts commit 55f16702a6f867ea589be1ea6389a5365d34dccb. --- Lib/test/test_mailbox.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index d69fbea84858be..0995b1e386d00b 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -672,11 +672,6 @@ def test_initialize_existing(self): self._box = mailbox.Maildir(self._path) self._check_basics() - self._delete_recursively(self._path) - os.mkdir(self._path) # maildir exists, but there's no subdirs in it - self._box = self._factory(self._path, factory=None) - self._check_basics() - 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)) From f7aa474f3c39eedc13cd49b86838ffedba544b83 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 13 Jul 2019 14:35:23 +0200 Subject: [PATCH 14/15] =?UTF-8?q?=F0=9F=93=9D=20Document=20Mailbox(create?= =?UTF-8?q?=3DTrue)=20existing=20behavior?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Doc/library/mailbox.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/library/mailbox.rst b/Doc/library/mailbox.rst index d901ad2cc1c48e..f82a3b200deb7c 100644 --- a/Doc/library/mailbox.rst +++ b/Doc/library/mailbox.rst @@ -308,6 +308,9 @@ Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF. representation. If *create* is ``True``, the mailbox is created if it does not exist. + If *create* is ``True`` and the *dirname* path exists, it will be treated as + an existing maildir without attempting to verify its directory layout. + It is for historical reasons that *dirname* is named as such rather than *path*. Maildir is a directory-based mailbox format invented for the qmail mail From 71282dacef39e9d2af859de6d6584efd80575259 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 13 Jul 2019 12:43:04 +0000 Subject: [PATCH 15/15] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Documentation/2019-07-13-12-43-01.bpo-30088.CIcBjy.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Documentation/2019-07-13-12-43-01.bpo-30088.CIcBjy.rst diff --git a/Misc/NEWS.d/next/Documentation/2019-07-13-12-43-01.bpo-30088.CIcBjy.rst b/Misc/NEWS.d/next/Documentation/2019-07-13-12-43-01.bpo-30088.CIcBjy.rst new file mode 100644 index 00000000000000..a39fe3fbbcb6e8 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2019-07-13-12-43-01.bpo-30088.CIcBjy.rst @@ -0,0 +1 @@ +Documented that :class:`mailbox.Maildir` constructor doesn't attempt to verify the maildir folder layout correctness. Patch by Sviatoslav Sydorenko. \ No newline at end of file