On Sep 11, 2009, at 11:39 PM, Stefan Behnel wrote:

> Hi,
>
> Robert Bradshaw wrote:
>> I'm surprised that str is
>> mapped to bytes in Python 3. What was the justification for this, or
>> is it just a bug? I think if
>>
>> def foo():
>>      return str, isinstance("abc", str)
>>
>> have different behavior in Cython and Python that there's a bug
>
> Define Python: Py2 or Py3?

Both. If I compile the module against Py2, it should behave as if it  
was a .py file under Py2, and if I compile the module under Py3, it  
should behave as if it were a .py file under Py3. Moving code  
from .py to .pyx should not change its behavior.

If I have this function in a module, in both Python 2 and Python 3 I  
have

 >>> foo() == str, True
True

> Python 2 is still the defining context for Cython, so "str" is a byte
> string and "unicode" is a Unicode string. If you do the above in  
> Py2, it
> will return True for a byte string and False for a unicode string.  
> If you
> do it in Cython, you will get the same result. And that's still  
> true when
> you compile your code for Py3. The same applies for the Cython code
>
>       isinstance(u"abc", unicode)
>
> which will return True in both environments. Given that "abc" is a  
> byte
> string in Cython, I'd be rather surprised to have
>
>       isinstance("abc", str)
>
> return True in Py2 and False in Py3, and

This returns True in both.

>
>       isinstance("abc", unicode)
>
> return False in Py2 and True in Py3.


This is an error in Py3.

I don't see "abc" as a byte string, I see it as a string literal. If  
it's used in a C context it's a byte string, and if used as a Python  
object it's a Python str. This is how we handle all other literals  
(e.g. large integer literals used as Python objects are not the same  
as large integer literals truncated to an int then used as a Python  
object).

- Robert

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

Reply via email to