summaryrefslogtreecommitdiff
path: root/coverage
diff options
context:
space:
mode:
Diffstat (limited to 'coverage')
-rw-r--r--coverage/collector.py12
-rw-r--r--coverage/control.py8
-rw-r--r--coverage/tracer.c6
3 files changed, 23 insertions, 3 deletions
diff --git a/coverage/collector.py b/coverage/collector.py
index 6b196e99..9c40d16c 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
diff --git a/coverage/control.py b/coverage/control.py
index cdaf9721..4fae198c 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -69,6 +69,9 @@ class coverage(object):
"""
from coverage import __version__
+ # A record of all the warnings that have been issued.
+ self._warnings = []
+
# Build our configuration from a number of sources:
# 1: defaults:
self.config = CoverageConfig()
@@ -120,7 +123,7 @@ class coverage(object):
self.collector = Collector(
self._should_trace, timid=self.config.timid,
- branch=self.config.branch
+ branch=self.config.branch, warn=self._warn
)
# Suffixes are a bit tricky. We want to use the data suffix only when
@@ -274,7 +277,8 @@ class coverage(object):
def _warn(self, msg):
"""Use `msg` as a warning."""
- sys.stderr.write("Coverage.py warning: " + msg + "\n")
+ self._warnings.append(msg)
+ sys.stderr.write("Coverage.py warning: %s\n" % msg)
def _abs_files(self, files):
"""Return a list of absolute file names for the names in `files`."""
diff --git a/coverage/tracer.c b/coverage/tracer.c
index 9eb3cca1..e046596a 100644
--- a/coverage/tracer.c
+++ b/coverage/tracer.c
@@ -63,6 +63,7 @@ typedef struct {
/* Python objects manipulated directly by the Collector class. */
PyObject * should_trace;
+ PyObject * warn;
PyObject * data;
PyObject * should_trace_cache;
PyObject * arcs;
@@ -134,6 +135,7 @@ Tracer_init(Tracer *self, PyObject *args_unused, PyObject *kwds_unused)
#endif /* COLLECT_STATS */
self->should_trace = NULL;
+ self->warn = NULL;
self->data = NULL;
self->should_trace_cache = NULL;
self->arcs = NULL;
@@ -166,6 +168,7 @@ Tracer_dealloc(Tracer *self)
}
Py_XDECREF(self->should_trace);
+ Py_XDECREF(self->warn);
Py_XDECREF(self->data);
Py_XDECREF(self->should_trace_cache);
@@ -539,6 +542,9 @@ Tracer_members[] = {
{ "should_trace", T_OBJECT, offsetof(Tracer, should_trace), 0,
PyDoc_STR("Function indicating whether to trace a file.") },
+ { "warn", T_OBJECT, offsetof(Tracer, warn), 0,
+ PyDoc_STR("Function for issuing warnings.") },
+
{ "data", T_OBJECT, offsetof(Tracer, data), 0,
PyDoc_STR("The raw dictionary of trace data.") },