Getting a module's byte code, how?

Irmen de Jong irmen.NOSPAM at xs4all.nl
Wed Feb 2 17:27:23 EST 2005


Mark Nenadov wrote:
> On Wed, 02 Feb 2005 23:03:17 +0100, Irmen de Jong wrote:
> 
> 
>>What would be the best way, if any, to obtain
>>the bytecode for a given loaded module?
>>
>>I can get the source:
>>import inspect
>>import os
>>src = inspect.getsource(os)
>>
>>but there is no ispect.getbytecode()  ;-)
>>
>>--Irmen
> 
> 
> The inspect API documentation says that code objects have "co_code", which
> is a string of raw compiled bytecode.
> 
> Hope that helps!

Not very much, sorry. Because we now changed the problem into:
"how to obtain the code object from a module".


Perhaps a bit of background is in order.
For Pyro, I'm sending module byte codes across the
wire if the other side needs it (the mobile code feature).
However, this only works for modules that can be loaded
from a file on disk (because I'm now reading the .pyc file
that belongs to the module). When the receiving end in turn
calls another Pyro object, it may have to send the module's
bytecode again-- but that is no longer possible because the
module has no associated file anymore! (It is considered to
be a builtin module)
But because it is *loaded*, there must be some way to get
to the bytecodes, right?


--Irmen



More information about the Python-list mailing list