annotate Lib/doctest.py @ 54294:dba767bc3155 legacy-trunk

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