[C++-sig] Convert C++ Class to exposed class and back

Marcus Jannes jannes80 at hotmail.de
Tue Feb 12 20:07:36 CET 2008


Okay, let me explain in detail then:
I got an existing c++ library where there is a class DerivType which i want exposed to python.
There is a function i also want exposed

void fEval( ddf_FctPtr f, interval, interval&)

where the first parameter is a function pointer which is typedef'ed:

typedef DerivType (*ddf_FctPtr)(const DerivType&);

I want to do this in python (argument and return type should be of class DerivType):
def f(x):
   x_temp = DerivType(x)
   return x_temp*x_temp
fEval(f,interval1,interval2)

Now the wrapper should look something like this:

PyObject* _pyfunc_ptr = NULL;

DerivType _pycall(const DerivType& d){
	DerivType c_result;
	//build Python data from C++ data:
	bp::object o(d);
	result = PyEval_CallObject(_pyfunc_ptr, o.ptr()); 
	c_result = bp::extract(result);//http://www.boost.org/libs/python/doc/v2/extract.html
	return c_result;
};

static PyObject *eval(PyObject *self, PyObject *args){
	PyObject *ddf_function;
		
	if (!PyArg_ParseTuple(args,"O"
				  ,&ddf_function))
	{ 
		return NULL; 
	}

	if(!PyCallable_Check(ddf_function)){
		PyErr_Format(PyExc_TypeError,"ddf_function is not callable function");
		return NULL;
	}
	
	_pyfunc_ptr = ddf_function; //cache new callback
	interval i1(10); interval i2; //just for testing
	fEval(_pycall,i1,i2);
	return Py_BuildValue("");
};

Well, i still couldn't get this to work at runtime yet. I am not sure if it is even possible, but i got an example from a book where there is a similar case using double data instead of a class (DerivType) and it seems like doing the trick. If this can be done with boost.python instead of C API or you got any other comments please let me know. 


----------------------------------------
> Date: Tue, 12 Feb 2008 09:07:22 -0500
> From: seefeld at sympatico.ca
> To: c++-sig at python.org
> Subject: Re: [C++-sig] Convert C++ Class to exposed class and back
> 
> Marcus Jannes wrote:
>> Thank you, i think this is exactly what i need. Regarding your question why i want to use it in this way:
>> It is because i want to convert Python function objects to C++ function pointers which get passed to a another function and this seems to be the way to do it. The boost.python FAQ (see: http://www.boost.org/libs/python/doc/v2/faq.html ) states that it isnt possible, unless i misunderstood it. So i am trying it in C API. Now i should be able to :-). If there is any better way to do it, i would be happy to know.
> 
> I think you misunderstood. The problem is not with the API, but with the 
> underlying object model: As Dave notes in the FAQ, a (C, C++) function 
> pointer is not an object, and thus has no state to carry around.
> Unless your function takes not only a function pointer but also some 
> closure argument (which would then carry that state), you are out of luck.
> 
> In contrast, if you do get to pass a closure argument, you can pass a 
> python (callable) there, and then implement the function whose pointer 
> you pass such that it simply invokes that callable.
> 
> In either case, the problem is the same, no matter whether you try to 
> solve it with Python's C API or boost.python.
> 
> HTH,
> 		Stefan
> 
> -- 
> 
>        ...ich hab' noch einen Koffer in Berlin...
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig

_________________________________________________________________
Kostenlose Messenger Emoticons! Hier downloaden!
http://messenger.live.de/mein/


More information about the Cplusplus-sig mailing list