From 5d8c40358eb9aeedd475985b61930bd875be82f3 Mon Sep 17 00:00:00 2001 From: rocky Date: Sat, 10 Feb 2024 16:29:59 -0500 Subject: [PATCH] showtree workaround until we have better sync.. with decompyle3 --- uncompyle6/semantics/transform.py | 43 ++++++++++++++++++------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/uncompyle6/semantics/transform.py b/uncompyle6/semantics/transform.py index e3f96c83e..0339d8af5 100644 --- a/uncompyle6/semantics/transform.py +++ b/uncompyle6/semantics/transform.py @@ -13,14 +13,15 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from uncompyle6.show import maybe_show_tree from copy import copy + from spark_parser import GenericASTTraversal, GenericASTTraversalPruningException -from uncompyle6.semantics.helper import find_code_node from uncompyle6.parsers.treenode import SyntaxTree from uncompyle6.scanners.tok import NoneToken, Token -from uncompyle6.semantics.consts import RETURN_NONE, ASSIGN_DOC_STRING +from uncompyle6.semantics.consts import ASSIGN_DOC_STRING, RETURN_NONE +from uncompyle6.semantics.helper import find_code_node +from uncompyle6.show import maybe_show_tree def is_docstring(node, version, co_consts): @@ -74,7 +75,9 @@ def __init__(self, version, show_ast=None, is_pypy=False): return def maybe_show_tree(self, ast): - if isinstance(self.showast, dict) and self.showast: + if isinstance(self.showast, dict) and ( + self.showast.get("before") or self.showast.get("after") + ): maybe_show_tree(self, ast) def preorder(self, node=None): @@ -121,10 +124,7 @@ def n_mkfunc(self, node): if isinstance(mkfunc_pattr, tuple): assert len(mkfunc_pattr) == 4 and isinstance(mkfunc_pattr, int) - if ( - len(code.co_consts) > 0 - and isinstance(code.co_consts[0], str) - ): + if len(code.co_consts) > 0 and isinstance(code.co_consts[0], str): docstring_node = SyntaxTree( "docstring", [Token("LOAD_STR", has_arg=True, pattr=code.co_consts[0])] ) @@ -136,7 +136,7 @@ def n_mkfunc(self, node): def n_ifstmt(self, node): """Here we check if we can turn an `ifstmt` or 'iflaststmtl` into - some kind of `assert` statement""" + some kind of `assert` statement""" testexpr = node[0] @@ -148,7 +148,11 @@ def n_ifstmt(self, node): if ifstmts_jump == "_ifstmts_jumpl" and ifstmts_jump[0] == "_ifstmts_jump": ifstmts_jump = ifstmts_jump[0] - elif ifstmts_jump not in ("_ifstmts_jump", "_ifstmts_jumpl", "ifstmts_jumpl"): + elif ifstmts_jump not in ( + "_ifstmts_jump", + "_ifstmts_jumpl", + "ifstmts_jumpl", + ): return node stmts = ifstmts_jump[0] else: @@ -208,7 +212,7 @@ def n_ifstmt(self, node): kind = "assert2not" LOAD_ASSERT = call[0].first_child() - if LOAD_ASSERT not in ( "LOAD_ASSERT", "LOAD_GLOBAL"): + if LOAD_ASSERT not in ("LOAD_ASSERT", "LOAD_GLOBAL"): return node if isinstance(call[1], SyntaxTree): expr = call[1][0] @@ -289,7 +293,12 @@ def n_ifelsestmt(self, node, preprocess=False): len_n = len(n) # Sometimes stmt is reduced away and n[0] can be a single reduction like continue -> CONTINUE. - if len_n == 1 and isinstance(n[0], SyntaxTree) and len(n[0]) == 1 and n[0] == "stmt": + if ( + len_n == 1 + and isinstance(n[0], SyntaxTree) + and len(n[0]) == 1 + and n[0] == "stmt" + ): n = n[0][0] elif len_n == 0: return node @@ -413,17 +422,15 @@ def n_stmts(self, node): new_stmts = [node[0]] for i, sstmt in enumerate(node[1:]): ann_assign = sstmt[0] - if ( - ann_assign == "ann_assign" - and prev == "assign" - ): + if ann_assign == "ann_assign" and prev == "assign": annotate_var = ann_assign[-2] if annotate_var.attr == prev[-1][0].attr: node[i].kind = "deleted " + node[i].kind del new_stmts[-1] ann_assign_init = SyntaxTree( - "ann_assign_init", [ann_assign[0], copy(prev[0]), annotate_var] - ) + "ann_assign_init", + [ann_assign[0], copy(prev[0]), annotate_var], + ) if sstmt[0] == "ann_assign": sstmt[0] = ann_assign_init else: