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/Lib/test/test_doctest.py

    r2 r391  
    44"""
    55
     6import sys
    67from test import test_support
    78import doctest
    8 import warnings
    99
    1010# NOTE: There are some additional tests relating to interaction with
     
    259259    >>> e.exc_msg
    260260    '\n'
     261
     262Compare `Example`:
     263    >>> example = doctest.Example('print 1', '1\n')
     264    >>> same_example = doctest.Example('print 1', '1\n')
     265    >>> other_example = doctest.Example('print 42', '42\n')
     266    >>> example == same_example
     267    True
     268    >>> example != same_example
     269    False
     270    >>> hash(example) == hash(same_example)
     271    True
     272    >>> example == other_example
     273    False
     274    >>> example != other_example
     275    True
    261276"""
    262277
     
    348363    ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print 1'
    349364
     365Compare `DocTest`:
     366
     367    >>> docstring = '''
     368    ...     >>> print 12
     369    ...     12
     370    ... '''
     371    >>> test = parser.get_doctest(docstring, globs, 'some_test',
     372    ...                           'some_test', 20)
     373    >>> same_test = parser.get_doctest(docstring, globs, 'some_test',
     374    ...                                'some_test', 20)
     375    >>> test == same_test
     376    True
     377    >>> test != same_test
     378    False
     379    >>> hash(test) == hash(same_test)
     380    True
     381    >>> docstring = '''
     382    ...     >>> print 42
     383    ...     42
     384    ... '''
     385    >>> other_test = parser.get_doctest(docstring, globs, 'other_test',
     386    ...                                 'other_file', 10)
     387    >>> test == other_test
     388    False
     389    >>> test != other_test
     390    True
     391
     392Compare `DocTestCase`:
     393
     394    >>> DocTestCase = doctest.DocTestCase
     395    >>> test_case = DocTestCase(test)
     396    >>> same_test_case = DocTestCase(same_test)
     397    >>> other_test_case = DocTestCase(other_test)
     398    >>> test_case == same_test_case
     399    True
     400    >>> test_case != same_test_case
     401    False
     402    >>> hash(test_case) == hash(same_test_case)
     403    True
     404    >>> test == other_test_case
     405    False
     406    >>> test != other_test_case
     407    True
     408
    350409"""
    351410
     
    500559
    501560    >>> from test import doctest_aliases
     561    >>> assert doctest_aliases.TwoNames.f
     562    >>> assert doctest_aliases.TwoNames.g
    502563    >>> tests = excl_empty_finder.find(doctest_aliases)
    503564    >>> print len(tests)
     
    864925    TestResults(failed=0, attempted=1)
    865926
     927IGNORE_EXCEPTION_DETAIL also ignores difference in exception formatting
     928between Python versions. For example, in Python 3.x, the module path of
     929the exception is in the output, but this will fail under Python 2:
     930
     931    >>> def f(x):
     932    ...     r'''
     933    ...     >>> from httplib import HTTPException
     934    ...     >>> raise HTTPException('message')
     935    ...     Traceback (most recent call last):
     936    ...     httplib.HTTPException: message
     937    ...     '''
     938    >>> test = doctest.DocTestFinder().find(f)[0]
     939    >>> doctest.DocTestRunner(verbose=False).run(test)
     940    ... # doctest: +ELLIPSIS
     941    **********************************************************************
     942    File ..., line 4, in f
     943    Failed example:
     944        raise HTTPException('message')
     945    Expected:
     946        Traceback (most recent call last):
     947        httplib.HTTPException: message
     948    Got:
     949        Traceback (most recent call last):
     950        ...
     951        HTTPException: message
     952    TestResults(failed=1, attempted=2)
     953
     954But in Python 2 the module path is not included, an therefore a test must look
     955like the following test to succeed in Python 2. But that test will fail under
     956Python 3.
     957
     958    >>> def f(x):
     959    ...     r'''
     960    ...     >>> from httplib import HTTPException
     961    ...     >>> raise HTTPException('message')
     962    ...     Traceback (most recent call last):
     963    ...     HTTPException: message
     964    ...     '''
     965    >>> test = doctest.DocTestFinder().find(f)[0]
     966    >>> doctest.DocTestRunner(verbose=False).run(test)
     967    TestResults(failed=0, attempted=2)
     968
     969However, with IGNORE_EXCEPTION_DETAIL, the module name of the exception
     970(if any) will be ignored:
     971
     972    >>> def f(x):
     973    ...     r'''
     974    ...     >>> from httplib import HTTPException
     975    ...     >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL
     976    ...     Traceback (most recent call last):
     977    ...     HTTPException: message
     978    ...     '''
     979    >>> test = doctest.DocTestFinder().find(f)[0]
     980    >>> doctest.DocTestRunner(verbose=False).run(test)
     981    TestResults(failed=0, attempted=2)
     982
     983The module path will be completely ignored, so two different module paths will
     984still pass if IGNORE_EXCEPTION_DETAIL is given. This is intentional, so it can
     985be used when exceptions have changed module.
     986
     987    >>> def f(x):
     988    ...     r'''
     989    ...     >>> from httplib import HTTPException
     990    ...     >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL
     991    ...     Traceback (most recent call last):
     992    ...     foo.bar.HTTPException: message
     993    ...     '''
     994    >>> test = doctest.DocTestFinder().find(f)[0]
     995    >>> doctest.DocTestRunner(verbose=False).run(test)
     996    TestResults(failed=0, attempted=2)
     997
    866998But IGNORE_EXCEPTION_DETAIL does not allow a mismatch in the exception type:
    867999
     
    9081040        ZeroDivisionError: integer division or modulo by zero
    9091041    TestResults(failed=1, attempted=1)
     1042"""
     1043    def displayhook(): r"""
     1044Test that changing sys.displayhook doesn't matter for doctest.
     1045
     1046    >>> import sys
     1047    >>> orig_displayhook = sys.displayhook
     1048    >>> def my_displayhook(x):
     1049    ...     print('hi!')
     1050    >>> sys.displayhook = my_displayhook
     1051    >>> def f():
     1052    ...     '''
     1053    ...     >>> 3
     1054    ...     3
     1055    ...     '''
     1056    >>> test = doctest.DocTestFinder().find(f)[0]
     1057    >>> r = doctest.DocTestRunner(verbose=False).run(test)
     1058    >>> post_displayhook = sys.displayhook
     1059
     1060    We need to restore sys.displayhook now, so that we'll be able to test
     1061    results.
     1062
     1063    >>> sys.displayhook = orig_displayhook
     1064
     1065    Ok, now we can check that everything is ok.
     1066
     1067    >>> r
     1068    TestResults(failed=0, attempted=1)
     1069    >>> post_displayhook is my_displayhook
     1070    True
    9101071"""
    9111072    def optionflags(): r"""
     
    11911352    TestResults(failed=1, attempted=1)
    11921353
    1193 The REPORT_ONLY_FIRST_FAILURE supresses result output after the first
     1354The REPORT_ONLY_FIRST_FAILURE suppresses result output after the first
    11941355failing example:
    11951356
     
    12211382    TestResults(failed=3, attempted=5)
    12221383
    1223 However, output from `report_start` is not supressed:
     1384However, output from `report_start` is not suppressed:
    12241385
    12251386    >>> doctest.DocTestRunner(verbose=True, optionflags=flags).run(test)
     
    15091670    Traceback (most recent call last):
    15101671    ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS'
    1511 """
     1672
     1673    """
     1674
     1675    def test_unicode_output(self): r"""
     1676
     1677Check that unicode output works:
     1678
     1679    >>> u'\xe9'
     1680    u'\xe9'
     1681
     1682If we return unicode, SpoofOut's buf variable becomes automagically
     1683converted to unicode. This means all subsequent output becomes converted
     1684to unicode, and if the output contains non-ascii characters that failed.
     1685It used to be that this state change carried on between tests, meaning
     1686tests would fail if unicode has been output previously in the testrun.
     1687This test tests that this is no longer so:
     1688
     1689    >>> print u'abc'
     1690    abc
     1691
     1692And then return a string with non-ascii characters:
     1693
     1694    >>> print u'\xe9'.encode('utf-8')
     1695    é
     1696
     1697    """
     1698
    15121699
    15131700def test_testsource(): r"""
     
    15941781      >>> doc = '''
    15951782      ... >>> x = 42
     1783      ... >>> raise Exception('clé')
     1784      ... Traceback (most recent call last):
     1785      ... Exception: clé
    15961786      ... >>> import pdb; pdb.set_trace()
    15971787      ... '''
    15981788      >>> parser = doctest.DocTestParser()
    1599       >>> test = parser.get_doctest(doc, {}, "foo", "foo.py", 0)
     1789      >>> test = parser.get_doctest(doc, {}, "foo-bÀr@baz", "foo-bÀr@baz.py", 0)
    16001790      >>> runner = doctest.DocTestRunner(verbose=False)
    16011791
     
    16131803      ... finally: sys.stdin = real_stdin
    16141804      --Return--
    1615       > <doctest foo[1]>(1)<module>()->None
     1805      > <doctest foo-bÀr@baz[2]>(1)<module>()->None
    16161806      -> import pdb; pdb.set_trace()
    16171807      (Pdb) print x
    16181808      42
    16191809      (Pdb) continue
    1620       TestResults(failed=0, attempted=2)
     1810      TestResults(failed=0, attempted=3)
    16211811
    16221812      You can also put pdb.set_trace in a function called from a test:
     
    16301820      ... >>> calls_set_trace()
    16311821      ... '''
    1632       >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0)
     1822      >>> test = parser.get_doctest(doc, globals(), "foo-bÀr@baz", "foo-bÀr@baz.py", 0)
    16331823      >>> real_stdin = sys.stdin
    16341824      >>> sys.stdin = _FakeInput([
     
    16491839      2
    16501840      (Pdb) up
    1651       > <doctest foo[1]>(1)<module>()
     1841      > <doctest foo-bÀr@baz[1]>(1)<module>()
    16521842      -> calls_set_trace()
    16531843      (Pdb) print x
     
    16671857      ... >>> f(3)
    16681858      ... '''
    1669       >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0)
     1859      >>> test = parser.get_doctest(doc, globals(), "foo-bÀr@baz", "foo-bÀr@baz.py", 0)
    16701860      >>> real_stdin = sys.stdin
    16711861      >>> sys.stdin = _FakeInput([
     
    16811871      ... # doctest: +NORMALIZE_WHITESPACE
    16821872      --Return--
    1683       > <doctest foo[1]>(3)g()->None
     1873      > <doctest foo-bÀr@baz[1]>(3)g()->None
    16841874      -> import pdb; pdb.set_trace()
    16851875      (Pdb) list
     
    16901880      (Pdb) next
    16911881      --Return--
    1692       > <doctest foo[0]>(2)f()->None
     1882      > <doctest foo-bÀr@baz[0]>(2)f()->None
    16931883      -> g(x*2)
    16941884      (Pdb) list
     
    16981888      (Pdb) next
    16991889      --Return--
    1700       > <doctest foo[2]>(1)<module>()->None
     1890      > <doctest foo-bÀr@baz[2]>(1)<module>()->None
    17011891      -> f(3)
    17021892      (Pdb) list
     
    17051895      (Pdb) continue
    17061896      **********************************************************************
    1707       File "foo.py", line 7, in foo
     1897      File "foo-bÀr@baz.py", line 7, in foo-bÀr@baz
    17081898      Failed example:
    17091899          f(3)
     
    17391929    >>> parser = doctest.DocTestParser()
    17401930    >>> runner = doctest.DocTestRunner(verbose=False)
    1741     >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0)
     1931    >>> test = parser.get_doctest(doc, globals(), "foo-bÀr@baz", "foo-bÀr@baz.py", 0)
    17421932    >>> real_stdin = sys.stdin
    17431933    >>> sys.stdin = _FakeInput([
     
    17911981    1
    17921982    (Pdb) up
    1793     > <doctest foo[1]>(1)<module>()
     1983    > <doctest foo-bÀr@baz[1]>(1)<module>()
    17941984    -> calls_set_trace()
    17951985    (Pdb) print foo
     
    18091999         >>> suite = doctest.DocTestSuite(test.sample_doctest)
    18102000         >>> suite.run(unittest.TestResult())
    1811          <unittest.TestResult run=9 errors=0 failures=4>
     2001         <unittest.result.TestResult run=9 errors=0 failures=4>
    18122002
    18132003       We can also supply the module by name:
     
    18152005         >>> suite = doctest.DocTestSuite('test.sample_doctest')
    18162006         >>> suite.run(unittest.TestResult())
    1817          <unittest.TestResult run=9 errors=0 failures=4>
     2007         <unittest.result.TestResult run=9 errors=0 failures=4>
     2008
     2009       The module need not contain any doctest examples:
     2010
     2011         >>> suite = doctest.DocTestSuite('test.sample_doctest_no_doctests')
     2012         >>> suite.run(unittest.TestResult())
     2013         <unittest.result.TestResult run=0 errors=0 failures=0>
     2014
     2015       However, if DocTestSuite finds no docstrings, it raises an error:
     2016
     2017         >>> try:
     2018         ...     doctest.DocTestSuite('test.sample_doctest_no_docstrings')
     2019         ... except ValueError as e:
     2020         ...     error = e
     2021
     2022         >>> print(error.args[1])
     2023         has no docstrings
     2024
     2025       You can prevent this error by passing a DocTestFinder instance with
     2026       the `exclude_empty` keyword argument set to False:
     2027
     2028         >>> finder = doctest.DocTestFinder(exclude_empty=False)
     2029         >>> suite = doctest.DocTestSuite('test.sample_doctest_no_docstrings',
     2030         ...                              test_finder=finder)
     2031         >>> suite.run(unittest.TestResult())
     2032         <unittest.result.TestResult run=0 errors=0 failures=0>
    18182033
    18192034       We can use the current module:
     
    18212036         >>> suite = test.sample_doctest.test_suite()
    18222037         >>> suite.run(unittest.TestResult())
    1823          <unittest.TestResult run=9 errors=0 failures=4>
     2038         <unittest.result.TestResult run=9 errors=0 failures=4>
    18242039
    18252040       We can supply global variables.  If we pass globs, they will be
     
    18292044         >>> suite = doctest.DocTestSuite('test.sample_doctest', globs={})
    18302045         >>> suite.run(unittest.TestResult())
    1831          <unittest.TestResult run=9 errors=0 failures=5>
     2046         <unittest.result.TestResult run=9 errors=0 failures=5>
    18322047
    18332048       Alternatively, we can provide extra globals.  Here we'll make an
     
    18372052         ...                              extraglobs={'y': 1})
    18382053         >>> suite.run(unittest.TestResult())
    1839          <unittest.TestResult run=9 errors=0 failures=3>
     2054         <unittest.result.TestResult run=9 errors=0 failures=3>
    18402055
    18412056       You can pass option flags.  Here we'll cause an extra error
     
    18452060         ...                      optionflags=doctest.DONT_ACCEPT_BLANKLINE)
    18462061         >>> suite.run(unittest.TestResult())
    1847          <unittest.TestResult run=9 errors=0 failures=5>
     2062         <unittest.result.TestResult run=9 errors=0 failures=5>
    18482063
    18492064       You can supply setUp and tearDown functions:
     
    18622077         ...      setUp=setUp, tearDown=tearDown)
    18632078         >>> suite.run(unittest.TestResult())
    1864          <unittest.TestResult run=9 errors=0 failures=3>
     2079         <unittest.result.TestResult run=9 errors=0 failures=3>
    18652080
    18662081       But the tearDown restores sanity:
     
    18802095         >>> suite = doctest.DocTestSuite('test.sample_doctest', setUp=setUp)
    18812096         >>> suite.run(unittest.TestResult())
    1882          <unittest.TestResult run=9 errors=0 failures=3>
     2097         <unittest.result.TestResult run=9 errors=0 failures=3>
    18832098
    18842099       Here, we didn't need to use a tearDown function because we
     
    18992114         ...                              'test_doctest4.txt')
    19002115         >>> suite.run(unittest.TestResult())
    1901          <unittest.TestResult run=3 errors=0 failures=3>
     2116         <unittest.result.TestResult run=3 errors=0 failures=3>
    19022117
    19032118       The test files are looked for in the directory containing the
     
    19112126         ...                              package='test')
    19122127         >>> suite.run(unittest.TestResult())
    1913          <unittest.TestResult run=3 errors=0 failures=3>
     2128         <unittest.result.TestResult run=3 errors=0 failures=3>
    19142129
    19152130       Support for using a package's __loader__.get_data() is also
     
    19302145         ...     if added_loader:
    19312146         ...         del test.__loader__
    1932          <unittest.TestResult run=3 errors=0 failures=3>
     2147         <unittest.result.TestResult run=3 errors=0 failures=3>
    19332148
    19342149       '/' should be used as a path separator.  It will be converted
     
    19372152         >>> suite = doctest.DocFileSuite('../test/test_doctest.txt')
    19382153         >>> suite.run(unittest.TestResult())
    1939          <unittest.TestResult run=1 errors=0 failures=1>
     2154         <unittest.result.TestResult run=1 errors=0 failures=1>
    19402155
    19412156       If DocFileSuite is used from an interactive session, then files
     
    19622177         >>> suite = doctest.DocFileSuite(test_file, module_relative=False)
    19632178         >>> suite.run(unittest.TestResult())
    1964          <unittest.TestResult run=1 errors=0 failures=1>
     2179         <unittest.result.TestResult run=1 errors=0 failures=1>
    19652180
    19662181       It is an error to specify `package` when `module_relative=False`:
     
    19782193         ...                              globs={'favorite_color': 'blue'})
    19792194         >>> suite.run(unittest.TestResult())
    1980          <unittest.TestResult run=3 errors=0 failures=2>
     2195         <unittest.result.TestResult run=3 errors=0 failures=2>
    19812196
    19822197       In this case, we supplied a missing favorite color. You can
     
    19892204         ...                              globs={'favorite_color': 'blue'})
    19902205         >>> suite.run(unittest.TestResult())
    1991          <unittest.TestResult run=3 errors=0 failures=3>
     2206         <unittest.result.TestResult run=3 errors=0 failures=3>
    19922207
    19932208       And, you can provide setUp and tearDown functions:
     
    20082223         ...                              setUp=setUp, tearDown=tearDown)
    20092224         >>> suite.run(unittest.TestResult())
    2010          <unittest.TestResult run=3 errors=0 failures=2>
     2225         <unittest.result.TestResult run=3 errors=0 failures=2>
    20112226
    20122227       But the tearDown restores sanity:
     
    20272242         >>> suite = doctest.DocFileSuite('test_doctest.txt', setUp=setUp)
    20282243         >>> suite.run(unittest.TestResult())
    2029          <unittest.TestResult run=1 errors=0 failures=0>
     2244         <unittest.result.TestResult run=1 errors=0 failures=0>
    20302245
    20312246       Here, we didn't need to use a tearDown function because we
     
    20392254         >>> suite = doctest.DocFileSuite('test_doctest3.txt')
    20402255         >>> suite.run(unittest.TestResult())
    2041          <unittest.TestResult run=1 errors=0 failures=0>
     2256         <unittest.result.TestResult run=1 errors=0 failures=0>
    20422257
    20432258       If the tests contain non-ASCII characters, we have to specify which
     
    20502265         ...                              encoding='utf-8')
    20512266         >>> suite.run(unittest.TestResult())
    2052          <unittest.TestResult run=3 errors=0 failures=2>
     2267         <unittest.result.TestResult run=3 errors=0 failures=2>
    20532268
    20542269       """
     
    21682383
    21692384(Note: we'll be clearing doctest.master after each call to
    2170 `doctest.testfile`, to supress warnings about multiple tests with the
     2385`doctest.testfile`, to suppress warnings about multiple tests with the
    21712386same name.)
    21722387
     
    22042419    >>> doctest.master = None  # Reset master.
    22052420
    2206 Verbosity can be increased with the optional `verbose` paremter:
     2421Verbosity can be increased with the optional `verbose` parameter:
    22072422
    22082423    >>> doctest.testfile('test_doctest.txt', globs=globs, verbose=True)
     
    22412456    >>> doctest.master = None  # Reset master.
    22422457
    2243 The summary report may be supressed with the optional `report`
     2458The summary report may be suppressed with the optional `report`
    22442459parameter:
    22452460
     
    23332548# that these use the deprecated doctest.Tester, so should go away (or
    23342549# be rewritten) someday.
    2335 
    2336 # Ignore all warnings about the use of class Tester in this module.
    2337 # Note that the name of this module may differ depending on how it's
    2338 # imported, so the use of __name__ is important.
    2339 warnings.filterwarnings("ignore", "class Tester", DeprecationWarning,
    2340                         __name__, 0)
    23412550
    23422551def old_test1(): r"""
     
    24632672    # Check the doctest cases in doctest itself:
    24642673    test_support.run_doctest(doctest, verbosity=True)
    2465     # Check the doctest cases defined here:
     2674
    24662675    from test import test_doctest
    2467     test_support.run_doctest(test_doctest, verbosity=True)
    2468 
    2469 import trace, sys
     2676
     2677    # Ignore all warnings about the use of class Tester in this module.
     2678    deprecations = []
     2679    if __debug__:
     2680        deprecations.append(("class Tester is deprecated", DeprecationWarning))
     2681    if sys.py3kwarning:
     2682        deprecations += [("backquote not supported", SyntaxWarning),
     2683                         ("execfile.. not supported", DeprecationWarning)]
     2684    with test_support.check_warnings(*deprecations):
     2685        # Check the doctest cases defined here:
     2686        test_support.run_doctest(test_doctest, verbosity=True)
     2687
     2688import sys
    24702689def test_coverage(coverdir):
     2690    trace = test_support.import_module('trace')
    24712691    tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,],
    24722692                         trace=0, count=1)
Note: See TracChangeset for help on using the changeset viewer.