Chris Colbert wrote:
> python strings are automagically converted to char* by cython.
I'll beat Stefan to it and note some very important points here. This
applies if you actually have a string (e.g. could be stored in "unicode"
in Py2), not if you have raw byte data ("bytes" in Py3).
1) A C char* is not a string as such, just a sequence of bytes. The C
library should have a definition of what kind of encoding it wants its
strings in, and one should call the library like this:
encoded = mystr.encode(encoding)
cdef char* encoded_buf = encoded
c_func(encoded_buf)
2) Note that char* does not say anything about releasing memory etc.
I.e., the following will likely crash:
c_func(mystr.encode(encoding))
because the temporary object returned from the encode method is
deallocated. *Always* keep a reference to the original Python object for
the duration someone could use the char*.
(Perhaps somebody could make this a FAQ entry in the wiki if it is not
already?)
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev