Adding and running functions at runtime

John Dunn jhndnn at yahoo.com
Wed Jun 12 19:27:43 CEST 2002


Hello-

I am currently embedding Python in my C++ application and would like
to add function 'scriplets' defined by strings and then call them from
C. Let me explain what I am trying to do -

1. load a string that defines a function ( or functions )
2. get the function object for a defined function
3. call function with arguments
4. get return value and modified argument values

so the code might look something like

const char * funcs = 
"def foo( arg1, arg2 )\n"
"    arg2 = arg2\n"
"def foo2( arg1, arg2 )\n"
"    return arg1+arg2\n"

Mythical_PyLoad( funcs );
PyObject* foo = Mythical_PyGet_Function( "foo" );
PyObject* foo2 = Mythical_PyGet_Function( "foo2" );
PyOjbect* args = some code here to create arguments
// this should return the sume of the args
PyObject* result = PyEval_CallObject( foo2, args ); 
// this should modify the second arg
PyObject* result = PyEval_CallObject( foo, args ); 

I have been able to load code using Py_CompileString, but that appears
to run inline code, and I couldn't get it to return a value or modify
something in the dictionary.

I hope that makes sense...

Thanks for any help-

John
--
John Dunn
Peak Audio, Inc. 
johnd at peakaudio.dontspamme.com
http://www.peakaudio.com



More information about the Python-list mailing list