Disassemble classes, methods, functions, and other compiled objects. With no argument, disassemble the last traceback. Compiled objects currently include generator objects, async generator objects, and coroutine objects, all of which store their code object in a special attribute.
(x=None, *, file=None, depth=None, show_caches=False, adaptive=False,
show_offsets=False, show_positions=False)
| 84 | return compile(source, name, 'exec') |
| 85 | |
| 86 | def dis(x=None, *, file=None, depth=None, show_caches=False, adaptive=False, |
| 87 | show_offsets=False, show_positions=False): |
| 88 | """Disassemble classes, methods, functions, and other compiled objects. |
| 89 | |
| 90 | With no argument, disassemble the last traceback. |
| 91 | |
| 92 | Compiled objects currently include generator objects, async generator |
| 93 | objects, and coroutine objects, all of which store their code object |
| 94 | in a special attribute. |
| 95 | """ |
| 96 | if x is None: |
| 97 | distb(file=file, show_caches=show_caches, adaptive=adaptive, |
| 98 | show_offsets=show_offsets, show_positions=show_positions) |
| 99 | return |
| 100 | # Extract functions from methods. |
| 101 | if hasattr(x, '__func__'): |
| 102 | x = x.__func__ |
| 103 | # Extract compiled code objects from... |
| 104 | if hasattr(x, '__code__'): # ...a function, or |
| 105 | x = x.__code__ |
| 106 | elif hasattr(x, 'gi_code'): #...a generator object, or |
| 107 | x = x.gi_code |
| 108 | elif hasattr(x, 'ag_code'): #...an asynchronous generator object, or |
| 109 | x = x.ag_code |
| 110 | elif hasattr(x, 'cr_code'): #...a coroutine. |
| 111 | x = x.cr_code |
| 112 | # Perform the disassembly. |
| 113 | if hasattr(x, '__dict__'): # Class or module |
| 114 | items = sorted(x.__dict__.items()) |
| 115 | for name, x1 in items: |
| 116 | if isinstance(x1, _have_code): |
| 117 | print("Disassembly of %s:" % name, file=file) |
| 118 | try: |
| 119 | dis(x1, file=file, depth=depth, show_caches=show_caches, adaptive=adaptive, show_offsets=show_offsets, show_positions=show_positions) |
| 120 | except TypeError as msg: |
| 121 | print("Sorry:", msg, file=file) |
| 122 | print(file=file) |
| 123 | elif hasattr(x, 'co_code'): # Code object |
| 124 | _disassemble_recursive(x, file=file, depth=depth, show_caches=show_caches, adaptive=adaptive, show_offsets=show_offsets, show_positions=show_positions) |
| 125 | elif isinstance(x, (bytes, bytearray)): # Raw bytecode |
| 126 | labels_map = _make_labels_map(x) |
| 127 | label_width = 4 + len(str(len(labels_map))) |
| 128 | formatter = Formatter(file=file, |
| 129 | offset_width=len(str(max(len(x) - 2, 9999))) if show_offsets else 0, |
| 130 | label_width=label_width, |
| 131 | show_caches=show_caches) |
| 132 | arg_resolver = ArgResolver(labels_map=labels_map) |
| 133 | _disassemble_bytes(x, arg_resolver=arg_resolver, formatter=formatter) |
| 134 | elif isinstance(x, str): # Source code |
| 135 | _disassemble_str(x, file=file, depth=depth, show_caches=show_caches, adaptive=adaptive, show_offsets=show_offsets, show_positions=show_positions) |
| 136 | else: |
| 137 | raise TypeError("don't know how to disassemble %s objects" % |
| 138 | type(x).__name__) |
| 139 | |
| 140 | def distb(tb=None, *, file=None, show_caches=False, adaptive=False, show_offsets=False, show_positions=False): |
| 141 | """Disassemble a traceback (default: last traceback).""" |
no test coverage detected