Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > getting from code object to class/function

Reply
Thread Tools

getting from code object to class/function

 
 
Skip Montanaro
Guest
Posts: n/a
 
      04-07-2004
The trace module allows you to print all functions which were called at
least once during a program's run. I just checked in a change to also track
caller/callee relationships and display then at program exit.

Both types of output suffer from the fact that all the trace function gets
is the current frame. From there it can find the code object and then the
source filename and function name. There is, however, no direct way back
from the code object to (for example) the class and method which "own" that
object. I didn't see anything obvious in the inspect module that would
allow me to worm around this problem.

A not perfect solution would seem to be: if the first element of the code's
co_varnames attribute is "self" and the object in question is an instance,
work back to the class and grab the class name (which is only available
indirectly via str()), then use it to embellish the function name found in
the code object.

I'm open to other suggestions.

Thx,

Skip

 
Reply With Quote
 
 
 
 
Michael Hudson
Guest
Posts: n/a
 
      04-07-2004
Skip Montanaro <(E-Mail Removed)> writes:

> The trace module allows you to print all functions which were called at
> least once during a program's run. I just checked in a change to also track
> caller/callee relationships and display then at program exit.
>
> Both types of output suffer from the fact that all the trace function gets
> is the current frame. From there it can find the code object and then the
> source filename and function name. There is, however, no direct way back
> from the code object to (for example) the class and method which "own" that
> object. I didn't see anything obvious in the inspect module that would
> allow me to worm around this problem.


gc.get_referrers?

It's impossible in general, of course, because you can using the 'new'
module construct multiple functions that refer to the same code
object.

Cheers,
mwh

--
[1] If you're lost in the woods, just bury some fibre in the ground
carrying data. Fairly soon a JCB will be along to cut it for you
- follow the JCB back to civilsation/hitch a lift.
-- Simon Burr, cam.misc
 
Reply With Quote
 
 
 
 
Michael Hudson
Guest
Posts: n/a
 
      04-08-2004
Skip Montanaro <(E-Mail Removed)> writes:

> >> There is, however, no direct way back from the code object to (for
> >> example) the class and method which "own" that object. I didn't see
> >> anything obvious in the inspect module that would allow me to worm
> >> around this problem.

>
> mwh> gc.get_referrers?
>
> Interesting idea.


I'm not sure it'll work though: I don't think code objects participate
in GC, so I don't know if function's traverse function call the
callback on the code object.

That said, I've just tried it, and it does work

Cheers,
mwh

--
We've had a lot of problems going from glibc 2.0 to glibc 2.1.
People claim binary compatibility. Except for functions they
don't like. -- Peter Van Eynde, comp.lang.lisp

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting a module's code object Arnaud Delobelle Python 2 08-25-2011 03:33 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Problem getting cookie from code-behind (worked in code-beside) Alan Silver ASP .Net 1 09-15-2005 05:23 PM
Object reference not set to an instance of an object On Code Page Register Tag? jw56578@gmail.com ASP .Net 2 05-16-2005 04:12 PM
Getting name of object instance from within object method Martin Javascript 6 08-20-2004 12:24 PM



Advertisments