Small bit of linting

This commit is contained in:
rocky
2023-10-06 02:44:41 -04:00
parent 0c18d35043
commit 0ea75cadca
3 changed files with 36 additions and 33 deletions

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2016, 2018-2022 by Rocky Bernstein # Copyright (c) 2016, 2018-2023 by Rocky Bernstein
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org> # Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com> # Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
# Copyright (c) 1999 John Aycock # Copyright (c) 1999 John Aycock
@@ -21,7 +21,8 @@ scanner/ingestion module. From here we call various version-specific
scanners, e.g. for Python 2.7 or 3.4. scanners, e.g. for Python 2.7 or 3.4.
""" """
from typing import Optional, Tuple from types import ModuleType
from typing import Optional, Tuple, Union
from array import array from array import array
from collections import namedtuple from collections import namedtuple
@@ -101,12 +102,15 @@ class Code(object):
self._tokens, self._customize = scanner.ingest(co, classname, show_asm=show_asm) self._tokens, self._customize = scanner.ingest(co, classname, show_asm=show_asm)
class Scanner(object): class Scanner:
def __init__(self, version: tuple, show_asm=None, is_pypy=False): def __init__(self, version: tuple, show_asm=None, is_pypy=False):
self.version = version self.version = version
self.show_asm = show_asm self.show_asm = show_asm
self.is_pypy = is_pypy self.is_pypy = is_pypy
# Temoorary initialization.
self.opc = ModuleType("uninitialized")
if version[:2] in PYTHON_VERSIONS: if version[:2] in PYTHON_VERSIONS:
v_str = f"""opcode_{version_tuple_to_str(version, start=0, end=2, delimiter="")}""" v_str = f"""opcode_{version_tuple_to_str(version, start=0, end=2, delimiter="")}"""
if is_pypy: if is_pypy:
@@ -319,15 +323,6 @@ class Scanner(object):
def next_offset(self, op, offset: int) -> int: def next_offset(self, op, offset: int) -> int:
return xdis.next_offset(op, self.opc, offset) return xdis.next_offset(op, self.opc, offset)
def print_bytecode(self):
for i in self.op_range(0, len(self.code)):
op = self.code[i]
if op in self.JUMP_OPS:
dest = self.get_target(i, op)
print("%i\t%s\t%i" % (i, self.opname[op], dest))
else:
print("%i\t%s\t" % (i, self.opname[op]))
def first_instr(self, start: int, end: int, instr, target=None, exact=True): def first_instr(self, start: int, end: int, instr, target=None, exact=True):
""" """
Find the first <instr> in the block from start to end. Find the first <instr> in the block from start to end.
@@ -483,7 +478,6 @@ class Scanner(object):
result = [] result = []
extended_arg = 0 extended_arg = 0
for offset in self.op_range(start, end): for offset in self.op_range(start, end):
op = code[offset] op = code[offset]
if op == self.opc.EXTENDED_ARG: if op == self.opc.EXTENDED_ARG:
@@ -542,7 +536,6 @@ class Scanner(object):
offset = inst.offset offset = inst.offset
continue continue
if last_was_extarg: if last_was_extarg:
# j = self.stmts.index(inst.offset) # j = self.stmts.index(inst.offset)
# self.lines[j] = offset # self.lines[j] = offset
@@ -595,7 +588,7 @@ class Scanner(object):
target = parent["end"] target = parent["end"]
return target return target
def setTokenClass(self, tokenClass) -> Token: def setTokenClass(self, tokenClass: Token) -> Token:
self.Token = tokenClass self.Token = tokenClass
return self.Token return self.Token
@@ -621,7 +614,7 @@ def parse_fn_counts_30_35(argc: int) -> Tuple[int, int, int]:
return ((argc & 0xFF), (argc >> 8) & 0xFF, annotate_count) return ((argc & 0xFF), (argc >> 8) & 0xFF, annotate_count)
def get_scanner(version, is_pypy=False, show_asm=None): def get_scanner(version: Union[str, tuple], is_pypy=False, show_asm=None) -> Scanner:
# If version is a string, turn that into the corresponding float. # If version is a string, turn that into the corresponding float.
if isinstance(version, str): if isinstance(version, str):

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2016-2018, 2021-2022 by Rocky Bernstein # Copyright (c) 2016-2018, 2021-2023 by Rocky Bernstein
""" """
Python 1.5 bytecode decompiler massaging. Python 1.5 bytecode decompiler massaging.
@@ -7,12 +7,15 @@ grammar parsing.
""" """
import uncompyle6.scanners.scanner21 as scan import uncompyle6.scanners.scanner21 as scan
# from uncompyle6.scanners.scanner26 import ingest as ingest26 # from uncompyle6.scanners.scanner26 import ingest as ingest26
# bytecode verification, verify(), uses JUMP_OPs from here # bytecode verification, verify(), uses JUMP_OPs from here
from xdis.opcodes import opcode_15 from xdis.opcodes import opcode_15
JUMP_OPS = opcode_15.JUMP_OPS JUMP_OPS = opcode_15.JUMP_OPS
# We base this off of 2.2 instead of the other way around # We base this off of 2.2 instead of the other way around
# because we cleaned things up this way. # because we cleaned things up this way.
# The history is that 2.7 support is the cleanest, # The history is that 2.7 support is the cleanest,
@@ -23,7 +26,7 @@ class Scanner15(scan.Scanner21):
self.opc = opcode_15 self.opc = opcode_15
self.opname = opcode_15.opname self.opname = opcode_15.opname
self.version = (1, 5) self.version = (1, 5)
self.genexpr_name = '<generator expression>' self.genexpr_name = "<generator expression>"
return return
def ingest(self, co, classname=None, code_objects={}, show_asm=None): def ingest(self, co, classname=None, code_objects={}, show_asm=None):
@@ -36,18 +39,22 @@ class Scanner15(scan.Scanner21):
Some transformations are made to assist the deparsing grammar: Some transformations are made to assist the deparsing grammar:
- various types of LOAD_CONST's are categorized in terms of what they load - various types of LOAD_CONST's are categorized in terms of what they load
- COME_FROM instructions are added to assist parsing control structures - COME_FROM instructions are added to assist parsing control structures
- operands with stack argument counts or flag masks are appended to the opcode name, e.g.: - operands with stack argument counts or flag masks are appended to the
opcode name, e.g.:
* BUILD_LIST, BUILD_SET * BUILD_LIST, BUILD_SET
* MAKE_FUNCTION and FUNCTION_CALLS append the number of positional arguments * MAKE_FUNCTION and FUNCTION_CALLS append the number of positional
arguments
- EXTENDED_ARGS instructions are removed - EXTENDED_ARGS instructions are removed
Also, when we encounter certain tokens, we add them to a set which will cause custom Also, when we encounter certain tokens, we add them to a set which will cause
grammar rules. Specifically, variable arg tokens like MAKE_FUNCTION or BUILD_LIST custom grammar rules. Specifically, variable arg tokens like MAKE_FUNCTION or
cause specific rules for the specific number of arguments they take. BUILD_LIST cause specific rules for the specific number of arguments they take.
""" """
tokens, customize = scan.Scanner21.ingest(self, co, classname, code_objects, show_asm) tokens, customize = scan.Scanner21.ingest(
self, co, classname, code_objects, show_asm
)
for t in tokens: for t in tokens:
if t.op == self.opc.UNPACK_LIST: if t.op == self.opc.UNPACK_LIST:
t.kind = 'UNPACK_LIST_%d' % t.attr t.kind = "UNPACK_LIST_%d" % t.attr
pass pass
return tokens, customize return tokens, customize

View File

@@ -188,8 +188,7 @@ class Scanner37Base(Scanner):
return return
def ingest(self, co, classname=None, code_objects={}, show_asm=None): def ingest(self, co, classname=None, code_objects={}, show_asm=None):
""" """Create "tokens" the bytecode of an Python code object. Largely these
Create "tokens" the bytecode of an Python code object. Largely these
are the opcode name, but in some cases that has been modified to make parsing are the opcode name, but in some cases that has been modified to make parsing
easier. easier.
returning a list of uncompyle6 Token's. returning a list of uncompyle6 Token's.
@@ -197,14 +196,18 @@ class Scanner37Base(Scanner):
Some transformations are made to assist the deparsing grammar: Some transformations are made to assist the deparsing grammar:
- various types of LOAD_CONST's are categorized in terms of what they load - various types of LOAD_CONST's are categorized in terms of what they load
- COME_FROM instructions are added to assist parsing control structures - COME_FROM instructions are added to assist parsing control structures
- operands with stack argument counts or flag masks are appended to the opcode name, e.g.: - operands with stack argument counts or flag masks are appended to the
* BUILD_LIST, BUILD_SET opcode name, e.g.:
* MAKE_FUNCTION and FUNCTION_CALLS append the number of positional arguments * BUILD_LIST, BUILD_SET
* MAKE_FUNCTION and FUNCTION_CALLS append the number of positional
arguments
- EXTENDED_ARGS instructions are removed - EXTENDED_ARGS instructions are removed
Also, when we encounter certain tokens, we add them to a set which will cause custom Also, when we encounter certain tokens, we add them to a set
grammar rules. Specifically, variable arg tokens like MAKE_FUNCTION or BUILD_LIST which will cause custom grammar rules. Specifically, variable
cause specific rules for the specific number of arguments they take. arg tokens like MAKE_FUNCTION or BUILD_LIST cause specific
rules for the specific number of arguments they take.
""" """
def tokens_append(j, token): def tokens_append(j, token):