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

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


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1403192555 -32400
#      Fri Jun 20 00:42:35 2014 +0900
# Branch stable
# Node ID eeafe4fad188da8d8513c02af576e9c9dda00cf3
# Parent  f599144d072c808414bcab06046b35ae941b9c2f
subrepo: ensure "close()" execution at the end of "_initrepo()"

Before this patch, "close()" for the file object opened in
"_initrepo()" 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 "_initrepo()" by
moving it into "finally" clause.

This patch puts configuration lines into "lines" array and write them
out at once, to narrow the scope of "try"/"finally" for review-ability.

This patch doesn't use "vfs.write()", because:

  - current "vfs.write()" implementation doesn't take "mode" argument
    to open file in "text" mode

  - writing hgrc file out in binary mode may break backward compatibility

diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
--- a/mercurial/subrepo.py
+++ b/mercurial/subrepo.py
@@ -605,12 +605,11 @@ class hgsubrepo(abstractsubrepo):
         self._repo._subsource = source
 
         if create:
-            fp = self._repo.opener("hgrc", "w", text=True)
-            fp.write('[paths]\n')
+            lines = ['[paths]\n']
 
             def addpathconfig(key, value):
                 if value:
-                    fp.write('%s = %s\n' % (key, value))
+                    lines.append('%s = %s\n' % (key, value))
                     self._repo.ui.setconfig('paths', key, value, 'subrepo')
 
             defpath = _abssource(self._repo, abort=False)
@@ -618,7 +617,12 @@ class hgsubrepo(abstractsubrepo):
             addpathconfig('default', defpath)
             if defpath != defpushpath:
                 addpathconfig('default-push', defpushpath)
-            fp.close()
+
+            fp = self._repo.opener("hgrc", "w", text=True)
+            try:
+                fp.write(''.join(lines))
+            finally:
+                fp.close()
 
     @annotatesubrepoerror
     def add(self, ui, match, dryrun, listsubrepos, prefix, explicitonly):


More information about the Mercurial-devel mailing list