Getting a module's code object

A

Arnaud Delobelle

Hi all,

In Python 3, a function f's code object can be accessed via f.__code__.

I'm interested in getting a module's code object, i.e. the code that
is executed when the module is run. I don't think it's accessible via
the module object itself (although I would be glad if somebody proved
me wrong :). In the marshal module docs [1] it is mentioned that:

"""
The marshal module exists mainly to support reading and writing the
“pseudo-compiled†code for Python modules of .pyc files.
"""

So it seems that the module's code object is marshalled into the .pyc
file - so there may be a way to unmarshal it - but I can't easily find
information about how to do this.

Is this a good lead, or is there another way to obtained a module's code object?

Thanks
 
P

Peter Otten

Arnaud said:
In Python 3, a function f's code object can be accessed via f.__code__.

I'm interested in getting a module's code object, i.e. the code that
is executed when the module is run. I don't think it's accessible via
the module object itself (although I would be glad if somebody proved
me wrong :). In the marshal module docs [1] it is mentioned that:

"""
The marshal module exists mainly to support reading and writing the
“pseudo-compiled†code for Python modules of .pyc files.
"""

So it seems that the module's code object is marshalled into the .pyc
file - so there may be a way to unmarshal it - but I can't easily find
information about how to do this.

Is this a good lead, or is there another way to obtained a module's code
object?

Taken from pkgutil.py:

def read_code(stream):
# This helper is needed in order for the PEP 302 emulation to
# correctly handle compiled files
import marshal

magic = stream.read(4)
if magic != imp.get_magic():
return None

stream.read(4) # Skip timestamp
return marshal.load(stream)

Alternatively you can compile the source yourself:

module = compile(source, filename, "exec")
 
A

Arnaud Delobelle

Arnaud said:
In Python 3, a function f's code object can be accessed via f.__code__.

I'm interested in getting a module's code object,
[...]

Taken from pkgutil.py:

def read_code(stream):
   # This helper is needed in order for the PEP 302 emulation to
   # correctly handle compiled files
   import marshal

   magic = stream.read(4)
   if magic != imp.get_magic():
       return None

   stream.read(4) # Skip timestamp
   return marshal.load(stream)

This works fine, thanks a lot!
Alternatively you can compile the source yourself:

module = compile(source, filename, "exec")

I don't necessarily have access to the source file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top