More Related Content More from Lennart Regebro (6) Python 3 Compatibility (PyCon 2009)1. Python 2.6 and 3.0 Compatibility
Lennart Regebro
http://regebro.wordpress.com/
regebro@gmail.com
PyCon 2009, Chicago
15. No Plone user will switch to
Python 3 until important
extensions are available
16. Nobody will make the extensions
support Python 3 until they
themselves move to Python 3
22. First support 2.5 and 2.6,
then support 2.6 and 3.0,
finally dropping 2.x completely
23. Python 3 is incompatible
Oh, right, I forgot
already.
33. from __future__ import
unicode_literals
try:
str = unicode
except NameError:
pass
isinstance(type(“Üniçodê”), str)
37. Solutions for 2.6 and 3.0:
bytearray(b”A list of bytes”)
bytearray(open(file, “rb”).read())
40. Solution for 2.6 and 3.0:
infile =open(“unicodefile”, “rb”)
uni = infile.read().decode(enc)
43. Python 2.5:
foo = bar.keys()
foo.sort()
Python 2.4 - 3.0:
foo = sorted(bar.keys())
49. Running on both 2.x and 3.0 will
mean some ugly hacks no matter
if you use 2to3 or not
50. Using 2to3:
Easy to support 2.3, 2.4, 2.5
Few contortions
Some setup cost
Single branch of code
52. 2.6 and 3.0 support without 2to3:
More contortions
Low setup cost
Single branch of code
But, no support for < 2.6
54. So what if you decide to go for
2.6 and 3.0 without 2.6?
1. 2to3
2. Make it run under 3.0
3. Backport to Python 2.6
59. Use // when you want integer
division.
Use
from __future__ import division
already now