Author barry
Recipients Arfrever, barry, eric.araujo, eric.smith, exarkun, giampaolo.rodola, loewis, martin.panter, meatballhat, milko.krachounov, ncoghlan, neologix, olemis, pitrou, tarek, vstinner
Date 2013-05-16.14:11:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1368713496.73.0.835428039378.issue8604@psf.upfronthosting.co.za>
In-reply-to
Content
Of course, I have my own atomic-rename thing, but I'm not going to post the code here.  It's fairly limited to my own use case and I have no interest in making it cross platform.

That being said, I personally found that a context manager with a signature identical to built-in open() was the most convenient API.  It looks natural:

with atomic(filename, 'w', encoding='utf-8') as fp:
   data = do_a_bunch_of_things_that_might_fail()
   fp.write(data)

If any of that fails, the temporary file is guaranteed to be cleaned up, otherwise, filename (which is the ultimate destination) will be guaranteed to exist.

Another reason why context managers are useful is with ExitStack(), where you might have a bunch of conditions that you want to guarantee get cleaned up properly if any of them fail.
History
Date User Action Args
2013-05-16 14:11:36barrysetrecipients: + barry, loewis, exarkun, ncoghlan, pitrou, vstinner, eric.smith, giampaolo.rodola, tarek, eric.araujo, Arfrever, olemis, meatballhat, milko.krachounov, neologix, martin.panter
2013-05-16 14:11:36barrysetmessageid: <1368713496.73.0.835428039378.issue8604@psf.upfronthosting.co.za>
2013-05-16 14:11:36barrylinkissue8604 messages
2013-05-16 14:11:36barrycreate