Barry A. Warsaw added the comment:

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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue8604>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to