Add default 'except:' must be last.

This commit is contained in:
Dave Halter
2017-07-20 01:33:59 +02:00
parent 9c4bf8cec4
commit 03526cd84e
3 changed files with 27 additions and 0 deletions

View File

@@ -58,6 +58,16 @@ class ErrorFinder(Normalizer):
else:
self._add_syntax_error("invalid syntax", leaf)
elif node.type in _BLOCK_STMTS:
if node.type == 'try_stmt':
default_except = None
for except_clause in node.children[3::3]:
if except_clause in ('else', 'finally'):
break
if except_clause == 'except':
default_except = except_clause
elif default_except is not None:
self._add_syntax_error("default 'except:' must be last", default_except)
with self._context.add_block(node):
if len(self._context.blocks) == _MAX_BLOCK_SIZE:
self._add_syntax_error("too many statically nested blocks", node)

View File

@@ -14,3 +14,19 @@ for x in [1]:
for x in [1]:
break
continue
try:
pass
except ZeroDivisionError:
pass
#: E722:0
except:
pass
try:
pass
#: E722:0 E901:0
except:
pass
except ZeroDivisionError:
pass

View File

@@ -70,6 +70,7 @@ def test_indentation_errors(code, positions):
'continue',
'break',
'return',
'try: pass\nexcept: pass\nexcept X: pass',
# IndentationError
' foo',