Porting code to Python 3 with 2to3

  1. Diving in
  2. print statement
  3. <> comparison
  4. has_key() dictionary method
  5. Dictionary methods
  6. filter() global function
  7. map() global function
  8. apply() global function
  9. intern() global function
  10. exec statement
  11. repr literals (backticks)
  12. try...except statement
  13. raise statement
  14. throw statement
  15. long data type
  16. xrange() global function
  17. raw_input() and input() global functions
  18. func_* function attributes
  19. xreadlines() I/O method
  20. imports
  21. lambda functions with multiple parameters
  22. __class__ special class attribute
  23. next() iterator method
  24. __nonzero__ special class attribute
  25. Number literals
  26. sys.maxint
  27. unicode() global function
  28. Unicode string literals
  29. callable() global function
  30. zip() global function
  31. StandardError() exception
  32. types module constants
  33. basestring datatype
  34. itertools module
  35. Relative imports
  36. sys.exc_type, sys.exc_value, sys.exc_traceback
  37. List comprehensions over tuples
  38. os.getcwdu() function
  39. Metaclasses
  40. set() literals
  41. buffer() global function
  42. Whitespace around commas
  43. Common idioms

Diving in

FIXME intro

...

print statement

FIXME intro

Notes Python 2 Python 3
print print()
print 1 print(1)
print 1, 2 print(1, 2)
print 1, 2, print(1, 2, end=' ')
print >>sys.stderr, 1, 2, 3 print(1, 2, 3, file=sys.stderr)
  1. ...
  2. ...
  3. ...
  4. ...
  5. ...

<> comparison

FIXME intro

Notes Python 2 Python 3
if x <> y: if x != y:
if x <> y <> z: if x != y != z:
  1. ...
  2. ...

has_key() dictionary method

FIXME intro

Notes Python 2 Python 3
aDictionary.has_key("PapayaWhip") "PapayaWhip" in aDictionary
aDictionary.has_key(x) or aDictionary.has_key(y) x in aDictionary or y in aDictionary
aDictionary.has_key(x + y) (x + y) in aDictionary
x + aDictionary.has_key(y) x + (y in aDictionary)
aDictionary.has_key(x or y) (x or y) in aDictionary
  1. ...
  2. ...
  3. ...
  4. ...
  5. ...

Dictionary methods

FIXME intro

Notes Python 2 Python 3
aDictionary.keys() list(aDictionary.keys())
aDictionary.items() list(aDictionary.items())
aDictionary.iterkeys() iter(aDictionary.keys())
[i for i in aDictionary.iterkeys()] [i for i in aDictionary.keys()]
min(aDictionary.keys()) no change
  1. ...
  2. ...
  3. ...
  4. ...
  5. ...

filter() global function

FIXME intro

Notes Python 2 Python 3
filter(aFunction, aList) list(filter(aFunction, aList))
list(filter(aFunction, aList)) no change
filter(None, aList) [i for i in aList if i]
for i in filter(None, aList) no change
[i for i in filter(aFunction, aList)] no change
  1. ...
  2. ...
  3. ...
  4. ...
  5. ...

map() global function

FIXME intro

Notes Python 2 Python 3
map(aFunction, 'PapayaWhip') list(map(aFunction, 'PapayaWhip'))
map(None, 'PapayaWhip') list('PapayaWhip')
map(lambda x: x+1, range(42)) [x+1 for x in range(42)]
for i in map(aFunction, aList): unchanged
[i for i in map(aFunction, aList)] unchanged
  1. ...
  2. ...
  3. ...
  4. ...
  5. ...

apply() global function

FIXME intro

Notes Python 2 Python 3
apply(aFunction, args) aFunction(*args)
apply(aFunction, args, kwds) aFunction(*args, **kwds)
apply(aFunction, args + z) aFunction(*args + z)
apply(aModule.aFunction, args) aModule.aFunction(*args)
  1. ...
  2. ...
  3. ...
  4. ...

intern() global function

FIXME intro

Notes Python 2 Python 3
intern(aString) sys.intern(aString)
  1. ...

exec statement

FIXME intro

Notes Python 2 Python 3
exec codeString exec(codeString)
exec codeString in aGlobalNamespace exec(codeString, aGlobalNamespace)
exec codeString in aGlobalNamespace, aLocalNamespace exec(codeString, aGlobalNamespace, aLocalNamespace)
  1. ...
  2. ...
  3. ...

repr literals (backticks)

FIXME intro

Notes Python 2 Python 3
`x` repr(x)
`1 + 2` repr(1 + 2)
`"PapayaWhip" + `2`` repr("PapayaWhip" + repr(2))
  1. ...
  2. ...
  3. ...

try...except statement

FIXME intro

Notes Python 2 Python 3
try:
    import mymodule
except ImportError, e
    pass
try:
    import mymodule
except ImportError as e:
    pass
try:
    import mymodule
except (RuntimeError, ImportError), e
    pass
try:
    import mymodule
except (RuntimeError, ImportError) as e:
    pass
try:
    import mymodule
except ImportError:
    pass
unchanged
try:
    import mymodule
except:
    pass
unchanged
  1. ...
  2. ...
  3. ...
  4. ...

raise statement

FIXME intro

Notes Python 2 Python 3
raise MyException, "error message" raise MyException("error message")
raise MyException, "error message", aTraceback raise MyException("error message").with_traceback(aTraceback)
raise "error message" unsupported
  1. ...
  2. ...
  3. ...

throw statement

FIXME intro

Notes Python 2 Python 3
aGenerator.throw(MyException) unchanged
aGenerator.throw(MyException, "error message") aGenerator.throw(MyException("error message"))
aGenerator.throw("error message") unsupported
  1. ...
  2. ...
  3. ...

long data type

FIXME intro

Notes Python 2 Python 3
x = 1000000000000L x = 1000000000000
x = 0xFFFFFFFFFFFFL x = 0xFFFFFFFFFFFF
long(x) int(x)
type(x) is long type(x) is int
isinstance(x, long) isinstance(x, int)
  1. ...
  2. ...
  3. ...
  4. ...
  5. ...

xrange() global function

FIXME intro

Notes Python 2 Python 3
xrange(10) range(10)
aList = range(10) aList = list(range(10))
[i for i in xrange(10)] [i for i in range(10)]
for i in range(10): unchanged
sum(range(10)) unchanged
  1. ...
  2. ...
  3. ...
  4. ...
  5. ...

raw_input() and input() global functions

FIXME intro

Notes Python 2 Python 3
raw_input() input()
raw_input("prompt") input("prompt")
input() eval(input())
input("prompt") eval(input("prompt"))
  1. ...
  2. ...
  3. ...
  4. ...

func_* function attributes

FIXME intro

Notes Python 2 Python 3
aFunction.func_closure aFunction.__closure__
aFunction.func_doc aFunction.__doc__
aFunction.func_name aFunction.__name__
aFunction.func_defaults aFunction.__defaults__
aFunction.func_code aFunction.__code__
aFunction.func_globals aFunction.__globals__
aFunction.func_dict aFunction.__dict__
  1. ...
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  7. ...

xreadlines() I/O method

FIXME intro

Notes Python 2 Python 3
for line in aFile.xreadlines(): for line in aFile:
for line in aFile.xreadlines(5): unchanged
  1. ...
  2. ...

imports

FIXME intro

this includes fixer_imports, fixer_imports2, fixer_urllib

Notes Python 2 Python 3
FIXME FIXME
  1. ...

lambda functions with multiple parameters

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

__class__ special class attribute

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

next() iterator method

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

__nonzero__ special class attribute

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

Number literals

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

sys.maxint

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

unicode() global function

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

Unicode string literals

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

callable() global function

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

zip() global function

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

StandardError() exception

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

types module constants

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

basestring datatype

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

itertools module

FIXME intro

this includes fixer_itertools, fixer_itertools_imports

Notes Python 2 Python 3
FIXME FIXME
  1. ...

Relative imports

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

sys.exc_type, sys.exc_value, sys.exc_traceback

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

List comprehensions over tuples

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

os.getcwdu() function

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

Metaclasses

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

set() literals

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

buffer() global function

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

Whitespace around commas

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...

Common idioms

FIXME intro

Notes Python 2 Python 3
FIXME FIXME
  1. ...