Commits

Anonymous committed 8fac8d3

Don't report coverage for non-Python source files even if __file__ says they were executed.

Closes #82

Comments (0)

Files changed (2)

coverage/parser.py

         statements.
 
         """
-        self._raw_parse()
+
+        try:
+            self._raw_parse()
+        except tokenize.TokenError:
+            _, tokerr, _ = sys.exc_info()
+            msg, lineno = tokerr.args
+            raise NotPython(
+                "Couldn't parse '%s' as Python source: '%s' at %s" %
+                    (self.filename, msg, lineno)
+                )
 
         excluded_lines = self.first_lines(self.excluded)
         ignore = excluded_lines + list(self.docstrings)

test/test_html.py

         self.make_file("innocuous.html", "<h1>This isn't python at all!</h1>")
         output = self.run_command("coverage html")
         self.assertEqual(output.strip(), "No data to report.")
+
+    def test_execed_liar_ignored(self):
+        """
+        Jinja2 sets __file__ to be a non-Python file, and then execs code.
+
+        If that file contains non-Python code, a TokenError shouldn't
+        have been raised when writing the HTML report.
+
+        """
+
+        source = "exec compile('', '', 'exec') in {'__file__' : 'liar.html'}"
+        self.make_file("liar.py", source)
+        self.make_file("liar.html", "{# Whoops, not python code #}")
+        cov = coverage.coverage()
+        cov.start()
+        self.import_local_file("liar")
+        cov.stop()
+        cov.html_report()