[Cython] automatic character conversion problem

Robert Bradshaw robertwb at math.washington.edu
Tue May 3 06:38:11 CEST 2011


On Fri, Apr 29, 2011 at 6:57 AM, Hans Terlouw <J.P.Terlouw at astro.rug.nl> wrote:
> Dear Cython developers,
>
> Recently I encountered a problem with Cython's automatic char* to string
> conversion (Cython version 0.14.1). I'll attach two sample source files. The
> first one, char2str_a.pyx prints "The quick...", just as I expected. But the
> second example prints "... lazy dog.". In the original situation I had a
> call to
> free() instead of the call to strcpy() which I use here for illustration
> purposes. Then I got unpredictable results. Apparently the Python string
> object
> keeps referring to the C char* a bit longer than I would expect. A previous
> version (0.11.2) didn't have this problem.

This is due to type inference, in the second example, p_str is
inferred to be of type char*.

- Robert

> cdef extern from "stdlib.h":
>   void free(void* ptr)
>   void* malloc(size_t size)
>
> cdef extern from "string.h":
>   char *strcpy(char *dest, char *src)
>
> def char2str():
>   cdef char *c_str_a = <char*>malloc(80)
>   cdef char *c_str_b = "The quick...   "
>   cdef char *c_str_c = "... lazy dog.  "
>
>   strcpy(c_str_a, c_str_b)
>
>   p_str = c_str_a
>   strcpy(c_str_a, c_str_c)
>   p_str = p_str.rstrip()
>   print p_str
>
>
>
> cdef extern from "stdlib.h":
>   void free(void* ptr)
>   void* malloc(size_t size)
>
> cdef extern from "string.h":
>   char *strcpy(char *dest, char *src)
>
> def char2str():
>   cdef char *c_str_a = <char*>malloc(80)
>   cdef char *c_str_b = "The quick...   "
>   cdef char *c_str_c = "... lazy dog.  "
>
>   strcpy(c_str_a, c_str_b)
>
>   p_str = c_str_a
>   strcpy(c_str_a, c_str_c)
>   print p_str.rstrip()
>
>
>
>


More information about the cython-devel mailing list