mirror of
https://github.com/python/cpython.git
synced 2024-12-12 03:04:15 +08:00
25c95f1298
svn+ssh://pythondev@svn.python.org/python/trunk ........ r70768 | andrew.kuchling | 2009-03-30 17:29:15 -0500 (Mon, 30 Mar 2009) | 1 line Typo fixes ........ r71657 | vinay.sajip | 2009-04-16 14:07:37 -0500 (Thu, 16 Apr 2009) | 1 line Issue #5768: Change to Unicode output logic and test case for same. ........ r71721 | benjamin.peterson | 2009-04-18 14:26:19 -0500 (Sat, 18 Apr 2009) | 1 line fix a few nits in unittest.py #5771 ........ r71729 | benjamin.peterson | 2009-04-18 16:03:10 -0500 (Sat, 18 Apr 2009) | 1 line move test to a more appropiate one ........ r71794 | vinay.sajip | 2009-04-22 07:10:47 -0500 (Wed, 22 Apr 2009) | 2 lines Issue #5170: Fixed regression caused when fixing #5768. ........ r71976 | mark.dickinson | 2009-04-26 14:54:55 -0500 (Sun, 26 Apr 2009) | 2 lines Fix typo in function name ........ r72036 | georg.brandl | 2009-04-27 12:04:23 -0500 (Mon, 27 Apr 2009) | 1 line #5848: small unittest doc patch. ........ r72037 | georg.brandl | 2009-04-27 12:09:53 -0500 (Mon, 27 Apr 2009) | 1 line #5840: dont claim we dont support TLS. ........ r72079 | r.david.murray | 2009-04-28 14:02:55 -0500 (Tue, 28 Apr 2009) | 2 lines Remove spurious 'u'. ........ r72085 | georg.brandl | 2009-04-28 16:48:35 -0500 (Tue, 28 Apr 2009) | 1 line Make the doctests in the docs pass, except for those in the turtle module. ........ r72131 | benjamin.peterson | 2009-04-29 17:43:35 -0500 (Wed, 29 Apr 2009) | 1 line fix test_shutil on ZFS #5676 ........ r72132 | georg.brandl | 2009-04-29 17:44:07 -0500 (Wed, 29 Apr 2009) | 1 line #5878: fix repr of re object. ........ r72133 | benjamin.peterson | 2009-04-29 17:44:15 -0500 (Wed, 29 Apr 2009) | 1 line make sure mode is removable while cleaning up test droppings ........ r72134 | benjamin.peterson | 2009-04-29 19:06:33 -0500 (Wed, 29 Apr 2009) | 1 line make sure to close file ........ r72191 | michael.foord | 2009-05-02 06:43:06 -0500 (Sat, 02 May 2009) | 9 lines Adds an exit parameter to unittest.main(). If False main no longer calls sys.exit. Closes issue 3379. Michael Foord ........ r72197 | benjamin.peterson | 2009-05-02 11:24:37 -0500 (Sat, 02 May 2009) | 1 line don't let sys.argv be used in the tests ........ r72198 | andrew.kuchling | 2009-05-02 12:12:15 -0500 (Sat, 02 May 2009) | 1 line Add items ........ r72219 | michael.foord | 2009-05-02 15:15:05 -0500 (Sat, 02 May 2009) | 8 lines Add addCleanup and doCleanups to unittest.TestCase. Closes issue 5679. Michael Foord ........ r72221 | benjamin.peterson | 2009-05-02 15:26:53 -0500 (Sat, 02 May 2009) | 1 line add myself ........ r72225 | michael.foord | 2009-05-02 17:43:34 -0500 (Sat, 02 May 2009) | 1 line ........ r72303 | benjamin.peterson | 2009-05-04 19:55:24 -0500 (Mon, 04 May 2009) | 1 line using sys._getframe(x), where x > 0 doesnt' work on IronPython ........ r72434 | r.david.murray | 2009-05-07 13:09:58 -0500 (Thu, 07 May 2009) | 2 lines Pre-opened test file needs to be opened in binary mode. ........ r72467 | georg.brandl | 2009-05-08 07:17:34 -0500 (Fri, 08 May 2009) | 1 line Fix name. ........ r72476 | thomas.heller | 2009-05-08 15:09:40 -0500 (Fri, 08 May 2009) | 4 lines Add a file that contains diffs between offical libffi files and the files in this repository. Should make it easier to merge new libffi versions. ........
364 lines
11 KiB
ReStructuredText
364 lines
11 KiB
ReStructuredText
.. _2to3-reference:
|
|
|
|
2to3 - Automated Python 2 to 3 code translation
|
|
===============================================
|
|
|
|
.. sectionauthor:: Benjamin Peterson <benjamin@python.org>
|
|
|
|
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.
|
|
|
|
|
|
.. _2to3-using:
|
|
|
|
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. (A backup of the original
|
|
file is 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 :ref:`predefined fixers <2to3-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, the :2to3fixer:`print` fixer ``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.
|
|
|
|
|
|
.. _2to3-fixers:
|
|
|
|
Fixers
|
|
------
|
|
|
|
Each step of transforming code is encapsulated in a fixer. The command ``2to3
|
|
-l`` lists them. As :ref:`documented above <2to3-using>`, each can be turned on
|
|
and off individually. They are described here in more detail.
|
|
|
|
|
|
.. 2to3fixer:: apply
|
|
|
|
Removes usage of :func:`apply`. For example ``apply(function, *args,
|
|
**kwargs)`` is converted to ``function(*args, **kwargs)``.
|
|
|
|
.. 2to3fixer:: basestring
|
|
|
|
Converts :class:`basestring` to :class:`str`.
|
|
|
|
.. 2to3fixer:: buffer
|
|
|
|
Converts :class:`buffer` to :class:`memoryview`. This fixer is optional
|
|
because the :class:`memoryview` API is similar but not exactly the same as
|
|
that of :class:`buffer`.
|
|
|
|
.. 2to3fixer:: callable
|
|
|
|
Converts ``callable(x)`` to ``hasattr(x, "__call_")``.
|
|
|
|
.. 2to3fixer:: dict
|
|
|
|
Fixes dictionary iteration methods. :meth:`dict.iteritems` is converted to
|
|
:meth:`dict.items`, :meth:`dict.iterkeys` to :meth:`dict.keys`, and
|
|
:meth:`dict.itervalues` to :meth:`dict.values`. It also wraps existing
|
|
usages of :meth:`dict.items`, :meth:`dict.keys`, and :meth:`dict.values` in a
|
|
call to :class:`list`.
|
|
|
|
.. 2to3fixer:: except
|
|
|
|
Converts ``except X, T`` to ``except X as T``.
|
|
|
|
.. 2to3fixer:: exec
|
|
|
|
Converts the :keyword:`exec` statement to the :func:`exec` function.
|
|
|
|
.. 2to3fixer:: execfile
|
|
|
|
Removes usage of :func:`execfile`. The argument to :func:`execfile` is
|
|
wrapped in calls to :func:`open`, :func:`compile`, and :func:`exec`.
|
|
|
|
.. 2to3fixer:: filter
|
|
|
|
Wraps :func:`filter` usage in a :class:`list` call.
|
|
|
|
.. 2to3fixer:: funcattrs
|
|
|
|
Fixes function attributes that have been renamed. For example,
|
|
``my_function.func_closure`` is converted to ``my_function.__closure__``.
|
|
|
|
.. 2to3fixer:: future
|
|
|
|
Removes ``from __future__ import new_feature`` statements.
|
|
|
|
.. 2to3fixer:: getcwdu
|
|
|
|
Renames :func:`os.getcwdu` to :func:`os.getcwd`.
|
|
|
|
.. 2to3fixer:: has_key
|
|
|
|
Changes ``dict.has_key(key)`` to ``key in dict``.
|
|
|
|
.. 2to3fixer:: idioms
|
|
|
|
This optional fixer performs several transformations that make Python code
|
|
more idiomatic. Type comparisons like ``type(x) is SomeClass`` and
|
|
``type(x) == SomeClass`` are converted to ``isinstance(x, SomeClass)``.
|
|
``while 1`` becomes ``while True``. This fixer also tries to make use of
|
|
:func:`sorted` in appropriate places. For example, this block ::
|
|
|
|
L = list(some_iterable)
|
|
L.sort()
|
|
|
|
is changed to ::
|
|
|
|
L = sorted(some_iterable)
|
|
|
|
.. 2to3fixer:: import
|
|
|
|
Detects sibling imports and converts them to relative imports.
|
|
|
|
.. 2to3fixer:: imports
|
|
|
|
Handles module renames in the standard library.
|
|
|
|
.. 2to3fixer:: imports2
|
|
|
|
Handles other modules renames in the standard library. It is separate from
|
|
the :2to3fixer:`imports` fixer only because of technical limitations.
|
|
|
|
.. 2to3fixer:: input
|
|
|
|
Converts ``input(prompt)`` to ``eval(input(prompt))``
|
|
|
|
.. 2to3fixer:: intern
|
|
|
|
Converts :func:`intern` to :func:`sys.intern`.
|
|
|
|
.. 2to3fixer:: isinstance
|
|
|
|
Fixes duplicate types in the second argument of :func:`isinstance`. For
|
|
example, ``isinstance(x, (int, int))`` is converted to ``isinstance(x,
|
|
(int))``.
|
|
|
|
.. 2to3fixer:: itertools_imports
|
|
|
|
Removes imports of :func:`itertools.ifilter`, :func:`itertools.izip`, and
|
|
:func:`itertools.imap`. Imports of :func:`itertools.ifilterfalse` are also
|
|
changed to :func:`itertools.filterfalse`.
|
|
|
|
.. 2to3fixer:: itertools
|
|
|
|
Changes usage of :func:`itertools.ifilter`, :func:`itertools.izip`, and
|
|
:func:`itertools.imap` to their builtin equivalents.
|
|
:func:`itertools.ifilterfalse` is changed to :func:`itertools.filterfalse`.
|
|
|
|
.. 2to3fixer:: long
|
|
|
|
Strips the ``L`` prefix on long literals and renames :class:`long` to
|
|
:class:`int`.
|
|
|
|
.. 2to3fixer:: map
|
|
|
|
Wraps :func:`map` in a :class:`list` call. It also changes ``map(None, x)``
|
|
to ``list(x)``. Using ``from future_builtins import map`` disables this
|
|
fixer.
|
|
|
|
.. 2to3fixer:: metaclass
|
|
|
|
Converts the old metaclass syntax (``__metaclass__ = Meta`` in the class
|
|
body) to the new (``class X(metaclass=Meta)``).
|
|
|
|
.. 2to3fixer:: methodattrs
|
|
|
|
Fixes old method attribute names. For example, ``meth.im_func`` is converted
|
|
to ``meth.__func__``.
|
|
|
|
.. 2to3fixer:: ne
|
|
|
|
Converts the old not-equal syntax, ``<>``, to ``!=``.
|
|
|
|
.. 2to3fixer:: next
|
|
|
|
Converts the use of iterator's :meth:`next` methods to the :func:`next`
|
|
function. It also renames :meth:`next` methods to :meth:`~object.__next__`.
|
|
|
|
.. 2to3fixer:: nonzero
|
|
|
|
Renames :meth:`~object.__nonzero__` to :meth:`~object.__bool__`.
|
|
|
|
.. 2to3fixer:: numliterals
|
|
|
|
Converts octal literals into the new syntax.
|
|
|
|
.. 2to3fixer:: paren
|
|
|
|
Add extra parenthesis where they are required in list comprehensions. For
|
|
example, ``[x for x in 1, 2]`` becomes ``[x for x in (1, 2)]``.
|
|
|
|
.. 2to3fixer:: print
|
|
|
|
Converts the :keyword:`print` statement to the :func:`print` function.
|
|
|
|
.. 2to3fixer:: raises
|
|
|
|
Converts ``raise E, V`` to ``raise E(V)``, and ``raise E, V, T`` to ``raise
|
|
E(V).with_traceback(T)``. If ``E`` is a tuple, the translation will be
|
|
incorrect because substituting tuples for exceptions has been removed in 3.0.
|
|
|
|
.. 2to3fixer:: raw_input
|
|
|
|
Converts :func:`raw_input` to :func:`input`.
|
|
|
|
.. 2to3fixer:: reduce
|
|
|
|
Handles the move of :func:`reduce` to :func:`functools.reduce`.
|
|
|
|
.. 2to3fixer:: renames
|
|
|
|
Changes :data:`sys.maxint` to :data:`sys.maxsize`.
|
|
|
|
.. 2to3fixer:: repr
|
|
|
|
Replaces backtick repr with the :func:`repr` function.
|
|
|
|
.. 2to3fixer:: set_literal
|
|
|
|
Replaces use of the :class:`set` constructor with set literals. This fixer
|
|
is optional.
|
|
|
|
.. 2to3fixer:: standard_error
|
|
|
|
Renames :exc:`StandardError` to :exc:`Exception`.
|
|
|
|
.. 2to3fixer:: sys_exc
|
|
|
|
Changes the deprecated :data:`sys.exc_value`, :data:`sys.exc_type`,
|
|
:data:`sys.exc_traceback` to use :func:`sys.exc_info`.
|
|
|
|
.. 2to3fixer:: throw
|
|
|
|
Fixes the API change in generator's :meth:`throw` method.
|
|
|
|
.. 2to3fixer:: tuple_params
|
|
|
|
Removes implicit tuple parameter unpacking. This fixer inserts temporary
|
|
variables.
|
|
|
|
.. 2to3fixer:: types
|
|
|
|
Fixes code broken from the removal of some members in the :mod:`types`
|
|
module.
|
|
|
|
.. 2to3fixer:: unicode
|
|
|
|
Renames :class:`unicode` to :class:`str`.
|
|
|
|
.. 2to3fixer:: urllib
|
|
|
|
Handles the rename of :mod:`urllib` and :mod:`urllib2` to the :mod:`urllib`
|
|
package.
|
|
|
|
.. 2to3fixer:: ws_comma
|
|
|
|
Removes excess whitespace from comma separated items. This fixer is
|
|
optional.
|
|
|
|
.. 2to3fixer:: xrange
|
|
|
|
Renames :func:`xrange` to :func:`range` and wraps existing :func:`range`
|
|
calls with :class:`list`.
|
|
|
|
.. 2to3fixer:: xreadlines
|
|
|
|
Changes ``for x in file.xreadlines()`` to ``for x in file``.
|
|
|
|
.. 2to3fixer:: zip
|
|
|
|
Wraps :func:`zip` usage in a :class:`list` call. This is disabled when
|
|
``from future_builtins import zip`` appears.
|
|
|
|
|
|
:mod:`lib2to3` - 2to3's library
|
|
-------------------------------
|
|
|
|
.. module:: lib2to3
|
|
:synopsis: the 2to3 library
|
|
.. moduleauthor:: Guido van Rossum
|
|
.. moduleauthor:: Collin Winter
|
|
.. moduleauthor:: Benjamin Peterson <benjamin@python.org>
|
|
|
|
|
|
.. note::
|
|
|
|
The :mod:`lib2to3` API should be considered unstable and may change
|
|
drastically in the future.
|
|
|
|
.. XXX What is the public interface anyway?
|