Discussion:
How to write a Cython wrapper for a C++ function that has a function pointer argument
kstueve
2011-12-12 16:09:42 UTC
Permalink
While writing a Cython wrapper for primesieve (http://code.google.com/
p/primesieve/) I have been unable to wrap the generatePrimes function
because of the function pointer argument.
I get "Cannot convert 'void' to Python object" on the last line
below. Please help me. I have been reading the Cython tutorials and
Googling, but haven't found an example that demonstrates this case.

cdef extern from "primesieve/PrimeSieve.h":
cdef cppclass PrimeSieve:
PrimeSieve()

#Prime number generation methods (32-bit & 64-bit ).
void generatePrimes(uint32_t, uint32_t, void (*callback)
(uint32_t))
void generatePrimes(uint32_t, uint32_t, void (*callback)
(uint32_t, void*), void* cbObj)
void generatePrimes(uint64_t, uint64_t, void (*callback)
(uint64_t))
void generatePrimes(uint64_t, uint64_t, void (*callback)
(uint64_t, void*), void* cbObj)

cdef class primesieve:
def __cinit__(self):
self.thisptr = new PrimeSieve()

def __dealloc__(self):
del self.thisptr

cdef generatePrimes(self, uint32_t startNumber, uint32_t
stopNumber, function callback):
return self.thisptr.generatePrimes(startNumber, stopNumber,
callback)
--
To post to this group, send an email to sage-***@googlegroups.com
To unsubscribe from this group, send an email to sage-devel+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org
Volker Braun
2011-12-12 16:29:40 UTC
Permalink
return self.thisptr.generatePrimes...
tries to return a C void, but
cdef generatePrimes
is implicitly declared as returning a python object (try cdef
void generatePrimes or return None)
--
To post to this group, send an email to sage-***@googlegroups.com
To unsubscribe from this group, send an email to sage-devel+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org
kstueve
2011-12-12 21:17:17 UTC
Permalink
Thanks Volker. Now I have the following code. I need to take the
Python function callback which takes one argument and create a void
(*callback) (uint32_t) function pointer to pass to
self.thisptr.generatePrimes. How do I do this? Right now I get the
error "Cannot convert Python object to 'void (*)(uint32_t)'" on the
last line.

def generatePrimes(self,uint32_t startNumber, uint32_t stopNumber,
callback):
self.thisptr.generatePrimes(startNumber, stopNumber,callback)
Post by Volker Braun
 return self.thisptr.generatePrimes...
tries to return a C void, but
 cdef generatePrimes
is implicitly declared as returning a python object (try cdef
void generatePrimes or return None)
--
To post to this group, send an email to sage-***@googlegroups.com
To unsubscribe from this group, send an email to sage-devel+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org
Volker Braun
2011-12-13 10:51:34 UTC
Permalink
Well a Python function does not have a function pointer, only C functions
do. You can wrap some canned C callbacks or have a cdef function callback
that communicates with Python. Whats best really depends on what the
callback does.
--
To post to this group, send an email to sage-***@googlegroups.com
To unsubscribe from this group, send an email to sage-devel+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org
Loading...