--- mailbox.py.orig 2006-11-12 12:38:00.000000000 +0000 +++ mailbox.py 2006-11-12 14:12:28.000000000 +0000 @@ -4,6 +4,10 @@ # Notes for authors of new mailbox subclasses: # +# To implement different locking requirements, override the +# _acquire_lock() and _release_lock() methods, as these are used +# internally during mailbox operations. +# # Remember to fsync() changes to disk before closing a modified file # or returning from a flush() method. See functions _sync_flush() and # _sync_close(). @@ -179,6 +183,14 @@ """Write any pending changes to the disk.""" raise NotImplementedError('Method must be implemented by subclass') + def _acquire_lock(self, f, dotlock=True): + """Lock file f using lockf and dot locking.""" + _lock_file(f, dotlock) + + def _release_lock(self, f): + """Unlock file f using lockf and dot locking.""" + _unlock_file(f) + def lock(self): """Lock the mailbox.""" raise NotImplementedError('Method must be implemented by subclass') @@ -553,13 +565,13 @@ def lock(self): """Lock the mailbox.""" if not self._locked: - _lock_file(self._file) + self._acquire_lock(self._file) self._locked = True def unlock(self): """Unlock the mailbox if it is locked.""" if self._locked: - _unlock_file(self._file) + self._release_lock(self._file) self._locked = False def flush(self): @@ -604,7 +616,7 @@ self._toc = new_toc self._pending = False if self._locked: - _lock_file(self._file, dotlock=False) + self._acquire_lock(self._file, dotlock=False) def _pre_mailbox_hook(self, f): """Called before writing the mailbox to file f.""" @@ -804,14 +816,14 @@ f = _create_carefully(new_path) try: if self._locked: - _lock_file(f) + self._acquire_lock(f) try: self._dump_message(message, f) if isinstance(message, MHMessage): self._dump_sequences(message, new_key) finally: if self._locked: - _unlock_file(f) + self._release_lock(f) finally: _sync_close(f) return new_key @@ -828,13 +840,13 @@ raise try: if self._locked: - _lock_file(f) + self._acquire_lock(f) try: f.close() os.remove(os.path.join(self._path, str(key))) finally: if self._locked: - _unlock_file(f) + self._release_lock(f) finally: f.close() @@ -850,7 +862,7 @@ raise try: if self._locked: - _lock_file(f) + self._acquire_lock(f) try: os.close(os.open(path, os.O_WRONLY | os.O_TRUNC)) self._dump_message(message, f) @@ -858,7 +870,7 @@ self._dump_sequences(message, key) finally: if self._locked: - _unlock_file(f) + self._release_lock(f) finally: _sync_close(f) @@ -876,12 +888,12 @@ raise try: if self._locked: - _lock_file(f) + self._acquire_lock(f) try: msg = MHMessage(f) finally: if self._locked: - _unlock_file(f) + self._release_lock(f) finally: f.close() for name, key_list in self.get_sequences(): @@ -903,12 +915,12 @@ raise try: if self._locked: - _lock_file(f) + self._acquire_lock(f) try: return f.read() finally: if self._locked: - _unlock_file(f) + self._release_lock(f) finally: f.close() @@ -940,13 +952,13 @@ """Lock the mailbox.""" if not self._locked: self._file = open(os.path.join(self._path, '.mh_sequences'), 'rb+') - _lock_file(self._file) + self._acquire_lock(self._file) self._locked = True def unlock(self): """Unlock the mailbox if it is locked.""" if self._locked: - _unlock_file(self._file) + self._release_lock(self._file) _sync_close(self._file) del self._file self._locked = False @@ -1057,7 +1069,7 @@ f = open(os.path.join(self._path, str(key)), 'r+') try: if self._locked: - _lock_file(f) + self._acquire_lock(f) try: if hasattr(os, 'link'): os.link(os.path.join(self._path, str(key)), @@ -1072,7 +1084,7 @@ os.path.join(self._path, str(prev + 1))) finally: if self._locked: - _unlock_file(f) + self._release_lock(f) finally: f.close() prev += 1