Skip to content

Commit e585618

Browse files
committed
Don't crash if build_tokens_line() returns None; issue #306
1 parent 60ad926 commit e585618

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Bug fixes:
2626

2727
* Update ``--format`` documentation. (Issue #198 / Pull Request #310)
2828

29+
* Don't crash if Checker.build_tokens_line() returns None. (Issue #306)
30+
2931

3032
1.5.7 (2014-05-29)
3133
------------------

pep8.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,10 @@ def check_logical(self):
13461346
"""Build a line from tokens and run all logical checks on it."""
13471347
self.report.increment_logical_line()
13481348
mapping = self.build_tokens_line()
1349+
1350+
if not mapping:
1351+
return
1352+
13491353
(start_row, start_col) = mapping[0][1]
13501354
start_line = self.lines[start_row - 1]
13511355
self.indent_level = expand_indent(start_line[:start_col])

testsuite/test_api.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,5 +342,50 @@ def test_check_nullbytes(self):
342342
self.assertFalse(sys.stderr)
343343
self.assertEqual(count_errors, 1)
344344

345+
def test_styleguide_unmatched_triple_quotes(self):
346+
pep8.register_check(DummyChecker, ['Z701'])
347+
lines = [
348+
'def foo():\n',
349+
' """test docstring""\'\n',
350+
]
351+
352+
pep8style = pep8.StyleGuide()
353+
count_errors = pep8style.input_file('stdin', lines=lines)
354+
stdout = sys.stdout.getvalue()
355+
self.assertEqual(count_errors, 2)
356+
357+
expected = 'stdin:2:5: E901 TokenError: EOF in multi-line string'
358+
self.assertTrue(expected in stdout)
359+
360+
expected = (
361+
'stdin:2:26: '
362+
'E901 SyntaxError: EOF while scanning triple-quoted string literal'
363+
)
364+
self.assertTrue(expected in stdout)
365+
366+
def test_styleguide_continuation_line_outdented(self):
367+
pep8.register_check(DummyChecker, ['Z701'])
368+
lines = [
369+
'def foo():\n',
370+
' pass\n',
371+
'\n',
372+
'\\\n',
373+
'\n',
374+
'def bar():\n',
375+
' pass\n',
376+
]
377+
378+
pep8style = pep8.StyleGuide()
379+
count_errors = pep8style.input_file('stdin', lines=lines)
380+
self.assertEqual(count_errors, 2)
381+
stdout = sys.stdout.getvalue()
382+
expected = (
383+
'stdin:6:1: '
384+
'E122 continuation line missing indentation or outdented'
385+
)
386+
self.assertTrue(expected in stdout)
387+
expected = 'stdin:6:1: E302 expected 2 blank lines, found 1'
388+
self.assertTrue(expected in stdout)
389+
345390
# TODO: runner
346391
# TODO: input_file

0 commit comments

Comments
 (0)