Helper to handle methods, compiled or raw code objects, and strings.
(x)
| 158 | UNKNOWN = _Unknown() |
| 159 | |
| 160 | def _get_code_object(x): |
| 161 | """Helper to handle methods, compiled or raw code objects, and strings.""" |
| 162 | # Extract functions from methods. |
| 163 | if hasattr(x, '__func__'): |
| 164 | x = x.__func__ |
| 165 | # Extract compiled code objects from... |
| 166 | if hasattr(x, '__code__'): # ...a function, or |
| 167 | x = x.__code__ |
| 168 | elif hasattr(x, 'gi_code'): #...a generator object, or |
| 169 | x = x.gi_code |
| 170 | elif hasattr(x, 'ag_code'): #...an asynchronous generator object, or |
| 171 | x = x.ag_code |
| 172 | elif hasattr(x, 'cr_code'): #...a coroutine. |
| 173 | x = x.cr_code |
| 174 | # Handle source code. |
| 175 | if isinstance(x, str): |
| 176 | x = _try_compile(x, "<disassembly>") |
| 177 | # By now, if we don't have a code object, we can't disassemble x. |
| 178 | if hasattr(x, 'co_code'): |
| 179 | return x |
| 180 | raise TypeError("don't know how to disassemble %s objects" % |
| 181 | type(x).__name__) |
| 182 | |
| 183 | def _deoptop(op): |
| 184 | name = _all_opname[op] |
no test coverage detected