Patchwork [2,of,6] profile: introduce a "start" method to the profile context

login
register
mail settings
Submitter Pierre-Yves David
Date June 9, 2017, 12:32 p.m.
Message ID <5b9718803532b81e240e.1497011578@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/21279/
State Accepted
Headers show

Comments

Pierre-Yves David - June 9, 2017, 12:32 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@octobus.net>
# Date 1497004793 -3600
#      Fri Jun 09 11:39:53 2017 +0100
# Node ID 5b9718803532b81e240e126913608385a32352bd
# Parent  a3a4b52464f3dbaf533f62bca7512f42c6a6d449
# EXP-Topic profile
# Available At https://www.mercurial-scm.org/repo/users/marmoute/mercurial/
#              hg pull https://www.mercurial-scm.org/repo/users/marmoute/mercurial/ -r 5b9718803532
profile: introduce a "start" method to the profile context

The start method is doing all profiler setup and activation. It is currently
unconditionally called by '__init__' but this will be made more flexible in
later changesets.

Patch

diff --git a/mercurial/profiling.py b/mercurial/profiling.py
--- a/mercurial/profiling.py
+++ b/mercurial/profiling.py
@@ -152,8 +152,24 @@  class profile(object):
         self._output = None
         self._fp = None
         self._profiler = None
+        self._entered = False
+        self._started = False
 
     def __enter__(self):
+        self._entered = True
+        self.start()
+
+    def start(self):
+        """Start profiling.
+
+        The profiling will stop at the context exit.
+
+        If the profiler was already started, this has no effect."""
+        if not self._entered:
+            raise error.ProgrammingError()
+        if self._started:
+            return
+        self._started = True
         profiler = encoding.environ.get('HGPROF')
         proffn = None
         if profiler is None: