[Pyrex] How to deal with a void *
Stefan Behnel
behnel_ml at gkec.informatik.tu-darmstadt.de
Thu May 11 06:49:50 CEST 2006
Hi Travis,
Travis Caldwell wrote:
> I am trying to wrap a C API which uses void * as the type of an opaque
> token that gets passed around.
>
> Basically you have things like:
>
> void *initResource(....)
>
> int doSomething( void *resourceID,...)
>
> where you get a void * at resource creation and then do nothing with
> it excpet to pass it back into other API functions to identify the
> resource you wish the function to operate on.
> when I try to return the void * directly I get and
> error that it can't be turned into a PyObject.
Obviously, it's not a Python object.
> I can get the error message to go away by defining a C long varriable
> and casting the void * to that. ex:
>
> cdef void *foo
> cdef unsigned long bar
>
> foo = initResource()
> bar = <unsigned long> foo
Don't ever do these things.
A possible approach is to write an extension class that contains the pointer
as data field and pass that through the API calls.
cdef class MyContext:
void* _something
def initWrapper():
cdef void* c_resource
# ... create resource as 'c_resource'
context = MyContext()
context._something = c_resource
return context
def pyDoSomething(MyContext context, myarg):
doSomething(context._something, myarg)
Does that help?
Stefan
More information about the Pyrex
mailing list