Reputation: 1341
I have a python, ctypes wrapped function to a C API taking a void* parameter for a class X, e.g. C:
myFunc(void* aXHandle); //where X is a class, of class X.
For the class X, I have access to a swigged API and can get the myX.this, which is of type SwigPyObject.
How do I cast this so I can call my C-API function using ctypes and using a ctypes c_void_p as an argument? In Python, I thought something like: aXObjectThis = myX.this
myHandle = cast(aXObjectThis, c_void_p)
but that does not work; getting "wrong type in argument 1"
Upvotes: 1
Views: 1715
Reputation: 34260
The __int__
method of SwigPyObject
is handled by SwigPyObject_long
, defined in pyrun.swg. It returns PyLong_FromVoidPtr(v->ptr)
, so you should be able to use the following:
myHandle = c_void_p(int(myX.this))
More conveniently ctypes looks for an _as_parameter_
attribute. You could set this on the instance or as a property:
type(myX)._as_parameter_ = property(
lambda self: c_void_p(int(self.this)))
The simply call myFunc(myX)
.
Upvotes: 3