SlideShare una empresa de Scribd logo
1 de 61
Descargar para leer sin conexión
Python 2.6 and 3.0 Compatibility
Lennart Regebro
http://regebro.wordpress.com/
regebro@gmail.com
PyCon 2009, Chicago
Python 3.0 is here!
Python 3 is incompatible
Python 3 is incompatible
O RLY?
Python 3 is incompatible
Ya, really!
O RLY?
Strategies to deal with
incompatibility
For applications:
Just port to Python 3
2to3 helps you port
Tests are really helpful
Libraries need to support both
Python 2 and Python 3
For most libraries:
Develop in Python 2 and run 2to3
for Python 3 support
2to3 supported development
has a significant startup cost
For stable libraries:
Just port to Python 3
Platform Extensions/
Plugins/etc.
No Plone user will switch to
Python 3 until important
extensions are available
Nobody will make the extensions
support Python 3 until they
themselves move to Python 3
Dead lock
The Zope experience
Zope 3: A complete break
Zope 3.1 etc: No break
Wanted: Gradual upgrade path
First support 2.5 and 2.6,
then support 2.6 and 3.0,
finally dropping 2.x completely
Python 3 is incompatible
Oh, right, I forgot
already.
Python 2.6 introduces quite a lot
of forward compatibility!
Python 2.5:
except Exception, e:
Python 3.0:
except Exception as e:
Python 2.6:
Both works!
Python 2.5:
3/2 == 1
Python 3.0:
3/2 == 1.5
Solutions:
3//2 == 1
from __future__ import division
3/2 == 1.5
Python2.5:
print >> file, “bla”, “bla”, “bla”,
Python3.0:
print(“bla”, “bla”, “bla”,
file=file, ending='')
Python 2.6:
from __future__ import
print_function
Python2.5:
u”Üniçodê”
Python3.0:
“Üniçodê”
Python 2.6:
from __future__ import
unicode_literals
from __future__ import 
unicode_literals
try:
str = unicode
except NameError:
pass
isinstance(type(“Üniçodê”), str)
Python2.5:
open('filename', 'r').read()
'A stringn'
open('filename', 'rb').read()
'A stringn'
Python3.0:
open('filename', 'r').read()
'A stringn'
open('filename', 'rb').read()
b'A stringn'
Python 2.6:
b“A string”[5] == “i”
Python 3.0:
b“A string”[5] == 105
Solutions for 2.6 and 3.0:
bytearray(b”A list of bytes”)
bytearray(open(file, “rb”).read())
Python 2.6:
>>> bytes(a_byte_array)
'Binary data'
Python 3.0:
>>> bytes(a_byte_array)
b'Binary data'
Python2.5:
open(“unicodefile”).read()
'xc3x9cnixc3xa7odxc3xaan'
Python3.0:
'Üniçodên'
Solution for 2.6 and 3.0:
infile =open(“unicodefile”, “rb”)
uni = infile.read().decode(enc)
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
Python2.5:
dict.items()/dict.keys()/dict.values()
return lists
Python3.0:
dict.items()/dict.keys()/dict.values()
return views
Python 2.5:
foo = bar.keys()
foo.sort()
Python 2.4 - 3.0:
foo = sorted(bar.keys())
Removed:
dict.iteritems()
dict.itervalues()
dict.iterkeys()
Python 2.6 solution:
try:
iter = d.iteritems()
except AttributeError:
iter = d.items()
Aaaahhhhh........
Now meta classes make sense
infile = open('something', 'rb')
bdata = infile.read()
python2.6:
pass
python 3.0:
sdata = bdata.decode()
Python 2.x and 3.0:
sdata = str(bdata.decode('ascii'))
Running on both 2.x and 3.0 will
mean some ugly hacks no matter
if you use 2to3 or not
Using 2to3:
Easy to support 2.3, 2.4, 2.5
Few contortions
Some setup cost
Single branch of code
Separate branches:
Easy to support 2.3, 2.4, 2.5
No contortions
No setup cost
You have to fix all bugs twice
2.6 and 3.0 support without 2to3:
More contortions
Low setup cost
Single branch of code
But, no support for < 2.6
Supporting Python 2.5 or lower
and 3.0 without 2to3:
Contortion fest!
High hack value
Lot's of work
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
2.6 compatible 2to3?
Preparing yourself for 3.0
Already in 2.6:
String exceptions are gone
“as” and “with” are keywords
Python 2.3:
alist.sort(cmp=func)
Python 2.4 and later:
alist = sorted(alist, key=func)
Use // when you want integer
division.
Use
from __future__ import division
already now
Mark binary files with “rb” or
“wb”
http://code.google.com/p/python-incompatibility
regebro@gmail.com

Más contenido relacionado

La actualidad más candente

How to develop a rich terminal UI application
How to develop a rich terminal UI applicationHow to develop a rich terminal UI application
How to develop a rich terminal UI applicationMasashi Shibata
 
AmI 2017 - Python basics
AmI 2017 - Python basicsAmI 2017 - Python basics
AmI 2017 - Python basicsLuigi De Russis
 
Pandora's Cash Box
Pandora's Cash BoxPandora's Cash Box
Pandora's Cash Boxnitayart
 
Python by Martin Geisler
Python by Martin GeislerPython by Martin Geisler
Python by Martin GeislerAberla
 
Plone ♥︎ Python 3
Plone ♥︎ Python 3Plone ♥︎ Python 3
Plone ♥︎ Python 3Philip Bauer
 

La actualidad más candente (8)

How to develop a rich terminal UI application
How to develop a rich terminal UI applicationHow to develop a rich terminal UI application
How to develop a rich terminal UI application
 
AmI 2017 - Python basics
AmI 2017 - Python basicsAmI 2017 - Python basics
AmI 2017 - Python basics
 
Python - Lecture 9
Python - Lecture 9Python - Lecture 9
Python - Lecture 9
 
Pandora's Cash Box
Pandora's Cash BoxPandora's Cash Box
Pandora's Cash Box
 
Python by Martin Geisler
Python by Martin GeislerPython by Martin Geisler
Python by Martin Geisler
 
No more dumb hex!
No more dumb hex!No more dumb hex!
No more dumb hex!
 
Python basics
Python basicsPython basics
Python basics
 
Plone ♥︎ Python 3
Plone ♥︎ Python 3Plone ♥︎ Python 3
Plone ♥︎ Python 3
 

Más de Lennart Regebro

Zope is dead - Long live Zope
Zope is dead - Long live ZopeZope is dead - Long live Zope
Zope is dead - Long live ZopeLennart Regebro
 
How not to develop with Plone
How not to develop with PloneHow not to develop with Plone
How not to develop with PloneLennart Regebro
 
Transmogrifier: Migrating to Plone with less pain
Transmogrifier: Migrating to Plone with less painTransmogrifier: Migrating to Plone with less pain
Transmogrifier: Migrating to Plone with less painLennart Regebro
 
What Zope Did Wrong (PyCon 2008)
What Zope Did Wrong (PyCon 2008)What Zope Did Wrong (PyCon 2008)
What Zope Did Wrong (PyCon 2008)Lennart Regebro
 

Más de Lennart Regebro (6)

Zope is dead - Long live Zope
Zope is dead - Long live ZopeZope is dead - Long live Zope
Zope is dead - Long live Zope
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
 
How not to develop with Plone
How not to develop with PloneHow not to develop with Plone
How not to develop with Plone
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
 
Transmogrifier: Migrating to Plone with less pain
Transmogrifier: Migrating to Plone with less painTransmogrifier: Migrating to Plone with less pain
Transmogrifier: Migrating to Plone with less pain
 
What Zope Did Wrong (PyCon 2008)
What Zope Did Wrong (PyCon 2008)What Zope Did Wrong (PyCon 2008)
What Zope Did Wrong (PyCon 2008)
 

Python 3 Compatibility (PyCon 2009)