Patchwork [03,of,10,V2] statprof: require paths to save or load profile data

login
register
mail settings
Submitter Gregory Szorc
Date Aug. 17, 2016, 4:03 p.m.
Message ID <2828b7bd26694c78c9d6.1471449821@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/16328/
State Changes Requested
Headers show

Comments

Gregory Szorc - Aug. 17, 2016, 4:03 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1471227245 25200
#      Sun Aug 14 19:14:05 2016 -0700
# Node ID 2828b7bd26694c78c9d6e0bcaa865b93530cc6e6
# Parent  3f37aba5e38717c7373101e7c868d13f6d809574
statprof: require paths to save or load profile data

Upstream appears to aggressively save statprof data in a well-defined
home directory path. Change the code to not do that.

We also change file saving to fail if an error has occurred
instead of silently failing. Callers can catch the exception.

This behavior is more suitable for a generic "library" module.

Patch

diff --git a/mercurial/statprof.py b/mercurial/statprof.py
--- a/mercurial/statprof.py
+++ b/mercurial/statprof.py
@@ -304,38 +304,30 @@  def stop():
             state.remaining_prof_time = rpt[0]
         elif lastmechanism == 'thread':
             stopthread.set()
             state.thread.join()
 
         state.accumulate_time(clock())
         state.last_start_time = None
         statprofpath = os.environ.get('STATPROF_DEST')
-        save_data(statprofpath)
+        if statprofpath:
+            save_data(statprofpath)
 
-def save_data(path=None):
-    try:
-        path = path or (os.environ['HOME'] + '/statprof.data')
-        file = open(path, "w+")
-
+def save_data(path):
+    with open(path, 'w+') as file:
         file.write(str(state.accumulated_time) + '\n')
         for sample in state.samples:
             time = str(sample.time)
             stack = sample.stack
             sites = ['\1'.join([s.path, str(s.lineno), s.function])
                      for s in stack]
             file.write(time + '\0' + '\0'.join(sites) + '\n')
 
-        file.close()
-    except (IOError, OSError):
-        # The home directory probably didn't exist, or wasn't writable. Oh well.
-        pass
-
-def load_data(path=None):
-    path = path or (os.environ['HOME'] + '/statprof.data')
+def load_data(path):
     lines = open(path, 'r').read().splitlines()
 
     state.accumulated_time = float(lines[0])
     state.samples = []
     for line in lines[1:]:
         parts = line.split('\0')
         time = float(parts[0])
         rawsites = parts[1:]