Skip to content

Commit

Permalink
More lint
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Mar 14, 2024
1 parent 68b4d2e commit 844206d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
/.venv*
/README
/__pkginfo__.pyc
/decompyle3.egg-info
/dist
/how-to-make-a-release.txt
/nose-*.egg
/pycharm-venv
/tmp
/decompyle3.egg-info
/unpyc
ChangeLog
__pycache__
Expand Down
22 changes: 12 additions & 10 deletions decompyle3/parsers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* get_python_parser().parse(), or
* python_parser() which does the above
Note however all of this is imported from the __init__ module
"""

import sys
Expand Down Expand Up @@ -53,7 +52,7 @@
from decompyle3.show import maybe_show_asm


def parse(p, tokens, customize, is_lambda) -> SyntaxTree:
def parse(p, tokens, customize, is_lambda: bool) -> SyntaxTree:
was_lambda = p.is_lambda
p.is_lambda = is_lambda
p.customize_grammar_rules(tokens, customize)
Expand Down Expand Up @@ -150,22 +149,26 @@ def python_parser(
co,
version: tuple = PYTHON_VERSION_TRIPLE,
out=sys.stdout,
showasm=False,
showasm: bool = False,
parser_debug=PARSER_DEFAULT_DEBUG,
compile_mode="exec",
is_pypy=False,
is_lambda=False,
):
compile_mode: str = "exec",
is_pypy: bool = False,
is_lambda: bool = False,
) -> SyntaxTree:
"""
Parse a code object to an abstract syntax tree representation.
:param version: The python version this code is from as a float, for
example 2.6, 2.7, 3.2, 3.3, 3.4, 3.5 etc.
:param co: The code object to parse.
:param version: The python version of this code is from as a float, for
example, 2.6, 2.7, 3.2, 3.3, 3.4, 3.5 etc.
:param out: File like object to write the output to.
:param showasm: Flag which determines whether the disassembled and
ingested code is written to sys.stdout or not.
:param parser_debug: dict containing debug flags for the spark parser.
:param compile_mode: compile mode that we want to parse input `co` as.
This is either "exec", "eval" or, "single".
:param is_pypy: True if ``co`` comes is PyPy code
:param is_lambda True if ``co`` is a lambda expression
:return: Abstract syntax tree representation of the code object.
"""
Expand Down Expand Up @@ -196,7 +199,6 @@ def python_parser(
if __name__ == "__main__":

def parse_test(co) -> None:
from decompyle3 import IS_PYPY

tree = python_parser(co, (3, 8, 2), showasm=True, is_pypy=IS_PYPY)
print(tree)
Expand Down
17 changes: 16 additions & 1 deletion decompyle3/parsers/p37/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
# Copyright (c) 2016-2017, 2019-2023 Rocky Bernstein
# Copyright (c) 2016-2017, 2019-2024 Rocky Bernstein

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
Python 3.7 base code. We keep non-custom-generated grammar rules out of this file.
"""

from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from spark_parser.spark import rule2str

Expand Down
6 changes: 5 additions & 1 deletion decompyle3/parsers/p38/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2022 Rocky Bernstein
# Copyright (c) 2020-2022, 2024 Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -13,6 +13,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
Python 3.8 base code. We keep non-custom-generated grammar rules out of this file.
"""

from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG

from decompyle3.parsers.parse_heads import PythonBaseParser
Expand Down
15 changes: 13 additions & 2 deletions decompyle3/parsers/p38/lambda_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
class Python38LambdaCustom(Python38BaseParser):
def __init__(self):
self.new_rules = set()

# Special opcodes we see that trigger adding new grammar rules.
self.seen_ops = frozenset()

# Special opcodes we see that trigger adding new grammar rules.
self.seen_ops_basenames = frozenset()

# Customized grammar rules
self.customized = {}

def customize_grammar_rules_lambda38(self, tokens, customize):
Expand Down Expand Up @@ -194,8 +202,11 @@ def customize_grammar_rules_lambda38(self, tokens, customize):
is_LOAD_CLOSURE = False
break
if is_LOAD_CLOSURE:
rule = "load_closure ::= %s%s" % (("LOAD_CLOSURE " * v), opname)
self.add_unique_rule(rule, opname, token.attr, customize)
rule_str = "load_closure ::= %s%s" % (
("LOAD_CLOSURE " * v),
opname,
)
self.add_unique_doc_rules(rule_str, customize)

elif opname_base == "BUILD_LIST":
v = token.attr
Expand Down
22 changes: 14 additions & 8 deletions decompyle3/scanners/scanner37base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ def __init__(
super(Scanner37Base, self).__init__(version, show_asm, is_pypy)
self.offset2tok_index = None
self.debug = debug

# True is code is from PyPy
self.is_pypy = is_pypy

# Bytecode converted into instruction
self.insts = []

# Create opcode classification sets
# Note: super initialization above initializes self.opc

Expand Down Expand Up @@ -409,12 +414,13 @@ def tokens_append(j, token):

if inst.offset in jump_targets:
jump_idx = 0
# We want to process COME_FROMs to the same offset to be in *descending*
# offset order, so we have the larger range or biggest instruction interval
# last. (I think they are sorted in increasing order, but for safety
# we sort them). That way, specific COME_FROM tags will match up
# properly. For example, a "loop" with an "if" nested in it should have the
# "loop" tag last so the grammar rule matches that properly.
# We want to process COME_FROMs to the same offset to be in
# *descending* offset order, so we have the larger range or
# biggest instruction interval last. (I think they are sorted
# in increasing order, but for safety we sort them). That way,
# specific COME_FROM tags will match up properly. For example,
# a "loop" with an "if" nested in it should have the "loop" tag
# last so the grammar rule matches that properly.
for jump_offset in sorted(jump_targets[inst.offset], reverse=True):
come_from_name = "COME_FROM"

Expand Down Expand Up @@ -1089,8 +1095,8 @@ def next_except_jump(self, start):
my_co = inspect.currentframe().f_code # type: ignore

my_tokens, customize = Scanner37Base(PYTHON_VERSION_TRIPLE).ingest(my_co)
for token in my_tokens:
print(token)
for my_token in my_tokens:
print(my_token)
else:
print(
"Need to be Python 3.7..3.8 to demo; "
Expand Down

0 comments on commit 844206d

Please sign in to comment.