From 223804ac1f6a3b5b0aa301a6179376e98a2505cf Mon Sep 17 00:00:00 2001 From: rocky Date: Sat, 14 May 2022 09:51:50 -0400 Subject: [PATCH] semi-black scanner26.py --- uncompyle6/scanners/scanner26.py | 68 ++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/uncompyle6/scanners/scanner26.py b/uncompyle6/scanners/scanner26.py index a1067fbd..f0d46f90 100755 --- a/uncompyle6/scanners/scanner26.py +++ b/uncompyle6/scanners/scanner26.py @@ -26,6 +26,7 @@ import sys import uncompyle6.scanners.scanner2 as scan # bytecode verification, verify(), uses JUMP_OPs from here +from xdis import iscode from xdis.opcodes import opcode_26 from xdis.bytecode import _get_const_info @@ -72,7 +73,7 @@ class Scanner26(scan.Scanner2): bytecode = self.build_instructions(co) # show_asm = 'after' - if show_asm in ('both', 'before'): + if show_asm in ("both", "before"): for instr in bytecode.get_instructions(co): print(instr.disassemble()) @@ -81,7 +82,7 @@ class Scanner26(scan.Scanner2): customize = {} if self.is_pypy: - customize['PyPy'] = 1 + customize["PyPy"] = 0 codelen = len(self.code) @@ -93,6 +94,7 @@ class Scanner26(scan.Scanner2): # 'LOAD_ASSERT' is used in assert statements. self.load_asserts = set() for i in self.op_range(0, codelen): + # We need to detect the difference between: # raise AssertionError # and @@ -115,9 +117,9 @@ class Scanner26(scan.Scanner2): # Distinguish "print ..." from "print ...," if self.code[last_stmt] == self.opc.PRINT_ITEM: if self.code[i] == self.opc.PRINT_ITEM: - replace[i] = 'PRINT_ITEM_CONT' + replace[i] = "PRINT_ITEM_CONT" elif self.code[i] == self.opc.PRINT_NEWLINE: - replace[i] = 'PRINT_NEWLINE_CONT' + replace[i] = "PRINT_NEWLINE_CONT" last_stmt = i i = self.next_stmt[i] @@ -181,29 +183,25 @@ class Scanner26(scan.Scanner2): if op in self.opc.CONST_OPS: const = co.co_consts[oparg] - # We can't use inspect.iscode() because we may be - # using a different version of Python than the - # one that this was byte-compiled on. So the code - # types may mismatch. - if hasattr(const, 'co_name'): + if iscode(const): oparg = const - if const.co_name == '': - assert op_name == 'LOAD_CONST' - op_name = 'LOAD_LAMBDA' + if const.co_name == "": + assert op_name == "LOAD_CONST" + op_name = "LOAD_LAMBDA" elif const.co_name == self.genexpr_name: - op_name = 'LOAD_GENEXPR' - elif const.co_name == '': - op_name = 'LOAD_DICTCOMP' - elif const.co_name == '': - op_name = 'LOAD_SETCOMP' + op_name = "LOAD_GENEXPR" + elif const.co_name == "": + op_name = "LOAD_DICTCOMP" + elif const.co_name == "": + op_name = "LOAD_SETCOMP" else: op_name = "LOAD_CODE" - # verify uses 'pattr' for comparison, since 'attr' + # verify() uses 'pattr' for comparison, since 'attr' # now holds Code(const) and thus can not be used # for comparison (todo: think about changing this) - # pattr = 'code_object @ 0x%x %s->%s' % \ + # pattr = 'code_object @ 0x%x %s->%s' %\ # (id(const), const.co_filename, const.co_name) - pattr = '' + pattr = "" else: if oparg < len(co.co_consts): argval, _ = _get_const_info(oparg, co.co_consts) @@ -235,6 +233,7 @@ class Scanner26(scan.Scanner2): pattr = self.opc.cmp_op[oparg] elif op in self.opc.FREE_OPS: pattr = free[oparg] + if op in self.varargs_ops: # CE - Hack for >= 2.5 # Now all values loaded via LOAD_CLOSURE are packed into @@ -285,25 +284,36 @@ class Scanner26(scan.Scanner2): elif op == self.opc.LOAD_GLOBAL: if offset in self.load_asserts: - op_name = 'LOAD_ASSERT' + op_name = "LOAD_ASSERT" elif op == self.opc.RETURN_VALUE: if offset in self.return_end_ifs: - op_name = 'RETURN_END_IF' + op_name = "RETURN_END_IF" linestart = self.linestarts.get(offset, None) if offset not in replace: - tokens.append(Token( - op_name, oparg, pattr, offset, linestart, op, - has_arg, self.opc)) + tokens.append( + Token( + op_name, oparg, pattr, offset, linestart, op, has_arg, self.opc + ) + ) else: - tokens.append(Token( - replace[offset], oparg, pattr, offset, linestart, op, - has_arg, self.opc)) + tokens.append( + Token( + replace[offset], + oparg, + pattr, + offset, + linestart, + op, + has_arg, + self.opc, + ) + ) pass pass - if show_asm in ('both', 'after'): + if show_asm in ("both", "after"): for t in tokens: print(t.format(line_prefix="")) print()