Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 10th, 2008, 01:55 PM
Okko Willeboordse
Guest
 
Posts: n/a
Default Get "code object" of class

To get the "code object" c of my_class I can do;

c = compile(inspect.getsource(my_class), "<script>", "exec")

This fails when inspect can't get hold of the source of my_class,
for instance when my_class is in a pyc file.


Is there another way of getting c?
  #2  
Old October 10th, 2008, 07:05 PM
Matimus
Guest
 
Posts: n/a
Default Re: Get "code object" of class

On Oct 10, 5:50*am, Okko Willeboordse <tr...@willeboordse.demon.nl>
wrote:
Quote:
To get the "code object" c of my_class I can do;
>
c = compile(inspect.getsource(my_class), "<script>", "exec")
>
This fails when inspect can't get hold of the source of my_class,
for instance when my_class is in a pyc file.
>
Is there another way of getting c?
Classes don't have a code object that is visible at run-time, at least
not that I know of. The class body is executed when the class is
created and then discarded. _Most_ of the time classes are created at
the module level. However, when a module is compiled, the class does
have a code object which is marshaled and placed inside of the
compiled code. If anybody knows of a simpler way, I would be
interested, but here is _one_ way you can get at the code object if
the class is in a compiled module:
Quote:
Quote:
Quote:
>>f = open("testclass.pyc", "rb")
>>import marshal
>>f.seek(8)
>>o = marshal.load(f)
>>o
<code object <moduleat 00D81DA0, file "testclass.py", line 2>
Quote:
Quote:
Quote:
>>dir(o)
['__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__',
'__hash__
', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr_
_', '__str__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts',
'co_filenam
e', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab',
'co_name', 'co_nam
es', 'co_nlocals', 'co_stacksize', 'co_varnames']
Quote:
Quote:
Quote:
>>o.co_consts
('CBase', <code object CBase at 00D81578, file "testclass.py", line
2>, 'C', <co
de object C at 00D81380, file "testclass.py", line 11>, 'D', <code
object D at 0
0D81530, file "testclass.py", line 15>, None)
Quote:
Quote:
Quote:
>># Get the code object for class 'C'
>>code_obj = o.co_consts[3]
I wouldn't recommend doing it this way though. It is very convoluted
and since the 'dir' method sometimes hides some of the attributes, I
wouldn't be surprised if the code object _was_ available and I just
don't know the name. Why do you even need the classes code object
anyway?

Matt
  #3  
Old October 11th, 2008, 08:55 AM
Okko Willeboordse
Guest
 
Posts: n/a
Default Re: Get "code object" of class

Thanks,

"Why do you even need the classes code object anyway?"

I need to instantiate and use the class in another process.
This other process doesn't has access to the py or pyc file
holding the m_class (source) code so I can't use pickle.

Something like;
In the first process (that has access to my_class (source) code) I do;

c = compile(inspect.getsource(my_class), "<script>", "exec")
s = marshal.dumps(c)
# Send s to other process

In the other process, lacking access, I do;

c = marshal.loads(s)
exec c in my_dict
o = my_dict["my_class"]()
# Use o
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles