summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2019-04-21 08:48:16 -0400
committerNed Batchelder <ned@nedbatchelder.com>2019-04-21 08:48:16 -0400
commit7b9c1053bb75eab197acfcec53e9bb6ea7568a0e (patch)
tree71da40506553d813bcc8243e71f043d9ecdbec40
parent0bdb413621460807583ab0baedf1d615a7a62da6 (diff)
downloadpython-coveragepy-git-7b9c1053bb75eab197acfcec53e9bb6ea7568a0e.tar.gz
This method should be a function in context.py
-rw-r--r--coverage/context.py31
-rw-r--r--coverage/control.py39
2 files changed, 36 insertions, 34 deletions
diff --git a/coverage/context.py b/coverage/context.py
index 25be98c5..22c0e531 100644
--- a/coverage/context.py
+++ b/coverage/context.py
@@ -3,6 +3,37 @@
"""Determine contexts for coverage.py"""
+
+def combine_context_switchers(context_switchers):
+ """Create a single context switcher from multiple switchers.
+
+ `context_switchers` is a list of functions that take a frame as an
+ argument and return a string to use as the new context label.
+
+ Returns a function that composites `context_switchers` functions, or None
+ if `context_switchers` is an empty list.
+
+ When invoked, the combined switcher calls `context_switchers` one-by-one
+ until a string is returned. The combined switcher returns None if all
+ `context_switchers` return None.
+ """
+ if not context_switchers:
+ return None
+
+ if len(context_switchers) == 1:
+ return context_switchers[0]
+
+ def should_start_context(frame):
+ """The combiner for multiple context switchers."""
+ for switcher in context_switchers:
+ new_context = switcher(frame)
+ if new_context is not None:
+ return new_context
+ return None
+
+ return should_start_context
+
+
def should_start_context_test_function(frame):
"""Is this frame calling a test_* function?"""
if frame.f_code.co_name.startswith("test"):
diff --git a/coverage/control.py b/coverage/control.py
index ae5f0442..9bcab844 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -15,7 +15,7 @@ from coverage.annotate import AnnotateReporter
from coverage.backward import string_class, iitems
from coverage.collector import Collector, CTracer
from coverage.config import read_coverage_config
-from coverage.context import should_start_context_test_function
+from coverage.context import should_start_context_test_function, combine_context_switchers
from coverage.data import CoverageData, combine_parallel_data
from coverage.debug import DebugControl, write_formatted_info
from coverage.disposition import disposition_debug_msg
@@ -335,34 +335,6 @@ class Coverage(object):
if not should_skip:
self._data.read()
- def _combine_context_switchers(self, context_switchers):
- """Create a single context switcher from multiple switchers.
-
- `context_switchers` is a list of methods that take a frame
- as an argument and return a string to use as the new context label.
-
- Returns a method that composits `context_switchers` methods, or None
- if `context_switchers` is an empty list.
-
- When invoked, the combined switcher calls `context_switchers` one-by-one
- until a string is returned. Combined switcher returns None if all
- `context_switchers` return None.
- """
- if not context_switchers:
- return None
-
- if len(context_switchers) == 1:
- return context_switchers[0]
-
- def should_start_context(frame):
- for switcher in context_switchers:
- new_context = switcher(frame)
- if new_context is not None:
- return new_context
- return None
-
- return should_start_context
-
def _init_for_start(self):
"""Initialization for start()"""
# Construct the collector.
@@ -386,12 +358,11 @@ class Coverage(object):
"Don't understand dynamic_context setting: {!r}".format(self.config.dynamic_context)
)
- context_switchers.extend([
- plugin.dynamic_context
- for plugin in self._plugins.context_switchers
- ])
+ context_switchers.extend(
+ plugin.dynamic_context for plugin in self._plugins.context_switchers
+ )
- should_start_context = self._combine_context_switchers(context_switchers)
+ should_start_context = combine_context_switchers(context_switchers)
self._collector = Collector(
should_trace=self._should_trace,