From cd3cf5ec2960a733e9fedca9c4549caf33c2d1d0 Mon Sep 17 00:00:00 2001 From: rocky Date: Thu, 3 Nov 2016 21:26:12 -0400 Subject: [PATCH] Use L. for line number prefix in asm and AST --- uncompyle6/bin/uncompile.py | 2 +- uncompyle6/main.py | 6 +++--- uncompyle6/parsers/astnode.py | 2 +- uncompyle6/scanners/scanner2.py | 2 +- uncompyle6/scanners/tok.py | 6 +++++- uncompyle6/semantics/pysource.py | 9 ++++----- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/uncompyle6/bin/uncompile.py b/uncompyle6/bin/uncompile.py index d5284797..69a97d3d 100755 --- a/uncompyle6/bin/uncompile.py +++ b/uncompyle6/bin/uncompile.py @@ -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 diff --git a/uncompyle6/main.py b/uncompyle6/main.py index de82d794..e81eff26 100644 --- a/uncompyle6/main.py +++ b/uncompyle6/main.py @@ -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 diff --git a/uncompyle6/parsers/astnode.py b/uncompyle6/parsers/astnode.py index 7c66502d..69d06388 100644 --- a/uncompyle6/parsers/astnode.py +++ b/uncompyle6/parsers/astnode.py @@ -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:] diff --git a/uncompyle6/scanners/scanner2.py b/uncompyle6/scanners/scanner2.py index 839887a3..0625de03 100644 --- a/uncompyle6/scanners/scanner2.py +++ b/uncompyle6/scanners/scanner2.py @@ -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 diff --git a/uncompyle6/scanners/tok.py b/uncompyle6/scanners/tok.py index f9879334..a260a24c 100644 --- a/uncompyle6/scanners/tok.py +++ b/uncompyle6/scanners/tok.py @@ -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) diff --git a/uncompyle6/semantics/pysource.py b/uncompyle6/semantics/pysource.py index dbbbac0e..8b0eef72 100644 --- a/uncompyle6/semantics/pysource.py +++ b/uncompyle6/semantics/pysource.py @@ -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