Skip to content

gh-66515: Fix locking of an MH mailbox without ".mh_sequences" file #113482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Lib/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,10 +1141,24 @@ def __len__(self):
"""Return a count of messages in the mailbox."""
return len(list(self.iterkeys()))

def _open_mh_sequences_file(self, text):
mode = '' if text else 'b'
kwargs = {'encoding': 'ASCII'} if text else {}
path = os.path.join(self._path, '.mh_sequences')
while True:
try:
return open(path, 'r+' + mode, **kwargs)
except FileNotFoundError:
pass
try:
return open(path, 'x+' + mode, **kwargs)
except FileExistsError:
pass

def lock(self):
"""Lock the mailbox."""
if not self._locked:
self._file = open(os.path.join(self._path, '.mh_sequences'), 'rb+')
self._file = self._open_mh_sequences_file(text=False)
_lock_file(self._file)
self._locked = True

Expand Down Expand Up @@ -1225,8 +1239,9 @@ def get_sequences(self):

def set_sequences(self, sequences):
"""Set sequences using the given name-to-key-list dictionary."""
f = open(os.path.join(self._path, '.mh_sequences'), 'w', encoding='ASCII')
f = self._open_mh_sequences_file(text=True)
try:
os.close(os.open(f.name, os.O_WRONLY | os.O_TRUNC))
for name, keys in sequences.items():
if len(keys) == 0:
continue
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,15 @@ def test_no_dot_mh_sequences_file(self):
box.set_sequences({})
self.assertEqual(os.listdir(path), ['.mh_sequences'])

def test_lock_unlock_no_dot_mh_sequences_file(self):
path = os.path.join(self._path, 'foo.bar')
os.mkdir(path)
box = self._factory(path)
self.assertEqual(os.listdir(path), [])
box.lock()
box.unlock()
self.assertEqual(os.listdir(path), ['.mh_sequences'])

def test_issue2625(self):
msg0 = mailbox.MHMessage(self._template % 0)
msg0.add_sequence('foo')
Expand Down