profile: introduce a "start" method to the profile context
authorPierre-Yves David <pierre-yves.david@octobus.net>
Fri, 09 Jun 2017 11:39:53 +0100
changeset 32784 086c1ef0f666
parent 32783 4483696dacee
child 32785 37ec8f24d912
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.
mercurial/profiling.py
--- a/mercurial/profiling.py	Thu Jun 08 01:38:48 2017 +0100
+++ b/mercurial/profiling.py	Fri Jun 09 11:39:53 2017 +0100
@@ -152,8 +152,24 @@
         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: