D6780: statprof: use context manager for file when writing flame graph

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


Closed by commit rHGdac4a3b6f86e: statprof: use context manager for file when writing flame graph (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/D6780?vs=16345&id=16350

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

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

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
@@ -729,10 +729,6 @@
         fp.write(b'get it here: https://github.com/brendangregg/FlameGraph\n')
         return
 
-    fd, path = pycompat.mkstemp()
-
-    file = open(path, "w+")
-
     lines = {}
     for sample in data.samples:
         sites = [s.function for s in sample.stack]
@@ -743,10 +739,11 @@
         else:
             lines[line] = 1
 
-    for line, count in lines.iteritems():
-        file.write("%s %d\n" % (line, count))
+    fd, path = pycompat.mkstemp()
 
-    file.close()
+    with open(path, "w+") as file:
+        for line, count in lines.iteritems():
+                file.write("%s %d\n" % (line, count))
 
     if outputfile is None:
         outputfile = '~/flamegraph.svg'



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


More information about the Mercurial-devel mailing list