changeset 43910:c41ed5d4f770

config: close file even if we fail to read it If we get an exception from cfg.read(), we would not close the file before this patch. This patch uses a context manager to make sure we close it. Differential Revision: https://phab.mercurial-scm.org/D7626
author Martin von Zweigbergk <martinvonz@google.com>
date Wed, 11 Dec 2019 15:06:09 -0800
parents dbaf9aabfb69
children f2de8dc9c52f
files mercurial/ui.py
diffstat 1 files changed, 9 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/ui.py	Wed Dec 11 15:33:07 2019 -0800
+++ b/mercurial/ui.py	Wed Dec 11 15:06:09 2019 -0800
@@ -434,16 +434,16 @@
                 return
             raise
 
-        cfg = config.config()
-        trusted = sections or trust or self._trusted(fp, filename)
+        with fp:
+            cfg = config.config()
+            trusted = sections or trust or self._trusted(fp, filename)
 
-        try:
-            cfg.read(filename, fp, sections=sections, remap=remap)
-            fp.close()
-        except error.ParseError as inst:
-            if trusted:
-                raise
-            self.warn(_(b"ignored: %s\n") % stringutil.forcebytestr(inst))
+            try:
+                cfg.read(filename, fp, sections=sections, remap=remap)
+            except error.ParseError as inst:
+                if trusted:
+                    raise
+                self.warn(_(b'ignored: %s\n') % stringutil.forcebytestr(inst))
 
         if self.plain():
             for k in (