Message256806
| Author |
r.david.murray |
| Recipients |
Eduardo.Seabra, berker.peksag, georg.brandl, ncoghlan, pitrou, r.david.murray, serhiy.storchaka, socketpair, vstinner |
| Date |
2015年12月21日.20:14:01 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1450728841.97.0.919545935213.issue21579@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
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. |
|