Changeset 391 for python/trunk/Doc/library/2to3.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/2to3.rst
r2 r391 24 24 25 25 2to3's basic arguments are a list of files or directories to transform. The 26 directories are torecursively traversed for Python sources.26 directories are recursively traversed for Python sources. 27 27 28 28 Here is a sample Python 2.x source file, :file:`example.py`:: … … 87 87 process. 88 88 89 Since some print statements can be parsed as function calls or statements, 2to3 90 cannot always read files containing the print function. When 2to3 detects the 91 presence of the ``from __future__ import print_function`` compiler directive, it 92 modifies its internal grammar to interpret :func:`print` as a function. This 93 change can also be enabled manually with the :option:`-p` flag. Use 94 :option:`-p` to run fixers on code that already has had its print statements 95 converted. 96 97 The :option:`-o` or :option:`--output-dir` option allows specification of an 98 alternate directory for processed output files to be written to. The 99 :option:`-n` flag is required when using this as backup files do not make sense 100 when not overwriting the input files. 101 102 .. versionadded:: 2.7.3 103 The :option:`-o` option was added. 104 105 The :option:`-W` or :option:`--write-unchanged-files` flag tells 2to3 to always 106 write output files even if no changes were required to the file. This is most 107 useful with :option:`-o` so that an entire Python source tree is copied with 108 translation from one directory to another. 109 This option implies the :option:`-w` flag as it would not make sense otherwise. 110 111 .. versionadded:: 2.7.3 112 The :option:`-W` flag was added. 113 114 The :option:`--add-suffix` option specifies a string to append to all output 115 filenames. The :option:`-n` flag is required when specifying this as backups 116 are not necessary when writing to different filenames. Example:: 117 118 $ 2to3 -n -W --add-suffix=3 example.py 119 120 Will cause a converted file named ``example.py3`` to be written. 121 122 .. versionadded:: 2.7.3 123 The :option:`--add-suffix` option was added. 124 125 To translate an entire project from one directory tree to another use:: 126 127 $ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode 128 89 129 90 130 .. _2to3-fixers: … … 93 133 ------ 94 134 95 Each step of tran forming code is encapsulated in a fixer. The command ``2to3135 Each step of transforming code is encapsulated in a fixer. The command ``2to3 96 136 -l`` lists them. As :ref:`documented above <2to3-using>`, each can be turned on 97 137 and off individually. They are described here in more detail. … … 115 155 .. 2to3fixer:: callable 116 156 117 Converts ``callable(x)`` to ``hasattr(x, "__call_")``. 157 Converts ``callable(x)`` to ``isinstance(x, collections.Callable)``, adding 158 an import to :mod:`collections` if needed. Note ``callable(x)`` has returned 159 in Python 3.2, so if you do not intend to support Python 3.1, you can disable 160 this fixer. 118 161 119 162 .. 2to3fixer:: dict … … 121 164 Fixes dictionary iteration methods. :meth:`dict.iteritems` is converted to 122 165 :meth:`dict.items`, :meth:`dict.iterkeys` to :meth:`dict.keys`, and 123 :meth:`dict.itervalues` to :meth:`dict.values`. It also wraps existing 124 usages of :meth:`dict.items`, :meth:`dict.keys`, and :meth:`dict.values` in a 125 call to :class:`list`. 166 :meth:`dict.itervalues` to :meth:`dict.values`. Similarly, 167 :meth:`dict.viewitems`, :meth:`dict.viewkeys` and :meth:`dict.viewvalues` are 168 converted respectively to :meth:`dict.items`, :meth:`dict.keys` and 169 :meth:`dict.values`. It also wraps existing usages of :meth:`dict.items`, 170 :meth:`dict.keys`, and :meth:`dict.values` in a call to :class:`list`. 126 171 127 172 .. 2to3fixer:: except … … 138 183 wrapped in calls to :func:`open`, :func:`compile`, and :func:`exec`. 139 184 185 .. 2to3fixer:: exitfunc 186 187 Changes assignment of :attr:`sys.exitfunc` to use of the :mod:`atexit` 188 module. 189 140 190 .. 2to3fixer:: filter 141 191 … … 161 211 .. 2to3fixer:: idioms 162 212 163 This optional fixer p reforms several transformations that make Python code164 more idiomatic. Type comparis ions like ``type(x) is SomeClass`` and213 This optional fixer performs several transformations that make Python code 214 more idiomatic. Type comparisons like ``type(x) is SomeClass`` and 165 215 ``type(x) == SomeClass`` are converted to ``isinstance(x, SomeClass)``. 166 216 ``while 1`` becomes ``while True``. This fixer also tries to make use of 167 :func:`sorted` in approp iate places. For example, this block ::217 :func:`sorted` in appropriate places. For example, this block :: 168 218 169 219 L = list(some_iterable) … … 215 265 .. 2to3fixer:: long 216 266 217 Strips the ``L`` prefix on long literals and renames :class:`long` to 218 :class:`int`. 267 Renames :class:`long` to :class:`int`. 219 268 220 269 .. 2to3fixer:: map … … 242 291 Converts the use of iterator's :meth:`~iterator.next` methods to the 243 292 :func:`next` function. It also renames :meth:`next` methods to 244 :meth:`~ object.__next__`.293 :meth:`~iterator.__next__`. 245 294 246 295 .. 2to3fixer:: nonzero 247 296 248 Renames :meth:` ~object.__nonzero__` to :meth:`~object.__bool__`.297 Renames :meth:`__nonzero__` to :meth:`~object.__bool__`. 249 298 250 299 .. 2to3fixer:: numliterals … … 261 310 Converts the :keyword:`print` statement to the :func:`print` function. 262 311 263 .. 2to3fixer:: raise s312 .. 2to3fixer:: raise 264 313 265 314 Converts ``raise E, V`` to ``raise E(V)``, and ``raise E, V, T`` to ``raise 266 315 E(V).with_traceback(T)``. If ``E`` is a tuple, the translation will be 267 incorrect because substituting tuples for exceptions has been removed in 3.0.316 incorrect because substituting tuples for exceptions has been removed in Python 3. 268 317 269 318 .. 2to3fixer:: raw_input
Note:
See TracChangeset
for help on using the changeset viewer.