customize3.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. # Copyright (c) 2018-2021, 2023-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. """
  16. Isolate Python 3 version-specific semantic actions here.
  17. """
  18. from xdis import iscode
  19. from uncompyle6.semantics.consts import TABLE_DIRECT
  20. from uncompyle6.semantics.customize35 import customize_for_version35
  21. from uncompyle6.semantics.customize36 import customize_for_version36
  22. from uncompyle6.semantics.customize37 import customize_for_version37
  23. from uncompyle6.semantics.customize38 import customize_for_version38
  24. from uncompyle6.semantics.helper import find_code_node, gen_function_parens_adjust
  25. from uncompyle6.semantics.make_function3 import make_function3_annotate
  26. from uncompyle6.util import get_code_name
  27. def customize_for_version3(self, version: tuple):
  28. self.TABLE_DIRECT.update(
  29. {
  30. "comp_for": (" for %c in %c", (2, "store"), (0, "expr")),
  31. "if_exp_not": (
  32. "%c if not %c else %c",
  33. (2, "expr"),
  34. (0, "expr"),
  35. (4, "expr"),
  36. ),
  37. "except_cond2": ("%|except %c as %c:\n", (1, "expr"), (5, "store")),
  38. "function_def_annotate": ("\n\n%|def %c%c\n", -1, 0),
  39. # When a generator is a single parameter of a function,
  40. # it doesn't need the surrounding parenethesis.
  41. "call_generator": ("%c%P", 0, (1, -1, ", ", 100)),
  42. "importmultiple": ("%|import %c%c\n", 2, 3),
  43. "import_cont": (", %c", 2),
  44. "kwarg": ("%[0]{attr}=%c", 1),
  45. "raise_stmt2": ("%|raise %c from %c\n", 0, 1),
  46. "tf_tryelsestmtl3": ("%c%-%c%|else:\n%+%c", 1, 3, 5),
  47. "store_locals": ("%|# inspect.currentframe().f_locals = __locals__\n",),
  48. "with": ("%|with %c:\n%+%c%-", 0, 3),
  49. }
  50. )
  51. assert version >= (3, 0)
  52. # In 2.5+ and 3.0+ "except" handlers and the "finally" can appear in one
  53. # "try" statement. So the below has the effect of combining the
  54. # "tryfinally" with statement with the "try_except" statement.
  55. # FIXME: something doesn't smell right, since the semantics
  56. # are different. See test_fileio.py for an example that shows this.
  57. def tryfinallystmt(node):
  58. suite_stmts = node[1][0]
  59. if len(suite_stmts) == 1 and suite_stmts[0] == "stmt":
  60. stmt = suite_stmts[0]
  61. try_something = stmt[0]
  62. if try_something == "try_except":
  63. try_something.kind = "tf_try_except"
  64. if try_something.kind.startswith("tryelsestmt"):
  65. if try_something == "tryelsestmtl3":
  66. try_something.kind = "tf_tryelsestmtl3"
  67. else:
  68. try_something.kind = "tf_tryelsestmt"
  69. self.default(node)
  70. self.n_tryfinallystmt = tryfinallystmt
  71. def n_classdef3(node):
  72. """Handle "classdef" nonterminal for 3.0 >= version 3.0 < 3.6"""
  73. assert (3, 0) <= self.version < (3, 6)
  74. # class definition ('class X(A,B,C):')
  75. cclass = self.currentclass
  76. # Pick out various needed bits of information
  77. # * class_name - the name of the class
  78. # * subclass_info - the parameters to the class e.g.
  79. # class Foo(bar, baz)
  80. # ----------
  81. # * subclass_code - the code for the subclass body
  82. subclass_info = None
  83. if node == "classdefdeco2":
  84. if self.version < (3, 4):
  85. class_name = node[2][0].attr
  86. else:
  87. class_name = node[1][2].attr
  88. build_class = node
  89. else:
  90. build_class = node[0]
  91. class_name = node[1][0].attr
  92. build_class = node[0]
  93. assert "mkfunc" == build_class[1]
  94. mkfunc = build_class[1]
  95. if mkfunc[0] in ("kwargs", "no_kwargs"):
  96. if (3, 0) <= self.version < (3, 3):
  97. for n in mkfunc:
  98. if hasattr(n, "attr") and iscode(n.attr):
  99. subclass_code = n.attr
  100. break
  101. elif n == "expr":
  102. subclass_code = n[0].attr
  103. pass
  104. pass
  105. else:
  106. for n in mkfunc:
  107. if hasattr(n, "attr") and iscode(n.attr):
  108. subclass_code = n.attr
  109. break
  110. pass
  111. pass
  112. if node == "classdefdeco2":
  113. subclass_info = node
  114. else:
  115. subclass_info = node[0]
  116. elif build_class[1][0] == "load_closure":
  117. # Python 3 with closures not functions
  118. load_closure = build_class[1]
  119. if hasattr(load_closure[-3], "attr"):
  120. # Python 3.3 classes with closures work like this.
  121. # Note have to test before 3.2 case because
  122. # index -2 also has an attr.
  123. subclass_code = find_code_node(load_closure, -3).attr
  124. elif hasattr(load_closure[-2], "attr"):
  125. # Python 3.2 works like this
  126. subclass_code = find_code_node(load_closure, -2).attr
  127. else:
  128. raise RuntimeError("Internal Error n_classdef: cannot find class body")
  129. subclass_info = build_class
  130. elif not subclass_info:
  131. if mkfunc[0] in ("no_kwargs", "kwargs"):
  132. subclass_code = mkfunc[1].attr
  133. else:
  134. subclass_code = mkfunc[0].attr
  135. if node == "classdefdeco2":
  136. subclass_info = node
  137. else:
  138. subclass_info = node[0]
  139. if node == "classdefdeco2":
  140. self.write("\n")
  141. else:
  142. self.write("\n\n")
  143. self.currentclass = str(class_name)
  144. self.write(self.indent, "class ", self.currentclass)
  145. self.print_super_classes3(subclass_info)
  146. self.println(":")
  147. # class body
  148. self.indent_more()
  149. self.build_class(subclass_code)
  150. self.indent_less()
  151. self.currentclass = cclass
  152. if len(self.param_stack) > 1:
  153. self.write("\n\n")
  154. else:
  155. self.write("\n\n\n")
  156. self.prune()
  157. self.n_classdef3 = n_classdef3
  158. if version == 3.0:
  159. # In Python 3.0 there is code to move from _[dd] into
  160. # the iteration variable. These rules we can ignore
  161. # since we pick up the iteration variable some other way and
  162. # we definitely don't include in the source _[dd].
  163. self.TABLE_DIRECT.update(
  164. {
  165. "ifstmt30": (
  166. "%|if %c:\n%+%c%-",
  167. (0, "testfalse_then"),
  168. (1, "_ifstmts_jump30"),
  169. ),
  170. "ifnotstmt30": (
  171. "%|if not %c:\n%+%c%-",
  172. (0, "testtrue_then"),
  173. (1, "_ifstmts_jump30"),
  174. ),
  175. "try_except30": (
  176. "%|try:\n%+%c%-%c\n\n",
  177. (1, "suite_stmts_opt"),
  178. (4, "except_handler"),
  179. ),
  180. }
  181. )
  182. def n_comp_iter(node):
  183. if node[0] == "expr":
  184. n = node[0][0]
  185. if n == "LOAD_FAST" and n.pattr[0:2] == "_[":
  186. self.prune()
  187. pass
  188. pass
  189. # Not this special case, proceed as normal...
  190. self.default(node)
  191. self.n_comp_iter = n_comp_iter
  192. elif version == 3.3:
  193. # FIXME: perhaps this can be folded into the 3.4+ case?
  194. def n_yield_from(node):
  195. assert node[0] == "expr"
  196. if node[0][0] == "get_iter":
  197. # Skip over yield_from.expr.get_iter which adds an
  198. # extra iter(). Maybe we can do in transformation phase instead?
  199. template = ("yield from %c", (0, "expr"))
  200. self.template_engine(template, node[0][0])
  201. else:
  202. template = ("yield from %c", (0, "attribute"))
  203. self.template_engine(template, node[0][0][0])
  204. self.prune()
  205. self.n_yield_from = n_yield_from
  206. if (3, 2) <= version <= (3, 4):
  207. def n_call(node):
  208. mapping = self._get_mapping(node)
  209. key = node
  210. for i in mapping[1:]:
  211. key = key[i]
  212. pass
  213. if key.kind.startswith("CALL_FUNCTION_VAR_KW"):
  214. # We may want to fill this in...
  215. # But it is distinct from CALL_FUNCTION_VAR below
  216. pass
  217. elif key.kind.startswith("CALL_FUNCTION_VAR"):
  218. # CALL_FUNCTION_VAR's top element of the stack contains
  219. # the variable argument list, then comes
  220. # annotation args, then keyword args.
  221. # In the most least-top-most stack entry, but position 1
  222. # in node order, the positional args.
  223. argc = node[-1].attr
  224. nargs = argc & 0xFF
  225. kwargs = (argc >> 8) & 0xFF
  226. # FIXME: handle annotation args
  227. if kwargs != 0:
  228. # kwargs == 0 is handled by the table entry
  229. # Should probably handle it here though.
  230. if nargs == 0:
  231. template = ("%c(*%c, %C)", 0, -2, (1, kwargs + 1, ", "))
  232. else:
  233. template = (
  234. "%c(%C, *%c, %C)",
  235. 0,
  236. (1, nargs + 1, ", "),
  237. -2,
  238. (-2 - kwargs, -2, ", "),
  239. )
  240. self.template_engine(template, node)
  241. self.prune()
  242. else:
  243. gen_function_parens_adjust(key, node)
  244. self.default(node)
  245. self.n_call = n_call
  246. elif version < (3, 2):
  247. def n_call(node):
  248. mapping = self._get_mapping(node)
  249. key = node
  250. for i in mapping[1:]:
  251. key = key[i]
  252. pass
  253. gen_function_parens_adjust(key, node)
  254. self.default(node)
  255. self.n_call = n_call
  256. def n_mkfunc_annotate(node):
  257. # Handling EXTENDED_ARG before MAKE_FUNCTION ...
  258. i = -1 if node[-2] == "EXTENDED_ARG" else 0
  259. if self.version < (3, 3):
  260. code_node = node[-2 + i]
  261. elif self.version >= (3, 3) or node[-2] == "kwargs":
  262. # LOAD_CONST code object ..
  263. # LOAD_CONST 'x0' if >= 3.3
  264. # EXTENDED_ARG
  265. # MAKE_FUNCTION ..
  266. code_node = node[-3 + i]
  267. elif node[-3] == "expr":
  268. code_node = node[-3][0]
  269. else:
  270. # LOAD_CONST code object ..
  271. # MAKE_FUNCTION ..
  272. code_node = node[-3]
  273. self.indent_more()
  274. for annotate_last in range(len(node) - 1, -1, -1):
  275. if node[annotate_last] == "annotate_tuple":
  276. break
  277. # FIXME: the real situation is that when derived from
  278. # function_def_annotate we the name has been filled in.
  279. # But when derived from funcdefdeco it hasn't Would like a better
  280. # way to distinguish.
  281. if self.f.getvalue()[-4:] == "def ":
  282. self.write(get_code_name(code_node.attr))
  283. # FIXME: handle and pass full annotate args
  284. make_function3_annotate(
  285. self,
  286. node,
  287. is_lambda=False,
  288. code_node=code_node,
  289. annotate_last=annotate_last,
  290. )
  291. if len(self.param_stack) > 1:
  292. self.write("\n\n")
  293. else:
  294. self.write("\n\n\n")
  295. self.indent_less()
  296. self.prune() # stop recursing
  297. self.n_mkfunc_annotate = n_mkfunc_annotate
  298. self.TABLE_DIRECT.update(
  299. {
  300. "tryelsestmtl3": (
  301. "%|try:\n%+%c%-%c%|else:\n%+%c%-",
  302. (1, "suite_stmts_opt"),
  303. 3, # "except_handler_else" or "except_handler"
  304. (5, "else_suitel"),
  305. ),
  306. "LOAD_CLASSDEREF": ("%{pattr}",),
  307. }
  308. )
  309. if version >= (3, 4):
  310. #######################
  311. # Python 3.4+ Changes #
  312. #######################
  313. self.TABLE_DIRECT.update(
  314. {
  315. "LOAD_CLASSDEREF": ("%{pattr}",),
  316. "yield_from": ("yield from %c", (0, "expr")),
  317. }
  318. )
  319. if version >= (3, 5):
  320. customize_for_version35(self, version)
  321. if version >= (3, 6):
  322. customize_for_version36(self, version)
  323. if version >= (3, 7):
  324. customize_for_version37(self, version)
  325. if version >= (3, 8):
  326. customize_for_version38(self, version)
  327. pass # version >= 3.8
  328. pass # 3.7
  329. pass # 3.6
  330. pass # 3.5
  331. pass # 3.4
  332. return