[Pyrex] char * from python

Chris Perkins chrisperkins99 at gmail.com
Fri Dec 1 19:05:55 UTC 2006


On 12/1/06, Brent Pedersen <bpederse at gmail.com> wrote:
> hi, i've just been tinkering with pyrex, trying to do this (below).
> but it segfaults on the sscanf line in pyscanf.
> what am i doing wrong? is this more involved than my naive approach?
>
> thanks,
> -brent
>
> ##############################
> # pyscanf.pyx
>
> cdef extern from "stdio.h":
>     int sscanf (char *,char *,...)
>
> def pyscanf(char * str):
>     cdef char * q
>     cdef char * s
>     cdef int my_int
>     cdef float my_float
>     sscanf(str,"%s %s %i %f",q,s,&my_int,&my_float)
>     return [q,s,my_int,my_float]
>
>
> ####### run with ###########
> from pyscanf import pyscanf
> print pyscanf("asdf asdf 123 12.22")

You need to allocate space for p and s.  sscanf tries to copy "asdf"
in to the memory location that q points to, but you have left it
uninitialized.  Same for s.

Chris Perkins



More information about the Pyrex mailing list