Start simplifying higher-level API

This commit is contained in:
rocky
2018-02-27 17:48:26 -05:00
parent 68692abaf6
commit b6d96929cb
3 changed files with 61 additions and 34 deletions

View File

@@ -1,4 +1,4 @@
This project has history of over 17 years spanning back to Python 1.5 This project has history of over 18 years spanning back to Python 1.5
There have been a number of people who have worked on this. I am awed There have been a number of people who have worked on this. I am awed
by the amount of work, number of people who have contributed to this, by the amount of work, number of people who have contributed to this,

View File

@@ -23,8 +23,8 @@ from uncompyle6.parser import ParserError
from uncompyle6.version import VERSION from uncompyle6.version import VERSION
# from uncompyle6.linenumbers import line_number_mapping # from uncompyle6.linenumbers import line_number_mapping
from uncompyle6.semantics.pysource import deparse_code from uncompyle6.semantics.pysource import code_deparse
from uncompyle6.semantics.fragments import deparse_code as deparse_code_fragments from uncompyle6.semantics.fragments import code_deparse as code_deparse_fragments
from uncompyle6.semantics.linemap import deparse_code_with_map from uncompyle6.semantics.linemap import deparse_code_with_map
from xdis.load import load_module from xdis.load import load_module
@@ -43,7 +43,7 @@ def _get_outstream(outfile):
def decompile( def decompile(
bytecode_version, co, out=None, showasm=None, showast=False, bytecode_version, co, out=None, showasm=None, showast=False,
timestamp=None, showgrammar=False, code_objects={}, timestamp=None, showgrammar=False, code_objects={},
source_size=None, is_pypy=False, magic_int=None, source_size=None, is_pypy=None, magic_int=None,
mapstream=None, do_fragments=False): mapstream=None, do_fragments=False):
""" """
ingests and deparses a given code block 'co' ingests and deparses a given code block 'co'
@@ -74,6 +74,12 @@ def decompile(
if source_size: if source_size:
write('# Size of source mod 2**32: %d bytes' % source_size) write('# Size of source mod 2**32: %d bytes' % source_size)
debug_opts = {
'asm': showasm,
'ast': showast,
'grammar': showgrammar
}
try: try:
if mapstream: if mapstream:
if isinstance(mapstream, str): if isinstance(mapstream, str):
@@ -91,12 +97,12 @@ def decompile(
mapstream.write("\n\n# %s\n" % linemap) mapstream.write("\n\n# %s\n" % linemap)
else: else:
if do_fragments: if do_fragments:
deparse_fn = deparse_code_fragments deparse_fn = code_deparse_fragments
else: else:
deparse_fn = deparse_code deparse_fn = code_deparse
deparsed = deparse_fn(bytecode_version, co, out, showasm, showast, deparsed = deparse_fn(co, out, bytecode_version,
showgrammar, code_objects=code_objects, debug_opts = debug_opts,
is_pypy=is_pypy) is_pypy=is_pypy)
pass pass
return deparsed return deparsed
except pysource.SourceWalkerError as e: except pysource.SourceWalkerError as e:

View File

@@ -73,6 +73,7 @@ from uncompyle6 import parser
from uncompyle6.scanner import Token, Code, get_scanner from uncompyle6.scanner import Token, Code, get_scanner
import uncompyle6.parser as python_parser import uncompyle6.parser as python_parser
from uncompyle6.semantics.check_ast import checker from uncompyle6.semantics.check_ast import checker
from uncompyle6 import IS_PYPY
from uncompyle6.show import ( from uncompyle6.show import (
maybe_show_asm, maybe_show_asm,
@@ -1717,9 +1718,29 @@ class FragmentsWalker(pysource.SourceWalker, object):
pass pass
#
DEFAULT_DEBUG_OPTS = {
'asm': False,
'tree': False,
'grammar': False
}
# This interface is deprecated
def deparse_code(version, co, out=StringIO(), showasm=False, showast=False, def deparse_code(version, co, out=StringIO(), showasm=False, showast=False,
showgrammar=False, code_objects={}, compile_mode='exec', showgrammar=False, code_objects={}, compile_mode='exec',
is_pypy=False, walker=FragmentsWalker): is_pypy=None, walker=FragmentsWalker):
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=StringIO(), version=None, is_pypy=None,
debug_opts=DEFAULT_DEBUG_OPTS,
code_objects={}, compile_mode='exec',
walker=FragmentsWalker):
""" """
Convert the code object co into a python source fragment. Convert the code object co into a python source fragment.
@@ -1727,40 +1748,44 @@ def deparse_code(version, co, out=StringIO(), showasm=False, showast=False,
example 2.6, 2.7, 3.2, 3.3, 3.4, 3.5 etc. example 2.6, 2.7, 3.2, 3.3, 3.4, 3.5 etc.
:param co: The code object to parse. :param co: The code object to parse.
:param out: File like object to write the output to. :param out: File like object to write the output to.
:param showasm: Flag which determines whether the ingestd code :param debug_opts: A dictionary with keys
is written to sys.stdout or not. (It is also to 'asm': value determines whether to show
pass a file like object, into which the asm will be mangled bytecode disdassembly
written). 'ast': value determines whether to show
:param showast: Flag which determines whether the constructed 'grammar': boolean determining whether to show
parse tree is written to sys.stdout or grammar reduction rules.
not. (It is also to pass a file like object, into If value is a file-like object, output that object's write method will
which the ast will be written). be used rather than sys.stdout
:param showgrammar: Flag which determines whether the grammar reduction rules
is written to sys.stdout or not. (It is also to
pass a file like object, into which the grammar
will be written).
:return: The deparsed source fragment. :return: The deparsed source fragment.
""" """
assert iscode(co) assert iscode(co)
# store final output stream for case of error
if version is None:
version = float(sys.version[0:3])
if is_pypy is None:
is_pypy = IS_PYPY
scanner = get_scanner(version, is_pypy=is_pypy) scanner = get_scanner(version, is_pypy=is_pypy)
show_asm = debug_opts.get('asm', None)
tokens, customize = scanner.ingest(co, code_objects=code_objects, tokens, customize = scanner.ingest(co, code_objects=code_objects,
show_asm=showasm) show_asm=show_asm)
tokens, customize = scanner.ingest(co) tokens, customize = scanner.ingest(co)
maybe_show_asm(showasm, tokens) maybe_show_asm(show_asm, tokens)
debug_parser = dict(PARSER_DEFAULT_DEBUG) debug_parser = dict(PARSER_DEFAULT_DEBUG)
if showgrammar: show_grammar = debug_opts.get('grammar', None)
debug_parser['reduce'] = showgrammar if show_grammar:
debug_parser['reduce'] = show_grammar
debug_parser['errorstack'] = True debug_parser['errorstack'] = True
# Build Syntax Tree from tokenized and massaged disassembly. # Build Syntax Tree from tokenized and massaged disassembly.
# deparsed = pysource.FragmentsWalker(out, scanner, showast=showast) # deparsed = pysource.FragmentsWalker(out, scanner, showast=showast)
deparsed = walker(version, scanner, showast=showast, show_ast = debug_opts.get('ast', None)
deparsed = walker(version, scanner, showast=show_ast,
debug_parser=debug_parser, compile_mode=compile_mode, debug_parser=debug_parser, compile_mode=compile_mode,
is_pypy=is_pypy) is_pypy=is_pypy)
@@ -1870,12 +1895,8 @@ def deparsed_find(tup, deparsed, code):
# if __name__ == '__main__': # if __name__ == '__main__':
# from uncompyle6 import IS_PYPY
# def deparse_test(co, is_pypy=IS_PYPY): # def deparse_test(co, is_pypy=IS_PYPY):
# from xdis.magics import sysinfo2float # deparsed = code_deparse(co, is_pypy=IS_PYPY)
# float_version = sysinfo2float()
# deparsed = deparse_code(float_version, co, showasm=False, showast=False,
# showgrammar=False, is_pypy=IS_PYPY)
# print("deparsed source") # print("deparsed source")
# print(deparsed.text, "\n") # print(deparsed.text, "\n")
# print('------------------------') # print('------------------------')