| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- # Copyright (c) 2020 Rocky Bernstein
- def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
- come_from_except = ast[-1]
- if rule in (
- (
- "try_except",
- (
- "SETUP_EXCEPT",
- "suite_stmts_opt",
- "POP_BLOCK",
- "except_handler",
- "opt_come_from_except",
- ),
- ),
- (
- "c_try_except",
- (
- "SETUP_EXCEPT",
- "c_suite_stmts",
- "POP_BLOCK",
- "c_except_handler",
- "opt_come_from_except",
- ),
- ),
- ):
- if come_from_except[0] == "COME_FROM":
- # There should be at least two COME_FROMs, one from an
- # exception handler and one from the try. Otherwise
- # we have a try/else.
- return True
- pass
- elif rule == (
- "try_except",
- (
- "SETUP_EXCEPT",
- "suite_stmts_opt",
- "POP_BLOCK",
- "except_handler",
- "\\e_opt_come_from_except",
- ),
- ):
- # Find END_FINALLY.
- for i in range(last, first, -1):
- if tokens[i] == "END_FINALLY":
- jump_before_finally = tokens[i - 1]
- if jump_before_finally.kind.startswith("JUMP"):
- if jump_before_finally == "JUMP_FORWARD":
- # If there is a JUMP_FORWARD before
- # the END_FINALLY to some jumps place
- # beyond tokens[last].off2int() then
- # this is a try/else rather than an
- # try (no else).
- return tokens[i - 1].attr > tokens[last].off2int(
- prefer_last=True
- )
- elif jump_before_finally == "JUMP_LOOP":
- # If there is a JUMP_LOOP before the
- # END_FINALLY then this is a looping
- # jump, but then jumps in the except
- # handlers have to also be a looping
- # jump or this is a try/else rather
- # than an try (no else).
- except_handler = ast[3]
- if (
- except_handler == "except_handler"
- and except_handler[0] == "JUMP_FORWARD"
- ):
- return True
- return False
- pass
- pass
- pass
- return False
|