[Pyrex] Casting Python sequence to pointer and vice versa

Christian Heimes lists at cheimes.de
Sat Mar 3 23:41:51 UTC 2007


Greg Ewing schrieb:
> 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.

I didn't know the signature of the C functions so I had to look them
update. IMO malloc & friends are a common construct and often used. I
suggest that you either add them to the documentation or declare them as
cdef in Pyrex's core.

cdef extern from "stdlib.h":
    ctypedef unsigned long size_t
    void *malloc(size_t size)
    void free(void* ptr)
    size_t sizeof(void *)

>> 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:

I was using an older pyrex version from my ubuntu installation. It works
as expected in 0.9.5.1a.

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

As a Pyrex newbie I missed some good examples in the docs or example
directory. I tried different ways like

cdef unsigned long *v
v = pythonlist

but it didn't work. I expected Pyrex to do all the dirty walk like
malloc() for me. It looks like I coded too much Python in the path and
forgot all about manual memory management and pointers on pointers on
structs ... :)

Christian




More information about the Pyrex mailing list