[Zope-dev] Porting zope.tal to Python 3
Marius Gedminas
marius at gedmin.as
Mon Feb 11 18:44:38 UTC 2013
I don't plan to write about every package I port, but there were some
lessons learned from zope.tal that I thought I should mention.
- Running 2to3 on your sources is a good way to identify places that
need changing, but you'll have to revert most of the actual changes to
keep it compatible with Python 2.
I recently learned about a tool called python-modernize[1] that is
basically 2to3 except it rewrites into a common Python 2 & 3 subset.
I haven't checked it out yet, but it sounds promising
[1] http://pypi.python.org/pypi/modernize
- StringIO: while Python-3-style io.StringIO() and io.BytesIO() classes
exist in Python 2.6+, you can't easily use either them as a
replacement for StringIO.StringIO or cStringIO.StringIO. Why?
Because people tend to mix native (ASCII-only) string literals with
Unicode objects, and that's a no-no in Python 3 land.
I didn't like the idea of importing unicode_literals from the
__future__, or of dropping u'' prefixes everywhere. Partially because
I'm not sure that would be enough -- str objects might make it all the
way though the public API of zope.tal and suddenly code that used to
work on Python 2 just fine would now explode. So I went with a
conditional import -- from cStringIO import StringIO on Python 2 and
from io import StringIO on Python 3.
- tox/detox make it very easy to add just one more supported Python
version -- edit tox.ini, run detox, fix tests. This works with PyPy
too, not just Python 3.x!
Marius Gedminas
--
http://pov.lt/ -- Zope 3/BlueBream consulting and development
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: Digital signature
URL: <http://mail.zope.org/pipermail/zope-dev/attachments/20130211/100ac7a1/attachment.sig>
More information about the Zope-Dev
mailing list