Calling local functions from the C API

Jon Parise jon at csh.rit.edu
Sat Nov 16 20:13:24 EST 2002


On Sat, Nov 16, 2002 at 11:24:46PM +0100, Martin v. Loewis wrote:

> Jon Parise <jon at csh.rit.edu> writes:
> 
> > I'm familiar with calling functions using PyObject_CallObject().
> > I generally use PyImport_ImportModule() to import a callable object
> > from some module before executing it.
> > 
> > However, I'm unclear as to how to go about getting a callable object
> > from the current local scope (i.e. not from a named module).
> > 
> > Help, please. =)
> 
> By "local scope", do you mean as in "local variable"? I.e. you do
> 
> def foo():
>   def bar()
>     pass
>   myext.somefunc()
> 
> and then you want the somefunc, which is implemented in C, to access
> bar??? That isn't supported. It is possible, but you will have to read
> the source yourself to find out how to do it.
> 
> Instead, I recommend that you do
> 
>   myext.somefunc(bar)
> 
> instead, i.e. passing the callable as an argument to the extension.

I think that's roughly what I'm saying.  For example:

def SomeFunction():
    pass

... gets evaluated by PyRun_RunString().  Later on, I'd like to call
SomeFunction from C (embedded Python interpreter).

Is that still not possible without playing with stack frames and such?

-- 
Jon Parise (jon at csh.rit.edu)  ::  http://www.csh.rit.edu/~jon/




More information about the Python-list mailing list