Start changing API to make version optional...

and use debug option dictionary
This commit is contained in:
rocky
2018-02-27 11:42:29 -05:00
parent 55a10d9e20
commit 68692abaf6
3 changed files with 40 additions and 12 deletions

View File

@@ -55,7 +55,10 @@ from uncompyle6.main import decompile_file
uncompyle_file = decompile_file
# Conventience functions so you can say:
# from uncompyle6 import (deparse_code, deparse_code2str)
# from uncompyle6 import (code_deparse, deparse_code2str)
deparse_code = uncompyle6.semantics.pysource.deparse_code
code_deparse = uncompyle6.semantics.pysource.code_deparse
deparse_code2str = uncompyle6.semantics.pysource.deparse_code2str
# This is deprecated:
deparse_code = uncompyle6.semantics.pysource.deparse_code

View File

@@ -1,4 +1,17 @@
# Copyright (c) 2015-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/>.
"""
Creates Python source code from an uncompyle6 parse tree,

View File

@@ -2630,9 +2630,27 @@ class SourceWalker(GenericASTTraversal, object):
return MAP.get(node, MAP_DIRECT)
#
DEFAULT_DEBUG_OPTS = {
'asm': False,
'tree': False,
'grammar': False
}
# This interface is deprecated. Use simpler code_deparse.
def deparse_code(version, co, out=sys.stdout, showasm=None, showast=False,
showgrammar=False, code_objects={}, compile_mode='exec',
is_pypy=False, walker=SourceWalker):
debug_opts = {
'asm': showasm,
'ast': showast,
'grammar': showgrammar
}
return code_deparse(co, out, version, debug_opts, code_objects, compile_mode,
is_pypy, walker)
def code_deparse(co, out=sys.stdout, version=None, debug_opts=DEFAULT_DEBUG_OPTS,
code_objects={}, compile_mode='exec', is_pypy=False, walker=SourceWalker):
"""
ingests and deparses a given code block 'co'. If version is None,
we will use the current Python interpreter version.
@@ -2647,16 +2665,16 @@ def deparse_code(version, co, out=sys.stdout, showasm=None, showast=False,
scanner = get_scanner(version, is_pypy=is_pypy)
tokens, customize = scanner.ingest(co, code_objects=code_objects,
show_asm=showasm)
show_asm=debug_opts['asm'])
debug_parser = dict(PARSER_DEFAULT_DEBUG)
if showgrammar:
debug_parser['reduce'] = showgrammar
if debug_opts.get('grammar', None):
debug_parser['reduce'] = debug_opts['grammar']
debug_parser['errorstack'] = 'full'
# Build Syntax Tree from disassembly.
linestarts = dict(scanner.opc.findlinestarts(co))
deparsed = walker(version, out, scanner, showast=showast,
deparsed = walker(version, out, scanner, showast=debug_opts['ast'],
debug_parser=debug_parser, compile_mode=compile_mode,
is_pypy=is_pypy, linestarts=linestarts)
@@ -2705,12 +2723,6 @@ def deparse_code(version, co, out=sys.stdout, showasm=None, showast=False,
raise SourceWalkerError("Deparsing stopped due to parse error")
return deparsed
#
DEFAULT_DEBUG_OPTS = {
'asm': False,
'tree': False,
'grammar': False
}
def deparse_code2str(code, out=sys.stdout, version=None,
debug_opts=DEFAULT_DEBUG_OPTS,
code_objects={}, compile_mode='exec',