more deparse_code -> code_deparse API additions

This commit is contained in:
rocky
2018-03-01 16:55:45 -05:00
parent ef076c065b
commit 2edc757b6f
5 changed files with 65 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
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
def map_stmts(x, y):
@@ -31,12 +31,13 @@ def list_comp():
def get_parsed_for_fn(fn):
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):
debug = False
i = 2
max_expect = len(expect)
code = get_parsed_for_fn(fn_name)
for name, offset in sorted(parsed.offsets.keys()):
assert i+1 <= max_expect, (
"%s: ran out if items in testing node" % fn_name)

View File

@@ -1,5 +1,18 @@
# 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
# 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):
"""
Like deparse_code but saves line number correspondences.
Deprecated. Use code_deparse_with_map
"""
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):
"""
Like deparse_code_with_map but saves fragments.
Deprecated. Use code_deparse_with_fragments_and_map
"""
kwargs['walker'] = LineMapFragmentWalker
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__':
def deparse_test(co):
"This is a docstring"
import sys
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)
deparsed = code_deparse_with_map(co)
a = 1; b = 2
print("\n")
linemap = [(line_no, deparsed.source_linemap[line_no])
for line_no in
sorted(deparsed.source_linemap.keys())]
print(linemap)
deparsed = deparse_code_with_fragments_and_map(sys_version,
co, showasm=False,
showast=False,
showgrammar=False)
a = 1; b = 2
deparsed = code_deparse_with_fragments_and_map(co)
print("\n")
linemap2 = [(line_no, deparsed.source_linemap[line_no])
for line_no in
sorted(deparsed.source_linemap.keys())]
print(linemap2)
assert linemap == linemap2
# assert linemap == linemap2
return
deparse_test(deparse_test.__code__)

View File

@@ -1,5 +1,18 @@
# Copyright (c) 2015-2018 by Rocky Bernstein
# 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
"""

View File

@@ -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
class ParserError(python_parser.ParserError):
def __init__(self, error, tokens):

View File

@@ -2712,7 +2712,7 @@ def code_deparse(co, out=sys.stdout, version=None, debug_opts=DEFAULT_DEBUG_OPTS
# Build Syntax Tree from disassembly.
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,
is_pypy=is_pypy, linestarts=linestarts)