Skip to content

Commit 8228cfa

Browse files
author
rocky
committed
More lint
1 parent 9a5ddd8 commit 8228cfa

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed

xdis/cross_dis.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1616

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

2121
from typing import List
@@ -116,7 +116,7 @@ def findlabels_pre_310(code, opc):
116116
return offsets
117117

118118

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

122122
NO_LINE_NUMBER = -128
@@ -188,9 +188,9 @@ def instruction_size(op, opc):
188188
"""For a given opcode, `op`, in opcode module `opc`,
189189
return the size, in bytes, of an `op` instruction.
190190
191-
This is the size of the opcode (1 byte) and any operand it has. In
192-
Python before version 3.6 this will be either 1 or 3 bytes. In
193-
Python 3.6 or later, it is 2 bytes or a "word"."""
191+
This is the size of the opcode (one byte) and any operand it has.
192+
In Python before version 3.6, this will be either 1 or 3 bytes.
193+
In Python 3.6 or later, it is 2 bytes: a "word"."""
194194
if op < opc.HAVE_ARGUMENT:
195195
return 2 if opc.version_tuple >= (3, 6) else 1
196196
else:

xdis/disasm.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def disco(
156156
is_graal=is_graal,
157157
)
158158

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

162162
if co.co_filename and asm_format != "xasm":
@@ -195,7 +195,7 @@ def disco_loop(
195195
"""Disassembles a queue of code objects. If we discover
196196
another code object which will be found in co_consts, we add
197197
the new code to the list. Note that the order of code discovery
198-
is in the order of first encountered which is not amenable for
198+
is in the order of first encountered that is not amenable for
199199
the format used by a disassembler where code objects should
200200
be defined before using them in other functions.
201201
However, this is not recursive and will overall lead to less
@@ -282,8 +282,8 @@ def disco_loop_asm_format(opc, version_tuple, co, real_out, fn_name_map, all_fns
282282
mapped_name = code_uniquify(basename, co.co_code)
283283
co_name = mapped_name
284284
if mapped_name in fn_name_map:
285-
# We can have two lambda's created that are the same
286-
# but have different line numbers
285+
# We can have two lambdas created that are the same
286+
# but have different line numbers.
287287
mapped_name += f"_{str(co.co_firstlineno)}"
288288
fn_name_map[mapped_name] = basename
289289
co.co_name = mapped_name
@@ -314,12 +314,12 @@ def disassemble_file(
314314
show_source=False,
315315
):
316316
"""
317-
disassemble Python byte-code file (.pyc)
317+
Disassemble Python byte-code file (.pyc).
318318
319319
If given a Python source file (".py") file, we'll
320320
try to find the corresponding compiled object.
321321
322-
If that fails we'll compile internally for the Python version currently running
322+
If that fails, we'll compile internally for the Python version currently running.
323323
"""
324324
pyc_filename = None
325325
try:
@@ -400,11 +400,9 @@ def _test():
400400
argc = len(sys.argv)
401401
if argc == 1:
402402
if xdis.PYTHON3:
403-
fn = __file__
403+
disassemble_file(__file__)
404404
else:
405-
sys.stderr.write(
406-
"usage: %s [-|CPython compiled file [format]]\n" % __file__
407-
)
405+
sys.stderr.write(f"usage: {__file__} [-|CPython compiled file [format]]\n")
408406
sys.exit(2)
409407
elif argc == 3:
410408
fn, asm_format = sys.argv[1:3]

xdis/instruction.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class Instruction(NamedTuple):
170170
argval: Any
171171

172172
# String representation of argval if argval is not None.
173-
argrepr: str
173+
argrepr: Optional[str]
174174

175175
# Offset of the instruction
176176
offset: int
@@ -293,7 +293,7 @@ def disassemble(
293293
hex_bytecode += " %02x" % (self.arg % 256)
294294
else:
295295
hex_bytecode += " 00"
296-
elif self.inst_size == 3:
296+
elif self.inst_size == 3 and self.arg is not None:
297297
# Not 3.6 or later
298298
hex_bytecode += " %02x %02x" % divmod(self.arg, 256)
299299

@@ -310,23 +310,31 @@ def disassemble(
310310
# for "asm" format, want additional explicit information
311311
# linking operands to tables.
312312
if asm_format == "asm":
313-
if self.is_jump():
313+
if self.is_jump() and self.argrepr is not None:
314314
assert self.argrepr.startswith("to ")
315315
jump_target = self.argrepr[len("to ") :]
316316
fields.append("L" + jump_target)
317317
elif self.optype in indexed_operand:
318318
fields.append(repr(self.arg))
319319
fields.append("(%s)" % argrepr)
320320
argrepr = None
321-
elif self.optype == "const" and not re.search(r"\s", argrepr):
321+
elif (
322+
self.optype == "const"
323+
and argrepr is not None
324+
and not re.search(r"\s", argrepr)
325+
):
322326
fields.append(repr(self.arg))
323327
fields.append("(%s)" % argrepr)
324328
argrepr = None
325329
else:
326330
fields.append(repr(self.arg))
327331
elif asm_format in ("extended", "extended-bytes"):
328332
op = self.opcode
329-
if self.is_jump() and line_starts.get(self.argval) is not None:
333+
if (
334+
self.is_jump()
335+
and line_starts is not None
336+
and line_starts.get(self.argval) is not None
337+
):
330338
new_instruction = list(self)
331339
new_instruction[-2] = f"To line {line_starts[self.argval]}"
332340
self = Instruction(*new_instruction)
@@ -381,7 +389,7 @@ def disassemble(
381389
prefix += "TOS = "
382390
fields.append(f"{prefix}{self.tos_str}")
383391
pass
384-
else:
392+
elif self.argrepr is not None:
385393
fields.append(self.argrepr)
386394
pass
387395
pass
@@ -408,7 +416,7 @@ def disassemble(
408416
pass
409417
elif (
410418
hasattr(opc, "opcode_arg_fmt") and opc.opname[op] in opc.opcode_arg_fmt
411-
):
419+
) and self.argrepr is not None:
412420
fields.append(self.argrepr)
413421
pass
414422
pass

xdis/op_imports.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def get_opcode_module(version_info=None, variant=None):
204204
pass
205205
except ImportError:
206206
# Python may be too old, e.g. < 2.6 or implementation may
207-
# just not have platform
207+
# just not have the ``platform`` attribute.
208208
pass
209209
elif variant != "Graal":
210210
vers_str += variant

xdis/opcodes/format/extended.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ def extended_format_CALL_METHOD(opc, instructions) -> Tuple[str, Optional[int]]:
663663

664664
def extended_format_RAISE_VARARGS_older(
665665
opc, instructions: List[Instruction]
666-
) -> Tuple[Optional[str], int]:
666+
) -> Tuple[str, Optional[int]]:
667667
raise_inst = instructions[0]
668668
assert raise_inst.opname == "RAISE_VARARGS"
669669
argc = raise_inst.argval

0 commit comments

Comments
 (0)