[capi-sig] Code for a Python function

Campbell Barton ideasman42 at gmail.com
Sat Jul 16 08:59:10 CEST 2011

Hi Enrico,
Officially, I think what you want to do isn't really supported by python.
- What if the python function is loaded from a pyc with no source code?
- What if the function is defined in a string which is executed but
does not correspond to a valid __file__ you can open and inspect.

In practice you can probably manage it if make a few assumptions.

You can access these attributes (or their C values directly):

Personally I'd not worry about the PyCodeObject changing (though
without more details on what you do its hard to say).

Or you could import the 'inspect' module via the C api and call its
getsource() function, however 'inspect' just makes a guess too and
isn't guaranteed to give the correct result, this at least gets around
changes in PyCodeObject but assumes a full python installation.

More information about the capi-sig mailing list