Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/library/2to3.rst

    r2 r391  
    2424
    25252to3's basic arguments are a list of files or directories to transform.  The
    26 directories are to recursively traversed for Python sources.
     26directories are recursively traversed for Python sources.
    2727
    2828Here is a sample Python 2.x source file, :file:`example.py`::
     
    8787process.
    8888
     89Since some print statements can be parsed as function calls or statements, 2to3
     90cannot always read files containing the print function.  When 2to3 detects the
     91presence of the ``from __future__ import print_function`` compiler directive, it
     92modifies its internal grammar to interpret :func:`print` as a function.  This
     93change 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
     95converted.
     96
     97The :option:`-o` or :option:`--output-dir` option allows specification of an
     98alternate 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
     100when not overwriting the input files.
     101
     102.. versionadded:: 2.7.3
     103   The :option:`-o` option was added.
     104
     105The :option:`-W` or :option:`--write-unchanged-files` flag tells 2to3 to always
     106write output files even if no changes were required to the file.  This is most
     107useful with :option:`-o` so that an entire Python source tree is copied with
     108translation from one directory to another.
     109This 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
     114The :option:`--add-suffix` option specifies a string to append to all output
     115filenames.  The :option:`-n` flag is required when specifying this as backups
     116are not necessary when writing to different filenames.  Example::
     117
     118   $ 2to3 -n -W --add-suffix=3 example.py
     119
     120Will 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
     125To 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
    89129
    90130.. _2to3-fixers:
     
    93133------
    94134
    95 Each step of tranforming code is encapsulated in a fixer.  The command ``2to3
     135Each step of transforming code is encapsulated in a fixer.  The command ``2to3
    96136-l`` lists them.  As :ref:`documented above <2to3-using>`, each can be turned on
    97137and off individually.  They are described here in more detail.
     
    115155.. 2to3fixer:: callable
    116156
    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.
    118161
    119162.. 2to3fixer:: dict
     
    121164   Fixes dictionary iteration methods.  :meth:`dict.iteritems` is converted to
    122165   :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`.
    126171
    127172.. 2to3fixer:: except
     
    138183   wrapped in calls to :func:`open`, :func:`compile`, and :func:`exec`.
    139184
     185.. 2to3fixer:: exitfunc
     186
     187   Changes assignment of :attr:`sys.exitfunc` to use of the :mod:`atexit`
     188   module.
     189
    140190.. 2to3fixer:: filter
    141191
     
    161211.. 2to3fixer:: idioms
    162212
    163    This optional fixer preforms several transformations that make Python code
    164    more idiomatic.  Type comparisions like ``type(x) is SomeClass`` and
     213   This optional fixer performs several transformations that make Python code
     214   more idiomatic.  Type comparisons like ``type(x) is SomeClass`` and
    165215   ``type(x) == SomeClass`` are converted to ``isinstance(x, SomeClass)``.
    166216   ``while 1`` becomes ``while True``.  This fixer also tries to make use of
    167    :func:`sorted` in appropiate places.  For example, this block ::
     217   :func:`sorted` in appropriate places.  For example, this block ::
    168218
    169219       L = list(some_iterable)
     
    215265.. 2to3fixer:: long
    216266
    217    Strips the ``L`` prefix on long literals and renames :class:`long` to
    218    :class:`int`.
     267   Renames :class:`long` to :class:`int`.
    219268
    220269.. 2to3fixer:: map
     
    242291   Converts the use of iterator's :meth:`~iterator.next` methods to the
    243292   :func:`next` function.  It also renames :meth:`next` methods to
    244    :meth:`~object.__next__`.
     293   :meth:`~iterator.__next__`.
    245294
    246295.. 2to3fixer:: nonzero
    247296
    248    Renames :meth:`~object.__nonzero__` to :meth:`~object.__bool__`.
     297   Renames :meth:`__nonzero__` to :meth:`~object.__bool__`.
    249298
    250299.. 2to3fixer:: numliterals
     
    261310   Converts the :keyword:`print` statement to the :func:`print` function.
    262311
    263 .. 2to3fixer:: raises
     312.. 2to3fixer:: raise
    264313
    265314   Converts ``raise E, V`` to ``raise E(V)``, and ``raise E, V, T`` to ``raise
    266315   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.
    268317
    269318.. 2to3fixer:: raw_input
Note: See TracChangeset for help on using the changeset viewer.