eric6/DebugClients/Python/coverage/pytracer.py

changeset 7427
362cd1b6f81a
parent 6942
2602857055c5
child 7975
7d493839a8fc
--- a/eric6/DebugClients/Python/coverage/pytracer.py	Wed Feb 19 19:38:36 2020 +0100
+++ b/eric6/DebugClients/Python/coverage/pytracer.py	Sat Feb 22 14:27:42 2020 +0100
@@ -1,5 +1,5 @@
 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
-# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
+# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
 
 """Raw data collector for coverage.py."""
 
@@ -40,6 +40,7 @@
         self.trace_arcs = False
         self.should_trace = None
         self.should_trace_cache = None
+        self.should_start_context = None
         self.warn = None
         # The threading module to use, if any.
         self.threading = None
@@ -47,6 +48,8 @@
         self.cur_file_dict = None
         self.last_line = 0          # int, but uninitialized.
         self.cur_file_name = None
+        self.context = None
+        self.started_context = False
 
         self.data_stack = []
         self.last_exc_back = None
@@ -60,7 +63,7 @@
         atexit.register(setattr, self, 'in_atexit', True)
 
     def __repr__(self):
-        return "<PyTracer at {0}: {1} lines in {2} files>".format(
+        return "<PyTracer at {}: {} lines in {} files>".format(
             id(self),
             sum(len(v) for v in self.data.values()),
             len(self.data),
@@ -83,7 +86,7 @@
 
         #self.log(":", frame.f_code.co_filename, frame.f_lineno, event)
 
-        if (self.stopped and sys.gettrace() == self._trace):
+        if (self.stopped and sys.gettrace() == self._trace):    # pylint: disable=comparison-with-callable
             # The PyTrace.stop() method has been called, possibly by another
             # thread, let's deactivate ourselves now.
             #self.log("X", frame.f_code.co_filename, frame.f_lineno)
@@ -96,14 +99,35 @@
                 if self.trace_arcs and self.cur_file_dict:
                     pair = (self.last_line, -self.last_exc_firstlineno)
                     self.cur_file_dict[pair] = None
-                self.cur_file_dict, self.cur_file_name, self.last_line = self.data_stack.pop()
+                self.cur_file_dict, self.cur_file_name, self.last_line, self.started_context = (
+                    self.data_stack.pop()
+                )
             self.last_exc_back = None
 
         if event == 'call':
-            # Entering a new function context.  Decide if we should trace
+            # Should we start a new context?
+            if self.should_start_context and self.context is None:
+                context_maybe = self.should_start_context(frame)
+                if context_maybe is not None:
+                    self.context = context_maybe
+                    self.started_context = True
+                    self.switch_context(self.context)
+                else:
+                    self.started_context = False
+            else:
+                self.started_context = False
+
+            # Entering a new frame.  Decide if we should trace
             # in this file.
             self._activity = True
-            self.data_stack.append((self.cur_file_dict, self.cur_file_name, self.last_line))
+            self.data_stack.append(
+                (
+                    self.cur_file_dict,
+                    self.cur_file_name,
+                    self.last_line,
+                    self.started_context,
+                )
+            )
             filename = frame.f_code.co_filename
             self.cur_file_name = filename
             disp = self.should_trace_cache.get(filename)
@@ -146,7 +170,13 @@
                     first = frame.f_code.co_firstlineno
                     self.cur_file_dict[(self.last_line, -first)] = None
             # Leaving this function, pop the filename stack.
-            self.cur_file_dict, self.cur_file_name, self.last_line = self.data_stack.pop()
+            self.cur_file_dict, self.cur_file_name, self.last_line, self.started_context = (
+                self.data_stack.pop()
+            )
+            # Leaving a context?
+            if self.started_context:
+                self.context = None
+                self.switch_context(None)
         elif event == 'exception':
             self.last_exc_back = frame.f_back
             self.last_exc_firstlineno = frame.f_code.co_firstlineno
@@ -175,7 +205,7 @@
 
     def stop(self):
         """Stop this Tracer."""
-        # Get the activate tracer callback before setting the stop flag to be
+        # Get the active tracer callback before setting the stop flag to be
         # able to detect if the tracer was changed prior to stopping it.
         tf = sys.gettrace()
 
@@ -196,7 +226,7 @@
             # so don't warn if we are in atexit on PyPy and the trace function
             # has changed to None.
             dont_warn = (env.PYPY and env.PYPYVERSION >= (5, 4) and self.in_atexit and tf is None)
-            if (not dont_warn) and tf != self._trace:
+            if (not dont_warn) and tf != self._trace:   # pylint: disable=comparison-with-callable
                 self.warn(
                     "Trace function changed, measurement is likely wrong: %r" % (tf,),
                     slug="trace-changed",

eric ide

mercurial