Hi,

[email protected] wrote:
> As *all* strings in cython code get converted to bytes, nothing works out.

Actuall, Python byte strings and unicode strings are equally well supported
in Cython, for both Py2 and Py3. Cython just uses (mainly) Py2.x syntax.


> I my case I have a c function like:
> 
> void foo(char *)
> 
> cdef c_foo "foo"(char *)
> 
> 
> cdef class A:
>    def __init__(self,arg0):
>      c_foo(arg0)

Unless you are dealing with binary data, you should not do this.


> calling from python with
> x = A('test')
> 
> in python the argument 'test' is of type str, cython expects the
> argument for c_foo to be of type bytes as char * is mapped to bytes.
> 
> Now, as I do not want to change all calls to all functions which take
> str arguments, I tried to convert 'test' from str to bytes.
> 
> arg0 = arg0.encode("ascii")
> 
> Result:
> TypeError: encode() argument 1 must be string, not bytes.

This is because you passed a bytes object into the encode() method, which
requires a text value as parameter. Try

        arg0 = arg0.encode(u"ascii")

This will work in both Py2 and Py3.


> Even worse:
> if isinstance(arg0, str):
> 
> actually checks if arg0 is of type bytes ...

Yes, because Cython uses the builtin names "str" and "unicode" of Python
2.x. They will be mapped to the Py3 'equivalents' as expected, though, so
if you test for the "unicode" type, this will continue to work in Py3.

Stefan

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to