On Sep 13, 2009, at 12:39 PM, Stefan Behnel wrote: > > Lisandro Dalcin wrote: >> On Sun, Sep 13, 2009 at 7:55 AM, Stefan Behnel wrote: >>> One thing I see missing is how this would be handled: >>> >>> cdef str s = "some string" >>> cdef char* cs = s >>> >>> Should this simply result in a runtime error under Py3? >>> >>> Or would you forbid this and just raise a Cython compiler error (or >>> warning), stating that "bytes" should be used instead? Although, >>> this might >>> actually appear inside of a Py2-only or try-except block, so I >>> guess a >>> warning would be the most we can do. >> >> I'm inclined for a warning... and that warning would not be generated >> in this case: "cdef char*cs = <bytes>s" , right? > > Sure.
That could be bad, <bytes>s doesn't actually do a typecheck, especially if the bytes -> char* is eventually optimized. One should do <bytes?>s or <object>s (neither of which generate a warning). > >>> BTW, we shouldn't forget to adapt the .pxd files in Cython/Includes >>> accordingly, so that they return either "bytes" or "unicode", but >>> *never* >>> "str" (or "object", if we know it's a string type). >> >> Could you point to a couple of the C-API calls you are talking about? > > Things like the encoding functions, for example, that convert > between bytes > and unicode. Using str here would be wrong. > > Plus, changing the argument/return value types from "object" to the > right > types will allow Cython to do actual type checking. Often the type checking will be redundant with the type checking that happens inside the method, so I'm not so sure this is a good idea. > >>> And "str", "bytes" and "unicode" wouldn't be assignable to each >>> other, >>> right? Or would you also leave that to runtime? >> >> "bytes" <-> "unicode" (obviously?) would not be assignable, tough for >> the case of "bytes" <-> "str" or "str" <-> "unicode", we could >> generate similar Cython compile warnings as for the "[unsigned ]char >> *" conversions. > > Yes, I guess that's a similar case. I'd be inclined to outright disallow them, favoring requiring <bytes? > or <unicode?> or <object> cast. Currently, though, I can't think of any reason to type str/bytes/unicode variables at all. - Robert _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
