|
From: John P. <joh...@st...> - 2006-02-23 00:08:53
|
Hi all,
I'm having trouble using Py_BuildValue to pass a SWIG-wrapped *C++
object* back to my python callback function. It says in the python docs
that I can pass a PyObject using the "o" string, but I'm not sure how I
can cast my C++ object to a PyObject from inside C++. What's the correct
way of doing that?
Here are some details: I'm setting up a callback mechanism for a Python
method to display selected status data from a C++ solver engine. In C++
I have written a class 'SolverReporter':
class SolverReporter{
SolverReporter();
virtual ~SolverReporter();
virtual int report(const SolverStatus &status) = 0;
}
Then I have written a subclass PythonSolverReporter with the constructor
PythonSolverReporter::PythonSolverReporter(PyObject *pyfunc){
this->pyfunc = pyfunc;
Py_INCREF(pyfunc);
}
and now, there's the 'report' method, and *this* is what I'm struggling
with:
int PythonSolverReporter::report(const SolverStatus &status){
PyObject *pyarglist, *pyresult;
pyarglist = Py_BuildValue("(o)",status); // <------------------ what
to do here?
pyresult = PyEval_CallObject(this->pyfunc,pyarglist);
Py_DECREF(pyarglist);
}
Usage of all this in Python would be like this, for example:
def show_something(status):
print "Solver status is",status.getIterationNum()
reporter = PythonSolverReporter(show_something)
simulation = Simulation(...)
simulation.solve(reporter)
Any suggestions -- even if it's a completely different way of doing
things -- would be really appreciated.
Cheers
JP
|