diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2010-11-06 16:49:23 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2010-11-06 16:49:23 -0400 |
commit | 226aa60227cc4e9e39d71dd1bbbcfe77c83f0280 (patch) | |
tree | b5a13149a73adaf3cc895ba274380ca752d700d9 /coverage/collector.py | |
parent | 968bbe56458fa91e19222cf879d4cb1bda213bac (diff) | |
download | python-coveragepy-226aa60227cc4e9e39d71dd1bbbcfe77c83f0280.tar.gz |
Detect when our trace function is yanked out from under us, and warn the user. Finishes, but does not fix, issue #93.
Diffstat (limited to 'coverage/collector.py')
-rw-r--r-- | coverage/collector.py | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/coverage/collector.py b/coverage/collector.py index 6b196e9..9c40d16 100644 --- a/coverage/collector.py +++ b/coverage/collector.py @@ -33,6 +33,7 @@ class PyTracer(object): self.data = None self.should_trace = None self.should_trace_cache = None + self.warn = None self.cur_file_data = None self.last_line = 0 self.data_stack = [] @@ -109,6 +110,10 @@ class PyTracer(object): def stop(self): """Stop this Tracer.""" + if hasattr(sys, "gettrace") and self.warn: + if sys.gettrace() != self._trace: + msg = "Trace function changed, measurement is likely wrong: %r" + self.warn(msg % sys.gettrace()) sys.settrace(None) def get_stats(self): @@ -137,7 +142,7 @@ class Collector(object): # the top, and resumed when they become the top again. _collectors = [] - def __init__(self, should_trace, timid, branch): + def __init__(self, should_trace, timid, branch, warn): """Create a collector. `should_trace` is a function, taking a filename, and returning a @@ -153,8 +158,12 @@ class Collector(object): collecting data on which statements followed each other (arcs). Use `get_arc_data` to get the arc data. + `warn` is a warning function, taking a single string message argument, + to be used if a warning needs to be issued. + """ self.should_trace = should_trace + self.warn = warn self.branch = branch self.reset() @@ -194,6 +203,7 @@ class Collector(object): tracer.arcs = self.branch tracer.should_trace = self.should_trace tracer.should_trace_cache = self.should_trace_cache + tracer.warn = self.warn fn = tracer.start() self.tracers.append(tracer) return fn |