diff --git a/uncompyle6/scanner.py b/uncompyle6/scanner.py index d6651836..64b335db 100644 --- a/uncompyle6/scanner.py +++ b/uncompyle6/scanner.py @@ -21,6 +21,7 @@ scanner/ingestion module. From here we call various version-specific scanners, e.g. for Python 2.7 or 3.4. """ +from abc import ABC from array import array from collections import namedtuple from types import ModuleType @@ -89,7 +90,7 @@ def long(num): CONST_COLLECTIONS = ("CONST_LIST", "CONST_SET", "CONST_DICT", "CONST_MAP") -class Code(object): +class Code: """ Class for representing code-objects. @@ -108,7 +109,7 @@ class Code(object): self._tokens, self._customize = scanner.ingest(co, classname, show_asm=show_asm) -class Scanner: +class Scanner(ABC): def __init__(self, version: tuple, show_asm=None, is_pypy=False): self.version = version self.show_asm = show_asm @@ -293,6 +294,12 @@ class Scanner: return False return offset < self.get_target(offset) + def ingest(self, co, classname=None, code_objects={}, show_asm=None): + """ + Code to tokenize disassembly. Subclasses must implement this. + """ + raise NotImplementedError("This method should have been implemented") + def prev_offset(self, offset: int) -> int: return self.insts[self.offset2inst_index[offset] - 1].offset diff --git a/uncompyle6/scanners/scanner37.py b/uncompyle6/scanners/scanner37.py index 894da02c..af4e8f5e 100644 --- a/uncompyle6/scanners/scanner37.py +++ b/uncompyle6/scanners/scanner37.py @@ -53,7 +53,7 @@ class Scanner37(Scanner37Base): if collection_type == "CONST_DICT": # constant dictionaries work via BUILD_CONST_KEY_MAP and # handle the values() like sets and lists. - # However the keys() are an LOAD_CONST of the keys. + # However, the keys() are an LOAD_CONST of the keys. # adjust offset to account for this count += 1 diff --git a/uncompyle6/scanners/scanner37base.py b/uncompyle6/scanners/scanner37base.py index dc9804ed..e1c300d0 100644 --- a/uncompyle6/scanners/scanner37base.py +++ b/uncompyle6/scanners/scanner37base.py @@ -266,10 +266,9 @@ class Scanner37Base(Scanner): if ( next_inst.opname == "LOAD_GLOBAL" and next_inst.argval == "AssertionError" - and inst.argval + and inst.argval is not None ): - raise_idx = self.offset2inst_index[self.prev_op[inst.argval]] - raise_inst = self.insts[raise_idx] + raise_inst = self.get_inst(self.prev_op[inst.argval]) if raise_inst.opname.startswith("RAISE_VARARGS"): self.load_asserts.add(next_inst.offset) pass @@ -286,7 +285,7 @@ class Scanner37Base(Scanner): # some backward jumps, are turned into forward jumps to another # "extended arg" backward jump to the same location. if inst.opname == "JUMP_FORWARD": - jump_inst = self.insts[self.offset2inst_index[inst.argval]] + jump_inst = self.get_inst(inst.argval) if jump_inst.has_extended_arg and jump_inst.opname.startswith("JUMP"): # Create a combination of the jump-to instruction and # this one. Keep the position information of this instruction,