Skip to content

Commit 5252e82

Browse files
authored
Don't crash on unexpected PY2 except syntax (#4011)
Rather than supporting esoteric forms like `except Ex, (err, info)` we just generate an error. At least it should no longer be possible to generate a crash this way.
1 parent c332ea0 commit 5252e82

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

mypy/fastparse2.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -586,15 +586,16 @@ def try_handler(self,
586586
orelse: List[ast27.stmt],
587587
finalbody: List[ast27.stmt],
588588
lineno: int) -> TryStmt:
589-
def produce_name(item: ast27.ExceptHandler) -> Optional[NameExpr]:
589+
vs = [] # type: List[Optional[NameExpr]]
590+
for item in handlers:
590591
if item.name is None:
591-
return None
592+
vs.append(None)
592593
elif isinstance(item.name, ast27.Name):
593-
return NameExpr(item.name.id)
594+
vs.append(NameExpr(item.name.id))
594595
else:
595-
raise RuntimeError("'{}' has non-Name name.".format(ast27.dump(item)))
596-
597-
vs = [produce_name(h) for h in handlers]
596+
self.fail("Sorry, `except <expr>, <anything but a name>` is not supported",
597+
item.lineno, item.col_offset)
598+
vs.append(None)
598599
types = [self.visit(h.type) for h in handlers]
599600
handlers_ = [self.as_required_block(h.body, h.lineno) for h in handlers]
600601

test-data/unit/check-python2.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ except BaseException, e:
7777
e() # E: "BaseException" not callable
7878
[builtins_py2 fixtures/exception.pyi]
7979

80+
[case testTryExceptUnsupported]
81+
try:
82+
pass
83+
except BaseException, (e, f): # E: Sorry, `except <expr>, <anything but a name>` is not supported
84+
pass
85+
try:
86+
pass
87+
except BaseException, [e, f, g]: # E: Sorry, `except <expr>, <anything but a name>` is not supported
88+
pass
89+
try:
90+
pass
91+
except BaseException, e[0]: # E: Sorry, `except <expr>, <anything but a name>` is not supported
92+
pass
93+
[builtins_py2 fixtures/exception.pyi]
94+
8095
[case testAlternateNameSuggestions]
8196
class Foo(object):
8297
def say_hello(self):

0 commit comments

Comments
 (0)