changeset 42835:db6d7cbda80b

statprof: use context manager for file when writing flame graph Differential Revision: https://phab.mercurial-scm.org/D6780
author Martin von Zweigbergk <martinvonz@google.com>
date Fri, 30 Aug 2019 16:44:31 -0700
parents c085cb134b9e
children cd3b5be5515d
files mercurial/statprof.py
diffstat 1 files changed, 4 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/statprof.py	Fri Aug 30 16:43:43 2019 -0700
+++ b/mercurial/statprof.py	Fri Aug 30 16:44:31 2019 -0700
@@ -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'