[Cython] How to deal with byte strings and unicode strings at the API level

Stefan Behnel stefan_ml at behnel.de
Sat May 10 07:36:15 CEST 2008


Hi,

Lisandro Dalcin wrote:
> Stefan, many, many thanks for your explanations. I believe I've
> started to understand the whole beast. Please clarify me this:
> 
> Suppose I write this method in a pyx file:
> 
> def foo(char value[]): pass
> 
> and next call it (in a Py3 runtime env) in this two ways:
> 
> 1-  foo(b"abc")
> 
> 2-  foo("abc")
> 
> then (1) should be just fine, Cython should pass the raw C data as it
> comes. But in the case of (2), will/should Cython (or at the Python C
> API level) generate an error?

Yes, you will get a TypeError for (2). The Python3 equivalent is this:

    def foo(value): value = bytes(value)

Stefan


More information about the Cython-dev mailing list