Re: [capi-sig] Code for a Python function

On Jul 15, 2011, at 11:59 PM, Campbell Barton <ideasman42@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@python.org http://mail.python.org/mailman/listinfo/capi-sig
participants (1)
-
Enrico Granata