By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,402 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

getting from code object to class/function

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Skip Montanaro <sk**@pobox.com> 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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.