Python 2.6 and 3.0 Compatibility
             Lennart Regebro
        http://regebro.wordpress.com/
              regebro@...
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ê”), s...
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.key...
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 tw...
2.6 and 3.0 support without 2to3:

        More contortions
         Low setup cost
     Single branch of code
    But, no...
Supporting Python 2.5 or lower
    and 3.0 without 2to3:

       Contortion fest!
       High hack value
        Lot's of ...
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...
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
Upcoming SlideShare
Loading in …5
×

Python 3 Compatibility (PyCon 2009)

3,218
-1

Published on

This talks takes a look at the various options of migrating to Python 3, and takes up examples of some tricks you can do to make you code run unmodified under both 2.6 and 3.0. Video at http://pycon.blip.tv/file/1949281/

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,218
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
23
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

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
  2. Python 3.0 is here!
  3. Python 3 is incompatible
  4. Python 3 is incompatible O RLY?
  5. Python 3 is incompatible Ya, really! O RLY?
  6. Strategies to deal with incompatibility
  7. For applications: Just port to Python 3
  8. 2to3 helps you port
  9. Tests are really helpful
  10. Libraries need to support both Python 2 and Python 3
  11. For most libraries: Develop in Python 2 and run 2to3 for Python 3 support
  12. 2to3 supported development has a significant startup cost
  13. For stable libraries: Just port to Python 3
  14. Platform Extensions/ Plugins/etc.
  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
  17. Dead lock
  18. The Zope experience
  19. Zope 3: A complete break
  20. Zope 3.1 etc: No break
  21. Wanted: Gradual upgrade path
  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.
  24. Python 2.6 introduces quite a lot of forward compatibility!
  25. Python 2.5: except Exception, e: Python 3.0: except Exception as e:
  26. Python 2.6: Both works!
  27. Python 2.5: 3/2 == 1 Python 3.0: 3/2 == 1.5
  28. Solutions: 3//2 == 1 from __future__ import division 3/2 == 1.5
  29. Python2.5: print >> file, “bla”, “bla”, “bla”, Python3.0: print(“bla”, “bla”, “bla”, file=file, ending='')
  30. Python 2.6: from __future__ import print_function
  31. Python2.5: u”Üniçodê” Python3.0: “Üniçodê”
  32. Python 2.6: from __future__ import unicode_literals
  33. from __future__ import unicode_literals try: str = unicode except NameError: pass isinstance(type(“Üniçodê”), str)
  34. Python2.5: open('filename', 'r').read() 'A stringn' open('filename', 'rb').read() 'A stringn'
  35. Python3.0: open('filename', 'r').read() 'A stringn' open('filename', 'rb').read() b'A stringn'
  36. Python 2.6: b“A string”[5] == “i” Python 3.0: b“A string”[5] == 105
  37. Solutions for 2.6 and 3.0: bytearray(b”A list of bytes”) bytearray(open(file, “rb”).read())
  38. Python 2.6: >>> bytes(a_byte_array) 'Binary data' Python 3.0: >>> bytes(a_byte_array) b'Binary data'
  39. Python2.5: open(“unicodefile”).read() 'xc3x9cnixc3xa7odxc3xaan' Python3.0: 'Üniçodên'
  40. Solution for 2.6 and 3.0: infile =open(“unicodefile”, “rb”) uni = infile.read().decode(enc)
  41. try: from cStringIO import StringIO except ImportError: from io import StringIO
  42. Python2.5: dict.items()/dict.keys()/dict.values() return lists Python3.0: dict.items()/dict.keys()/dict.values() return views
  43. Python 2.5: foo = bar.keys() foo.sort() Python 2.4 - 3.0: foo = sorted(bar.keys())
  44. Removed: dict.iteritems() dict.itervalues() dict.iterkeys()
  45. Python 2.6 solution: try: iter = d.iteritems() except AttributeError: iter = d.items()
  46. Aaaahhhhh........ Now meta classes make sense
  47. infile = open('something', 'rb') bdata = infile.read() python2.6: pass python 3.0: sdata = bdata.decode()
  48. Python 2.x and 3.0: sdata = str(bdata.decode('ascii'))
  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
  51. Separate branches: Easy to support 2.3, 2.4, 2.5 No contortions No setup cost You have to fix all bugs twice
  52. 2.6 and 3.0 support without 2to3: More contortions Low setup cost Single branch of code But, no support for < 2.6
  53. Supporting Python 2.5 or lower and 3.0 without 2to3: Contortion fest! High hack value Lot's of work
  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
  55. 2.6 compatible 2to3?
  56. Preparing yourself for 3.0
  57. Already in 2.6: String exceptions are gone “as” and “with” are keywords
  58. Python 2.3: alist.sort(cmp=func) Python 2.4 and later: alist = sorted(alist, key=func)
  59. Use // when you want integer division. Use from __future__ import division already now
  60. Mark binary files with “rb” or “wb”
  61. http://code.google.com/p/python-incompatibility regebro@gmail.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×