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

login
register
mail settings
Submitter phabricator
Date Aug. 31, 2019, 5:35 p.m.
Message ID <differential-rev-PHID-DREV-rcaok5j4s26d56ztp52m-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/41448/
State Superseded
Headers show

Comments

phabricator - Aug. 31, 2019, 5:35 p.m.
martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/statprof.py

CHANGE DETAILS




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

Patch

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'