Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c481b6

Browse files
committed
Fix Maildir initialization so that maildir contents are read correctly.
Closes #13254.
2 parents f16a350 + 8c482ee commit 8c481b6

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

Lib/mailbox.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,9 @@ def __init__(self, dirname, factory=None, create=True):
273273
else:
274274
raise NoSuchMailboxError(self._path)
275275
self._toc = {}
276-
self._toc_mtimes = {}
277-
for subdir in ('cur', 'new'):
278-
self._toc_mtimes[subdir] = os.path.getmtime(self._paths[subdir])
279-
self._last_read = time.time() # Records last time we read cur/new
280-
self._skewfactor = 0.1 # Adjust if os/fs clocks are skewing
276+
self._toc_mtimes = {'cur': 0, 'new': 0}
277+
self._last_read = 0 # Records last time we read cur/new
278+
self._skewfactor = 0.1 # Adjust if os/fs clocks are skewing
281279

282280
def add(self, message):
283281
"""Add message and return assigned key."""

Lib/test/test_mailbox.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,25 @@ def test_refresh(self):
801801
key1: os.path.join('new', key1),
802802
key2: os.path.join('new', key2)})
803803

804+
def test_refresh_after_safety_period(self):
805+
# Issue #13254: Call _refresh after the "file system safety
806+
# period" of 2 seconds has passed; _toc should still be
807+
# updated because this is the first call to _refresh.
808+
key0 = self._box.add(self._template % 0)
809+
key1 = self._box.add(self._template % 1)
810+
811+
self._box = self._factory(self._path)
812+
self.assertEqual(self._box._toc, {})
813+
814+
# Emulate sleeping. Instead of sleeping for 2 seconds, use the
815+
# skew factor to make _refresh think that the filesystem
816+
# safety period has passed and re-reading the _toc is only
817+
# required if mtimes differ.
818+
self._box._skewfactor = -2
819+
820+
self._box._refresh()
821+
self.assertEqual(sorted(self._box._toc.keys()), sorted([key0, key1]))
822+
804823
def test_lookup(self):
805824
# Look up message subpaths in the TOC
806825
self.assertRaises(KeyError, lambda: self._box._lookup('foo'))
@@ -876,6 +895,8 @@ def test_folder_file_perms(self):
876895
self.assertFalse((perms & 0o111)) # Execute bits should all be off.
877896

878897
def test_reread(self):
898+
# Do an initial unconditional refresh
899+
self._box._refresh()
879900

880901
# Put the last modified times more than two seconds into the past
881902
# (because mtime may have a two second granularity)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ Core and Builtins
354354
Library
355355
-------
356356

357+
- Issue #13254: Fix Maildir initialization so that maildir contents
358+
are read correctly.
359+
357360
- Issue #3067: locale.setlocale() now raises TypeError if the second
358361
argument is an invalid iterable. Initial patch by Jyrki Pulliainen.
359362

0 commit comments

Comments
 (0)