Search code examples
pythonbytecodecpythonbytecode-manipulation

How to generate code objects from modules in Python?


I have a .pyc file with no corresponding Python source code. I want to see the disassembly of the module using dis. I can import my module just fine with

import dis
import foo

But to call dis.dis on it, I can't use the module object. I need the corresponding code object which backs foo. How do I create it? It seems that the compile builtin can compile strings, classes, methods, and functions, but not files or modules.

How do I generate this code object given what I have?


Solution

  • See http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html