diff options
Diffstat (limited to 'coverage/codeunit.py')
-rw-r--r-- | coverage/codeunit.py | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py index ac90cb21..2b581d0d 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -2,7 +2,7 @@ import glob, os -from coverage.backward import string_class, StringIO +from coverage.backward import open_source, string_class, StringIO from coverage.misc import CoverageException @@ -52,7 +52,7 @@ class CodeUnit(object): else: f = morf # .pyc files should always refer to a .py instead. - if f.endswith('.pyc'): + if f.endswith('.pyc') or f.endswith('.pyo'): f = f[:-1] elif f.endswith('$py.class'): # jython f = f[:-9] + ".py" @@ -106,7 +106,7 @@ class CodeUnit(object): """Return an open file for reading the source of the code unit.""" if os.path.exists(self.filename): # A regular text file: open it. - return open(self.filename) + return open_source(self.filename) # Maybe it's in a zip file? source = self.file_locator.get_zip_data(self.filename) @@ -117,3 +117,23 @@ class CodeUnit(object): raise CoverageException( "No source for code %r." % self.filename ) + + def should_be_python(self): + """Does it seem like this file should contain Python? + + This is used to decide if a file reported as part of the exection of + a program was really likely to have contained Python in the first + place. + + """ + # Get the file extension. + _, ext = os.path.splitext(self.filename) + + # Anything named *.py* should be Python. + if ext.startswith('.py'): + return True + # A file with no extension should be Python. + if not ext: + return True + # Everything else is probably not Python. + return False |