diff --git a/Lib/mailbox.py b/Lib/mailbox.py index e23ea8c..8c15301 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -273,6 +273,7 @@ class Maildir(Mailbox): else: raise NoSuchMailboxError(self._path) self._toc = {} + self._toc_read = False self._toc_mtimes = {} for subdir in ('cur', 'new'): self._toc_mtimes[subdir] = os.path.getmtime(self._paths[subdir]) @@ -527,7 +528,7 @@ class Maildir(Mailbox): if mtime > self._toc_mtimes[subdir]: refresh = True self._toc_mtimes[subdir] = mtime - if not refresh: + if self._toc_read and not refresh: return # Refresh toc self._toc = {} @@ -540,6 +541,7 @@ class Maildir(Mailbox): uniq = entry.split(self.colon)[0] self._toc[uniq] = os.path.join(subdir, entry) self._last_read = time.time() + self._toc_read = True def _lookup(self, key): """Use TOC to return subpath for given key, or raise a KeyError.""" diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index fb4812d..e0d8da2 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -801,6 +801,25 @@ class TestMaildir(TestMailbox): key1: os.path.join('new', key1), key2: os.path.join('new', key2)}) + def test_refresh_after_safety_period(self): + # Issue #13254: Call _refresh after the "file system safety + # period" of 2 seconds has passed; _toc should still be + # updated because this is the first call to _refresh. + key0 = self._box.add(self._template % 0) + key1 = self._box.add(self._template % 1) + + self._box = self._factory(self._path) + self.assertEqual(self._box._toc, {}) + + # Emulate sleeping. Instead of sleeping for 2 seconds, use the + # skew factor to make _refresh think that the filesystem + # safety period has passed and re-reading the _toc is only + # required if mtimes differ. + self._box._skewfactor = -2 + + self._box._refresh() + self.assertEqual(sorted(self._box._toc.keys()), sorted([key0, key1])) + def test_lookup(self): # Look up message subpaths in the TOC self.assertRaises(KeyError, lambda: self._box._lookup('foo')) @@ -876,6 +895,8 @@ class TestMaildir(TestMailbox): self.assertFalse((perms & 0o111)) # Execute bits should all be off. def test_reread(self): + # Do an initial unconditional refresh + self._box._refresh() # Put the last modified times more than two seconds into the past # (because mtime may have a two second granularity)