Skip to content

Commit

Permalink
Lint some files
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Jul 22, 2024
1 parent 5048653 commit 9b9de26
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion decompyle3/parsers/reduce_check/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
These check the validity of rule reduction based on properties that aren't in
the tokens. These checks basically have full access to everything.
Optionally it can have access to the tree built for the reduction nonterminal
Optionally, it can have access to the tree built for the reduction nonterminal
it checks.
"""

Expand Down
4 changes: 2 additions & 2 deletions decompyle3/parsers/reduce_check/and_check.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
# the Free Software Foundation, either version 3 of the License, or
Expand Down Expand Up @@ -81,7 +81,7 @@ def and_invalid(
return jump_target != tokens[last].attr
elif tokens[last] in ("POP_JUMP_IF_TRUE", "JUMP_IF_TRUE_OR_POP"):
# Ok if jump_target jumps to a COME_FROM after
# the last instruction or jumps right after last instruction
# the last instruction or jumps right after the last instruction
if last + 1 < n and tokens[last + 1] == "COME_FROM":
return jump_target != tokens[last + 1].off2int()
return jump_target + 2 != tokens[last].attr
Expand Down
24 changes: 15 additions & 9 deletions decompyle3/parsers/reduce_check/for38_check.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020, 2022-2023 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 @@ -19,8 +19,8 @@
def for38_invalid(
self, lhs: str, n: int, rule, tree, tokens: list, first: int, last: int
) -> bool:
"""The only difference between a "for" and and a "for else" is that
that jumps within the "for" never go past the "FOR_ITER" offset.
"""The only difference between a "for" and a "for else" is that
jumps within the "for" never go past the "FOR_ITER" offset.
"""

first_offset = tokens[first].off2int(prefer_last=False)
Expand All @@ -31,21 +31,21 @@ def for38_invalid(
start = self.offset2inst_index[first_offset]
end = off2int(self.offset2inst_index[last_offset], prefer_last=True)

# In the loop below we expect the first "FOR_ITER" to
# In the loop below, we expect the first "FOR_ITER" to
# be before any jumps that go to the end of it (in the case of "for")
# or beyond it (in the case of "for else").

for_body_end_offset = None
for i in range(start, end):
inst = self.insts[i]

# Hack alert for magic number 2's below: in Python 3.8+ instructions are 2 bytes
# Hack alert for magic number 2's below: in Python 3.8+ instructions are 2 bytes
# inst.argval - 2 is the offset of the instruction *before* inst.argval and
# +2 for the instruction that follows.

if not for_body_end_offset and inst.opname == "FOR_ITER":
# There can be some slop in "last" as to where the body ends. If the rule
# end in "JUMP_LOOP", then "last" doesn't need adjusting.
# ends in "JUMP_LOOP", then "last" doesn't need adjusting.
for_body_end_offset = (
inst.argval if rule[1][-1] == "JUMP_LOOP" else inst.argval - 2
)
Expand All @@ -60,7 +60,13 @@ def for38_invalid(
and inst.is_jump()
and inst.argval > for_body_end_offset + 2
):
# Another weird case. Guard against misclassifying things like:
# Another weird case.
# Guard against misclassified things like:
# if a:
# for n in l:
# if b: break # jumps past "else" which is after the end of the "for"
# eird case.
# Guard against misclassified things like:
# if a:
# for n in l:
# if b: break # jumps past "else" which is after the end of the "for"
Expand All @@ -69,8 +75,8 @@ def for38_invalid(
# else:
# r = 3
# The way we distinguish this is to check if the instruction after the body end
# starts with a jump (the start of the encompassing if/else. The "else" part
# of a "for/else" never starts with a jump.
# starts with a jump, the start of the encompassing if/else.
# The "else" part of a "for/else" never starts with a jump.
body_end_next_inst = self.insts[
self.offset2inst_index[for_body_end_offset + 2]
]
Expand Down
2 changes: 1 addition & 1 deletion decompyle3/scanners/scanner37base.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def ingest(self, co, classname=None, code_objects={}, show_asm=None):
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.
rules for the specific number of arguments they take./src/external-vcs/github/rocky/python-decompile3
"""

Expand Down

0 comments on commit 9b9de26

Please sign in to comment.