I never mind being corrected/updated/put-in-my-place with such a great answer. <br><br>So there you have it :)<br><br>Cheers!<br><br><div class="gmail_quote">On Mon, Jun 1, 2009 at 3:09 PM, Dag Sverre Seljebotn <span dir="ltr"><<a href="mailto:dagss@student.matnat.uio.no">dagss@student.matnat.uio.no</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">Chris Colbert wrote:<br>
> python strings are automagically converted to char* by cython.<br>
</div>I'll beat Stefan to it and note some very important points here. This<br>
applies if you actually have a string (e.g. could be stored in "unicode"<br>
in Py2), not if you have raw byte data ("bytes" in Py3).<br>
<br>
1) A C char* is not a string as such, just a sequence of bytes. The C<br>
library should have a definition of what kind of encoding it wants its<br>
strings in, and one should call the library like this:<br>
<br>
encoded = mystr.encode(encoding)<br>
cdef char* encoded_buf = encoded<br>
c_func(encoded_buf)<br>
<br>
2) Note that char* does not say anything about releasing memory etc.<br>
I.e., the following will likely crash:<br>
<br>
c_func(mystr.encode(encoding))<br>
<br>
because the temporary object returned from the encode method is<br>
deallocated. *Always* keep a reference to the original Python object for<br>
the duration someone could use the char*.<br>
<br>
(Perhaps somebody could make this a FAQ entry in the wiki if it is not<br>
already?)<br>
<font color="#888888"><br>
Dag Sverre<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Cython-dev mailing list<br>
<a href="mailto:Cython-dev@codespeak.net">Cython-dev@codespeak.net</a><br>
<a href="http://codespeak.net/mailman/listinfo/cython-dev" target="_blank">http://codespeak.net/mailman/listinfo/cython-dev</a><br>
</div></div></blockquote></div><br>