Skip to content
Open
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
6 changes: 1 addition & 5 deletions Lib/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,7 @@ def flush(self):
os.chown(new_file.name, info.st_uid, info.st_gid)
except (AttributeError, OSError):
pass
try:
os.rename(new_file.name, self._path)
except FileExistsError:
os.remove(self._path)
os.rename(new_file.name, self._path)
os.replace(new_file.name, self._path)
self._file = open(self._path, 'rb+')
self._toc = new_toc
self._pending = False
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_mailbox.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import errno
import os
import sys
import time
Expand All @@ -14,6 +15,7 @@
from test.support import requires_root_user
from test.support import socket_helper
import unittest
from unittest import mock
import textwrap
import mailbox
import glob
Expand Down Expand Up @@ -1284,6 +1286,26 @@ class TestMbox(_TestMboxMMDF, unittest.TestCase):

_factory = lambda self, path, factory=None: mailbox.mbox(path, factory)

def test_flush_replacement_failure(self):
box = self._box
box.add(self._template % 0)
key = box.add(self._template % 1)
box.flush()
box.remove(key)

error = OSError(errno.EIO, 'injected replacement failure')
with mock.patch.object(mailbox.os, 'replace', side_effect=error):
with self.assertRaises(OSError) as cm:
box.flush()
self.assertEqual(cm.exception.errno, errno.EIO)
self.assertTrue(os.path.exists(self._path))

self._box = mailbox.mbox(self._path)
self.assertEqual(
[message.get_payload() for message in self._box.values()],
['0\n', '1\n'],
)

@unittest.skipUnless(hasattr(os, 'umask'), 'test needs os.umask()')
def test_file_perms(self):
# From bug #3228, we want to verify that the mailbox file isn't executable,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Preserve :class:`mailbox.mbox`, :class:`mailbox.MMDF`, and
:class:`mailbox.Babyl` files when replacing a rewritten mailbox fails.
Loading