How to use the uncompyle6.parsers.treenode.SyntaxTree function in uncompyle6

To help you get started, we’ve selected a few uncompyle6 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rocky / python-uncompyle6 / uncompyle6 / semantics / fragments.py View on Github external
def n_return(self, node):
        start = len(self.f.getvalue()) + len(self.indent)
        if self.params["is_lambda"]:
            self.preorder(node[0])
            if hasattr(node[-1], "offset"):
                self.set_pos_info(node[-1], start, len(self.f.getvalue()))
            self.prune()
        else:
            start = len(self.f.getvalue()) + len(self.indent)
            self.write(self.indent, "return")
            if self.return_none or node != SyntaxTree(
                "return", [SyntaxTree("ret_expr", [NONE]), Token("RETURN_VALUE")]
            ):
                self.write(" ")
                self.last_finish = len(self.f.getvalue())
                self.preorder(node[0])
                if hasattr(node[-1], "offset"):
                    self.set_pos_info(node[-1], start, len(self.f.getvalue()))
                    pass
                pass
            else:
                for n in node:
                    self.set_pos_info_recurse(n, start, len(self.f.getvalue()))
                    pass
                pass
            self.set_pos_info(node, start, len(self.f.getvalue()))
            self.println()
github rocky / python-uncompyle6 / uncompyle6 / semantics / transform.py View on Github external
tree. When generating the source string that AST node rather
        than the code field is seen and used.
        """

        code = find_code_node(node, -2).attr

        if (
            node[-1].pattr != "closure"
            and len(code.co_consts) > 0
            and code.co_consts[0] is not None
        ):
            docstring_node = SyntaxTree(
                "docstring", [Token("LOAD_STR", has_arg=True, pattr=code.co_consts[0])]
            )
            docstring_node.transformed_by = "n_mkfunc"
            node = SyntaxTree("mkfunc", node[:-1] + [docstring_node, node[-1]])
            node.transformed_by = "n_mkfunc"

        return node
github rocky / python-uncompyle6 / uncompyle6 / semantics / consts.py View on Github external
'list_comp':              0,
    'set_comp':               0,
    'set_comp_expr':          0,
    'unary_convert':          0,
}

LINE_LENGTH = 80

# Some parse trees created below are used for comparing code
# fragments (like 'return None' at the end of functions).

RETURN_LOCALS = SyntaxTree('return',
                    [ SyntaxTree('ret_expr', [SyntaxTree('expr', [ Token('LOAD_LOCALS') ])]),
                      Token('RETURN_VALUE')])

NONE = SyntaxTree('expr', [ NoneToken ] )

RETURN_NONE = SyntaxTree('stmt',
                  [ SyntaxTree('return',
                        [ NONE, Token('RETURN_VALUE')]) ])

PASS = SyntaxTree('stmts',
           [ SyntaxTree('sstmt',
                 [ SyntaxTree('stmt',
                       [ SyntaxTree('pass', [])])])])

ASSIGN_DOC_STRING = lambda doc_string, doc_load: \
  SyntaxTree('stmt',
      [ SyntaxTree('assign',
            [ SyntaxTree('expr', [ Token(doc_load, pattr=doc_string, attr=doc_string) ]),
              SyntaxTree('store', [ Token('STORE_NAME', pattr='__doc__')])
            ])])
github rocky / python-uncompyle6 / uncompyle6 / semantics / pysource.py View on Github external
def is_return_none(self, node):
        # Is there a better way?
        ret = (
            node[0] == "ret_expr"
            and node[0][0] == "expr"
            and node[0][0][0] == "LOAD_CONST"
            and node[0][0][0].pattr is None
        )
        if self.version <= 2.6:
            return ret
        else:
            # FIXME: should the SyntaxTree expression be folded into
            # the global RETURN_NONE constant?
            return ret or node == SyntaxTree(
                "return", [SyntaxTree("ret_expr", [NONE]), Token("RETURN_VALUE")]
            )
github rocky / python-uncompyle6 / uncompyle6 / semantics / consts.py View on Github external
LINE_LENGTH = 80

# Some parse trees created below are used for comparing code
# fragments (like 'return None' at the end of functions).

RETURN_LOCALS = SyntaxTree('return',
                    [ SyntaxTree('ret_expr', [SyntaxTree('expr', [ Token('LOAD_LOCALS') ])]),
                      Token('RETURN_VALUE')])

NONE = SyntaxTree('expr', [ NoneToken ] )

RETURN_NONE = SyntaxTree('stmt',
                  [ SyntaxTree('return',
                        [ NONE, Token('RETURN_VALUE')]) ])

PASS = SyntaxTree('stmts',
           [ SyntaxTree('sstmt',
                 [ SyntaxTree('stmt',
                       [ SyntaxTree('pass', [])])])])

ASSIGN_DOC_STRING = lambda doc_string, doc_load: \
  SyntaxTree('stmt',
      [ SyntaxTree('assign',
            [ SyntaxTree('expr', [ Token(doc_load, pattr=doc_string, attr=doc_string) ]),
              SyntaxTree('store', [ Token('STORE_NAME', pattr='__doc__')])
            ])])

NAME_MODULE = SyntaxTree('stmt',
                [ SyntaxTree('assign',
                    [ SyntaxTree('expr',
                          [Token('LOAD_NAME', pattr='__name__', offset=0, has_arg=True)]),
                      SyntaxTree('store',
github rocky / python-uncompyle6 / uncompyle6 / semantics / consts.py View on Github external
PASS = SyntaxTree('stmts',
           [ SyntaxTree('sstmt',
                 [ SyntaxTree('stmt',
                       [ SyntaxTree('pass', [])])])])

ASSIGN_DOC_STRING = lambda doc_string, doc_load: \
  SyntaxTree('stmt',
      [ SyntaxTree('assign',
            [ SyntaxTree('expr', [ Token(doc_load, pattr=doc_string, attr=doc_string) ]),
              SyntaxTree('store', [ Token('STORE_NAME', pattr='__doc__')])
            ])])

NAME_MODULE = SyntaxTree('stmt',
                [ SyntaxTree('assign',
                    [ SyntaxTree('expr',
                          [Token('LOAD_NAME', pattr='__name__', offset=0, has_arg=True)]),
                      SyntaxTree('store',
                          [ Token('STORE_NAME', pattr='__module__', offset=3, has_arg=True)])
                      ])])

# God intended \t, but Python has decided to use 4 spaces.
# If you want real tabs, use Go.
# TAB = '\t'
TAB = ' ' * 4
INDENT_PER_LEVEL = ' ' # additional intent per pretty-print level

TABLE_R = {
    'STORE_ATTR':	( '%c.%[1]{pattr}', 0),
    'DELETE_ATTR':	( '%|del %c.%[-1]{pattr}\n', 0 ),
}
github rocky / python-uncompyle6 / uncompyle6 / parsers / treenode.py View on Github external
def __init__(self, *args, **kwargs):
        super(SyntaxTree, self).__init__(*args, **kwargs)
        self.transformed_by = None
github rocky / python-uncompyle6 / uncompyle6 / semantics / consts.py View on Github external
[ NONE, Token('RETURN_VALUE')]) ])

PASS = SyntaxTree('stmts',
           [ SyntaxTree('sstmt',
                 [ SyntaxTree('stmt',
                       [ SyntaxTree('pass', [])])])])

ASSIGN_DOC_STRING = lambda doc_string, doc_load: \
  SyntaxTree('stmt',
      [ SyntaxTree('assign',
            [ SyntaxTree('expr', [ Token(doc_load, pattr=doc_string, attr=doc_string) ]),
              SyntaxTree('store', [ Token('STORE_NAME', pattr='__doc__')])
            ])])

NAME_MODULE = SyntaxTree('stmt',
                [ SyntaxTree('assign',
                    [ SyntaxTree('expr',
                          [Token('LOAD_NAME', pattr='__name__', offset=0, has_arg=True)]),
                      SyntaxTree('store',
                          [ Token('STORE_NAME', pattr='__module__', offset=3, has_arg=True)])
                      ])])

# God intended \t, but Python has decided to use 4 spaces.
# If you want real tabs, use Go.
# TAB = '\t'
TAB = ' ' * 4
INDENT_PER_LEVEL = ' ' # additional intent per pretty-print level

TABLE_R = {
    'STORE_ATTR':	( '%c.%[1]{pattr}', 0),
    'DELETE_ATTR':	( '%|del %c.%[-1]{pattr}\n', 0 ),
}