summaryrefslogtreecommitdiff
path: root/coverage/python.py
diff options
context:
space:
mode:
authorloic@dachary.org <loic@dachary.org>2017-01-10 16:33:14 +0100
committerloic@dachary.org <loic@dachary.org>2017-01-10 16:33:14 +0100
commitd93a997913baa51989e33c94588d31bd07707b22 (patch)
tree98ae1a024dc7e4a42f1cb7d0d6469dcf01cba908 /coverage/python.py
parentbcc6d71c52207fa4808f14234231af7d399b975f (diff)
downloadpython-coveragepy-git-d93a997913baa51989e33c94588d31bd07707b22.tar.gz
move _source_for_file to python.py
--HG-- branch : issue-426
Diffstat (limited to 'coverage/python.py')
-rw-r--r--coverage/python.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/coverage/python.py b/coverage/python.py
index c3ca0e1e..ada21e67 100644
--- a/coverage/python.py
+++ b/coverage/python.py
@@ -91,6 +91,39 @@ def get_zip_bytes(filename):
return None
+def _source_for_file(self, filename):
+ """Return the source file for `filename`.
+
+ Given a file name being traced, return the best guess as to the source
+ file to attribute it to.
+
+ """
+ if filename.endswith(".py"):
+ # .py files are themselves source files.
+ return filename
+
+ elif filename.endswith((".pyc", ".pyo")):
+ # Bytecode files probably have source files near them.
+ py_filename = filename[:-1]
+ if os.path.exists(py_filename):
+ # Found a .py file, use that.
+ return py_filename
+ if env.WINDOWS:
+ # On Windows, it could be a .pyw file.
+ pyw_filename = py_filename + "w"
+ if os.path.exists(pyw_filename):
+ return pyw_filename
+ # Didn't find source, but it's probably the .py file we want.
+ return py_filename
+
+ elif filename.endswith("$py.class"):
+ # Jython is easy to guess.
+ return filename[:-9] + ".py"
+
+ # No idea, just use the file name as-is.
+ return filename
+
+
class PythonFileReporter(FileReporter):
"""Report support for a Python file."""