Getting a code obj's function?

Bjoern Giesler un4e at
Fri Dec 15 10:54:56 CET 2000


Fredrik Lundh <fredrik at> wrote:
: Bjoern Giesler wrote:
:> 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...

import sys
import exceptions

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

    tb = sys.exc_traceback
    while tb.tb_next:
        tb = tb.tb_next
    code = tb.tb_frame.f_code
    print code.co_name

...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. :-)


More information about the Python-list mailing list