[Pyrex] How to deal with a void *
Tomasz Primke
tprimke at interia.pl
Fri May 12 04:08:57 CEST 2006
> 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.
> Just like him, when I try to return the void * directly I get and
> error that it can't be turned into a PyObject.
Just as Stefan Behnel has written, void* is not a PyObject.
I don't know, what exactly you're doing in your code, but if you deal with C
pointers, you can return them only from C functions in Pyrex, that means
you have do declare such a functions using cdef statement:
cdef some_C_type* func_name ( ... ):
Such a functions will be accessible only in your Pyrex code, not in Python.
> 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
>
> However I'not really sure if this is safe or if there is a chance of
> the value getting mangled in the casting to long and back.
According to my knowledge of C, I doubt it.
Have you tried:
cdef void *foo
cdef unsigned long int *bar
foo = initResource()
bar = <unsigned long int*> foo
and then refer to bar[0] when you need unsigned long int value?
I'm doing such a "tricks" in my code and they work.
Best regards,
Tomek
More information about the Pyrex
mailing list