Skip to content

Commit 844206d

Browse files
committed
More lint
1 parent 68b4d2e commit 844206d

File tree

6 files changed

+62
-23
lines changed

6 files changed

+62
-23
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
/.venv*
1414
/README
1515
/__pkginfo__.pyc
16+
/decompyle3.egg-info
1617
/dist
1718
/how-to-make-a-release.txt
1819
/nose-*.egg
20+
/pycharm-venv
1921
/tmp
20-
/decompyle3.egg-info
2122
/unpyc
2223
ChangeLog
2324
__pycache__

decompyle3/parsers/main.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* get_python_parser().parse(), or
2020
* python_parser() which does the above
2121
22-
Note however all of this is imported from the __init__ module
2322
"""
2423

2524
import sys
@@ -53,7 +52,7 @@
5352
from decompyle3.show import maybe_show_asm
5453

5554

56-
def parse(p, tokens, customize, is_lambda) -> SyntaxTree:
55+
def parse(p, tokens, customize, is_lambda: bool) -> SyntaxTree:
5756
was_lambda = p.is_lambda
5857
p.is_lambda = is_lambda
5958
p.customize_grammar_rules(tokens, customize)
@@ -150,22 +149,26 @@ def python_parser(
150149
co,
151150
version: tuple = PYTHON_VERSION_TRIPLE,
152151
out=sys.stdout,
153-
showasm=False,
152+
showasm: bool = False,
154153
parser_debug=PARSER_DEFAULT_DEBUG,
155-
compile_mode="exec",
156-
is_pypy=False,
157-
is_lambda=False,
158-
):
154+
compile_mode: str = "exec",
155+
is_pypy: bool = False,
156+
is_lambda: bool = False,
157+
) -> SyntaxTree:
159158
"""
160159
Parse a code object to an abstract syntax tree representation.
161160
162-
:param version: The python version this code is from as a float, for
163-
example 2.6, 2.7, 3.2, 3.3, 3.4, 3.5 etc.
164161
:param co: The code object to parse.
162+
:param version: The python version of this code is from as a float, for
163+
example, 2.6, 2.7, 3.2, 3.3, 3.4, 3.5 etc.
165164
:param out: File like object to write the output to.
166165
:param showasm: Flag which determines whether the disassembled and
167166
ingested code is written to sys.stdout or not.
168167
:param parser_debug: dict containing debug flags for the spark parser.
168+
:param compile_mode: compile mode that we want to parse input `co` as.
169+
This is either "exec", "eval" or, "single".
170+
:param is_pypy: True if ``co`` comes is PyPy code
171+
:param is_lambda True if ``co`` is a lambda expression
169172
170173
:return: Abstract syntax tree representation of the code object.
171174
"""
@@ -196,7 +199,6 @@ def python_parser(
196199
if __name__ == "__main__":
197200

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

201203
tree = python_parser(co, (3, 8, 2), showasm=True, is_pypy=IS_PYPY)
202204
print(tree)

decompyle3/parsers/p37/base.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
# Copyright (c) 2016-2017, 2019-2023 Rocky Bernstein
1+
# Copyright (c) 2016-2017, 2019-2024 Rocky Bernstein
2+
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation, either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
216
"""
317
Python 3.7 base code. We keep non-custom-generated grammar rules out of this file.
418
"""
19+
520
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
621
from spark_parser.spark import rule2str
722

decompyle3/parsers/p38/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2020-2022 Rocky Bernstein
1+
# Copyright (c) 2020-2022, 2024 Rocky Bernstein
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -13,6 +13,10 @@
1313
# You should have received a copy of the GNU General Public License
1414
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1515

16+
"""
17+
Python 3.8 base code. We keep non-custom-generated grammar rules out of this file.
18+
"""
19+
1620
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
1721

1822
from decompyle3.parsers.parse_heads import PythonBaseParser

decompyle3/parsers/p38/lambda_custom.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
class Python38LambdaCustom(Python38BaseParser):
2525
def __init__(self):
2626
self.new_rules = set()
27+
28+
# Special opcodes we see that trigger adding new grammar rules.
29+
self.seen_ops = frozenset()
30+
31+
# Special opcodes we see that trigger adding new grammar rules.
32+
self.seen_ops_basenames = frozenset()
33+
34+
# Customized grammar rules
2735
self.customized = {}
2836

2937
def customize_grammar_rules_lambda38(self, tokens, customize):
@@ -194,8 +202,11 @@ def customize_grammar_rules_lambda38(self, tokens, customize):
194202
is_LOAD_CLOSURE = False
195203
break
196204
if is_LOAD_CLOSURE:
197-
rule = "load_closure ::= %s%s" % (("LOAD_CLOSURE " * v), opname)
198-
self.add_unique_rule(rule, opname, token.attr, customize)
205+
rule_str = "load_closure ::= %s%s" % (
206+
("LOAD_CLOSURE " * v),
207+
opname,
208+
)
209+
self.add_unique_doc_rules(rule_str, customize)
199210

200211
elif opname_base == "BUILD_LIST":
201212
v = token.attr

decompyle3/scanners/scanner37base.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ def __init__(
5454
super(Scanner37Base, self).__init__(version, show_asm, is_pypy)
5555
self.offset2tok_index = None
5656
self.debug = debug
57+
58+
# True is code is from PyPy
5759
self.is_pypy = is_pypy
5860

61+
# Bytecode converted into instruction
62+
self.insts = []
63+
5964
# Create opcode classification sets
6065
# Note: super initialization above initializes self.opc
6166

@@ -409,12 +414,13 @@ def tokens_append(j, token):
409414

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

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

10911097
my_tokens, customize = Scanner37Base(PYTHON_VERSION_TRIPLE).ingest(my_co)
1092-
for token in my_tokens:
1093-
print(token)
1098+
for my_token in my_tokens:
1099+
print(my_token)
10941100
else:
10951101
print(
10961102
"Need to be Python 3.7..3.8 to demo; "

0 commit comments

Comments
 (0)