# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 # For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt """ Dump the contents of a .pyc file. The output will only be correct if run with the same version of Python that produced the .pyc. """ import binascii import dis import marshal import struct import sys import time import types import warnings def show_pyc_file(fname): f = open(fname, "rb") magic = f.read(4) print("magic %s" % (binascii.hexlify(magic))) read_date_and_size = True flags = struct.unpack("= (3, 14): CO_FLAGS += [ ('CO_NO_MONITORING_EVENTS', 0x2000000), ] # fmt: on def show_code(code, indent="", number=None): label = "" if number is not None: label = "%d: " % number print(f"{indent}{label}code") indent += " " print(f"{indent}name {code.co_name!r}") print("%sargcount %d" % (indent, code.co_argcount)) print("%snlocals %d" % (indent, code.co_nlocals)) print("%sstacksize %d" % (indent, code.co_stacksize)) print(f"{indent}flags {code.co_flags:04x}: {flag_words(code.co_flags, CO_FLAGS)}") show_hex("code", code.co_code, indent=indent) kwargs = {} if sys.version_info >= (3, 13): kwargs["show_offsets"] = True if sys.version_info >= (3, 14): kwargs["show_positions"] = True dis.disassemble(code, **kwargs) print("%sconsts" % indent) for i, const in enumerate(code.co_consts): if type(const) == types.CodeType: show_code(const, indent + " ", number=i) else: print(" %s%d: %r" % (indent, i, const)) print(f"{indent}names {code.co_names!r}") print(f"{indent}varnames {code.co_varnames!r}") print(f"{indent}freevars {code.co_freevars!r}") print(f"{indent}cellvars {code.co_cellvars!r}") print(f"{indent}filename {code.co_filename!r}") print("%sfirstlineno %d" % (indent, code.co_firstlineno)) show_hex("lnotab", code.co_lnotab, indent=indent) print( " {}{}".format( indent, ", ".join(f"{line!r}:{byte!r}" for byte, line in lnotab_interpreted(code)) ) ) if hasattr(code, "co_linetable"): show_hex("linetable", code.co_linetable, indent=indent) if hasattr(code, "co_lines"): print( " {}co_lines {}".format( indent, ", ".join(f"{line!r}:{start!r}-{end!r}" for start, end, line in code.co_lines()), ) ) if hasattr(code, "co_branches"): print( " {}co_branches {}".format( indent, ", ".join( f"{start!r}:{taken!r}/{nottaken!r}" for start, taken, nottaken in code.co_branches() ), ) ) def show_hex(label, h, indent): h = binascii.hexlify(h) if len(h) < 60: print("{}{} {}".format(indent, label, h.decode("ascii"))) else: print(f"{indent}{label}") for i in range(0, len(h), 60): print("{} {}".format(indent, h[i : i + 60].decode("ascii"))) def lnotab_interpreted(code): # Adapted from dis.py in the standard library. byte_increments = code.co_lnotab[0::2] line_increments = code.co_lnotab[1::2] last_line_num = None line_num = code.co_firstlineno byte_num = 0 for byte_incr, line_incr in zip(byte_increments, line_increments): if byte_incr: if line_num != last_line_num: yield (byte_num, line_num) last_line_num = line_num byte_num += byte_incr if line_incr >= 0x80: line_incr -= 0x100 line_num += line_incr if line_num != last_line_num: yield (byte_num, line_num) def flag_words(flags, flag_defs): words = [] for word, flag in flag_defs: if flag & flags: words.append(word) return ", ".join(words) def show_file(fname): if fname.endswith("pyc"): show_pyc_file(fname) elif fname.endswith("py"): show_py_file(fname) else: print("Odd file:", fname) def main(args): warnings.filterwarnings( "ignore", "co_lnotab is deprecated, use co_lines instead", category=DeprecationWarning ) if args[0] == "-c": show_py_text(" ".join(args[1:]).replace(";", "\n")) else: for a in args: show_file(a) if __name__ == "__main__": main(sys.argv[1:])