From e585618c224568c2f47d99ab615b32e5f821a633 Mon Sep 17 00:00:00 2001 From: Ian Lee Date: Sat, 13 Dec 2014 19:08:06 -0800 Subject: Don't crash if build_tokens_line() returns None; issue #306 --- CHANGES.txt | 2 ++ pep8.py | 4 ++++ testsuite/test_api.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index fc4f984..72072ca 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -26,6 +26,8 @@ Bug fixes: * Update ``--format`` documentation. (Issue #198 / Pull Request #310) +* Don't crash if Checker.build_tokens_line() returns None. (Issue #306) + 1.5.7 (2014-05-29) ------------------ diff --git a/pep8.py b/pep8.py index d638388..0fbe22d 100755 --- a/pep8.py +++ b/pep8.py @@ -1346,6 +1346,10 @@ class Checker(object): """Build a line from tokens and run all logical checks on it.""" self.report.increment_logical_line() mapping = self.build_tokens_line() + + if not mapping: + return + (start_row, start_col) = mapping[0][1] start_line = self.lines[start_row - 1] self.indent_level = expand_indent(start_line[:start_col]) diff --git a/testsuite/test_api.py b/testsuite/test_api.py index 672f202..173eef5 100644 --- a/testsuite/test_api.py +++ b/testsuite/test_api.py @@ -342,5 +342,50 @@ class APITestCase(unittest.TestCase): self.assertFalse(sys.stderr) self.assertEqual(count_errors, 1) + def test_styleguide_unmatched_triple_quotes(self): + pep8.register_check(DummyChecker, ['Z701']) + lines = [ + 'def foo():\n', + ' """test docstring""\'\n', + ] + + pep8style = pep8.StyleGuide() + count_errors = pep8style.input_file('stdin', lines=lines) + stdout = sys.stdout.getvalue() + self.assertEqual(count_errors, 2) + + expected = 'stdin:2:5: E901 TokenError: EOF in multi-line string' + self.assertTrue(expected in stdout) + + expected = ( + 'stdin:2:26: ' + 'E901 SyntaxError: EOF while scanning triple-quoted string literal' + ) + self.assertTrue(expected in stdout) + + def test_styleguide_continuation_line_outdented(self): + pep8.register_check(DummyChecker, ['Z701']) + lines = [ + 'def foo():\n', + ' pass\n', + '\n', + '\\\n', + '\n', + 'def bar():\n', + ' pass\n', + ] + + pep8style = pep8.StyleGuide() + count_errors = pep8style.input_file('stdin', lines=lines) + self.assertEqual(count_errors, 2) + stdout = sys.stdout.getvalue() + expected = ( + 'stdin:6:1: ' + 'E122 continuation line missing indentation or outdented' + ) + self.assertTrue(expected in stdout) + expected = 'stdin:6:1: E302 expected 2 blank lines, found 1' + self.assertTrue(expected in stdout) + # TODO: runner # TODO: input_file -- cgit v1.2.1