diff --git a/xdis/cross_dis.py b/xdis/cross_dis.py index d85b7d93..775d2b78 100644 --- a/xdis/cross_dis.py +++ b/xdis/cross_dis.py @@ -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 @@ -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 @@ -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: diff --git a/xdis/disasm.py b/xdis/disasm.py index 33f4ec7e..227b32c2 100644 --- a/xdis/disasm.py +++ b/xdis/disasm.py @@ -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": @@ -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 @@ -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 @@ -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: @@ -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] diff --git a/xdis/instruction.py b/xdis/instruction.py index dc355e5a..34ea3331 100644 --- a/xdis/instruction.py +++ b/xdis/instruction.py @@ -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 @@ -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) @@ -310,7 +310,7 @@ 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) @@ -318,7 +318,11 @@ def disassemble( 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 @@ -326,7 +330,11 @@ def disassemble( 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) @@ -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 @@ -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 diff --git a/xdis/op_imports.py b/xdis/op_imports.py index b7fc8439..37e412d5 100644 --- a/xdis/op_imports.py +++ b/xdis/op_imports.py @@ -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 diff --git a/xdis/opcodes/format/extended.py b/xdis/opcodes/format/extended.py index 78640c6a..3063e004 100644 --- a/xdis/opcodes/format/extended.py +++ b/xdis/opcodes/format/extended.py @@ -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