alright I figured it out, I just dont dereference the function pointer
before calling it. I don't understand why that works though...
At any rate, here is a self contained working example for future googlers
############
# cytypestest.pyx
############
import ctypes
libc = ctypes.CDLL('libc.so.6')
ctypedef int (*timefuncptr)()
cdef timefuncptr mytime
mytime = (<timefuncptr*><size_t>ctypes.addressof(libc.time))[0]
print mytime()
#########
# EOF
#########
$cython cytypestest.pyx
$gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing
-I/usr/include/python2.6 -o cytypestest.so cytypestest.c
$python
>>> import cytypestest
1255008817
On Thu, Oct 8, 2009 at 3:05 PM, Chris Colbert <[email protected]> wrote:
> Ok, tried both of those methods, and they pass cython compilation, but
> then I get this error from gcc:
>
> brucewa...@broo:~/Desktop$ gcc -shared -pthread -fPIC -fwrapv -O2
> -Wall -fno-strict-aliasing -I/usr/include/python2.6 -o cytypestest.so
> cytypestest.c
> cytypestest.c: In function ‘initcytypestest’:
> cytypestest.c:707: error: subscripted value is pointer to function
>
> Cheers!
>
> Chris
>
>
> On Thu, Oct 8, 2009 at 2:35 PM, Dag Sverre Seljebotn
> <[email protected]> wrote:
>> Dag Sverre Seljebotn wrote:
>>> Chris Colbert wrote:
>>>> Given our previous conversation, it will probably be easier to use
>>>> ctypes to get the function handles for dynamically loaded libraries
>>>> and *somehow* get that function pointer into Cython.
>>>>
>>>> My attempt is like so (for a simple example) but Cython is complaining
>>>> during compilation:
>>>>
>>>>
>>>> from ctypes import *
>>>>
>>>> cdef extern from "Python.h":
>>>> void* PyLong_AsVoidPtr(object)
>>>>
>>>> cdef int a
>>>>
>>>> libc = CDLL('libc.so.6')
>>>> timefunc = libc.time
>>>>
>>>> cdef object tptr = ctypes.addressof(timefunc)
>>>> cdef int (*mytime)()
>>>> mytime = PyLong_AsVoidPtr(tptr)
>>>>
>>> I think this should do it:
>>>
>>> ctypedef int (*timefuncptr)()
>>> cdef timefuncptr mytime
>>> mytime = <timefuncptr>ctypes.addressof(libc.time)
>> Sorry, that should be
>>
>> mytime = <timefuncptr><size_t>ctypes.addressof(libc.time)
>>
>> i.e. convert to typed integer of pointer size before converting to
>> pointer. The same goes for the example below.
>>>
>>> That is, unless ctypes.addressof returns the address of the *pointer*
>>> to the function stored in libc.time (I'm not familiar enough with
>>> ctypes). In that case it is instead:
>>>
>>> mytime = (<timefuncptr*>ctypes.addressof(libc.time))[0]
>>>
>>> Dag Sverre
>>>
>>
>> _______________________________________________
>> Cython-dev mailing list
>> [email protected]
>> http://codespeak.net/mailman/listinfo/cython-dev
>>
>
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev