# HG changeset patch # User FUJIWARA Katsunori # Date 2014-06-19 15:42:35 # Node ID db8a27d92818904b2102d83fea57bc00fc3d72bd # Parent 0f916db7f297351bbcc61b810db00bfd16bc6303 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 @@ -609,12 +609,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) @@ -622,7 +621,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):