tryelsestmtl3.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (c) 2020 Rocky Bernstein
  2. from uncompyle6.parsers.treenode import SyntaxTree
  3. def tryelsestmtl3(self, lhs, n, rule, ast, tokens, first, last):
  4. # Check the end of the except handler that there isn't a jump from
  5. # inside the except handler to the end. If that happens
  6. # then this is a "try" with no "else".
  7. except_handler = ast[3]
  8. if except_handler == "except_handler_else":
  9. except_handler = except_handler[0]
  10. come_from = except_handler[-1]
  11. # We only care about the *first* come_from because that is the
  12. # the innermost one. So if the "tryelse" is invalid (should be a "try")
  13. # it will be invalid here.
  14. if come_from == "COME_FROM":
  15. first_come_from = except_handler[-1]
  16. elif come_from == "END_FINALLY":
  17. return False
  18. elif come_from == "except_return":
  19. return False
  20. else:
  21. assert come_from in ("come_froms", "opt_come_from_except")
  22. first_come_from = come_from[0]
  23. if not hasattr(first_come_from, "attr"):
  24. # optional come from
  25. return False
  26. leading_jump = except_handler[0]
  27. if not hasattr(leading_jump, "offset"):
  28. return False
  29. # We really don't care that this is a jump per-se. But
  30. # we could also check that this jumps to the end of the except if
  31. # desired.
  32. if isinstance(leading_jump, SyntaxTree):
  33. except_handler_first_offset = leading_jump.first_child().off2int()
  34. else:
  35. except_handler_first_offset = leading_jump.off2int()
  36. return first_come_from.attr > except_handler_first_offset