tryelsestmt.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (c) 2020 Rocky Bernstein
  2. from uncompyle6.parsers.treenode import SyntaxTree
  3. def tryelsestmt(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. # for t in range(first, last):
  8. # print(tokens[t])
  9. # print("=" * 30)
  10. except_handler = ast[3]
  11. if except_handler == "except_handler_else":
  12. except_handler = except_handler[0]
  13. if except_handler == "except_handler":
  14. come_from = except_handler[-1]
  15. # We only care about the *first* come_from because that is the
  16. # the innermost one. So if the "tryelse" is invalid (should be a "try")
  17. # it will be invalid here.
  18. if come_from == "COME_FROM":
  19. first_come_from = except_handler[-1]
  20. elif come_from == "END_FINALLY":
  21. return False
  22. else:
  23. assert come_from == "come_froms"
  24. first_come_from = come_from[0]
  25. leading_jump = except_handler[0]
  26. # We really don't care that this is a jump per-se. But
  27. # we could also check that this jumps to the end of the except if
  28. # desired.
  29. if isinstance(leading_jump, SyntaxTree):
  30. except_handler_first_offset = leading_jump.first_child().off2int()
  31. else:
  32. except_handler_first_offset = leading_jump.off2int()
  33. if first_come_from.attr < tokens[first].offset:
  34. return True
  35. return first_come_from.attr > except_handler_first_offset
  36. return False