~pythonregexp2.7/python/issue2636-01+09-01-01

« back to all changes in this revision

Viewing changes to Doc/library/2to3.rst

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 00:02:12 UTC
  • mfrom: (39022.1.34 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922000212-7r0q4f4ugiq57jph
Merged in changes from the Atomic Grouping / Possessive Qualifiers branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.. _2to3-reference:
 
2
 
 
3
2to3 - Automated Python 2 to 3 code translation
 
4
===============================================
 
5
 
 
6
.. sectionauthor:: Benjamin Peterson
 
7
 
 
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
 
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.
 
15
 
 
16
 
 
17
Using 2to3
 
18
----------
 
19
 
 
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.
 
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
 
 
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::
 
42
 
 
43
   $ 2to3 -w example.py
 
44
 
 
45
After transformation, :file:`example.py` looks like this::
 
46
 
 
47
   def greet(name):
 
48
       print("Hello, {0}!".format(name))
 
49
   print("What's your name?")
 
50
   name = input()
 
51
   greet(name)
 
52
 
 
53
Comments and and exact indentation are preserved throughout the translation
 
54
process.
 
55
 
 
56
By default, 2to3 runs a set of predefined fixers.  The :option:`-l` flag
 
57
lists all avaible fixers.  An explicit set of fixers to run can be given by use
 
58
of the :option:`-f` flag.  The following example runs only the ``imports`` and
 
59
``has_key`` fixers::
 
60
 
 
61
   $ 2to3 -f imports -f has_key example.py
 
62
 
 
63
Some fixers are *explicit*, meaning they aren't run be default and must be
 
64
listed on the command line to be run.  Here, in addition to the default fixers,
 
65
the ``idioms`` fixer is run::
 
66
 
 
67
   $ 2to3 -f all -f idioms example.py
 
68
 
 
69
Notice how passing ``all`` enables all default fixers.
 
70
 
 
71
Sometimes 2to3 will find will find a place in your source code that needs to be
 
72
changed, but 2to3 cannot fix automatically.  In this case, 2to3 will print a
 
73
warning beneath the diff for a file.  You should address the warning in order to
 
74
have compliant 3.x code.
 
75
 
 
76
2to3 can also refactor doctests.  To enable this mode, use the :option:`-d`
 
77
flag.  Note that *only* doctests will be refactored.
 
78
 
 
79
The :option:`-v` option enables the output of more information on the
 
80
translation process.
 
81
 
 
82
When the :option:`-p` is passed, 2to3 treats ``print`` as a function instead of
 
83
a statement.  This is useful when ``from __future__ import print_function`` is
 
84
being used.  If this option is not given, the print fixer will surround print
 
85
calls in an extra set of parentheses because it cannot differentiate between the
 
86
and print statement with parentheses (such as ``print ("a" + "b" + "c")``) and a
 
87
true function call.
 
88
 
 
89
 
 
90
:mod:`lib2to3` - 2to3's library
 
91
-------------------------------
 
92
 
 
93
.. module:: lib2to3
 
94
   :synopsis: the 2to3 library
 
95
.. moduleauthor:: Guido van Rossum
 
96
.. moduleauthor:: Collin Winter
 
97
 
 
98
.. XXX What is the public interface anyway?