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/doctest.rst

    r2 r391  
     1:keepdoctest:
     2
    13:mod:`doctest` --- Test interactive Python examples
    24===================================================
     
    9698
    9799There's no output!  That's normal, and it means all the examples worked.  Pass
    98 :option:`-v` to the script, and :mod:`doctest` prints a detailed log of what
     100``-v`` to the script, and :mod:`doctest` prints a detailed log of what
    99101it's trying, and prints a summary at the end::
    100102
     
    164166number of examples that failed.
    165167
    166 Run it with the :option:`-v` switch instead::
     168Run it with the ``-v`` switch instead::
    167169
    168170   python M.py -v
     
    173175You can force verbose mode by passing ``verbose=True`` to :func:`testmod`, or
    174176prohibit it by passing ``verbose=False``.  In either of those cases,
    175 ``sys.argv`` is not examined by :func:`testmod` (so passing :option:`-v` or not
     177``sys.argv`` is not examined by :func:`testmod` (so passing ``-v`` or not
    176178has no effect).
    177179
     
    243245
    244246Like :func:`testmod`, :func:`testfile`'s verbosity can be set with the
    245 :option:`-v` command-line switch or with the optional keyword argument
     247``-v`` command-line switch or with the optional keyword argument
    246248*verbose*.
    247249
     
    300302^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    301303
    302 In most cases a copy-and-paste of an interactive console session works fine, but
    303 doctest isn't trying to do an exact emulation of any specific Python shell.  All
    304 hard tab characters are expanded to spaces, using 8-column tab stops.  If you
    305 don't believe tabs should mean that, too bad:  don't use hard tabs, or write
    306 your own :class:`DocTestParser` class.
    307 
    308 .. versionchanged:: 2.4
    309    Expanding tabs to spaces is new; previous versions tried to preserve hard tabs,
    310    with confusing results.
     304In most cases a copy-and-paste of an interactive console session works fine,
     305but doctest isn't trying to do an exact emulation of any specific Python shell.
    311306
    312307::
     
    339334  is expected.
    340335
    341   .. versionchanged:: 2.4
     336  .. versionadded:: 2.4
    342337     ``<BLANKLINE>`` was added; there was no way to use expected output containing
    343338     empty lines in previous versions.
     339
     340* All hard tab characters are expanded to spaces, using 8-column tab stops.
     341  Tabs in output generated by the tested code are not modified.  Because any
     342  hard tabs in the sample output *are* expanded, this means that if the code
     343  output includes hard tabs, the only way the doctest can pass is if the
     344  :const:`NORMALIZE_WHITESPACE` option or :ref:`directive <doctest-directives>`
     345  is in effect.
     346  Alternatively, the test can be rewritten to capture the output and compare it
     347  to an expected value as part of the test.  This handling of tabs in the
     348  source was arrived at through trial and error, and has proven to be the least
     349  error prone way of handling them.  It is possible to use a different
     350  algorithm for handling tabs by writing a custom :class:`DocTestParser` class.
     351
     352  .. versionchanged:: 2.4
     353     Expanding tabs to spaces is new; previous versions tried to preserve hard tabs,
     354     with confusing results.
    344355
    345356* Output to stdout is captured, but not output to stderr (exception tracebacks
     
    356367
    357368  Otherwise, the backslash will be interpreted as part of the string. For example,
    358   the "\\" above would be interpreted as a newline character.  Alternatively, you
     369  the ``\n`` above would be interpreted as a newline character.  Alternatively, you
    359370  can double each backslash in the doctest version (and not use a raw string)::
    360371
     
    439450exception's type and detail, and the rest are ignored.
    440451
     452.. versionchanged:: 2.4
     453   Previous versions were unable to handle multi-line exception details.
     454
    441455Best practice is to omit the traceback stack, unless it adds significant
    442456documentation value to the example.  So the last example is probably better as::
     
    470484  course this does the right thing for genuine tracebacks.
    471485
    472 * When the :const:`IGNORE_EXCEPTION_DETAIL` doctest option is is specified,
    473   everything following the leftmost colon is ignored.
     486* When the :const:`IGNORE_EXCEPTION_DETAIL` doctest option is specified,
     487  everything following the leftmost colon and any module information in the
     488  exception name is ignored.
    474489
    475490* The interactive shell omits the traceback header line for some
     
    499514     SyntaxError: invalid syntax
    500515
    501 .. versionchanged:: 2.4
    502    The ability to handle a multi-line exception detail, and the
    503    :const:`IGNORE_EXCEPTION_DETAIL` doctest option, were added.
    504 
    505 
     516
     517.. _option-flags-and-directives:
    506518.. _doctest-options:
    507519
    508 Option Flags and Directives
    509 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
     520Option Flags
     521^^^^^^^^^^^^
    510522
    511523A number of option flags control various aspects of doctest's behavior.
    512524Symbolic names for the flags are supplied as module constants, which can be
    513525or'ed together and passed to various functions.  The names can also be used in
    514 doctest directives (see below).
     526:ref:`doctest directives <doctest-directives>`.
    515527
    516528The first group of options define test semantics, controlling aspects of how
     
    565577   :exc:`TypeError` is raised.
    566578
    567    Note that a similar effect can be obtained using :const:`ELLIPSIS`, and
    568    :const:`IGNORE_EXCEPTION_DETAIL` may go away when Python releases prior to 2.4
    569    become uninteresting.  Until then, :const:`IGNORE_EXCEPTION_DETAIL` is the only
    570    clear way to write a doctest that doesn't care about the exception detail yet
    571    continues to pass under Python releases prior to 2.4 (doctest directives appear
    572    to be comments to them).  For example, ::
    573 
    574       >>> (1, 2)[3] = 'moo' #doctest: +IGNORE_EXCEPTION_DETAIL
     579   It will also ignore the module name used in Python 3 doctest reports. Hence
     580   both of these variations will work with the flag specified, regardless of
     581   whether the test is run under Python 2.7 or Python 3.2 (or later versions)::
     582
     583      >>> raise CustomError('message')
     584      Traceback (most recent call last):
     585      CustomError: message
     586
     587      >>> raise CustomError('message')
     588      Traceback (most recent call last):
     589      my_module.CustomError: message
     590
     591   Note that :const:`ELLIPSIS` can also be used to ignore the
     592   details of the exception message, but such a test may still fail based
     593   on whether or not the module details are printed as part of the
     594   exception name. Using :const:`IGNORE_EXCEPTION_DETAIL` and the details
     595   from Python 2.3 is also the only clear way to write a doctest that doesn't
     596   care about the exception detail yet continues to pass under Python 2.3 or
     597   earlier (those releases do not support :ref:`doctest directives
     598   <doctest-directives>` and ignore them as irrelevant comments). For example::
     599
     600      >>> (1, 2)[3] = 'moo'
    575601      Traceback (most recent call last):
    576602        File "<stdin>", line 1, in ?
    577603      TypeError: object doesn't support item assignment
    578604
    579    passes under Python 2.4 and Python 2.3.  The detail changed in 2.4, to say "does
    580    not" instead of "doesn't".
     605   passes under Python 2.3 and later Python versions with the flag specified,
     606   even though the detail
     607   changed in Python 2.4 to say "does not" instead of "doesn't".
     608
     609   .. versionchanged:: 2.7
     610      :const:`IGNORE_EXCEPTION_DETAIL` now also ignores any information
     611      relating to the module containing the exception under test
    581612
    582613
     
    590621
    591622   The SKIP flag can also be used for temporarily "commenting out" examples.
     623
     624.. versionadded:: 2.5
    592625
    593626
     
    635668   A bitmask or'ing together all the reporting flags above.
    636669
    637 "Doctest directives" may be used to modify the option flags for individual
    638 examples.  Doctest directives are expressed as a special Python comment
    639 following an example's source code:
     670
     671.. versionadded:: 2.4
     672   The constants
     673   :const:`DONT_ACCEPT_BLANKLINE`, :const:`NORMALIZE_WHITESPACE`,
     674   :const:`ELLIPSIS`, :const:`IGNORE_EXCEPTION_DETAIL`, :const:`REPORT_UDIFF`,
     675   :const:`REPORT_CDIFF`, :const:`REPORT_NDIFF`,
     676   :const:`REPORT_ONLY_FIRST_FAILURE`, :const:`COMPARISON_FLAGS` and
     677   :const:`REPORTING_FLAGS` were added.
     678
     679There's also a way to register new option flag names, although this isn't useful
     680unless you intend to extend :mod:`doctest` internals via subclassing:
     681
     682
     683.. function:: register_optionflag(name)
     684
     685   Create a new option flag with a given name, and return the new flag's integer
     686   value.  :func:`register_optionflag` can be used when subclassing
     687   :class:`OutputChecker` or :class:`DocTestRunner` to create new options that are
     688   supported by your subclasses.  :func:`register_optionflag` should always be
     689   called using the following idiom::
     690
     691      MY_FLAG = register_optionflag('MY_FLAG')
     692
     693   .. versionadded:: 2.4
     694
     695
     696.. _doctest-directives:
     697
     698Directives
     699^^^^^^^^^^
     700
     701Doctest directives may be used to modify the :ref:`option flags
     702<doctest-options>` for an individual example.  Doctest directives are
     703special Python comments following an example's source code:
    640704
    641705.. productionlist:: doctest
     
    655719For example, this test passes::
    656720
    657    >>> print range(20) #doctest: +NORMALIZE_WHITESPACE
     721   >>> print range(20) # doctest: +NORMALIZE_WHITESPACE
    658722   [0,   1,  2,  3,  4,  5,  6,  7,  8,  9,
    659723   10,  11, 12, 13, 14, 15, 16, 17, 18, 19]
     
    664728so::
    665729
    666    >>> print range(20) # doctest:+ELLIPSIS
     730   >>> print range(20) # doctest: +ELLIPSIS
    667731   [0, 1, ..., 18, 19]
    668732
    669 Multiple directives can be used on a single physical line, separated by commas::
     733Multiple directives can be used on a single physical line, separated by
     734commas::
    670735
    671736   >>> print range(20) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
     
    693758disabling an option via ``-`` in a directive can be useful.
    694759
    695 .. versionchanged:: 2.4
    696    Constants :const:`DONT_ACCEPT_BLANKLINE`, :const:`NORMALIZE_WHITESPACE`,
    697    :const:`ELLIPSIS`, :const:`IGNORE_EXCEPTION_DETAIL`, :const:`REPORT_UDIFF`,
    698    :const:`REPORT_CDIFF`, :const:`REPORT_NDIFF`,
    699    :const:`REPORT_ONLY_FIRST_FAILURE`, :const:`COMPARISON_FLAGS` and
    700    :const:`REPORTING_FLAGS` were added; by default ``<BLANKLINE>`` in expected
    701    output matches an empty line in actual output; and doctest directives were
    702    added.
    703 
    704 .. versionchanged:: 2.5
    705    Constant :const:`SKIP` was added.
    706 
    707 There's also a way to register new option flag names, although this isn't useful
    708 unless you intend to extend :mod:`doctest` internals via subclassing:
    709 
    710 
    711 .. function:: register_optionflag(name)
    712 
    713    Create a new option flag with a given name, and return the new flag's integer
    714    value.  :func:`register_optionflag` can be used when subclassing
    715    :class:`OutputChecker` or :class:`DocTestRunner` to create new options that are
    716    supported by your subclasses.  :func:`register_optionflag` should always be
    717    called using the following idiom::
    718 
    719       MY_FLAG = register_optionflag('MY_FLAG')
    720 
    721    .. versionadded:: 2.4
     760.. versionadded:: 2.4
     761   Support for doctest directives was added.
    722762
    723763
     
    949989Python 2.4, :mod:`doctest`'s :class:`Tester` class is deprecated, and
    950990:mod:`doctest` provides two functions that can be used to create :mod:`unittest`
    951 test suites from modules and text files containing doctests.  These test suites
    952 can then be run using :mod:`unittest` test runners::
     991test suites from modules and text files containing doctests.  To integrate with
     992:mod:`unittest` test discovery, include a :func:`load_tests` function in your
     993test module::
    953994
    954995   import unittest
    955996   import doctest
    956    import my_module_with_doctests, and_another
    957 
    958    suite = unittest.TestSuite()
    959    for mod in my_module_with_doctests, and_another:
    960        suite.addTest(doctest.DocTestSuite(mod))
    961    runner = unittest.TextTestRunner()
    962    runner.run(suite)
     997   import my_module_with_doctests
     998
     999   def load_tests(loader, tests, ignore):
     1000       tests.addTests(doctest.DocTestSuite(my_module_with_doctests))
     1001       return tests
    9631002
    9641003There are two main functions for creating :class:`unittest.TestSuite` instances
     
    10371076   .. versionchanged:: 2.5
    10381077      The parameter *encoding* was added.
     1078
     1079   .. note::
     1080      Unlike :func:`testmod` and :class:`DocTestFinder`, this function raises
     1081      a :exc:`ValueError` if *module* contains no docstrings.  You can prevent
     1082      this error by passing a :class:`DocTestFinder` instance as the
     1083      *test_finder* argument with its *exclude_empty* keyword argument set
     1084      to ``False``::
     1085
     1086         >>> finder = doctest.DocTestFinder(exclude_empty=False)
     1087         >>> suite = doctest.DocTestSuite(test_finder=finder)
    10391088
    10401089
     
    11771226
    11781227   A collection of doctest examples that should be run in a single namespace.  The
    1179    constructor arguments are used to initialize the member variables of the same
    1180    names.
     1228   constructor arguments are used to initialize the attributes of the same names.
    11811229
    11821230   .. versionadded:: 2.4
    11831231
    1184    :class:`DocTest` defines the following member variables.  They are initialized by
     1232   :class:`DocTest` defines the following attributes.  They are initialized by
    11851233   the constructor, and should not be modified directly.
    11861234
     
    12351283
    12361284   A single interactive example, consisting of a Python statement and its expected
    1237    output.  The constructor arguments are used to initialize the member variables
    1238    of the same names.
     1285   output.  The constructor arguments are used to initialize the attributes of the
     1286   same names.
    12391287
    12401288   .. versionadded:: 2.4
    12411289
    1242    :class:`Example` defines the following member variables.  They are initialized by
     1290   :class:`Example` defines the following attributes.  They are initialized by
    12431291   the constructor, and should not be modified directly.
    12441292
     
    14311479   example, as it is run.  If *verbose* is ``False``, then only failures are
    14321480   printed.  If *verbose* is unspecified, or ``None``, then verbose output is used
    1433    iff the command-line switch :option:`-v` is used.
     1481   iff the command-line switch ``-v`` is used.
    14341482
    14351483   The optional keyword argument *optionflags* can be used to control how the test
     
    17461794.. exception:: DocTestFailure(test, example, got)
    17471795
    1748    An exception thrown by :class:`DocTestRunner` to signal that a doctest example's
     1796   An exception raised by :class:`DocTestRunner` to signal that a doctest example's
    17491797   actual output did not match its expected output. The constructor arguments are
    1750    used to initialize the member variables of the same names.
    1751 
    1752 :exc:`DocTestFailure` defines the following member variables:
     1798   used to initialize the attributes of the same names.
     1799
     1800:exc:`DocTestFailure` defines the following attributes:
    17531801
    17541802
     
    17701818.. exception:: UnexpectedException(test, example, exc_info)
    17711819
    1772    An exception thrown by :class:`DocTestRunner` to signal that a doctest example
    1773    raised an unexpected exception.  The constructor arguments are used to
    1774    initialize the member variables of the same names.
    1775 
    1776 :exc:`UnexpectedException` defines the following member variables:
     1820   An exception raised by :class:`DocTestRunner` to signal that a doctest
     1821   example raised an unexpected exception.  The constructor arguments are used
     1822   to initialize the attributes of the same names.
     1823
     1824:exc:`UnexpectedException` defines the following attributes:
    17771825
    17781826
     
    18571905   Trying to guess where one ends and the other begins is too error-prone, and that
    18581906   also makes for a confusing test.
    1859 
Note: See TracChangeset for help on using the changeset viewer.