comparison mercurial/ui.py @ 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 e3ce3731d621
comparison
equal deleted inserted replaced
43909:dbaf9aabfb69 43910:c41ed5d4f770
432 except IOError: 432 except IOError:
433 if not sections: # ignore unless we were looking for something 433 if not sections: # ignore unless we were looking for something
434 return 434 return
435 raise 435 raise
436 436
437 cfg = config.config() 437 with fp:
438 trusted = sections or trust or self._trusted(fp, filename) 438 cfg = config.config()
439 439 trusted = sections or trust or self._trusted(fp, filename)
440 try: 440
441 cfg.read(filename, fp, sections=sections, remap=remap) 441 try:
442 fp.close() 442 cfg.read(filename, fp, sections=sections, remap=remap)
443 except error.ParseError as inst: 443 except error.ParseError as inst:
444 if trusted: 444 if trusted:
445 raise 445 raise
446 self.warn(_(b"ignored: %s\n") % stringutil.forcebytestr(inst)) 446 self.warn(_(b'ignored: %s\n') % stringutil.forcebytestr(inst))
447 447
448 if self.plain(): 448 if self.plain():
449 for k in ( 449 for k in (
450 b'debug', 450 b'debug',
451 b'fallbackencoding', 451 b'fallbackencoding',