[PATCH] Use try/finally to close files on error

Victor Stinner victor.stinner at haypocalc.com
Wed Nov 2 16:03:38 CDT 2011


Le 02/11/2011 21:47, Matt Mackall a écrit :
>> Use "try: ...  finally: fp.close()" in other places to ensure that the
>> file is
>> also closed on error.
>
> Given that Python automatically closes files when garbage-collecting,
> what do we gain by this?

It is not a good idea to relay on the garbage collector, it's not 
reliable. In PyPy, the garbage collector can be called "later" (you 
cannot know when exactly).

In Python 3.2+, a ResourceWarning is emited if a file is destroyed 
before it is closed explicitly.

> Your email client damages patches.

Oh sorry. I attached the patch as a file to this mail.

Victor
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: try_finally_close.patch
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20111102/967be39c/attachment.ksh>


More information about the Mercurial-devel mailing list