annotate Lib/doctest.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 64f1b8ad9214
children d012f645b8b2 ff7957aa01a1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19359
eff8ca679bf6 Remove the horrid generators hack from doctest.py. This relies on a
Tim Peters <tim.peters@gmail.com>
parents: 18633
diff changeset
1 # Module doctest.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2 # Released to the public domain 16-Jan-2001, by Tim Peters (tim@python.org).
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
3 # Major enhancements and refactoring by:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
4 # Jim Fulton
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
5 # Edward Loper
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
6
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
7 # Provided as-is; use at your own risk; no warranty; no promises; enjoy!
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
8
32165
44828cc136e0 Patch #932930: suggest the use of rawstrings for backslashes.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
9 r"""Module doctest -- a framework for running examples in docstrings.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
10
33062
764d28fc3d36 Removed lots of stuff from the module docstring. My intent for 2.4 is
Tim Peters <tim.peters@gmail.com>
parents: 33061
diff changeset
11 In simplest use, end each module M to be tested with:
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
12
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
13 def _test():
33062
764d28fc3d36 Removed lots of stuff from the module docstring. My intent for 2.4 is
Tim Peters <tim.peters@gmail.com>
parents: 33061
diff changeset
14 import doctest
33574
e478ebe1cb31 Removed most of the module docstring. There's too much to explain now,
Tim Peters <tim.peters@gmail.com>
parents: 33533
diff changeset
15 doctest.testmod()
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
16
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
17 if __name__ == "__main__":
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
18 _test()
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
19
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
20 Then running the module as a script will cause the examples in the
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
21 docstrings to get executed and verified:
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
22
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
23 python M.py
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
24
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
25 This won't display anything unless an example fails, in which case the
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
26 failing example(s) and the cause(s) of the failure(s) are printed to stdout
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
27 (why not stderr? because stderr is a lame hack <0.2 wink>), and the final
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
28 line of output is "Test failed.".
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
29
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
30 Run it with the -v switch instead:
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
31
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
32 python M.py -v
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
33
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
34 and a detailed report of all examples tried is printed to stdout, along
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
35 with assorted summaries at the end.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
36
33062
764d28fc3d36 Removed lots of stuff from the module docstring. My intent for 2.4 is
Tim Peters <tim.peters@gmail.com>
parents: 33061
diff changeset
37 You can force verbose mode by passing "verbose=True" to testmod, or prohibit
764d28fc3d36 Removed lots of stuff from the module docstring. My intent for 2.4 is
Tim Peters <tim.peters@gmail.com>
parents: 33061
diff changeset
38 it by passing "verbose=False". In either of those cases, sys.argv is not
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
39 examined by testmod.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
40
33062
764d28fc3d36 Removed lots of stuff from the module docstring. My intent for 2.4 is
Tim Peters <tim.peters@gmail.com>
parents: 33061
diff changeset
41 There are a variety of other ways to run doctests, including integration
764d28fc3d36 Removed lots of stuff from the module docstring. My intent for 2.4 is
Tim Peters <tim.peters@gmail.com>
parents: 33061
diff changeset
42 with the unittest framework, and support for running non-Python text
764d28fc3d36 Removed lots of stuff from the module docstring. My intent for 2.4 is
Tim Peters <tim.peters@gmail.com>
parents: 33061
diff changeset
43 files containing doctests. There are also many ways to override parts
764d28fc3d36 Removed lots of stuff from the module docstring. My intent for 2.4 is
Tim Peters <tim.peters@gmail.com>
parents: 33061
diff changeset
44 of doctest's default behaviors. See the Library Reference Manual for
764d28fc3d36 Removed lots of stuff from the module docstring. My intent for 2.4 is
Tim Peters <tim.peters@gmail.com>
parents: 33061
diff changeset
45 details.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
46 """
33574
e478ebe1cb31 Removed most of the module docstring. There's too much to explain now,
Tim Peters <tim.peters@gmail.com>
parents: 33533
diff changeset
47
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
48 __docformat__ = 'reStructuredText en'
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
49
19359
eff8ca679bf6 Remove the horrid generators hack from doctest.py. This relies on a
Tim Peters <tim.peters@gmail.com>
parents: 18633
diff changeset
50 __all__ = [
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
51 # 0, Option Flags
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
52 'register_optionflag',
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
53 'DONT_ACCEPT_TRUE_FOR_1',
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
54 'DONT_ACCEPT_BLANKLINE',
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
55 'NORMALIZE_WHITESPACE',
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
56 'ELLIPSIS',
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
57 'SKIP',
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
58 'IGNORE_EXCEPTION_DETAIL',
33403
5ed56493f0b0 Added a couple names to __all__.
Tim Peters <tim.peters@gmail.com>
parents: 33340
diff changeset
59 'COMPARISON_FLAGS',
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
60 'REPORT_UDIFF',
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
61 'REPORT_CDIFF',
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
62 'REPORT_NDIFF',
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
63 'REPORT_ONLY_FIRST_FAILURE',
33403
5ed56493f0b0 Added a couple names to __all__.
Tim Peters <tim.peters@gmail.com>
parents: 33340
diff changeset
64 'REPORTING_FLAGS',
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
65 # 1. Utility Functions
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
66 # 2. Example & DocTest
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
67 'Example',
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
68 'DocTest',
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
69 # 3. Doctest Parser
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
70 'DocTestParser',
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
71 # 4. Doctest Finder
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
72 'DocTestFinder',
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
73 # 5. Doctest Runner
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
74 'DocTestRunner',
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
75 'OutputChecker',
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
76 'DocTestFailure',
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
77 'UnexpectedException',
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
78 'DebugRunner',
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
79 # 6. Test Functions
19359
eff8ca679bf6 Remove the horrid generators hack from doctest.py. This relies on a
Tim Peters <tim.peters@gmail.com>
parents: 18633
diff changeset
80 'testmod',
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
81 'testfile',
19359
eff8ca679bf6 Remove the horrid generators hack from doctest.py. This relies on a
Tim Peters <tim.peters@gmail.com>
parents: 18633
diff changeset
82 'run_docstring_examples',
46964
826aff95cf0b Fix doctest's __all__.
Georg Brandl <georg@python.org>
parents: 46957
diff changeset
83 # 7. Unittest Support
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
84 'DocTestSuite',
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
85 'DocFileSuite',
33579
8a5c87f685cd Add set_unittest_reportflags() to the public API. Docs will follow
Tim Peters <tim.peters@gmail.com>
parents: 33578
diff changeset
86 'set_unittest_reportflags',
46964
826aff95cf0b Fix doctest's __all__.
Georg Brandl <georg@python.org>
parents: 46957
diff changeset
87 # 8. Debugging Support
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
88 'script_from_examples',
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
89 'testsource',
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
90 'debug_src',
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
91 'debug',
19359
eff8ca679bf6 Remove the horrid generators hack from doctest.py. This relies on a
Tim Peters <tim.peters@gmail.com>
parents: 18633
diff changeset
92 ]
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
93
19359
eff8ca679bf6 Remove the horrid generators hack from doctest.py. This relies on a
Tim Peters <tim.peters@gmail.com>
parents: 18633
diff changeset
94 import __future__
71093
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
95 import difflib
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
96 import inspect
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
97 import linecache
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
98 import os
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
99 import pdb
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
100 import re
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
101 import sys
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
102 import traceback
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
103 import unittest
42501
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42428
diff changeset
104 from io import StringIO
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
105 from collections import namedtuple
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
106
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
107 TestResults = namedtuple('TestResults', 'failed attempted')
20213
d053726fb01b SF patch [#466616] Exclude imported items from doctest,
Tim Peters <tim.peters@gmail.com>
parents: 19359
diff changeset
108
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
109 # There are 4 basic classes:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
110 # - Example: a <source, want> pair, plus an intra-docstring line number.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
111 # - DocTest: a collection of examples, parsed from a docstring, plus
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
112 # info about where the docstring came from (name, filename, lineno).
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
113 # - DocTestFinder: extracts DocTests from a given object's docstring and
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
114 # its contained objects' docstrings.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
115 # - DocTestRunner: runs DocTest cases, and accumulates statistics.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
116 #
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
117 # So the basic picture is:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
118 #
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
119 # list of:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
120 # +------+ +---------+ +-------+
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
121 # |object| --DocTestFinder-> | DocTest | --DocTestRunner-> |results|
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
122 # +------+ +---------+ +-------+
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
123 # | Example |
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
124 # | ... |
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
125 # | Example |
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
126 # +---------+
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
127
33086
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
128 # Option constants.
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
129
33086
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
130 OPTIONFLAGS_BY_NAME = {}
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
131 def register_optionflag(name):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
132 # Create a new flag unless `name` is already known.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
133 return OPTIONFLAGS_BY_NAME.setdefault(name, 1 << len(OPTIONFLAGS_BY_NAME))
33086
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
134
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
135 DONT_ACCEPT_TRUE_FOR_1 = register_optionflag('DONT_ACCEPT_TRUE_FOR_1')
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
136 DONT_ACCEPT_BLANKLINE = register_optionflag('DONT_ACCEPT_BLANKLINE')
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
137 NORMALIZE_WHITESPACE = register_optionflag('NORMALIZE_WHITESPACE')
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
138 ELLIPSIS = register_optionflag('ELLIPSIS')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
139 SKIP = register_optionflag('SKIP')
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
140 IGNORE_EXCEPTION_DETAIL = register_optionflag('IGNORE_EXCEPTION_DETAIL')
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
141
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
142 COMPARISON_FLAGS = (DONT_ACCEPT_TRUE_FOR_1 |
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
143 DONT_ACCEPT_BLANKLINE |
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
144 NORMALIZE_WHITESPACE |
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
145 ELLIPSIS |
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
146 SKIP |
33618
8e20ee2ecc0c Reverted the addition of a NORMALIZE_NUMBERS option, per Tim Peter's
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33615
diff changeset
147 IGNORE_EXCEPTION_DETAIL)
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
148
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
149 REPORT_UDIFF = register_optionflag('REPORT_UDIFF')
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
150 REPORT_CDIFF = register_optionflag('REPORT_CDIFF')
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
151 REPORT_NDIFF = register_optionflag('REPORT_NDIFF')
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
152 REPORT_ONLY_FIRST_FAILURE = register_optionflag('REPORT_ONLY_FIRST_FAILURE')
33086
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
153
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
154 REPORTING_FLAGS = (REPORT_UDIFF |
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
155 REPORT_CDIFF |
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
156 REPORT_NDIFF |
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
157 REPORT_ONLY_FIRST_FAILURE)
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
158
33086
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
159 # Special string markers for use in `want` strings:
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
160 BLANKLINE_MARKER = '<BLANKLINE>'
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
161 ELLIPSIS_MARKER = '...'
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
162
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
163 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
164 ## Table of Contents
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
165 ######################################################################
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
166 # 1. Utility Functions
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
167 # 2. Example & DocTest -- store test cases
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
168 # 3. DocTest Parser -- extracts examples from strings
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
169 # 4. DocTest Finder -- extracts test cases from objects
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
170 # 5. DocTest Runner -- runs test cases
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
171 # 6. Test Functions -- convenient wrappers for testing
46957
9d46c289d166 Remove deprecated Tester class from doctest module.
Georg Brandl <georg@python.org>
parents: 44643
diff changeset
172 # 7. Unittest Support
9d46c289d166 Remove deprecated Tester class from doctest module.
Georg Brandl <georg@python.org>
parents: 44643
diff changeset
173 # 8. Debugging Support
9d46c289d166 Remove deprecated Tester class from doctest module.
Georg Brandl <georg@python.org>
parents: 44643
diff changeset
174 # 9. Example Usage
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
175
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
176 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
177 ## 1. Utility Functions
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
178 ######################################################################
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
179
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
180 def _extract_future_flags(globs):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
181 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
182 Return the compiler-flags associated with the future features that
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
183 have been imported into the given namespace (globs).
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
184 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
185 flags = 0
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
186 for fname in __future__.all_feature_names:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
187 feature = globs.get(fname, None)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
188 if feature is getattr(__future__, fname):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
189 flags |= feature.compiler_flag
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
190 return flags
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
191
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
192 def _normalize_module(module, depth=2):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
193 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
194 Return the module specified by `module`. In particular:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
195 - If `module` is a module, then return module.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
196 - If `module` is a string, then import and return the
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
197 module with that name.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
198 - If `module` is None, then return the calling module.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
199 The calling module is assumed to be the module of
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
200 the stack frame at the given depth in the call stack.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
201 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
202 if inspect.ismodule(module):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
203 return module
41592
1e5a2a2b18ee Fix various spots where int/long and str/unicode unification
Walter Dörwald <walter@livinglogic.de>
parents: 41575
diff changeset
204 elif isinstance(module, str):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
205 return __import__(module, globals(), locals(), ["*"])
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
206 elif module is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
207 return sys.modules[sys._getframe(depth).f_globals['__name__']]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
208 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
209 raise TypeError("Expected a module, string, or None")
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
210
42973
1ce89f2578cf Change the way the encoding parameter is handled.
Guido van Rossum <guido@python.org>
parents: 42552
diff changeset
211 def _load_testfile(filename, package, module_relative, encoding):
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35167
diff changeset
212 if module_relative:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35167
diff changeset
213 package = _normalize_module(package, 3)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35167
diff changeset
214 filename = _module_relative_path(package, filename)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35167
diff changeset
215 if hasattr(package, '__loader__'):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35167
diff changeset
216 if hasattr(package.__loader__, 'get_data'):
43957
23befe540d0d Merged revisions 59077-59104 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
217 file_contents = package.__loader__.get_data(filename)
23befe540d0d Merged revisions 59077-59104 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
218 file_contents = file_contents.decode(encoding)
23befe540d0d Merged revisions 59077-59104 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
219 # get_data() opens files as 'rb', so one must do the equivalent
23befe540d0d Merged revisions 59077-59104 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
220 # conversion as universal newlines would do.
23befe540d0d Merged revisions 59077-59104 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
221 return file_contents.replace(os.linesep, '\n'), filename
65338
1782da440fb1 Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64077
diff changeset
222 with open(filename, encoding=encoding) as f:
1782da440fb1 Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64077
diff changeset
223 return f.read(), filename
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35167
diff changeset
224
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
225 def _indent(s, indent=4):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
226 """
59181
815092a5e5c1 Merged revisions 78493 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58630
diff changeset
227 Add the given number of space characters to the beginning of
815092a5e5c1 Merged revisions 78493 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58630
diff changeset
228 every non-blank line in `s`, and return the result.
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
229 """
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
230 # This regexp matches the start of non-blank lines:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
231 return re.sub('(?m)^(?!$)', indent*' ', s)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
232
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
233 def _exception_traceback(exc_info):
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
234 """
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
235 Return a string containing a traceback message for the given
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
236 exc_info tuple (as returned by sys.exc_info()).
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
237 """
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
238 # Get a traceback message.
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
239 excout = StringIO()
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
240 exc_type, exc_val, exc_tb = exc_info
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
241 traceback.print_exception(exc_type, exc_val, exc_tb, file=excout)
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
242 return excout.getvalue()
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
243
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
244 # Override some StringIO methods.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
245 class _SpoofOut(StringIO):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
246 def getvalue(self):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
247 result = StringIO.getvalue(self)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
248 # If anything at all was written, make sure there's a trailing
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
249 # newline. There's no way for the expected output to indicate
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
250 # that a trailing newline is missing.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
251 if result and not result.endswith("\n"):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
252 result += "\n"
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
253 return result
20213
d053726fb01b SF patch [#466616] Exclude imported items from doctest,
Tim Peters <tim.peters@gmail.com>
parents: 19359
diff changeset
254
40799
04f5cbc74424 Kill off softspace completely (except in formatter.py which seems to have
Guido van Rossum <guido@python.org>
parents: 40785
diff changeset
255 def truncate(self, size=None):
58630
c7e32b4e3874 r77895 broke doctest.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57863
diff changeset
256 self.seek(size)
c7e32b4e3874 r77895 broke doctest.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57863
diff changeset
257 StringIO.truncate(self)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
258
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
259 # Worst-case linear-time ellipsis matching.
33200
26563204660c Gave _ellipsis_match() an attractive new leading underscore.
Tim Peters <tim.peters@gmail.com>
parents: 33196
diff changeset
260 def _ellipsis_match(want, got):
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
261 """
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
262 Essentially the only subtle case:
33200
26563204660c Gave _ellipsis_match() an attractive new leading underscore.
Tim Peters <tim.peters@gmail.com>
parents: 33196
diff changeset
263 >>> _ellipsis_match('aa...aa', 'aaa')
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
264 False
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
265 """
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
266 if ELLIPSIS_MARKER not in want:
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
267 return want == got
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
268
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
269 # Find "the real" strings.
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
270 ws = want.split(ELLIPSIS_MARKER)
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
271 assert len(ws) >= 2
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
272
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
273 # Deal with exact matches possibly needed at one or both ends.
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
274 startpos, endpos = 0, len(got)
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
275 w = ws[0]
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
276 if w: # starts with exact match
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
277 if got.startswith(w):
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
278 startpos = len(w)
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
279 del ws[0]
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
280 else:
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
281 return False
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
282 w = ws[-1]
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
283 if w: # ends with exact match
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
284 if got.endswith(w):
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
285 endpos -= len(w)
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
286 del ws[-1]
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
287 else:
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
288 return False
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
289
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
290 if startpos > endpos:
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
291 # Exact end matches required more characters than we have, as in
33200
26563204660c Gave _ellipsis_match() an attractive new leading underscore.
Tim Peters <tim.peters@gmail.com>
parents: 33196
diff changeset
292 # _ellipsis_match('aa...aa', 'aaa')
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
293 return False
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
294
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
295 # For the rest, we only need to find the leftmost non-overlapping
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
296 # match for each piece. If there's no overall match that way alone,
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
297 # there's no overall match period.
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
298 for w in ws:
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
299 # w may be '' at times, if there are consecutive ellipses, or
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
300 # due to an ellipsis at the start or end of `want`. That's OK.
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
301 # Search for an empty string succeeds, and doesn't change startpos.
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
302 startpos = got.find(w, startpos, endpos)
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
303 if startpos < 0:
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
304 return False
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
305 startpos += len(w)
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
306
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
307 return True
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
308
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
309 def _comment_line(line):
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
310 "Return a commented form of the given line"
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
311 line = line.rstrip()
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
312 if line:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
313 return '# '+line
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
314 else:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
315 return '#'
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
316
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
317 class _OutputRedirectingPdb(pdb.Pdb):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
318 """
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
319 A specialized version of the python debugger that redirects stdout
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
320 to a given stream when interacting with the user. Stdout is *not*
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
321 redirected when traced code is executed.
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
322 """
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
323 def __init__(self, out):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
324 self.__out = out
44025
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
325 self.__debugger_used = False
66716
4e62927a2bda Fix test suite to not activate new sigint behavior in pdb.
Georg Brandl <georg@python.org>
parents: 65337
diff changeset
326 # do not play signal games in the pdb
4e62927a2bda Fix test suite to not activate new sigint behavior in pdb.
Georg Brandl <georg@python.org>
parents: 65337
diff changeset
327 pdb.Pdb.__init__(self, stdout=out, nosigint=True)
63453
4903e7577454 Merged revisions 83259,83261,83264-83265,83268-83269,83271-83272,83281 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59181
diff changeset
328 # still use input() to get user input
4903e7577454 Merged revisions 83259,83261,83264-83265,83268-83269,83271-83272,83281 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59181
diff changeset
329 self.use_rawinput = 1
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
330
44025
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
331 def set_trace(self, frame=None):
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
332 self.__debugger_used = True
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
333 if frame is None:
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
334 frame = sys._getframe().f_back
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
335 pdb.Pdb.set_trace(self, frame)
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
336
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
337 def set_continue(self):
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
338 # Calling set_continue unconditionally would break unit test
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
339 # coverage reporting, as Bdb.set_continue calls sys.settrace(None).
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
340 if self.__debugger_used:
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
341 pdb.Pdb.set_continue(self)
ebc7389297d6 Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43957
diff changeset
342
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
343 def trace_dispatch(self, *args):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
344 # Redirect stdout to the given stream.
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
345 save_stdout = sys.stdout
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
346 sys.stdout = self.__out
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
347 # Call Pdb's trace dispatch method.
33945
904771bef3f4 _OutputRedirectingPdb.trace_dispatch(): Return the base class's
Tim Peters <tim.peters@gmail.com>
parents: 33944
diff changeset
348 try:
904771bef3f4 _OutputRedirectingPdb.trace_dispatch(): Return the base class's
Tim Peters <tim.peters@gmail.com>
parents: 33944
diff changeset
349 return pdb.Pdb.trace_dispatch(self, *args)
904771bef3f4 _OutputRedirectingPdb.trace_dispatch(): Return the base class's
Tim Peters <tim.peters@gmail.com>
parents: 33944
diff changeset
350 finally:
904771bef3f4 _OutputRedirectingPdb.trace_dispatch(): Return the base class's
Tim Peters <tim.peters@gmail.com>
parents: 33944
diff changeset
351 sys.stdout = save_stdout
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
352
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
353 # [XX] Normalize with respect to os.path.pardir?
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
354 def _module_relative_path(module, path):
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
355 if not inspect.ismodule(module):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42973
diff changeset
356 raise TypeError('Expected a module: %r' % module)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
357 if path.startswith('/'):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42973
diff changeset
358 raise ValueError('Module-relative files may not have absolute paths')
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
359
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
360 # Find the base directory for the path.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
361 if hasattr(module, '__file__'):
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
362 # A normal module/package
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
363 basedir = os.path.split(module.__file__)[0]
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
364 elif module.__name__ == '__main__':
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
365 # An interactive session.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
366 if len(sys.argv)>0 and sys.argv[0] != '':
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
367 basedir = os.path.split(sys.argv[0])[0]
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
368 else:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
369 basedir = os.curdir
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
370 else:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
371 # A module w/o __file__ (this includes builtins)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
372 raise ValueError("Can't resolve paths relative to the module " +
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
373 module + " (it has no __file__)")
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
374
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
375 # Combine the base directory and the path.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
376 return os.path.join(basedir, *(path.split('/')))
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
377
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
378 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
379 ## 2. Example & DocTest
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
380 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
381 ## - An "example" is a <source, want> pair, where "source" is a
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
382 ## fragment of source code, and "want" is the expected output for
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
383 ## "source." The Example class also includes information about
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
384 ## where the example was extracted from.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
385 ##
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
386 ## - A "doctest" is a collection of examples, typically extracted from
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
387 ## a string (such as an object's docstring). The DocTest class also
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
388 ## includes information about where the string was extracted from.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
389
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
390 class Example:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
391 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
392 A single doctest example, consisting of source code and expected
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
393 output. `Example` defines the following attributes:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
394
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
395 - source: A single Python statement, always ending with a newline.
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
396 The constructor adds a newline if needed.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
397
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
398 - want: The expected output from running the source code (either
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
399 from stdout, or a traceback in case of exception). `want` ends
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
400 with a newline unless it's empty, in which case it's an empty
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
401 string. The constructor adds a newline if needed.
20213
d053726fb01b SF patch [#466616] Exclude imported items from doctest,
Tim Peters <tim.peters@gmail.com>
parents: 19359
diff changeset
402
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
403 - exc_msg: The exception message generated by the example, if
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
404 the example is expected to generate an exception; or `None` if
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
405 it is not expected to generate an exception. This exception
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
406 message is compared against the return value of
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
407 `traceback.format_exception_only()`. `exc_msg` ends with a
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
408 newline unless it's `None`. The constructor adds a newline
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
409 if needed.
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
410
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
411 - lineno: The line number within the DocTest string containing
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
412 this Example where the Example begins. This line number is
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
413 zero-based, with respect to the beginning of the DocTest.
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
414
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
415 - indent: The example's indentation in the DocTest string.
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
416 I.e., the number of space characters that preceed the
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
417 example's first prompt.
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
418
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
419 - options: A dictionary mapping from option flags to True or
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
420 False, which is used to override default options for this
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
421 example. Any option flags not contained in this dictionary
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
422 are left at their default value (as specified by the
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
423 DocTestRunner's optionflags). By default, no options are set.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
424 """
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
425 def __init__(self, source, want, exc_msg=None, lineno=0, indent=0,
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
426 options=None):
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
427 # Normalize inputs.
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
428 if not source.endswith('\n'):
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
429 source += '\n'
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
430 if want and not want.endswith('\n'):
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
431 want += '\n'
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
432 if exc_msg is not None and not exc_msg.endswith('\n'):
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
433 exc_msg += '\n'
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
434 # Store properties.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
435 self.source = source
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
436 self.want = want
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
437 self.lineno = lineno
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
438 self.indent = indent
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
439 if options is None: options = {}
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
440 self.options = options
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
441 self.exc_msg = exc_msg
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
442
74047
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
443 def __eq__(self, other):
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
444 if type(self) is not type(other):
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
445 return NotImplemented
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
446
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
447 return self.source == other.source and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
448 self.want == other.want and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
449 self.lineno == other.lineno and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
450 self.indent == other.indent and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
451 self.options == other.options and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
452 self.exc_msg == other.exc_msg
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
453
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
454 def __ne__(self, other):
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
455 return not self == other
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
456
74056
e37c71698409 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74047
diff changeset
457 def __hash__(self):
e37c71698409 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74047
diff changeset
458 return hash((self.source, self.want, self.lineno, self.indent,
e37c71698409 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74047
diff changeset
459 self.exc_msg))
e37c71698409 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74047
diff changeset
460
74047
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
461
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
462 class DocTest:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
463 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
464 A collection of doctest examples that should be run in a single
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
465 namespace. Each `DocTest` defines the following attributes:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
466
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
467 - examples: the list of examples.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
468
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
469 - globs: The namespace (aka globals) that the examples should
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
470 be run in.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
471
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
472 - name: A name identifying the DocTest (typically, the name of
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
473 the object whose docstring this DocTest was extracted from).
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
474
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
475 - filename: The name of the file that this DocTest was extracted
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
476 from, or `None` if the filename is unknown.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
477
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
478 - lineno: The line number within filename where this DocTest
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
479 begins, or `None` if the line number is unavailable. This
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
480 line number is zero-based, with respect to the beginning of
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
481 the file.
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
482
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
483 - docstring: The string that the examples were extracted from,
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
484 or `None` if the string is unavailable.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
485 """
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
486 def __init__(self, examples, globs, name, filename, lineno, docstring):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
487 """
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
488 Create a new DocTest containing the given examples. The
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
489 DocTest's globals are initialized with a copy of `globs`.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
490 """
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43547
diff changeset
491 assert not isinstance(examples, str), \
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
492 "DocTest no longer accepts str; use DocTestParser instead"
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
493 self.examples = examples
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
494 self.docstring = docstring
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
495 self.globs = globs.copy()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
496 self.name = name
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
497 self.filename = filename
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
498 self.lineno = lineno
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
499
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
500 def __repr__(self):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
501 if len(self.examples) == 0:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
502 examples = 'no examples'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
503 elif len(self.examples) == 1:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
504 examples = '1 example'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
505 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
506 examples = '%d examples' % len(self.examples)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
507 return ('<DocTest %s from %s:%s (%s)>' %
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
508 (self.name, self.filename, self.lineno, examples))
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
509
74047
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
510 def __eq__(self, other):
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
511 if type(self) is not type(other):
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
512 return NotImplemented
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
513
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
514 return self.examples == other.examples and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
515 self.docstring == other.docstring and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
516 self.globs == other.globs and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
517 self.name == other.name and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
518 self.filename == other.filename and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
519 self.lineno == other.lineno
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
520
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
521 def __ne__(self, other):
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
522 return not self == other
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
523
74056
e37c71698409 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74047
diff changeset
524 def __hash__(self):
e37c71698409 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74047
diff changeset
525 return hash((self.docstring, self.name, self.filename, self.lineno))
e37c71698409 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74047
diff changeset
526
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
527 # This lets us sort tests by name:
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
528 def __lt__(self, other):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
529 if not isinstance(other, DocTest):
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
530 return NotImplemented
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
531 return ((self.name, self.filename, self.lineno, id(self))
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
532 <
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
533 (other.name, other.filename, other.lineno, id(other)))
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
534
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
535 ######################################################################
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
536 ## 3. DocTestParser
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
537 ######################################################################
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
538
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
539 class DocTestParser:
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
540 """
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
541 A class used to parse strings containing doctest examples.
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
542 """
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
543 # This regular expression is used to find doctest examples in a
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
544 # string. It defines three groups: `source` is the source code
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
545 # (including leading indentation and prompts); `indent` is the
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
546 # indentation of the first (PS1) line of the source code; and
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
547 # `want` is the expected output (including leading indentation).
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
548 _EXAMPLE_RE = re.compile(r'''
33058
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
549 # Source consists of a PS1 line followed by zero or more PS2 lines.
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
550 (?P<source>
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
551 (?:^(?P<indent> [ ]*) >>> .*) # PS1 line
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
552 (?:\n [ ]* \.\.\. .*)*) # PS2 lines
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
553 \n?
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
554 # Want consists of any non-blank lines that do not start with PS1.
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
555 (?P<want> (?:(?![ ]*$) # Not a blank line
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
556 (?![ ]*>>>) # Not a line starting with PS1
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
557 .*$\n? # But any other line
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
558 )*)
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
559 ''', re.MULTILINE | re.VERBOSE)
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
560
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
561 # A regular expression for handling `want` strings that contain
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
562 # expected exceptions. It divides `want` into three pieces:
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
563 # - the traceback header line (`hdr`)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
564 # - the traceback stack (`stack`)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
565 # - the exception message (`msg`), as generated by
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
566 # traceback.format_exception_only()
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
567 # `msg` may have multiple lines. We assume/require that the
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
568 # exception message is the first non-indented line starting with a word
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
569 # character following the traceback header line.
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
570 _EXCEPTION_RE = re.compile(r"""
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
571 # Grab the traceback header. Different versions of Python have
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
572 # said different things on the first traceback line.
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
573 ^(?P<hdr> Traceback\ \(
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
574 (?: most\ recent\ call\ last
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
575 | innermost\ last
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
576 ) \) :
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
577 )
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
578 \s* $ # toss trailing whitespace on the header.
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
579 (?P<stack> .*?) # don't blink: absorb stuff until...
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
580 ^ (?P<msg> \w+ .*) # a line *starts* with alphanum.
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
581 """, re.VERBOSE | re.MULTILINE | re.DOTALL)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
582
33109
450f5e21eac8 Nit in _IS_BLANK_OR_COMMENT comment -- it doesn't matter how this is
Tim Peters <tim.peters@gmail.com>
parents: 33090
diff changeset
583 # A callable returning a true value iff its argument is a blank line
450f5e21eac8 Nit in _IS_BLANK_OR_COMMENT comment -- it doesn't matter how this is
Tim Peters <tim.peters@gmail.com>
parents: 33090
diff changeset
584 # or contains a single comment.
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
585 _IS_BLANK_OR_COMMENT = re.compile(r'^[ ]*(#.*)?$').match
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
586
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
587 def parse(self, string, name='<string>'):
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
588 """
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
589 Divide the given string into examples and intervening text,
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
590 and return them as a list of alternating Examples and strings.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
591 Line numbers for the Examples are 0-based. The optional
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
592 argument `name` is a name identifying this string, and is only
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
593 used for error messages.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
594 """
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
595 string = string.expandtabs()
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
596 # If all lines begin with the same indentation, then strip it.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
597 min_indent = self._min_indent(string)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
598 if min_indent > 0:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
599 string = '\n'.join([l[min_indent:] for l in string.split('\n')])
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
600
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
601 output = []
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
602 charno, lineno = 0, 0
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
603 # Find all doctest examples in the string:
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
604 for m in self._EXAMPLE_RE.finditer(string):
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
605 # Add the pre-example text to `output`.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
606 output.append(string[charno:m.start()])
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
607 # Update lineno (lines before this example)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
608 lineno += string.count('\n', charno, m.start())
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
609 # Extract info from the regexp match.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
610 (source, options, want, exc_msg) = \
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
611 self._parse_example(m, name, lineno)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
612 # Create an Example, and add it to the list.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
613 if not self._IS_BLANK_OR_COMMENT(source):
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
614 output.append( Example(source, want, exc_msg,
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
615 lineno=lineno,
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
616 indent=min_indent+len(m.group('indent')),
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
617 options=options) )
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
618 # Update lineno (lines inside this example)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
619 lineno += string.count('\n', m.start(), m.end())
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
620 # Update charno.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
621 charno = m.end()
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
622 # Add any remaining post-example text to `output`.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
623 output.append(string[charno:])
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
624 return output
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
625
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
626 def get_doctest(self, string, globs, name, filename, lineno):
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
627 """
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
628 Extract all doctest examples from the given string, and
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
629 collect them into a `DocTest` object.
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
630
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
631 `globs`, `name`, `filename`, and `lineno` are attributes for
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
632 the new `DocTest` object. See the documentation for `DocTest`
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
633 for more information.
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
634 """
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
635 return DocTest(self.get_examples(string, name), globs,
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
636 name, filename, lineno, string)
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
637
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
638 def get_examples(self, string, name='<string>'):
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
639 """
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
640 Extract all doctest examples from the given string, and return
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
641 them as a list of `Example` objects. Line numbers are
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
642 0-based, because it's most common in doctests that nothing
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
643 interesting appears on the same line as opening triple-quote,
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
644 and so the first interesting line is called \"line 1\" then.
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
645
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
646 The optional argument `name` is a name identifying this
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
647 string, and is only used for error messages.
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
648 """
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
649 return [x for x in self.parse(string, name)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
650 if isinstance(x, Example)]
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
651
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
652 def _parse_example(self, m, name, lineno):
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
653 """
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
654 Given a regular expression match from `_EXAMPLE_RE` (`m`),
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
655 return a pair `(source, want)`, where `source` is the matched
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
656 example's source code (with prompts and indentation stripped);
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
657 and `want` is the example's expected output (with indentation
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
658 stripped).
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
659
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
660 `name` is the string's name, and `lineno` is the line number
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
661 where the example starts; both are used for error messages.
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
662 """
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
663 # Get the example's indentation level.
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
664 indent = len(m.group('indent'))
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
665
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
666 # Divide source into lines; check that they're properly
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
667 # indented; and then strip their indentation & prompts.
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
668 source_lines = m.group('source').split('\n')
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
669 self._check_prompt_blank(source_lines, indent, name, lineno)
33228
40b5aee20b83 _parse_example(): Simplified new code to preserve trailing spaces before
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
670 self._check_prefix(source_lines[1:], ' '*indent + '.', name, lineno)
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
671 source = '\n'.join([sl[indent+4:] for sl in source_lines])
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
672
33228
40b5aee20b83 _parse_example(): Simplified new code to preserve trailing spaces before
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
673 # Divide want into lines; check that it's properly indented; and
40b5aee20b83 _parse_example(): Simplified new code to preserve trailing spaces before
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
674 # then strip the indentation. Spaces before the last newline should
40b5aee20b83 _parse_example(): Simplified new code to preserve trailing spaces before
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
675 # be preserved, so plain rstrip() isn't good enough.
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
676 want = m.group('want')
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
677 want_lines = want.split('\n')
33228
40b5aee20b83 _parse_example(): Simplified new code to preserve trailing spaces before
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
678 if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]):
40b5aee20b83 _parse_example(): Simplified new code to preserve trailing spaces before
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
679 del want_lines[-1] # forget final newline & spaces after it
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
680 self._check_prefix(want_lines, ' '*indent, name,
33228
40b5aee20b83 _parse_example(): Simplified new code to preserve trailing spaces before
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
681 lineno + len(source_lines))
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
682 want = '\n'.join([wl[indent:] for wl in want_lines])
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
683
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
684 # If `want` contains a traceback message, then extract it.
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
685 m = self._EXCEPTION_RE.match(want)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
686 if m:
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
687 exc_msg = m.group('msg')
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
688 else:
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
689 exc_msg = None
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
690
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
691 # Extract options from the source.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
692 options = self._find_options(source, name, lineno)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
693
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
694 return source, options, want, exc_msg
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
695
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
696 # This regular expression looks for option directives in the
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
697 # source code of an example. Option directives are comments
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
698 # starting with "doctest:". Warning: this may give false
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
699 # positives for string-literals that contain the string
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
700 # "#doctest:". Eliminating these false positives would require
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
701 # actually parsing the string; but we limit them by ignoring any
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
702 # line containing "#doctest:" that is *followed* by a quote mark.
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
703 _OPTION_DIRECTIVE_RE = re.compile(r'#\s*doctest:\s*([^\n\'"]*)$',
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
704 re.MULTILINE)
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
705
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
706 def _find_options(self, source, name, lineno):
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
707 """
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
708 Return a dictionary containing option overrides extracted from
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
709 option directives in the given source string.
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
710
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
711 `name` is the string's name, and `lineno` is the line number
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
712 where the example starts; both are used for error messages.
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
713 """
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
714 options = {}
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
715 # (note: with the current regexp, this will match at most once:)
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
716 for m in self._OPTION_DIRECTIVE_RE.finditer(source):
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
717 option_strings = m.group(1).replace(',', ' ').split()
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
718 for option in option_strings:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
719 if (option[0] not in '+-' or
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
720 option[1:] not in OPTIONFLAGS_BY_NAME):
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
721 raise ValueError('line %r of the doctest for %s '
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
722 'has an invalid option: %r' %
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
723 (lineno+1, name, option))
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
724 flag = OPTIONFLAGS_BY_NAME[option[1:]]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
725 options[flag] = (option[0] == '+')
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
726 if options and self._IS_BLANK_OR_COMMENT(source):
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
727 raise ValueError('line %r of the doctest for %s has an option '
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
728 'directive on a line with no example: %r' %
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
729 (lineno, name, source))
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
730 return options
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
731
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
732 # This regular expression finds the indentation of every non-blank
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
733 # line in a string.
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
734 _INDENT_RE = re.compile('^([ ]*)(?=\S)', re.MULTILINE)
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
735
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
736 def _min_indent(self, s):
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
737 "Return the minimum indentation of any non-blank line in `s`"
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
738 indents = [len(indent) for indent in self._INDENT_RE.findall(s)]
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
739 if len(indents) > 0:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
740 return min(indents)
33059
6f38e3b64ef4 Give return stmts their own lines.
Tim Peters <tim.peters@gmail.com>
parents: 33058
diff changeset
741 else:
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
742 return 0
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
743
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
744 def _check_prompt_blank(self, lines, indent, name, lineno):
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
745 """
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
746 Given the lines of a source string (including prompts and
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
747 leading indentation), check to make sure that every prompt is
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
748 followed by a space character. If any line is not followed by
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
749 a space character, then raise ValueError.
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
750 """
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
751 for i, line in enumerate(lines):
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
752 if len(line) >= indent+4 and line[indent+3] != ' ':
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
753 raise ValueError('line %r of the docstring for %s '
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
754 'lacks blank after %s: %r' %
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
755 (lineno+i+1, name,
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
756 line[indent:indent+3], line))
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
757
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
758 def _check_prefix(self, lines, prefix, name, lineno):
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
759 """
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
760 Check that every line in the given list starts with the given
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
761 prefix; if any line does not, then raise a ValueError.
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
762 """
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
763 for i, line in enumerate(lines):
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
764 if line and not line.startswith(prefix):
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
765 raise ValueError('line %r of the docstring for %s has '
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
766 'inconsistent leading whitespace: %r' %
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
767 (lineno+i+1, name, line))
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
768
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
769
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
770 ######################################################################
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
771 ## 4. DocTest Finder
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
772 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
773
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
774 class DocTestFinder:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
775 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
776 A class used to extract the DocTests that are relevant to a given
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
777 object, from its docstring and the docstrings of its contained
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
778 objects. Doctests can currently be extracted from the following
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
779 object types: modules, functions, classes, methods, staticmethods,
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
780 classmethods, and properties.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
781 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
782
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
783 def __init__(self, verbose=False, parser=DocTestParser(),
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
784 recurse=True, exclude_empty=True):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
785 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
786 Create a new doctest finder.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
787
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
788 The optional argument `parser` specifies a class or
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
789 function that should be used to create new DocTest objects (or
33043
1dbd0f345a78 Type in docstring.
Tim Peters <tim.peters@gmail.com>
parents: 33042
diff changeset
790 objects that implement the same interface as DocTest). The
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
791 signature for this factory function should match the signature
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
792 of the DocTest constructor.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
793
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
794 If the optional argument `recurse` is false, then `find` will
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
795 only examine the given object, and not any contained objects.
33469
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
796
33473
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
797 If the optional argument `exclude_empty` is false, then `find`
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
798 will include tests for objects with empty docstrings.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
799 """
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
800 self._parser = parser
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
801 self._verbose = verbose
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
802 self._recurse = recurse
33469
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
803 self._exclude_empty = exclude_empty
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
804
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
805 def find(self, obj, name=None, module=None, globs=None, extraglobs=None):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
806 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
807 Return a list of the DocTests that are defined by the given
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
808 object's docstring, or by any of its contained objects'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
809 docstrings.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
810
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
811 The optional parameter `module` is the module that contains
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
812 the given object. If the module is not specified or is None, then
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
813 the test finder will attempt to automatically determine the
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
814 correct module. The object's module is used:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
815
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
816 - As a default namespace, if `globs` is not specified.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
817 - To prevent the DocTestFinder from extracting DocTests
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
818 from objects that are imported from other modules.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
819 - To find the name of the file containing the object.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
820 - To help find the line number of the object within its
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
821 file.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
822
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
823 Contained objects whose module does not match `module` are ignored.
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
824
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
825 If `module` is False, no attempt to find the module will be made.
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
826 This is obscure, of use mostly in tests: if `module` is False, or
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
827 is None but cannot be found automatically, then all objects are
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
828 considered to belong to the (non-existent) module, so all contained
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
829 objects will (recursively) be searched for doctests.
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
830
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
831 The globals for each DocTest is formed by combining `globs`
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
832 and `extraglobs` (bindings in `extraglobs` override bindings
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
833 in `globs`). A new copy of the globals dictionary is created
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
834 for each DocTest. If `globs` is not specified, then it
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
835 defaults to the module's `__dict__`, if specified, or {}
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
836 otherwise. If `extraglobs` is not specified, then it defaults
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
837 to {}.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
838
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
839 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
840 # If name was not specified, then extract it from the object.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
841 if name is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
842 name = getattr(obj, '__name__', None)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
843 if name is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
844 raise ValueError("DocTestFinder.find: name must be given "
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
845 "when obj.__name__ doesn't exist: %r" %
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
846 (type(obj),))
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
847
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
848 # Find the module that contains the given object (if obj is
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
849 # a module, then module=obj.). Note: this may fail, in which
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
850 # case module will be None.
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
851 if module is False:
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
852 module = None
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
853 elif module is None:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
854 module = inspect.getmodule(obj)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
855
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
856 # Read the module's source code. This is used by
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
857 # DocTestFinder._find_lineno to find the line number for a
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
858 # given object's docstring.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
859 try:
54712
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
860 file = inspect.getsourcefile(obj)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
861 except TypeError:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
862 source_lines = None
54712
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
863 else:
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
864 if not file:
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
865 # Check to see if it's one of our special internal "files"
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
866 # (see __patched_linecache_getlines).
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
867 file = inspect.getfile(obj)
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
868 if not file[0]+file[-2:] == '<]>': file = None
55606
ff6eea017a7f Merged revisions 74010,74034,74054 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54712
diff changeset
869 if file is None:
ff6eea017a7f Merged revisions 74010,74034,74054 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54712
diff changeset
870 source_lines = None
54712
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
871 else:
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
872 if module is not None:
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
873 # Supply the module globals in case the module was
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
874 # originally loaded via a PEP 302 loader and
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
875 # file is not a valid filesystem path
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
876 source_lines = linecache.getlines(file, module.__dict__)
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
877 else:
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
878 # No access to a loader, so assume it's a normal
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
879 # filesystem path
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
880 source_lines = linecache.getlines(file)
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
881 if not source_lines:
f57680748ecb Issue #6195: fix doctest to no longer try to read 'source' data from
R. David Murray <rdmurray@bitdance.com>
parents: 51112
diff changeset
882 source_lines = None
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
883
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
884 # Initialize globals, and merge in extraglobs.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
885 if globs is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
886 if module is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
887 globs = {}
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
888 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
889 globs = module.__dict__.copy()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
890 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
891 globs = globs.copy()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
892 if extraglobs is not None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
893 globs.update(extraglobs)
51112
8cd3ced1ed29 Issue 5021: doctest.testfile should set __name__
Raymond Hettinger <python@rcn.com>
parents: 50187
diff changeset
894 if '__name__' not in globs:
8cd3ced1ed29 Issue 5021: doctest.testfile should set __name__
Raymond Hettinger <python@rcn.com>
parents: 50187
diff changeset
895 globs['__name__'] = '__main__' # provide a default module name
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
896
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
897 # Recursively expore `obj`, extracting DocTests.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
898 tests = []
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
899 self._find(tests, obj, name, module, source_lines, globs, {})
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
900 # Sort the tests by alpha order of names, for consistency in
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
901 # verbose-mode output. This was a feature of doctest in Pythons
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
902 # <= 2.3 that got lost by accident in 2.4. It was repaired in
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
903 # 2.4.4 and 2.5.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
904 tests.sort()
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
905 return tests
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
906
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
907 def _from_module(self, module, object):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
908 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
909 Return true if the given object is defined in the given
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
910 module.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
911 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
912 if module is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
913 return True
49772
088fdadf9609 Merged revisions 67243,67245,67277-67278,67289 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48583
diff changeset
914 elif inspect.getmodule(object) is not None:
088fdadf9609 Merged revisions 67243,67245,67277-67278,67289 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48583
diff changeset
915 return module is inspect.getmodule(object)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
916 elif inspect.isfunction(object):
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40799
diff changeset
917 return module.__dict__ is object.__globals__
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
918 elif inspect.isclass(object):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
919 return module.__name__ == object.__module__
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
920 elif hasattr(object, '__module__'):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
921 return module.__name__ == object.__module__
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
922 elif isinstance(object, property):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
923 return True # [XX] no way not be sure.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
924 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
925 raise ValueError("object must be a class or function")
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
926
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
927 def _find(self, tests, obj, name, module, source_lines, globs, seen):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
928 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
929 Find tests for the given object and any contained objects, and
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
930 add them to `tests`.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
931 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
932 if self._verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
933 print('Finding tests in %s' % name)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
934
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
935 # If we've already processed this object, then ignore it.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
936 if id(obj) in seen:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
937 return
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
938 seen[id(obj)] = 1
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
939
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
940 # Find a test for this object, and add it to the list of tests.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
941 test = self._get_test(obj, name, module, globs, source_lines)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
942 if test is not None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
943 tests.append(test)
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
944
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
945 # Look for tests in a module's contained objects.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
946 if inspect.ismodule(obj) and self._recurse:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
947 for valname, val in obj.__dict__.items():
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
948 valname = '%s.%s' % (name, valname)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
949 # Recurse to functions & classes.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
950 if ((inspect.isfunction(val) or inspect.isclass(val)) and
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
951 self._from_module(module, val)):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
952 self._find(tests, val, valname, module, source_lines,
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
953 globs, seen)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
954
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
955 # Look for tests in a module's __test__ dictionary.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
956 if inspect.ismodule(obj) and self._recurse:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
957 for valname, val in getattr(obj, '__test__', {}).items():
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43547
diff changeset
958 if not isinstance(valname, str):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
959 raise ValueError("DocTestFinder.find: __test__ keys "
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
960 "must be strings: %r" %
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
961 (type(valname),))
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
962 if not (inspect.isfunction(val) or inspect.isclass(val) or
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
963 inspect.ismethod(val) or inspect.ismodule(val) or
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43547
diff changeset
964 isinstance(val, str)):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
965 raise ValueError("DocTestFinder.find: __test__ values "
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
966 "must be strings, functions, methods, "
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
967 "classes, or modules: %r" %
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
968 (type(val),))
33468
a04515a98831 DocTestFinder._find(): for tests derived from a module __test__ global,
Tim Peters <tim.peters@gmail.com>
parents: 33467
diff changeset
969 valname = '%s.__test__.%s' % (name, valname)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
970 self._find(tests, val, valname, module, source_lines,
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
971 globs, seen)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
972
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
973 # Look for tests in a class's contained objects.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
974 if inspect.isclass(obj) and self._recurse:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
975 for valname, val in obj.__dict__.items():
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
976 # Special handling for staticmethod/classmethod.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
977 if isinstance(val, staticmethod):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
978 val = getattr(obj, valname)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
979 if isinstance(val, classmethod):
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 44025
diff changeset
980 val = getattr(obj, valname).__func__
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
981
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
982 # Recurse to methods, properties, and nested classes.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
983 if ((inspect.isfunction(val) or inspect.isclass(val) or
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
984 isinstance(val, property)) and
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
985 self._from_module(module, val)):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
986 valname = '%s.%s' % (name, valname)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
987 self._find(tests, val, valname, module, source_lines,
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
988 globs, seen)
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
989
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
990 def _get_test(self, obj, name, module, globs, source_lines):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
991 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
992 Return a DocTest for the given object, if it defines a docstring;
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
993 otherwise, return None.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
994 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
995 # Extract the object's docstring. If it doesn't have one,
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
996 # then return None (no test for this object).
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43547
diff changeset
997 if isinstance(obj, str):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
998 docstring = obj
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
999 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1000 try:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1001 if obj.__doc__ is None:
33469
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
1002 docstring = ''
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
1003 else:
33746
a27b0271711b Fixed a small bug. doctest didn't handle unicode docstrings containing
Jim Fulton <jim@zope.com>
parents: 33618
diff changeset
1004 docstring = obj.__doc__
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43547
diff changeset
1005 if not isinstance(docstring, str):
33746
a27b0271711b Fixed a small bug. doctest didn't handle unicode docstrings containing
Jim Fulton <jim@zope.com>
parents: 33618
diff changeset
1006 docstring = str(docstring)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1007 except (TypeError, AttributeError):
33469
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
1008 docstring = ''
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1009
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1010 # Find the docstring's location in the file.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1011 lineno = self._find_lineno(obj, source_lines)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1012
33469
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
1013 # Don't bother if the docstring is empty.
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
1014 if self._exclude_empty and not docstring:
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
1015 return None
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
1016
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1017 # Return a DocTest for this object.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1018 if module is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1019 filename = None
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1020 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1021 filename = getattr(module, '__file__', module.__name__)
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1022 if filename[-4:] in (".pyc", ".pyo"):
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1023 filename = filename[:-1]
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
1024 return self._parser.get_doctest(docstring, globs, name,
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
1025 filename, lineno)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1026
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1027 def _find_lineno(self, obj, source_lines):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1028 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1029 Return a line number of the given object's docstring. Note:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1030 this method assumes that the object has a docstring.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1031 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1032 lineno = None
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1033
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1034 # Find the line number for modules.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1035 if inspect.ismodule(obj):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1036 lineno = 0
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1037
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1038 # Find the line number for classes.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1039 # Note: this could be fooled if a class is defined multiple
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1040 # times in a single file.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1041 if inspect.isclass(obj):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1042 if source_lines is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1043 return None
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1044 pat = re.compile(r'^\s*class\s*%s\b' %
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1045 getattr(obj, '__name__', '-'))
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1046 for i, line in enumerate(source_lines):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1047 if pat.match(line):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1048 lineno = i
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1049 break
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1050
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1051 # Find the line number for functions & methods.
44026
48af6375207e Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents: 44025
diff changeset
1052 if inspect.ismethod(obj): obj = obj.__func__
40910
eaaee785eb86 Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents: 40799
diff changeset
1053 if inspect.isfunction(obj): obj = obj.__code__
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1054 if inspect.istraceback(obj): obj = obj.tb_frame
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1055 if inspect.isframe(obj): obj = obj.f_code
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1056 if inspect.iscode(obj):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1057 lineno = getattr(obj, 'co_firstlineno', None)-1
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1058
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1059 # Find the line number where the docstring starts. Assume
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1060 # that it's the first line that begins with a quote mark.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1061 # Note: this could be fooled by a multiline function
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1062 # signature, where a continuation line begins with a quote
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1063 # mark.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1064 if lineno is not None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1065 if source_lines is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1066 return lineno+1
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1067 pat = re.compile('(^|.*:)\s*\w*("|\')')
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1068 for lineno in range(lineno, len(source_lines)):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1069 if pat.match(source_lines[lineno]):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1070 return lineno
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1071
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1072 # We couldn't find the line number.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1073 return None
29623
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
1074
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1075 ######################################################################
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
1076 ## 5. DocTest Runner
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1077 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1078
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1079 class DocTestRunner:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1080 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1081 A class used to run DocTest test cases, and accumulate statistics.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1082 The `run` method is used to process a single DocTest case. It
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1083 returns a tuple `(f, t)`, where `t` is the number of test cases
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1084 tried, and `f` is the number of test cases that failed.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1085
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1086 >>> tests = DocTestFinder().find(_TestClass)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1087 >>> runner = DocTestRunner(verbose=False)
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1088 >>> tests.sort(key = lambda test: test.name)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1089 >>> for test in tests:
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1090 ... print(test.name, '->', runner.run(test))
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
1091 _TestClass -> TestResults(failed=0, attempted=2)
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
1092 _TestClass.__init__ -> TestResults(failed=0, attempted=2)
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
1093 _TestClass.get -> TestResults(failed=0, attempted=2)
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
1094 _TestClass.square -> TestResults(failed=0, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1095
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1096 The `summarize` method prints a summary of all the test cases that
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1097 have been run by the runner, and returns an aggregated `(f, t)`
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1098 tuple:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1099
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1100 >>> runner.summarize(verbose=1)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1101 4 items passed all tests:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1102 2 tests in _TestClass
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1103 2 tests in _TestClass.__init__
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1104 2 tests in _TestClass.get
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1105 1 tests in _TestClass.square
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1106 7 tests in 4 items.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1107 7 passed and 0 failed.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1108 Test passed.
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
1109 TestResults(failed=0, attempted=7)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1110
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1111 The aggregated number of tried examples and failed examples is
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1112 also available via the `tries` and `failures` attributes:
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1113
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1114 >>> runner.tries
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1115 7
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1116 >>> runner.failures
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1117 0
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1118
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1119 The comparison between expected outputs and actual outputs is done
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1120 by an `OutputChecker`. This comparison may be customized with a
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1121 number of option flags; see the documentation for `testmod` for
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1122 more information. If the option flags are insufficient, then the
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1123 comparison may also be customized by passing a subclass of
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1124 `OutputChecker` to the constructor.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1125
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1126 The test runner's display output can be controlled in two ways.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1127 First, an output function (`out) can be passed to
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1128 `TestRunner.run`; this function will be called with strings that
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1129 should be displayed. It defaults to `sys.stdout.write`. If
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1130 capturing the output is not sufficient, then the display output
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1131 can be also customized by subclassing DocTestRunner, and
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1132 overriding the methods `report_start`, `report_success`,
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1133 `report_unexpected_exception`, and `report_failure`.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1134 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1135 # This divider string is used to separate failure messages, and to
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1136 # separate sections of the summary.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1137 DIVIDER = "*" * 70
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1138
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1139 def __init__(self, checker=None, verbose=None, optionflags=0):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1140 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1141 Create a new test runner.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1142
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1143 Optional keyword arg `checker` is the `OutputChecker` that
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1144 should be used to compare the expected outputs and actual
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1145 outputs of doctest examples.
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1146
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1147 Optional keyword arg 'verbose' prints lots of stuff if true,
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1148 only failures if false; by default, it's true iff '-v' is in
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1149 sys.argv.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1150
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1151 Optional argument `optionflags` can be used to control how the
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1152 test runner compares expected output to actual output, and how
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1153 it displays failures. See the documentation for `testmod` for
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1154 more information.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1155 """
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1156 self._checker = checker or OutputChecker()
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1157 if verbose is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1158 verbose = '-v' in sys.argv
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1159 self._verbose = verbose
29623
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
1160 self.optionflags = optionflags
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1161 self.original_optionflags = optionflags
29623
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
1162
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1163 # Keep track of the examples we've run.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1164 self.tries = 0
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1165 self.failures = 0
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1166 self._name2ft = {}
19359
eff8ca679bf6 Remove the horrid generators hack from doctest.py. This relies on a
Tim Peters <tim.peters@gmail.com>
parents: 18633
diff changeset
1167
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1168 # Create a fake output target for capturing doctest output.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1169 self._fakeout = _SpoofOut()
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1170
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1171 #/////////////////////////////////////////////////////////////////
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1172 # Reporting methods
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1173 #/////////////////////////////////////////////////////////////////
20227
92f86436b6be SF bug [#467336] doctest failures w/ new-style classes.
Tim Peters <tim.peters@gmail.com>
parents: 20222
diff changeset
1174
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1175 def report_start(self, out, test, example):
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1176 """
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1177 Report that the test runner is about to process the given
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1178 example. (Only displays a message if verbose=True)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1179 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1180 if self._verbose:
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1181 if example.want:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1182 out('Trying:\n' + _indent(example.source) +
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1183 'Expecting:\n' + _indent(example.want))
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1184 else:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1185 out('Trying:\n' + _indent(example.source) +
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1186 'Expecting nothing\n')
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1187
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1188 def report_success(self, out, test, example, got):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1189 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1190 Report that the given example ran successfully. (Only
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1191 displays a message if verbose=True)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1192 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1193 if self._verbose:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1194 out("ok\n")
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1195
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1196 def report_failure(self, out, test, example, got):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1197 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1198 Report that the given example failed.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1199 """
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
1200 out(self._failure_header(test, example) +
33287
449141aa011c Changed OutputChecker.output_difference to expect an Example object,
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33286
diff changeset
1201 self._checker.output_difference(example, got, self.optionflags))
20213
d053726fb01b SF patch [#466616] Exclude imported items from doctest,
Tim Peters <tim.peters@gmail.com>
parents: 19359
diff changeset
1202
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1203 def report_unexpected_exception(self, out, test, example, exc_info):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1204 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1205 Report that the given example raised an unexpected exception.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1206 """
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
1207 out(self._failure_header(test, example) +
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1208 'Exception raised:\n' + _indent(_exception_traceback(exc_info)))
20213
d053726fb01b SF patch [#466616] Exclude imported items from doctest,
Tim Peters <tim.peters@gmail.com>
parents: 19359
diff changeset
1209
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
1210 def _failure_header(self, test, example):
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1211 out = [self.DIVIDER]
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1212 if test.filename:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1213 if test.lineno is not None and example.lineno is not None:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1214 lineno = test.lineno + example.lineno + 1
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1215 else:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1216 lineno = '?'
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1217 out.append('File "%s", line %s, in %s' %
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1218 (test.filename, lineno, test.name))
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1219 else:
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1220 out.append('Line %s, in %s' % (example.lineno+1, test.name))
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1221 out.append('Failed example:')
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1222 source = example.source
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1223 out.append(_indent(source))
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1224 return '\n'.join(out)
20213
d053726fb01b SF patch [#466616] Exclude imported items from doctest,
Tim Peters <tim.peters@gmail.com>
parents: 19359
diff changeset
1225
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1226 #/////////////////////////////////////////////////////////////////
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1227 # DocTest Running
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1228 #/////////////////////////////////////////////////////////////////
20213
d053726fb01b SF patch [#466616] Exclude imported items from doctest,
Tim Peters <tim.peters@gmail.com>
parents: 19359
diff changeset
1229
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1230 def __run(self, test, compileflags, out):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1231 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1232 Run the examples in `test`. Write the outcome of each example
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1233 with one of the `DocTestRunner.report_*` methods, using the
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1234 writer function `out`. `compileflags` is the set of compiler
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1235 flags that should be used to execute examples. Return a tuple
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1236 `(f, t)`, where `t` is the number of examples tried, and `f`
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1237 is the number of examples that failed. The examples are run
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1238 in the namespace `test.globs`.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1239 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1240 # Keep track of the number of failures and tries.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1241 failures = tries = 0
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1242
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1243 # Save the option flags (since option directives can be used
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1244 # to modify them).
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1245 original_optionflags = self.optionflags
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1246
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1247 SUCCESS, FAILURE, BOOM = range(3) # `outcome` state
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1248
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1249 check = self._checker.check_output
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1250
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1251 # Process each example.
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1252 for examplenum, example in enumerate(test.examples):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1253
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65338
diff changeset
1254 # If REPORT_ONLY_FIRST_FAILURE is set, then suppress
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1255 # reporting after the first failure.
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1256 quiet = (self.optionflags & REPORT_ONLY_FIRST_FAILURE and
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1257 failures > 0)
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1258
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1259 # Merge in the example's options.
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1260 self.optionflags = original_optionflags
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1261 if example.options:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1262 for (optionflag, val) in example.options.items():
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1263 if val:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1264 self.optionflags |= optionflag
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1265 else:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1266 self.optionflags &= ~optionflag
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1267
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1268 # If 'SKIP' is set, then skip this example.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1269 if self.optionflags & SKIP:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1270 continue
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1271
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1272 # Record that we started this example.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1273 tries += 1
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1274 if not quiet:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1275 self.report_start(out, test, example)
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1276
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1277 # Use a special filename for compile(), so we can retrieve
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1278 # the source code during interactive debugging (see
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1279 # __patched_linecache_getlines).
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1280 filename = '<doctest %s[%d]>' % (test.name, examplenum)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1281
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1282 # Run the example in the given context (globs), and record
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1283 # any exception that gets raised. (But don't intercept
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1284 # keyboard interrupts.)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1285 try:
33061
ee43ca4a2c23 Repair some out-of-date comments.
Tim Peters <tim.peters@gmail.com>
parents: 33060
diff changeset
1286 # Don't blink! This is where the user's code gets run.
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39699
diff changeset
1287 exec(compile(example.source, filename, "single",
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39699
diff changeset
1288 compileflags, 1), test.globs)
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1289 self.debugger.set_continue() # ==== Example Finished ====
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1290 exception = None
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1291 except KeyboardInterrupt:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1292 raise
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1293 except:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1294 exception = sys.exc_info()
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1295 self.debugger.set_continue() # ==== Example Finished ====
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1296
33061
ee43ca4a2c23 Repair some out-of-date comments.
Tim Peters <tim.peters@gmail.com>
parents: 33060
diff changeset
1297 got = self._fakeout.getvalue() # the actual output
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1298 self._fakeout.truncate(0)
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1299 outcome = FAILURE # guilty until proved innocent or insane
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1300
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1301 # If the example executed without raising any exceptions,
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1302 # verify its output.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1303 if exception is None:
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1304 if check(example.want, got, self.optionflags):
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1305 outcome = SUCCESS
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1306
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1307 # The example raised an exception: check if it was expected.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1308 else:
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47463
diff changeset
1309 exc_msg = traceback.format_exception_only(*exception[:2])[-1]
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1310 if not quiet:
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47463
diff changeset
1311 got += _exception_traceback(exception)
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1312
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1313 # If `example.exc_msg` is None, then we weren't expecting
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1314 # an exception.
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
1315 if example.exc_msg is None:
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1316 outcome = BOOM
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1317
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1318 # We expected an exception: see whether it matches.
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1319 elif check(example.exc_msg, exc_msg, self.optionflags):
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1320 outcome = SUCCESS
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1321
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1322 # Another chance if they didn't care about the detail.
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1323 elif self.optionflags & IGNORE_EXCEPTION_DETAIL:
62293
606bad584344 Merged revisions 80578 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 60518
diff changeset
1324 m1 = re.match(r'(?:[^:]*\.)?([^:]*:)', example.exc_msg)
606bad584344 Merged revisions 80578 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 60518
diff changeset
1325 m2 = re.match(r'(?:[^:]*\.)?([^:]*:)', exc_msg)
606bad584344 Merged revisions 80578 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 60518
diff changeset
1326 if m1 and m2 and check(m1.group(1), m2.group(1),
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1327 self.optionflags):
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1328 outcome = SUCCESS
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1329
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1330 # Report the outcome.
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1331 if outcome is SUCCESS:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1332 if not quiet:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1333 self.report_success(out, test, example, got)
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1334 elif outcome is FAILURE:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1335 if not quiet:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1336 self.report_failure(out, test, example, got)
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1337 failures += 1
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1338 elif outcome is BOOM:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1339 if not quiet:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1340 self.report_unexpected_exception(out, test, example,
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47463
diff changeset
1341 exception)
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1342 failures += 1
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1343 else:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1344 assert False, ("unknown outcome", outcome)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1345
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1346 # Restore the option flags (in case they were modified)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1347 self.optionflags = original_optionflags
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1348
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1349 # Record and return the number of failures and tries.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1350 self.__record_outcome(test, failures, tries)
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
1351 return TestResults(failures, tries)
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1352
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1353 def __record_outcome(self, test, f, t):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1354 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1355 Record the fact that the given DocTest (`test`) generated `f`
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1356 failures out of `t` tried examples.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1357 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1358 f2, t2 = self._name2ft.get(test.name, (0,0))
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1359 self._name2ft[test.name] = (f+f2, t+t2)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1360 self.failures += f
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1361 self.tries += t
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1362
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1363 __LINECACHE_FILENAME_RE = re.compile(r'<doctest '
65329
052c199ae19a Fix the regex to match all kind of filenames, for interactive debugging in doctests. (issue #9409)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64076
diff changeset
1364 r'(?P<name>.+)'
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1365 r'\[(?P<examplenum>\d+)\]>$')
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35167
diff changeset
1366 def __patched_linecache_getlines(self, filename, module_globals=None):
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1367 m = self.__LINECACHE_FILENAME_RE.match(filename)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1368 if m and m.group('name') == self.test.name:
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1369 example = self.test.examples[int(m.group('examplenum'))]
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1370 return example.source.splitlines(True)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1371 else:
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35167
diff changeset
1372 return self.save_linecache_getlines(filename, module_globals)
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1373
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1374 def run(self, test, compileflags=None, out=None, clear_globs=True):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1375 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1376 Run the examples in `test`, and display the results using the
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1377 writer function `out`.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1378
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1379 The examples are run in the namespace `test.globs`. If
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1380 `clear_globs` is true (the default), then this namespace will
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1381 be cleared after the test runs, to help with garbage
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1382 collection. If you would like to examine the namespace after
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1383 the test completes, then use `clear_globs=False`.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1384
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1385 `compileflags` gives the set of flags that should be used by
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1386 the Python compiler when running the examples. If not
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1387 specified, then it will default to the set of future-import
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1388 flags that apply to `globs`.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1389
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1390 The output of each example is checked using
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1391 `DocTestRunner.check_output`, and the results are formatted by
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1392 the `DocTestRunner.report_*` methods.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1393 """
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1394 self.test = test
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1395
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1396 if compileflags is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1397 compileflags = _extract_future_flags(test.globs)
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33062
diff changeset
1398
33073
bfcc4e39fc45 Edward's latest checkins somehow managed to wipe out my previous latest
Tim Peters <tim.peters@gmail.com>
parents: 33072
diff changeset
1399 save_stdout = sys.stdout
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1400 if out is None:
59181
815092a5e5c1 Merged revisions 78493 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58630
diff changeset
1401 encoding = save_stdout.encoding
815092a5e5c1 Merged revisions 78493 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58630
diff changeset
1402 if encoding is None or encoding.lower() == 'utf-8':
815092a5e5c1 Merged revisions 78493 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58630
diff changeset
1403 out = save_stdout.write
815092a5e5c1 Merged revisions 78493 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58630
diff changeset
1404 else:
815092a5e5c1 Merged revisions 78493 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58630
diff changeset
1405 # Use backslashreplace error handling on write
815092a5e5c1 Merged revisions 78493 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58630
diff changeset
1406 def out(s):
815092a5e5c1 Merged revisions 78493 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58630
diff changeset
1407 s = str(s.encode(encoding, 'backslashreplace'), encoding)
815092a5e5c1 Merged revisions 78493 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58630
diff changeset
1408 save_stdout.write(s)
33073
bfcc4e39fc45 Edward's latest checkins somehow managed to wipe out my previous latest
Tim Peters <tim.peters@gmail.com>
parents: 33072
diff changeset
1409 sys.stdout = self._fakeout
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1410
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1411 # Patch pdb.set_trace to restore sys.stdout during interactive
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1412 # debugging (so it's not still redirected to self._fakeout).
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1413 # Note that the interactive output will go to *our*
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1414 # save_stdout, even if that's not the real sys.stdout; this
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1415 # allows us to write test cases for the set_trace behavior.
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1416 save_set_trace = pdb.set_trace
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1417 self.debugger = _OutputRedirectingPdb(save_stdout)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1418 self.debugger.reset()
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1419 pdb.set_trace = self.debugger.set_trace
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33062
diff changeset
1420
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1421 # Patch linecache.getlines, so we can see the example's source
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1422 # when we're inside the debugger.
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1423 self.save_linecache_getlines = linecache.getlines
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1424 linecache.getlines = self.__patched_linecache_getlines
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1425
63453
4903e7577454 Merged revisions 83259,83261,83264-83265,83268-83269,83271-83272,83281 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59181
diff changeset
1426 # Make sure sys.displayhook just prints the value to stdout
4903e7577454 Merged revisions 83259,83261,83264-83265,83268-83269,83271-83272,83281 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59181
diff changeset
1427 save_displayhook = sys.displayhook
4903e7577454 Merged revisions 83259,83261,83264-83265,83268-83269,83271-83272,83281 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59181
diff changeset
1428 sys.displayhook = sys.__displayhook__
4903e7577454 Merged revisions 83259,83261,83264-83265,83268-83269,83271-83272,83281 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59181
diff changeset
1429
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1430 try:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1431 return self.__run(test, compileflags, out)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1432 finally:
33073
bfcc4e39fc45 Edward's latest checkins somehow managed to wipe out my previous latest
Tim Peters <tim.peters@gmail.com>
parents: 33072
diff changeset
1433 sys.stdout = save_stdout
bfcc4e39fc45 Edward's latest checkins somehow managed to wipe out my previous latest
Tim Peters <tim.peters@gmail.com>
parents: 33072
diff changeset
1434 pdb.set_trace = save_set_trace
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1435 linecache.getlines = self.save_linecache_getlines
63453
4903e7577454 Merged revisions 83259,83261,83264-83265,83268-83269,83271-83272,83281 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59181
diff changeset
1436 sys.displayhook = save_displayhook
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1437 if clear_globs:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1438 test.globs.clear()
48583
3a267fe7383c a much cleaner fix from Antoine
Benjamin Peterson <benjamin@python.org>
parents: 48582
diff changeset
1439 import builtins
3a267fe7383c a much cleaner fix from Antoine
Benjamin Peterson <benjamin@python.org>
parents: 48582
diff changeset
1440 builtins._ = None
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1441
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1442 #/////////////////////////////////////////////////////////////////
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1443 # Summarization
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1444 #/////////////////////////////////////////////////////////////////
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1445 def summarize(self, verbose=None):
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1446 """
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1447 Print a summary of all the test cases that have been run by
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1448 this DocTestRunner, and return a tuple `(f, t)`, where `f` is
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1449 the total number of failed examples, and `t` is the total
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1450 number of tried examples.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1451
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1452 The optional `verbose` argument controls how detailed the
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1453 summary is. If the verbosity is not specified, then the
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1454 DocTestRunner's verbosity is used.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1455 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1456 if verbose is None:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1457 verbose = self._verbose
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1458 notests = []
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1459 passed = []
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1460 failed = []
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1461 totalt = totalf = 0
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1462 for x in self._name2ft.items():
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1463 name, (f, t) = x
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1464 assert f <= t
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1465 totalt += t
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1466 totalf += f
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1467 if t == 0:
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1468 notests.append(name)
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1469 elif f == 0:
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1470 passed.append( (name, t) )
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1471 else:
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1472 failed.append(x)
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1473 if verbose:
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1474 if notests:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1475 print(len(notests), "items had no tests:")
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1476 notests.sort()
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1477 for thing in notests:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1478 print(" ", thing)
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1479 if passed:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1480 print(len(passed), "items passed all tests:")
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1481 passed.sort()
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1482 for thing, count in passed:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1483 print(" %3d tests in %s" % (count, thing))
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1484 if failed:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1485 print(self.DIVIDER)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1486 print(len(failed), "items had failures:")
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1487 failed.sort()
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1488 for thing, (f, t) in failed:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1489 print(" %3d of %3d in %s" % (f, t, thing))
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1490 if verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1491 print(totalt, "tests in", len(self._name2ft), "items.")
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1492 print(totalt - totalf, "passed and", totalf, "failed.")
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1493 if totalf:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1494 print("***Test Failed***", totalf, "failures.")
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1495 elif verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1496 print("Test passed.")
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
1497 return TestResults(totalf, totalt)
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1498
33467
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1499 #/////////////////////////////////////////////////////////////////
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1500 # Backward compatibility cruft to maintain doctest.master.
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1501 #/////////////////////////////////////////////////////////////////
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1502 def merge(self, other):
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1503 d = self._name2ft
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1504 for name, (f, t) in other._name2ft.items():
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1505 if name in d:
50187
eb2876ae0476 Merged revisions 67792 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 49772
diff changeset
1506 # Don't print here by default, since doing
eb2876ae0476 Merged revisions 67792 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 49772
diff changeset
1507 # so breaks some of the buildbots
eb2876ae0476 Merged revisions 67792 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 49772
diff changeset
1508 #print("*** DocTestRunner.merge: '" + name + "' in both" \
eb2876ae0476 Merged revisions 67792 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 49772
diff changeset
1509 # " testers; summing outcomes.")
33467
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1510 f2, t2 = d[name]
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1511 f = f + f2
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1512 t = t + t2
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1513 d[name] = f, t
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1514
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1515 class OutputChecker:
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1516 """
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1517 A class used to check the whether the actual output from a doctest
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1518 example matches the expected output. `OutputChecker` defines two
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1519 methods: `check_output`, which compares a given pair of outputs,
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1520 and returns true if they match; and `output_difference`, which
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1521 returns a string describing the differences between two outputs.
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1522 """
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1523 def _toAscii(self, s):
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1524 """
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1525 Convert string to hex-escaped ASCII string.
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1526 """
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1527 return str(s.encode('ASCII', 'backslashreplace'), "ASCII")
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1528
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1529 def check_output(self, want, got, optionflags):
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1530 """
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1531 Return True iff the actual output from an example (`got`)
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1532 matches the expected output (`want`). These strings are
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1533 always considered to match if they are identical; but
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1534 depending on what option flags the test runner is using,
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1535 several non-exact match types are also possible. See the
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1536 documentation for `TestRunner` for more information about
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1537 option flags.
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1538 """
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1539
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1540 # If `want` contains hex-escaped character such as "\u1234",
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1541 # then `want` is a string of six characters(e.g. [\,u,1,2,3,4]).
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1542 # On the other hand, `got` could be an another sequence of
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1543 # characters such as [\u1234], so `want` and `got` should
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1544 # be folded to hex-escaped ASCII string to compare.
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1545 got = self._toAscii(got)
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1546 want = self._toAscii(want)
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47594
diff changeset
1547
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1548 # Handle the common case first, for efficiency:
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1549 # if they're string-identical, always return true.
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1550 if got == want:
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1551 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1552
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1553 # The values True and False replaced 1 and 0 as the return
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1554 # value for boolean comparisons in Python 2.3.
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1555 if not (optionflags & DONT_ACCEPT_TRUE_FOR_1):
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1556 if (got,want) == ("True\n", "1\n"):
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1557 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1558 if (got,want) == ("False\n", "0\n"):
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1559 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1560
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1561 # <BLANKLINE> can be used as a special sequence to signify a
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1562 # blank line, unless the DONT_ACCEPT_BLANKLINE flag is used.
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1563 if not (optionflags & DONT_ACCEPT_BLANKLINE):
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1564 # Replace <BLANKLINE> in want with a blank line.
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1565 want = re.sub('(?m)^%s\s*?$' % re.escape(BLANKLINE_MARKER),
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1566 '', want)
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1567 # If a line in got contains only spaces, then remove the
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1568 # spaces.
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1569 got = re.sub('(?m)^\s*?$', '', got)
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1570 if got == want:
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1571 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1572
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1573 # This flag causes doctest to ignore any differences in the
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1574 # contents of whitespace strings. Note that this can be used
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
1575 # in conjunction with the ELLIPSIS flag.
33186
4c78fcb60df3 ELLIPSIS implementation: an ellipsis couldn't match nothing if it
Tim Peters <tim.peters@gmail.com>
parents: 33165
diff changeset
1576 if optionflags & NORMALIZE_WHITESPACE:
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1577 got = ' '.join(got.split())
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1578 want = ' '.join(want.split())
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1579 if got == want:
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1580 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1581
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1582 # The ELLIPSIS flag says to let the sequence "..." in `want`
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
1583 # match any substring in `got`.
33186
4c78fcb60df3 ELLIPSIS implementation: an ellipsis couldn't match nothing if it
Tim Peters <tim.peters@gmail.com>
parents: 33165
diff changeset
1584 if optionflags & ELLIPSIS:
33200
26563204660c Gave _ellipsis_match() an attractive new leading underscore.
Tim Peters <tim.peters@gmail.com>
parents: 33196
diff changeset
1585 if _ellipsis_match(want, got):
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1586 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1587
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1588 # We didn't find any match; return false.
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1589 return False
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1590
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1591 # Should we do a fancy diff?
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1592 def _do_a_fancy_diff(self, want, got, optionflags):
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1593 # Not unless they asked for a fancy diff.
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1594 if not optionflags & (REPORT_UDIFF |
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1595 REPORT_CDIFF |
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1596 REPORT_NDIFF):
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1597 return False
33293
5fab4f43b94f _do_a_fancy_diff(): Pay no attention to the ellipses behind the curtain.
Tim Peters <tim.peters@gmail.com>
parents: 33287
diff changeset
1598
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1599 # If expected output uses ellipsis, a meaningful fancy diff is
33293
5fab4f43b94f _do_a_fancy_diff(): Pay no attention to the ellipses behind the curtain.
Tim Peters <tim.peters@gmail.com>
parents: 33287
diff changeset
1600 # too hard ... or maybe not. In two real-life failures Tim saw,
5fab4f43b94f _do_a_fancy_diff(): Pay no attention to the ellipses behind the curtain.
Tim Peters <tim.peters@gmail.com>
parents: 33287
diff changeset
1601 # a diff was a major help anyway, so this is commented out.
5fab4f43b94f _do_a_fancy_diff(): Pay no attention to the ellipses behind the curtain.
Tim Peters <tim.peters@gmail.com>
parents: 33287
diff changeset
1602 # [todo] _ellipsis_match() knows which pieces do and don't match,
5fab4f43b94f _do_a_fancy_diff(): Pay no attention to the ellipses behind the curtain.
Tim Peters <tim.peters@gmail.com>
parents: 33287
diff changeset
1603 # and could be the basis for a kick-ass diff in this case.
5fab4f43b94f _do_a_fancy_diff(): Pay no attention to the ellipses behind the curtain.
Tim Peters <tim.peters@gmail.com>
parents: 33287
diff changeset
1604 ##if optionflags & ELLIPSIS and ELLIPSIS_MARKER in want:
5fab4f43b94f _do_a_fancy_diff(): Pay no attention to the ellipses behind the curtain.
Tim Peters <tim.peters@gmail.com>
parents: 33287
diff changeset
1605 ## return False
5fab4f43b94f _do_a_fancy_diff(): Pay no attention to the ellipses behind the curtain.
Tim Peters <tim.peters@gmail.com>
parents: 33287
diff changeset
1606
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1607 # ndiff does intraline difference marking, so can be useful even
33293
5fab4f43b94f _do_a_fancy_diff(): Pay no attention to the ellipses behind the curtain.
Tim Peters <tim.peters@gmail.com>
parents: 33287
diff changeset
1608 # for 1-line differences.
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1609 if optionflags & REPORT_NDIFF:
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1610 return True
33293
5fab4f43b94f _do_a_fancy_diff(): Pay no attention to the ellipses behind the curtain.
Tim Peters <tim.peters@gmail.com>
parents: 33287
diff changeset
1611
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1612 # The other diff types need at least a few lines to be helpful.
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1613 return want.count('\n') > 2 and got.count('\n') > 2
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1614
33287
449141aa011c Changed OutputChecker.output_difference to expect an Example object,
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33286
diff changeset
1615 def output_difference(self, example, got, optionflags):
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1616 """
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1617 Return a string describing the differences between the
33287
449141aa011c Changed OutputChecker.output_difference to expect an Example object,
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33286
diff changeset
1618 expected output for a given example (`example`) and the actual
449141aa011c Changed OutputChecker.output_difference to expect an Example object,
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33286
diff changeset
1619 output (`got`). `optionflags` is the set of option flags used
449141aa011c Changed OutputChecker.output_difference to expect an Example object,
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33286
diff changeset
1620 to compare `want` and `got`.
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1621 """
33287
449141aa011c Changed OutputChecker.output_difference to expect an Example object,
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33286
diff changeset
1622 want = example.want
33090
4b0a0fc1baea In output_difference(), replace blank lines in `want` with <BLANKLINE>
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
1623 # If <BLANKLINE>s are being used, then replace blank lines
4b0a0fc1baea In output_difference(), replace blank lines in `want` with <BLANKLINE>
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
1624 # with <BLANKLINE> in the actual output string.
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1625 if not (optionflags & DONT_ACCEPT_BLANKLINE):
33090
4b0a0fc1baea In output_difference(), replace blank lines in `want` with <BLANKLINE>
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
1626 got = re.sub('(?m)^[ ]*(?=\n)', BLANKLINE_MARKER, got)
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1627
33293
5fab4f43b94f _do_a_fancy_diff(): Pay no attention to the ellipses behind the curtain.
Tim Peters <tim.peters@gmail.com>
parents: 33287
diff changeset
1628 # Check if we should use diff.
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1629 if self._do_a_fancy_diff(want, got, optionflags):
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1630 # Split want & got into lines.
33296
3e25d6e3da19 output_difference(): In fancy-diff cases, the way this split expected &
Tim Peters <tim.peters@gmail.com>
parents: 33293
diff changeset
1631 want_lines = want.splitlines(True) # True == keep line ends
3e25d6e3da19 output_difference(): In fancy-diff cases, the way this split expected &
Tim Peters <tim.peters@gmail.com>
parents: 33293
diff changeset
1632 got_lines = got.splitlines(True)
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1633 # Use difflib to find their differences.
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1634 if optionflags & REPORT_UDIFF:
33283
2747dbd5938b Shortened diff output for unified & context diffs
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33282
diff changeset
1635 diff = difflib.unified_diff(want_lines, got_lines, n=2)
2747dbd5938b Shortened diff output for unified & context diffs
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33282
diff changeset
1636 diff = list(diff)[2:] # strip the diff header
2747dbd5938b Shortened diff output for unified & context diffs
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33282
diff changeset
1637 kind = 'unified diff with -expected +actual'
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1638 elif optionflags & REPORT_CDIFF:
33283
2747dbd5938b Shortened diff output for unified & context diffs
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33282
diff changeset
1639 diff = difflib.context_diff(want_lines, got_lines, n=2)
2747dbd5938b Shortened diff output for unified & context diffs
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33282
diff changeset
1640 diff = list(diff)[2:] # strip the diff header
2747dbd5938b Shortened diff output for unified & context diffs
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33282
diff changeset
1641 kind = 'context diff with expected followed by actual'
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1642 elif optionflags & REPORT_NDIFF:
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1643 engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK)
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1644 diff = list(engine.compare(want_lines, got_lines))
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1645 kind = 'ndiff with -expected +actual'
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1646 else:
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1647 assert 0, 'Bad diff option'
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1648 # Remove trailing whitespace on diff output.
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1649 diff = [line.rstrip() + '\n' for line in diff]
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1650 return 'Differences (%s):\n' % kind + _indent(''.join(diff))
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1651
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1652 # If we're not using diff, then simply list the expected
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1653 # output followed by the actual output.
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1654 if want and got:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1655 return 'Expected:\n%sGot:\n%s' % (_indent(want), _indent(got))
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1656 elif want:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1657 return 'Expected:\n%sGot nothing\n' % _indent(want)
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1658 elif got:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1659 return 'Expected nothing\nGot:\n%s' % _indent(got)
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1660 else:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1661 return 'Expected nothing\nGot nothing\n'
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1662
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1663 class DocTestFailure(Exception):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1664 """A DocTest example has failed in debugging mode.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1665
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1666 The exception instance has variables:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1667
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1668 - test: the DocTest object being run
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1669
39699
f974890c6cd2 Fix another typo.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39690
diff changeset
1670 - example: the Example object that failed
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1671
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1672 - got: the actual output
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1673 """
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1674 def __init__(self, test, example, got):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1675 self.test = test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1676 self.example = example
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1677 self.got = got
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1678
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1679 def __str__(self):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1680 return str(self.test)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1681
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1682 class UnexpectedException(Exception):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1683 """A DocTest example has encountered an unexpected exception
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1684
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1685 The exception instance has variables:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1686
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1687 - test: the DocTest object being run
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1688
39690
0cb9e57ec406 Inspired by SF patch #860326, make the exception formatting by
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
1689 - example: the Example object that failed
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1690
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1691 - exc_info: the exception info
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1692 """
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1693 def __init__(self, test, example, exc_info):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1694 self.test = test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1695 self.example = example
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1696 self.exc_info = exc_info
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1697
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1698 def __str__(self):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1699 return str(self.test)
32983
726db5854746 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1700
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1701 class DebugRunner(DocTestRunner):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1702 r"""Run doc tests but raise an exception as soon as there is a failure.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1703
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1704 If an unexpected exception occurs, an UnexpectedException is raised.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1705 It contains the test, the example, and the original exception:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1706
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1707 >>> runner = DebugRunner(verbose=False)
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
1708 >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42',
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
1709 ... {}, 'foo', 'foo.py', 0)
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1710 >>> try:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1711 ... runner.run(test)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1712 ... except UnexpectedException as f:
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1713 ... failure = f
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1714
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1715 >>> failure.test is test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1716 True
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1717
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1718 >>> failure.example.want
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1719 '42\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1720
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1721 >>> exc_info = failure.exc_info
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 43020
diff changeset
1722 >>> raise exc_info[1] # Already has the traceback
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1723 Traceback (most recent call last):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1724 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1725 KeyError
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1726
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1727 We wrap the original exception to give the calling application
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1728 access to the test and example information.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1729
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1730 If the output doesn't match, then a DocTestFailure is raised:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1731
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
1732 >>> test = DocTestParser().get_doctest('''
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1733 ... >>> x = 1
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1734 ... >>> x
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1735 ... 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1736 ... ''', {}, 'foo', 'foo.py', 0)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1737
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1738 >>> try:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1739 ... runner.run(test)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1740 ... except DocTestFailure as f:
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1741 ... failure = f
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1742
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1743 DocTestFailure objects provide access to the test:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1744
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1745 >>> failure.test is test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1746 True
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1747
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1748 As well as to the example:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1749
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1750 >>> failure.example.want
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1751 '2\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1752
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1753 and the actual output:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1754
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1755 >>> failure.got
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1756 '1\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1757
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1758 If a failure or error occurs, the globals are left intact:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1759
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1760 >>> del test.globs['__builtins__']
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1761 >>> test.globs
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1762 {'x': 1}
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1763
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
1764 >>> test = DocTestParser().get_doctest('''
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1765 ... >>> x = 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1766 ... >>> raise KeyError
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1767 ... ''', {}, 'foo', 'foo.py', 0)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1768
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1769 >>> runner.run(test)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1770 Traceback (most recent call last):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1771 ...
39690
0cb9e57ec406 Inspired by SF patch #860326, make the exception formatting by
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
1772 doctest.UnexpectedException: <DocTest foo from foo.py:0 (2 examples)>
32983
726db5854746 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1773
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1774 >>> del test.globs['__builtins__']
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1775 >>> test.globs
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1776 {'x': 2}
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1777
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1778 But the globals are cleared if there is no error:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1779
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
1780 >>> test = DocTestParser().get_doctest('''
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1781 ... >>> x = 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1782 ... ''', {}, 'foo', 'foo.py', 0)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1783
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1784 >>> runner.run(test)
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
1785 TestResults(failed=0, attempted=1)
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1786
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1787 >>> test.globs
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1788 {}
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1789
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1790 """
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1791
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1792 def run(self, test, compileflags=None, out=None, clear_globs=True):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1793 r = DocTestRunner.run(self, test, compileflags, out, False)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1794 if clear_globs:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1795 test.globs.clear()
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1796 return r
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1797
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1798 def report_unexpected_exception(self, out, test, example, exc_info):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1799 raise UnexpectedException(test, example, exc_info)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1800
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1801 def report_failure(self, out, test, example, got):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1802 raise DocTestFailure(test, example, got)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1803
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1804 ######################################################################
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
1805 ## 6. Test Functions
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1806 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1807 # These should be backwards compatible.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1808
33467
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1809 # For backward compatibility, a global instance of a DocTestRunner
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1810 # class, updated by testmod.
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1811 master = None
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1812
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1813 def testmod(m=None, name=None, globs=None, verbose=None,
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1814 report=True, optionflags=0, extraglobs=None,
33473
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
1815 raise_on_error=False, exclude_empty=False):
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1816 """m=None, name=None, globs=None, verbose=None, report=True,
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1817 optionflags=0, extraglobs=None, raise_on_error=False,
33473
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
1818 exclude_empty=False
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1819
26338
ac7872e55405 Patch #486438: Make module argument to testmod optional.
Martin v. Löwis <martin@v.loewis.de>
parents: 23620
diff changeset
1820 Test examples in docstrings in functions and classes reachable
ac7872e55405 Patch #486438: Make module argument to testmod optional.
Martin v. Löwis <martin@v.loewis.de>
parents: 23620
diff changeset
1821 from module m (or the current module if m is not supplied), starting
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1822 with m.__doc__.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1823
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1824 Also test examples reachable from dict m.__test__ if it exists and is
33078
d075cba851fd Start rewriting doctest's LaTeX docs. Damn, this is slow going!
Tim Peters <tim.peters@gmail.com>
parents: 33073
diff changeset
1825 not None. m.__test__ maps names to functions, classes and strings;
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1826 function and class docstrings are tested even if the name is private;
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1827 strings are tested directly, as if they were docstrings.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1828
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1829 Return (#failures, #tests).
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1830
64077
4085cc560e9e Merged revisions 84106 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63453
diff changeset
1831 See help(doctest) for an overview.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1832
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1833 Optional keyword arg "name" gives the name of the module; by default
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1834 use m.__name__.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1835
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1836 Optional keyword arg "globs" gives a dict to be used as the globals
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1837 when executing examples; by default, use m.__dict__. A copy of this
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1838 dict is actually used for each docstring, so that each docstring's
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1839 examples start with a clean slate.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1840
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1841 Optional keyword arg "extraglobs" gives a dictionary that should be
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1842 merged into the globals that are used to execute examples. By
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1843 default, no extra globals are used. This is new in 2.4.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1844
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1845 Optional keyword arg "verbose" prints lots of stuff if true, prints
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1846 only failures if false; by default, it's true iff "-v" is in sys.argv.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1847
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1848 Optional keyword arg "report" prints a summary at the end when true,
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1849 else prints nothing at the end. In verbose mode, the summary is
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1850 detailed, else very brief (in fact, empty if all tests passed).
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1851
29623
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
1852 Optional keyword arg "optionflags" or's together module constants,
33232
69976cf59149 Start deferring to the LaTeX docs for details. I'd like to move the
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
1853 and defaults to 0. This is new in 2.3. Possible values (see the
69976cf59149 Start deferring to the LaTeX docs for details. I'd like to move the
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
1854 docs for details):
29623
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
1855
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
1856 DONT_ACCEPT_TRUE_FOR_1
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1857 DONT_ACCEPT_BLANKLINE
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1858 NORMALIZE_WHITESPACE
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1859 ELLIPSIS
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1860 SKIP
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1861 IGNORE_EXCEPTION_DETAIL
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1862 REPORT_UDIFF
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1863 REPORT_CDIFF
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1864 REPORT_NDIFF
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1865 REPORT_ONLY_FIRST_FAILURE
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1866
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1867 Optional keyword arg "raise_on_error" raises an exception on the
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1868 first unexpected exception or failure. This allows failures to be
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1869 post-mortem debugged.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1870
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1871 Advanced tomfoolery: testmod runs methods of a local instance of
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1872 class doctest.Tester, then merges the results into (or creates)
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1873 global Tester instance doctest.master. Methods of doctest.master
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1874 can be called directly too, if you want to do something unusual.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1875 Passing report=0 to testmod is especially useful then, to delay
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1876 displaying a summary. Invoke doctest.master.summarize(verbose)
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1877 when you're done fiddling.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1878 """
33467
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1879 global master
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1880
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1881 # If no module was given, then use __main__.
26338
ac7872e55405 Patch #486438: Make module argument to testmod optional.
Martin v. Löwis <martin@v.loewis.de>
parents: 23620
diff changeset
1882 if m is None:
ac7872e55405 Patch #486438: Make module argument to testmod optional.
Martin v. Löwis <martin@v.loewis.de>
parents: 23620
diff changeset
1883 # DWA - m will still be None if this wasn't invoked from the command
ac7872e55405 Patch #486438: Make module argument to testmod optional.
Martin v. Löwis <martin@v.loewis.de>
parents: 23620
diff changeset
1884 # line, in which case the following TypeError is about as good an error
ac7872e55405 Patch #486438: Make module argument to testmod optional.
Martin v. Löwis <martin@v.loewis.de>
parents: 23620
diff changeset
1885 # as we should expect
ac7872e55405 Patch #486438: Make module argument to testmod optional.
Martin v. Löwis <martin@v.loewis.de>
parents: 23620
diff changeset
1886 m = sys.modules.get('__main__')
ac7872e55405 Patch #486438: Make module argument to testmod optional.
Martin v. Löwis <martin@v.loewis.de>
parents: 23620
diff changeset
1887
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1888 # Check that we were actually given a module.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1889 if not inspect.ismodule(m):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30448
diff changeset
1890 raise TypeError("testmod: module required; %r" % (m,))
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1891
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1892 # If no name was given, then use the module's name.
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1893 if name is None:
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1894 name = m.__name__
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1895
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1896 # Find, parse, and run all tests in the given module.
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1897 finder = DocTestFinder(exclude_empty=exclude_empty)
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1898
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1899 if raise_on_error:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1900 runner = DebugRunner(verbose=verbose, optionflags=optionflags)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1901 else:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1902 runner = DocTestRunner(verbose=verbose, optionflags=optionflags)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1903
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1904 for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1905 runner.run(test)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1906
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1907 if report:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1908 runner.summarize()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1909
33467
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1910 if master is None:
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1911 master = runner
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1912 else:
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1913 master.merge(runner)
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1914
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
1915 return TestResults(runner.failures, runner.tries)
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1916
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1917 def testfile(filename, module_relative=True, name=None, package=None,
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1918 globs=None, verbose=None, report=True, optionflags=0,
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1919 extraglobs=None, raise_on_error=False, parser=DocTestParser(),
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1920 encoding=None):
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1921 """
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1922 Test examples in the given file. Return (#failures, #tests).
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1923
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1924 Optional keyword arg "module_relative" specifies how filenames
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1925 should be interpreted:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1926
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1927 - If "module_relative" is True (the default), then "filename"
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1928 specifies a module-relative path. By default, this path is
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1929 relative to the calling module's directory; but if the
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1930 "package" argument is specified, then it is relative to that
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1931 package. To ensure os-independence, "filename" should use
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1932 "/" characters to separate path segments, and should not
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1933 be an absolute path (i.e., it may not begin with "/").
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1934
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1935 - If "module_relative" is False, then "filename" specifies an
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1936 os-specific path. The path may be absolute or relative (to
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1937 the current working directory).
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1938
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
1939 Optional keyword arg "name" gives the name of the test; by default
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
1940 use the file's basename.
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1941
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1942 Optional keyword argument "package" is a Python package or the
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1943 name of a Python package whose directory should be used as the
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1944 base directory for a module relative filename. If no package is
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1945 specified, then the calling module's directory is used as the base
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1946 directory for module relative filenames. It is an error to
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1947 specify "package" if "module_relative" is False.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1948
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1949 Optional keyword arg "globs" gives a dict to be used as the globals
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1950 when executing examples; by default, use {}. A copy of this dict
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1951 is actually used for each docstring, so that each docstring's
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1952 examples start with a clean slate.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1953
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1954 Optional keyword arg "extraglobs" gives a dictionary that should be
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1955 merged into the globals that are used to execute examples. By
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1956 default, no extra globals are used.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1957
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1958 Optional keyword arg "verbose" prints lots of stuff if true, prints
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1959 only failures if false; by default, it's true iff "-v" is in sys.argv.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1960
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1961 Optional keyword arg "report" prints a summary at the end when true,
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1962 else prints nothing at the end. In verbose mode, the summary is
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1963 detailed, else very brief (in fact, empty if all tests passed).
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1964
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1965 Optional keyword arg "optionflags" or's together module constants,
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1966 and defaults to 0. Possible values (see the docs for details):
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1967
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1968 DONT_ACCEPT_TRUE_FOR_1
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1969 DONT_ACCEPT_BLANKLINE
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1970 NORMALIZE_WHITESPACE
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1971 ELLIPSIS
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1972 SKIP
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1973 IGNORE_EXCEPTION_DETAIL
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1974 REPORT_UDIFF
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1975 REPORT_CDIFF
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1976 REPORT_NDIFF
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1977 REPORT_ONLY_FIRST_FAILURE
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1978
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1979 Optional keyword arg "raise_on_error" raises an exception on the
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1980 first unexpected exception or failure. This allows failures to be
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1981 post-mortem debugged.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1982
33594
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
1983 Optional keyword arg "parser" specifies a DocTestParser (or
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
1984 subclass) that should be used to extract tests from the files.
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
1985
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1986 Optional keyword arg "encoding" specifies an encoding that should
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1987 be used to convert the file to unicode.
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1988
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1989 Advanced tomfoolery: testmod runs methods of a local instance of
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1990 class doctest.Tester, then merges the results into (or creates)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1991 global Tester instance doctest.master. Methods of doctest.master
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1992 can be called directly too, if you want to do something unusual.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1993 Passing report=0 to testmod is especially useful then, to delay
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1994 displaying a summary. Invoke doctest.master.summarize(verbose)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1995 when you're done fiddling.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1996 """
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1997 global master
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1998
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1999 if package and not module_relative:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2000 raise ValueError("Package may only be specified for module-"
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2001 "relative paths.")
33529
25e07c44e5d0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33522
diff changeset
2002
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2003 # Relativize the path
42973
1ce89f2578cf Change the way the encoding parameter is handled.
Guido van Rossum <guido@python.org>
parents: 42552
diff changeset
2004 text, filename = _load_testfile(filename, package, module_relative,
1ce89f2578cf Change the way the encoding parameter is handled.
Guido van Rossum <guido@python.org>
parents: 42552
diff changeset
2005 encoding or "utf-8")
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2006
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2007 # If no name was given, then use the file's name.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2008 if name is None:
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
2009 name = os.path.basename(filename)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2010
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2011 # Assemble the globals.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2012 if globs is None:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2013 globs = {}
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2014 else:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2015 globs = globs.copy()
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2016 if extraglobs is not None:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2017 globs.update(extraglobs)
51112
8cd3ced1ed29 Issue 5021: doctest.testfile should set __name__
Raymond Hettinger <python@rcn.com>
parents: 50187
diff changeset
2018 if '__name__' not in globs:
8cd3ced1ed29 Issue 5021: doctest.testfile should set __name__
Raymond Hettinger <python@rcn.com>
parents: 50187
diff changeset
2019 globs['__name__'] = '__main__'
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2020
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2021 if raise_on_error:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2022 runner = DebugRunner(verbose=verbose, optionflags=optionflags)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2023 else:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2024 runner = DocTestRunner(verbose=verbose, optionflags=optionflags)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2025
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2026 # Read the file, convert it to a test, and run it.
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35167
diff changeset
2027 test = parser.get_doctest(text, globs, name, filename, 0)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2028 runner.run(test)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2029
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2030 if report:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2031 runner.summarize()
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2032
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2033 if master is None:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2034 master = runner
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2035 else:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2036 master.merge(runner)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2037
44643
d8e4342c145e Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
2038 return TestResults(runner.failures, runner.tries)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2039
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2040 def run_docstring_examples(f, globs, verbose=False, name="NoName",
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2041 compileflags=None, optionflags=0):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2042 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2043 Test examples in the given object's docstring (`f`), using `globs`
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2044 as globals. Optional argument `name` is used in failure messages.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2045 If the optional argument `verbose` is true, then generate output
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2046 even if there are no failures.
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2047
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2048 `compileflags` gives the set of flags that should be used by the
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2049 Python compiler when running the examples. If not specified, then
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2050 it will default to the set of future-import flags that apply to
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2051 `globs`.
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2052
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2053 Optional keyword arg `optionflags` specifies options for the
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2054 testing and output. See the documentation for `testmod` for more
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2055 information.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2056 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2057 # Find, parse, and run all tests in the given module.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2058 finder = DocTestFinder(verbose=verbose, recurse=False)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2059 runner = DocTestRunner(verbose=verbose, optionflags=optionflags)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2060 for test in finder.find(f, name, globs=globs):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2061 runner.run(test, compileflags=compileflags)
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2062
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2063 ######################################################################
46957
9d46c289d166 Remove deprecated Tester class from doctest module.
Georg Brandl <georg@python.org>
parents: 44643
diff changeset
2064 ## 7. Unittest Support
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2065 ######################################################################
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2066
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2067 _unittest_reportflags = 0
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
2068
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2069 def set_unittest_reportflags(flags):
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
2070 """Sets the unittest option flags.
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2071
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2072 The old flag is returned so that a runner could restore the old
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2073 value if it wished to:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2074
34829
b72df8aee56d SF patch 1167316: doctest.py fails self-test if run directly.
Tim Peters <tim.peters@gmail.com>
parents: 34215
diff changeset
2075 >>> import doctest
b72df8aee56d SF patch 1167316: doctest.py fails self-test if run directly.
Tim Peters <tim.peters@gmail.com>
parents: 34215
diff changeset
2076 >>> old = doctest._unittest_reportflags
b72df8aee56d SF patch 1167316: doctest.py fails self-test if run directly.
Tim Peters <tim.peters@gmail.com>
parents: 34215
diff changeset
2077 >>> doctest.set_unittest_reportflags(REPORT_NDIFF |
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2078 ... REPORT_ONLY_FIRST_FAILURE) == old
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2079 True
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2080
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2081 >>> doctest._unittest_reportflags == (REPORT_NDIFF |
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2082 ... REPORT_ONLY_FIRST_FAILURE)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2083 True
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2084
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2085 Only reporting flags can be set:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2086
34829
b72df8aee56d SF patch 1167316: doctest.py fails self-test if run directly.
Tim Peters <tim.peters@gmail.com>
parents: 34215
diff changeset
2087 >>> doctest.set_unittest_reportflags(ELLIPSIS)
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2088 Traceback (most recent call last):
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2089 ...
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
2090 ValueError: ('Only reporting flags allowed', 8)
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2091
34829
b72df8aee56d SF patch 1167316: doctest.py fails self-test if run directly.
Tim Peters <tim.peters@gmail.com>
parents: 34215
diff changeset
2092 >>> doctest.set_unittest_reportflags(old) == (REPORT_NDIFF |
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2093 ... REPORT_ONLY_FIRST_FAILURE)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2094 True
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2095 """
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2096 global _unittest_reportflags
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
2097
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
2098 if (flags & REPORTING_FLAGS) != flags:
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
2099 raise ValueError("Only reporting flags allowed", flags)
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2100 old = _unittest_reportflags
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2101 _unittest_reportflags = flags
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2102 return old
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2103
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2104
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2105 class DocTestCase(unittest.TestCase):
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2106
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
2107 def __init__(self, test, optionflags=0, setUp=None, tearDown=None,
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
2108 checker=None):
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2109
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2110 unittest.TestCase.__init__(self)
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2111 self._dt_optionflags = optionflags
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
2112 self._dt_checker = checker
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2113 self._dt_test = test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2114 self._dt_setUp = setUp
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2115 self._dt_tearDown = tearDown
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2116
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2117 def setUp(self):
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2118 test = self._dt_test
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2119
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2120 if self._dt_setUp is not None:
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2121 self._dt_setUp(test)
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2122
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2123 def tearDown(self):
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2124 test = self._dt_test
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2125
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2126 if self._dt_tearDown is not None:
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2127 self._dt_tearDown(test)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2128
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2129 test.globs.clear()
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2130
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2131 def runTest(self):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2132 test = self._dt_test
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2133 old = sys.stdout
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2134 new = StringIO()
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2135 optionflags = self._dt_optionflags
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2136
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
2137 if not (optionflags & REPORTING_FLAGS):
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2138 # The option flags don't include any reporting flags,
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2139 # so add the default reporting flags
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2140 optionflags |= _unittest_reportflags
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2141
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2142 runner = DocTestRunner(optionflags=optionflags,
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
2143 checker=self._dt_checker, verbose=False)
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2144
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2145 try:
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2146 runner.DIVIDER = "-"*70
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2147 failures, tries = runner.run(
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2148 test, out=new.write, clear_globs=False)
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2149 finally:
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2150 sys.stdout = old
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2151
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2152 if failures:
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2153 raise self.failureException(self.format_failure(new.getvalue()))
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2154
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2155 def format_failure(self, err):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2156 test = self._dt_test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2157 if test.lineno is None:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2158 lineno = 'unknown line number'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2159 else:
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2160 lineno = '%s' % test.lineno
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2161 lname = '.'.join(test.name.split('.')[-1:])
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2162 return ('Failed doctest test for %s\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2163 ' File "%s", line %s, in %s\n\n%s'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2164 % (test.name, test.filename, lineno, lname, err)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2165 )
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2166
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2167 def debug(self):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2168 r"""Run the test case without results and without catching exceptions
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2169
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2170 The unit test framework includes a debug method on test cases
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2171 and test suites to support post-mortem debugging. The test code
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2172 is run in such a way that errors are not caught. This way a
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2173 caller can catch the errors and initiate post-mortem debugging.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2174
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2175 The DocTestCase provides a debug method that raises
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65338
diff changeset
2176 UnexpectedException errors if there is an unexpected
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2177 exception:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2178
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
2179 >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42',
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2180 ... {}, 'foo', 'foo.py', 0)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2181 >>> case = DocTestCase(test)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2182 >>> try:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2183 ... case.debug()
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
2184 ... except UnexpectedException as f:
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
2185 ... failure = f
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2186
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2187 The UnexpectedException contains the test, the example, and
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2188 the original exception:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2189
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2190 >>> failure.test is test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2191 True
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2192
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2193 >>> failure.example.want
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2194 '42\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2195
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2196 >>> exc_info = failure.exc_info
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 43020
diff changeset
2197 >>> raise exc_info[1] # Already has the traceback
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2198 Traceback (most recent call last):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2199 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2200 KeyError
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2201
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2202 If the output doesn't match, then a DocTestFailure is raised:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2203
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
2204 >>> test = DocTestParser().get_doctest('''
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2205 ... >>> x = 1
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2206 ... >>> x
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2207 ... 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2208 ... ''', {}, 'foo', 'foo.py', 0)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2209 >>> case = DocTestCase(test)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2210
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2211 >>> try:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2212 ... case.debug()
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
2213 ... except DocTestFailure as f:
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
2214 ... failure = f
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2215
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2216 DocTestFailure objects provide access to the test:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2217
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2218 >>> failure.test is test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2219 True
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2220
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2221 As well as to the example:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2222
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2223 >>> failure.example.want
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2224 '2\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2225
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2226 and the actual output:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2227
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2228 >>> failure.got
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2229 '1\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2230
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2231 """
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2232
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2233 self.setUp()
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
2234 runner = DebugRunner(optionflags=self._dt_optionflags,
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
2235 checker=self._dt_checker, verbose=False)
47080
58a0d5524f9a Merged revisions 63119-63128,63130-63131,63133,63135-63144,63146-63148,63151-63152,63155-63165,63167-63176,63181-63186,63188-63189 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46964
diff changeset
2236 runner.run(self._dt_test, clear_globs=False)
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2237 self.tearDown()
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2238
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2239 def id(self):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2240 return self._dt_test.name
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2241
74047
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2242 def __eq__(self, other):
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2243 if type(self) is not type(other):
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2244 return NotImplemented
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2245
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2246 return self._dt_test == other._dt_test and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2247 self._dt_optionflags == other._dt_optionflags and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2248 self._dt_setUp == other._dt_setUp and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2249 self._dt_tearDown == other._dt_tearDown and \
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2250 self._dt_checker == other._dt_checker
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2251
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2252 def __ne__(self, other):
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2253 return not self == other
c478734ded4b Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71093
diff changeset
2254
74056
e37c71698409 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74047
diff changeset
2255 def __hash__(self):
e37c71698409 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74047
diff changeset
2256 return hash((self._dt_optionflags, self._dt_setUp, self._dt_tearDown,
e37c71698409 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74047
diff changeset
2257 self._dt_checker))
e37c71698409 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74047
diff changeset
2258
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2259 def __repr__(self):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2260 name = self._dt_test.name.split('.')
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2261 return "%s (%s)" % (name[-1], '.'.join(name[:-1]))
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2262
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2263 __str__ = __repr__
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2264
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2265 def shortDescription(self):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2266 return "Doctest: " + self._dt_test.name
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2267
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2268 class SkipDocTestCase(DocTestCase):
75859
64f1b8ad9214 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74056
diff changeset
2269 def __init__(self, module):
64f1b8ad9214 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74056
diff changeset
2270 self.module = module
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2271 DocTestCase.__init__(self, None)
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2272
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2273 def setUp(self):
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2274 self.skipTest("DocTestSuite will not work with -O2 and above")
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2275
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2276 def test_skip(self):
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2277 pass
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2278
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2279 def shortDescription(self):
75859
64f1b8ad9214 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74056
diff changeset
2280 return "Skipping tests from %s" % self.module.__name__
64f1b8ad9214 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74056
diff changeset
2281
64f1b8ad9214 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74056
diff changeset
2282 __str__ = shortDescription
64f1b8ad9214 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74056
diff changeset
2283
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2284
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2285 def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None,
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2286 **options):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2287 """
33229
7fec0662cf4a Type in docstring.
Tim Peters <tim.peters@gmail.com>
parents: 33228
diff changeset
2288 Convert doctest tests for a module to a unittest test suite.
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2289
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2290 This converts each documentation string in a module that
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2291 contains doctest tests to a unittest test case. If any of the
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2292 tests in a doc string fail, then the test case fails. An exception
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2293 is raised showing the name of the file containing the test and a
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2294 (sometimes approximate) line number.
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2295
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2296 The `module` argument provides the module to be tested. The argument
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2297 can be either a module or a module name.
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2298
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2299 If no argument is given, the calling module is used.
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2300
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2301 A number of options may be provided as keyword arguments:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2302
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2303 setUp
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
2304 A set-up function. This is called before running the
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2305 tests in each file. The setUp function will be passed a DocTest
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2306 object. The setUp function can access the test globals as the
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2307 globs attribute of the test passed.
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2308
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2309 tearDown
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
2310 A tear-down function. This is called after running the
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2311 tests in each file. The tearDown function will be passed a DocTest
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2312 object. The tearDown function can access the test globals as the
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2313 globs attribute of the test passed.
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2314
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2315 globs
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2316 A dictionary containing initial global variables for the tests.
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2317
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2318 optionflags
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2319 A set of doctest option flags expressed as an integer.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2320 """
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2321
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2322 if test_finder is None:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2323 test_finder = DocTestFinder()
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2324
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2325 module = _normalize_module(module)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2326 tests = test_finder.find(module, globs=globs, extraglobs=extraglobs)
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2327
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2328 if not tests and sys.flags.optimize >=2:
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2329 # Skip doctests when running with -O2
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2330 suite = unittest.TestSuite()
75859
64f1b8ad9214 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74056
diff changeset
2331 suite.addTest(SkipDocTestCase(module))
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2332 return suite
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2333 elif not tests:
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2334 # Why do we want to do this? Because it reveals a bug that might
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2335 # otherwise be hidden.
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2336 raise ValueError(module, "has no tests")
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2337
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2338 tests.sort()
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2339 suite = unittest.TestSuite()
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 58630
diff changeset
2340
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2341 for test in tests:
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2342 if len(test.examples) == 0:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2343 continue
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2344 if not test.filename:
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2345 filename = module.__file__
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2346 if filename[-4:] in (".pyc", ".pyo"):
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2347 filename = filename[:-1]
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2348 test.filename = filename
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2349 suite.addTest(DocTestCase(test, **options))
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2350
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2351 return suite
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2352
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2353 class DocFileCase(DocTestCase):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2354
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2355 def id(self):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2356 return '_'.join(self._dt_test.name.split('.'))
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2357
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2358 def __repr__(self):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2359 return self._dt_test.filename
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2360 __str__ = __repr__
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2361
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2362 def format_failure(self, err):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2363 return ('Failed doctest test for %s\n File "%s", line 0\n\n%s'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2364 % (self._dt_test.name, self._dt_test.filename, err)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2365 )
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2366
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2367 def DocFileTest(path, module_relative=True, package=None,
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2368 globs=None, parser=DocTestParser(),
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2369 encoding=None, **options):
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2370 if globs is None:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2371 globs = {}
34215
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33946
diff changeset
2372 else:
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33946
diff changeset
2373 globs = globs.copy()
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2374
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2375 if package and not module_relative:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2376 raise ValueError("Package may only be specified for module-"
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2377 "relative paths.")
33529
25e07c44e5d0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33522
diff changeset
2378
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2379 # Relativize the path.
42973
1ce89f2578cf Change the way the encoding parameter is handled.
Guido van Rossum <guido@python.org>
parents: 42552
diff changeset
2380 doc, path = _load_testfile(path, package, module_relative,
1ce89f2578cf Change the way the encoding parameter is handled.
Guido van Rossum <guido@python.org>
parents: 42552
diff changeset
2381 encoding or "utf-8")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35167
diff changeset
2382
34215
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33946
diff changeset
2383 if "__file__" not in globs:
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33946
diff changeset
2384 globs["__file__"] = path
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2385
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2386 # Find the file and read it.
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
2387 name = os.path.basename(path)
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2388
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2389 # Convert it to a test, and wrap it in a DocFileCase.
33594
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
2390 test = parser.get_doctest(doc, globs, name, path, 0)
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2391 return DocFileCase(test, **options)
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2392
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2393 def DocFileSuite(*paths, **kw):
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2394 """A unittest suite for one or more doctest files.
33529
25e07c44e5d0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33522
diff changeset
2395
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2396 The path to each doctest file is given as a string; the
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2397 interpretation of that string depends on the keyword argument
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2398 "module_relative".
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2399
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2400 A number of options may be provided as keyword arguments:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2401
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2402 module_relative
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2403 If "module_relative" is True, then the given file paths are
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2404 interpreted as os-independent module-relative paths. By
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2405 default, these paths are relative to the calling module's
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2406 directory; but if the "package" argument is specified, then
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2407 they are relative to that package. To ensure os-independence,
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2408 "filename" should use "/" characters to separate path
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2409 segments, and may not be an absolute path (i.e., it may not
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2410 begin with "/").
33529
25e07c44e5d0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33522
diff changeset
2411
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2412 If "module_relative" is False, then the given file paths are
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2413 interpreted as os-specific paths. These paths may be absolute
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2414 or relative (to the current working directory).
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2415
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2416 package
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2417 A Python package or the name of a Python package whose directory
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2418 should be used as the base directory for module relative paths.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2419 If "package" is not specified, then the calling module's
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2420 directory is used as the base directory for module relative
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2421 filenames. It is an error to specify "package" if
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2422 "module_relative" is False.
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2423
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2424 setUp
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
2425 A set-up function. This is called before running the
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2426 tests in each file. The setUp function will be passed a DocTest
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2427 object. The setUp function can access the test globals as the
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2428 globs attribute of the test passed.
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2429
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2430 tearDown
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
2431 A tear-down function. This is called after running the
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2432 tests in each file. The tearDown function will be passed a DocTest
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2433 object. The tearDown function can access the test globals as the
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2434 globs attribute of the test passed.
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2435
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2436 globs
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2437 A dictionary containing initial global variables for the tests.
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2438
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2439 optionflags
33594
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
2440 A set of doctest option flags expressed as an integer.
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
2441
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
2442 parser
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
2443 A DocTestParser (or subclass) that should be used to extract
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
2444 tests from the files.
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2445
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2446 encoding
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2447 An encoding that will be used to convert the files to unicode.
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2448 """
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2449 suite = unittest.TestSuite()
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2450
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2451 # We do this here so that _normalize_module is called at the right
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2452 # level. If it were called in DocFileTest, then this function
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2453 # would be the caller and we might guess the package incorrectly.
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2454 if kw.get('module_relative', True):
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2455 kw['package'] = _normalize_module(kw.get('package'))
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2456
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2457 for path in paths:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2458 suite.addTest(DocFileTest(path, **kw))
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2459
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2460 return suite
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2461
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2462 ######################################################################
46957
9d46c289d166 Remove deprecated Tester class from doctest module.
Georg Brandl <georg@python.org>
parents: 44643
diff changeset
2463 ## 8. Debugging Support
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2464 ######################################################################
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2465
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2466 def script_from_examples(s):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2467 r"""Extract script from text with examples.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2468
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2469 Converts text with examples to a Python script. Example input is
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2470 converted to regular code. Example output and all other words
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2471 are converted to comments:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2472
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2473 >>> text = '''
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2474 ... Here are examples of simple math.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2475 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2476 ... Python has super accurate integer addition
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2477 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2478 ... >>> 2 + 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2479 ... 5
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2480 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2481 ... And very friendly error messages:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2482 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2483 ... >>> 1/0
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2484 ... To Infinity
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2485 ... And
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2486 ... Beyond
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2487 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2488 ... You can use logic if you want:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2489 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2490 ... >>> if 0:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2491 ... ... blah
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2492 ... ... blah
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2493 ... ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2494 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2495 ... Ho hum
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2496 ... '''
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2497
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
2498 >>> print(script_from_examples(text))
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2499 # Here are examples of simple math.
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2500 #
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2501 # Python has super accurate integer addition
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2502 #
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2503 2 + 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2504 # Expected:
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2505 ## 5
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2506 #
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2507 # And very friendly error messages:
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2508 #
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2509 1/0
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2510 # Expected:
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2511 ## To Infinity
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2512 ## And
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2513 ## Beyond
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2514 #
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2515 # You can use logic if you want:
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2516 #
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2517 if 0:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2518 blah
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2519 blah
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2520 #
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2521 # Ho hum
35167
021a4abf7f27 Fix test cases for doctest.
Georg Brandl <georg@python.org>
parents: 35161
diff changeset
2522 <BLANKLINE>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2523 """
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2524 output = []
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2525 for piece in DocTestParser().parse(s):
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2526 if isinstance(piece, Example):
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2527 # Add the example's source code (strip trailing NL)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2528 output.append(piece.source[:-1])
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2529 # Add the expected output:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2530 want = piece.want
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2531 if want:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2532 output.append('# Expected:')
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2533 output += ['## '+l for l in want.split('\n')[:-1]]
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2534 else:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2535 # Add non-example text.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2536 output += [_comment_line(l)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2537 for l in piece.split('\n')[:-1]]
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2538
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2539 # Trim junk on both ends.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2540 while output and output[-1] == '#':
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2541 output.pop()
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2542 while output and output[0] == '#':
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2543 output.pop(0)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2544 # Combine the output, and return it.
35161
b33a7264454f bug [ 1172785 ] doctest.script_from_examples() result sometimes un-exec-able
Georg Brandl <georg@python.org>
parents: 34829
diff changeset
2545 # Add a courtesy newline to prevent exec from choking (see bug #1172785)
b33a7264454f bug [ 1172785 ] doctest.script_from_examples() result sometimes un-exec-able
Georg Brandl <georg@python.org>
parents: 34829
diff changeset
2546 return '\n'.join(output) + '\n'
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2547
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2548 def testsource(module, name):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2549 """Extract the test sources from a doctest docstring as a script.
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2550
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2551 Provide the module (or dotted name of the module) containing the
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2552 test to be debugged and the name (within the module) of the object
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2553 with the doc string with tests to be debugged.
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2554 """
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2555 module = _normalize_module(module)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2556 tests = DocTestFinder().find(module)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2557 test = [t for t in tests if t.name == name]
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2558 if not test:
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2559 raise ValueError(name, "not found in tests")
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2560 test = test[0]
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2561 testsrc = script_from_examples(test.docstring)
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2562 return testsrc
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2563
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2564 def debug_src(src, pm=False, globs=None):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2565 """Debug a single doctest docstring, in argument `src`'"""
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2566 testsrc = script_from_examples(src)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2567 debug_script(testsrc, pm, globs)
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2568
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2569 def debug_script(src, pm=False, globs=None):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2570 "Debug a test script. `src` is the script, as a string."
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2571 import pdb
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2572
71093
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2573 if globs:
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2574 globs = globs.copy()
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2575 else:
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2576 globs = {}
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2577
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2578 if pm:
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2579 try:
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2580 exec(src, globs, globs)
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2581 except:
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2582 print(sys.exc_info()[1])
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2583 p = pdb.Pdb(nosigint=True)
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2584 p.reset()
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2585 p.interaction(None, sys.exc_info()[2])
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2586 else:
bafc5c7d24b2 Issue #12451: doctest.debug_script() doesn't create a temporary file anymore to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
2587 pdb.Pdb(nosigint=True).run("exec(%r)" % src, globs, globs)
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2588
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2589 def debug(module, name, pm=False):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2590 """Debug a single doctest docstring.
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2591
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2592 Provide the module (or dotted name of the module) containing the
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2593 test to be debugged and the name (within the module) of the object
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2594 with the docstring with tests to be debugged.
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2595 """
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2596 module = _normalize_module(module)
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2597 testsrc = testsource(module, name)
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2598 debug_script(testsrc, pm, module.__dict__)
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2599
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2600 ######################################################################
46957
9d46c289d166 Remove deprecated Tester class from doctest module.
Georg Brandl <georg@python.org>
parents: 44643
diff changeset
2601 ## 9. Example Usage
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2602 ######################################################################
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2603 class _TestClass:
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2604 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2605 A pointless class, for sanity-checking of docstring testing.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2606
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2607 Methods:
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2608 square()
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2609 get()
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2610
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2611 >>> _TestClass(13).get() + _TestClass(-12).get()
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2612 1
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2613 >>> hex(_TestClass(13).square().get())
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2614 '0xa9'
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2615 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2616
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2617 def __init__(self, val):
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2618 """val -> _TestClass object with associated value val.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2619
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2620 >>> t = _TestClass(123)
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
2621 >>> print(t.get())
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2622 123
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2623 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2624
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2625 self.val = val
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2626
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2627 def square(self):
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2628 """square() -> square TestClass's associated value
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2629
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2630 >>> _TestClass(13).square().get()
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2631 169
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2632 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2633
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2634 self.val = self.val ** 2
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2635 return self
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2636
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2637 def get(self):
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2638 """get() -> return TestClass's associated value.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2639
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2640 >>> x = _TestClass(-42)
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
2641 >>> print(x.get())
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2642 -42
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2643 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2644
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2645 return self.val
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2646
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2647 __test__ = {"_TestClass": _TestClass,
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2648 "string": r"""
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2649 Example of a string object, searched as-is.
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2650 >>> x = 1; y = 2
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2651 >>> x + y, x * y
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2652 (3, 2)
29623
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2653 """,
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2654
29623
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2655 "bool-int equivalence": r"""
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2656 In 2.2, boolean expressions displayed
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2657 0 or 1. By default, we still accept
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2658 them. This can be disabled by passing
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2659 DONT_ACCEPT_TRUE_FOR_1 to the new
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2660 optionflags argument.
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2661 >>> 4 == 4
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2662 1
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2663 >>> 4 == 4
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2664 True
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2665 >>> 4 > 4
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2666 0
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2667 >>> 4 > 4
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2668 False
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
2669 """,
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2670
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2671 "blank lines": r"""
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2672 Blank lines can be marked with <BLANKLINE>:
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
2673 >>> print('foo\n\nbar\n')
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2674 foo
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2675 <BLANKLINE>
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2676 bar
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2677 <BLANKLINE>
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2678 """,
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2679
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2680 "ellipsis": r"""
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2681 If the ellipsis flag is used, then '...' can be used to
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2682 elide substrings in the desired output:
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41592
diff changeset
2683 >>> print(list(range(1000))) #doctest: +ELLIPSIS
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2684 [0, 1, 2, ..., 999]
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2685 """,
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2686
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2687 "whitespace normalization": r"""
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2688 If the whitespace normalization flag is used, then
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2689 differences in whitespace are ignored.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41592
diff changeset
2690 >>> print(list(range(30))) #doctest: +NORMALIZE_WHITESPACE
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2691 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2692 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2693 27, 28, 29]
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2694 """,
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2695 }
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2696
57829
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2697
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2698 def _test():
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40910
diff changeset
2699 testfiles = [arg for arg in sys.argv[1:] if arg and arg[0] != '-']
57829
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2700 if not testfiles:
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2701 name = os.path.basename(sys.argv[0])
57863
7559b70f9899 Merged revisions 76975 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 57829
diff changeset
2702 if '__loader__' in globals(): # python -m
57829
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2703 name, _ = os.path.splitext(name)
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2704 print("usage: {0} [-v] file ...".format(name))
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2705 return 2
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2706 for filename in testfiles:
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2707 if filename.endswith(".py"):
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2708 # It is a module -- insert its dir into sys.path and try to
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2709 # import it. If it is part of a package, that possibly
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2710 # won't work because of package imports.
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2711 dirname, filename = os.path.split(filename)
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2712 sys.path.insert(0, dirname)
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2713 m = __import__(filename[:-3])
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2714 del sys.path[0]
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2715 failures, _ = testmod(m)
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2716 else:
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2717 failures, _ = testfile(filename, module_relative=False)
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2718 if failures:
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2719 return 1
44230
6b517ac10e8e Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44036
diff changeset
2720 return 0
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2721
57829
58133714189c Merged revisions 76937 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55606
diff changeset
2722
15907
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2723 if __name__ == "__main__":
44230
6b517ac10e8e Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44036
diff changeset
2724 sys.exit(_test())