Download - Python 3 Compatibility (PyCon 2009)

Transcript
Page 1: Python 3 Compatibility (PyCon 2009)

Python 2.6 and 3.0 Compatibility

Lennart Regebrohttp://regebro.wordpress.com/

[email protected] 2009, Chicago

Page 2: Python 3 Compatibility (PyCon 2009)

Python 3.0 is here!

Page 3: Python 3 Compatibility (PyCon 2009)

Python 3 is incompatible

Page 4: Python 3 Compatibility (PyCon 2009)

Python 3 is incompatible

O RLY?

Page 5: Python 3 Compatibility (PyCon 2009)

Python 3 is incompatible Ya, really!

O RLY?

Page 6: Python 3 Compatibility (PyCon 2009)

Strategies to deal with incompatibility

Page 7: Python 3 Compatibility (PyCon 2009)

For applications:Just port to Python 3

Page 8: Python 3 Compatibility (PyCon 2009)

2to3 helps you port

Page 9: Python 3 Compatibility (PyCon 2009)

Tests are really helpful

Page 10: Python 3 Compatibility (PyCon 2009)

Libraries need to support both Python 2 and Python 3

Page 11: Python 3 Compatibility (PyCon 2009)

For most libraries:Develop in Python 2 and run 2to3

for Python 3 support

Page 12: Python 3 Compatibility (PyCon 2009)

2to3 supported development has a significant startup cost

Page 13: Python 3 Compatibility (PyCon 2009)

For stable libraries:Just port to Python 3

Page 14: Python 3 Compatibility (PyCon 2009)

Platform Extensions/Plugins/etc.

Page 15: Python 3 Compatibility (PyCon 2009)

No Plone user will switch to Python 3 until important extensions are available

Page 16: Python 3 Compatibility (PyCon 2009)

Nobody will make the extensions support Python 3 until they

themselves move to Python 3

Page 17: Python 3 Compatibility (PyCon 2009)

Dead lock

Page 18: Python 3 Compatibility (PyCon 2009)

The Zope experience

Page 19: Python 3 Compatibility (PyCon 2009)

Zope 3: A complete break

Page 20: Python 3 Compatibility (PyCon 2009)

Zope 3.1 etc: No break

Page 21: Python 3 Compatibility (PyCon 2009)

Wanted: Gradual upgrade path

Page 22: Python 3 Compatibility (PyCon 2009)

First support 2.5 and 2.6, then support 2.6 and 3.0,

finally dropping 2.x completely

Page 23: Python 3 Compatibility (PyCon 2009)

Python 3 is incompatible

Oh, right, I forgot already.

Page 24: Python 3 Compatibility (PyCon 2009)

Python 2.6 introduces quite a lot of forward compatibility!

Page 25: Python 3 Compatibility (PyCon 2009)

Python 2.5:except Exception, e:

Python 3.0:except Exception as e:

Page 26: Python 3 Compatibility (PyCon 2009)

Python 2.6:

Both works!

Page 27: Python 3 Compatibility (PyCon 2009)

Python 2.5:3/2 == 1

Python 3.0:3/2 == 1.5

Page 28: Python 3 Compatibility (PyCon 2009)

Solutions:

3//2 == 1

from __future__ import division

3/2 == 1.5

Page 29: Python 3 Compatibility (PyCon 2009)

Python2.5:print >> file, “bla”, “bla”, “bla”,

Python3.0:print(“bla”, “bla”, “bla”,

file=file, ending='')

Page 30: Python 3 Compatibility (PyCon 2009)

Python 2.6:from __future__ import

print_function

Page 31: Python 3 Compatibility (PyCon 2009)

Python2.5:u”Üniçodê”

Python3.0:“Üniçodê”

Page 32: Python 3 Compatibility (PyCon 2009)

Python 2.6:from __future__ import

unicode_literals

Page 33: Python 3 Compatibility (PyCon 2009)

from __future__ import \ unicode_literalstry: str = unicodeexcept NameError: pass

isinstance(type(“Üniçodê”), str)

Page 34: Python 3 Compatibility (PyCon 2009)

Python2.5:

open('filename', 'r').read()'A string\n'

open('filename', 'rb').read()'A string\n'

Page 35: Python 3 Compatibility (PyCon 2009)

Python3.0:

open('filename', 'r').read()'A string\n'

open('filename', 'rb').read()b'A string\n'

Page 36: Python 3 Compatibility (PyCon 2009)

Python 2.6:b“A string”[5] == “i”

Python 3.0:b“A string”[5] == 105

Page 37: Python 3 Compatibility (PyCon 2009)

Solutions for 2.6 and 3.0:

bytearray(b”A list of bytes”)bytearray(open(file, “rb”).read())

Page 38: Python 3 Compatibility (PyCon 2009)

Python 2.6:>>> bytes(a_byte_array)'Binary data'

Python 3.0:>>> bytes(a_byte_array)b'Binary data'

Page 39: Python 3 Compatibility (PyCon 2009)

Python2.5:open(“unicodefile”).read()

'\xc3\x9cni\xc3\xa7od\xc3\xaa\n'

Python3.0:'Üniçodê\n'

Page 40: Python 3 Compatibility (PyCon 2009)

Solution for 2.6 and 3.0:

infile =open(“unicodefile”, “rb”)uni = infile.read().decode(enc)

Page 41: Python 3 Compatibility (PyCon 2009)

try: from cStringIO import StringIOexcept ImportError: from io import StringIO

Page 42: Python 3 Compatibility (PyCon 2009)

Python2.5:dict.items()/dict.keys()/dict.values()

return lists

Python3.0:dict.items()/dict.keys()/dict.values()

return views

Page 43: Python 3 Compatibility (PyCon 2009)

Python 2.5:foo = bar.keys()foo.sort()

Python 2.4 - 3.0:foo = sorted(bar.keys())

Page 44: Python 3 Compatibility (PyCon 2009)

Removed:dict.iteritems()dict.itervalues()dict.iterkeys()

Page 45: Python 3 Compatibility (PyCon 2009)

Python 2.6 solution:

try: iter = d.iteritems() except AttributeError: iter = d.items()

Page 46: Python 3 Compatibility (PyCon 2009)

Aaaahhhhh........

Now meta classes make sense

Page 47: Python 3 Compatibility (PyCon 2009)

infile = open('something', 'rb')bdata = infile.read()

python2.6:pass

python 3.0:sdata = bdata.decode()

Page 48: Python 3 Compatibility (PyCon 2009)

Python 2.x and 3.0:sdata = str(bdata.decode('ascii'))

Page 49: Python 3 Compatibility (PyCon 2009)

Running on both 2.x and 3.0 will mean some ugly hacks no matter

if you use 2to3 or not

Page 50: Python 3 Compatibility (PyCon 2009)

Using 2to3:

Easy to support 2.3, 2.4, 2.5Few contortionsSome setup cost

Single branch of code

Page 51: Python 3 Compatibility (PyCon 2009)

Separate branches:

Easy to support 2.3, 2.4, 2.5No contortionsNo setup cost

You have to fix all bugs twice

Page 52: Python 3 Compatibility (PyCon 2009)

2.6 and 3.0 support without 2to3:

More contortionsLow setup cost

Single branch of codeBut, no support for < 2.6

Page 53: Python 3 Compatibility (PyCon 2009)

Supporting Python 2.5 or lower and 3.0 without 2to3:

Contortion fest!High hack valueLot's of work

Page 54: Python 3 Compatibility (PyCon 2009)

So what if you decide to go for 2.6 and 3.0 without 2.6?

1. 2to32. Make it run under 3.03. Backport to Python 2.6

Page 55: Python 3 Compatibility (PyCon 2009)

2.6 compatible 2to3?

Page 56: Python 3 Compatibility (PyCon 2009)

Preparing yourself for 3.0

Page 57: Python 3 Compatibility (PyCon 2009)

Already in 2.6:

String exceptions are gone

“as” and “with” are keywords

Page 58: Python 3 Compatibility (PyCon 2009)

Python 2.3:alist.sort(cmp=func)

Python 2.4 and later:alist = sorted(alist, key=func)

Page 59: Python 3 Compatibility (PyCon 2009)

Use // when you want integer division.

Usefrom __future__ import division

already now

Page 60: Python 3 Compatibility (PyCon 2009)

Mark binary files with “rb” or “wb”

Page 61: Python 3 Compatibility (PyCon 2009)

http://code.google.com/p/python-incompatibility

[email protected]