On May 16, 2008, at 10:29 PM, Stefan Behnel wrote: > Hi, > > Robert Bradshaw wrote: >>>> --- a.pyx --- >>>> >>>> def foo(x): >>>> if x > 0: >>>> return "good" >>>> else: >>>> return "bad" >>>> ------------- >>>> >>>> import a >>>> print "3 is %s" % a.foo(3) >>>> >>>> won't work in both Py2 and Py3, which I think it should. >>>> "Principle >>>> of least surprise." >> >> What it does mean is that you have to ship two separate sets of C >> files, > > Not at all. You just have to state what you mean *in your source > file*, i.e. > in your Cython source. If you say "I want this literal to be a byte > string", > you will get a byte string in both Py2 and Py3. If you say "I want > this > literal to be a unicode string", you will get a unicode string in > both Py2 and > Py3. How is that a surprise?
I think we're all OK on being able to specify byte string or unicode string. It's a question of what happens when you don't specify one or the other. > > Just because your code assumes that a byte string is the same as a > unicode > string does not mean Cython has to take measures to fix this for you, > especially in a way that you might or might not have intended. Be > explicit. > > You now have a number of ways to say what a literal should be, > based on the > Py2 syntax + the 'b' prefix of Py3: > > u"abc" # a unicode string > "abc" # a byte string > b"abc" # a byte string I'm suggesting "abc" is a byte string when linked against Py2, and a unicode string when linked against Py3. This way string literals from the module have the same type as string literals in the ambient python environment. There is no way to say that in the above proposal. > > You can do > > from __future__ import unicode_literals > > u"abc" # a unicode string > "abc" # a unicode string > b"abc" # a byte string > > > This is actually different from Py3 (and I think in line with 2.6) > in that > both the 'u' prefix and the 'b' prefix are allowed at the same > time. I think > that's ok, you will just have to use them wisely. Some style guide > policies > will save your project here. ;) > > Stefan > _______________________________________________ > Cython-dev mailing list > [email protected] > http://codespeak.net/mailman/listinfo/cython-dev _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
