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

login
register
mail settings
Submitter Katsunori FUJIWARA
Date June 19, 2014, 3:43 p.m.
Message ID <a20591a932b7a3420eb8.1403192598@juju>
Download mbox | patch
Permalink /patch/5016/
State Accepted
Commit 9aaffb22d7d7e59d5079368536011a17bc2804de
Headers show

Comments

Katsunori FUJIWARA - June 19, 2014, 3:43 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@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.

Patch

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):