You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 01:09:52 +08:00
more deparse_code -> code_deparse API additions
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from uncompyle6.semantics.fragments import deparse_code as deparse, deparsed_find
|
from uncompyle6.semantics.fragments import code_deparse as deparse, deparsed_find
|
||||||
from uncompyle6 import PYTHON_VERSION, PYTHON3
|
from uncompyle6 import PYTHON_VERSION, PYTHON3
|
||||||
|
|
||||||
def map_stmts(x, y):
|
def map_stmts(x, y):
|
||||||
@@ -31,12 +31,13 @@ def list_comp():
|
|||||||
|
|
||||||
def get_parsed_for_fn(fn):
|
def get_parsed_for_fn(fn):
|
||||||
code = fn.__code__ if PYTHON3 else fn.func_code
|
code = fn.__code__ if PYTHON3 else fn.func_code
|
||||||
return deparse(PYTHON_VERSION, code)
|
return deparse(code, version=PYTHON_VERSION)
|
||||||
|
|
||||||
def check_expect(expect, parsed, fn_name):
|
def check_expect(expect, parsed, fn_name):
|
||||||
debug = False
|
debug = False
|
||||||
i = 2
|
i = 2
|
||||||
max_expect = len(expect)
|
max_expect = len(expect)
|
||||||
|
code = get_parsed_for_fn(fn_name)
|
||||||
for name, offset in sorted(parsed.offsets.keys()):
|
for name, offset in sorted(parsed.offsets.keys()):
|
||||||
assert i+1 <= max_expect, (
|
assert i+1 <= max_expect, (
|
||||||
"%s: ran out if items in testing node" % fn_name)
|
"%s: ran out if items in testing node" % fn_name)
|
||||||
|
@@ -1,5 +1,18 @@
|
|||||||
# Copyright (c) 2018 by Rocky Bernstein
|
# Copyright (c) 2018 by Rocky Bernstein
|
||||||
from uncompyle6.semantics.pysource import SourceWalker, deparse_code
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
from uncompyle6.semantics.pysource import SourceWalker, code_deparse
|
||||||
import uncompyle6.semantics.fragments as fragments
|
import uncompyle6.semantics.fragments as fragments
|
||||||
|
|
||||||
# FIXME: does this handle nested code, and lambda properly
|
# FIXME: does this handle nested code, and lambda properly
|
||||||
@@ -46,42 +59,49 @@ class LineMapFragmentWalker(fragments.FragmentsWalker, LineMapWalker):
|
|||||||
def deparse_code_with_map(*args, **kwargs):
|
def deparse_code_with_map(*args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Like deparse_code but saves line number correspondences.
|
Like deparse_code but saves line number correspondences.
|
||||||
|
Deprecated. Use code_deparse_with_map
|
||||||
"""
|
"""
|
||||||
kwargs['walker'] = LineMapWalker
|
kwargs['walker'] = LineMapWalker
|
||||||
return deparse_code(*args, **kwargs)
|
return code_deparse(*args, **kwargs)
|
||||||
|
|
||||||
|
def code_deparse_with_map(*args, **kwargs):
|
||||||
|
"""
|
||||||
|
Like code_deparse but saves line number correspondences.
|
||||||
|
"""
|
||||||
|
kwargs['walker'] = LineMapWalker
|
||||||
|
return code_deparse(*args, **kwargs)
|
||||||
|
|
||||||
def deparse_code_with_fragments_and_map(*args, **kwargs):
|
def deparse_code_with_fragments_and_map(*args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Like deparse_code_with_map but saves fragments.
|
Like deparse_code_with_map but saves fragments.
|
||||||
|
Deprecated. Use code_deparse_with_fragments_and_map
|
||||||
"""
|
"""
|
||||||
kwargs['walker'] = LineMapFragmentWalker
|
kwargs['walker'] = LineMapFragmentWalker
|
||||||
return fragments.deparse_code(*args, **kwargs)
|
return fragments.deparse_code(*args, **kwargs)
|
||||||
|
|
||||||
|
def code_deparse_with_fragments_and_map(*args, **kwargs):
|
||||||
|
"""
|
||||||
|
Like code_deparse_with_map but saves fragments.
|
||||||
|
"""
|
||||||
|
kwargs['walker'] = LineMapFragmentWalker
|
||||||
|
return fragments.code_deparse(*args, **kwargs)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
def deparse_test(co):
|
def deparse_test(co):
|
||||||
"This is a docstring"
|
"This is a docstring"
|
||||||
import sys
|
deparsed = code_deparse_with_map(co)
|
||||||
sys_version = float(sys.version[0:3])
|
|
||||||
# deparsed = deparse_code(sys_version, co, showasm=True, showast=True)
|
|
||||||
deparsed = deparse_code_with_map(sys_version, co, showasm=False,
|
|
||||||
showast=False,
|
|
||||||
showgrammar=False)
|
|
||||||
a = 1; b = 2
|
a = 1; b = 2
|
||||||
print("\n")
|
print("\n")
|
||||||
linemap = [(line_no, deparsed.source_linemap[line_no])
|
linemap = [(line_no, deparsed.source_linemap[line_no])
|
||||||
for line_no in
|
for line_no in
|
||||||
sorted(deparsed.source_linemap.keys())]
|
sorted(deparsed.source_linemap.keys())]
|
||||||
print(linemap)
|
print(linemap)
|
||||||
deparsed = deparse_code_with_fragments_and_map(sys_version,
|
deparsed = code_deparse_with_fragments_and_map(co)
|
||||||
co, showasm=False,
|
|
||||||
showast=False,
|
|
||||||
showgrammar=False)
|
|
||||||
a = 1; b = 2
|
|
||||||
print("\n")
|
print("\n")
|
||||||
linemap2 = [(line_no, deparsed.source_linemap[line_no])
|
linemap2 = [(line_no, deparsed.source_linemap[line_no])
|
||||||
for line_no in
|
for line_no in
|
||||||
sorted(deparsed.source_linemap.keys())]
|
sorted(deparsed.source_linemap.keys())]
|
||||||
print(linemap2)
|
print(linemap2)
|
||||||
assert linemap == linemap2
|
# assert linemap == linemap2
|
||||||
return
|
return
|
||||||
deparse_test(deparse_test.__code__)
|
deparse_test(deparse_test.__code__)
|
||||||
|
@@ -1,5 +1,18 @@
|
|||||||
# Copyright (c) 2015-2018 by Rocky Bernstein
|
# Copyright (c) 2015-2018 by Rocky Bernstein
|
||||||
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
|
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
All the crazy things we have to do to handle Python functions
|
All the crazy things we have to do to handle Python functions
|
||||||
"""
|
"""
|
||||||
|
@@ -1,3 +1,17 @@
|
|||||||
|
# Copyright (c) 2018 by Rocky Bernstein
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
import uncompyle6.parser as python_parser
|
import uncompyle6.parser as python_parser
|
||||||
class ParserError(python_parser.ParserError):
|
class ParserError(python_parser.ParserError):
|
||||||
def __init__(self, error, tokens):
|
def __init__(self, error, tokens):
|
||||||
|
@@ -2712,7 +2712,7 @@ def code_deparse(co, out=sys.stdout, version=None, debug_opts=DEFAULT_DEBUG_OPTS
|
|||||||
|
|
||||||
# Build Syntax Tree from disassembly.
|
# Build Syntax Tree from disassembly.
|
||||||
linestarts = dict(scanner.opc.findlinestarts(co))
|
linestarts = dict(scanner.opc.findlinestarts(co))
|
||||||
deparsed = walker(version, out, scanner, showast=debug_opts['ast'],
|
deparsed = walker(version, out, scanner, showast=debug_opts.get('ast', None),
|
||||||
debug_parser=debug_parser, compile_mode=compile_mode,
|
debug_parser=debug_parser, compile_mode=compile_mode,
|
||||||
is_pypy=is_pypy, linestarts=linestarts)
|
is_pypy=is_pypy, linestarts=linestarts)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user