Hi,
Robert Bradshaw wrote:
> On Apr 16, 2008, at 4:25 AM, Stefan Behnel wrote:
> I think our C files should always be pure ascii.
You mean with C string escapes?
>> That's the main reason why Py3 has a well defined "bytes" type and a
>> Unicode "str" type instead of a Unicode "unicode" type and an
>> underdefined
>> "str" type in Py2.
>>
>>> What hasn't been resolved is conversions
>>>
>>> cdef object o = s # s is a char*
>> Sure, the semantics are clear: char* is a byte sequence in C, so the
>> result is the equivalent of a byte sequence in Python: a byte
>> string, i.e.
>> a str object in Python2 and a bytes object in Py3.
>
> I understand this distinction. Technically a char* is a byte string.
> The problem is that people are going to want to implicitly handle
> unicode <-> char* much more often.
But they shouldn't do that. Python3 is very strict here. There is no automatic
conversion between bytes and str. You must be explicit about the way you want
to convert it. And believe me, they didn't break it doing that, they fixed it.
Doing magic in Cython would actually be unexpected in that light.
>>> (That would break a lot of
>>> code...including really nice code like declaring a function argument
>>> to be char*)
>> That would still accept any kind of byte string or a bytes object
>> in Py3, which is just fine IMHO.
>
> I think this significantly impacts usability. For example, if I have
> a function
>
> def foo(char* x):
> ...
>
> then users of my module won't be able to write foo("eggs") anymore,
> they will have to write foo(b"eggs") or even foo(x.encode('UTF-8'))
> if x is given to them from elsewhere.
That's fine, because your function expects a byte string. It cannot handle a
unicode string, and it even says so in its signature.
> Likewise, if I have
>
> def foo():
> cdef char* s
> ...
> return s
>
> Then the user won't be able to write
>
> print "The answer is %s" % foo()
>
> or
>
> foo() + "eggs"
But again, that's because of Python semantics, not of Cython semantics.
Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev