cdef class Test:
def get_stuff(self):
cdef str id = "test"
cdef char* id2 = id <- error
This produces error:
'str' objects do not support coercion to C types (use 'bytes'?)
How this should work? From document http://docs.cython.org/src/tutorial/strings.html
I though this should work.
bytes is just a byte string. I think it's following Py3 now, so str ==
unicode, and bytes == str
'str' is a special type that is bytes in Py2 and unicode in Py3. It is
therefore not possible to assign it to a char* without prior treatment.
If you want a byte string, use bytes. If you use unicode, encode it
appropriately.
Note that there is some discussion going on amongst the core developers
regarding better ways to deal with this. As of now, nothing has been decided.
> How this should work? From document http://docs.cython.org/src/tutorial/strings.html
> I though this should work.
I can't find any reference to "cdef str" in that document.
Stefan
> > How this should work? From documenthttp://docs.cython.org/src/tutorial/strings.html
> > I though this should work.
>
> I can't find any reference to "cdef str" in that document.
>
I misunderstood that type str equals to python string (document says
this: cdef char* other_c_string = py_string).
How do you check what version of Cython you are using, btw?
Well, yes, read the extensive docs on string handling.
Admittedly, the most up-to-date tutorial for Cython 0.13 is not on the web
site yet, but here it is in text form:
http://hg.cython.org/cython-docs/raw-file/tip/src/tutorial/strings.rst
In short: switch to using bytes objects internally and convert unicode
string input to bytes when receiving it from Python space by encoding it in
a suitable encoding.
Stefan
Cass, 20.07.2010 23:01:
> Thanks!! It worked. Just used:
>
> py_byte_string = py_unicode_string.encode('UTF-8')
> cdef char* c_string = py_byte_string
Looks good.
> However, now I'm confused. After my last post, I read at (http://
> farmdev.com/talks/unicode/) that the default encoding for Python 2 is
> ASCII. Was that information wrong?
Yes. The default encoding in Python 2 is configurable and platform
specific. Don't rely on it being anything.
Stefan