D6779: statprof: use context manager when reading source from file

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Sat Aug 31 18:59:14 EDT 2019


Closed by commit rHGc085cb134b9e: statprof: use context manager when reading source from file (authored by martinvonz).
This revision was automatically updated to reflect the committed changes.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D6779?vs=16344&id=16349

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D6779/new/

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

AFFECTED FILES
  mercurial/statprof.py

CHANGE DETAILS

diff --git a/mercurial/statprof.py b/mercurial/statprof.py
--- a/mercurial/statprof.py
+++ b/mercurial/statprof.py
@@ -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 = ''
 



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


More information about the Mercurial-devel mailing list