[C++-sig] Fwd: passing pointers from python

Jim Bosch talljimbo at gmail.com
Fri Mar 19 04:22:19 CET 2010


On Fri, 2010-03-19 at 10:56 +0800, hitesh dhiman wrote:
> 
> 
> ---------- Forwarded message ----------
> From: hitesh dhiman <hitesh.dhiman.1988 at gmail.com>
> Date: Thu, Mar 18, 2010 at 11:39 AM
> Subject: passing pointers from python
> To: Development of Python/C++ integration <cplusplus-sig at python.org>,
> boost-users at lists.boost.org
> 
> 
> Hi all,
> i'm trying to wrap c++ functions that have pointer variables. The test
> example i'm using is shown below:
> int World::addition(int* a, int* b)
> {
> int z = *a + *b;
> return z;
> }
> 
> 
> Now, i defined it in my wrapper as :
> .def("addition", &World::addition)
> 
> 
> My code compiles, but when I try to execute it from python, i get this
> error:
> Python Argument types in World.Addition(World, int, int) did not match
> C++ signature: addition(class World {lvalue}, int* , int*)
> 
> 
> Is there a workaround for this?? 
> 

I can't think of an easy one - your C++ function signature implies that
it could change the arguments (otherwise, why pass by pointer?), but
Python integers are immutable objects, and can't be changed when passed
as arguments.  That's why Boost.Python isn't doing the conversion
automatically.

If your code doesn't need to actually modify its arguments - as is the
case in your example - you can of course write C++ wrappers that take
arguments by value and forward them as pointers, and just wrap those
functions for Python.  Note that you can wrap a free function that takes
a World& as its first argument as a member function of the World class;
there will be no difference between that and wrapping a true member
function on the Python side.


Jim Bosch





More information about the Cplusplus-sig mailing list