Mercurial > public > mercurial-scm > hg-stable
diff mercurial/util.py @ 15057:774da7121fc9
atomictempfile: make close() consistent with other file-like objects.
The usual contract is that close() makes your writes permanent, so
atomictempfile's use of close() to *discard* writes (and rename() to
keep them) is rather unexpected. Thus, change it so close() makes
things permanent and add a new discard() method to throw them away.
discard() is only used internally, in __del__(), to ensure that writes
are discarded when an atomictempfile object goes out of scope.
I audited mercurial.*, hgext.*, and ~80 third-party extensions, and
found no one using the existing semantics of close() to discard
writes, so this should be safe.
author | Greg Ward <greg@gerg.ca> |
---|---|
date | Thu, 25 Aug 2011 20:21:04 -0400 |
parents | ff3e93686306 |
children | 81f33be0ea79 |
line wrap: on
line diff
--- a/mercurial/util.py Wed Aug 24 05:42:41 2011 -0400 +++ b/mercurial/util.py Thu Aug 25 20:21:04 2011 -0400 @@ -745,11 +745,10 @@ '''writeable file object that atomically updates a file All writes will go to a temporary copy of the original file. Call - rename() when you are done writing, and atomictempfile will rename - the temporary copy to the original name, making the changes visible. - - Unlike other file-like objects, close() discards your writes by - simply deleting the temporary file. + close() when you are done writing, and atomictempfile will rename + the temporary copy to the original name, making the changes + visible. If the object is destroyed without being closed, all your + writes are discarded. ''' def __init__(self, name, mode='w+b', createmode=None): self.__name = name # permanent name @@ -761,12 +760,12 @@ self.write = self._fp.write self.fileno = self._fp.fileno - def rename(self): + def close(self): if not self._fp.closed: self._fp.close() rename(self._tempname, localpath(self.__name)) - def close(self): + def discard(self): if not self._fp.closed: try: os.unlink(self._tempname) @@ -776,7 +775,7 @@ def __del__(self): if safehasattr(self, '_fp'): # constructor actually did something - self.close() + self.discard() def makedirs(name, mode=None): """recursive directory creation with parent mode inheritance"""