[Python-Dev] PEP 414
Vinay Sajip
vinay_sajip at yahoo.co.uk
Sat Mar 3 11:39:45 CET 2012
Lennart Regebro <regebro <at> gmail.com> writes:
> I'm not sure that's true at all. In most cases where you support both
> Python 2 and Python 3, most strings will be "native", ie, without
> prefix in either Python 2 or Python 3. The native case is the most
> common case.
Sorry, I didn't make myself clear. If you import unicode_literals, then in both
2.x and 3.x code, an unadorned literal string is text, and a b-adorned literal
string is bytes. My assertion was based on that assumption - the text (Unicode)
case then becomes the most common case.
> > In terms of practicality, it is
> > IMO quite practical (assuming 2.5 / earlier support can be dropped) to
> > move to a
> > 2.6+/3.x-friendly codebase, e.g. by using Armin's python-modernize.
>
> I think there is some misunderstanding here. The binary/unicode/native
> separation is only possible on Python 2.6 and 2.7 at the moment,
> unless you use function wrappers like b().
Right, and that is a possible option for 2.5 and earlier: though obviously not a
desirable one from an aesthetic point of view!
What I meant (and should have said) was: if you can drop support for 2.5 /
earlier, a lib2to3 fixer-based approach brings your 2.x code into the 3-friendly
region of 2.x - 2.6 and 2.7. You can then, using the unicode_literals approach,
arrive at a common codebase for 2.6+ and 3.x which is not slow to run (see my
other post on ported Django test run performance), and clean (looks just like 3
code, pretty much, and means the same, as far as string literals are concerned).
Where you hit native string requirements, apply the wrapper.
I don't actually use python-modernize, as I independently developed fixers when
doing the Django port late last year. I initially wrote a fixer to transform
u'xxx' to u('xxx') (as I was assuming 2.5 support was needed), and then, when it
appeared likely that Django would drop 2.5 support after 1.4, I wrote a fixer to
go from u('xxx') to 'xxx'. Once I learned to use lib2to3, with a few pointers
from Benjamin, it worked like a charm for me.
Regards,
Vinay Sajip
More information about the Python-Dev
mailing list