diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2019-04-21 08:48:16 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2019-04-21 08:48:16 -0400 |
commit | 7b9c1053bb75eab197acfcec53e9bb6ea7568a0e (patch) | |
tree | 71da40506553d813bcc8243e71f043d9ecdbec40 /coverage/context.py | |
parent | 0bdb413621460807583ab0baedf1d615a7a62da6 (diff) | |
download | python-coveragepy-git-7b9c1053bb75eab197acfcec53e9bb6ea7568a0e.tar.gz |
This method should be a function in context.py
Diffstat (limited to 'coverage/context.py')
-rw-r--r-- | coverage/context.py | 31 |
1 files changed, 31 insertions, 0 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"): |