PageRenderTime 110ms CodeModel.GetById 94ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/Doc/library/2to3.rst

http://unladen-swallow.googlecode.com/
ReStructuredText | 102 lines | 69 code | 33 blank | 0 comment | 0 complexity | edacd78bb7fcffc21ce592855fcf8512 MD5 | raw file
  1.. _2to3-reference:
  2
  32to3 - Automated Python 2 to 3 code translation
  4===============================================
  5
  6.. sectionauthor:: Benjamin Peterson <benjamin@python.org>
  7
  82to3 is a Python program that reads Python 2.x source code and applies a series
  9of *fixers* to transform it into valid Python 3.x code.  The standard library
 10contains a rich set of fixers that will handle almost all code.  2to3 supporting
 11library :mod:`lib2to3` is, however, a flexible and generic library, so it is
 12possible to write your own fixers for 2to3.  :mod:`lib2to3` could also be
 13adapted to custom applications in which Python code needs to be edited
 14automatically.
 15
 16
 17Using 2to3
 18----------
 19
 202to3 will usually be installed with the Python interpreter as a script.  It is
 21also located in the :file:`Tools/scripts` directory of the Python root.
 22
 232to3's basic arguments are a list of files or directories to transform.  The
 24directories are to recursively traversed for Python sources.
 25
 26Here is a sample Python 2.x source file, :file:`example.py`::
 27
 28   def greet(name):
 29       print "Hello, {0}!".format(name)
 30   print "What's your name?"
 31   name = raw_input()
 32   greet(name)
 33
 34It can be converted to Python 3.x code via 2to3 on the command line::
 35
 36   $ 2to3 example.py
 37
 38A diff against the original source file is printed.  2to3 can also write the
 39needed modifications right back to the source file.  (A backup of the original
 40file is made unless :option:`-n` is also given.)  Writing the changes back is
 41enabled with the :option:`-w` flag::
 42
 43   $ 2to3 -w example.py
 44
 45After transformation, :file:`example.py` looks like this::
 46
 47   def greet(name):
 48       print("Hello, {0}!".format(name))
 49   print("What's your name?")
 50   name = input()
 51   greet(name)
 52
 53Comments and exact indentation are preserved throughout the translation process.
 54
 55By default, 2to3 runs a set of predefined fixers.  The :option:`-l` flag lists
 56all available fixers.  An explicit set of fixers to run can be given with
 57:option:`-f`.  Likewise the :option:`-x` explicitly disables a fixer.  The
 58following example runs only the ``imports`` and ``has_key`` fixers::
 59
 60   $ 2to3 -f imports -f has_key example.py
 61
 62This command runs every fixer except the ``apply`` fixer::
 63
 64   $ 2to3 -x apply example.py
 65
 66Some fixers are *explicit*, meaning they aren't run by default and must be
 67listed on the command line to be run.  Here, in addition to the default fixers,
 68the ``idioms`` fixer is run::
 69
 70   $ 2to3 -f all -f idioms example.py
 71
 72Notice how passing ``all`` enables all default fixers.
 73
 74Sometimes 2to3 will find a place in your source code that needs to be changed,
 75but 2to3 cannot fix automatically.  In this case, 2to3 will print a warning
 76beneath the diff for a file.  You should address the warning in order to have
 77compliant 3.x code.
 78
 792to3 can also refactor doctests.  To enable this mode, use the :option:`-d`
 80flag.  Note that *only* doctests will be refactored.  This also doesn't require
 81the module to be valid Python.  For example, doctest like examples in a reST
 82document could also be refactored with this option.
 83
 84The :option:`-v` option enables output of more information on the translation
 85process.
 86
 87
 88:mod:`lib2to3` - 2to3's library
 89-------------------------------
 90
 91.. module:: lib2to3
 92   :synopsis: the 2to3 library
 93.. moduleauthor:: Guido van Rossum
 94.. moduleauthor:: Collin Winter
 95
 96
 97.. note::
 98
 99   The :mod:`lib2to3` API should be considered unstable and may change
100   drastically in the future.
101
102.. XXX What is the public interface anyway?