customize26_27.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (c) 2019 2021, 2024 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.6 and 2.7 version-specific semantic actions here.
  16. """
  17. from uncompyle6.semantics.consts import TABLE_DIRECT
  18. def customize_for_version26_27(self, version: tuple):
  19. ########################################
  20. # Python 2.6+
  21. # except <condition> as <var>
  22. # vs. older:
  23. # except <condition> , <var>
  24. #
  25. # For 2.6 we use the older syntax which
  26. # matches how we parse this in bytecode
  27. ########################################
  28. if version > (2, 6):
  29. self.TABLE_DIRECT.update(
  30. {
  31. "except_cond2": ("%|except %c as %c:\n", 1, 5),
  32. # When a generator is a single parameter of a function,
  33. # it doesn't need the surrounding parenethesis.
  34. "call_generator": ("%c%P", 0, (1, -1, ", ", 100)),
  35. }
  36. )
  37. else:
  38. self.TABLE_DIRECT.update(
  39. {
  40. "testtrue_then": ("not %p", (0, 22)),
  41. }
  42. )
  43. # FIXME: this should be a transformation
  44. def n_call(node):
  45. mapping = self._get_mapping(node)
  46. key = node
  47. for i in mapping[1:]:
  48. key = key[i]
  49. pass
  50. if key.kind == "CALL_FUNCTION_1":
  51. # A function with one argument. If this is a generator,
  52. # no parenthesis is needed.
  53. args_node = node[-2]
  54. if args_node == "expr":
  55. n = args_node[0]
  56. if n == "generator_exp":
  57. node.kind = "call_generator"
  58. pass
  59. pass
  60. self.default(node)
  61. self.n_call = n_call
  62. def n_import_from(node):
  63. if node[0].pattr > 0:
  64. node[2].pattr = ("." * node[0].pattr) + node[2].pattr
  65. self.default(node)
  66. self.n_import_from = n_import_from