New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't crash on unexpected PY2 except syntax #4011

Merged
merged 5 commits into from Sep 26, 2017
Jump to file or symbol
Failed to load files and symbols.
+6 −6
Diff settings

Always

Just for now

View
@@ -586,15 +586,15 @@ def try_handler(self,
orelse: List[ast27.stmt],
finalbody: List[ast27.stmt],
lineno: int) -> TryStmt:
def produce_name(item: ast27.ExceptHandler) -> Optional[NameExpr]:
vs = []
for item in handlers:
if item.name is None:
return None
vs.append(None)
elif isinstance(item.name, ast27.Name):
return NameExpr(item.name.id)
vs.append(NameExpr(item.name.id))
else:
raise RuntimeError("'{}' has non-Name name.".format(ast27.dump(item)))
vs = [produce_name(h) for h in handlers]
self.fail("Sorry, `except <expr>, (<tuple>)` is not supported", item.lineno, item.col_offset)

This comment has been minimized.

@ethanhs

ethanhs Sep 26, 2017

Collaborator

This could be something other than a tuple however. For example, this works fine at runtime:

try:
    raise Exception('foo', 'bar')
except Exception, [a, b]:
    print a, b  # prints foo bar

Perhaps just "Unsupported exception handler syntax ...". If you want a specific warning, you can check that the item.name is an ast27.Tuple or ast27.List.

@ethanhs

ethanhs Sep 26, 2017

Collaborator

This could be something other than a tuple however. For example, this works fine at runtime:

try:
    raise Exception('foo', 'bar')
except Exception, [a, b]:
    print a, b  # prints foo bar

Perhaps just "Unsupported exception handler syntax ...". If you want a specific warning, you can check that the item.name is an ast27.Tuple or ast27.List.

This comment has been minimized.

@gvanrossum

gvanrossum Sep 26, 2017

Member

I'll change it to except <expr>, <anything but a name> -- it's syntactically an assignment target. I still don't think it's worth trying to support other syntax -- we just need to not crash.

@gvanrossum

gvanrossum Sep 26, 2017

Member

I'll change it to except <expr>, <anything but a name> -- it's syntactically an assignment target. I still don't think it's worth trying to support other syntax -- we just need to not crash.

vs.append(None)
types = [self.visit(h.type) for h in handlers]
handlers_ = [self.as_required_block(h.body, h.lineno) for h in handlers]
ProTip! Use n and p to navigate between commits in a pull request.