getting from code object to class/function

Skip Montanaro skip at pobox.com
Wed Apr 7 12:37:03 EDT 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




More information about the Python-list mailing list