1. Ned Batchelder
  2. coverage.py


Ned Batchelder  committed 316cb1b

Don't complain about files that can't be parsed as Python if they didn't seem like Python at all. #82.

  • Participants
  • Parent commits 1d44060
  • Branches default

Comments (0)

Files changed (5)

File CHANGES.txt

View file
 - Backup files left behind by editors are no longer collected by the source=
   option, fixing `issue 168`_.
+- If a file doesn't parse properly as Python, we don't report it as an error
+  if the filename seems like maybe it wasn't meant to be Python.  This is a
+  pragmatic fix for `issue 82`_.
 - When running a module with ``coverage run -m <modulename>``, certain details
   of the execution environment weren't the same as for
   ``python -m <modulename>``.  This had the unfortunate side-effect of making
 - When installing into pypy, we no longer attempt (and fail) to compile
   the C tracer function, closing `issue 166`_.
+.. _issue 82: https://bitbucket.org/ned/coveragepy/issue/82/tokenerror-when-generating-html-report
 .. _issue 155: https://bitbucket.org/ned/coveragepy/issue/155/cant-use-coverage-run-m-unittest-discover
 .. _issue 157: https://bitbucket.org/ned/coveragepy/issue/157/chokes-on-source-files-with-non-utf-8
 .. _issue 166: https://bitbucket.org/ned/coveragepy/issue/166/dont-try-to-compile-c-extension-on-pypy

File coverage/codeunit.py

View file
         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

File coverage/control.py

View file
             # can't do anything with the data later anyway.
             return False
-        if filename.endswith(".html"):
-            # Jinja and maybe other templating systems compile templates into
-            # Python code, but use the template filename as the filename in
-            # the compiled code.  Of course, those filenames are useless later
-            # so don't bother collecting.  TODO: How should we really separate
-            # out good file extensions from bad?
-            return False
         # Compiled Python files have two filenames: frame.f_code.co_filename is

File coverage/report.py

View file
         for cu in self.code_units:
                 report_fn(cu, self.coverage._analyze(cu))
-            except (NoSource, NotPython):
+            except NoSource:
                 if not self.ignore_errors:
+            except NotPython:
+                # Only report errors for .py files, and only if we didn't
+                # explicitly suppress those errors.
+                if cu.should_be_python(".py") and not self.ignore_errors:
+                    raise

File coverage/summary.py

View file
 from coverage.report import Reporter
 from coverage.results import Numbers
+from coverage.misc import NotPython
 class SummaryReporter(Reporter):
             except KeyboardInterrupt:                       # pragma: no cover
-                if not self.ignore_errors:
+                report_it = not self.ignore_errors
+                if report_it:
                     typ, msg = sys.exc_info()[:2]
+                    if typ is NotPython and not cu.should_be_python(".py"):
+                        report_it = False
+                if report_it:
                     outfile.write(fmt_err % (cu.name, typ.__name__, msg))
         if total.n_files > 1: