/Doc/library/2to3.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 102 lines · 69 code · 33 blank · 0 comment · 0 complexity · edacd78bb7fcffc21ce592855fcf8512 MD5 · raw file

  1. .. _2to3-reference:
  2. 2to3 - Automated Python 2 to 3 code translation
  3. ===============================================
  4. .. sectionauthor:: Benjamin Peterson <benjamin@python.org>
  5. 2to3 is a Python program that reads Python 2.x source code and applies a series
  6. of *fixers* to transform it into valid Python 3.x code. The standard library
  7. contains a rich set of fixers that will handle almost all code. 2to3 supporting
  8. library :mod:`lib2to3` is, however, a flexible and generic library, so it is
  9. possible to write your own fixers for 2to3. :mod:`lib2to3` could also be
  10. adapted to custom applications in which Python code needs to be edited
  11. automatically.
  12. Using 2to3
  13. ----------
  14. 2to3 will usually be installed with the Python interpreter as a script. It is
  15. also located in the :file:`Tools/scripts` directory of the Python root.
  16. 2to3's basic arguments are a list of files or directories to transform. The
  17. directories are to recursively traversed for Python sources.
  18. Here is a sample Python 2.x source file, :file:`example.py`::
  19. def greet(name):
  20. print "Hello, {0}!".format(name)
  21. print "What's your name?"
  22. name = raw_input()
  23. greet(name)
  24. It can be converted to Python 3.x code via 2to3 on the command line::
  25. $ 2to3 example.py
  26. A diff against the original source file is printed. 2to3 can also write the
  27. needed modifications right back to the source file. (A backup of the original
  28. file is made unless :option:`-n` is also given.) Writing the changes back is
  29. enabled with the :option:`-w` flag::
  30. $ 2to3 -w example.py
  31. After transformation, :file:`example.py` looks like this::
  32. def greet(name):
  33. print("Hello, {0}!".format(name))
  34. print("What's your name?")
  35. name = input()
  36. greet(name)
  37. Comments and exact indentation are preserved throughout the translation process.
  38. By default, 2to3 runs a set of predefined fixers. The :option:`-l` flag lists
  39. all available fixers. An explicit set of fixers to run can be given with
  40. :option:`-f`. Likewise the :option:`-x` explicitly disables a fixer. The
  41. following example runs only the ``imports`` and ``has_key`` fixers::
  42. $ 2to3 -f imports -f has_key example.py
  43. This command runs every fixer except the ``apply`` fixer::
  44. $ 2to3 -x apply example.py
  45. Some fixers are *explicit*, meaning they aren't run by default and must be
  46. listed on the command line to be run. Here, in addition to the default fixers,
  47. the ``idioms`` fixer is run::
  48. $ 2to3 -f all -f idioms example.py
  49. Notice how passing ``all`` enables all default fixers.
  50. Sometimes 2to3 will find a place in your source code that needs to be changed,
  51. but 2to3 cannot fix automatically. In this case, 2to3 will print a warning
  52. beneath the diff for a file. You should address the warning in order to have
  53. compliant 3.x code.
  54. 2to3 can also refactor doctests. To enable this mode, use the :option:`-d`
  55. flag. Note that *only* doctests will be refactored. This also doesn't require
  56. the module to be valid Python. For example, doctest like examples in a reST
  57. document could also be refactored with this option.
  58. The :option:`-v` option enables output of more information on the translation
  59. process.
  60. :mod:`lib2to3` - 2to3's library
  61. -------------------------------
  62. .. module:: lib2to3
  63. :synopsis: the 2to3 library
  64. .. moduleauthor:: Guido van Rossum
  65. .. moduleauthor:: Collin Winter
  66. .. note::
  67. The :mod:`lib2to3` API should be considered unstable and may change
  68. drastically in the future.
  69. .. XXX What is the public interface anyway?