mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00

svn+ssh://pythondev@svn.python.org/python/trunk ........ r66974 | benjamin.peterson | 2008-10-19 08:59:01 -0500 (Sun, 19 Oct 2008) | 1 line fix compiler warning ........ r66977 | benjamin.peterson | 2008-10-19 14:39:16 -0500 (Sun, 19 Oct 2008) | 1 line mention -n ........ r66984 | armin.ronacher | 2008-10-20 16:29:08 -0500 (Mon, 20 Oct 2008) | 3 lines Fixed #4062, added import for _ast.__version__ to ast to match the documented behavior. ........ r66989 | matthias.klose | 2008-10-21 04:12:25 -0500 (Tue, 21 Oct 2008) | 2 lines - install versioned manpage ........ r66992 | benjamin.peterson | 2008-10-21 15:51:13 -0500 (Tue, 21 Oct 2008) | 1 line make sure to call iteritems() ........ r66994 | amaury.forgeotdarc | 2008-10-21 17:01:38 -0500 (Tue, 21 Oct 2008) | 6 lines #4157 move two test functions out of platform.py. Turn them into unit tests, and correct an obvious typo: (("a", "b") ("c", "d") ("e", "f")) compiles even with the missing commas, but does not execute very well... ........ r66995 | benjamin.peterson | 2008-10-21 17:18:29 -0500 (Tue, 21 Oct 2008) | 1 line return ArgInfo from inspect.getargvalues #4092 ........ r66996 | benjamin.peterson | 2008-10-21 17:20:31 -0500 (Tue, 21 Oct 2008) | 1 line add NEWs note for last change ........ r66998 | benjamin.peterson | 2008-10-22 15:57:43 -0500 (Wed, 22 Oct 2008) | 1 line fix a few typos ........ r66999 | benjamin.peterson | 2008-10-22 16:05:30 -0500 (Wed, 22 Oct 2008) | 1 line and another typo... ........ r67000 | benjamin.peterson | 2008-10-22 16:16:34 -0500 (Wed, 22 Oct 2008) | 1 line fix #4150: pdb's up command didn't work for generators in post-mortem ........ r67007 | benjamin.peterson | 2008-10-23 16:43:48 -0500 (Thu, 23 Oct 2008) | 1 line only nonempty __slots__ don't work ........ r67015 | georg.brandl | 2008-10-25 02:00:52 -0500 (Sat, 25 Oct 2008) | 2 lines Typo fix. ........
109 lines
3.8 KiB
ReStructuredText
109 lines
3.8 KiB
ReStructuredText
.. _2to3-reference:
|
|
|
|
2to3 - Automated Python 2 to 3 code translation
|
|
===============================================
|
|
|
|
.. sectionauthor:: Benjamin Peterson
|
|
|
|
2to3 is a Python program that reads Python 2.x source code and applies a series
|
|
of *fixers* to transform it into valid Python 3.x code. The standard library
|
|
contains a rich set of fixers that will handle almost all code. 2to3 supporting
|
|
library :mod:`lib2to3` is, however, a flexible and generic library, so it is
|
|
possible to write your own fixers for 2to3. :mod:`lib2to3` could also be
|
|
adapted to custom applications in which Python code needs to be edited
|
|
automatically.
|
|
|
|
|
|
Using 2to3
|
|
----------
|
|
|
|
2to3 will usually be installed with the Python interpreter as a script. It is
|
|
also located in the :file:`Tools/scripts` directory of the Python root.
|
|
|
|
2to3's basic arguments are a list of files or directories to transform. The
|
|
directories are to recursively traversed for Python sources.
|
|
|
|
Here is a sample Python 2.x source file, :file:`example.py`::
|
|
|
|
def greet(name):
|
|
print "Hello, {0}!".format(name)
|
|
print "What's your name?"
|
|
name = raw_input()
|
|
greet(name)
|
|
|
|
It can be converted to Python 3.x code via 2to3 on the command line::
|
|
|
|
$ 2to3 example.py
|
|
|
|
A diff against the original source file is printed. 2to3 can also write the
|
|
needed modifications right back to the source file. (Of course, a backup of the
|
|
original is also be made unless :option:`-n` is also given.) Writing the
|
|
changes back is enabled with the :option:`-w` flag::
|
|
|
|
$ 2to3 -w example.py
|
|
|
|
After transformation, :file:`example.py` looks like this::
|
|
|
|
def greet(name):
|
|
print("Hello, {0}!".format(name))
|
|
print("What's your name?")
|
|
name = input()
|
|
greet(name)
|
|
|
|
Comments and exact indentation are preserved throughout the translation process.
|
|
|
|
By default, 2to3 runs a set of predefined fixers. The :option:`-l` flag lists
|
|
all available fixers. An explicit set of fixers to run can be given with
|
|
:option:`-f`. Likewise the :option:`-x` explicitly disables a fixer. The
|
|
following example runs only the ``imports`` and ``has_key`` fixers::
|
|
|
|
$ 2to3 -f imports -f has_key example.py
|
|
|
|
This command runs every fixer except the ``apply`` fixer::
|
|
|
|
$ 2to3 -x apply example.py
|
|
|
|
Some fixers are *explicit*, meaning they aren't run by default and must be
|
|
listed on the command line to be run. Here, in addition to the default fixers,
|
|
the ``idioms`` fixer is run::
|
|
|
|
$ 2to3 -f all -f idioms example.py
|
|
|
|
Notice how passing ``all`` enables all default fixers.
|
|
|
|
Sometimes 2to3 will find a place in your source code that needs to be changed,
|
|
but 2to3 cannot fix automatically. In this case, 2to3 will print a warning
|
|
beneath the diff for a file. You should address the warning in order to have
|
|
compliant 3.x code.
|
|
|
|
2to3 can also refactor doctests. To enable this mode, use the :option:`-d`
|
|
flag. Note that *only* doctests will be refactored. This also doesn't require
|
|
the module to be valid Python. For example, doctest like examples in a reST
|
|
document could also be refactored with this option.
|
|
|
|
The :option:`-v` option enables output of more information on the translation
|
|
process.
|
|
|
|
When the :option:`-p` is passed, 2to3 treats ``print`` as a function instead of
|
|
a statement. This is useful when ``from __future__ import print_function`` is
|
|
being used. If this option is not given, the print fixer will surround print
|
|
calls in an extra set of parentheses because it cannot differentiate between the
|
|
print statement with parentheses (such as ``print ("a" + "b" + "c")``) and a
|
|
true function call.
|
|
|
|
|
|
:mod:`lib2to3` - 2to3's library
|
|
-------------------------------
|
|
|
|
.. module:: lib2to3
|
|
:synopsis: the 2to3 library
|
|
.. moduleauthor:: Guido van Rossum
|
|
.. moduleauthor:: Collin Winter
|
|
|
|
|
|
.. warning::
|
|
|
|
The :mod:`lib2to3` API should be considered unstable and may change
|
|
drastically in the future.
|
|
|
|
.. XXX What is the public interface anyway?
|