diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2012-11-21 20:58:14 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2012-11-21 20:58:14 -0500 |
commit | c97ab5ffc039d9d35b2f03992d4d9a185fd16f2b (patch) | |
tree | 6d3efa8c05afd9b76e1df6d2594c3f346c833903 | |
parent | 62458bcb03cfc4c351d9885bbab607ac1ec69f13 (diff) | |
download | python-coveragepy-git-c97ab5ffc039d9d35b2f03992d4d9a185fd16f2b.tar.gz |
Fix IndentationErrors when parsing non-Python files. #82.
-rw-r--r-- | CHANGES.txt | 4 | ||||
-rw-r--r-- | coverage/parser.py | 2 | ||||
-rw-r--r-- | test/test_html.py | 23 |
3 files changed, 11 insertions, 18 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index c19421eb..1316df13 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -52,6 +52,9 @@ Version 3.6b1 HTML report would fail with a UnicodeDecodeError. This issue (`issue 193`_) is now fixed. +- Fixed yet another error trying to parse non-Python files as Python, this + time an IndentationError, closing `issue 82`_ for the fourth time... + - If `coverage xml` fails because there is no data to report, it used to create a zero-length XML file. Now it doesn't, fixing `issue 210`_. @@ -69,6 +72,7 @@ Version 3.6b1 - Other minor bugs fixed: `issue 153`_. +.. _issue 82: https://bitbucket.org/ned/coveragepy/issue/82/tokenerror-when-generating-html-report .. _issue 89: https://bitbucket.org/ned/coveragepy/issue/89/on-windows-all-packages-are-reported-in .. _issue 111: https://bitbucket.org/ned/coveragepy/issue/111/when-installing-coverage-with-pip-not .. _issue 137: https://bitbucket.org/ned/coveragepy/issue/137/provide-docs-with-source-distribution diff --git a/coverage/parser.py b/coverage/parser.py index 3694b924..d894e61c 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -205,7 +205,7 @@ class CodeParser(object): """ try: self._raw_parse() - except tokenize.TokenError: + except (tokenize.TokenError, IndentationError): _, tokerr, _ = sys.exc_info() msg, lineno = tokerr.args raise NotPython( diff --git a/test/test_html.py b/test/test_html.py index 633b506b..7c42b8c7 100644 --- a/test/test_html.py +++ b/test/test_html.py @@ -253,18 +253,6 @@ class HtmlWithUnparsableFilesTest(CoverageTest): output = self.run_command("coverage html") self.assertEqual(output.strip(), "No data to report.") - def test_dothtml_not_python_indentation_error(self): - # We run a .html file, and when reporting, we can't parse it as - # Python. Since it wasn't .py, no error is reported. - - # Run an "html" file - self.make_file("innocuous.html", "a = 1") - self.run_command("coverage run innocuous.html") - # Before reporting, change it to be an indentation error. - self.make_file("innocuous.html", " hello\n bye\n") - 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 @@ -284,14 +272,15 @@ class HtmlWithUnparsableFilesTest(CoverageTest): def test_execed_liar_ignored_indentation_error(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 that file contains untokenizable code, we shouldn't get an + # exception. if sys.version_info < (3, 0): - source = "exec compile('','','exec') in {'__file__': 'liar.jinja2'}" + source = "exec compile('','','exec') in {'__file__': 'liar.html'}" else: - source = "exec(compile('','','exec'), {'__file__': 'liar.jinja2'})" + source = "exec(compile('','','exec'), {'__file__': 'liar.html'})" self.make_file("liar.py", source) - self.make_file("liar.jinja2", " hello\n bye\n") + # Tokenize will raise an IndentationError if it can't dedent. + self.make_file("liar.html", "0\n 2\n 1\n") cov = coverage.coverage() cov.start() self.import_local_file("liar") |