Use L. for line number prefix in asm and AST

This commit is contained in:
rocky 2016-11-03 21:26:12 -04:00
parent 2eaea447eb
commit cd3cf5ec29
6 changed files with 15 additions and 12 deletions

View File

@ -96,7 +96,7 @@ def main_bin():
elif opt == '--verify':
options['do_verify'] = True
elif opt in ('--asm', '-a'):
options['showasm'] = True
options['showasm'] = 'after'
options['do_verify'] = False
elif opt in ('--tree', '-t'):
options['showast'] = True

View File

@ -11,7 +11,7 @@ from uncompyle6.version import VERSION
from xdis.load import load_module
def uncompyle(
bytecode_version, co, out=None, showasm=False, showast=False,
bytecode_version, co, out=None, showasm=None, showast=False,
timestamp=None, showgrammar=False, code_objects={},
source_size=None, is_pypy=False, magic_int=None):
"""
@ -53,7 +53,7 @@ def uncompyle(
def uncompyle_file(filename, outstream=None, showasm=False, showast=False,
def uncompyle_file(filename, outstream=None, showasm=None, showast=False,
showgrammar=False):
"""
decompile Python byte-code file (.pyc)
@ -79,7 +79,7 @@ def uncompyle_file(filename, outstream=None, showasm=False, showast=False,
# FIXME: combine into an options parameter
def main(in_base, out_base, files, codes, outfile=None,
showasm=False, showast=False, do_verify=False,
showasm=None, showast=False, do_verify=False,
showgrammar=False, raise_on_error=False):
"""
in_base base directory for input files

View File

@ -33,7 +33,7 @@ class AST(spark_AST):
else:
child = node.__repr1__(indent, None)
else:
inst = str(node)
inst = node.format(line_prefix='L.')
if inst.startswith("\n"):
# Nuke leading \n
inst = inst[1:]

View File

@ -291,7 +291,7 @@ class Scanner2(scan.Scanner):
if show_asm in ('both', 'after'):
for t in tokens:
print(t)
print(t.format(line_prefix='L.'))
print()
return tokens, customize

View File

@ -53,7 +53,11 @@ class Token:
# ('%9s %-18s %r' % (self.offset, self.type, pattr)))
def __str__(self):
prefix = '\n%4d ' % self.linestart if self.linestart else (' ' * 6)
return self.format(line_prefix='')
def format(self, line_prefix=''):
prefix = ('\n%s%4d ' % (line_prefix, self.linestart)
if self.linestart else (' ' * (6 + len(line_prefix))))
offset_opname = '%6s %-17s' % (self.offset, self.type)
if not self.has_arg:
return "%s%s" % (prefix, offset_opname)

View File

@ -2264,7 +2264,7 @@ class SourceWalker(GenericASTTraversal, object):
return MAP.get(node, MAP_DIRECT)
def deparse_code(version, co, out=sys.stdout, showasm=False, showast=False,
def deparse_code(version, co, out=sys.stdout, showasm=None, showast=False,
showgrammar=False, code_objects={}, compile_mode='exec', is_pypy=False):
"""
ingests and deparses a given code block 'co'
@ -2274,8 +2274,7 @@ def deparse_code(version, co, out=sys.stdout, showasm=False, showast=False,
# store final output stream for case of error
scanner = get_scanner(version, is_pypy=is_pypy)
tokens, customize = scanner.ingest(co, code_objects=code_objects)
maybe_show_asm(showasm, tokens)
tokens, customize = scanner.ingest(co, code_objects=code_objects, show_asm=showasm)
debug_parser = dict(PARSER_DEFAULT_DEBUG)
if showgrammar:
@ -2323,8 +2322,8 @@ if __name__ == '__main__':
def deparse_test(co):
"This is a docstring"
sys_version = sys.version_info.major + (sys.version_info.minor / 10.0)
deparsed = deparse_code(sys_version, co, showasm=True, showast=True)
# deparsed = deparse_code(sys_version, co, showasm=False, showast=False,
deparsed = deparse_code(sys_version, co, showasm='after', showast=True)
# deparsed = deparse_code(sys_version, co, showasm=None, showast=False,
# showgrammar=True)
print(deparsed.text)
return