diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2012-08-07 21:29:37 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2012-08-07 21:29:37 -0400 |
commit | 1728c47a79e17bdbc0889868f466a892881e8c82 (patch) | |
tree | c7f0b9afe1e85fed988ae70b9269f647998ae68b | |
parent | 8ecba376fabb6300e6ce2b71fa07737fde7cf7ab (diff) | |
parent | 6527a504c0871621563bb5d2b4147a065beaa55e (diff) | |
download | python-coveragepy-git-1728c47a79e17bdbc0889868f466a892881e8c82.tar.gz |
Merged
-rw-r--r-- | AUTHORS.txt | 1 | ||||
-rw-r--r-- | CHANGES.txt | 5 | ||||
-rw-r--r-- | coverage/parser.py | 10 | ||||
-rw-r--r-- | test/test_html.py | 21 |
4 files changed, 36 insertions, 1 deletions
diff --git a/AUTHORS.txt b/AUTHORS.txt index ef8ef276..1bb1d0c6 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -6,6 +6,7 @@ Other contributions have been made by: Marc Abramowitz Chris Adams Geoff Bache +Julian Berman Titus Brown Brett Cannon Pablo Carballo diff --git a/CHANGES.txt b/CHANGES.txt index 196ea1df..40408850 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -12,11 +12,16 @@ Version 3.5.3b1 - Files encoded as UTF-8 with a BOM are now properly handled, fixing `issue 179`_. Thanks, Pablo Carballo. +- Fixed more cases of non-Python files being reported as Python source, and + then not being able to parse them as Python. Closes `issue 82` (again). + Thanks, Julian Berman. + - Try to do a better job of the impossible task of detecting when we can't build the C extension, fixing `issue 183`_. - Testing is now done with `tox`_. +.. _issue 82: https://bitbucket.org/ned/coveragepy/issue/82/tokenerror-when-generating-html-report .. _issue 179: https://bitbucket.org/ned/coveragepy/issue/179/htmlreporter-fails-when-source-file-is .. _issue 183: https://bitbucket.org/ned/coveragepy/issue/183/install-fails-for-python-23 .. _tox: http://tox.readthedocs.org/ diff --git a/coverage/parser.py b/coverage/parser.py index f868d357..636a8353 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -203,7 +203,15 @@ class CodeParser(object): 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) diff --git a/test/test_html.py b/test/test_html.py index c5c87d2a..1877a30d 100644 --- a/test/test_html.py +++ b/test/test_html.py @@ -195,3 +195,24 @@ class HtmlWithUnparsableFilesTest(CoverageTest): 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. + + """ + if sys.version_info < (3, 0): + source = "exec compile('','','exec') in {'__file__': 'liar.html'}" + else: + source = "exec(compile('','','exec'), {'__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() + self.assert_exists("htmlcov/index.html") |