Make AtomicFile support context managers.

This commit is contained in:
Valentin Lorentz 2014-07-31 22:52:16 +02:00
parent e42a3dd6ac
commit 5f582e3f52

View File

@ -156,13 +156,19 @@ class AtomicFile(object):
# self.__parent = super(AtomicFile, self) # self.__parent = super(AtomicFile, self)
self._fd = open(self.tempFilename, mode) self._fd = open(self.tempFilename, mode)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
if exc_type:
self.rollback()
else:
self.close()
@property @property
def closed(self): def closed(self):
return self._fd.closed return self._fd.closed
def close(self):
return self._fd.close()
def write(self, data): def write(self, data):
return self._fd.write(data) return self._fd.write(data)