subrepo: ensure "close()" execution at the end of "_initrepo()"
authorFUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Fri, 20 Jun 2014 00:42:35 +0900
changeset 21891 db8a27d92818
parent 21890 0f916db7f297
child 21892 89cdebc31cda
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
mercurial/subrepo.py
--- a/mercurial/subrepo.py	Fri Jun 20 00:41:31 2014 +0900
+++ b/mercurial/subrepo.py	Fri Jun 20 00:42:35 2014 +0900
@@ -609,12 +609,11 @@
         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)
@@ -622,7 +621,12 @@
             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):