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