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 | |
| 6 | .. sectionauthor:: Benjamin Peterson |
| 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 | |
| 17 | Using 2to3 |
| 18 | ---------- |
| 19 | |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +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 |
| 24 | directories are to recursively traversed for Python sources. |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 25 | |
| 26 | Here is a sample Python 2.x source file, :file:`example.py`:: |
| 27 | |
| 28 | def greet(name): |
Georg Brandl | 340739e | 2008-07-24 07:09:21 +0000 | [diff] [blame] | 29 | print "Hello, {0}!".format(name) |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 30 | print "What's your name?" |
| 31 | name = raw_input() |
| 32 | greet(name) |
| 33 | |
| 34 | It can be converted to Python 3.x code via 2to3 on the command line:: |
| 35 | |
| 36 | $ 2to3 example.py |
| 37 | |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 38 | A diff against the original source file is printed. 2to3 can also write the |
| 39 | needed modifications right back to the source file. (Of course, a backup of the |
Benjamin Peterson | f3d0ce1 | 2008-10-19 19:39:16 +0000 | [diff] [blame] | 40 | original is also be made unless :option:`-n` is also given.) Writing the |
| 41 | changes back is enabled with the :option:`-w` flag:: |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 42 | |
| 43 | $ 2to3 -w example.py |
| 44 | |
Benjamin Peterson | ad2a9e7 | 2008-09-06 03:00:00 +0000 | [diff] [blame] | 45 | After transformation, :file:`example.py` looks like this:: |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 46 | |
| 47 | def greet(name): |
Benjamin Peterson | 3ac2f24 | 2008-07-25 21:59:53 +0000 | [diff] [blame] | 48 | print("Hello, {0}!".format(name)) |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 49 | print("What's your name?") |
| 50 | name = input() |
| 51 | greet(name) |
| 52 | |
Benjamin Peterson | cd29e9d | 2008-10-22 21:05:30 +0000 | [diff] [blame] | 53 | Comments and exact indentation are preserved throughout the translation process. |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 54 | |
Benjamin Peterson | 0ecbcca | 2008-10-13 21:51:40 +0000 | [diff] [blame] | 55 | By default, 2to3 runs a set of predefined fixers. The :option:`-l` flag lists |
Benjamin Peterson | 92be539 | 2008-10-22 20:57:43 +0000 | [diff] [blame] | 56 | all available fixers. An explicit set of fixers to run can be given with |
Benjamin Peterson | 0ecbcca | 2008-10-13 21:51:40 +0000 | [diff] [blame] | 57 | :option:`-f`. Likewise the :option:`-x` explicitly disables a fixer. The |
| 58 | following example runs only the ``imports`` and ``has_key`` fixers:: |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 59 | |
| 60 | $ 2to3 -f imports -f has_key example.py |
| 61 | |
Benjamin Peterson | 0ecbcca | 2008-10-13 21:51:40 +0000 | [diff] [blame] | 62 | This command runs every fixer except the ``apply`` fixer:: |
| 63 | |
| 64 | $ 2to3 -x apply example.py |
| 65 | |
Benjamin Peterson | 92be539 | 2008-10-22 20:57:43 +0000 | [diff] [blame] | 66 | 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] | 67 | listed on the command line to be run. Here, in addition to the default fixers, |
| 68 | the ``idioms`` fixer is run:: |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 69 | |
| 70 | $ 2to3 -f all -f idioms example.py |
| 71 | |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 72 | Notice how passing ``all`` enables all default fixers. |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 73 | |
Benjamin Peterson | 92be539 | 2008-10-22 20:57:43 +0000 | [diff] [blame] | 74 | Sometimes 2to3 will find a place in your source code that needs to be changed, |
| 75 | but 2to3 cannot fix automatically. In this case, 2to3 will print a warning |
| 76 | beneath the diff for a file. You should address the warning in order to have |
| 77 | compliant 3.x code. |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 78 | |
| 79 | 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] | 80 | flag. Note that *only* doctests will be refactored. This also doesn't require |
| 81 | the module to be valid Python. For example, doctest like examples in a reST |
| 82 | document could also be refactored with this option. |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 83 | |
Benjamin Peterson | 0ecbcca | 2008-10-13 21:51:40 +0000 | [diff] [blame] | 84 | The :option:`-v` option enables output of more information on the translation |
| 85 | process. |
Benjamin Peterson | 15ad6c0 | 2008-09-04 23:31:27 +0000 | [diff] [blame] | 86 | |
Benjamin Peterson | 6be425c | 2008-09-16 21:20:28 +0000 | [diff] [blame] | 87 | When the :option:`-p` is passed, 2to3 treats ``print`` as a function instead of |
| 88 | a statement. This is useful when ``from __future__ import print_function`` is |
| 89 | being used. If this option is not given, the print fixer will surround print |
| 90 | calls in an extra set of parentheses because it cannot differentiate between the |
Benjamin Peterson | 92be539 | 2008-10-22 20:57:43 +0000 | [diff] [blame] | 91 | print statement with parentheses (such as ``print ("a" + "b" + "c")``) and a |
Benjamin Peterson | 6be425c | 2008-09-16 21:20:28 +0000 | [diff] [blame] | 92 | true function call. |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 93 | |
| 94 | |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 95 | :mod:`lib2to3` - 2to3's library |
| 96 | ------------------------------- |
| 97 | |
| 98 | .. module:: lib2to3 |
| 99 | :synopsis: the 2to3 library |
| 100 | .. moduleauthor:: Guido van Rossum |
| 101 | .. moduleauthor:: Collin Winter |
| 102 | |
Benjamin Peterson | 7f8f660 | 2008-09-27 16:23:55 +0000 | [diff] [blame] | 103 | |
| 104 | .. warning:: |
| 105 | |
| 106 | The :mod:`lib2to3` API should be considered unstable and may change |
| 107 | drastically in the future. |
| 108 | |
Benjamin Peterson | 4020221 | 2008-07-24 02:45:37 +0000 | [diff] [blame] | 109 | .. XXX What is the public interface anyway? |