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

Matt Mackall mpm at selenic.com
Wed Nov 2 15:47:55 CDT 2011


On Wed, 2011-11-02 at 21:42 +0100, Victor Stinner wrote:
> Hi,
> 
> Following patch close the open file in ignore.ignore(), 
> revlog.revlog._loadchunk(), ui.ui.readconfig(). It uses also "try: ... 
> finally: fp.close()" pattern in other places to ensure that the file
> is also closed on error.
> 
> Victor
> 
> --
> 
> exporting patch:
> # HG changeset patch
> # User Victor Stinner <victor.stinner at haypocalc.com>
> # Date 1320266504 -3600
> # Node ID d49fd76a1ec1e317e42d63ed75d006bb02fb0115
> # Parent  872f06c342ffa01f414cb57506090faaff410cd4
> Close the file in ignore.ignore(), revlog.revlog._loadchunk(),
> ui.ui.readconfig().
> 
> 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?

> diff --git a/mercurial/changelog.py b/mercurial/changelog.py
> --- a/mercurial/changelog.py
> +++ b/mercurial/changelog.py
> @@ -130,8 +130,10 @@ class changelog(revlog.revlog):
>               util.rename(n, n[:-2])
>           elif self._delaybuf:
>               fp = self.opener(self.indexfile, 'a')
> -            fp.write("".join(self._delaybuf))

Your email client damages patches.

-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list