# HG changeset patch # User Martin von Zweigbergk # Date 2019-08-30 23:43:43 # Node ID c085cb134b9efcd494513f28028667d68aaa4e21 # Parent 3f81d58aae25dda07178cbe125e4439c53f19ccb statprof: use context manager when reading source from file Differential Revision: https://phab.mercurial-scm.org/D6779 diff --git a/mercurial/statprof.py b/mercurial/statprof.py --- a/mercurial/statprof.py +++ b/mercurial/statprof.py @@ -236,18 +236,14 @@ class CodeSite(object): def getsource(self, length): if self.source is None: lineno = self.lineno - 1 - fp = None try: - fp = open(self.path, 'rb') - for i, line in enumerate(fp): - if i == lineno: - self.source = line.strip() - break + with open(self.path, 'rb') as fp: + for i, line in enumerate(fp): + if i == lineno: + self.source = line.strip() + break except: pass - finally: - if fp: - fp.close() if self.source is None: self.source = ''