customize25.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (c) 2019-2020 by Rocky Bernstein
  2. #
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. """Isolate Python 2.5+ version-specific semantic actions here.
  16. """
  17. from uncompyle6.semantics.consts import TABLE_DIRECT
  18. #######################
  19. # Python 2.5+ Changes #
  20. #######################
  21. def customize_for_version25(self, version):
  22. ########################
  23. # Import style for 2.5+
  24. ########################
  25. self.TABLE_DIRECT.update(
  26. {
  27. "importmultiple": ("%|import %c%c\n", 2, 3),
  28. "import_cont": (", %c", 2),
  29. # With/as is allowed as "from future" thing in 2.5
  30. # Note: It is safe to put the variables after "as" in parenthesis,
  31. # and sometimes it is needed.
  32. "with": ("%|with %c:\n%+%c%-", 0, 3),
  33. "and_then": ("%c and %c", (0, "expr"), (4, "expr")),
  34. }
  35. )
  36. # In 2.5+ "except" handlers and the "finally" can appear in one
  37. # "try" statement. So the below has the effect of combining the
  38. # "tryfinally" with statement with the "try_except" statement.
  39. # FIXME: something doesn't smell right, since the semantics
  40. # are different. See test_fileio.py for an example that shows this.
  41. def tryfinallystmt(node):
  42. if len(node[1][0]) == 1 and node[1][0][0] == "stmt":
  43. if node[1][0][0][0] == "try_except":
  44. node[1][0][0][0].kind = "tf_try_except"
  45. if node[1][0][0][0] == "tryelsestmt":
  46. node[1][0][0][0].kind = "tf_tryelsestmt"
  47. self.default(node)
  48. self.n_tryfinallystmt = tryfinallystmt
  49. def n_import_from(node):
  50. if node[0].pattr > 0:
  51. node[2].pattr = ("." * node[0].pattr) + node[2].pattr
  52. self.default(node)
  53. self.n_import_from = n_import_from