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

login
register
mail settings
Submitter Katsunori FUJIWARA
Date June 19, 2014, 3:43 p.m.
Message ID <eeafe4fad188da8d8513.1403192602@juju>
Download mbox | patch
Permalink /patch/5022/
State Accepted
Commit db8a27d92818904b2102d83fea57bc00fc3d72bd
Headers show

Comments

Katsunori FUJIWARA - June 19, 2014, 3:43 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@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
Matt Mackall - July 16, 2014, 10:42 p.m.
On Fri, 2014-06-20 at 00:43 +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy@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()"

These are queued for default, thanks.

Patch

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