[PATCH 3 of 7 STABLE] subrepo: ensure "close()" execution at the end of "_calcfilehash()"

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Thu Jun 19 10:43:18 CDT 2014


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1403191279 -32400
#      Fri Jun 20 00:21:19 2014 +0900
# Branch stable
# Node ID a20591a932b7a3420eb8f0c2cd7549514ea8d734
# Parent  f1af69e11e972b1be65ca7d5e62126c00713a10b
subrepo: ensure "close()" execution at the end of "_calcfilehash()"

Before this patch, "close()" for the file object opened in
"_calcfilehash()" may not be executed, if unexpected exception is
raised, because it isn't executed in "finally" clause.

This patch ensures "close()" execution at the end of "_calcfilehash()"
by moving it into "finally" clause.

diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
--- a/mercurial/subrepo.py
+++ b/mercurial/subrepo.py
@@ -35,8 +35,10 @@ def _calcfilehash(filename):
     data = ''
     if os.path.exists(filename):
         fd = open(filename, 'rb')
-        data = fd.read()
-        fd.close()
+        try:
+            data = fd.read()
+        finally:
+            fd.close()
     return util.sha1(data).hexdigest()
 
 class SubrepoAbort(error.Abort):


More information about the Mercurial-devel mailing list