Add better error reporting for MemoryErrors caused by str->float conversions.
[python.git] / Doc / library / 2to3.rst
blob6f675b0576546656b4b3a0746f9dc2ccbf3f63c4
1 .. _2to3-reference:
3 2to3 - Automated Python 2 to 3 code translation
4 ===============================================
6 .. sectionauthor:: Benjamin Peterson <benjamin@python.org>
8 2to3 is a Python program that reads Python 2.x source code and applies a series
9 of *fixers* to transform it into valid Python 3.x code.  The standard library
10 contains a rich set of fixers that will handle almost all code.  2to3 supporting
11 library :mod:`lib2to3` is, however, a flexible and generic library, so it is
12 possible to write your own fixers for 2to3.  :mod:`lib2to3` could also be
13 adapted to custom applications in which Python code needs to be edited
14 automatically.
17 .. _2to3-using:
19 Using 2to3
20 ----------
22 2to3 will usually be installed with the Python interpreter as a script.  It is
23 also located in the :file:`Tools/scripts` directory of the Python root.
25 2to3's basic arguments are a list of files or directories to transform.  The
26 directories are to recursively traversed for Python sources.
28 Here is a sample Python 2.x source file, :file:`example.py`::
30    def greet(name):
31        print "Hello, {0}!".format(name)
32    print "What's your name?"
33    name = raw_input()
34    greet(name)
36 It can be converted to Python 3.x code via 2to3 on the command line::
38    $ 2to3 example.py
40 A diff against the original source file is printed.  2to3 can also write the
41 needed modifications right back to the source file.  (A backup of the original
42 file is made unless :option:`-n` is also given.)  Writing the changes back is
43 enabled with the :option:`-w` flag::
45    $ 2to3 -w example.py
47 After transformation, :file:`example.py` looks like this::
49    def greet(name):
50        print("Hello, {0}!".format(name))
51    print("What's your name?")
52    name = input()
53    greet(name)
55 Comments and exact indentation are preserved throughout the translation process.
57 By default, 2to3 runs a set of :ref:`predefined fixers <2to3-fixers>`.  The
58 :option:`-l` flag lists all available fixers.  An explicit set of fixers to run
59 can be given with :option:`-f`.  Likewise the :option:`-x` explicitly disables a
60 fixer.  The following example runs only the ``imports`` and ``has_key`` fixers::
62    $ 2to3 -f imports -f has_key example.py
64 This command runs every fixer except the ``apply`` fixer::
66    $ 2to3 -x apply example.py
68 Some fixers are *explicit*, meaning they aren't run by default and must be
69 listed on the command line to be run.  Here, in addition to the default fixers,
70 the ``idioms`` fixer is run::
72    $ 2to3 -f all -f idioms example.py
74 Notice how passing ``all`` enables all default fixers.
76 Sometimes 2to3 will find a place in your source code that needs to be changed,
77 but 2to3 cannot fix automatically.  In this case, 2to3 will print a warning
78 beneath the diff for a file.  You should address the warning in order to have
79 compliant 3.x code.
81 2to3 can also refactor doctests.  To enable this mode, use the :option:`-d`
82 flag.  Note that *only* doctests will be refactored.  This also doesn't require
83 the module to be valid Python.  For example, doctest like examples in a reST
84 document could also be refactored with this option.
86 The :option:`-v` option enables output of more information on the translation
87 process.
89 Since some print statements can be parsed as function calls or statements, 2to3
90 cannot always read files containing the print function.  When 2to3 detects the
91 presence of the ``from __future__ import print_function`` compiler directive, it
92 modifies its internal grammar to interpert :func:`print` as a function.  This
93 change can also be enabled manually with the :option:`-p` flag.  Use
94 :option:`-p` to run fixers on code that already has had its print statements
95 converted.
98 .. _2to3-fixers:
100 Fixers
101 ------
103 Each step of transforming code is encapsulated in a fixer.  The command ``2to3
104 -l`` lists them.  As :ref:`documented above <2to3-using>`, each can be turned on
105 and off individually.  They are described here in more detail.
108 .. 2to3fixer:: apply
110    Removes usage of :func:`apply`.  For example ``apply(function, *args,
111    **kwargs)`` is converted to ``function(*args, **kwargs)``.
113 .. 2to3fixer:: basestring
115    Converts :class:`basestring` to :class:`str`.
117 .. 2to3fixer:: buffer
119    Converts :class:`buffer` to :class:`memoryview`.  This fixer is optional
120    because the :class:`memoryview` API is similar but not exactly the same as
121    that of :class:`buffer`.
123 .. 2to3fixer:: callable
125    Converts ``callable(x)`` to ``isinstance(x, collections.Callable)``, adding
126    an import to :mod:`collections` if needed.
128 .. 2to3fixer:: dict
130    Fixes dictionary iteration methods.  :meth:`dict.iteritems` is converted to
131    :meth:`dict.items`, :meth:`dict.iterkeys` to :meth:`dict.keys`, and
132    :meth:`dict.itervalues` to :meth:`dict.values`.  Similarly,
133    :meth:`dict.viewitems`, :meth:`dict.viewkeys` and :meth:`dict.viewvalues`
134    are converted respectively to :meth:`dict.items`, :meth:`dict.keys` and
135    :meth:`dict.values`.  It also wraps existing usages of :meth:`dict.items`,
136    :meth:`dict.keys`, and :meth:`dict.values` in a call to :class:`list`.
138 .. 2to3fixer:: except
140    Converts ``except X, T`` to ``except X as T``.
142 .. 2to3fixer:: exec
144    Converts the :keyword:`exec` statement to the :func:`exec` function.
146 .. 2to3fixer:: execfile
148    Removes usage of :func:`execfile`.  The argument to :func:`execfile` is
149    wrapped in calls to :func:`open`, :func:`compile`, and :func:`exec`.
151 .. 2to3fixer:: filter
153    Wraps :func:`filter` usage in a :class:`list` call.
155 .. 2to3fixer:: funcattrs
157    Fixes function attributes that have been renamed.  For example,
158    ``my_function.func_closure`` is converted to ``my_function.__closure__``.
160 .. 2to3fixer:: future
162    Removes ``from __future__ import new_feature`` statements.
164 .. 2to3fixer:: getcwdu
166    Renames :func:`os.getcwdu` to :func:`os.getcwd`.
168 .. 2to3fixer:: has_key
170    Changes ``dict.has_key(key)`` to ``key in dict``.
172 .. 2to3fixer:: idioms
174    This optional fixer performs several transformations that make Python code
175    more idiomatic.  Type comparisons like ``type(x) is SomeClass`` and
176    ``type(x) == SomeClass`` are converted to ``isinstance(x, SomeClass)``.
177    ``while 1`` becomes ``while True``.  This fixer also tries to make use of
178    :func:`sorted` in appropriate places.  For example, this block ::
180        L = list(some_iterable)
181        L.sort()
183    is changed to ::
185       L = sorted(some_iterable)
187 .. 2to3fixer:: import
189    Detects sibling imports and converts them to relative imports.
191 .. 2to3fixer:: imports
193    Handles module renames in the standard library.
195 .. 2to3fixer:: imports2
197    Handles other modules renames in the standard library.  It is separate from
198    the :2to3fixer:`imports` fixer only because of technical limitations.
200 .. 2to3fixer:: input
202    Converts ``input(prompt)`` to ``eval(input(prompt))``
204 .. 2to3fixer:: intern
206    Converts :func:`intern` to :func:`sys.intern`.
208 .. 2to3fixer:: isinstance
210    Fixes duplicate types in the second argument of :func:`isinstance`.  For
211    example, ``isinstance(x, (int, int))`` is converted to ``isinstance(x,
212    (int))``.
214 .. 2to3fixer:: itertools_imports
216    Removes imports of :func:`itertools.ifilter`, :func:`itertools.izip`, and
217    :func:`itertools.imap`.  Imports of :func:`itertools.ifilterfalse` are also
218    changed to :func:`itertools.filterfalse`.
220 .. 2to3fixer:: itertools
222    Changes usage of :func:`itertools.ifilter`, :func:`itertools.izip`, and
223    :func:`itertools.imap` to their built-in equivalents.
224    :func:`itertools.ifilterfalse` is changed to :func:`itertools.filterfalse`.
226 .. 2to3fixer:: long
228    Strips the ``L`` prefix on long literals and renames :class:`long` to
229    :class:`int`.
231 .. 2to3fixer:: map
233    Wraps :func:`map` in a :class:`list` call.  It also changes ``map(None, x)``
234    to ``list(x)``.  Using ``from future_builtins import map`` disables this
235    fixer.
237 .. 2to3fixer:: metaclass
239    Converts the old metaclass syntax (``__metaclass__ = Meta`` in the class
240    body) to the new (``class X(metaclass=Meta)``).
242 .. 2to3fixer:: methodattrs
244    Fixes old method attribute names.  For example, ``meth.im_func`` is converted
245    to ``meth.__func__``.
247 .. 2to3fixer:: ne
249    Converts the old not-equal syntax, ``<>``, to ``!=``.
251 .. 2to3fixer:: next
253    Converts the use of iterator's :meth:`~iterator.next` methods to the
254    :func:`next` function.  It also renames :meth:`next` methods to
255    :meth:`~object.__next__`.
257 .. 2to3fixer:: nonzero
259    Renames :meth:`~object.__nonzero__` to :meth:`~object.__bool__`.
261 .. 2to3fixer:: numliterals
263    Converts octal literals into the new syntax.
265 .. 2to3fixer:: paren
267    Add extra parenthesis where they are required in list comprehensions.  For
268    example, ``[x for x in 1, 2]`` becomes ``[x for x in (1, 2)]``.
270 .. 2to3fixer:: print
272    Converts the :keyword:`print` statement to the :func:`print` function.
274 .. 2to3fixer:: raises
276    Converts ``raise E, V`` to ``raise E(V)``, and ``raise E, V, T`` to ``raise
277    E(V).with_traceback(T)``.  If ``E`` is a tuple, the translation will be
278    incorrect because substituting tuples for exceptions has been removed in 3.0.
280 .. 2to3fixer:: raw_input
282    Converts :func:`raw_input` to :func:`input`.
284 .. 2to3fixer:: reduce
286    Handles the move of :func:`reduce` to :func:`functools.reduce`.
288 .. 2to3fixer:: renames
290    Changes :data:`sys.maxint` to :data:`sys.maxsize`.
292 .. 2to3fixer:: repr
294    Replaces backtick repr with the :func:`repr` function.
296 .. 2to3fixer:: set_literal
298    Replaces use of the :class:`set` constructor with set literals.  This fixer
299    is optional.
301 .. 2to3fixer:: standard_error
303    Renames :exc:`StandardError` to :exc:`Exception`.
305 .. 2to3fixer:: sys_exc
307    Changes the deprecated :data:`sys.exc_value`, :data:`sys.exc_type`,
308    :data:`sys.exc_traceback` to use :func:`sys.exc_info`.
310 .. 2to3fixer:: throw
312    Fixes the API change in generator's :meth:`throw` method.
314 .. 2to3fixer:: tuple_params
316    Removes implicit tuple parameter unpacking.  This fixer inserts temporary
317    variables.
319 .. 2to3fixer:: types
321    Fixes code broken from the removal of some members in the :mod:`types`
322    module.
324 .. 2to3fixer:: unicode
326    Renames :class:`unicode` to :class:`str`.
328 .. 2to3fixer:: urllib
330    Handles the rename of :mod:`urllib` and :mod:`urllib2` to the :mod:`urllib`
331    package.
333 .. 2to3fixer:: ws_comma
335    Removes excess whitespace from comma separated items.  This fixer is
336    optional.
338 .. 2to3fixer:: xrange
340    Renames :func:`xrange` to :func:`range` and wraps existing :func:`range`
341    calls with :class:`list`.
343 .. 2to3fixer:: xreadlines
345    Changes ``for x in file.xreadlines()`` to ``for x in file``.
347 .. 2to3fixer:: zip
349    Wraps :func:`zip` usage in a :class:`list` call.  This is disabled when
350    ``from future_builtins import zip`` appears.
353 :mod:`lib2to3` - 2to3's library
354 -------------------------------
356 .. module:: lib2to3
357    :synopsis: the 2to3 library
358 .. moduleauthor:: Guido van Rossum
359 .. moduleauthor:: Collin Winter
360 .. moduleauthor:: Benjamin Peterson <benjamin@python.org>
363 .. note::
365    The :mod:`lib2to3` API should be considered unstable and may change
366    drastically in the future.
368 .. XXX What is the public interface anyway?