Skip to content

Commit

Permalink
Small bit of linting
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Oct 6, 2023
1 parent 0c18d35 commit 0ea75ca
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
25 changes: 9 additions & 16 deletions uncompyle6/scanner.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
# Copyright (c) 2000-2002 by hartmut Goebel <[email protected]>
# Copyright (c) 1999 John Aycock
Expand All @@ -21,7 +21,8 @@
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

Expand Down Expand Up @@ -101,12 +102,15 @@ def __init__(self, co, scanner, classname=None, show_asm=None):
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:
Expand Down Expand Up @@ -319,15 +323,6 @@ def get_argument(self, pos: int):
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.
Expand Down Expand Up @@ -483,7 +478,6 @@ def all_instr(
result = []
extended_arg = 0
for offset in self.op_range(start, end):

op = code[offset]

if op == self.opc.EXTENDED_ARG:
Expand Down Expand Up @@ -542,7 +536,6 @@ def remove_extended_args(self, instructions):
offset = inst.offset
continue
if last_was_extarg:

# j = self.stmts.index(inst.offset)
# self.lines[j] = offset

Expand Down Expand Up @@ -595,7 +588,7 @@ def restrict_to_parent(self, target: int, parent) -> int:
target = parent["end"]
return target

def setTokenClass(self, tokenClass) -> Token:
def setTokenClass(self, tokenClass: Token) -> Token:
self.Token = tokenClass
return self.Token

Expand All @@ -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):
Expand Down
25 changes: 16 additions & 9 deletions uncompyle6/scanners/scanner15.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -7,12 +7,15 @@
"""

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,
Expand All @@ -23,7 +26,7 @@ def __init__(self, show_asm=False):
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):
Expand All @@ -36,18 +39,22 @@ def ingest(self, co, classname=None, code_objects={}, show_asm=None):
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
19 changes: 11 additions & 8 deletions uncompyle6/scanners/scanner37base.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,26 @@ def __init__(self, version: Tuple[int, int], show_asm=None, debug="", is_pypy=Fa
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.
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):
Expand Down

0 comments on commit 0ea75ca

Please sign in to comment.