From a7fc111387f1f782f3404a21c103cac4812f9e74 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 17 Jan 2015 16:44:34 -0500 Subject: When finding the source for a frame, really check if it exists --- coverage/control.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'coverage/control.py') diff --git a/coverage/control.py b/coverage/control.py index 63c9d382..1044fad0 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -308,11 +308,18 @@ class Coverage(object): def _source_for_file(self, filename): """Return the source file for `filename`.""" - if not filename.endswith(".py"): - if filename[-4:-1] == ".py": - filename = filename[:-1] - elif filename.endswith("$py.class"): # jython - filename = filename[:-9] + ".py" + if filename.endswith(".py"): + return filename + elif filename.endswith((".pyc", ".pyo")): + try_filename = filename[:-1] + if os.path.exists(try_filename): + return try_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" return filename def _name_for_module(self, module_globals, filename): -- cgit v1.2.1