c_tryelsestmt.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (c) 2020 Rocky Bernstein
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. from decompyle3.parsers.treenode import SyntaxTree
  15. def c_tryelsestmt(self, lhs, n, rule, ast, tokens, first, last):
  16. # Check the end of the except handler that there isn't a jump from
  17. # inside the except handler to the end. If that happens
  18. # then this is a "try" with no "else".
  19. except_handler = ast[3]
  20. # print("XXX", first, last, rule)
  21. # for t in range(first, last): print(tokens[t])
  22. # print("="*40)
  23. if except_handler == "except_handler_else":
  24. except_handler = except_handler[0]
  25. come_from = except_handler[-1]
  26. # We only care about the *first* come_from because that is the
  27. # the innermost one. So if the "tryelse" is invalid (should be a "try")
  28. # it will be invalid here.
  29. if come_from == "COME_FROM":
  30. first_come_from = except_handler[-1]
  31. elif come_from == "END_FINALLY":
  32. first_come_from = None
  33. elif come_from == "except_return":
  34. return False
  35. else:
  36. assert come_from in ("come_froms", "opt_come_from_except")
  37. first_come_from = come_from[0]
  38. if not hasattr(first_come_from, "attr"):
  39. # optional come from
  40. return False
  41. leading_jump = except_handler[0]
  42. # We really don't care that this is a jump per-se. But
  43. # we could also check that this jumps to the end of the except if
  44. # desired.
  45. if isinstance(leading_jump, SyntaxTree):
  46. leading_jump = leading_jump.first_child()
  47. # If there is a jump in the except that goes to the same place as
  48. # except_handler_first_offset, then this is a "try" without an else.
  49. except_stmt = except_handler[2]
  50. if except_stmt in ("c_except_stmts", "except_stmts"):
  51. first_except = except_stmt[0]
  52. first_except_offset = first_except.first_child().off2int(prefer_last=False)
  53. i = self.offset2inst_index[first_except_offset]
  54. else_offset = leading_jump.attr
  55. inst = self.insts[i]
  56. while inst.offset < else_offset:
  57. if inst.is_jump() and inst.argval == else_offset:
  58. return True
  59. i += 1
  60. inst = self.insts[i]
  61. pass
  62. pass
  63. return False