From d93a997913baa51989e33c94588d31bd07707b22 Mon Sep 17 00:00:00 2001 From: "loic@dachary.org" Date: Tue, 10 Jan 2017 16:33:14 +0100 Subject: move _source_for_file to python.py --HG-- branch : issue-426 --- coverage/python.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'coverage/python.py') 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.""" -- cgit v1.2.1 From 6da69c9cfcf8874cc423177a9376e90e27218d1d Mon Sep 17 00:00:00 2001 From: "loic@dachary.org" Date: Tue, 10 Jan 2017 17:31:34 +0100 Subject: source_for_file helper with unit tests --HG-- branch : issue-426 --- coverage/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'coverage/python.py') diff --git a/coverage/python.py b/coverage/python.py index ada21e67..5e142d24 100644 --- a/coverage/python.py +++ b/coverage/python.py @@ -91,7 +91,7 @@ def get_zip_bytes(filename): return None -def _source_for_file(self, filename): +def source_for_file(filename): """Return the source file for `filename`. Given a file name being traced, return the best guess as to the source -- cgit v1.2.1 From ffc050e243b4c329f582372dd49c55aa225197b9 Mon Sep 17 00:00:00 2001 From: "loic@dachary.org" Date: Tue, 10 Jan 2017 22:21:13 +0100 Subject: use the new source_for_file helper where it makes sense --HG-- branch : issue-426 --- coverage/python.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'coverage/python.py') diff --git a/coverage/python.py b/coverage/python.py index 5e142d24..f75be60a 100644 --- a/coverage/python.py +++ b/coverage/python.py @@ -139,13 +139,7 @@ class PythonFileReporter(FileReporter): else: filename = morf - filename = files.unicode_filename(filename) - - # .pyc files should always refer to a .py instead. - if filename.endswith(('.pyc', '.pyo')): - filename = filename[:-1] - elif filename.endswith('$py.class'): # Jython - filename = filename[:-9] + ".py" + filename = source_for_file(files.unicode_filename(filename)) super(PythonFileReporter, self).__init__(files.canonical_filename(filename)) -- cgit v1.2.1