On 25 August 2010 17:23, Robert Bradshaw <[email protected]> wrote:
> On Wed, Aug 25, 2010 at 12:57 PM, Lisandro Dalcin <[email protected]> wrote:
>> On 25 August 2010 16:16, Stefan Behnel <[email protected]> wrote:
>>> Lisandro Dalcin, 25.08.2010 21:00:
>>>> $ cython -3 tmp.pyx
>>>>
>>>> Error converting Pyrex file to C:
>>>> ------------------------------------------------------------
>>>> ...
>>>> cdef str a = "abc"
>>>>              ^
>>>> ------------------------------------------------------------
>>>>
>>>> /u/dalcinl/tmp/tmp.pyx:1:13: Cannot convert Unicode string to 'str'
>>>> implicitly. This is not portable and requires explicit encoding.
>>>
>>> Same thing I said before: if you request unicode string literals, you get
>>> unicode literals.
>>>
>>
>> But in Python 3, the the Python-level 'str' type actually  is an unicode 
>> string!
>>
>> So, cdef str a = "xyz" should definitely work. If not, once more,
>> writing Cython code that target both Python 2 and 3 runtimes is a
>> PITA. How should I write my code to get a byte string in Py2 and a
>> unicode string in Py3? Yes, I know, for that to make sense the string
>> should be pure ASCII, but that's a pretty common case (e.g. Python 3
>> stdlib is guaranteed to have all identifiers in the ASCII range)
>>
>> I'm likely missing something... To start: Is Cython -3 generated code
>> supposed to work in a Python 2 runtime? If the answer is yes, I think
>> we should provide mechanisms letting developers use cython -3 but
>> still support Python 2 without too much extra work.
>
> For one thing, the Python 3 print function is not supported for Python
> < 2.6 runtime environment. However, the -2 mode will support both the
> Py2 and Py3 runtimes for the foreseeable future, these flags are just
> about the input source (in the case that the two have different
> semantics).
>

This is from my own code (mpi4py):

from array import array as arraytype
ary = arraytype('i', [0])

Thanks to the special handling of string literals under -2, this code
works (at runtime) in both Python 2 and Python 3...

Howerver, when using cython -3, I get a failure under Python 2:

TypeError: array() argument 1 must be char, not unicode



-- 
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to