changeset 42834:c085cb134b9e

statprof: use context manager when reading source from file Differential Revision: https://phab.mercurial-scm.org/D6779
author Martin von Zweigbergk <martinvonz@google.com>
date Fri, 30 Aug 2019 16:43:43 -0700
parents 3f81d58aae25
children db6d7cbda80b
files mercurial/statprof.py
diffstat 1 files changed, 5 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/statprof.py	Fri Aug 30 15:12:37 2019 -0700
+++ b/mercurial/statprof.py	Fri Aug 30 16:43:43 2019 -0700
@@ -236,18 +236,14 @@
     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 = ''