473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Getting a module's byte code, how?

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
Jul 18 '05 #1
8 3920
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!

- -
Mark Nenadov
Python Byte Solutions
http://www.pythonbyte.com/
Jul 18 '05 #2
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
Jul 18 '05 #3
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!

Unfortunately co_code is an attribute of code objects, and a module
doesn't *have* a code object, as far as I know - when it's imported its
bytecode is executed in the scope of the module directory, but no
attempt is made to keep the code around thereafter: what would be the point?

Having said which, if the module was loaded from a .pyc file then the
bytecode is available from that - take everything but the first eight
bytes and use marshal.loads() to turn it back into a code object:
mbc = file("/lib/python2.4/re.pyc", "rb").read()[8:]
import marshal
code = marshal.loads(mbc)
code <code object ? at 0xa085d60, file
"/tmp/python.2568/usr/lib/python2.4/re.py", line 1>


Note that the ugly details *might* change, and that byte codes are
version-dependent.

tadaaa-ly y'rs - steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.python.org/pycon/2005/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #4
Irmen de Jong wrote:
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?

I'm not sure why you think the module's code would be needed once it's
been executed. That assigns all necessary code blocks to the functions
defined therein, so the code, once the import has been executed, is
garbage (from the interpreter's rather process-centric view of things).

The module will, however, have a __file__ attribute, which should allow
you to retrieve the code as mentioned in my previous post - a .pyc file,
it appears, is just a four-byte magic number, a four-byte timestamp and
a marshalled code object.

Of course there's no guarantee that the module has been loaded from a
compiled file. In that case your only option is to read in the source
and compile it.

I am presuming that this feature of Pyro won't allow a 2.3 system to
talk to a 2.4 one, since the byte codes are incompatible.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.python.org/pycon/2005/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #5
Steve Holden wrote:
Having said which, if the module was loaded from a .pyc file then the
bytecode is available from that - take everything but the first eight
bytes and use marshal.loads() to turn it back into a code object:
Yup. As I explained in the other message, this is basically
what I'm doing at the moment (with a few twists; it reads the .py
file if no .pyc is available).
But I also want the bytecode of modules that don't have a .pyc file,
possibly because they have already been 'dynamically' loaded from
another bytecode string ;-)

Now, I could ofcourse store the bytecode string that I started
with *inside* the module itself, in a special attribute or so.
This just occurred to me and I think it's a possible solution.
But the bytecodes must be stored by Python itself somewhere
already... because Python is able to execute my module... right?
I want them! :-)
Note that the ugly details *might* change, and that byte codes are
version-dependent.


I know, but this fact was not yet mentioned in the Pyro manual.
Thanks for reminding me, I'll add it.

--Irmen
Jul 18 '05 #6
Steve Holden wrote:
I'm not sure why you think the module's code would be needed once it's
been executed. That assigns all necessary code blocks to the functions
defined therein, so the code, once the import has been executed, is
garbage (from the interpreter's rather process-centric view of things).
Ah, ofcourse. Thanks for this insight.
I may have to rethink some of this...
I am presuming that this feature of Pyro won't allow a 2.3 system to
talk to a 2.4 one, since the byte codes are incompatible.


Correct. However, when you're not using mobile code (the default),
you're fine.

--Irmen
Jul 18 '05 #7
Irmen de Jong wrote:
Steve Holden wrote:
Having said which, if the module was loaded from a .pyc file then the
bytecode is available from that - take everything but the first eight
bytes and use marshal.loads() to turn it back into a code object:

Yup. As I explained in the other message, this is basically
what I'm doing at the moment (with a few twists; it reads the .py
file if no .pyc is available).
But I also want the bytecode of modules that don't have a .pyc file,
possibly because they have already been 'dynamically' loaded from
another bytecode string ;-)

Aah, right, I suspect in these cases (which *are* pretty far from the
ordinary run of things) you'd sometimes be up the creek without a paddle.
Now, I could ofcourse store the bytecode string that I started
with *inside* the module itself, in a special attribute or so.
This just occurred to me and I think it's a possible solution.
But the bytecodes must be stored by Python itself somewhere
already... because Python is able to execute my module... right?
Not necessarily. I've just been playing with importing modules from a
database. Here's the relevant extract from my load_module() function:

code, package, path = row # ... from the database
code = marshal.loads(code)
module = DBmodule(modname) # subclass of moduleType
sys.modules[modname] = module
module.__name__ = modname
module.__file__ = path # "db:%s" % modname
module.__loader__ = dbimporter
if package:
module.__path__ = ["*db*"]
exec code in module.__dict__
#print modname, "loaded:", module, "pkg:", package
return module

Note well that the module is essentially imported by executing its
bytecode in the context of the module's directory. From that point on
the module doesn't need access to its code - all its functions and
classes have been created, and the functions and methods reachable from
the module's __dict__ now have appropriate snippets of the byte code as
their own code objects.
I want them! :-)

Well I'm afraid there's no guarantee that they haven't already been
garbage collected, and stamping your foot isn't going to do any good :-)
Note that the ugly details *might* change, and that byte codes are
version-dependent.

I know, but this fact was not yet mentioned in the Pyro manual.
Thanks for reminding me, I'll add it.

--Irmen


A pleasure. Thanks for Pyro! (and thanks for reminding me indirectly
that I need to guard that execution of hte module's code against
exceptions).

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.python.org/pycon/2005/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #8
Steve Holden wrote:
But I also want the bytecode of modules that don't have a .pyc file,
possibly because they have already been 'dynamically' loaded from
another bytecode string ;-)
Aah, right, I suspect in these cases (which *are* pretty far from the
ordinary run of things) you'd sometimes be up the creek without a paddle.


I was afraid of that.
The whole mobile code stuff in Pyro is very powerful, for the
cases where it works and makes sense, I think.
However it is also fairly messy.
In the end perhaps it introduces more problems than it solves,
but its really nice to see it work for the simple cases :)

I want them! :-)

Well I'm afraid there's no guarantee that they haven't already been
garbage collected, and stamping your foot isn't going to do any good :-)


Heh, that was funny :D
A pleasure. Thanks for Pyro! (and thanks for reminding me indirectly
that I need to guard that execution of hte module's code against
exceptions).


Pyro happily throws these exceptions in your face.
Like I said, rather messy.

--Irmen
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: F. GEIGER | last post by:
py2exe and datetime -> No module named datetime I've begun to use the stdlib module datetime instead of my home brewn classes. Since then a py2exe app doesn't run anymore: Traceback (most...
16
by: Jacob H | last post by:
Hi there list, I'm a beginning programmer, so please correct me if any of the following assumptions are wrong. Suppose I have the decimal number 255. Since integers in Python are 32 bits, it...
0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
0
by: lglmi | last post by:
'********************START OF BAS MODULE******************** Option Explicit Private Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long...
42
by: WindAndWaves | last post by:
Dear All Can you tell me why you use a class module??? Thank you Nicolaas ---
8
by: Salad | last post by:
I designed a small app and I wanted to do a BrowseFolder (see http://www.mvps.org/access/api/api0002.htm), basically do a file open diaglog and select a directory/folder. The problem is that you...
1
by: les | last post by:
Hello, I get the error on refering to the function created in the module. I've tried to declare the module but it is not halping. Please let me know what I'm doing wrong. This is my...
4
by: http://www.visual-basic-data-mining.net/forum | last post by:
Hi Does anyone know how to stay connected to the server and at the same time i can pass the string to and from the module to the form..... What I want: I put the connection at the...
0
by: bkriznar | last post by:
The problem could be reproduced like this: Web Service: public string HelloWorld(byte aB) { byte lB = aB; return "Hello World"; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.