diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-01-18 11:54:03 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-01-18 11:54:03 -0500 |
commit | 0d0836309b6354a07fb09cd8d28b309b6c8dba6c (patch) | |
tree | 53e7f22f8c83f09a6b00a54c9e0bb862d73dc68f /coverage/control.py | |
parent | b90a9db23aa99e58e20f48e53218fdb7fbd3c965 (diff) | |
download | python-coveragepy-git-0d0836309b6354a07fb09cd8d28b309b6c8dba6c.tar.gz |
Properly deal with .pyc files missing their sources.
Diffstat (limited to 'coverage/control.py')
-rw-r--r-- | coverage/control.py | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/coverage/control.py b/coverage/control.py index 1044fad0..16a92971 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -307,19 +307,35 @@ class Coverage(object): return os.path.split(morf_filename)[0] def _source_for_file(self, filename): - """Return the source file for `filename`.""" + """Return the source file for `filename`. + + Given a filename 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")): - try_filename = filename[:-1] - if os.path.exists(try_filename): - return try_filename + # 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 sys.platform == "win32": - try_filename += "w" - if os.path.exists(try_filename): - return try_filename - elif filename.endswith("$py.class"): # Jython - filename = filename[:-9] + ".py" + # 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 filename as-is. return filename def _name_for_module(self, module_globals, filename): |