Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 1 | .. _2to3-reference: |
| 2 | |
| 3 | 2to3 - Automated Python 2 to 3 code translation |
| 4 | =============================================== |
| 5 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 6 | .. sectionauthor:: Benjamin Peterson <benjamin@python.org> |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 7 | |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +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 | ae5360b | 2008-09-08 23:05:23 +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 |
Carl Meyer | 503de71 | 2020-04-24 12:19:46 -0600 | [diff] [blame] | 12 | possible to write your own fixers for 2to3. |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 13 | |
| 14 | |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 15 | .. _2to3-using: |
| 16 | |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 17 | Using 2to3 |
| 18 | ---------- |
| 19 | |
Benjamin Peterson | ae5360b | 2008-09-08 23:05:23 +0000 | [diff] [blame] | 20 | 2to3 will usually be installed with the Python interpreter as a script. It is |
| 21 | also located in the :file:`Tools/scripts` directory of the Python root. |
| 22 | |
| 23 | 2to3's basic arguments are a list of files or directories to transform. The |
Chris Jerdonek | 4639749 | 2012-10-11 15:59:32 -0700 | [diff] [blame] | 24 | directories are recursively traversed for Python sources. |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 25 | |
| 26 | Here is a sample Python 2.x source file, :file:`example.py`:: |
| 27 | |
| 28 | def greet(name): |
| 29 | print "Hello, {0}!".format(name) |
| 30 | print "What's your name?" |
| 31 | name = raw_input() |
| 32 | greet(name) |
| 33 | |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 34 | It can be converted to Python 3.x code via 2to3 on the command line: |
| 35 | |
| 36 | .. code-block:: shell-session |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 37 | |
| 38 | $ 2to3 example.py |
| 39 | |
Benjamin Peterson | ae5360b | 2008-09-08 23:05:23 +0000 | [diff] [blame] | 40 | A diff against the original source file is printed. 2to3 can also write the |
Benjamin Peterson | ad3d5c2 | 2009-02-26 03:38:59 +0000 | [diff] [blame] | 41 | needed modifications right back to the source file. (A backup of the original |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 42 | file is made unless :option:`!-n` is also given.) Writing the changes back is |
| 43 | enabled with the :option:`!-w` flag: |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 44 | |
| 45 | .. code-block:: shell-session |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 46 | |
| 47 | $ 2to3 -w example.py |
| 48 | |
Benjamin Peterson | ae5360b | 2008-09-08 23:05:23 +0000 | [diff] [blame] | 49 | After transformation, :file:`example.py` looks like this:: |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 50 | |
| 51 | def greet(name): |
| 52 | print("Hello, {0}!".format(name)) |
| 53 | print("What's your name?") |
| 54 | name = input() |
| 55 | greet(name) |
| 56 | |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 57 | Comments and exact indentation are preserved throughout the translation process. |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 58 | |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 59 | By default, 2to3 runs a set of :ref:`predefined fixers <2to3-fixers>`. The |
Martin Panter | 254da19 | 2016-07-26 06:46:06 +0000 | [diff] [blame] | 60 | :option:`!-l` flag lists all available fixers. An explicit set of fixers to run |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 61 | can be given with :option:`!-f`. Likewise the :option:`!-x` explicitly disables a |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 62 | fixer. The following example runs only the ``imports`` and ``has_key`` fixers: |
| 63 | |
| 64 | .. code-block:: shell-session |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 65 | |
| 66 | $ 2to3 -f imports -f has_key example.py |
| 67 | |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 68 | This command runs every fixer except the ``apply`` fixer: |
| 69 | |
| 70 | .. code-block:: shell-session |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 71 | |
| 72 | $ 2to3 -x apply example.py |
| 73 | |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 74 | Some fixers are *explicit*, meaning they aren't run by default and must be |
Benjamin Peterson | ae5360b | 2008-09-08 23:05:23 +0000 | [diff] [blame] | 75 | listed on the command line to be run. Here, in addition to the default fixers, |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 76 | the ``idioms`` fixer is run: |
| 77 | |
| 78 | .. code-block:: shell-session |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 79 | |
| 80 | $ 2to3 -f all -f idioms example.py |
| 81 | |
Benjamin Peterson | ae5360b | 2008-09-08 23:05:23 +0000 | [diff] [blame] | 82 | Notice how passing ``all`` enables all default fixers. |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 83 | |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 84 | Sometimes 2to3 will find a place in your source code that needs to be changed, |
| 85 | but 2to3 cannot fix automatically. In this case, 2to3 will print a warning |
| 86 | beneath the diff for a file. You should address the warning in order to have |
| 87 | compliant 3.x code. |
Benjamin Peterson | ae5360b | 2008-09-08 23:05:23 +0000 | [diff] [blame] | 88 | |
Martin Panter | 00ccacc | 2016-04-16 04:59:38 +0000 | [diff] [blame] | 89 | 2to3 can also refactor doctests. To enable this mode, use the :option:`!-d` |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 90 | flag. Note that *only* doctests will be refactored. This also doesn't require |
| 91 | the module to be valid Python. For example, doctest like examples in a reST |
| 92 | document could also be refactored with this option. |
Benjamin Peterson | ae5360b | 2008-09-08 23:05:23 +0000 | [diff] [blame] | 93 | |
Martin Panter | 00ccacc | 2016-04-16 04:59:38 +0000 | [diff] [blame] | 94 | The :option:`!-v` option enables output of more information on the translation |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 95 | process. |
Benjamin Peterson | ae5360b | 2008-09-08 23:05:23 +0000 | [diff] [blame] | 96 | |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 97 | Since some print statements can be parsed as function calls or statements, 2to3 |
| 98 | cannot always read files containing the print function. When 2to3 detects the |
| 99 | presence of the ``from __future__ import print_function`` compiler directive, it |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 100 | modifies its internal grammar to interpret :func:`print` as a function. This |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 101 | change can also be enabled manually with the :option:`!-p` flag. Use |
| 102 | :option:`!-p` to run fixers on code that already has had its print statements |
Batuhan Taşkaya | 61b1415 | 2020-01-13 01:13:31 +0300 | [diff] [blame] | 103 | converted. Also :option:`!-e` can be used to make :func:`exec` a function. |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 104 | |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 105 | The :option:`!-o` or :option:`!--output-dir` option allows specification of an |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 106 | alternate directory for processed output files to be written to. The |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 107 | :option:`!-n` flag is required when using this as backup files do not make sense |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 108 | when not overwriting the input files. |
| 109 | |
| 110 | .. versionadded:: 3.2.3 |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 111 | The :option:`!-o` option was added. |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 112 | |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 113 | The :option:`!-W` or :option:`!--write-unchanged-files` flag tells 2to3 to always |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 114 | write output files even if no changes were required to the file. This is most |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 115 | useful with :option:`!-o` so that an entire Python source tree is copied with |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 116 | translation from one directory to another. |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 117 | This option implies the :option:`!-w` flag as it would not make sense otherwise. |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 118 | |
| 119 | .. versionadded:: 3.2.3 |
Martin Panter | 00ccacc | 2016-04-16 04:59:38 +0000 | [diff] [blame] | 120 | The :option:`!-W` flag was added. |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 121 | |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 122 | The :option:`!--add-suffix` option specifies a string to append to all output |
| 123 | filenames. The :option:`!-n` flag is required when specifying this as backups |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 124 | are not necessary when writing to different filenames. Example: |
| 125 | |
| 126 | .. code-block:: shell-session |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 127 | |
| 128 | $ 2to3 -n -W --add-suffix=3 example.py |
| 129 | |
| 130 | Will cause a converted file named ``example.py3`` to be written. |
| 131 | |
| 132 | .. versionadded:: 3.2.3 |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 133 | The :option:`!--add-suffix` option was added. |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 134 | |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 135 | To translate an entire project from one directory tree to another use: |
| 136 | |
| 137 | .. code-block:: shell-session |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 138 | |
| 139 | $ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode |
| 140 | |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 141 | |
| 142 | .. _2to3-fixers: |
| 143 | |
| 144 | Fixers |
| 145 | ------ |
| 146 | |
Georg Brandl | ae2dbe2 | 2009-03-13 19:04:40 +0000 | [diff] [blame] | 147 | Each step of transforming code is encapsulated in a fixer. The command ``2to3 |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 148 | -l`` lists them. As :ref:`documented above <2to3-using>`, each can be turned on |
| 149 | and off individually. They are described here in more detail. |
| 150 | |
| 151 | |
| 152 | .. 2to3fixer:: apply |
| 153 | |
| 154 | Removes usage of :func:`apply`. For example ``apply(function, *args, |
| 155 | **kwargs)`` is converted to ``function(*args, **kwargs)``. |
| 156 | |
Ezio Melotti | 6bdd986 | 2013-11-23 21:14:42 +0200 | [diff] [blame] | 157 | .. 2to3fixer:: asserts |
| 158 | |
| 159 | Replaces deprecated :mod:`unittest` method names with the correct ones. |
| 160 | |
| 161 | ================================ ========================================== |
| 162 | From To |
| 163 | ================================ ========================================== |
| 164 | ``failUnlessEqual(a, b)`` :meth:`assertEqual(a, b) |
| 165 | <unittest.TestCase.assertEqual>` |
| 166 | ``assertEquals(a, b)`` :meth:`assertEqual(a, b) |
| 167 | <unittest.TestCase.assertEqual>` |
| 168 | ``failIfEqual(a, b)`` :meth:`assertNotEqual(a, b) |
| 169 | <unittest.TestCase.assertNotEqual>` |
| 170 | ``assertNotEquals(a, b)`` :meth:`assertNotEqual(a, b) |
| 171 | <unittest.TestCase.assertNotEqual>` |
| 172 | ``failUnless(a)`` :meth:`assertTrue(a) |
| 173 | <unittest.TestCase.assertTrue>` |
| 174 | ``assert_(a)`` :meth:`assertTrue(a) |
| 175 | <unittest.TestCase.assertTrue>` |
| 176 | ``failIf(a)`` :meth:`assertFalse(a) |
| 177 | <unittest.TestCase.assertFalse>` |
| 178 | ``failUnlessRaises(exc, cal)`` :meth:`assertRaises(exc, cal) |
| 179 | <unittest.TestCase.assertRaises>` |
| 180 | ``failUnlessAlmostEqual(a, b)`` :meth:`assertAlmostEqual(a, b) |
| 181 | <unittest.TestCase.assertAlmostEqual>` |
| 182 | ``assertAlmostEquals(a, b)`` :meth:`assertAlmostEqual(a, b) |
| 183 | <unittest.TestCase.assertAlmostEqual>` |
| 184 | ``failIfAlmostEqual(a, b)`` :meth:`assertNotAlmostEqual(a, b) |
| 185 | <unittest.TestCase.assertNotAlmostEqual>` |
| 186 | ``assertNotAlmostEquals(a, b)`` :meth:`assertNotAlmostEqual(a, b) |
| 187 | <unittest.TestCase.assertNotAlmostEqual>` |
| 188 | ================================ ========================================== |
| 189 | |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 190 | .. 2to3fixer:: basestring |
| 191 | |
| 192 | Converts :class:`basestring` to :class:`str`. |
| 193 | |
| 194 | .. 2to3fixer:: buffer |
| 195 | |
| 196 | Converts :class:`buffer` to :class:`memoryview`. This fixer is optional |
| 197 | because the :class:`memoryview` API is similar but not exactly the same as |
| 198 | that of :class:`buffer`. |
| 199 | |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 200 | .. 2to3fixer:: dict |
| 201 | |
| 202 | Fixes dictionary iteration methods. :meth:`dict.iteritems` is converted to |
| 203 | :meth:`dict.items`, :meth:`dict.iterkeys` to :meth:`dict.keys`, and |
Alexandre Vassalotti | a515bab | 2010-01-12 18:38:14 +0000 | [diff] [blame] | 204 | :meth:`dict.itervalues` to :meth:`dict.values`. Similarly, |
Benjamin Peterson | e90f54e | 2010-03-21 23:17:57 +0000 | [diff] [blame] | 205 | :meth:`dict.viewitems`, :meth:`dict.viewkeys` and :meth:`dict.viewvalues` are |
| 206 | converted respectively to :meth:`dict.items`, :meth:`dict.keys` and |
Alexandre Vassalotti | a515bab | 2010-01-12 18:38:14 +0000 | [diff] [blame] | 207 | :meth:`dict.values`. It also wraps existing usages of :meth:`dict.items`, |
| 208 | :meth:`dict.keys`, and :meth:`dict.values` in a call to :class:`list`. |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 209 | |
| 210 | .. 2to3fixer:: except |
| 211 | |
| 212 | Converts ``except X, T`` to ``except X as T``. |
| 213 | |
| 214 | .. 2to3fixer:: exec |
| 215 | |
Georg Brandl | 375aec2 | 2011-01-15 17:03:02 +0000 | [diff] [blame] | 216 | Converts the ``exec`` statement to the :func:`exec` function. |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 217 | |
| 218 | .. 2to3fixer:: execfile |
| 219 | |
| 220 | Removes usage of :func:`execfile`. The argument to :func:`execfile` is |
| 221 | wrapped in calls to :func:`open`, :func:`compile`, and :func:`exec`. |
| 222 | |
Benjamin Peterson | e90f54e | 2010-03-21 23:17:57 +0000 | [diff] [blame] | 223 | .. 2to3fixer:: exitfunc |
| 224 | |
| 225 | Changes assignment of :attr:`sys.exitfunc` to use of the :mod:`atexit` |
| 226 | module. |
| 227 | |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 228 | .. 2to3fixer:: filter |
| 229 | |
| 230 | Wraps :func:`filter` usage in a :class:`list` call. |
| 231 | |
| 232 | .. 2to3fixer:: funcattrs |
| 233 | |
| 234 | Fixes function attributes that have been renamed. For example, |
| 235 | ``my_function.func_closure`` is converted to ``my_function.__closure__``. |
| 236 | |
| 237 | .. 2to3fixer:: future |
| 238 | |
| 239 | Removes ``from __future__ import new_feature`` statements. |
| 240 | |
| 241 | .. 2to3fixer:: getcwdu |
| 242 | |
| 243 | Renames :func:`os.getcwdu` to :func:`os.getcwd`. |
| 244 | |
| 245 | .. 2to3fixer:: has_key |
| 246 | |
| 247 | Changes ``dict.has_key(key)`` to ``key in dict``. |
| 248 | |
| 249 | .. 2to3fixer:: idioms |
| 250 | |
Georg Brandl | ae2dbe2 | 2009-03-13 19:04:40 +0000 | [diff] [blame] | 251 | This optional fixer performs several transformations that make Python code |
| 252 | more idiomatic. Type comparisons like ``type(x) is SomeClass`` and |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 253 | ``type(x) == SomeClass`` are converted to ``isinstance(x, SomeClass)``. |
| 254 | ``while 1`` becomes ``while True``. This fixer also tries to make use of |
Georg Brandl | ae2dbe2 | 2009-03-13 19:04:40 +0000 | [diff] [blame] | 255 | :func:`sorted` in appropriate places. For example, this block :: |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 256 | |
| 257 | L = list(some_iterable) |
| 258 | L.sort() |
| 259 | |
| 260 | is changed to :: |
| 261 | |
| 262 | L = sorted(some_iterable) |
| 263 | |
| 264 | .. 2to3fixer:: import |
| 265 | |
| 266 | Detects sibling imports and converts them to relative imports. |
| 267 | |
| 268 | .. 2to3fixer:: imports |
| 269 | |
| 270 | Handles module renames in the standard library. |
| 271 | |
| 272 | .. 2to3fixer:: imports2 |
| 273 | |
| 274 | Handles other modules renames in the standard library. It is separate from |
| 275 | the :2to3fixer:`imports` fixer only because of technical limitations. |
| 276 | |
| 277 | .. 2to3fixer:: input |
| 278 | |
Martin Panter | d21e0b5 | 2015-10-10 10:36:22 +0000 | [diff] [blame] | 279 | Converts ``input(prompt)`` to ``eval(input(prompt))``. |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 280 | |
| 281 | .. 2to3fixer:: intern |
| 282 | |
| 283 | Converts :func:`intern` to :func:`sys.intern`. |
| 284 | |
| 285 | .. 2to3fixer:: isinstance |
| 286 | |
| 287 | Fixes duplicate types in the second argument of :func:`isinstance`. For |
| 288 | example, ``isinstance(x, (int, int))`` is converted to ``isinstance(x, |
Eli Boyarski | 26248ef | 2017-07-24 03:39:07 +0300 | [diff] [blame] | 289 | int)`` and ``isinstance(x, (int, float, int))`` is converted to |
| 290 | ``isinstance(x, (int, float))``. |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 291 | |
| 292 | .. 2to3fixer:: itertools_imports |
| 293 | |
| 294 | Removes imports of :func:`itertools.ifilter`, :func:`itertools.izip`, and |
| 295 | :func:`itertools.imap`. Imports of :func:`itertools.ifilterfalse` are also |
| 296 | changed to :func:`itertools.filterfalse`. |
| 297 | |
| 298 | .. 2to3fixer:: itertools |
| 299 | |
| 300 | Changes usage of :func:`itertools.ifilter`, :func:`itertools.izip`, and |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 301 | :func:`itertools.imap` to their built-in equivalents. |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 302 | :func:`itertools.ifilterfalse` is changed to :func:`itertools.filterfalse`. |
| 303 | |
| 304 | .. 2to3fixer:: long |
| 305 | |
Benjamin Peterson | 4dafd40 | 2013-01-28 18:28:38 -0500 | [diff] [blame] | 306 | Renames :class:`long` to :class:`int`. |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 307 | |
| 308 | .. 2to3fixer:: map |
| 309 | |
| 310 | Wraps :func:`map` in a :class:`list` call. It also changes ``map(None, x)`` |
| 311 | to ``list(x)``. Using ``from future_builtins import map`` disables this |
| 312 | fixer. |
| 313 | |
| 314 | .. 2to3fixer:: metaclass |
| 315 | |
| 316 | Converts the old metaclass syntax (``__metaclass__ = Meta`` in the class |
| 317 | body) to the new (``class X(metaclass=Meta)``). |
| 318 | |
| 319 | .. 2to3fixer:: methodattrs |
| 320 | |
| 321 | Fixes old method attribute names. For example, ``meth.im_func`` is converted |
| 322 | to ``meth.__func__``. |
| 323 | |
| 324 | .. 2to3fixer:: ne |
| 325 | |
| 326 | Converts the old not-equal syntax, ``<>``, to ``!=``. |
| 327 | |
| 328 | .. 2to3fixer:: next |
| 329 | |
Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 330 | Converts the use of iterator's :meth:`~iterator.next` methods to the |
| 331 | :func:`next` function. It also renames :meth:`next` methods to |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 332 | :meth:`~iterator.__next__`. |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 333 | |
| 334 | .. 2to3fixer:: nonzero |
| 335 | |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 336 | Renames :meth:`__nonzero__` to :meth:`~object.__bool__`. |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 337 | |
| 338 | .. 2to3fixer:: numliterals |
| 339 | |
| 340 | Converts octal literals into the new syntax. |
| 341 | |
Alexandre Vassalotti | ae78018 | 2010-08-05 07:12:18 +0000 | [diff] [blame] | 342 | .. 2to3fixer:: operator |
| 343 | |
| 344 | Converts calls to various functions in the :mod:`operator` module to other, |
| 345 | but equivalent, function calls. When needed, the appropriate ``import`` |
Serhiy Storchaka | 0a2abdf | 2017-11-16 09:16:24 +0200 | [diff] [blame] | 346 | statements are added, e.g. ``import collections.abc``. The following mapping |
Alexandre Vassalotti | ae78018 | 2010-08-05 07:12:18 +0000 | [diff] [blame] | 347 | are made: |
| 348 | |
Serhiy Storchaka | 0a2abdf | 2017-11-16 09:16:24 +0200 | [diff] [blame] | 349 | ================================== ============================================= |
Alexandre Vassalotti | ae78018 | 2010-08-05 07:12:18 +0000 | [diff] [blame] | 350 | From To |
Serhiy Storchaka | 0a2abdf | 2017-11-16 09:16:24 +0200 | [diff] [blame] | 351 | ================================== ============================================= |
Dong-hee Na | a489599 | 2017-11-29 01:26:56 +0900 | [diff] [blame] | 352 | ``operator.isCallable(obj)`` ``callable(obj)`` |
Alexandre Vassalotti | ae78018 | 2010-08-05 07:12:18 +0000 | [diff] [blame] | 353 | ``operator.sequenceIncludes(obj)`` ``operator.contains(obj)`` |
Serhiy Storchaka | 0a2abdf | 2017-11-16 09:16:24 +0200 | [diff] [blame] | 354 | ``operator.isSequenceType(obj)`` ``isinstance(obj, collections.abc.Sequence)`` |
| 355 | ``operator.isMappingType(obj)`` ``isinstance(obj, collections.abc.Mapping)`` |
Alexandre Vassalotti | ae78018 | 2010-08-05 07:12:18 +0000 | [diff] [blame] | 356 | ``operator.isNumberType(obj)`` ``isinstance(obj, numbers.Number)`` |
| 357 | ``operator.repeat(obj, n)`` ``operator.mul(obj, n)`` |
| 358 | ``operator.irepeat(obj, n)`` ``operator.imul(obj, n)`` |
Serhiy Storchaka | 0a2abdf | 2017-11-16 09:16:24 +0200 | [diff] [blame] | 359 | ================================== ============================================= |
Alexandre Vassalotti | ae78018 | 2010-08-05 07:12:18 +0000 | [diff] [blame] | 360 | |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 361 | .. 2to3fixer:: paren |
| 362 | |
| 363 | Add extra parenthesis where they are required in list comprehensions. For |
| 364 | example, ``[x for x in 1, 2]`` becomes ``[x for x in (1, 2)]``. |
| 365 | |
| 366 | .. 2to3fixer:: print |
| 367 | |
Georg Brandl | 375aec2 | 2011-01-15 17:03:02 +0000 | [diff] [blame] | 368 | Converts the ``print`` statement to the :func:`print` function. |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 369 | |
Benjamin Peterson | b51b5c4 | 2010-07-01 17:49:01 +0000 | [diff] [blame] | 370 | .. 2to3fixer:: raise |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 371 | |
| 372 | Converts ``raise E, V`` to ``raise E(V)``, and ``raise E, V, T`` to ``raise |
| 373 | E(V).with_traceback(T)``. If ``E`` is a tuple, the translation will be |
| 374 | incorrect because substituting tuples for exceptions has been removed in 3.0. |
| 375 | |
| 376 | .. 2to3fixer:: raw_input |
| 377 | |
| 378 | Converts :func:`raw_input` to :func:`input`. |
| 379 | |
| 380 | .. 2to3fixer:: reduce |
| 381 | |
| 382 | Handles the move of :func:`reduce` to :func:`functools.reduce`. |
| 383 | |
Benjamin Peterson | 448e81b | 2012-12-07 22:44:10 -0500 | [diff] [blame] | 384 | .. 2to3fixer:: reload |
| 385 | |
Berker Peksag | 7a3056f | 2018-07-23 09:49:08 +0300 | [diff] [blame] | 386 | Converts :func:`reload` to :func:`importlib.reload`. |
Benjamin Peterson | 448e81b | 2012-12-07 22:44:10 -0500 | [diff] [blame] | 387 | |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 388 | .. 2to3fixer:: renames |
| 389 | |
| 390 | Changes :data:`sys.maxint` to :data:`sys.maxsize`. |
| 391 | |
| 392 | .. 2to3fixer:: repr |
| 393 | |
| 394 | Replaces backtick repr with the :func:`repr` function. |
| 395 | |
| 396 | .. 2to3fixer:: set_literal |
| 397 | |
| 398 | Replaces use of the :class:`set` constructor with set literals. This fixer |
| 399 | is optional. |
| 400 | |
Benjamin Peterson | a819577 | 2014-05-31 13:16:49 -0700 | [diff] [blame] | 401 | .. 2to3fixer:: standarderror |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 402 | |
| 403 | Renames :exc:`StandardError` to :exc:`Exception`. |
| 404 | |
| 405 | .. 2to3fixer:: sys_exc |
| 406 | |
| 407 | Changes the deprecated :data:`sys.exc_value`, :data:`sys.exc_type`, |
| 408 | :data:`sys.exc_traceback` to use :func:`sys.exc_info`. |
| 409 | |
| 410 | .. 2to3fixer:: throw |
| 411 | |
| 412 | Fixes the API change in generator's :meth:`throw` method. |
| 413 | |
| 414 | .. 2to3fixer:: tuple_params |
| 415 | |
| 416 | Removes implicit tuple parameter unpacking. This fixer inserts temporary |
| 417 | variables. |
| 418 | |
| 419 | .. 2to3fixer:: types |
| 420 | |
| 421 | Fixes code broken from the removal of some members in the :mod:`types` |
| 422 | module. |
| 423 | |
| 424 | .. 2to3fixer:: unicode |
| 425 | |
| 426 | Renames :class:`unicode` to :class:`str`. |
| 427 | |
| 428 | .. 2to3fixer:: urllib |
| 429 | |
| 430 | Handles the rename of :mod:`urllib` and :mod:`urllib2` to the :mod:`urllib` |
| 431 | package. |
| 432 | |
| 433 | .. 2to3fixer:: ws_comma |
| 434 | |
| 435 | Removes excess whitespace from comma separated items. This fixer is |
| 436 | optional. |
| 437 | |
| 438 | .. 2to3fixer:: xrange |
| 439 | |
| 440 | Renames :func:`xrange` to :func:`range` and wraps existing :func:`range` |
| 441 | calls with :class:`list`. |
| 442 | |
| 443 | .. 2to3fixer:: xreadlines |
| 444 | |
| 445 | Changes ``for x in file.xreadlines()`` to ``for x in file``. |
| 446 | |
| 447 | .. 2to3fixer:: zip |
| 448 | |
| 449 | Wraps :func:`zip` usage in a :class:`list` call. This is disabled when |
| 450 | ``from future_builtins import zip`` appears. |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 451 | |
| 452 | |
| 453 | :mod:`lib2to3` - 2to3's library |
| 454 | ------------------------------- |
| 455 | |
| 456 | .. module:: lib2to3 |
Sanchit Khurana | f8a6316 | 2019-11-26 03:47:59 +0530 | [diff] [blame] | 457 | :synopsis: The 2to3 library |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 458 | |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 459 | .. moduleauthor:: Guido van Rossum |
| 460 | .. moduleauthor:: Collin Winter |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 461 | .. moduleauthor:: Benjamin Peterson <benjamin@python.org> |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 462 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 463 | **Source code:** :source:`Lib/lib2to3/` |
| 464 | |
| 465 | -------------- |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 466 | |
Carl Meyer | 503de71 | 2020-04-24 12:19:46 -0600 | [diff] [blame] | 467 | .. deprecated:: 3.10 |
| 468 | Python 3.9 will switch to a PEG parser (see :pep:`617`), and Python 3.10 may |
| 469 | include new language syntax that is not parsable by lib2to3's LL(1) parser. |
| 470 | The ``lib2to3`` module may be removed from the standard library in a future |
| 471 | Python version. Consider third-party alternatives such as `LibCST`_ or |
| 472 | `parso`_. |
| 473 | |
Georg Brandl | e720c0a | 2009-04-27 16:20:50 +0000 | [diff] [blame] | 474 | .. note:: |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 475 | |
| 476 | The :mod:`lib2to3` API should be considered unstable and may change |
| 477 | drastically in the future. |
| 478 | |
Carl Meyer | 503de71 | 2020-04-24 12:19:46 -0600 | [diff] [blame] | 479 | .. _LibCST: https://libcst.readthedocs.io/ |
| 480 | .. _parso: https://parso.readthedocs.io/ |