Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 1 | .. _2to3-reference: |
| 2 | |
| 3 | 2to3 - Automated Python 2 to 3 code translation |
| 4 | =============================================== |
| 5 | |
Benjamin Peterson | 51a3703 | 2009-01-11 19:48:15 +0000 | [diff] [blame] | 6 | .. sectionauthor:: Benjamin Peterson <benjamin@python.org> |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 7 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 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. |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 15 | |
| 16 | |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 17 | .. _2to3-using: |
| 18 | |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 19 | Using 2to3 |
| 20 | ---------- |
| 21 | |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 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. |
| 24 | |
| 25 | 2to3's basic arguments are a list of files or directories to transform. The |
| 26 | directories are to recursively traversed for Python sources. |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 27 | |
| 28 | Here is a sample Python 2.x source file, :file:`example.py`:: |
| 29 | |
| 30 | def greet(name): |
Georg Brandl | 340739e | 2008-07-24 07:09:21 +0000 | [diff] [blame] | 31 | print "Hello, {0}!".format(name) |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 32 | print "What's your name?" |
| 33 | name = raw_input() |
| 34 | greet(name) |
| 35 | |
| 36 | It can be converted to Python 3.x code via 2to3 on the command line:: |
| 37 | |
| 38 | $ 2to3 example.py |
| 39 | |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 40 | A diff against the original source file is printed. 2to3 can also write the |
Georg Brandl | 52bc7b8 | 2009-02-23 18:33:48 +0000 | [diff] [blame] | 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:: |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 44 | |
| 45 | $ 2to3 -w example.py |
| 46 | |
Benjamin Peterson | ad2a9e7 | 2008-09-06 03:00:00 +0000 | [diff] [blame] | 47 | After transformation, :file:`example.py` looks like this:: |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 48 | |
| 49 | def greet(name): |
Benjamin Peterson | 3ac2f24 | 2008-07-25 21:59:53 +0000 | [diff] [blame] | 50 | print("Hello, {0}!".format(name)) |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 51 | print("What's your name?") |
| 52 | name = input() |
| 53 | greet(name) |
| 54 | |
Benjamin Peterson | cd29e9d | 2008-10-22 21:05:30 +0000 | [diff] [blame] | 55 | Comments and exact indentation are preserved throughout the translation process. |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 56 | |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 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:: |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 61 | |
| 62 | $ 2to3 -f imports -f has_key example.py |
| 63 | |
Benjamin Peterson | 0ecbcca | 2008-10-13 21:51:40 +0000 | [diff] [blame] | 64 | This command runs every fixer except the ``apply`` fixer:: |
| 65 | |
| 66 | $ 2to3 -x apply example.py |
| 67 | |
Benjamin Peterson | 92be539 | 2008-10-22 20:57:43 +0000 | [diff] [blame] | 68 | Some fixers are *explicit*, meaning they aren't run by default and must be |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 69 | listed on the command line to be run. Here, in addition to the default fixers, |
| 70 | the ``idioms`` fixer is run:: |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 71 | |
| 72 | $ 2to3 -f all -f idioms example.py |
| 73 | |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 74 | Notice how passing ``all`` enables all default fixers. |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 75 | |
Benjamin Peterson | 92be539 | 2008-10-22 20:57:43 +0000 | [diff] [blame] | 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. |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 80 | |
| 81 | 2to3 can also refactor doctests. To enable this mode, use the :option:`-d` |
Benjamin Peterson | b51f81d | 2008-09-28 01:53:29 +0000 | [diff] [blame] | 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. |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | 0ecbcca | 2008-10-13 21:51:40 +0000 | [diff] [blame] | 86 | The :option:`-v` option enables output of more information on the translation |
| 87 | process. |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 88 | |
Benjamin Peterson | 0ac6942 | 2009-11-09 04:10:53 +0000 | [diff] [blame] | 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 |
Georg Brandl | 0930228 | 2010-10-06 09:32:48 +0000 | [diff] [blame] | 92 | modifies its internal grammar to interpret :func:`print` as a function. This |
Benjamin Peterson | 0ac6942 | 2009-11-09 04:10:53 +0000 | [diff] [blame] | 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. |
| 96 | |
Gregory P. Smith | 1242699 | 2012-02-12 15:51:21 -0800 | [diff] [blame] | 97 | The :option:`-o` or :option:`--output-dir` option allows specification of an |
| 98 | alternate directory for processed output files to be written to. The |
| 99 | :option:`-n` flag is required when using this as backup files do not make sense |
| 100 | when not overwriting the input files. |
| 101 | |
| 102 | .. versionadded:: 2.7.3 |
| 103 | The :option:`-o` option was added. |
| 104 | |
| 105 | The :option:`-W` or :option:`--write-unchanged-files` flag tells 2to3 to always |
| 106 | write output files even if no changes were required to the file. This is most |
| 107 | useful with :option:`-o` so that an entire Python source tree is copied with |
| 108 | translation from one directory to another. |
| 109 | This option implies the :option:`-w` flag as it would not make sense otherwise. |
| 110 | |
| 111 | .. versionadded:: 2.7.3 |
| 112 | The :option:`-W` flag was added. |
| 113 | |
| 114 | The :option:`--add-suffix` option specifies a string to append to all output |
| 115 | filenames. The :option:`-n` flag is required when specifying this as backups |
| 116 | are not necessary when writing to different filenames. Example:: |
| 117 | |
| 118 | $ 2to3 -n -W --add-suffix=3 example.py |
| 119 | |
| 120 | Will cause a converted file named ``example.py3`` to be written. |
| 121 | |
| 122 | .. versionadded:: 2.7.3 |
| 123 | The :option:`--add-suffix` option was added. |
| 124 | |
| 125 | To translate an entire project from one directory tree to another use:: |
| 126 | |
| 127 | $ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode |
| 128 | |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 129 | |
| 130 | .. _2to3-fixers: |
| 131 | |
| 132 | Fixers |
| 133 | ------ |
| 134 | |
Georg Brandl | e83a4ad | 2009-03-13 19:03:58 +0000 | [diff] [blame] | 135 | Each step of transforming code is encapsulated in a fixer. The command ``2to3 |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 136 | -l`` lists them. As :ref:`documented above <2to3-using>`, each can be turned on |
| 137 | and off individually. They are described here in more detail. |
| 138 | |
| 139 | |
| 140 | .. 2to3fixer:: apply |
| 141 | |
| 142 | Removes usage of :func:`apply`. For example ``apply(function, *args, |
| 143 | **kwargs)`` is converted to ``function(*args, **kwargs)``. |
| 144 | |
| 145 | .. 2to3fixer:: basestring |
| 146 | |
| 147 | Converts :class:`basestring` to :class:`str`. |
| 148 | |
| 149 | .. 2to3fixer:: buffer |
| 150 | |
| 151 | Converts :class:`buffer` to :class:`memoryview`. This fixer is optional |
| 152 | because the :class:`memoryview` API is similar but not exactly the same as |
| 153 | that of :class:`buffer`. |
| 154 | |
| 155 | .. 2to3fixer:: callable |
| 156 | |
Benjamin Peterson | 0d19eaf | 2009-12-28 20:51:17 +0000 | [diff] [blame] | 157 | Converts ``callable(x)`` to ``isinstance(x, collections.Callable)``, adding |
Benjamin Peterson | 414ffa8 | 2011-10-24 08:51:15 -0400 | [diff] [blame] | 158 | an import to :mod:`collections` if needed. Note ``callable(x)`` has returned |
| 159 | in Python 3.2, so if you do not intend to support Python 3.1, you can disable |
| 160 | this fixer. |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 161 | |
| 162 | .. 2to3fixer:: dict |
| 163 | |
| 164 | Fixes dictionary iteration methods. :meth:`dict.iteritems` is converted to |
| 165 | :meth:`dict.items`, :meth:`dict.iterkeys` to :meth:`dict.keys`, and |
Alexandre Vassalotti | b227f47 | 2010-01-12 18:25:33 +0000 | [diff] [blame] | 166 | :meth:`dict.itervalues` to :meth:`dict.values`. Similarly, |
Benjamin Peterson | 2405547 | 2010-03-20 16:17:37 +0000 | [diff] [blame] | 167 | :meth:`dict.viewitems`, :meth:`dict.viewkeys` and :meth:`dict.viewvalues` are |
| 168 | converted respectively to :meth:`dict.items`, :meth:`dict.keys` and |
Alexandre Vassalotti | b227f47 | 2010-01-12 18:25:33 +0000 | [diff] [blame] | 169 | :meth:`dict.values`. It also wraps existing usages of :meth:`dict.items`, |
| 170 | :meth:`dict.keys`, and :meth:`dict.values` in a call to :class:`list`. |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 171 | |
| 172 | .. 2to3fixer:: except |
| 173 | |
| 174 | Converts ``except X, T`` to ``except X as T``. |
| 175 | |
| 176 | .. 2to3fixer:: exec |
| 177 | |
| 178 | Converts the :keyword:`exec` statement to the :func:`exec` function. |
| 179 | |
| 180 | .. 2to3fixer:: execfile |
| 181 | |
| 182 | Removes usage of :func:`execfile`. The argument to :func:`execfile` is |
| 183 | wrapped in calls to :func:`open`, :func:`compile`, and :func:`exec`. |
| 184 | |
Benjamin Peterson | d47667c | 2010-03-20 16:16:44 +0000 | [diff] [blame] | 185 | .. 2to3fixer:: exitfunc |
| 186 | |
| 187 | Changes assignment of :attr:`sys.exitfunc` to use of the :mod:`atexit` |
| 188 | module. |
| 189 | |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 190 | .. 2to3fixer:: filter |
| 191 | |
Benjamin Peterson | b8e17f7 | 2009-02-08 15:14:57 +0000 | [diff] [blame] | 192 | Wraps :func:`filter` usage in a :class:`list` call. |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 193 | |
| 194 | .. 2to3fixer:: funcattrs |
| 195 | |
| 196 | Fixes function attributes that have been renamed. For example, |
| 197 | ``my_function.func_closure`` is converted to ``my_function.__closure__``. |
| 198 | |
| 199 | .. 2to3fixer:: future |
| 200 | |
| 201 | Removes ``from __future__ import new_feature`` statements. |
| 202 | |
| 203 | .. 2to3fixer:: getcwdu |
| 204 | |
| 205 | Renames :func:`os.getcwdu` to :func:`os.getcwd`. |
| 206 | |
| 207 | .. 2to3fixer:: has_key |
| 208 | |
| 209 | Changes ``dict.has_key(key)`` to ``key in dict``. |
| 210 | |
| 211 | .. 2to3fixer:: idioms |
| 212 | |
Georg Brandl | e83a4ad | 2009-03-13 19:03:58 +0000 | [diff] [blame] | 213 | This optional fixer performs several transformations that make Python code |
| 214 | more idiomatic. Type comparisons like ``type(x) is SomeClass`` and |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 215 | ``type(x) == SomeClass`` are converted to ``isinstance(x, SomeClass)``. |
| 216 | ``while 1`` becomes ``while True``. This fixer also tries to make use of |
Georg Brandl | e83a4ad | 2009-03-13 19:03:58 +0000 | [diff] [blame] | 217 | :func:`sorted` in appropriate places. For example, this block :: |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 218 | |
| 219 | L = list(some_iterable) |
| 220 | L.sort() |
| 221 | |
| 222 | is changed to :: |
| 223 | |
| 224 | L = sorted(some_iterable) |
| 225 | |
| 226 | .. 2to3fixer:: import |
| 227 | |
| 228 | Detects sibling imports and converts them to relative imports. |
| 229 | |
| 230 | .. 2to3fixer:: imports |
| 231 | |
| 232 | Handles module renames in the standard library. |
| 233 | |
| 234 | .. 2to3fixer:: imports2 |
| 235 | |
Benjamin Peterson | b8e17f7 | 2009-02-08 15:14:57 +0000 | [diff] [blame] | 236 | Handles other modules renames in the standard library. It is separate from |
| 237 | the :2to3fixer:`imports` fixer only because of technical limitations. |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 238 | |
| 239 | .. 2to3fixer:: input |
| 240 | |
| 241 | Converts ``input(prompt)`` to ``eval(input(prompt))`` |
| 242 | |
| 243 | .. 2to3fixer:: intern |
| 244 | |
Benjamin Peterson | b8e17f7 | 2009-02-08 15:14:57 +0000 | [diff] [blame] | 245 | Converts :func:`intern` to :func:`sys.intern`. |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 246 | |
| 247 | .. 2to3fixer:: isinstance |
| 248 | |
| 249 | Fixes duplicate types in the second argument of :func:`isinstance`. For |
| 250 | example, ``isinstance(x, (int, int))`` is converted to ``isinstance(x, |
| 251 | (int))``. |
| 252 | |
| 253 | .. 2to3fixer:: itertools_imports |
| 254 | |
| 255 | Removes imports of :func:`itertools.ifilter`, :func:`itertools.izip`, and |
| 256 | :func:`itertools.imap`. Imports of :func:`itertools.ifilterfalse` are also |
| 257 | changed to :func:`itertools.filterfalse`. |
| 258 | |
| 259 | .. 2to3fixer:: itertools |
| 260 | |
| 261 | Changes usage of :func:`itertools.ifilter`, :func:`itertools.izip`, and |
Georg Brandl | d7d4fd7 | 2009-07-26 14:37:28 +0000 | [diff] [blame] | 262 | :func:`itertools.imap` to their built-in equivalents. |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 263 | :func:`itertools.ifilterfalse` is changed to :func:`itertools.filterfalse`. |
| 264 | |
| 265 | .. 2to3fixer:: long |
| 266 | |
Raymond Hettinger | cec795d | 2011-07-14 14:41:43 +0800 | [diff] [blame] | 267 | Strips the ``L`` suffix on long literals and renames :class:`long` to |
Benjamin Peterson | b8e17f7 | 2009-02-08 15:14:57 +0000 | [diff] [blame] | 268 | :class:`int`. |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 269 | |
| 270 | .. 2to3fixer:: map |
| 271 | |
Benjamin Peterson | b8e17f7 | 2009-02-08 15:14:57 +0000 | [diff] [blame] | 272 | Wraps :func:`map` in a :class:`list` call. It also changes ``map(None, x)`` |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 273 | to ``list(x)``. Using ``from future_builtins import map`` disables this |
| 274 | fixer. |
| 275 | |
| 276 | .. 2to3fixer:: metaclass |
| 277 | |
| 278 | Converts the old metaclass syntax (``__metaclass__ = Meta`` in the class |
| 279 | body) to the new (``class X(metaclass=Meta)``). |
| 280 | |
| 281 | .. 2to3fixer:: methodattrs |
| 282 | |
| 283 | Fixes old method attribute names. For example, ``meth.im_func`` is converted |
| 284 | to ``meth.__func__``. |
| 285 | |
| 286 | .. 2to3fixer:: ne |
| 287 | |
| 288 | Converts the old not-equal syntax, ``<>``, to ``!=``. |
| 289 | |
| 290 | .. 2to3fixer:: next |
| 291 | |
Georg Brandl | 9fa61bb | 2009-07-26 14:19:57 +0000 | [diff] [blame] | 292 | Converts the use of iterator's :meth:`~iterator.next` methods to the |
| 293 | :func:`next` function. It also renames :meth:`next` methods to |
| 294 | :meth:`~object.__next__`. |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 295 | |
| 296 | .. 2to3fixer:: nonzero |
| 297 | |
| 298 | Renames :meth:`~object.__nonzero__` to :meth:`~object.__bool__`. |
| 299 | |
Benjamin Peterson | c5e68b1 | 2009-02-08 14:38:13 +0000 | [diff] [blame] | 300 | .. 2to3fixer:: numliterals |
| 301 | |
| 302 | Converts octal literals into the new syntax. |
| 303 | |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 304 | .. 2to3fixer:: paren |
| 305 | |
| 306 | Add extra parenthesis where they are required in list comprehensions. For |
Benjamin Peterson | b8e17f7 | 2009-02-08 15:14:57 +0000 | [diff] [blame] | 307 | example, ``[x for x in 1, 2]`` becomes ``[x for x in (1, 2)]``. |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 308 | |
| 309 | .. 2to3fixer:: print |
| 310 | |
| 311 | Converts the :keyword:`print` statement to the :func:`print` function. |
| 312 | |
Benjamin Peterson | 52a70c4 | 2010-07-01 17:45:52 +0000 | [diff] [blame] | 313 | .. 2to3fixer:: raise |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 314 | |
Benjamin Peterson | b8e17f7 | 2009-02-08 15:14:57 +0000 | [diff] [blame] | 315 | Converts ``raise E, V`` to ``raise E(V)``, and ``raise E, V, T`` to ``raise |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 316 | E(V).with_traceback(T)``. If ``E`` is a tuple, the translation will be |
| 317 | incorrect because substituting tuples for exceptions has been removed in 3.0. |
| 318 | |
| 319 | .. 2to3fixer:: raw_input |
| 320 | |
| 321 | Converts :func:`raw_input` to :func:`input`. |
| 322 | |
| 323 | .. 2to3fixer:: reduce |
| 324 | |
| 325 | Handles the move of :func:`reduce` to :func:`functools.reduce`. |
| 326 | |
| 327 | .. 2to3fixer:: renames |
| 328 | |
| 329 | Changes :data:`sys.maxint` to :data:`sys.maxsize`. |
| 330 | |
| 331 | .. 2to3fixer:: repr |
| 332 | |
| 333 | Replaces backtick repr with the :func:`repr` function. |
| 334 | |
| 335 | .. 2to3fixer:: set_literal |
| 336 | |
| 337 | Replaces use of the :class:`set` constructor with set literals. This fixer |
| 338 | is optional. |
| 339 | |
| 340 | .. 2to3fixer:: standard_error |
| 341 | |
| 342 | Renames :exc:`StandardError` to :exc:`Exception`. |
| 343 | |
| 344 | .. 2to3fixer:: sys_exc |
| 345 | |
| 346 | Changes the deprecated :data:`sys.exc_value`, :data:`sys.exc_type`, |
| 347 | :data:`sys.exc_traceback` to use :func:`sys.exc_info`. |
| 348 | |
| 349 | .. 2to3fixer:: throw |
| 350 | |
Benjamin Peterson | b8e17f7 | 2009-02-08 15:14:57 +0000 | [diff] [blame] | 351 | Fixes the API change in generator's :meth:`throw` method. |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 352 | |
| 353 | .. 2to3fixer:: tuple_params |
| 354 | |
| 355 | Removes implicit tuple parameter unpacking. This fixer inserts temporary |
| 356 | variables. |
| 357 | |
| 358 | .. 2to3fixer:: types |
| 359 | |
| 360 | Fixes code broken from the removal of some members in the :mod:`types` |
| 361 | module. |
| 362 | |
| 363 | .. 2to3fixer:: unicode |
| 364 | |
| 365 | Renames :class:`unicode` to :class:`str`. |
| 366 | |
| 367 | .. 2to3fixer:: urllib |
| 368 | |
| 369 | Handles the rename of :mod:`urllib` and :mod:`urllib2` to the :mod:`urllib` |
| 370 | package. |
| 371 | |
| 372 | .. 2to3fixer:: ws_comma |
| 373 | |
| 374 | Removes excess whitespace from comma separated items. This fixer is |
| 375 | optional. |
| 376 | |
| 377 | .. 2to3fixer:: xrange |
| 378 | |
| 379 | Renames :func:`xrange` to :func:`range` and wraps existing :func:`range` |
| 380 | calls with :class:`list`. |
| 381 | |
| 382 | .. 2to3fixer:: xreadlines |
| 383 | |
Benjamin Peterson | b8e17f7 | 2009-02-08 15:14:57 +0000 | [diff] [blame] | 384 | Changes ``for x in file.xreadlines()`` to ``for x in file``. |
Benjamin Peterson | e0820e2 | 2009-02-07 23:01:19 +0000 | [diff] [blame] | 385 | |
| 386 | .. 2to3fixer:: zip |
| 387 | |
| 388 | Wraps :func:`zip` usage in a :class:`list` call. This is disabled when |
| 389 | ``from future_builtins import zip`` appears. |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 390 | |
| 391 | |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 392 | :mod:`lib2to3` - 2to3's library |
| 393 | ------------------------------- |
| 394 | |
| 395 | .. module:: lib2to3 |
| 396 | :synopsis: the 2to3 library |
| 397 | .. moduleauthor:: Guido van Rossum |
| 398 | .. moduleauthor:: Collin Winter |
Benjamin Peterson | 2b42c29 | 2009-05-02 20:26:53 +0000 | [diff] [blame] | 399 | .. moduleauthor:: Benjamin Peterson <benjamin@python.org> |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 400 | |
Benjamin Peterson | 7f8f660 | 2008-09-27 16:23:55 +0000 | [diff] [blame] | 401 | |
Georg Brandl | 16a57f6 | 2009-04-27 15:29:09 +0000 | [diff] [blame] | 402 | .. note:: |
Benjamin Peterson | 7f8f660 | 2008-09-27 16:23:55 +0000 | [diff] [blame] | 403 | |
| 404 | The :mod:`lib2to3` API should be considered unstable and may change |
| 405 | drastically in the future. |
| 406 | |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 407 | .. XXX What is the public interface anyway? |