Message256806
You'd have to do that anyway if we implemented a delete=False constructor argument, since you want it deleted if there are any errors, and that's not what a delete=False API would do.
If it were me, I'd write it (untested)
@contextlib.contextmanager
def open_for_atomic_replace(fn):
try:
fd, name = tempfile.mkstemp()
with io.open(fd) as fff:
yield fff
fff.flush()
os.fdatasync(fff)
os.rename(name, fn)
except BaseException:
os.unlink(name)
raise
which would make your code simpler than it is now.
Naming it 'open_for_atomic_replace' reminded me of issue 8604, which is what you really want, not delete=False. |
|
Date |
User |
Action |
Args |
2015-12-21 20:14:02 | r.david.murray | set | recipients:
+ r.david.murray, georg.brandl, ncoghlan, pitrou, vstinner, socketpair, berker.peksag, serhiy.storchaka, Eduardo.Seabra |
2015-12-21 20:14:01 | r.david.murray | set | messageid: <1450728841.97.0.919545935213.issue21579@psf.upfronthosting.co.za> |
2015-12-21 20:14:01 | r.david.murray | link | issue21579 messages |
2015-12-21 20:14:01 | r.david.murray | create | |
|