summaryrefslogtreecommitdiffstats
path: root/Lib/mailbox.py
diff options
context:
space:
mode:
authorR. David Murray <rdmurray@bitdance.com>2011-02-11 22:47:17 (GMT)
committerR. David Murray <rdmurray@bitdance.com>2011-02-11 22:47:17 (GMT)
commitfc14114ca936829a74556bdbdaa494bf55103bb1 (patch)
tree3fdcaa5c2b62cf8c894b6f60cf399245d9ffa863 /Lib/mailbox.py
parent8b24aac9363bfe44081415f6cff3bc65918ad321 (diff)
downloadcpython-fc14114ca936829a74556bdbdaa494bf55103bb1.zip
cpython-fc14114ca936829a74556bdbdaa494bf55103bb1.tar.gz
cpython-fc14114ca936829a74556bdbdaa494bf55103bb1.tar.bz2
#11116: roll back on error during add so mailbox isn't left corrupted.
Diffstat (limited to 'Lib/mailbox.py')
-rw-r--r--Lib/mailbox.py24
1 files changed, 18 insertions, 6 deletions
diff --git a/Lib/mailbox.py b/Lib/mailbox.py
index 53f4159..928e795 100644
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -277,8 +277,11 @@ class Maildir(Mailbox):
tmp_file = self._create_tmp()
try:
self._dump_message(message, tmp_file)
- finally:
- _sync_close(tmp_file)
+ except BaseException:
+ tmp_file.close()
+ os.remove(tmp_file.name)
+ raise
+ _sync_close(tmp_file)
if isinstance(message, MaildirMessage):
subdir = message.get_subdir()
suffix = self.colon + message.get_info()
@@ -724,9 +727,14 @@ class _singlefileMailbox(Mailbox):
def _append_message(self, message):
"""Append message to mailbox and return (start, stop) offsets."""
self._file.seek(0, 2)
- self._pre_message_hook(self._file)
- offsets = self._install_message(message)
- self._post_message_hook(self._file)
+ before = self._file.tell()
+ try:
+ self._pre_message_hook(self._file)
+ offsets = self._install_message(message)
+ self._post_message_hook(self._file)
+ except BaseException:
+ self._file.truncate(before)
+ raise
self._file.flush()
self._file_length = self._file.tell() # Record current length of mailbox
return offsets
@@ -906,7 +914,11 @@ class MH(Mailbox):
if self._locked:
_lock_file(f)
try:
- self._dump_message(message, f)
+ try:
+ self._dump_message(message, f)
+ except BaseException:
+ os.remove(new_path)
+ raise
if isinstance(message, MHMessage):
self._dump_sequences(message, new_key)
finally: