summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-10-03 18:54:55 -0400
committerNed Batchelder <ned@nedbatchelder.com>2015-10-03 18:54:55 -0400
commit8fbef1db95ca65a7bec898283da350b0e0b01847 (patch)
tree17ed9ca2737b3b638914beadc2827200fcbc3eca
parenta877aaf13d9f72ee317b392c86313a3003bcb3b8 (diff)
downloadpython-coveragepy-git-8fbef1db95ca65a7bec898283da350b0e0b01847.tar.gz
Prevent pyexpat.c from being recorded as source code. #419
-rw-r--r--CHANGES.rst4
-rw-r--r--coverage/control.py10
-rw-r--r--tests/test_oddball.py8
3 files changed, 20 insertions, 2 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 349247ff..610ea4bf 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -15,7 +15,11 @@ Version 4.1
are seeing corrupt data files, but this lets them continue combining anyway.
Prompted by `issue 418`_.
+- Pyexpat C code will no longer be recorded as a source file, fixing
+ `issue 419`_.
+
.. _issue 418: https://bitbucket.org/ned/coveragepy/issues/418/json-parse-error
+.. _issue 419: https://bitbucket.org/ned/coveragepy/issues/419/nosource-no-source-for-code-path-to-c
Version 4.0 --- 20 September 2015
diff --git a/coverage/control.py b/coverage/control.py
index 77737181..a5741a47 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -7,6 +7,7 @@ import atexit
import inspect
import os
import platform
+import re
import sys
import traceback
@@ -289,7 +290,7 @@ class Coverage(object):
# environments (virtualenv, for example), these modules may be
# spread across a few locations. Look at all the candidate modules
# we've imported, and take all the different ones.
- for m in (atexit, inspect, os, platform, _structseq, traceback):
+ for m in (atexit, inspect, os, platform, re, _structseq, traceback):
if m is not None and hasattr(m, "__file__"):
self.pylib_dirs.add(self._canonical_dir(m))
if _structseq and not hasattr(_structseq, '__file__'):
@@ -475,6 +476,11 @@ class Coverage(object):
# can't do anything with the data later anyway.
return nope(disp, "not a real file name")
+ # pyexpat does a dumb thing, calling the trace function explicitly from
+ # C code with a C file name.
+ if re.search(r"[/\\]Modules[/\\]pyexpat.c", filename):
+ return nope(disp, "pyexpat lies about itself")
+
# Jython reports the .class file to the tracer, use the source file.
if filename.endswith("$py.class"):
filename = filename[:-9] + ".py"
@@ -805,7 +811,7 @@ class Coverage(object):
"""
self._init()
if not self._measured:
- return
+ return self.data
self.collector.save_data(self.data)
diff --git a/tests/test_oddball.py b/tests/test_oddball.py
index 2ca1aa75..bca7f127 100644
--- a/tests/test_oddball.py
+++ b/tests/test_oddball.py
@@ -226,6 +226,14 @@ class PyexpatTest(CoverageTest):
self.assertEqual(statements, [101, 102])
self.assertEqual(missing, [])
+ # Make sure pyexpat isn't recorded as a source file.
+ # https://bitbucket.org/ned/coveragepy/issues/419/nosource-no-source-for-code-path-to-c
+ files = cov.get_data().measured_files()
+ self.assertFalse(
+ any(f.endswith("pyexpat.c") for f in files),
+ "Pyexpat.c is in the measured files!: %r:" % (files,)
+ )
+
class ExceptionTest(CoverageTest):
"""I suspect different versions of Python deal with exceptions differently