[Pyrex] How to deal with a void *

Filip Wasilewski filip at ftv.pl
Thu May 11 17:03:34 CEST 2006


Hi Travis,

>  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.
[...]

I think cObjects can be used to pass void pointers between Python
functions. See http://docs.python.org/api/cObjects.html for API
description. Your code may then look like (untested):

def get_resource():
    cdef void* resourcePtr
    resourcePtr = *initResource(....)
    return PyCObject_FromVoidPtr(resourcePtr, NULL)

def do_somestuff(object resource):
    cdef void* resourcePtr
    if PyCObject_Check(resource):
       resourcePtr = PyCObject_AsVoidPtr(resource)
       [do the stuff]

       
cheers,
fw




More information about the Pyrex mailing list