# HG changeset patch # User Martin von Zweigbergk # Date 2019-12-11 23:06:09 # Node ID c41ed5d4f77025d883e87f1fb5b21f38476cb49e # Parent dbaf9aabfb69d726c9185cebbfed2ac0e74c30b4 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 diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -434,16 +434,16 @@ class ui(object): 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 (