annotate Lib/doctest.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b48ef168d8c5
children d234dd21374a
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',
37967
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37576
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',
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
83 # 7. Tester
19359
eff8ca679bf6 Remove the horrid generators hack from doctest.py. This relies on a
Tim Peters <tim.peters@gmail.com>
parents: 18633
diff changeset
84 'Tester',
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
85 # 8. 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
86 'DocTestSuite',
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
87 'DocFileSuite',
33579
8a5c87f685cd Add set_unittest_reportflags() to the public API. Docs will follow
Tim Peters <tim.peters@gmail.com>
parents: 33578
diff changeset
88 'set_unittest_reportflags',
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
89 # 9. Debugging Support
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
90 '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
91 'testsource',
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
92 '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
93 '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
94 ]
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
95
19359
eff8ca679bf6 Remove the horrid generators hack from doctest.py. This relies on a
Tim Peters <tim.peters@gmail.com>
parents: 18633
diff changeset
96 import __future__
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
97
38769
189304c9d07e Remove unused import
Neal Norwitz <nnorwitz@gmail.com>
parents: 38601
diff changeset
98 import sys, traceback, inspect, linecache, os, re
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33062
diff changeset
99 import unittest, difflib, pdb, tempfile
33040
5a4d77b6efa6 Deprecated testmod's useless & confusing isprivate gimmick.
Tim Peters <tim.peters@gmail.com>
parents: 32983
diff changeset
100 import warnings
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
101 from StringIO import StringIO
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
102 from collections import namedtuple
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
103
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
104 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
105
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
106 # 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
107 # - 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
108 # - 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
109 # 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
110 # - 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
111 # its contained objects' docstrings.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
112 # - 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
113 #
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
114 # 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
115 #
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
116 # list of:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
117 # +------+ +---------+ +-------+
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
118 # |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
119 # +------+ +---------+ +-------+
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
120 # | Example |
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
121 # | ... |
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
122 # | Example |
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
123 # +---------+
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
124
33086
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
125 # 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
126
33086
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
127 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
128 def register_optionflag(name):
38136
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
129 # Create a new flag unless `name` is already known.
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
130 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
131
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
132 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
133 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
134 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
135 ELLIPSIS = register_optionflag('ELLIPSIS')
37967
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
136 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
137 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
138
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
139 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
140 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
141 NORMALIZE_WHITESPACE |
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
142 ELLIPSIS |
37967
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
143 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
144 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
145
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
146 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
147 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
148 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
149 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
150
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
151 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
152 REPORT_CDIFF |
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
153 REPORT_NDIFF |
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
154 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
155
33086
afd54126e9a1 - Added a register_optionflag function (so users can add their own
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33078
diff changeset
156 # 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
157 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
158 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
159
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
160 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
161 ## 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
162 ######################################################################
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
163 # 1. Utility Functions
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
164 # 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
165 # 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
166 # 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
167 # 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
168 # 6. Test Functions -- convenient wrappers for testing
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
169 # 7. Tester Class -- for backwards compatibility
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
170 # 8. Unittest Support
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
171 # 9. Debugging Support
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
172 # 10. 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
173
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
174 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
175 ## 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
176 ######################################################################
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
177
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
178 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
179 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
180 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
181 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
182 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
183 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
184 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
185 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
186 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
187 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
188 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
189
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
190 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
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 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
193 - 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
194 - 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
195 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
196 - 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
197 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
198 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
199 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
200 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
201 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
202 elif isinstance(module, (str, unicode)):
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 __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
204 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
205 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
206 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
207 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
208
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
209 def _load_testfile(filename, package, module_relative):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
210 if module_relative:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
211 package = _normalize_module(package, 3)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
212 filename = _module_relative_path(package, filename)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
213 if hasattr(package, '__loader__'):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
214 if hasattr(package.__loader__, 'get_data'):
43941
3b04894116fe Backport of r59082 (doctest and using __loader__.get_data()).
Brett Cannon <bcannon@gmail.com>
parents: 39700
diff changeset
215 file_contents = package.__loader__.get_data(filename)
3b04894116fe Backport of r59082 (doctest and using __loader__.get_data()).
Brett Cannon <bcannon@gmail.com>
parents: 39700
diff changeset
216 # get_data() opens files as 'rb', so one must do the equivalent
3b04894116fe Backport of r59082 (doctest and using __loader__.get_data()).
Brett Cannon <bcannon@gmail.com>
parents: 39700
diff changeset
217 # conversion as universal newlines would do.
3b04894116fe Backport of r59082 (doctest and using __loader__.get_data()).
Brett Cannon <bcannon@gmail.com>
parents: 39700
diff changeset
218 return file_contents.replace(os.linesep, '\n'), filename
65339
fea8f21b199d Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65335
diff changeset
219 with open(filename) as f:
fea8f21b199d Merged revisions 85503 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65335
diff changeset
220 return f.read(), filename
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
221
59180
891e79d770d8 Merged revisions 78488 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 51113
diff changeset
222 # Use sys.stdout encoding for ouput.
891e79d770d8 Merged revisions 78488 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 51113
diff changeset
223 _encoding = getattr(sys.__stdout__, 'encoding', None) or 'utf-8'
891e79d770d8 Merged revisions 78488 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 51113
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 """
59180
891e79d770d8 Merged revisions 78488 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 51113
diff changeset
227 Add the given number of space characters to the beginning of
891e79d770d8 Merged revisions 78488 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 51113
diff changeset
228 every non-blank line in `s`, and return the result.
891e79d770d8 Merged revisions 78488 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 51113
diff changeset
229 If the string `s` is Unicode, it is encoded using the stdout
891e79d770d8 Merged revisions 78488 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 51113
diff changeset
230 encoding and the `backslashreplace` error handler.
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
231 """
59180
891e79d770d8 Merged revisions 78488 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 51113
diff changeset
232 if isinstance(s, unicode):
891e79d770d8 Merged revisions 78488 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 51113
diff changeset
233 s = s.encode(_encoding, 'backslashreplace')
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
234 # 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
235 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
236
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
237 def _exception_traceback(exc_info):
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
238 """
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
239 Return a string containing a traceback message for the given
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
240 exc_info tuple (as returned by sys.exc_info()).
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
241 """
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
242 # Get a traceback message.
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
243 excout = StringIO()
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
244 exc_type, exc_val, exc_tb = exc_info
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
245 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
246 return excout.getvalue()
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
247
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
248 # 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
249 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
250 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
251 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
252 # 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
253 # 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
254 # 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
255 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
256 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
257 # Prevent softspace from screwing up the next test case, in
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
258 # case they used print with a trailing comma in an example.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
259 if hasattr(self, "softspace"):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
260 del self.softspace
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
261 return result
20213
d053726fb01b SF patch [#466616] Exclude imported items from doctest,
Tim Peters <tim.peters@gmail.com>
parents: 19359
diff changeset
262
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
263 def truncate(self, size=None):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
264 StringIO.truncate(self, size)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
265 if hasattr(self, "softspace"):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
266 del self.softspace
63508
97cb1dd8566e Merged revisions 83392,83426 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59953
diff changeset
267 if not self.buf:
97cb1dd8566e Merged revisions 83392,83426 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59953
diff changeset
268 # Reset it to an empty string, to make sure it's not unicode.
97cb1dd8566e Merged revisions 83392,83426 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59953
diff changeset
269 self.buf = ''
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
270
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
271 # 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
272 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
273 """
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
274 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
275 >>> _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
276 False
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
277 """
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
278 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
279 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
280
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
281 # 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
282 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
283 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
284
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
285 # 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
286 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
287 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
288 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
289 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
290 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
291 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
292 else:
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
293 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
294 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
295 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
296 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
297 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
298 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
299 else:
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
300 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
301
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
302 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
303 # 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
304 # _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
305 return False
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
306
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
307 # 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
308 # 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
309 # 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
310 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
311 # 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
312 # 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
313 # 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
314 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
315 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
316 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
317 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
318
33190
b0f2681bcf7f ellipsis_match(): Changed treatment of start- and end-of-string exact
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
319 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
320
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
321 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
322 "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
323 line = line.rstrip()
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
324 if line:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
325 return '# '+line
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
326 else:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
327 return '#'
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
328
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
329 class _OutputRedirectingPdb(pdb.Pdb):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
330 """
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
331 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
332 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
333 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
334 """
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
335 def __init__(self, out):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
336 self.__out = out
44014
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
337 self.__debugger_used = False
38147
7fe49dbcbbda Patch #721464: pdb.Pdb instances can now be given explicit stdin and
Georg Brandl <georg@python.org>
parents: 38136
diff changeset
338 pdb.Pdb.__init__(self, stdout=out)
63509
5fd09e410d36 Merged revisions 83429,83436 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63508
diff changeset
339 # still use input() to get user input
5fd09e410d36 Merged revisions 83429,83436 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63508
diff changeset
340 self.use_rawinput = 1
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
341
44014
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
342 def set_trace(self, frame=None):
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
343 self.__debugger_used = True
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
344 if frame is None:
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
345 frame = sys._getframe().f_back
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
346 pdb.Pdb.set_trace(self, frame)
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
347
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
348 def set_continue(self):
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
349 # Calling set_continue unconditionally would break unit test
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
350 # coverage reporting, as Bdb.set_continue calls sys.settrace(None).
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
351 if self.__debugger_used:
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
352 pdb.Pdb.set_continue(self)
e0465ba0e95d back in these go - thanks to Titus Brown for the fix
Skip Montanaro <skip@pobox.com>
parents: 44001
diff changeset
353
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
354 def trace_dispatch(self, *args):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
355 # 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
356 save_stdout = sys.stdout
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
357 sys.stdout = self.__out
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
358 # 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
359 try:
904771bef3f4 _OutputRedirectingPdb.trace_dispatch(): Return the base class's
Tim Peters <tim.peters@gmail.com>
parents: 33944
diff changeset
360 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
361 finally:
904771bef3f4 _OutputRedirectingPdb.trace_dispatch(): Return the base class's
Tim Peters <tim.peters@gmail.com>
parents: 33944
diff changeset
362 sys.stdout = save_stdout
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
363
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
364 # [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
365 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
366 if not inspect.ismodule(module):
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
367 raise TypeError, 'Expected a module: %r' % module
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
368 if path.startswith('/'):
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
369 raise ValueError, 'Module-relative files may not have absolute paths'
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
370
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
371 # 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
372 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
373 # 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
374 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
375 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
376 # An interactive session.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
377 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
378 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
379 else:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
380 basedir = os.curdir
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
381 else:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
382 # 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
383 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
384 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
385
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
386 # 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
387 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
388
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 ## 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
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 ## - 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
393 ## 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
394 ## "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
395 ## 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
396 ##
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
397 ## - 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
398 ## 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
399 ## 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
400
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
401 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
402 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
403 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
404 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
405
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
406 - 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
407 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
408
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
409 - 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
410 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
411 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
412 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
413
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
414 - 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
415 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
416 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
417 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
418 `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
419 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
420 if needed.
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
421
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
422 - 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
423 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
424 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
425
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
426 - 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
427 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
428 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
429
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
430 - 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
431 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
432 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
433 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
434 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
435 """
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
436 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
437 options=None):
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
438 # Normalize inputs.
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
439 if not source.endswith('\n'):
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
440 source += '\n'
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33059
diff changeset
441 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
442 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
443 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
444 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
445 # 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
446 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
447 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
448 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
449 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
450 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
451 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
452 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
453
74050
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
454 def __eq__(self, other):
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
455 if type(self) is not type(other):
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
456 return NotImplemented
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
457
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
458 return self.source == other.source and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
459 self.want == other.want and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
460 self.lineno == other.lineno and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
461 self.indent == other.indent and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
462 self.options == other.options and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
463 self.exc_msg == other.exc_msg
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
464
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
465 def __ne__(self, other):
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
466 return not self == other
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
467
74055
6a95820b9607 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74050
diff changeset
468 def __hash__(self):
6a95820b9607 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74050
diff changeset
469 return hash((self.source, self.want, self.lineno, self.indent,
6a95820b9607 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74050
diff changeset
470 self.exc_msg))
6a95820b9607 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74050
diff changeset
471
74050
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
472
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
473 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
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 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
476 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
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 - 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
479
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
480 - 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
481 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
482
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
483 - 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
484 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
485
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
486 - 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
487 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
488
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
489 - 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
490 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
491 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
492 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
493
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 - 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
495 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
496 """
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
497 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
498 """
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
499 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
500 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
501 """
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
502 assert not isinstance(examples, basestring), \
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
503 "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
504 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
505 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
506 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
507 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
508 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
509 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
510
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
511 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
512 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
513 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
514 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
515 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
516 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
517 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
518 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
519 (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
520
74050
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
521 def __eq__(self, other):
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
522 if type(self) is not type(other):
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
523 return NotImplemented
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
524
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
525 return self.examples == other.examples and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
526 self.docstring == other.docstring and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
527 self.globs == other.globs and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
528 self.name == other.name and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
529 self.filename == other.filename and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
530 self.lineno == other.lineno
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
531
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
532 def __ne__(self, other):
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
533 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
534
74055
6a95820b9607 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74050
diff changeset
535 def __hash__(self):
6a95820b9607 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74050
diff changeset
536 return hash((self.docstring, self.name, self.filename, self.lineno))
6a95820b9607 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74050
diff changeset
537
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
538 # This lets us sort tests by name:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
539 def __cmp__(self, other):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
540 if not isinstance(other, DocTest):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
541 return -1
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
542 return cmp((self.name, self.filename, self.lineno, id(self)),
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
543 (other.name, other.filename, other.lineno, id(other)))
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
544
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
545 ######################################################################
33195
7f57b3c7ebbf Updated __all__ to include every non-underscored class, function, and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33190
diff changeset
546 ## 3. DocTestParser
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
547 ######################################################################
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
548
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
549 class DocTestParser:
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
550 """
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
551 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
552 """
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
553 # 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
554 # string. It defines three groups: `source` is the source code
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
555 # (including leading indentation and prompts); `indent` is the
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
556 # 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
557 # `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
558 _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
559 # 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
560 (?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
561 (?:^(?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
562 (?:\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
563 \n?
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
564 # 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
565 (?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
566 (?![ ]*>>>) # 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
567 .*$\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
568 )*)
5e69f7c80ef4 Indent body of _EXAMPLE_RE for readability. _IS_BLANK_OR_COMMENT makes
Tim Peters <tim.peters@gmail.com>
parents: 33057
diff changeset
569 ''', re.MULTILINE | re.VERBOSE)
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
570
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
571 # 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
572 # 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
573 # - 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
574 # - 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
575 # - 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
576 # 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
577 # `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
578 # 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
579 # 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
580 _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
581 # 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
582 # 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
583 ^(?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
584 (?: 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
585 | innermost\ last
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
586 ) \) :
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
587 )
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
588 \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
589 (?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
590 ^ (?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
591 """, 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
592
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
593 # 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
594 # or contains a single comment.
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
595 _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
596
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
597 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
598 """
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
599 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
600 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
601 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
602 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
603 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
604 """
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
605 string = string.expandtabs()
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
606 # 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
607 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
608 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
609 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
610
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
611 output = []
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
612 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
613 # 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
614 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
615 # 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
616 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
617 # 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
618 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
619 # 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
620 (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
621 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
622 # 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
623 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
624 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
625 lineno=lineno,
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
626 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
627 options=options) )
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
628 # 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
629 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
630 # Update charno.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
631 charno = m.end()
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
632 # 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
633 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
634 return output
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
635
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
636 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
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 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
639 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
640
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 `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
642 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
643 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
644 """
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
645 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
646 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
647
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
648 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
649 """
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
650 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
651 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
652 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
653 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
654 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
655
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
656 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
657 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
658 """
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
659 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
660 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
661
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
662 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
663 """
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
664 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
665 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
666 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
667 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
668 stripped).
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
669
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
670 `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
671 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
672 """
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
673 # 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
674 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
675
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
676 # 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
677 # 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
678 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
679 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
680 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
681 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
682
33228
40b5aee20b83 _parse_example(): Simplified new code to preserve trailing spaces before
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
683 # 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
684 # 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
685 # be preserved, so plain rstrip() isn't good enough.
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
686 want = m.group('want')
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
687 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
688 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
689 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
690 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
691 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
692 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
693
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
694 # 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
695 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
696 if m:
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
697 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
698 else:
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
699 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
700
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
701 # 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
702 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
703
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
704 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
705
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
706 # 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
707 # 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
708 # 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
709 # 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
710 # "#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
711 # 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
712 # 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
713 _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
714 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
715
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
716 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
717 """
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
718 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
719 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
720
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
721 `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
722 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
723 """
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
724 options = {}
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
725 # (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
726 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
727 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
728 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
729 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
730 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
731 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
732 '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
733 (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
734 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
735 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
736 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
737 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
738 '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
739 (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
740 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
741
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
742 # 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
743 # 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
744 _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
745
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
746 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
747 "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
748 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
749 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
750 return min(indents)
33059
6f38e3b64ef4 Give return stmts their own lines.
Tim Peters <tim.peters@gmail.com>
parents: 33058
diff changeset
751 else:
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
752 return 0
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
753
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
754 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
755 """
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
756 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
757 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
758 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
759 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
760 """
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
761 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
762 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
763 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
764 '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
765 (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
766 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
767
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
768 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
769 """
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
770 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
771 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
772 """
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
773 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
774 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
775 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
776 '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
777 (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
778
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
779
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
780 ######################################################################
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
781 ## 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
782 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
783
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
784 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
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 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
787 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
788 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
789 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
790 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
791 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
792
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
793 def __init__(self, verbose=False, parser=DocTestParser(),
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38496
diff changeset
794 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
795 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
796 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
797
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
798 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
799 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
800 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
801 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
802 of the DocTest constructor.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
803
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
804 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
805 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
806
33473
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
807 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
808 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
809 """
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
810 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
811 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
812 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
813 self._exclude_empty = exclude_empty
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38496
diff changeset
814
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38496
diff changeset
815 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
816 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
817 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
818 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
819 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
820
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
821 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
822 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
823 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
824 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
825
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
826 - 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
827 - 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
828 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
829 - 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
830 - 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
831 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
832
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
833 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
834
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
835 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
836 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
837 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
838 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
839 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
840
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
841 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
842 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
843 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
844 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
845 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
846 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
847 to {}.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
848
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
849 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
850 # 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
851 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
852 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
853 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
854 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
855 "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
856 (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
857
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
858 # 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
859 # 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
860 # 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
861 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
862 module = None
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
863 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
864 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
865
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
866 # 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
867 # 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
868 # 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
869 try:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
870 file = inspect.getsourcefile(obj) or inspect.getfile(obj)
50185
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
871 if module is not None:
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
872 # Supply the module globals in case the module was
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
873 # originally loaded via a PEP 302 loader and
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
874 # file is not a valid filesystem path
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
875 source_lines = linecache.getlines(file, module.__dict__)
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
876 else:
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
877 # No access to a loader, so assume it's a normal
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
878 # filesystem path
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
879 source_lines = linecache.getlines(file)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
880 if not 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
881 source_lines = None
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
882 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
883 source_lines = None
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
884
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
885 # 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
886 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
887 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
888 globs = {}
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
889 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
890 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
891 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
892 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
893 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
894 globs.update(extraglobs)
51113
e718d29874d7 Issue 5021: doctest.testfile should set __name__
Raymond Hettinger <python@rcn.com>
parents: 50185
diff changeset
895 if '__name__' not in globs:
e718d29874d7 Issue 5021: doctest.testfile should set __name__
Raymond Hettinger <python@rcn.com>
parents: 50185
diff changeset
896 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
897
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
898 # 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
899 tests = []
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
900 self._find(tests, obj, name, module, source_lines, globs, {})
39227
078172eeaae5 Bug #1529297: The rewrite of doctest for Python 2.4 unintentionally
Tim Peters <tim.peters@gmail.com>
parents: 38769
diff changeset
901 # Sort the tests by alpha order of names, for consistency in
078172eeaae5 Bug #1529297: The rewrite of doctest for Python 2.4 unintentionally
Tim Peters <tim.peters@gmail.com>
parents: 38769
diff changeset
902 # verbose-mode output. This was a feature of doctest in Pythons
078172eeaae5 Bug #1529297: The rewrite of doctest for Python 2.4 unintentionally
Tim Peters <tim.peters@gmail.com>
parents: 38769
diff changeset
903 # <= 2.3 that got lost by accident in 2.4. It was repaired in
078172eeaae5 Bug #1529297: The rewrite of doctest for Python 2.4 unintentionally
Tim Peters <tim.peters@gmail.com>
parents: 38769
diff changeset
904 # 2.4.4 and 2.5.
078172eeaae5 Bug #1529297: The rewrite of doctest for Python 2.4 unintentionally
Tim Peters <tim.peters@gmail.com>
parents: 38769
diff changeset
905 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
906 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
907
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
908 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
909 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
910 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
911 module.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
912 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
913 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
914 return True
50002
5ef8b5a74efa Merged revisions 67245,67277,67289,67295,67301-67303,67307,67330,67332,67336,67355,67359,67362,67364,67367-67368,67370 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46955
diff changeset
915 elif inspect.getmodule(object) is not None:
5ef8b5a74efa Merged revisions 67245,67277,67289,67295,67301-67303,67307,67330,67332,67336,67355,67359,67362,67364,67367-67368,67370 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46955
diff changeset
916 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
917 elif inspect.isfunction(object):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
918 return module.__dict__ is object.func_globals
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
919 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
920 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
921 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
922 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
923 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
924 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
925 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
926 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
927
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
928 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
929 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
930 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
931 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
932 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
933 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
934 print 'Finding tests in %s' % name
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
935
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 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
937 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
938 return
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
939 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
940
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
941 # 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
942 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
943 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
944 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
945
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
946 # 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
947 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
948 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
949 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
950 # 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
951 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
952 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
953 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
954 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
955
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
956 # 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
957 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
958 for valname, val in getattr(obj, '__test__', {}).items():
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
959 if not isinstance(valname, basestring):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
960 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
961 "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
962 (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
963 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
964 inspect.ismethod(val) or inspect.ismodule(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
965 isinstance(val, basestring)):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
966 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
967 "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
968 "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
969 (type(val),))
33468
a04515a98831 DocTestFinder._find(): for tests derived from a module __test__ global,
Tim Peters <tim.peters@gmail.com>
parents: 33467
diff changeset
970 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
971 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
972 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
973
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
974 # 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
975 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
976 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
977 # 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
978 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
979 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
980 if isinstance(val, classmethod):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
981 val = getattr(obj, valname).im_func
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
982
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
983 # 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
984 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
985 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
986 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
987 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
988 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
989 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
990
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
991 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
992 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
993 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
994 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
995 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
996 # 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
997 # then return None (no test 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
998 if isinstance(obj, basestring):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
999 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
1000 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1001 try:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1002 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
1003 docstring = ''
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
1004 else:
33746
a27b0271711b Fixed a small bug. doctest didn't handle unicode docstrings containing
Jim Fulton <jim@zope.com>
parents: 33618
diff changeset
1005 docstring = obj.__doc__
a27b0271711b Fixed a small bug. doctest didn't handle unicode docstrings containing
Jim Fulton <jim@zope.com>
parents: 33618
diff changeset
1006 if not isinstance(docstring, basestring):
a27b0271711b Fixed a small bug. doctest didn't handle unicode docstrings containing
Jim Fulton <jim@zope.com>
parents: 33618
diff changeset
1007 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
1008 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
1009 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
1010
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1011 # 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
1012 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
1013
33469
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
1014 # 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
1015 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
1016 return None
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
1017
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1018 # 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
1019 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
1020 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
1021 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1022 filename = getattr(module, '__file__', module.__name__)
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1023 if filename[-4:] in (".pyc", ".pyo"):
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1024 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
1025 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
1026 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
1027
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1028 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
1029 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1030 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
1031 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
1032 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1033 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
1034
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1035 # 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
1036 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
1037 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
1038
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1039 # 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
1040 # 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
1041 # 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
1042 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
1043 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
1044 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
1045 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
1046 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
1047 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
1048 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
1049 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
1050 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
1051
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1052 # Find the line number for 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
1053 if inspect.ismethod(obj): obj = obj.im_func
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.isfunction(obj): obj = obj.func_code
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.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
1056 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
1057 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
1058 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
1059
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1060 # 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
1061 # 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
1062 # 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
1063 # 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
1064 # mark.
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 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
1066 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
1067 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
1068 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
1069 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
1070 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
1071 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
1072
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1073 # 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
1074 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
1075
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1076 ######################################################################
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
1077 ## 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
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
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1080 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
1081 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1082 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
1083 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
1084 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
1085 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
1086
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1087 >>> 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
1088 >>> runner = DocTestRunner(verbose=False)
38464
e989ee5d404a ("Forward-port" of r46506)
Armin Rigo <arigo@tunes.org>
parents: 38459
diff changeset
1089 >>> 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
1090 >>> for test in tests:
38464
e989ee5d404a ("Forward-port" of r46506)
Armin Rigo <arigo@tunes.org>
parents: 38459
diff changeset
1091 ... print test.name, '->', runner.run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
1092 _TestClass -> TestResults(failed=0, attempted=2)
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
1093 _TestClass.__init__ -> TestResults(failed=0, attempted=2)
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
1094 _TestClass.get -> TestResults(failed=0, attempted=2)
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
1095 _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
1096
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1097 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
1098 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
1099 tuple:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1100
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1101 >>> 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
1102 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
1103 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
1104 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
1105 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
1106 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
1107 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
1108 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
1109 Test passed.
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
1110 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
1111
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1112 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
1113 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
1114
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1115 >>> 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
1116 7
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1117 >>> 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
1118 0
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1119
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1120 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
1121 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
1122 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
1123 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
1124 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
1125 `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
1126
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1127 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
1128 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
1129 `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
1130 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
1131 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
1132 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
1133 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
1134 `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
1135 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1136 # 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
1137 # 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
1138 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
1139
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1140 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
1141 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1142 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
1143
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1144 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
1145 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
1146 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
1147
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1148 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
1149 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
1150 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
1151
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1152 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
1153 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
1154 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
1155 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
1156 """
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1157 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
1158 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
1159 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
1160 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
1161 self.optionflags = optionflags
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1162 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
1163
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1164 # 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
1165 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
1166 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
1167 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
1168
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1169 # 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
1170 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
1171
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1172 #/////////////////////////////////////////////////////////////////
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1173 # 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
1174 #/////////////////////////////////////////////////////////////////
20227
92f86436b6be SF bug [#467336] doctest failures w/ new-style classes.
Tim Peters <tim.peters@gmail.com>
parents: 20222
diff changeset
1175
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1176 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
1177 """
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1178 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
1179 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
1180 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1181 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
1182 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
1183 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
1184 '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
1185 else:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1186 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
1187 '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
1188
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1189 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
1190 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1191 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
1192 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
1193 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1194 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
1195 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
1196
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1197 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
1198 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1199 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
1200 """
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
1201 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
1202 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
1203
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1204 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
1205 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1206 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
1207 """
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
1208 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
1209 '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
1210
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
1211 def _failure_header(self, test, example):
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1212 out = [self.DIVIDER]
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1213 if test.filename:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1214 if test.lineno is not None and example.lineno is not None:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1215 lineno = test.lineno + example.lineno + 1
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1216 else:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1217 lineno = '?'
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1218 out.append('File "%s", line %s, in %s' %
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1219 (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
1220 else:
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1221 out.append('Line %s, in %s' % (example.lineno+1, test.name))
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1222 out.append('Failed example:')
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1223 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
1224 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
1225 return '\n'.join(out)
20213
d053726fb01b SF patch [#466616] Exclude imported items from doctest,
Tim Peters <tim.peters@gmail.com>
parents: 19359
diff changeset
1226
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1227 #/////////////////////////////////////////////////////////////////
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1228 # 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
1229 #/////////////////////////////////////////////////////////////////
20213
d053726fb01b SF patch [#466616] Exclude imported items from doctest,
Tim Peters <tim.peters@gmail.com>
parents: 19359
diff changeset
1230
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1231 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
1232 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1233 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
1234 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
1235 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
1236 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
1237 `(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
1238 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
1239 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
1240 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1241 # 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
1242 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
1243
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1244 # 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
1245 # 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
1246 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
1247
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1248 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
1249
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1250 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
1251
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1252 # Process each example.
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1253 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
1254
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
1255 # 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
1256 # 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
1257 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
1258 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
1259
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1260 # 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
1261 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
1262 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
1263 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
1264 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
1265 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
1266 else:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1267 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
1268
37967
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
1269 # If 'SKIP' is set, then skip this example.
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
1270 if self.optionflags & SKIP:
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
1271 continue
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
1272
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1273 # 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
1274 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
1275 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
1276 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
1277
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1278 # 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
1279 # 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
1280 # __patched_linecache_getlines).
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1281 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
1282
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1283 # 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
1284 # 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
1285 # 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
1286 try:
33061
ee43ca4a2c23 Repair some out-of-date comments.
Tim Peters <tim.peters@gmail.com>
parents: 33060
diff changeset
1287 # Don't blink! This is where the user's code gets run.
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1288 exec compile(example.source, filename, "single",
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1289 compileflags, 1) in test.globs
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1290 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
1291 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
1292 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
1293 raise
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1294 except:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1295 exception = sys.exc_info()
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1296 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
1297
33061
ee43ca4a2c23 Repair some out-of-date comments.
Tim Peters <tim.peters@gmail.com>
parents: 33060
diff changeset
1298 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
1299 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
1300 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
1301
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1302 # 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
1303 # 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
1304 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
1305 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
1306 outcome = SUCCESS
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1307
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1308 # 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
1309 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1310 exc_info = sys.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
1311 exc_msg = traceback.format_exception_only(*exc_info[:2])[-1]
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1312 if not quiet:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1313 got += _exception_traceback(exc_info)
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1314
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1315 # 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
1316 # 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
1317 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
1318 outcome = BOOM
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1319
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1320 # 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
1321 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
1322 outcome = SUCCESS
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1323
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1324 # 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
1325 elif self.optionflags & IGNORE_EXCEPTION_DETAIL:
61051
51e30b916fc4 Issue 7490: make IGNORE_EXCEPTION_DETAIL also ignore details of the module containing the exception under test (original patch by Lennart Regebro)
Nick Coghlan <ncoghlan@gmail.com>
parents: 59921
diff changeset
1326 m1 = re.match(r'(?:[^:]*\.)?([^:]*:)', example.exc_msg)
51e30b916fc4 Issue 7490: make IGNORE_EXCEPTION_DETAIL also ignore details of the module containing the exception under test (original patch by Lennart Regebro)
Nick Coghlan <ncoghlan@gmail.com>
parents: 59921
diff changeset
1327 m2 = re.match(r'(?:[^:]*\.)?([^:]*:)', exc_msg)
51e30b916fc4 Issue 7490: make IGNORE_EXCEPTION_DETAIL also ignore details of the module containing the exception under test (original patch by Lennart Regebro)
Nick Coghlan <ncoghlan@gmail.com>
parents: 59921
diff changeset
1328 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
1329 self.optionflags):
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1330 outcome = SUCCESS
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1331
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1332 # Report the outcome.
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1333 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
1334 if not quiet:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1335 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
1336 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
1337 if not quiet:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1338 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
1339 failures += 1
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1340 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
1341 if not quiet:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1342 self.report_unexpected_exception(out, test, example,
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1343 exc_info)
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1344 failures += 1
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1345 else:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33403
diff changeset
1346 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
1347
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1348 # 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
1349 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
1350
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1351 # 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
1352 self.__record_outcome(test, failures, tries)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
1353 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
1354
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1355 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
1356 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1357 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
1358 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
1359 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1360 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
1361 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
1362 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
1363 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
1364
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1365 __LINECACHE_FILENAME_RE = re.compile(r'<doctest '
65330
f3da8cd74113 Merged revisions 85495 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64078
diff changeset
1366 r'(?P<name>.+)'
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1367 r'\[(?P<examplenum>\d+)\]>$')
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
1368 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
1369 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
1370 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
1371 example = self.test.examples[int(m.group('examplenum'))]
65335
ceb12703c6db Fix the issue with non-ascii char in doctest. Issue #9409
Florent Xicluna <florent.xicluna@gmail.com>
parents: 65330
diff changeset
1372 source = example.source
ceb12703c6db Fix the issue with non-ascii char in doctest. Issue #9409
Florent Xicluna <florent.xicluna@gmail.com>
parents: 65330
diff changeset
1373 if isinstance(source, unicode):
ceb12703c6db Fix the issue with non-ascii char in doctest. Issue #9409
Florent Xicluna <florent.xicluna@gmail.com>
parents: 65330
diff changeset
1374 source = source.encode('ascii', 'backslashreplace')
59953
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59180
diff changeset
1375 return source.splitlines(True)
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1376 else:
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
1377 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
1378
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1379 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
1380 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1381 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
1382 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
1383
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1384 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
1385 `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
1386 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
1387 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
1388 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
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 `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
1391 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
1392 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
1393 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
1394
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1395 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
1396 `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
1397 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
1398 """
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1399 self.test = test
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1400
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1401 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
1402 compileflags = _extract_future_flags(test.globs)
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33062
diff changeset
1403
33073
bfcc4e39fc45 Edward's latest checkins somehow managed to wipe out my previous latest
Tim Peters <tim.peters@gmail.com>
parents: 33072
diff changeset
1404 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
1405 if out is None:
33073
bfcc4e39fc45 Edward's latest checkins somehow managed to wipe out my previous latest
Tim Peters <tim.peters@gmail.com>
parents: 33072
diff changeset
1406 out = save_stdout.write
bfcc4e39fc45 Edward's latest checkins somehow managed to wipe out my previous latest
Tim Peters <tim.peters@gmail.com>
parents: 33072
diff changeset
1407 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
1408
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1409 # 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
1410 # 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
1411 # 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
1412 # 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
1413 # 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
1414 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
1415 self.debugger = _OutputRedirectingPdb(save_stdout)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1416 self.debugger.reset()
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1417 pdb.set_trace = self.debugger.set_trace
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33062
diff changeset
1418
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1419 # 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
1420 # 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
1421 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
1422 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
1423
63509
5fd09e410d36 Merged revisions 83429,83436 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63508
diff changeset
1424 # Make sure sys.displayhook just prints the value to stdout
5fd09e410d36 Merged revisions 83429,83436 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63508
diff changeset
1425 save_displayhook = sys.displayhook
5fd09e410d36 Merged revisions 83429,83436 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63508
diff changeset
1426 sys.displayhook = sys.__displayhook__
5fd09e410d36 Merged revisions 83429,83436 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63508
diff changeset
1427
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1428 try:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1429 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
1430 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
1431 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
1432 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
1433 linecache.getlines = self.save_linecache_getlines
63509
5fd09e410d36 Merged revisions 83429,83436 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63508
diff changeset
1434 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
1435 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
1436 test.globs.clear()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1437
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1438 #/////////////////////////////////////////////////////////////////
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1439 # Summarization
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1440 #/////////////////////////////////////////////////////////////////
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
1441 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
1442 """
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1443 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
1444 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
1445 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
1446 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
1447
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1448 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
1449 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
1450 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
1451 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1452 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
1453 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
1454 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
1455 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
1456 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
1457 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
1458 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
1459 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
1460 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
1461 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
1462 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
1463 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
1464 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
1465 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
1466 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
1467 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
1468 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
1469 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
1470 if 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
1471 print len(notests), "items had no 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
1472 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
1473 for thing in 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
1474 print " ", thing
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1475 if 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
1476 print len(passed), "items passed all 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
1477 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
1478 for thing, count in 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
1479 print " %3d tests in %s" % (count, thing)
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1480 if failed:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1481 print self.DIVIDER
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
1482 print len(failed), "items had failures:"
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1483 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
1484 for thing, (f, t) in 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
1485 print " %3d of %3d in %s" % (f, t, thing)
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1486 if verbose:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1487 print totalt, "tests in", len(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
1488 print totalt - totalf, "passed and", totalf, "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
1489 if totalf:
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 print "***Test Failed***", totalf, "failures."
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1491 elif 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
1492 print "Test passed."
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
1493 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
1494
33467
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1495 #/////////////////////////////////////////////////////////////////
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1496 # Backward compatibility cruft to maintain doctest.master.
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1497 #/////////////////////////////////////////////////////////////////
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1498 def merge(self, other):
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1499 d = self._name2ft
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1500 for name, (f, t) in other._name2ft.items():
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1501 if name in d:
50185
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
1502 # Don't print here by default, since doing
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
1503 # so breaks some of the buildbots
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
1504 #print "*** DocTestRunner.merge: '" + name + "' in both" \
edd72437cec3 Merged revisions 67790 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50002
diff changeset
1505 # " testers; summing outcomes."
33467
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1506 f2, t2 = d[name]
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1507 f = f + f2
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1508 t = t + t2
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1509 d[name] = f, t
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1510
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1511 class OutputChecker:
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1512 """
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1513 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
1514 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
1515 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
1516 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
1517 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
1518 """
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1519 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
1520 """
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33086
diff changeset
1521 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
1522 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
1523 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
1524 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
1525 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
1526 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
1527 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
1528 """
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1529 # 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
1530 # 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
1531 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
1532 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1533
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1534 # 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
1535 # 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
1536 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
1537 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
1538 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1539 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
1540 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1541
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1542 # <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
1543 # 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
1544 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
1545 # 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
1546 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
1547 '', want)
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1548 # 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
1549 # spaces.
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1550 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
1551 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
1552 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1553
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1554 # 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
1555 # contents of whitespace strings. Note that this can be used
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
1556 # 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
1557 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
1558 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
1559 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
1560 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
1561 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1562
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1563 # 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
1564 # 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
1565 if optionflags & ELLIPSIS:
33200
26563204660c Gave _ellipsis_match() an attractive new leading underscore.
Tim Peters <tim.peters@gmail.com>
parents: 33196
diff changeset
1566 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
1567 return True
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1568
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1569 # 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
1570 return False
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1571
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1572 # Should we do a fancy diff?
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1573 def _do_a_fancy_diff(self, want, got, optionflags):
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1574 # 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
1575 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
1576 REPORT_CDIFF |
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1577 REPORT_NDIFF):
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1578 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
1579
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1580 # 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
1581 # 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
1582 # 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
1583 # [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
1584 # 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
1585 ##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
1586 ## 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
1587
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1588 # 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
1589 # 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
1590 if optionflags & REPORT_NDIFF:
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1591 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
1592
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1593 # 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
1594 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
1595
33287
449141aa011c Changed OutputChecker.output_difference to expect an Example object,
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33286
diff changeset
1596 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
1597 """
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1598 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
1599 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
1600 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
1601 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
1602 """
33287
449141aa011c Changed OutputChecker.output_difference to expect an Example object,
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33286
diff changeset
1603 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
1604 # 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
1605 # 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
1606 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
1607 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
1608
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
1609 # Check if we should use diff.
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1610 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
1611 # 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
1612 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
1613 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
1614 # 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
1615 if optionflags & REPORT_UDIFF:
33283
2747dbd5938b Shortened diff output for unified & context diffs
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33282
diff changeset
1616 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
1617 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
1618 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
1619 elif optionflags & REPORT_CDIFF:
33283
2747dbd5938b Shortened diff output for unified & context diffs
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33282
diff changeset
1620 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
1621 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
1622 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
1623 elif optionflags & REPORT_NDIFF:
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1624 engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK)
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1625 diff = list(engine.compare(want_lines, got_lines))
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33229
diff changeset
1626 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
1627 else:
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1628 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
1629 # 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
1630 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
1631 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
1632
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
1633 # 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
1634 # 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
1635 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
1636 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
1637 elif want:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1638 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
1639 elif got:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1640 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
1641 else:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
1642 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
1643
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1644 class DocTestFailure(Exception):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1645 """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
1646
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1647 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
1648
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1649 - 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
1650
39700
218b6e900a8d Fix a couple of typos.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39227
diff changeset
1651 - 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
1652
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1653 - got: the actual output
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1654 """
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1655 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
1656 self.test = test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1657 self.example = example
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1658 self.got = got
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1659
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1660 def __str__(self):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1661 return str(self.test)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1662
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1663 class UnexpectedException(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 encountered an unexpected exception
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
39700
218b6e900a8d Fix a couple of typos.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39227
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 - 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
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, exc_info):
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.exc_info = exc_info
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)
32983
726db5854746 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1681
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1682 class DebugRunner(DocTestRunner):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1683 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
1684
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1685 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
1686 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
1687
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1688 >>> 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
1689 >>> 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
1690 ... {}, '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
1691 >>> try:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1692 ... runner.run(test)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1693 ... except UnexpectedException, failure:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1694 ... pass
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1695
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1696 >>> failure.test is test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1697 True
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1698
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1699 >>> failure.example.want
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1700 '42\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1701
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1702 >>> exc_info = failure.exc_info
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1703 >>> raise exc_info[0], exc_info[1], exc_info[2]
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1704 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
1705 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1706 KeyError
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1707
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1708 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
1709 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
1710
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1711 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
1712
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
1713 >>> 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
1714 ... >>> x = 1
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1715 ... >>> x
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1716 ... 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1717 ... ''', {}, 'foo', 'foo.py', 0)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1718
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1719 >>> try:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1720 ... runner.run(test)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1721 ... except DocTestFailure, failure:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1722 ... pass
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1723
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1724 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
1725
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1726 >>> failure.test is test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1727 True
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1728
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1729 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
1730
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1731 >>> failure.example.want
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1732 '2\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1733
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1734 and the actual output:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1735
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1736 >>> failure.got
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1737 '1\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1738
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1739 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
1740
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1741 >>> del test.globs['__builtins__']
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1742 >>> test.globs
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1743 {'x': 1}
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1744
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
1745 >>> 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
1746 ... >>> x = 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1747 ... >>> raise KeyError
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1748 ... ''', {}, 'foo', 'foo.py', 0)
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 >>> runner.run(test)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1751 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
1752 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1753 UnexpectedException: <DocTest foo from foo.py:0 (2 examples)>
32983
726db5854746 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1754
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1755 >>> del test.globs['__builtins__']
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1756 >>> test.globs
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1757 {'x': 2}
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1758
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1759 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
1760
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
1761 >>> 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
1762 ... >>> x = 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1763 ... ''', {}, 'foo', 'foo.py', 0)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1764
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1765 >>> runner.run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
1766 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
1767
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1768 >>> test.globs
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1769 {}
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1770
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1771 """
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1772
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1773 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
1774 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
1775 if clear_globs:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1776 test.globs.clear()
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1777 return r
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1778
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1779 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
1780 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
1781
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1782 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
1783 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
1784
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1785 ######################################################################
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
1786 ## 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
1787 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1788 # 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
1789
33467
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1790 # For backward compatibility, a global instance of a DocTestRunner
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1791 # class, updated by testmod.
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1792 master = None
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1793
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38496
diff changeset
1794 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
1795 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
1796 raise_on_error=False, exclude_empty=False):
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38496
diff changeset
1797 """m=None, name=None, globs=None, verbose=None, report=True,
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38496
diff changeset
1798 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
1799 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
1800
26338
ac7872e55405 Patch #486438: Make module argument to testmod optional.
Martin v. Löwis <martin@v.loewis.de>
parents: 23620
diff changeset
1801 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
1802 from module m (or the current module if m is not supplied), starting
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38496
diff changeset
1803 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
1804
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1805 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
1806 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
1807 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
1808 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
1809
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1810 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
1811
64078
756f8b398230 Merged revisions 84106 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63452
diff changeset
1812 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
1813
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1814 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
1815 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
1816
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1817 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
1818 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
1819 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
1820 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
1821
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1822 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
1823 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
1824 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
1825
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 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
1827 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
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 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
1830 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
1831 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
1832
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
1833 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
1834 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
1835 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
1836
853e6a2b9068 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
Tim Peters <tim.peters@gmail.com>
parents: 26338
diff changeset
1837 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
1838 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
1839 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
1840 ELLIPSIS
37967
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
1841 SKIP
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1842 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
1843 REPORT_UDIFF
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1844 REPORT_CDIFF
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1845 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
1846 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
1847
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1848 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
1849 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
1850 post-mortem debugged.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1851
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
1852 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
1853 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
1854 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
1855 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
1856 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
1857 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
1858 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
1859 """
33467
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1860 global master
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1861
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1862 # 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
1863 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
1864 # 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
1865 # 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
1866 # 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
1867 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
1868
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1869 # 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
1870 if not inspect.ismodule(m):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30448
diff changeset
1871 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
1872
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1873 # 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
1874 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
1875 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
1876
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1877 # Find, parse, and run all tests in the given module.
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38496
diff changeset
1878 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
1879
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1880 if raise_on_error:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1881 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
1882 else:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1883 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
1884
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
1885 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
1886 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
1887
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
1888 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
1889 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
1890
33467
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1891 if master is None:
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1892 master = runner
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1893 else:
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1894 master.merge(runner)
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
1895
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
1896 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
1897
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1898 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
1899 globs=None, verbose=None, report=True, optionflags=0,
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
1900 extraglobs=None, raise_on_error=False, parser=DocTestParser(),
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
1901 encoding=None):
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1902 """
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1903 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
1904
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1905 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
1906 should be interpreted:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1907
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1908 - 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
1909 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
1910 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
1911 "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
1912 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
1913 "/" 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
1914 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
1915
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1916 - 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
1917 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
1918 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
1919
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
1920 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
1921 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
1922
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1923 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
1924 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
1925 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
1926 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
1927 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
1928 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
1929
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1930 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
1931 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
1932 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
1933 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
1934
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1935 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
1936 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
1937 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
1938
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1939 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
1940 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
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 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
1943 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
1944 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
1945
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1946 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
1947 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
1948
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1949 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
1950 DONT_ACCEPT_BLANKLINE
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1951 NORMALIZE_WHITESPACE
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1952 ELLIPSIS
37967
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
1953 SKIP
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1954 IGNORE_EXCEPTION_DETAIL
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1955 REPORT_UDIFF
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1956 REPORT_CDIFF
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1957 REPORT_NDIFF
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1958 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
1959
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1960 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
1961 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
1962 post-mortem debugged.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1963
33594
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
1964 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
1965 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
1966
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
1967 Optional keyword arg "encoding" specifies an encoding that should
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
1968 be used to convert the file to unicode.
38496
6d79acdc812d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38464
diff changeset
1969
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1970 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
1971 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
1972 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
1973 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
1974 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
1975 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
1976 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
1977 """
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1978 global master
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1979
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1980 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
1981 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
1982 "relative paths.")
33529
25e07c44e5d0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33522
diff changeset
1983
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1984 # Relativize the path
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
1985 text, filename = _load_testfile(filename, package, module_relative)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1986
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1987 # 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
1988 if name is None:
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
1989 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
1990
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1991 # Assemble the globals.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1992 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
1993 globs = {}
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1994 else:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1995 globs = globs.copy()
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1996 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
1997 globs.update(extraglobs)
51113
e718d29874d7 Issue 5021: doctest.testfile should set __name__
Raymond Hettinger <python@rcn.com>
parents: 50185
diff changeset
1998 if '__name__' not in globs:
e718d29874d7 Issue 5021: doctest.testfile should set __name__
Raymond Hettinger <python@rcn.com>
parents: 50185
diff changeset
1999 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
2000
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2001 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
2002 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
2003 else:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2004 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
2005
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
2006 if encoding is not None:
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
2007 text = text.decode(encoding)
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
2008
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2009 # Read the file, convert it to a test, and run it.
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
2010 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
2011 runner.run(test)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2012
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2013 if report:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2014 runner.summarize()
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2015
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2016 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
2017 master = runner
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2018 else:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2019 master.merge(runner)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2020
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
2021 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
2022
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2023 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
2024 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
2025 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2026 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
2027 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
2028 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
2029 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
2030
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2031 `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
2032 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
2033 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
2034 `globs`.
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2035
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2036 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
2037 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
2038 information.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2039 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2040 # 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
2041 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
2042 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
2043 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
2044 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
2045
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2046 ######################################################################
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
2047 ## 7. Tester
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 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2049 # This is provided only for backwards compatibility. It's not
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2050 # actually used in any way.
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2051
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2052 class Tester:
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38496
diff changeset
2053 def __init__(self, mod=None, globs=None, verbose=None, optionflags=0):
33042
17fc88a1b873 Also deprecated the old Tester class, which is no longer used by anything
Tim Peters <tim.peters@gmail.com>
parents: 33041
diff changeset
2054
17fc88a1b873 Also deprecated the old Tester class, which is no longer used by anything
Tim Peters <tim.peters@gmail.com>
parents: 33041
diff changeset
2055 warnings.warn("class Tester is deprecated; "
17fc88a1b873 Also deprecated the old Tester class, which is no longer used by anything
Tim Peters <tim.peters@gmail.com>
parents: 33041
diff changeset
2056 "use class doctest.DocTestRunner instead",
17fc88a1b873 Also deprecated the old Tester class, which is no longer used by anything
Tim Peters <tim.peters@gmail.com>
parents: 33041
diff changeset
2057 DeprecationWarning, stacklevel=2)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2058 if mod is None and 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
2059 raise TypeError("Tester.__init__: must specify mod or globs")
33465
eb51728ed11f Tester.__init__(): this couldn't possibly work when a module argument
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
2060 if mod is not None and not inspect.ismodule(mod):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2061 raise TypeError("Tester.__init__: mod must be a module; %r" %
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2062 (mod,))
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2063 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
2064 globs = mod.__dict__
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2065 self.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
2066
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2067 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
2068 self.optionflags = optionflags
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38496
diff changeset
2069 self.testfinder = DocTestFinder()
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2070 self.testrunner = DocTestRunner(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
2071 optionflags=optionflags)
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2072
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2073 def runstring(self, s, name):
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
2074 test = DocTestParser().get_doctest(s, self.globs, name, None, 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
2075 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
2076 print "Running string", name
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2077 (f,t) = self.testrunner.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
2078 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
2079 print f, "of", t, "examples failed in string", name
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
2080 return TestResults(f,t)
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2081
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
2082 def rundoc(self, object, name=None, module=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
2083 f = t = 0
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2084 tests = self.testfinder.find(object, name, module=module,
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
2085 globs=self.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
2086 for test in tests:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2087 (f2, t2) = self.testrunner.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
2088 (f,t) = (f+f2, t+t2)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44217
diff changeset
2089 return TestResults(f,t)
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2090
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2091 def rundict(self, d, name, module=None):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 44013
diff changeset
2092 import types
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 44013
diff changeset
2093 m = types.ModuleType(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
2094 m.__dict__.update(d)
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
2095 if module is None:
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
2096 module = False
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33043
diff changeset
2097 return self.rundoc(m, name, module)
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2098
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2099 def run__test__(self, d, name):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 44013
diff changeset
2100 import types
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 44013
diff changeset
2101 m = types.ModuleType(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
2102 m.__test__ = d
33466
477cd0f622b9 Tester.run___test__(): This couldn't possibly work at all. I'm afraid
Tim Peters <tim.peters@gmail.com>
parents: 33465
diff changeset
2103 return self.rundoc(m, name)
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2104
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2105 def summarize(self, verbose=None):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2106 return self.testrunner.summarize(verbose)
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2107
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2108 def merge(self, other):
33467
e23ca94ed8ed Reluctantly, rehabilitate doctest.master.
Tim Peters <tim.peters@gmail.com>
parents: 33466
diff changeset
2109 self.testrunner.merge(other.testrunner)
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2110
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2111 ######################################################################
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
2112 ## 8. 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
2113 ######################################################################
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2114
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2115 _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
2116
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2117 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
2118 """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
2119
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2120 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
2121 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
2122
34829
b72df8aee56d SF patch 1167316: doctest.py fails self-test if run directly.
Tim Peters <tim.peters@gmail.com>
parents: 34215
diff changeset
2123 >>> import doctest
b72df8aee56d SF patch 1167316: doctest.py fails self-test if run directly.
Tim Peters <tim.peters@gmail.com>
parents: 34215
diff changeset
2124 >>> 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
2125 >>> 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
2126 ... 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
2127 True
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 >>> 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
2130 ... REPORT_ONLY_FIRST_FAILURE)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2131 True
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2132
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2133 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
2134
34829
b72df8aee56d SF patch 1167316: doctest.py fails self-test if run directly.
Tim Peters <tim.peters@gmail.com>
parents: 34215
diff changeset
2135 >>> 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
2136 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
2137 ...
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
2138 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
2139
34829
b72df8aee56d SF patch 1167316: doctest.py fails self-test if run directly.
Tim Peters <tim.peters@gmail.com>
parents: 34215
diff changeset
2140 >>> 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
2141 ... REPORT_ONLY_FIRST_FAILURE)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2142 True
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2143 """
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2144 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
2145
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
2146 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
2147 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
2148 old = _unittest_reportflags
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2149 _unittest_reportflags = flags
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2150 return old
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2151
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2152
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2153 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
2154
33056
29fa9d6afea0 - Split DocTestRunner's check_output and output_difference methods off
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33055
diff changeset
2155 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
2156 checker=None):
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2157
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2158 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
2159 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
2160 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
2161 self._dt_test = test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2162 self._dt_setUp = setUp
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2163 self._dt_tearDown = tearDown
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2164
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2165 def setUp(self):
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2166 test = self._dt_test
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2167
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2168 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
2169 self._dt_setUp(test)
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2170
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2171 def tearDown(self):
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2172 test = self._dt_test
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2173
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2174 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
2175 self._dt_tearDown(test)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2176
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2177 test.globs.clear()
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2178
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2179 def runTest(self):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2180 test = self._dt_test
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2181 old = sys.stdout
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2182 new = StringIO()
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2183 optionflags = self._dt_optionflags
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2184
33340
f30993d05890 The distinction between comparison flags and reporting flags isn't unique
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
2185 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
2186 # 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
2187 # 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
2188 optionflags |= _unittest_reportflags
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2189
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2190 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
2191 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
2192
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2193 try:
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2194 runner.DIVIDER = "-"*70
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2195 failures, tries = runner.run(
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2196 test, out=new.write, clear_globs=False)
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2197 finally:
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2198 sys.stdout = old
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2199
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2200 if failures:
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2201 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
2202
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2203 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
2204 test = self._dt_test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2205 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
2206 lineno = 'unknown line number'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2207 else:
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2208 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
2209 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
2210 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
2211 ' 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
2212 % (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
2213 )
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2214
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2215 def debug(self):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2216 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
2217
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2218 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
2219 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
2220 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
2221 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
2222
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2223 The DocTestCase provides a debug method that raises
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
2224 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
2225 exception:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2226
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
2227 >>> 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
2228 ... {}, 'foo', 'foo.py', 0)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2229 >>> case = DocTestCase(test)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2230 >>> try:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2231 ... case.debug()
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2232 ... except UnexpectedException, failure:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2233 ... pass
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2234
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2235 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
2236 the original exception:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2237
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2238 >>> failure.test is test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2239 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
2240
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2241 >>> failure.example.want
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2242 '42\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2243
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2244 >>> exc_info = failure.exc_info
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2245 >>> raise exc_info[0], exc_info[1], exc_info[2]
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2246 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
2247 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2248 KeyError
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2249
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2250 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
2251
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
2252 >>> 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
2253 ... >>> x = 1
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2254 ... >>> x
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2255 ... 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2256 ... ''', {}, 'foo', 'foo.py', 0)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2257 >>> case = DocTestCase(test)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2258
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2259 >>> try:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2260 ... case.debug()
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2261 ... except DocTestFailure, failure:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2262 ... pass
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2263
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2264 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
2265
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2266 >>> failure.test is test
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2267 True
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2268
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2269 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
2270
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2271 >>> failure.example.want
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2272 '2\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2273
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2274 and the actual output:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2275
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2276 >>> failure.got
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2277 '1\n'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2278
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2279 """
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2280
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2281 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
2282 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
2283 checker=self._dt_checker, verbose=False)
46955
99f50af692d0 #2767: don't clear globs in run() call, since they could be needed in tearDown,
Georg Brandl <georg@python.org>
parents: 46954
diff changeset
2284 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
2285 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
2286
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2287 def id(self):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2288 return self._dt_test.name
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2289
74050
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2290 def __eq__(self, other):
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2291 if type(self) is not type(other):
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2292 return NotImplemented
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2293
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2294 return self._dt_test == other._dt_test and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2295 self._dt_optionflags == other._dt_optionflags and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2296 self._dt_setUp == other._dt_setUp and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2297 self._dt_tearDown == other._dt_tearDown and \
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2298 self._dt_checker == other._dt_checker
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2299
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2300 def __ne__(self, other):
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2301 return not self == other
64d670a8b183 Issue #7502: Fix equality comparison for DocTestCase instances.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68588
diff changeset
2302
74055
6a95820b9607 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74050
diff changeset
2303 def __hash__(self):
6a95820b9607 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74050
diff changeset
2304 return hash((self._dt_optionflags, self._dt_setUp, self._dt_tearDown,
6a95820b9607 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74050
diff changeset
2305 self._dt_checker))
6a95820b9607 Followup to #7502: add __hash__ method and tests.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74050
diff changeset
2306
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2307 def __repr__(self):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2308 name = self._dt_test.name.split('.')
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2309 return "%s (%s)" % (name[-1], '.'.join(name[:-1]))
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2310
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2311 __str__ = __repr__
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2312
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2313 def shortDescription(self):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2314 return "Doctest: " + self._dt_test.name
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2315
59045
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2316 class SkipDocTestCase(DocTestCase):
75861
c50db3d06116 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74055
diff changeset
2317 def __init__(self, module):
c50db3d06116 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74055
diff changeset
2318 self.module = module
59045
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2319 DocTestCase.__init__(self, None)
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2320
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2321 def setUp(self):
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2322 self.skipTest("DocTestSuite will not work with -O2 and above")
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2323
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2324 def test_skip(self):
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2325 pass
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2326
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2327 def shortDescription(self):
75861
c50db3d06116 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74055
diff changeset
2328 return "Skipping tests from %s" % self.module.__name__
c50db3d06116 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74055
diff changeset
2329
c50db3d06116 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74055
diff changeset
2330 __str__ = shortDescription
c50db3d06116 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74055
diff changeset
2331
59045
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2332
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2333 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
2334 **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
2335 """
33229
7fec0662cf4a Type in docstring.
Tim Peters <tim.peters@gmail.com>
parents: 33228
diff changeset
2336 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
2337
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2338 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
2339 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
2340 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
2341 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
2342 (sometimes approximate) line number.
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2343
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2344 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
2345 can be either a module or a module name.
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2346
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2347 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
2348
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2349 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
2350
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2351 setUp
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
2352 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
2353 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
2354 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
2355 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
2356
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2357 tearDown
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
2358 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
2359 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
2360 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
2361 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
2362
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2363 globs
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2364 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
2365
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2366 optionflags
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2367 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
2368 """
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2369
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2370 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
2371 test_finder = DocTestFinder()
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2372
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2373 module = _normalize_module(module)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2374 tests = test_finder.find(module, globs=globs, extraglobs=extraglobs)
59045
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2375
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2376 if not tests and sys.flags.optimize >=2:
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2377 # Skip doctests when running with -O2
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2378 suite = unittest.TestSuite()
75861
c50db3d06116 #12757: Make doctest skipping in -OO mode work with unittest/regrtest -v
R David Murray <rdmurray@bitdance.com>
parents: 74055
diff changeset
2379 suite.addTest(SkipDocTestCase(module))
59045
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2380 return suite
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2381 elif not tests:
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2382 # 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
2383 # otherwise be hidden.
78961
b48ef168d8c5 #14649: clarify DocTestSuite error when there are no docstrings.
R David Murray <rdmurray@bitdance.com>
parents: 75861
diff changeset
2384 # It is probably a bug that this exception is not also raised if the
b48ef168d8c5 #14649: clarify DocTestSuite error when there are no docstrings.
R David Murray <rdmurray@bitdance.com>
parents: 75861
diff changeset
2385 # number of doctest examples in tests is zero (i.e. if no doctest
b48ef168d8c5 #14649: clarify DocTestSuite error when there are no docstrings.
R David Murray <rdmurray@bitdance.com>
parents: 75861
diff changeset
2386 # examples were found). However, we should probably not be raising
b48ef168d8c5 #14649: clarify DocTestSuite error when there are no docstrings.
R David Murray <rdmurray@bitdance.com>
parents: 75861
diff changeset
2387 # an exception at all here, though it is too late to make this change
b48ef168d8c5 #14649: clarify DocTestSuite error when there are no docstrings.
R David Murray <rdmurray@bitdance.com>
parents: 75861
diff changeset
2388 # for a maintenance release. See also issue #14649.
b48ef168d8c5 #14649: clarify DocTestSuite error when there are no docstrings.
R David Murray <rdmurray@bitdance.com>
parents: 75861
diff changeset
2389 raise ValueError(module, "has no docstrings")
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2390
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2391 tests.sort()
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2392 suite = unittest.TestSuite()
59045
a4c45f8e70aa Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents: 57860
diff changeset
2393
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2394 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
2395 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
2396 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
2397 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
2398 filename = module.__file__
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2399 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
2400 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
2401 test.filename = filename
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2402 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
2403
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2404 return suite
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2405
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2406 class DocFileCase(DocTestCase):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2407
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2408 def id(self):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2409 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
2410
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2411 def __repr__(self):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2412 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
2413 __str__ = __repr__
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2414
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2415 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
2416 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
2417 % (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
2418 )
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2419
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2420 def DocFileTest(path, module_relative=True, package=None,
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
2421 globs=None, parser=DocTestParser(),
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
2422 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
2423 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
2424 globs = {}
34215
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33946
diff changeset
2425 else:
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33946
diff changeset
2426 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
2427
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2428 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
2429 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
2430 "relative paths.")
33529
25e07c44e5d0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33522
diff changeset
2431
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2432 # Relativize the path.
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
2433 doc, path = _load_testfile(path, package, module_relative)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 35167
diff changeset
2434
34215
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33946
diff changeset
2435 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
2436 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
2437
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2438 # Find the file and read it.
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
2439 name = os.path.basename(path)
38496
6d79acdc812d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38464
diff changeset
2440
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
2441 # If an encoding is specified, use it to convert the file to unicode
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
2442 if encoding is not None:
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
2443 doc = doc.decode(encoding)
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2444
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2445 # 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
2446 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
2447 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
2448
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2449 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
2450 """A unittest suite for one or more doctest files.
33529
25e07c44e5d0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33522
diff changeset
2451
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2452 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
2453 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
2454 "module_relative".
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2455
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2456 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
2457
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2458 module_relative
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2459 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
2460 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
2461 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
2462 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
2463 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
2464 "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
2465 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
2466 begin with "/").
33529
25e07c44e5d0 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33522
diff changeset
2467
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2468 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
2469 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
2470 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
2471
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2472 package
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2473 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
2474 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
2475 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
2476 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
2477 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
2478 "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
2479
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2480 setUp
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
2481 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
2482 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
2483 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
2484 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
2485
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2486 tearDown
33533
e7fa7b5c9441 - Minor docstring fixes.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33529
diff changeset
2487 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
2488 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
2489 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
2490 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
2491
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2492 globs
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2493 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
2494
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33302
diff changeset
2495 optionflags
33594
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
2496 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
2497
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
2498 parser
e9370bcf02ad - Added a "parser" option to testfile() and DocFileTest().
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33582
diff changeset
2499 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
2500 tests from the files.
38496
6d79acdc812d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38464
diff changeset
2501
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
2502 encoding
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38147
diff changeset
2503 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
2504 """
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2505 suite = unittest.TestSuite()
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2506
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2507 # 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
2508 # 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
2509 # 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
2510 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
2511 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
2512
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2513 for path in paths:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2514 suite.addTest(DocFileTest(path, **kw))
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2515
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2516 return suite
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2517
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2518 ######################################################################
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
2519 ## 9. 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
2520 ######################################################################
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2521
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2522 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
2523 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
2524
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2525 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
2526 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
2527 are converted to comments:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2528
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2529 >>> text = '''
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2530 ... 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
2531 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2532 ... 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
2533 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2534 ... >>> 2 + 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2535 ... 5
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2536 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2537 ... 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
2538 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2539 ... >>> 1/0
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2540 ... To Infinity
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2541 ... And
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2542 ... Beyond
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2543 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2544 ... 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
2545 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2546 ... >>> if 0:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2547 ... ... blah
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2548 ... ... blah
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2549 ... ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2550 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2551 ... Ho hum
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2552 ... '''
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2553
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2554 >>> 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
2555 # 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
2556 #
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2557 # 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
2558 #
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2559 2 + 2
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2560 # 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
2561 ## 5
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2562 #
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2563 # 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
2564 #
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2565 1/0
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2566 # 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
2567 ## 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
2568 ## And
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2569 ## Beyond
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2570 #
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2571 # 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
2572 #
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2573 if 0:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2574 blah
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2575 blah
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2576 #
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
2577 # Ho hum
35167
021a4abf7f27 Fix test cases for doctest.
Georg Brandl <georg@python.org>
parents: 35161
diff changeset
2578 <BLANKLINE>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2579 """
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2580 output = []
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2581 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
2582 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
2583 # 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
2584 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
2585 # 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
2586 want = piece.want
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2587 if want:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2588 output.append('# Expected:')
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2589 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
2590 else:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2591 # 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
2592 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
2593 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
2594
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2595 # 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
2596 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
2597 output.pop()
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2598 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
2599 output.pop(0)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
2600 # 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
2601 # 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
2602 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
2603
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2604 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
2605 """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
2606
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2607 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
2608 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
2609 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
2610 """
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2611 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
2612 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
2613 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
2614 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
2615 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
2616 test = test[0]
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2617 testsrc = script_from_examples(test.docstring)
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2618 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
2619
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2620 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
2621 """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
2622 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
2623 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
2624
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2625 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
2626 "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
2627 import pdb
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2628
33241
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2629 # Note that tempfile.NameTemporaryFile() cannot be used. As the
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2630 # docs say, a file so created cannot be opened by name a second time
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2631 # on modern Windows boxes, and execfile() needs to open it.
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2632 srcfilename = tempfile.mktemp(".py", "doctestdebug")
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2633 f = open(srcfilename, 'w')
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2634 f.write(src)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2635 f.close()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2636
33241
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2637 try:
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2638 if globs:
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2639 globs = globs.copy()
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2640 else:
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2641 globs = {}
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2642
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2643 if pm:
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2644 try:
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2645 execfile(srcfilename, globs, globs)
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2646 except:
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2647 print sys.exc_info()[1]
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2648 pdb.post_mortem(sys.exc_info()[2])
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2649 else:
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2650 # Note that %r is vital here. '%s' instead can, e.g., cause
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2651 # backslashes to get treated as metacharacters on Windows.
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2652 pdb.run("execfile(%r)" % srcfilename, globs, globs)
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2653
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2654 finally:
2caa6f19a2e6 debug_script(): I changed this in haste before to take out the use of
Tim Peters <tim.peters@gmail.com>
parents: 33232
diff changeset
2655 os.remove(srcfilename)
29667
ba75af7ab136 Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
Tim Peters <tim.peters@gmail.com>
parents: 29652
diff changeset
2656
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2657 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
2658 """Debug a single doctest docstring.
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2659
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2660 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
2661 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
2662 with the docstring with tests to be debugged.
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2663 """
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2664 module = _normalize_module(module)
32668
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2665 testsrc = testsource(module, name)
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2666 debug_script(testsrc, pm, module.__dict__)
594996caff10 Ported some features from zope:
Jim Fulton <jim@zope.com>
parents: 32528
diff changeset
2667
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2668 ######################################################################
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
2669 ## 10. 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
2670 ######################################################################
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
2671 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
2672 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2673 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
2674
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2675 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
2676 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
2677 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
2678
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2679 >>> _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
2680 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
2681 >>> 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
2682 '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
2683 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2684
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2685 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
2686 """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
2687
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2688 >>> t = _TestClass(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
2689 >>> print t.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
2690 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
2691 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2692
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2693 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
2694
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2695 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
2696 """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
2697
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 >>> _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
2699 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
2700 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2701
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2702 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
2703 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
2704
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2705 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
2706 """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
2707
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2708 >>> x = _TestClass(-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
2709 >>> print x.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
2710 -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
2711 """
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2712
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2713 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
2714
1325bde94896 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2715 __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
2716 "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
2717 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
2718 >>> 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
2719 >>> 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
2720 (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
2721 """,
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2722
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
2723 "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
2724 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
2725 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
2726 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
2727 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
2728 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
2729 >>> 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
2730 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
2731 >>> 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
2732 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
2733 >>> 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
2734 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
2735 >>> 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
2736 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
2737 """,
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2738
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2739 "blank lines": r"""
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2740 Blank lines can be marked with <BLANKLINE>:
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2741 >>> print 'foo\n\nbar\n'
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2742 foo
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2743 <BLANKLINE>
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2744 bar
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2745 <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
2746 """,
33242
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2747
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2748 "ellipsis": r"""
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2749 If the ellipsis flag is used, then '...' can be used to
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2750 elide substrings in the desired output:
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2751 >>> print range(1000) #doctest: +ELLIPSIS
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2752 [0, 1, 2, ..., 999]
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2753 """,
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2754
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2755 "whitespace normalization": r"""
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2756 If the whitespace normalization flag is used, then
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2757 differences in whitespace are ignored.
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2758 >>> print range(30) #doctest: +NORMALIZE_WHITESPACE
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2759 [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
2760 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
2761 27, 28, 29]
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2762 """,
561702350e09 Misc cleanups.
Tim Peters <tim.peters@gmail.com>
parents: 33241
diff changeset
2763 }
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 32668
diff changeset
2764
57826
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2765
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
2766 def _test():
41023
4e835df2b4ba Patch #1663234: you can now run doctest on test files and modules
Georg Brandl <georg@python.org>
parents: 39700
diff changeset
2767 testfiles = [arg for arg in sys.argv[1:] if arg and arg[0] != '-']
57826
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2768 if not testfiles:
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2769 name = os.path.basename(sys.argv[0])
57860
13e62cf02bc3 Remove a leftover from a previous iteration of the issue 7376 patch.
R. David Murray <rdmurray@bitdance.com>
parents: 57826
diff changeset
2770 if '__loader__' in globals(): # python -m
57826
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2771 name, _ = os.path.splitext(name)
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2772 print("usage: {0} [-v] file ...".format(name))
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2773 return 2
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2774 for filename in testfiles:
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2775 if filename.endswith(".py"):
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2776 # It is a module -- insert its dir into sys.path and try to
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2777 # import it. If it is part of a package, that possibly
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2778 # won't work because of package imports.
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2779 dirname, filename = os.path.split(filename)
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2780 sys.path.insert(0, dirname)
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2781 m = __import__(filename[:-3])
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2782 del sys.path[0]
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2783 failures, _ = testmod(m)
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2784 else:
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2785 failures, _ = testfile(filename, module_relative=False)
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2786 if failures:
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2787 return 1
44217
0bb108a3c0c9 Fix issue #1530.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44034
diff changeset
2788 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
2789
57826
1c2b3f795d1b Issue #7376: When called with no arguments doctest was running a
R. David Murray <rdmurray@bitdance.com>
parents: 51113
diff changeset
2790
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
2791 if __name__ == "__main__":
44217
0bb108a3c0c9 Fix issue #1530.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44034
diff changeset
2792 sys.exit(_test())