Skip to content

Commit

Permalink
More lint
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Jul 21, 2024
1 parent 9a5ddd8 commit 8228cfa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
10 changes: 5 additions & 5 deletions xdis/cross_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

# Here, we are more closely modeling Python's ``dis`` module organization.
# However, it appears that Python names and code has copied a bit heavily from
# However, it appears that Python's names and code have been copied a bit heavily from
# earlier versions of xdis (and without attribution).

from typing import List
Expand Down Expand Up @@ -116,7 +116,7 @@ def findlabels_pre_310(code, opc):
return offsets


# For the `co_lines` attribute we want to emit the full form, omitting
# For the `co_lines` attribute, we want to emit the full form, omitting
# the (350, 360, No line number) and empty entries.

NO_LINE_NUMBER = -128
Expand Down Expand Up @@ -188,9 +188,9 @@ def instruction_size(op, opc):
"""For a given opcode, `op`, in opcode module `opc`,
return the size, in bytes, of an `op` instruction.
This is the size of the opcode (1 byte) and any operand it has. In
Python before version 3.6 this will be either 1 or 3 bytes. In
Python 3.6 or later, it is 2 bytes or a "word"."""
This is the size of the opcode (one byte) and any operand it has.
In Python before version 3.6, this will be either 1 or 3 bytes.
In Python 3.6 or later, it is 2 bytes: a "word"."""
if op < opc.HAVE_ARGUMENT:
return 2 if opc.version_tuple >= (3, 6) else 1
else:
Expand Down
18 changes: 8 additions & 10 deletions xdis/disasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def disco(
is_graal=is_graal,
)

# store final output stream for case of error
# Store final output stream when there is an error.
real_out = out or sys.stdout

if co.co_filename and asm_format != "xasm":
Expand Down Expand Up @@ -195,7 +195,7 @@ def disco_loop(
"""Disassembles a queue of code objects. If we discover
another code object which will be found in co_consts, we add
the new code to the list. Note that the order of code discovery
is in the order of first encountered which is not amenable for
is in the order of first encountered that is not amenable for
the format used by a disassembler where code objects should
be defined before using them in other functions.
However, this is not recursive and will overall lead to less
Expand Down Expand Up @@ -282,8 +282,8 @@ def disco_loop_asm_format(opc, version_tuple, co, real_out, fn_name_map, all_fns
mapped_name = code_uniquify(basename, co.co_code)
co_name = mapped_name
if mapped_name in fn_name_map:
# We can have two lambda's created that are the same
# but have different line numbers
# We can have two lambdas created that are the same
# but have different line numbers.
mapped_name += f"_{str(co.co_firstlineno)}"
fn_name_map[mapped_name] = basename
co.co_name = mapped_name
Expand Down Expand Up @@ -314,12 +314,12 @@ def disassemble_file(
show_source=False,
):
"""
disassemble Python byte-code file (.pyc)
Disassemble Python byte-code file (.pyc).
If given a Python source file (".py") file, we'll
try to find the corresponding compiled object.
If that fails we'll compile internally for the Python version currently running
If that fails, we'll compile internally for the Python version currently running.
"""
pyc_filename = None
try:
Expand Down Expand Up @@ -400,11 +400,9 @@ def _test():
argc = len(sys.argv)
if argc == 1:
if xdis.PYTHON3:
fn = __file__
disassemble_file(__file__)
else:
sys.stderr.write(
"usage: %s [-|CPython compiled file [format]]\n" % __file__
)
sys.stderr.write(f"usage: {__file__} [-|CPython compiled file [format]]\n")
sys.exit(2)
elif argc == 3:
fn, asm_format = sys.argv[1:3]
Expand Down
22 changes: 15 additions & 7 deletions xdis/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class Instruction(NamedTuple):
argval: Any

# String representation of argval if argval is not None.
argrepr: str
argrepr: Optional[str]

# Offset of the instruction
offset: int
Expand Down Expand Up @@ -293,7 +293,7 @@ def disassemble(
hex_bytecode += " %02x" % (self.arg % 256)
else:
hex_bytecode += " 00"
elif self.inst_size == 3:
elif self.inst_size == 3 and self.arg is not None:
# Not 3.6 or later
hex_bytecode += " %02x %02x" % divmod(self.arg, 256)

Expand All @@ -310,23 +310,31 @@ def disassemble(
# for "asm" format, want additional explicit information
# linking operands to tables.
if asm_format == "asm":
if self.is_jump():
if self.is_jump() and self.argrepr is not None:
assert self.argrepr.startswith("to ")
jump_target = self.argrepr[len("to ") :]
fields.append("L" + jump_target)
elif self.optype in indexed_operand:
fields.append(repr(self.arg))
fields.append("(%s)" % argrepr)
argrepr = None
elif self.optype == "const" and not re.search(r"\s", argrepr):
elif (
self.optype == "const"
and argrepr is not None
and not re.search(r"\s", argrepr)
):
fields.append(repr(self.arg))
fields.append("(%s)" % argrepr)
argrepr = None
else:
fields.append(repr(self.arg))
elif asm_format in ("extended", "extended-bytes"):
op = self.opcode
if self.is_jump() and line_starts.get(self.argval) is not None:
if (
self.is_jump()
and line_starts is not None
and line_starts.get(self.argval) is not None
):
new_instruction = list(self)
new_instruction[-2] = f"To line {line_starts[self.argval]}"
self = Instruction(*new_instruction)
Expand Down Expand Up @@ -381,7 +389,7 @@ def disassemble(
prefix += "TOS = "
fields.append(f"{prefix}{self.tos_str}")
pass
else:
elif self.argrepr is not None:
fields.append(self.argrepr)
pass
pass
Expand All @@ -408,7 +416,7 @@ def disassemble(
pass
elif (
hasattr(opc, "opcode_arg_fmt") and opc.opname[op] in opc.opcode_arg_fmt
):
) and self.argrepr is not None:
fields.append(self.argrepr)
pass
pass
Expand Down
2 changes: 1 addition & 1 deletion xdis/op_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def get_opcode_module(version_info=None, variant=None):
pass
except ImportError:
# Python may be too old, e.g. < 2.6 or implementation may
# just not have platform
# just not have the ``platform`` attribute.
pass
elif variant != "Graal":
vers_str += variant
Expand Down
2 changes: 1 addition & 1 deletion xdis/opcodes/format/extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ def extended_format_CALL_METHOD(opc, instructions) -> Tuple[str, Optional[int]]:

def extended_format_RAISE_VARARGS_older(
opc, instructions: List[Instruction]
) -> Tuple[Optional[str], int]:
) -> Tuple[str, Optional[int]]:
raise_inst = instructions[0]
assert raise_inst.opname == "RAISE_VARARGS"
argc = raise_inst.argval
Expand Down

0 comments on commit 8228cfa

Please sign in to comment.