summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2013-03-10 10:30:07 -0400
committerNed Batchelder <ned@nedbatchelder.com>2013-03-10 10:30:07 -0400
commitc2a99c7880afbcf7557a895b6fe9fa6cb3d14356 (patch)
treed36bffd7e2d877915c464fe1b03bc9d3e701d290
parentfa53c8a6abd4407a486c5d54c7ad760ea3126a95 (diff)
downloadpython-coveragepy-c2a99c7880afbcf7557a895b6fe9fa6cb3d14356.tar.gz
Don't issue spurious warnings about the trace function changing. Fixes #164
-rw-r--r--CHANGES.txt5
-rw-r--r--coverage/collector.py10
-rw-r--r--tests/test_process.py33
3 files changed, 45 insertions, 3 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 200204d..82b1b84 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -7,6 +7,11 @@ Change history for Coverage.py
- Improved the branch coverage mechanism, fixing `issue 175`_.
+- When running a threaded program under the Python tracer, coverage would issue
+ a spurious warning about the trace function changing: "Trace function
+ changed, measurement is likely wrong: None." This is now fixed.
+
+.. _issue 164: https://bitbucket.org/ned/coveragepy/issue/164/trace-function-changed-warning-when-using
.. _issue 175: https://bitbucket.org/ned/coveragepy/issue/175/branch-coverage-gets-confused-in-certain
diff --git a/coverage/collector.py b/coverage/collector.py
index 0cfd8ff..781a0fa 100644
--- a/coverage/collector.py
+++ b/coverage/collector.py
@@ -51,6 +51,7 @@ class PyTracer(object):
self.last_exc_back = None
self.last_exc_firstlineno = 0
self.arcs = False
+ self.thread = None
def _trace(self, frame, event, arg_unused):
"""The trace function passed to sys.settrace."""
@@ -118,18 +119,21 @@ class PyTracer(object):
Return a Python function suitable for use with sys.settrace().
"""
+ self.thread = threading.currentThread()
sys.settrace(self._trace)
return self._trace
def stop(self):
"""Stop this Tracer."""
+ if self.thread != threading.currentThread():
+ # Called on a different thread than started us: do nothing.
+ return
+
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(),))
- #--debug
- #from coverage.misc import short_stack
- #self.warn(msg % (sys.gettrace()))#, short_stack()))
+ #print("Stopping tracer on %s" % threading.current_thread().ident)
sys.settrace(None)
def get_stats(self):
diff --git a/tests/test_process.py b/tests/test_process.py
index f3d6c56..8e31bd1 100644
--- a/tests/test_process.py
+++ b/tests/test_process.py
@@ -430,6 +430,39 @@ class ProcessTest(CoverageTest):
self.assertNotIn("warning", out)
self.assertNotIn("Exception", out)
+ def test_warnings_trace_function_changed_with_threads(self):
+ # https://bitbucket.org/ned/coveragepy/issue/164
+ self.make_file("bug164.py", """\
+ import threading
+ import time
+
+ class MyThread (threading.Thread):
+ def run(self):
+ print("Hello")
+
+ thr = MyThread()
+ thr.start()
+ thr.join()
+ """)
+ out = self.run_command("coverage run --timid bug164.py")
+
+ self.assertIn("Hello\n", out)
+ self.assertNotIn("warning", out)
+
+ def test_warning_trace_function_changed(self):
+ self.make_file("settrace.py", """\
+ import sys
+ print("Hello")
+ sys.settrace(None)
+ print("Goodbye")
+ """)
+ out = self.run_command("coverage run --timid settrace.py")
+ self.assertIn("Hello\n", out)
+ self.assertIn("Goodbye\n", out)
+
+ if hasattr(sys, "gettrace"):
+ self.assertIn("Trace function changed", out)
+
if sys.version_info >= (3, 0): # This only works on 3.x for now.
# It only works with the C tracer,
c_tracer = os.getenv('COVERAGE_TEST_TRACER', 'c') == 'c'