[Pyrex] Convert tuple/list in primitive*

Lenard Lindstrom len-l at telus.net
Thu Dec 16 22:52:41 CET 2004


On Thu, 16 Dec 2004 10:21:54 -0500 Charles Moad <cmoad at indiana.edu> wrote:

> Probably another case of being answered before, but I can't find
> anything.  Given something simple like:
> 
> cdef extern c_setParam3fv "setParam3fv"(int param, float *value)
> 
> def setParam3fv(param, value):
>    cdef float args[3]
>    cdef int i
> 
>    i = 0
>    for val in value:
>       args[i] = val
>    c_setParam3fv(param, args)
> 
> This works fine and all, but I don't think this is my preferred way
> given a large list of floats since the python looping contruct is slow.
> 
> Is there a memcpy type trick I can do, or possibly a cast?
> 
No. Argument "value" could be any sequence or iterable, and
the only way to process it is one item at a time. Besides, each
entry in the sequence is a python float object (a wrapped double)
which must be converted to a C float. Even the Python C api lacks
list to C array mapping functions.

Lenard Lindstrom
<len-l at telus.net>





More information about the Pyrex mailing list