Getting a code obj's function?

Bjoern Giesler un4e at rzstud1.rz.uni-karlsruhe.de
Fri Dec 15 04:54:56 EST 2000


Hi,

Fredrik Lundh <fredrik at effbot.org> wrote:
: Bjoern Giesler wrote:
:> ...is there a standard way to get the function object that a code object is
:> associated with? On exception, I only get the code object from the
:> traceback.

: Use co_name:

no, this doesn't work... I need the function _object_, not its _name_. I
can't use the name to find the object, since the following...

[snip]
import sys
import exceptions

def spam():
    def eggs():
        raise exceptions.Exception
    eggs()

try:
    spam()
except:
    tb = sys.exc_traceback
    while tb.tb_next:
        tb = tb.tb_next
    code = tb.tb_frame.f_code
    print code.co_name
[snip]

...produces "eggs", and not "spam.eggs" (as would be better, IMHO, but alas,
it doesn't).

I need the function object because I want to replace the function object's 
code object. :-)

Regards,
		--Björn



More information about the Python-list mailing list