# HG changeset patch # User Martin von Zweigbergk # Date 2019-08-30 23:44:31 # Node ID db6d7cbda80bcc81a8f512f9eb46bcd1c0dc6cde # Parent c085cb134b9efcd494513f28028667d68aaa4e21 statprof: use context manager for file when writing flame graph Differential Revision: https://phab.mercurial-scm.org/D6780 diff --git a/mercurial/statprof.py b/mercurial/statprof.py --- a/mercurial/statprof.py +++ b/mercurial/statprof.py @@ -729,10 +729,6 @@ def write_to_flame(data, fp, scriptpath= 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 @@ def write_to_flame(data, fp, scriptpath= 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'