[capi-sig] Code for a Python function

Enrico Granata egranata at apple.com
Sat Jul 16 19:22:03 CEST 2011

On Jul 15, 2011, at 11:59 PM, Campbell Barton <ideasman42 at gmail.com> wrote:

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

Which is exactly the scenario I envision, unfortunately.

> In practice you can probably manage it if make a few assumptions.
> You can access these attributes (or their C values directly):
> myfunc.__code__.co_filename
> myfunc.__code__.co_firstlineno

However these are not going to help me if the function data comes from standard input instead of from a file

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

I guess this is not viable either, still thanks a lot for your helpful hints.

> _______________________________________________
> capi-sig mailing list
> capi-sig at python.org
> http://mail.python.org/mailman/listinfo/capi-sig

More information about the capi-sig mailing list