You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-02 16:44:46 +08:00
Small bit of linting
This commit is contained in:
@@ -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) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
|
||||
# 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.
|
||||
"""
|
||||
|
||||
from typing import Optional, Tuple
|
||||
from types import ModuleType
|
||||
from typing import Optional, Tuple, Union
|
||||
from array import array
|
||||
from collections import namedtuple
|
||||
|
||||
@@ -101,12 +102,15 @@ class Code(object):
|
||||
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):
|
||||
self.version = version
|
||||
self.show_asm = show_asm
|
||||
self.is_pypy = is_pypy
|
||||
|
||||
# Temoorary initialization.
|
||||
self.opc = ModuleType("uninitialized")
|
||||
|
||||
if version[:2] in PYTHON_VERSIONS:
|
||||
v_str = f"""opcode_{version_tuple_to_str(version, start=0, end=2, delimiter="")}"""
|
||||
if is_pypy:
|
||||
@@ -319,15 +323,6 @@ class Scanner(object):
|
||||
def next_offset(self, op, offset: int) -> int:
|
||||
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):
|
||||
"""
|
||||
Find the first <instr> in the block from start to end.
|
||||
@@ -483,7 +478,6 @@ class Scanner(object):
|
||||
result = []
|
||||
extended_arg = 0
|
||||
for offset in self.op_range(start, end):
|
||||
|
||||
op = code[offset]
|
||||
|
||||
if op == self.opc.EXTENDED_ARG:
|
||||
@@ -542,7 +536,6 @@ class Scanner(object):
|
||||
offset = inst.offset
|
||||
continue
|
||||
if last_was_extarg:
|
||||
|
||||
# j = self.stmts.index(inst.offset)
|
||||
# self.lines[j] = offset
|
||||
|
||||
@@ -595,7 +588,7 @@ class Scanner(object):
|
||||
target = parent["end"]
|
||||
return target
|
||||
|
||||
def setTokenClass(self, tokenClass) -> Token:
|
||||
def setTokenClass(self, tokenClass: Token) -> Token:
|
||||
self.Token = tokenClass
|
||||
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)
|
||||
|
||||
|
||||
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 isinstance(version, str):
|
||||
|
@@ -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.
|
||||
|
||||
@@ -7,12 +7,15 @@ grammar parsing.
|
||||
"""
|
||||
|
||||
import uncompyle6.scanners.scanner21 as scan
|
||||
|
||||
# from uncompyle6.scanners.scanner26 import ingest as ingest26
|
||||
|
||||
# bytecode verification, verify(), uses JUMP_OPs from here
|
||||
from xdis.opcodes import opcode_15
|
||||
|
||||
JUMP_OPS = opcode_15.JUMP_OPS
|
||||
|
||||
|
||||
# We base this off of 2.2 instead of the other way around
|
||||
# because we cleaned things up this way.
|
||||
# The history is that 2.7 support is the cleanest,
|
||||
@@ -23,7 +26,7 @@ class Scanner15(scan.Scanner21):
|
||||
self.opc = opcode_15
|
||||
self.opname = opcode_15.opname
|
||||
self.version = (1, 5)
|
||||
self.genexpr_name = '<generator expression>'
|
||||
self.genexpr_name = "<generator expression>"
|
||||
return
|
||||
|
||||
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:
|
||||
- various types of LOAD_CONST's are categorized in terms of what they load
|
||||
- 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
|
||||
* 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
|
||||
|
||||
Also, when we encounter certain tokens, we add them to a set which will cause custom
|
||||
grammar rules. Specifically, variable arg tokens like MAKE_FUNCTION or BUILD_LIST
|
||||
cause specific rules for the specific number of arguments they take.
|
||||
Also, when we encounter certain tokens, we add them to a set which will cause
|
||||
custom grammar rules. Specifically, variable arg tokens like MAKE_FUNCTION or
|
||||
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:
|
||||
if t.op == self.opc.UNPACK_LIST:
|
||||
t.kind = 'UNPACK_LIST_%d' % t.attr
|
||||
t.kind = "UNPACK_LIST_%d" % t.attr
|
||||
pass
|
||||
return tokens, customize
|
||||
|
@@ -188,8 +188,7 @@ class Scanner37Base(Scanner):
|
||||
return
|
||||
|
||||
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
|
||||
easier.
|
||||
returning a list of uncompyle6 Token's.
|
||||
@@ -197,14 +196,18 @@ class Scanner37Base(Scanner):
|
||||
Some transformations are made to assist the deparsing grammar:
|
||||
- various types of LOAD_CONST's are categorized in terms of what they load
|
||||
- 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.:
|
||||
* BUILD_LIST, BUILD_SET
|
||||
* MAKE_FUNCTION and FUNCTION_CALLS append the number of positional arguments
|
||||
- operands with stack argument counts or flag masks are appended to the
|
||||
opcode name, e.g.:
|
||||
* BUILD_LIST, BUILD_SET
|
||||
* MAKE_FUNCTION and FUNCTION_CALLS append the number of positional
|
||||
arguments
|
||||
- EXTENDED_ARGS instructions are removed
|
||||
|
||||
Also, when we encounter certain tokens, we add them to a set which will cause custom
|
||||
grammar rules. Specifically, variable arg tokens like MAKE_FUNCTION or BUILD_LIST
|
||||
cause specific rules for the specific number of arguments they take.
|
||||
Also, when we encounter certain tokens, we add them to a set
|
||||
which will cause custom grammar rules. Specifically, variable
|
||||
arg tokens like MAKE_FUNCTION or BUILD_LIST cause specific
|
||||
rules for the specific number of arguments they take.
|
||||
|
||||
"""
|
||||
|
||||
def tokens_append(j, token):
|
||||
|
Reference in New Issue
Block a user