Calling local functions from the C API

Martin v. Loewis martin at v.loewis.de
Sun Nov 17 03:02:17 EST 2002


Jon Parise <jon at csh.rit.edu> writes:

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

Ah, so you have a char* that has Python source code, and you want to
run a specific function in it.

For such questions, always ask yourself how you would achieve this
effect in pure Python, and then you'll see how to do it in C.

In Python, you could write

d = {}
exec sourcecode in d
func = d['SomeFunction']
func()

Now just do the same in C. Which of the four steps is unclear to you?

Regards,
Martin



More information about the Python-list mailing list