Patchwork D7626: config: close file even if we fail to read it

login
register
mail settings
Submitter phabricator
Date Dec. 12, 2019, 11:44 p.m.
Message ID <differential-rev-PHID-DREV-kueiohqjp4bi2ihaugxy-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/43769/
State Superseded
Headers show

Comments

phabricator - Dec. 12, 2019, 11:44 p.m.
martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D7626

AFFECTED FILES
  mercurial/ui.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -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 (