95
Each step of tranforming code is encapsulated in a fixer. The command ``2to3
96
-l`` lists them. As :ref:`documented above <2to3-using>`, each can be turned on
97
and off individually. They are described here in more detail.
102
Removes usage of :func:`apply`. For example ``apply(function, *args,
103
**kwargs)`` is converted to ``function(*args, **kwargs)``.
105
.. 2to3fixer:: basestring
107
Converts :class:`basestring` to :class:`str`.
109
.. 2to3fixer:: buffer
111
Converts :class:`buffer` to :class:`memoryview`. This fixer is optional
112
because the :class:`memoryview` API is similar but not exactly the same as
113
that of :class:`buffer`.
115
.. 2to3fixer:: callable
117
Converts ``callable(x)`` to ``hasattr(x, "__call_")``.
121
Fixes dictionary iteration methods. :meth:`dict.iteritems` is converted to
122
:meth:`dict.items`, :meth:`dict.iterkeys` to :meth:`dict.keys`, and
123
:meth:`dict.itervalues` to :meth:`dict.values`. It also wraps existing
124
usages of :meth:`dict.items`, :meth:`dict.keys`, and :meth:`dict.values` in a
125
call to :class:`list`.
127
.. 2to3fixer:: except
129
Converts ``except X, T`` to ``except X as T``.
133
Converts the :keyword:`exec` statement to the :func:`exec` function.
135
.. 2to3fixer:: execfile
137
Removes usage of :func:`execfile`. The argument to :func:`execfile` is
138
wrapped in calls to :func:`open`, :func:`compile`, and :func:`exec`.
140
.. 2to3fixer:: filter
142
Wraps :func:`filter` usage in a :class:`list` call.
144
.. 2to3fixer:: funcattrs
146
Fixes function attributes that have been renamed. For example,
147
``my_function.func_closure`` is converted to ``my_function.__closure__``.
149
.. 2to3fixer:: future
151
Removes ``from __future__ import new_feature`` statements.
153
.. 2to3fixer:: getcwdu
155
Renames :func:`os.getcwdu` to :func:`os.getcwd`.
157
.. 2to3fixer:: has_key
159
Changes ``dict.has_key(key)`` to ``key in dict``.
161
.. 2to3fixer:: idioms
163
This optional fixer preforms several transformations that make Python code
164
more idiomatic. Type comparisions like ``type(x) is SomeClass`` and
165
``type(x) == SomeClass`` are converted to ``isinstance(x, SomeClass)``.
166
``while 1`` becomes ``while True``. This fixer also tries to make use of
167
:func:`sorted` in appropiate places. For example, this block ::
169
L = list(some_iterable)
174
L = sorted(some_iterable)
176
.. 2to3fixer:: import
178
Detects sibling imports and converts them to relative imports.
180
.. 2to3fixer:: imports
182
Handles module renames in the standard library.
184
.. 2to3fixer:: imports2
186
Handles other modules renames in the standard library. It is separate from
187
the :2to3fixer:`imports` fixer only because of technical limitations.
191
Converts ``input(prompt)`` to ``eval(input(prompt))``
193
.. 2to3fixer:: intern
195
Converts :func:`intern` to :func:`sys.intern`.
197
.. 2to3fixer:: isinstance
199
Fixes duplicate types in the second argument of :func:`isinstance`. For
200
example, ``isinstance(x, (int, int))`` is converted to ``isinstance(x,
203
.. 2to3fixer:: itertools_imports
205
Removes imports of :func:`itertools.ifilter`, :func:`itertools.izip`, and
206
:func:`itertools.imap`. Imports of :func:`itertools.ifilterfalse` are also
207
changed to :func:`itertools.filterfalse`.
209
.. 2to3fixer:: itertools
211
Changes usage of :func:`itertools.ifilter`, :func:`itertools.izip`, and
212
:func:`itertools.imap` to their built-in equivalents.
213
:func:`itertools.ifilterfalse` is changed to :func:`itertools.filterfalse`.
217
Strips the ``L`` prefix on long literals and renames :class:`long` to
222
Wraps :func:`map` in a :class:`list` call. It also changes ``map(None, x)``
223
to ``list(x)``. Using ``from future_builtins import map`` disables this
226
.. 2to3fixer:: metaclass
228
Converts the old metaclass syntax (``__metaclass__ = Meta`` in the class
229
body) to the new (``class X(metaclass=Meta)``).
231
.. 2to3fixer:: methodattrs
233
Fixes old method attribute names. For example, ``meth.im_func`` is converted
234
to ``meth.__func__``.
238
Converts the old not-equal syntax, ``<>``, to ``!=``.
242
Converts the use of iterator's :meth:`~iterator.next` methods to the
243
:func:`next` function. It also renames :meth:`next` methods to
244
:meth:`~object.__next__`.
246
.. 2to3fixer:: nonzero
248
Renames :meth:`~object.__nonzero__` to :meth:`~object.__bool__`.
250
.. 2to3fixer:: numliterals
252
Converts octal literals into the new syntax.
256
Add extra parenthesis where they are required in list comprehensions. For
257
example, ``[x for x in 1, 2]`` becomes ``[x for x in (1, 2)]``.
261
Converts the :keyword:`print` statement to the :func:`print` function.
263
.. 2to3fixer:: raises
265
Converts ``raise E, V`` to ``raise E(V)``, and ``raise E, V, T`` to ``raise
266
E(V).with_traceback(T)``. If ``E`` is a tuple, the translation will be
267
incorrect because substituting tuples for exceptions has been removed in 3.0.
269
.. 2to3fixer:: raw_input
271
Converts :func:`raw_input` to :func:`input`.
273
.. 2to3fixer:: reduce
275
Handles the move of :func:`reduce` to :func:`functools.reduce`.
277
.. 2to3fixer:: renames
279
Changes :data:`sys.maxint` to :data:`sys.maxsize`.
283
Replaces backtick repr with the :func:`repr` function.
285
.. 2to3fixer:: set_literal
287
Replaces use of the :class:`set` constructor with set literals. This fixer
290
.. 2to3fixer:: standard_error
292
Renames :exc:`StandardError` to :exc:`Exception`.
294
.. 2to3fixer:: sys_exc
296
Changes the deprecated :data:`sys.exc_value`, :data:`sys.exc_type`,
297
:data:`sys.exc_traceback` to use :func:`sys.exc_info`.
301
Fixes the API change in generator's :meth:`throw` method.
303
.. 2to3fixer:: tuple_params
305
Removes implicit tuple parameter unpacking. This fixer inserts temporary
310
Fixes code broken from the removal of some members in the :mod:`types`
313
.. 2to3fixer:: unicode
315
Renames :class:`unicode` to :class:`str`.
317
.. 2to3fixer:: urllib
319
Handles the rename of :mod:`urllib` and :mod:`urllib2` to the :mod:`urllib`
322
.. 2to3fixer:: ws_comma
324
Removes excess whitespace from comma separated items. This fixer is
327
.. 2to3fixer:: xrange
329
Renames :func:`xrange` to :func:`range` and wraps existing :func:`range`
330
calls with :class:`list`.
332
.. 2to3fixer:: xreadlines
334
Changes ``for x in file.xreadlines()`` to ``for x in file``.
338
Wraps :func:`zip` usage in a :class:`list` call. This is disabled when
339
``from future_builtins import zip`` appears.
88
342
:mod:`lib2to3` - 2to3's library
89
343
-------------------------------