[Pyrex] Casting Python sequence to pointer and vice versa
Greg Ewing
greg.ewing at canterbury.ac.nz
Sat Mar 3 00:25:24 UTC 2007
Christian Heimes wrote:
> problem 1: The cdef extern from "stdlib.h" block isn't intuitive.
> Although I know that the C functions are declared in stdlib.h I had to
> google for the correct declarations.
You have to know how to translate C declarations
into Pyrex declarations. There's not much I can do
about that, other than improve the Pyrex documentation
if you can point out something that wasn't clear.
>
> problem 2 (the nasty gotcha): The usual malloc(sizeof(ULong)*size)
> statement failed with a cryptic error message.
Can you show me the exact Pyrex code you were using?
The following compiles okay for me:
cdef extern from "stdlib.h":
ctypedef unsigned long size_t
void *malloc(size_t size)
ctypedef unsigned long ULong
cdef void blarg(int size):
cdef void *p
p = malloc(sizeof(ULong)*size)
> # cast Python block of unsigned longs (k) to unsigned long pointer
> # return size or -1 in the case of an error
> size = Py2Ptr(block, 'k', v)
I haven't tried to provide anything like this because there
are so many different ways that you might want to convert
between Python data and C data. But I'll think about your
suggestion.
Thanks,
Greg
More information about the Pyrex
mailing list