Callback from C++ to Python, how to pass args?
Bram Stolk
bram at sara.nl
Wed Aug 16 04:56:00 EDT 2000
Hi Christof, thanks for the help.
As for my problem, I will try to state it more clearly.
- I use SWIG to create python support for my class, including shadow classes
- I use a python script to create such an object:
# python script
instance = MyClass();
- I register a python func with an object at some other class:
oc = OtherClass();
oc.SetCallback(mypythonfunc, instance);
- Now, I have a piece of C++ code in OtherClass, that should
callback the python script func, with a MyClass object as argument.
OtherClass::Call(void)
{
// make call to registered pythonfunc with instance as arg.
}
It turned out, I could use this code:
PyObject *func;
PyObject *instance;
PyObject *arglist = Py_BuildValue
(
"O",
instance
);
PyObject *result = PyEval_CallObject((*ip).func, arglist);
But only if I define some typemaps like:
%typemap(python,in) PyObject *func
{
$target = $source;
}
and same one for PyObjects called 'instance'.
This strikes me as odd, as the typemaps do not seem to do much.
Using this scheme, I can callback to python funcs, and in those
python funcs, use the MyClass argument as a proper python object.
# callback func
def mypythonfunc(myclassobj)
print myclassobj;
myclassobj.somememberfunc();
This seems to work ok now.
Thanks again,
Bram
--
------------------------------------------------------------------------------
Bram Stolk, VR Specialist.
SARA Academic Computing Services Amsterdam, PO Box 94613, 1090 GP AMSTERDAM
email: bram at sara.nl Phone +31-20-5923059 Fax +31-20-6683167
"I heard if you play the NT-4.0-CD backwards, you get a satanic message."
"Thats nothing, if you play it forward, it installs NT-4.0"
------------------------------------------------------------------------------
More information about the Python-list
mailing list