Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 1 | # Module doctest. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2 | # Released to the public domain 16-Jan-2001, by Tim Peters (tim@python.org). |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 3 | # Major enhancements and refactoring by: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 4 | # Jim Fulton |
| 5 | # Edward Loper |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 6 | |
| 7 | # Provided as-is; use at your own risk; no warranty; no promises; enjoy! |
| 8 | |
Martin v. Löwis | 92816de | 2004-05-31 19:01:00 +0000 | [diff] [blame] | 9 | r"""Module doctest -- a framework for running examples in docstrings. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 10 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 11 | In simplest use, end each module M to be tested with: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 12 | |
| 13 | def _test(): |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 14 | import doctest |
Tim Peters | 48983fc | 2004-09-25 02:41:28 +0000 | [diff] [blame] | 15 | doctest.testmod() |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 16 | |
| 17 | if __name__ == "__main__": |
| 18 | _test() |
| 19 | |
| 20 | Then running the module as a script will cause the examples in the |
| 21 | docstrings to get executed and verified: |
| 22 | |
| 23 | python M.py |
| 24 | |
| 25 | This won't display anything unless an example fails, in which case the |
| 26 | failing example(s) and the cause(s) of the failure(s) are printed to stdout |
| 27 | (why not stderr? because stderr is a lame hack <0.2 wink>), and the final |
| 28 | line of output is "Test failed.". |
| 29 | |
| 30 | Run it with the -v switch instead: |
| 31 | |
| 32 | python M.py -v |
| 33 | |
| 34 | and a detailed report of all examples tried is printed to stdout, along |
| 35 | with assorted summaries at the end. |
| 36 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 37 | You can force verbose mode by passing "verbose=True" to testmod, or prohibit |
| 38 | it by passing "verbose=False". In either of those cases, sys.argv is not |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 39 | examined by testmod. |
| 40 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 41 | There are a variety of other ways to run doctests, including integration |
| 42 | with the unittest framework, and support for running non-Python text |
| 43 | files containing doctests. There are also many ways to override parts |
| 44 | of doctest's default behaviors. See the Library Reference Manual for |
| 45 | details. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 46 | """ |
Tim Peters | 48983fc | 2004-09-25 02:41:28 +0000 | [diff] [blame] | 47 | |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 48 | __docformat__ = 'reStructuredText en' |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 49 | |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 50 | __all__ = [ |
Edward Loper | b7503ff | 2004-08-19 19:19:03 +0000 | [diff] [blame] | 51 | # 0, Option Flags |
| 52 | 'register_optionflag', |
| 53 | 'DONT_ACCEPT_TRUE_FOR_1', |
| 54 | 'DONT_ACCEPT_BLANKLINE', |
| 55 | 'NORMALIZE_WHITESPACE', |
| 56 | 'ELLIPSIS', |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 57 | 'SKIP', |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 58 | 'IGNORE_EXCEPTION_DETAIL', |
Tim Peters | ba60196 | 2004-09-04 15:04:06 +0000 | [diff] [blame] | 59 | 'COMPARISON_FLAGS', |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 60 | 'REPORT_UDIFF', |
| 61 | 'REPORT_CDIFF', |
| 62 | 'REPORT_NDIFF', |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 63 | 'REPORT_ONLY_FIRST_FAILURE', |
Tim Peters | ba60196 | 2004-09-04 15:04:06 +0000 | [diff] [blame] | 64 | 'REPORTING_FLAGS', |
R David Murray | 5a9d706 | 2012-11-21 15:09:21 -0500 | [diff] [blame] | 65 | 'FAIL_FAST', |
Edward Loper | b7503ff | 2004-08-19 19:19:03 +0000 | [diff] [blame] | 66 | # 1. Utility Functions |
Edward Loper | b7503ff | 2004-08-19 19:19:03 +0000 | [diff] [blame] | 67 | # 2. Example & DocTest |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 68 | 'Example', |
| 69 | 'DocTest', |
Edward Loper | b7503ff | 2004-08-19 19:19:03 +0000 | [diff] [blame] | 70 | # 3. Doctest Parser |
| 71 | 'DocTestParser', |
| 72 | # 4. Doctest Finder |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 73 | 'DocTestFinder', |
Edward Loper | b7503ff | 2004-08-19 19:19:03 +0000 | [diff] [blame] | 74 | # 5. Doctest Runner |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 75 | 'DocTestRunner', |
Edward Loper | b7503ff | 2004-08-19 19:19:03 +0000 | [diff] [blame] | 76 | 'OutputChecker', |
| 77 | 'DocTestFailure', |
| 78 | 'UnexpectedException', |
| 79 | 'DebugRunner', |
| 80 | # 6. Test Functions |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 81 | 'testmod', |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 82 | 'testfile', |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 83 | 'run_docstring_examples', |
Georg Brandl | cc80bfe | 2008-05-12 18:06:23 +0000 | [diff] [blame] | 84 | # 7. Unittest Support |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 85 | 'DocTestSuite', |
Edward Loper | b7503ff | 2004-08-19 19:19:03 +0000 | [diff] [blame] | 86 | 'DocFileSuite', |
Tim Peters | 9d02a7c | 2004-09-26 01:50:24 +0000 | [diff] [blame] | 87 | 'set_unittest_reportflags', |
Georg Brandl | cc80bfe | 2008-05-12 18:06:23 +0000 | [diff] [blame] | 88 | # 8. Debugging Support |
Edward Loper | b7503ff | 2004-08-19 19:19:03 +0000 | [diff] [blame] | 89 | 'script_from_examples', |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 90 | 'testsource', |
Edward Loper | b7503ff | 2004-08-19 19:19:03 +0000 | [diff] [blame] | 91 | 'debug_src', |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 92 | 'debug', |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 93 | ] |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 94 | |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 95 | import __future__ |
Victor Stinner | 12b8d14 | 2011-06-30 17:35:55 +0200 | [diff] [blame] | 96 | import difflib |
| 97 | import inspect |
| 98 | import linecache |
| 99 | import os |
| 100 | import pdb |
| 101 | import re |
| 102 | import sys |
| 103 | import traceback |
| 104 | import unittest |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 105 | from io import StringIO |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 106 | from collections import namedtuple |
| 107 | |
| 108 | TestResults = namedtuple('TestResults', 'failed attempted') |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 109 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 110 | # There are 4 basic classes: |
| 111 | # - Example: a <source, want> pair, plus an intra-docstring line number. |
| 112 | # - DocTest: a collection of examples, parsed from a docstring, plus |
| 113 | # info about where the docstring came from (name, filename, lineno). |
| 114 | # - DocTestFinder: extracts DocTests from a given object's docstring and |
| 115 | # its contained objects' docstrings. |
| 116 | # - DocTestRunner: runs DocTest cases, and accumulates statistics. |
| 117 | # |
| 118 | # So the basic picture is: |
| 119 | # |
| 120 | # list of: |
| 121 | # +------+ +---------+ +-------+ |
| 122 | # |object| --DocTestFinder-> | DocTest | --DocTestRunner-> |results| |
| 123 | # +------+ +---------+ +-------+ |
| 124 | # | Example | |
| 125 | # | ... | |
| 126 | # | Example | |
| 127 | # +---------+ |
| 128 | |
Edward Loper | ac20f57 | 2004-08-12 02:02:24 +0000 | [diff] [blame] | 129 | # Option constants. |
Tim Peters | 38330fe | 2004-08-30 16:19:24 +0000 | [diff] [blame] | 130 | |
Edward Loper | ac20f57 | 2004-08-12 02:02:24 +0000 | [diff] [blame] | 131 | OPTIONFLAGS_BY_NAME = {} |
| 132 | def register_optionflag(name): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 133 | # Create a new flag unless `name` is already known. |
| 134 | return OPTIONFLAGS_BY_NAME.setdefault(name, 1 << len(OPTIONFLAGS_BY_NAME)) |
Edward Loper | ac20f57 | 2004-08-12 02:02:24 +0000 | [diff] [blame] | 135 | |
| 136 | DONT_ACCEPT_TRUE_FOR_1 = register_optionflag('DONT_ACCEPT_TRUE_FOR_1') |
| 137 | DONT_ACCEPT_BLANKLINE = register_optionflag('DONT_ACCEPT_BLANKLINE') |
| 138 | NORMALIZE_WHITESPACE = register_optionflag('NORMALIZE_WHITESPACE') |
| 139 | ELLIPSIS = register_optionflag('ELLIPSIS') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 140 | SKIP = register_optionflag('SKIP') |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 141 | IGNORE_EXCEPTION_DETAIL = register_optionflag('IGNORE_EXCEPTION_DETAIL') |
Tim Peters | 38330fe | 2004-08-30 16:19:24 +0000 | [diff] [blame] | 142 | |
| 143 | COMPARISON_FLAGS = (DONT_ACCEPT_TRUE_FOR_1 | |
| 144 | DONT_ACCEPT_BLANKLINE | |
| 145 | NORMALIZE_WHITESPACE | |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 146 | ELLIPSIS | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 147 | SKIP | |
Edward Loper | 7d88a58 | 2004-09-28 05:50:57 +0000 | [diff] [blame] | 148 | IGNORE_EXCEPTION_DETAIL) |
Tim Peters | 38330fe | 2004-08-30 16:19:24 +0000 | [diff] [blame] | 149 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 150 | REPORT_UDIFF = register_optionflag('REPORT_UDIFF') |
| 151 | REPORT_CDIFF = register_optionflag('REPORT_CDIFF') |
| 152 | REPORT_NDIFF = register_optionflag('REPORT_NDIFF') |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 153 | REPORT_ONLY_FIRST_FAILURE = register_optionflag('REPORT_ONLY_FIRST_FAILURE') |
R David Murray | 5a9d706 | 2012-11-21 15:09:21 -0500 | [diff] [blame] | 154 | FAIL_FAST = register_optionflag('FAIL_FAST') |
Edward Loper | ac20f57 | 2004-08-12 02:02:24 +0000 | [diff] [blame] | 155 | |
Tim Peters | 38330fe | 2004-08-30 16:19:24 +0000 | [diff] [blame] | 156 | REPORTING_FLAGS = (REPORT_UDIFF | |
| 157 | REPORT_CDIFF | |
| 158 | REPORT_NDIFF | |
R David Murray | 5a9d706 | 2012-11-21 15:09:21 -0500 | [diff] [blame] | 159 | REPORT_ONLY_FIRST_FAILURE | |
| 160 | FAIL_FAST) |
Tim Peters | 38330fe | 2004-08-30 16:19:24 +0000 | [diff] [blame] | 161 | |
Edward Loper | ac20f57 | 2004-08-12 02:02:24 +0000 | [diff] [blame] | 162 | # Special string markers for use in `want` strings: |
| 163 | BLANKLINE_MARKER = '<BLANKLINE>' |
| 164 | ELLIPSIS_MARKER = '...' |
| 165 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 166 | ###################################################################### |
| 167 | ## Table of Contents |
| 168 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 169 | # 1. Utility Functions |
| 170 | # 2. Example & DocTest -- store test cases |
| 171 | # 3. DocTest Parser -- extracts examples from strings |
| 172 | # 4. DocTest Finder -- extracts test cases from objects |
| 173 | # 5. DocTest Runner -- runs test cases |
| 174 | # 6. Test Functions -- convenient wrappers for testing |
Georg Brandl | 3183585 | 2008-05-12 17:38:56 +0000 | [diff] [blame] | 175 | # 7. Unittest Support |
| 176 | # 8. Debugging Support |
| 177 | # 9. Example Usage |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 178 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 179 | ###################################################################### |
| 180 | ## 1. Utility Functions |
| 181 | ###################################################################### |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 182 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 183 | def _extract_future_flags(globs): |
| 184 | """ |
| 185 | Return the compiler-flags associated with the future features that |
| 186 | have been imported into the given namespace (globs). |
| 187 | """ |
| 188 | flags = 0 |
| 189 | for fname in __future__.all_feature_names: |
| 190 | feature = globs.get(fname, None) |
| 191 | if feature is getattr(__future__, fname): |
| 192 | flags |= feature.compiler_flag |
| 193 | return flags |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 194 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 195 | def _normalize_module(module, depth=2): |
| 196 | """ |
| 197 | Return the module specified by `module`. In particular: |
| 198 | - If `module` is a module, then return module. |
| 199 | - If `module` is a string, then import and return the |
| 200 | module with that name. |
| 201 | - If `module` is None, then return the calling module. |
| 202 | The calling module is assumed to be the module of |
| 203 | the stack frame at the given depth in the call stack. |
| 204 | """ |
| 205 | if inspect.ismodule(module): |
| 206 | return module |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 207 | elif isinstance(module, str): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 208 | return __import__(module, globals(), locals(), ["*"]) |
| 209 | elif module is None: |
| 210 | return sys.modules[sys._getframe(depth).f_globals['__name__']] |
| 211 | else: |
| 212 | raise TypeError("Expected a module, string, or None") |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | 1b81e7b | 2007-08-29 03:53:53 +0000 | [diff] [blame] | 214 | def _load_testfile(filename, package, module_relative, encoding): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 215 | if module_relative: |
| 216 | package = _normalize_module(package, 3) |
| 217 | filename = _module_relative_path(package, filename) |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 218 | if getattr(package, '__loader__', None) is not None: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 219 | if hasattr(package.__loader__, 'get_data'): |
Guido van Rossum | cd4d452 | 2007-11-22 00:30:02 +0000 | [diff] [blame] | 220 | file_contents = package.__loader__.get_data(filename) |
| 221 | file_contents = file_contents.decode(encoding) |
| 222 | # get_data() opens files as 'rb', so one must do the equivalent |
| 223 | # conversion as universal newlines would do. |
| 224 | return file_contents.replace(os.linesep, '\n'), filename |
Antoine Pitrou | 92f60ed | 2010-10-14 22:11:44 +0000 | [diff] [blame] | 225 | with open(filename, encoding=encoding) as f: |
| 226 | return f.read(), filename |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 227 | |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 228 | def _indent(s, indent=4): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 229 | """ |
Florent Xicluna | 5925085 | 2010-02-27 14:21:57 +0000 | [diff] [blame] | 230 | Add the given number of space characters to the beginning of |
| 231 | every non-blank line in `s`, and return the result. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 232 | """ |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 233 | # This regexp matches the start of non-blank lines: |
| 234 | return re.sub('(?m)^(?!$)', indent*' ', s) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 235 | |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 236 | def _exception_traceback(exc_info): |
| 237 | """ |
| 238 | Return a string containing a traceback message for the given |
| 239 | exc_info tuple (as returned by sys.exc_info()). |
| 240 | """ |
| 241 | # Get a traceback message. |
| 242 | excout = StringIO() |
| 243 | exc_type, exc_val, exc_tb = exc_info |
| 244 | traceback.print_exception(exc_type, exc_val, exc_tb, file=excout) |
| 245 | return excout.getvalue() |
| 246 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 247 | # Override some StringIO methods. |
| 248 | class _SpoofOut(StringIO): |
| 249 | def getvalue(self): |
| 250 | result = StringIO.getvalue(self) |
| 251 | # If anything at all was written, make sure there's a trailing |
| 252 | # newline. There's no way for the expected output to indicate |
| 253 | # that a trailing newline is missing. |
| 254 | if result and not result.endswith("\n"): |
| 255 | result += "\n" |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 256 | return result |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | 79139b2 | 2007-02-09 23:20:19 +0000 | [diff] [blame] | 258 | def truncate(self, size=None): |
Antoine Pitrou | b3d7700 | 2010-01-31 23:12:29 +0000 | [diff] [blame] | 259 | self.seek(size) |
| 260 | StringIO.truncate(self) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 261 | |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 262 | # Worst-case linear-time ellipsis matching. |
Tim Peters | b0a04e1 | 2004-08-20 02:08:04 +0000 | [diff] [blame] | 263 | def _ellipsis_match(want, got): |
Tim Peters | dc5de3b | 2004-08-19 14:06:20 +0000 | [diff] [blame] | 264 | """ |
| 265 | Essentially the only subtle case: |
Tim Peters | b0a04e1 | 2004-08-20 02:08:04 +0000 | [diff] [blame] | 266 | >>> _ellipsis_match('aa...aa', 'aaa') |
Tim Peters | dc5de3b | 2004-08-19 14:06:20 +0000 | [diff] [blame] | 267 | False |
| 268 | """ |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 269 | if ELLIPSIS_MARKER not in want: |
| 270 | return want == got |
Tim Peters | dc5de3b | 2004-08-19 14:06:20 +0000 | [diff] [blame] | 271 | |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 272 | # Find "the real" strings. |
| 273 | ws = want.split(ELLIPSIS_MARKER) |
| 274 | assert len(ws) >= 2 |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 275 | |
Tim Peters | dc5de3b | 2004-08-19 14:06:20 +0000 | [diff] [blame] | 276 | # Deal with exact matches possibly needed at one or both ends. |
| 277 | startpos, endpos = 0, len(got) |
| 278 | w = ws[0] |
| 279 | if w: # starts with exact match |
| 280 | if got.startswith(w): |
| 281 | startpos = len(w) |
| 282 | del ws[0] |
| 283 | else: |
| 284 | return False |
| 285 | w = ws[-1] |
| 286 | if w: # ends with exact match |
| 287 | if got.endswith(w): |
| 288 | endpos -= len(w) |
| 289 | del ws[-1] |
| 290 | else: |
| 291 | return False |
| 292 | |
| 293 | if startpos > endpos: |
| 294 | # Exact end matches required more characters than we have, as in |
Tim Peters | b0a04e1 | 2004-08-20 02:08:04 +0000 | [diff] [blame] | 295 | # _ellipsis_match('aa...aa', 'aaa') |
Tim Peters | dc5de3b | 2004-08-19 14:06:20 +0000 | [diff] [blame] | 296 | return False |
| 297 | |
| 298 | # For the rest, we only need to find the leftmost non-overlapping |
| 299 | # match for each piece. If there's no overall match that way alone, |
| 300 | # there's no overall match period. |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 301 | for w in ws: |
| 302 | # w may be '' at times, if there are consecutive ellipses, or |
| 303 | # due to an ellipsis at the start or end of `want`. That's OK. |
Tim Peters | dc5de3b | 2004-08-19 14:06:20 +0000 | [diff] [blame] | 304 | # Search for an empty string succeeds, and doesn't change startpos. |
| 305 | startpos = got.find(w, startpos, endpos) |
| 306 | if startpos < 0: |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 307 | return False |
Tim Peters | dc5de3b | 2004-08-19 14:06:20 +0000 | [diff] [blame] | 308 | startpos += len(w) |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 309 | |
Tim Peters | dc5de3b | 2004-08-19 14:06:20 +0000 | [diff] [blame] | 310 | return True |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 311 | |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 312 | def _comment_line(line): |
| 313 | "Return a commented form of the given line" |
| 314 | line = line.rstrip() |
| 315 | if line: |
| 316 | return '# '+line |
| 317 | else: |
| 318 | return '#' |
| 319 | |
Tim Peters | f9a07f2 | 2013-12-03 21:02:05 -0600 | [diff] [blame] | 320 | def _strip_exception_details(msg): |
| 321 | # Support for IGNORE_EXCEPTION_DETAIL. |
| 322 | # Get rid of everything except the exception name; in particular, drop |
| 323 | # the possibly dotted module path (if any) and the exception message (if |
| 324 | # any). We assume that a colon is never part of a dotted name, or of an |
| 325 | # exception name. |
| 326 | # E.g., given |
| 327 | # "foo.bar.MyError: la di da" |
| 328 | # return "MyError" |
| 329 | # Or for "abc.def" or "abc.def:\n" return "def". |
| 330 | |
| 331 | start, end = 0, len(msg) |
| 332 | # The exception name must appear on the first line. |
| 333 | i = msg.find("\n") |
| 334 | if i >= 0: |
| 335 | end = i |
| 336 | # retain up to the first colon (if any) |
| 337 | i = msg.find(':', 0, end) |
| 338 | if i >= 0: |
| 339 | end = i |
| 340 | # retain just the exception name |
| 341 | i = msg.rfind('.', 0, end) |
| 342 | if i >= 0: |
| 343 | start = i+1 |
| 344 | return msg[start: end] |
| 345 | |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 346 | class _OutputRedirectingPdb(pdb.Pdb): |
| 347 | """ |
| 348 | A specialized version of the python debugger that redirects stdout |
| 349 | to a given stream when interacting with the user. Stdout is *not* |
| 350 | redirected when traced code is executed. |
| 351 | """ |
| 352 | def __init__(self, out): |
| 353 | self.__out = out |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 354 | self.__debugger_used = False |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 355 | # do not play signal games in the pdb |
| 356 | pdb.Pdb.__init__(self, stdout=out, nosigint=True) |
Georg Brandl | d72e043 | 2010-07-30 09:59:28 +0000 | [diff] [blame] | 357 | # still use input() to get user input |
| 358 | self.use_rawinput = 1 |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 359 | |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 360 | def set_trace(self, frame=None): |
| 361 | self.__debugger_used = True |
| 362 | if frame is None: |
| 363 | frame = sys._getframe().f_back |
| 364 | pdb.Pdb.set_trace(self, frame) |
| 365 | |
| 366 | def set_continue(self): |
| 367 | # Calling set_continue unconditionally would break unit test |
| 368 | # coverage reporting, as Bdb.set_continue calls sys.settrace(None). |
| 369 | if self.__debugger_used: |
| 370 | pdb.Pdb.set_continue(self) |
| 371 | |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 372 | def trace_dispatch(self, *args): |
| 373 | # Redirect stdout to the given stream. |
| 374 | save_stdout = sys.stdout |
| 375 | sys.stdout = self.__out |
| 376 | # Call Pdb's trace dispatch method. |
Tim Peters | d7bbbbc | 2004-11-08 22:30:28 +0000 | [diff] [blame] | 377 | try: |
| 378 | return pdb.Pdb.trace_dispatch(self, *args) |
| 379 | finally: |
Tim Peters | d7bbbbc | 2004-11-08 22:30:28 +0000 | [diff] [blame] | 380 | sys.stdout = save_stdout |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 381 | |
Edward Loper | a2fc7ec | 2004-09-21 03:24:24 +0000 | [diff] [blame] | 382 | # [XX] Normalize with respect to os.path.pardir? |
Victor Stinner | b1511f7 | 2016-03-30 01:29:05 +0200 | [diff] [blame] | 383 | def _module_relative_path(module, test_path): |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 384 | if not inspect.ismodule(module): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 385 | raise TypeError('Expected a module: %r' % module) |
Victor Stinner | b1511f7 | 2016-03-30 01:29:05 +0200 | [diff] [blame] | 386 | if test_path.startswith('/'): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 387 | raise ValueError('Module-relative files may not have absolute paths') |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 388 | |
Victor Stinner | b1511f7 | 2016-03-30 01:29:05 +0200 | [diff] [blame] | 389 | # Normalize the path. On Windows, replace "/" with "\". |
| 390 | test_path = os.path.join(*(test_path.split('/'))) |
| 391 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 392 | # Find the base directory for the path. |
| 393 | if hasattr(module, '__file__'): |
| 394 | # A normal module/package |
| 395 | basedir = os.path.split(module.__file__)[0] |
| 396 | elif module.__name__ == '__main__': |
| 397 | # An interactive session. |
| 398 | if len(sys.argv)>0 and sys.argv[0] != '': |
| 399 | basedir = os.path.split(sys.argv[0])[0] |
| 400 | else: |
| 401 | basedir = os.curdir |
| 402 | else: |
Victor Stinner | b1511f7 | 2016-03-30 01:29:05 +0200 | [diff] [blame] | 403 | if hasattr(module, '__path__'): |
| 404 | for directory in module.__path__: |
| 405 | fullpath = os.path.join(directory, test_path) |
| 406 | if os.path.exists(fullpath): |
| 407 | return fullpath |
| 408 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 409 | # A module w/o __file__ (this includes builtins) |
Victor Stinner | 84ca9fe | 2016-03-25 12:50:36 +0100 | [diff] [blame] | 410 | raise ValueError("Can't resolve paths relative to the module " |
| 411 | "%r (it has no __file__)" |
| 412 | % module.__name__) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 413 | |
Victor Stinner | b1511f7 | 2016-03-30 01:29:05 +0200 | [diff] [blame] | 414 | # Combine the base directory and the test path. |
| 415 | return os.path.join(basedir, test_path) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 416 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 417 | ###################################################################### |
| 418 | ## 2. Example & DocTest |
| 419 | ###################################################################### |
| 420 | ## - An "example" is a <source, want> pair, where "source" is a |
| 421 | ## fragment of source code, and "want" is the expected output for |
| 422 | ## "source." The Example class also includes information about |
| 423 | ## where the example was extracted from. |
| 424 | ## |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 425 | ## - A "doctest" is a collection of examples, typically extracted from |
| 426 | ## a string (such as an object's docstring). The DocTest class also |
| 427 | ## includes information about where the string was extracted from. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 428 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 429 | class Example: |
| 430 | """ |
| 431 | A single doctest example, consisting of source code and expected |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 432 | output. `Example` defines the following attributes: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 433 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 434 | - source: A single Python statement, always ending with a newline. |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 435 | The constructor adds a newline if needed. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 436 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 437 | - want: The expected output from running the source code (either |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 438 | from stdout, or a traceback in case of exception). `want` ends |
| 439 | with a newline unless it's empty, in which case it's an empty |
| 440 | string. The constructor adds a newline if needed. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 441 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 442 | - exc_msg: The exception message generated by the example, if |
| 443 | the example is expected to generate an exception; or `None` if |
| 444 | it is not expected to generate an exception. This exception |
| 445 | message is compared against the return value of |
| 446 | `traceback.format_exception_only()`. `exc_msg` ends with a |
| 447 | newline unless it's `None`. The constructor adds a newline |
| 448 | if needed. |
| 449 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 450 | - lineno: The line number within the DocTest string containing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 451 | this Example where the Example begins. This line number is |
| 452 | zero-based, with respect to the beginning of the DocTest. |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 453 | |
| 454 | - indent: The example's indentation in the DocTest string. |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 455 | I.e., the number of space characters that precede the |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 456 | example's first prompt. |
| 457 | |
| 458 | - options: A dictionary mapping from option flags to True or |
| 459 | False, which is used to override default options for this |
| 460 | example. Any option flags not contained in this dictionary |
| 461 | are left at their default value (as specified by the |
| 462 | DocTestRunner's optionflags). By default, no options are set. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 463 | """ |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 464 | def __init__(self, source, want, exc_msg=None, lineno=0, indent=0, |
| 465 | options=None): |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 466 | # Normalize inputs. |
| 467 | if not source.endswith('\n'): |
| 468 | source += '\n' |
| 469 | if want and not want.endswith('\n'): |
| 470 | want += '\n' |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 471 | if exc_msg is not None and not exc_msg.endswith('\n'): |
| 472 | exc_msg += '\n' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 473 | # Store properties. |
| 474 | self.source = source |
| 475 | self.want = want |
| 476 | self.lineno = lineno |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 477 | self.indent = indent |
| 478 | if options is None: options = {} |
| 479 | self.options = options |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 480 | self.exc_msg = exc_msg |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 481 | |
Antoine Pitrou | 2bc801c | 2011-12-18 19:27:45 +0100 | [diff] [blame] | 482 | def __eq__(self, other): |
| 483 | if type(self) is not type(other): |
| 484 | return NotImplemented |
| 485 | |
| 486 | return self.source == other.source and \ |
| 487 | self.want == other.want and \ |
| 488 | self.lineno == other.lineno and \ |
| 489 | self.indent == other.indent and \ |
| 490 | self.options == other.options and \ |
| 491 | self.exc_msg == other.exc_msg |
| 492 | |
Antoine Pitrou | 165b128 | 2011-12-18 20:20:17 +0100 | [diff] [blame] | 493 | def __hash__(self): |
| 494 | return hash((self.source, self.want, self.lineno, self.indent, |
| 495 | self.exc_msg)) |
Antoine Pitrou | 2bc801c | 2011-12-18 19:27:45 +0100 | [diff] [blame] | 496 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 497 | class DocTest: |
| 498 | """ |
| 499 | A collection of doctest examples that should be run in a single |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 500 | namespace. Each `DocTest` defines the following attributes: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 501 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 502 | - examples: the list of examples. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 503 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 504 | - globs: The namespace (aka globals) that the examples should |
| 505 | be run in. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 506 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 507 | - name: A name identifying the DocTest (typically, the name of |
| 508 | the object whose docstring this DocTest was extracted from). |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 509 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 510 | - filename: The name of the file that this DocTest was extracted |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 511 | from, or `None` if the filename is unknown. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 512 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 513 | - lineno: The line number within filename where this DocTest |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 514 | begins, or `None` if the line number is unavailable. This |
| 515 | line number is zero-based, with respect to the beginning of |
| 516 | the file. |
| 517 | |
| 518 | - docstring: The string that the examples were extracted from, |
| 519 | or `None` if the string is unavailable. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 520 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 521 | def __init__(self, examples, globs, name, filename, lineno, docstring): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 522 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 523 | Create a new DocTest containing the given examples. The |
| 524 | DocTest's globals are initialized with a copy of `globs`. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 525 | """ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 526 | assert not isinstance(examples, str), \ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 527 | "DocTest no longer accepts str; use DocTestParser instead" |
| 528 | self.examples = examples |
| 529 | self.docstring = docstring |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 530 | self.globs = globs.copy() |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 531 | self.name = name |
| 532 | self.filename = filename |
| 533 | self.lineno = lineno |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 534 | |
| 535 | def __repr__(self): |
| 536 | if len(self.examples) == 0: |
| 537 | examples = 'no examples' |
| 538 | elif len(self.examples) == 1: |
| 539 | examples = '1 example' |
| 540 | else: |
| 541 | examples = '%d examples' % len(self.examples) |
Serhiy Storchaka | 465e60e | 2014-07-25 23:36:00 +0300 | [diff] [blame] | 542 | return ('<%s %s from %s:%s (%s)>' % |
| 543 | (self.__class__.__name__, |
| 544 | self.name, self.filename, self.lineno, examples)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | 2bc801c | 2011-12-18 19:27:45 +0100 | [diff] [blame] | 546 | def __eq__(self, other): |
| 547 | if type(self) is not type(other): |
| 548 | return NotImplemented |
| 549 | |
| 550 | return self.examples == other.examples and \ |
| 551 | self.docstring == other.docstring and \ |
| 552 | self.globs == other.globs and \ |
| 553 | self.name == other.name and \ |
| 554 | self.filename == other.filename and \ |
| 555 | self.lineno == other.lineno |
| 556 | |
Antoine Pitrou | 165b128 | 2011-12-18 20:20:17 +0100 | [diff] [blame] | 557 | def __hash__(self): |
| 558 | return hash((self.docstring, self.name, self.filename, self.lineno)) |
| 559 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 560 | # This lets us sort tests by name: |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 561 | def __lt__(self, other): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 562 | if not isinstance(other, DocTest): |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 563 | return NotImplemented |
| 564 | return ((self.name, self.filename, self.lineno, id(self)) |
| 565 | < |
| 566 | (other.name, other.filename, other.lineno, id(other))) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 567 | |
| 568 | ###################################################################### |
Edward Loper | b7503ff | 2004-08-19 19:19:03 +0000 | [diff] [blame] | 569 | ## 3. DocTestParser |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 570 | ###################################################################### |
| 571 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 572 | class DocTestParser: |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 573 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 574 | A class used to parse strings containing doctest examples. |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 575 | """ |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 576 | # This regular expression is used to find doctest examples in a |
| 577 | # string. It defines three groups: `source` is the source code |
| 578 | # (including leading indentation and prompts); `indent` is the |
| 579 | # indentation of the first (PS1) line of the source code; and |
| 580 | # `want` is the expected output (including leading indentation). |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 581 | _EXAMPLE_RE = re.compile(r''' |
Tim Peters | d40a92b | 2004-08-09 03:28:45 +0000 | [diff] [blame] | 582 | # Source consists of a PS1 line followed by zero or more PS2 lines. |
| 583 | (?P<source> |
| 584 | (?:^(?P<indent> [ ]*) >>> .*) # PS1 line |
| 585 | (?:\n [ ]* \.\.\. .*)*) # PS2 lines |
| 586 | \n? |
| 587 | # Want consists of any non-blank lines that do not start with PS1. |
| 588 | (?P<want> (?:(?![ ]*$) # Not a blank line |
| 589 | (?![ ]*>>>) # Not a line starting with PS1 |
Serhiy Storchaka | 1ca66ed | 2013-08-19 22:59:31 +0300 | [diff] [blame] | 590 | .+$\n? # But any other line |
Tim Peters | d40a92b | 2004-08-09 03:28:45 +0000 | [diff] [blame] | 591 | )*) |
| 592 | ''', re.MULTILINE | re.VERBOSE) |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 593 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 594 | # A regular expression for handling `want` strings that contain |
| 595 | # expected exceptions. It divides `want` into three pieces: |
| 596 | # - the traceback header line (`hdr`) |
| 597 | # - the traceback stack (`stack`) |
| 598 | # - the exception message (`msg`), as generated by |
| 599 | # traceback.format_exception_only() |
| 600 | # `msg` may have multiple lines. We assume/require that the |
| 601 | # exception message is the first non-indented line starting with a word |
| 602 | # character following the traceback header line. |
| 603 | _EXCEPTION_RE = re.compile(r""" |
| 604 | # Grab the traceback header. Different versions of Python have |
| 605 | # said different things on the first traceback line. |
| 606 | ^(?P<hdr> Traceback\ \( |
| 607 | (?: most\ recent\ call\ last |
| 608 | | innermost\ last |
| 609 | ) \) : |
| 610 | ) |
| 611 | \s* $ # toss trailing whitespace on the header. |
| 612 | (?P<stack> .*?) # don't blink: absorb stuff until... |
| 613 | ^ (?P<msg> \w+ .*) # a line *starts* with alphanum. |
| 614 | """, re.VERBOSE | re.MULTILINE | re.DOTALL) |
| 615 | |
Tim Peters | 7ea48dd | 2004-08-13 01:52:59 +0000 | [diff] [blame] | 616 | # A callable returning a true value iff its argument is a blank line |
| 617 | # or contains a single comment. |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 618 | _IS_BLANK_OR_COMMENT = re.compile(r'^[ ]*(#.*)?$').match |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 619 | |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 620 | def parse(self, string, name='<string>'): |
| 621 | """ |
| 622 | Divide the given string into examples and intervening text, |
| 623 | and return them as a list of alternating Examples and strings. |
| 624 | Line numbers for the Examples are 0-based. The optional |
| 625 | argument `name` is a name identifying this string, and is only |
| 626 | used for error messages. |
| 627 | """ |
| 628 | string = string.expandtabs() |
| 629 | # If all lines begin with the same indentation, then strip it. |
| 630 | min_indent = self._min_indent(string) |
| 631 | if min_indent > 0: |
| 632 | string = '\n'.join([l[min_indent:] for l in string.split('\n')]) |
| 633 | |
| 634 | output = [] |
| 635 | charno, lineno = 0, 0 |
| 636 | # Find all doctest examples in the string: |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 637 | for m in self._EXAMPLE_RE.finditer(string): |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 638 | # Add the pre-example text to `output`. |
| 639 | output.append(string[charno:m.start()]) |
| 640 | # Update lineno (lines before this example) |
| 641 | lineno += string.count('\n', charno, m.start()) |
| 642 | # Extract info from the regexp match. |
| 643 | (source, options, want, exc_msg) = \ |
| 644 | self._parse_example(m, name, lineno) |
| 645 | # Create an Example, and add it to the list. |
| 646 | if not self._IS_BLANK_OR_COMMENT(source): |
| 647 | output.append( Example(source, want, exc_msg, |
| 648 | lineno=lineno, |
| 649 | indent=min_indent+len(m.group('indent')), |
| 650 | options=options) ) |
| 651 | # Update lineno (lines inside this example) |
| 652 | lineno += string.count('\n', m.start(), m.end()) |
| 653 | # Update charno. |
| 654 | charno = m.end() |
| 655 | # Add any remaining post-example text to `output`. |
| 656 | output.append(string[charno:]) |
| 657 | return output |
| 658 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 659 | def get_doctest(self, string, globs, name, filename, lineno): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 660 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 661 | Extract all doctest examples from the given string, and |
| 662 | collect them into a `DocTest` object. |
| 663 | |
| 664 | `globs`, `name`, `filename`, and `lineno` are attributes for |
| 665 | the new `DocTest` object. See the documentation for `DocTest` |
| 666 | for more information. |
| 667 | """ |
| 668 | return DocTest(self.get_examples(string, name), globs, |
| 669 | name, filename, lineno, string) |
| 670 | |
| 671 | def get_examples(self, string, name='<string>'): |
| 672 | """ |
| 673 | Extract all doctest examples from the given string, and return |
| 674 | them as a list of `Example` objects. Line numbers are |
| 675 | 0-based, because it's most common in doctests that nothing |
| 676 | interesting appears on the same line as opening triple-quote, |
| 677 | and so the first interesting line is called \"line 1\" then. |
| 678 | |
| 679 | The optional argument `name` is a name identifying this |
| 680 | string, and is only used for error messages. |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 681 | """ |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 682 | return [x for x in self.parse(string, name) |
| 683 | if isinstance(x, Example)] |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 684 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 685 | def _parse_example(self, m, name, lineno): |
| 686 | """ |
| 687 | Given a regular expression match from `_EXAMPLE_RE` (`m`), |
| 688 | return a pair `(source, want)`, where `source` is the matched |
| 689 | example's source code (with prompts and indentation stripped); |
| 690 | and `want` is the example's expected output (with indentation |
| 691 | stripped). |
| 692 | |
| 693 | `name` is the string's name, and `lineno` is the line number |
| 694 | where the example starts; both are used for error messages. |
| 695 | """ |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 696 | # Get the example's indentation level. |
| 697 | indent = len(m.group('indent')) |
| 698 | |
| 699 | # Divide source into lines; check that they're properly |
| 700 | # indented; and then strip their indentation & prompts. |
| 701 | source_lines = m.group('source').split('\n') |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 702 | self._check_prompt_blank(source_lines, indent, name, lineno) |
Tim Peters | c504915 | 2004-08-22 17:34:58 +0000 | [diff] [blame] | 703 | self._check_prefix(source_lines[1:], ' '*indent + '.', name, lineno) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 704 | source = '\n'.join([sl[indent+4:] for sl in source_lines]) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 705 | |
Tim Peters | c504915 | 2004-08-22 17:34:58 +0000 | [diff] [blame] | 706 | # Divide want into lines; check that it's properly indented; and |
| 707 | # then strip the indentation. Spaces before the last newline should |
| 708 | # be preserved, so plain rstrip() isn't good enough. |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 709 | want = m.group('want') |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 710 | want_lines = want.split('\n') |
Tim Peters | c504915 | 2004-08-22 17:34:58 +0000 | [diff] [blame] | 711 | if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]): |
| 712 | del want_lines[-1] # forget final newline & spaces after it |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 713 | self._check_prefix(want_lines, ' '*indent, name, |
Tim Peters | c504915 | 2004-08-22 17:34:58 +0000 | [diff] [blame] | 714 | lineno + len(source_lines)) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 715 | want = '\n'.join([wl[indent:] for wl in want_lines]) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 716 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 717 | # If `want` contains a traceback message, then extract it. |
| 718 | m = self._EXCEPTION_RE.match(want) |
| 719 | if m: |
| 720 | exc_msg = m.group('msg') |
| 721 | else: |
| 722 | exc_msg = None |
| 723 | |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 724 | # Extract options from the source. |
| 725 | options = self._find_options(source, name, lineno) |
| 726 | |
| 727 | return source, options, want, exc_msg |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 728 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 729 | # This regular expression looks for option directives in the |
| 730 | # source code of an example. Option directives are comments |
| 731 | # starting with "doctest:". Warning: this may give false |
| 732 | # positives for string-literals that contain the string |
| 733 | # "#doctest:". Eliminating these false positives would require |
| 734 | # actually parsing the string; but we limit them by ignoring any |
| 735 | # line containing "#doctest:" that is *followed* by a quote mark. |
| 736 | _OPTION_DIRECTIVE_RE = re.compile(r'#\s*doctest:\s*([^\n\'"]*)$', |
| 737 | re.MULTILINE) |
| 738 | |
| 739 | def _find_options(self, source, name, lineno): |
| 740 | """ |
| 741 | Return a dictionary containing option overrides extracted from |
| 742 | option directives in the given source string. |
| 743 | |
| 744 | `name` is the string's name, and `lineno` is the line number |
| 745 | where the example starts; both are used for error messages. |
| 746 | """ |
| 747 | options = {} |
| 748 | # (note: with the current regexp, this will match at most once:) |
| 749 | for m in self._OPTION_DIRECTIVE_RE.finditer(source): |
| 750 | option_strings = m.group(1).replace(',', ' ').split() |
| 751 | for option in option_strings: |
| 752 | if (option[0] not in '+-' or |
| 753 | option[1:] not in OPTIONFLAGS_BY_NAME): |
| 754 | raise ValueError('line %r of the doctest for %s ' |
| 755 | 'has an invalid option: %r' % |
| 756 | (lineno+1, name, option)) |
| 757 | flag = OPTIONFLAGS_BY_NAME[option[1:]] |
| 758 | options[flag] = (option[0] == '+') |
| 759 | if options and self._IS_BLANK_OR_COMMENT(source): |
| 760 | raise ValueError('line %r of the doctest for %s has an option ' |
| 761 | 'directive on a line with no example: %r' % |
| 762 | (lineno, name, source)) |
| 763 | return options |
| 764 | |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 765 | # This regular expression finds the indentation of every non-blank |
| 766 | # line in a string. |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 767 | _INDENT_RE = re.compile(r'^([ ]*)(?=\S)', re.MULTILINE) |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 768 | |
| 769 | def _min_indent(self, s): |
| 770 | "Return the minimum indentation of any non-blank line in `s`" |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 771 | indents = [len(indent) for indent in self._INDENT_RE.findall(s)] |
| 772 | if len(indents) > 0: |
| 773 | return min(indents) |
Tim Peters | dd0e475 | 2004-08-09 03:31:56 +0000 | [diff] [blame] | 774 | else: |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 775 | return 0 |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 776 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 777 | def _check_prompt_blank(self, lines, indent, name, lineno): |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 778 | """ |
| 779 | Given the lines of a source string (including prompts and |
| 780 | leading indentation), check to make sure that every prompt is |
| 781 | followed by a space character. If any line is not followed by |
| 782 | a space character, then raise ValueError. |
| 783 | """ |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 784 | for i, line in enumerate(lines): |
| 785 | if len(line) >= indent+4 and line[indent+3] != ' ': |
| 786 | raise ValueError('line %r of the docstring for %s ' |
| 787 | 'lacks blank after %s: %r' % |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 788 | (lineno+i+1, name, |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 789 | line[indent:indent+3], line)) |
| 790 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 791 | def _check_prefix(self, lines, prefix, name, lineno): |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 792 | """ |
| 793 | Check that every line in the given list starts with the given |
| 794 | prefix; if any line does not, then raise a ValueError. |
| 795 | """ |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 796 | for i, line in enumerate(lines): |
| 797 | if line and not line.startswith(prefix): |
| 798 | raise ValueError('line %r of the docstring for %s has ' |
| 799 | 'inconsistent leading whitespace: %r' % |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 800 | (lineno+i+1, name, line)) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 801 | |
| 802 | |
| 803 | ###################################################################### |
| 804 | ## 4. DocTest Finder |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 805 | ###################################################################### |
| 806 | |
| 807 | class DocTestFinder: |
| 808 | """ |
| 809 | A class used to extract the DocTests that are relevant to a given |
| 810 | object, from its docstring and the docstrings of its contained |
| 811 | objects. Doctests can currently be extracted from the following |
| 812 | object types: modules, functions, classes, methods, staticmethods, |
| 813 | classmethods, and properties. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 814 | """ |
| 815 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 816 | def __init__(self, verbose=False, parser=DocTestParser(), |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 817 | recurse=True, exclude_empty=True): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 818 | """ |
| 819 | Create a new doctest finder. |
| 820 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 821 | The optional argument `parser` specifies a class or |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 822 | function that should be used to create new DocTest objects (or |
Tim Peters | 161c963 | 2004-08-08 03:38:33 +0000 | [diff] [blame] | 823 | objects that implement the same interface as DocTest). The |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 824 | signature for this factory function should match the signature |
| 825 | of the DocTest constructor. |
| 826 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 827 | If the optional argument `recurse` is false, then `find` will |
| 828 | only examine the given object, and not any contained objects. |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 829 | |
Tim Peters | 958cc89 | 2004-09-13 14:53:28 +0000 | [diff] [blame] | 830 | If the optional argument `exclude_empty` is false, then `find` |
| 831 | will include tests for objects with empty docstrings. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 832 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 833 | self._parser = parser |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 834 | self._verbose = verbose |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 835 | self._recurse = recurse |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 836 | self._exclude_empty = exclude_empty |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 837 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 838 | def find(self, obj, name=None, module=None, globs=None, extraglobs=None): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 839 | """ |
| 840 | Return a list of the DocTests that are defined by the given |
| 841 | object's docstring, or by any of its contained objects' |
| 842 | docstrings. |
| 843 | |
| 844 | The optional parameter `module` is the module that contains |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 845 | the given object. If the module is not specified or is None, then |
| 846 | the test finder will attempt to automatically determine the |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 847 | correct module. The object's module is used: |
| 848 | |
| 849 | - As a default namespace, if `globs` is not specified. |
| 850 | - To prevent the DocTestFinder from extracting DocTests |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 851 | from objects that are imported from other modules. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 852 | - To find the name of the file containing the object. |
| 853 | - To help find the line number of the object within its |
| 854 | file. |
| 855 | |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 856 | Contained objects whose module does not match `module` are ignored. |
| 857 | |
| 858 | If `module` is False, no attempt to find the module will be made. |
| 859 | This is obscure, of use mostly in tests: if `module` is False, or |
| 860 | is None but cannot be found automatically, then all objects are |
| 861 | considered to belong to the (non-existent) module, so all contained |
| 862 | objects will (recursively) be searched for doctests. |
| 863 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 864 | The globals for each DocTest is formed by combining `globs` |
| 865 | and `extraglobs` (bindings in `extraglobs` override bindings |
| 866 | in `globs`). A new copy of the globals dictionary is created |
| 867 | for each DocTest. If `globs` is not specified, then it |
| 868 | defaults to the module's `__dict__`, if specified, or {} |
| 869 | otherwise. If `extraglobs` is not specified, then it defaults |
| 870 | to {}. |
| 871 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 872 | """ |
| 873 | # If name was not specified, then extract it from the object. |
| 874 | if name is None: |
| 875 | name = getattr(obj, '__name__', None) |
| 876 | if name is None: |
| 877 | raise ValueError("DocTestFinder.find: name must be given " |
| 878 | "when obj.__name__ doesn't exist: %r" % |
| 879 | (type(obj),)) |
| 880 | |
| 881 | # Find the module that contains the given object (if obj is |
| 882 | # a module, then module=obj.). Note: this may fail, in which |
| 883 | # case module will be None. |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 884 | if module is False: |
| 885 | module = None |
| 886 | elif module is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 887 | module = inspect.getmodule(obj) |
| 888 | |
| 889 | # Read the module's source code. This is used by |
| 890 | # DocTestFinder._find_lineno to find the line number for a |
| 891 | # given object's docstring. |
| 892 | try: |
R. David Murray | 58641de | 2009-06-12 15:33:19 +0000 | [diff] [blame] | 893 | file = inspect.getsourcefile(obj) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 894 | except TypeError: |
| 895 | source_lines = None |
R. David Murray | 58641de | 2009-06-12 15:33:19 +0000 | [diff] [blame] | 896 | else: |
| 897 | if not file: |
| 898 | # Check to see if it's one of our special internal "files" |
| 899 | # (see __patched_linecache_getlines). |
| 900 | file = inspect.getfile(obj) |
| 901 | if not file[0]+file[-2:] == '<]>': file = None |
R. David Murray | 765b976 | 2009-07-15 14:16:54 +0000 | [diff] [blame] | 902 | if file is None: |
| 903 | source_lines = None |
R. David Murray | 58641de | 2009-06-12 15:33:19 +0000 | [diff] [blame] | 904 | else: |
| 905 | if module is not None: |
| 906 | # Supply the module globals in case the module was |
| 907 | # originally loaded via a PEP 302 loader and |
| 908 | # file is not a valid filesystem path |
| 909 | source_lines = linecache.getlines(file, module.__dict__) |
| 910 | else: |
| 911 | # No access to a loader, so assume it's a normal |
| 912 | # filesystem path |
| 913 | source_lines = linecache.getlines(file) |
| 914 | if not source_lines: |
| 915 | source_lines = None |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 916 | |
| 917 | # Initialize globals, and merge in extraglobs. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 918 | if globs is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 919 | if module is None: |
| 920 | globs = {} |
| 921 | else: |
| 922 | globs = module.__dict__.copy() |
| 923 | else: |
| 924 | globs = globs.copy() |
| 925 | if extraglobs is not None: |
| 926 | globs.update(extraglobs) |
Raymond Hettinger | 0f05517 | 2009-01-27 10:06:09 +0000 | [diff] [blame] | 927 | if '__name__' not in globs: |
| 928 | globs['__name__'] = '__main__' # provide a default module name |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 929 | |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 930 | # Recursively explore `obj`, extracting DocTests. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 931 | tests = [] |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 932 | self._find(tests, obj, name, module, source_lines, globs, {}) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 933 | # Sort the tests by alpha order of names, for consistency in |
| 934 | # verbose-mode output. This was a feature of doctest in Pythons |
| 935 | # <= 2.3 that got lost by accident in 2.4. It was repaired in |
| 936 | # 2.4.4 and 2.5. |
| 937 | tests.sort() |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 938 | return tests |
| 939 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 940 | def _from_module(self, module, object): |
| 941 | """ |
| 942 | Return true if the given object is defined in the given |
| 943 | module. |
| 944 | """ |
| 945 | if module is None: |
| 946 | return True |
Benjamin Peterson | 6cadba7 | 2008-11-19 22:38:29 +0000 | [diff] [blame] | 947 | elif inspect.getmodule(object) is not None: |
| 948 | return module is inspect.getmodule(object) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 949 | elif inspect.isfunction(object): |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 950 | return module.__dict__ is object.__globals__ |
Zachary Ware | a4b7a75 | 2013-11-24 01:19:09 -0600 | [diff] [blame] | 951 | elif inspect.ismethoddescriptor(object): |
Zachary Ware | eee44f2 | 2014-02-06 15:46:38 -0600 | [diff] [blame] | 952 | if hasattr(object, '__objclass__'): |
| 953 | obj_mod = object.__objclass__.__module__ |
| 954 | elif hasattr(object, '__module__'): |
| 955 | obj_mod = object.__module__ |
| 956 | else: |
| 957 | return True # [XX] no easy way to tell otherwise |
| 958 | return module.__name__ == obj_mod |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 959 | elif inspect.isclass(object): |
| 960 | return module.__name__ == object.__module__ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 961 | elif hasattr(object, '__module__'): |
| 962 | return module.__name__ == object.__module__ |
| 963 | elif isinstance(object, property): |
| 964 | return True # [XX] no way not be sure. |
| 965 | else: |
| 966 | raise ValueError("object must be a class or function") |
| 967 | |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 968 | def _find(self, tests, obj, name, module, source_lines, globs, seen): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 969 | """ |
| 970 | Find tests for the given object and any contained objects, and |
| 971 | add them to `tests`. |
| 972 | """ |
| 973 | if self._verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 974 | print('Finding tests in %s' % name) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 975 | |
| 976 | # If we've already processed this object, then ignore it. |
| 977 | if id(obj) in seen: |
| 978 | return |
| 979 | seen[id(obj)] = 1 |
| 980 | |
| 981 | # Find a test for this object, and add it to the list of tests. |
| 982 | test = self._get_test(obj, name, module, globs, source_lines) |
| 983 | if test is not None: |
| 984 | tests.append(test) |
| 985 | |
| 986 | # Look for tests in a module's contained objects. |
| 987 | if inspect.ismodule(obj) and self._recurse: |
| 988 | for valname, val in obj.__dict__.items(): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 989 | valname = '%s.%s' % (name, valname) |
| 990 | # Recurse to functions & classes. |
Yury Selivanov | b532df6 | 2014-12-08 15:00:05 -0500 | [diff] [blame] | 991 | if ((inspect.isroutine(inspect.unwrap(val)) |
| 992 | or inspect.isclass(val)) and |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 993 | self._from_module(module, val)): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 994 | self._find(tests, val, valname, module, source_lines, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 995 | globs, seen) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 996 | |
| 997 | # Look for tests in a module's __test__ dictionary. |
| 998 | if inspect.ismodule(obj) and self._recurse: |
| 999 | for valname, val in getattr(obj, '__test__', {}).items(): |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 1000 | if not isinstance(valname, str): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1001 | raise ValueError("DocTestFinder.find: __test__ keys " |
| 1002 | "must be strings: %r" % |
| 1003 | (type(valname),)) |
Zachary Ware | a4b7a75 | 2013-11-24 01:19:09 -0600 | [diff] [blame] | 1004 | if not (inspect.isroutine(val) or inspect.isclass(val) or |
| 1005 | inspect.ismodule(val) or isinstance(val, str)): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1006 | raise ValueError("DocTestFinder.find: __test__ values " |
| 1007 | "must be strings, functions, methods, " |
| 1008 | "classes, or modules: %r" % |
| 1009 | (type(val),)) |
Tim Peters | c568478 | 2004-09-13 01:07:12 +0000 | [diff] [blame] | 1010 | valname = '%s.__test__.%s' % (name, valname) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1011 | self._find(tests, val, valname, module, source_lines, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 1012 | globs, seen) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1013 | |
| 1014 | # Look for tests in a class's contained objects. |
| 1015 | if inspect.isclass(obj) and self._recurse: |
| 1016 | for valname, val in obj.__dict__.items(): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1017 | # Special handling for staticmethod/classmethod. |
| 1018 | if isinstance(val, staticmethod): |
| 1019 | val = getattr(obj, valname) |
| 1020 | if isinstance(val, classmethod): |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 1021 | val = getattr(obj, valname).__func__ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1022 | |
| 1023 | # Recurse to methods, properties, and nested classes. |
Zachary Ware | a4b7a75 | 2013-11-24 01:19:09 -0600 | [diff] [blame] | 1024 | if ((inspect.isroutine(val) or inspect.isclass(val) or |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 1025 | isinstance(val, property)) and |
| 1026 | self._from_module(module, val)): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1027 | valname = '%s.%s' % (name, valname) |
| 1028 | self._find(tests, val, valname, module, source_lines, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 1029 | globs, seen) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1030 | |
| 1031 | def _get_test(self, obj, name, module, globs, source_lines): |
| 1032 | """ |
| 1033 | Return a DocTest for the given object, if it defines a docstring; |
| 1034 | otherwise, return None. |
| 1035 | """ |
| 1036 | # Extract the object's docstring. If it doesn't have one, |
| 1037 | # then return None (no test for this object). |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 1038 | if isinstance(obj, str): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1039 | docstring = obj |
| 1040 | else: |
| 1041 | try: |
| 1042 | if obj.__doc__ is None: |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 1043 | docstring = '' |
| 1044 | else: |
Jim Fulton | 7d42878 | 2004-10-13 14:15:32 +0000 | [diff] [blame] | 1045 | docstring = obj.__doc__ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 1046 | if not isinstance(docstring, str): |
Jim Fulton | 7d42878 | 2004-10-13 14:15:32 +0000 | [diff] [blame] | 1047 | docstring = str(docstring) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1048 | except (TypeError, AttributeError): |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 1049 | docstring = '' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1050 | |
| 1051 | # Find the docstring's location in the file. |
| 1052 | lineno = self._find_lineno(obj, source_lines) |
| 1053 | |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 1054 | # Don't bother if the docstring is empty. |
| 1055 | if self._exclude_empty and not docstring: |
| 1056 | return None |
| 1057 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1058 | # Return a DocTest for this object. |
| 1059 | if module is None: |
| 1060 | filename = None |
| 1061 | else: |
| 1062 | filename = getattr(module, '__file__', module.__name__) |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 1063 | if filename[-4:] == ".pyc": |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1064 | filename = filename[:-1] |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1065 | return self._parser.get_doctest(docstring, globs, name, |
| 1066 | filename, lineno) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1067 | |
| 1068 | def _find_lineno(self, obj, source_lines): |
| 1069 | """ |
| 1070 | Return a line number of the given object's docstring. Note: |
| 1071 | this method assumes that the object has a docstring. |
| 1072 | """ |
| 1073 | lineno = None |
| 1074 | |
| 1075 | # Find the line number for modules. |
| 1076 | if inspect.ismodule(obj): |
| 1077 | lineno = 0 |
| 1078 | |
| 1079 | # Find the line number for classes. |
| 1080 | # Note: this could be fooled if a class is defined multiple |
| 1081 | # times in a single file. |
| 1082 | if inspect.isclass(obj): |
| 1083 | if source_lines is None: |
| 1084 | return None |
| 1085 | pat = re.compile(r'^\s*class\s*%s\b' % |
| 1086 | getattr(obj, '__name__', '-')) |
| 1087 | for i, line in enumerate(source_lines): |
| 1088 | if pat.match(line): |
| 1089 | lineno = i |
| 1090 | break |
| 1091 | |
| 1092 | # Find the line number for functions & methods. |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 1093 | if inspect.ismethod(obj): obj = obj.__func__ |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 1094 | if inspect.isfunction(obj): obj = obj.__code__ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1095 | if inspect.istraceback(obj): obj = obj.tb_frame |
| 1096 | if inspect.isframe(obj): obj = obj.f_code |
| 1097 | if inspect.iscode(obj): |
| 1098 | lineno = getattr(obj, 'co_firstlineno', None)-1 |
| 1099 | |
| 1100 | # Find the line number where the docstring starts. Assume |
| 1101 | # that it's the first line that begins with a quote mark. |
| 1102 | # Note: this could be fooled by a multiline function |
| 1103 | # signature, where a continuation line begins with a quote |
| 1104 | # mark. |
| 1105 | if lineno is not None: |
| 1106 | if source_lines is None: |
| 1107 | return lineno+1 |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 1108 | pat = re.compile(r'(^|.*:)\s*\w*("|\')') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1109 | for lineno in range(lineno, len(source_lines)): |
| 1110 | if pat.match(source_lines[lineno]): |
| 1111 | return lineno |
| 1112 | |
| 1113 | # We couldn't find the line number. |
| 1114 | return None |
| 1115 | |
| 1116 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 1117 | ## 5. DocTest Runner |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1118 | ###################################################################### |
| 1119 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1120 | class DocTestRunner: |
| 1121 | """ |
| 1122 | A class used to run DocTest test cases, and accumulate statistics. |
| 1123 | The `run` method is used to process a single DocTest case. It |
| 1124 | returns a tuple `(f, t)`, where `t` is the number of test cases |
| 1125 | tried, and `f` is the number of test cases that failed. |
| 1126 | |
| 1127 | >>> tests = DocTestFinder().find(_TestClass) |
| 1128 | >>> runner = DocTestRunner(verbose=False) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1129 | >>> tests.sort(key = lambda test: test.name) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1130 | >>> for test in tests: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1131 | ... print(test.name, '->', runner.run(test)) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1132 | _TestClass -> TestResults(failed=0, attempted=2) |
| 1133 | _TestClass.__init__ -> TestResults(failed=0, attempted=2) |
| 1134 | _TestClass.get -> TestResults(failed=0, attempted=2) |
| 1135 | _TestClass.square -> TestResults(failed=0, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1136 | |
| 1137 | The `summarize` method prints a summary of all the test cases that |
| 1138 | have been run by the runner, and returns an aggregated `(f, t)` |
| 1139 | tuple: |
| 1140 | |
| 1141 | >>> runner.summarize(verbose=1) |
| 1142 | 4 items passed all tests: |
| 1143 | 2 tests in _TestClass |
| 1144 | 2 tests in _TestClass.__init__ |
| 1145 | 2 tests in _TestClass.get |
| 1146 | 1 tests in _TestClass.square |
| 1147 | 7 tests in 4 items. |
| 1148 | 7 passed and 0 failed. |
| 1149 | Test passed. |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1150 | TestResults(failed=0, attempted=7) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1151 | |
| 1152 | The aggregated number of tried examples and failed examples is |
| 1153 | also available via the `tries` and `failures` attributes: |
| 1154 | |
| 1155 | >>> runner.tries |
| 1156 | 7 |
| 1157 | >>> runner.failures |
| 1158 | 0 |
| 1159 | |
| 1160 | The comparison between expected outputs and actual outputs is done |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1161 | by an `OutputChecker`. This comparison may be customized with a |
| 1162 | number of option flags; see the documentation for `testmod` for |
| 1163 | more information. If the option flags are insufficient, then the |
| 1164 | comparison may also be customized by passing a subclass of |
| 1165 | `OutputChecker` to the constructor. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1166 | |
| 1167 | The test runner's display output can be controlled in two ways. |
| 1168 | First, an output function (`out) can be passed to |
| 1169 | `TestRunner.run`; this function will be called with strings that |
| 1170 | should be displayed. It defaults to `sys.stdout.write`. If |
| 1171 | capturing the output is not sufficient, then the display output |
| 1172 | can be also customized by subclassing DocTestRunner, and |
| 1173 | overriding the methods `report_start`, `report_success`, |
| 1174 | `report_unexpected_exception`, and `report_failure`. |
| 1175 | """ |
| 1176 | # This divider string is used to separate failure messages, and to |
| 1177 | # separate sections of the summary. |
| 1178 | DIVIDER = "*" * 70 |
| 1179 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1180 | def __init__(self, checker=None, verbose=None, optionflags=0): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1181 | """ |
| 1182 | Create a new test runner. |
| 1183 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1184 | Optional keyword arg `checker` is the `OutputChecker` that |
| 1185 | should be used to compare the expected outputs and actual |
| 1186 | outputs of doctest examples. |
| 1187 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1188 | Optional keyword arg 'verbose' prints lots of stuff if true, |
| 1189 | only failures if false; by default, it's true iff '-v' is in |
| 1190 | sys.argv. |
| 1191 | |
| 1192 | Optional argument `optionflags` can be used to control how the |
| 1193 | test runner compares expected output to actual output, and how |
| 1194 | it displays failures. See the documentation for `testmod` for |
| 1195 | more information. |
| 1196 | """ |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1197 | self._checker = checker or OutputChecker() |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1198 | if verbose is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1199 | verbose = '-v' in sys.argv |
| 1200 | self._verbose = verbose |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 1201 | self.optionflags = optionflags |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1202 | self.original_optionflags = optionflags |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 1203 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1204 | # Keep track of the examples we've run. |
| 1205 | self.tries = 0 |
| 1206 | self.failures = 0 |
| 1207 | self._name2ft = {} |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1208 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1209 | # Create a fake output target for capturing doctest output. |
| 1210 | self._fakeout = _SpoofOut() |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 1211 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1212 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1213 | # Reporting methods |
| 1214 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 1215 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1216 | def report_start(self, out, test, example): |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1217 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1218 | Report that the test runner is about to process the given |
| 1219 | example. (Only displays a message if verbose=True) |
| 1220 | """ |
| 1221 | if self._verbose: |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 1222 | if example.want: |
| 1223 | out('Trying:\n' + _indent(example.source) + |
| 1224 | 'Expecting:\n' + _indent(example.want)) |
| 1225 | else: |
| 1226 | out('Trying:\n' + _indent(example.source) + |
| 1227 | 'Expecting nothing\n') |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1228 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1229 | def report_success(self, out, test, example, got): |
| 1230 | """ |
| 1231 | Report that the given example ran successfully. (Only |
| 1232 | displays a message if verbose=True) |
| 1233 | """ |
| 1234 | if self._verbose: |
| 1235 | out("ok\n") |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1236 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1237 | def report_failure(self, out, test, example, got): |
| 1238 | """ |
| 1239 | Report that the given example failed. |
| 1240 | """ |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1241 | out(self._failure_header(test, example) + |
Edward Loper | ca9111e | 2004-08-26 03:00:24 +0000 | [diff] [blame] | 1242 | self._checker.output_difference(example, got, self.optionflags)) |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1243 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1244 | def report_unexpected_exception(self, out, test, example, exc_info): |
| 1245 | """ |
| 1246 | Report that the given example raised an unexpected exception. |
| 1247 | """ |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1248 | out(self._failure_header(test, example) + |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 1249 | 'Exception raised:\n' + _indent(_exception_traceback(exc_info))) |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1250 | |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1251 | def _failure_header(self, test, example): |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1252 | out = [self.DIVIDER] |
| 1253 | if test.filename: |
| 1254 | if test.lineno is not None and example.lineno is not None: |
| 1255 | lineno = test.lineno + example.lineno + 1 |
| 1256 | else: |
| 1257 | lineno = '?' |
| 1258 | out.append('File "%s", line %s, in %s' % |
| 1259 | (test.filename, lineno, test.name)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1260 | else: |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1261 | out.append('Line %s, in %s' % (example.lineno+1, test.name)) |
| 1262 | out.append('Failed example:') |
| 1263 | source = example.source |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 1264 | out.append(_indent(source)) |
| 1265 | return '\n'.join(out) |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1266 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1267 | #///////////////////////////////////////////////////////////////// |
| 1268 | # DocTest Running |
| 1269 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1270 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1271 | def __run(self, test, compileflags, out): |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1272 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1273 | Run the examples in `test`. Write the outcome of each example |
| 1274 | with one of the `DocTestRunner.report_*` methods, using the |
| 1275 | writer function `out`. `compileflags` is the set of compiler |
| 1276 | flags that should be used to execute examples. Return a tuple |
| 1277 | `(f, t)`, where `t` is the number of examples tried, and `f` |
| 1278 | is the number of examples that failed. The examples are run |
| 1279 | in the namespace `test.globs`. |
| 1280 | """ |
| 1281 | # Keep track of the number of failures and tries. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1282 | failures = tries = 0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1283 | |
| 1284 | # Save the option flags (since option directives can be used |
| 1285 | # to modify them). |
| 1286 | original_optionflags = self.optionflags |
| 1287 | |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1288 | SUCCESS, FAILURE, BOOM = range(3) # `outcome` state |
| 1289 | |
| 1290 | check = self._checker.check_output |
| 1291 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1292 | # Process each example. |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1293 | for examplenum, example in enumerate(test.examples): |
| 1294 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1295 | # If REPORT_ONLY_FIRST_FAILURE is set, then suppress |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1296 | # reporting after the first failure. |
| 1297 | quiet = (self.optionflags & REPORT_ONLY_FIRST_FAILURE and |
| 1298 | failures > 0) |
| 1299 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1300 | # Merge in the example's options. |
| 1301 | self.optionflags = original_optionflags |
| 1302 | if example.options: |
| 1303 | for (optionflag, val) in example.options.items(): |
| 1304 | if val: |
| 1305 | self.optionflags |= optionflag |
| 1306 | else: |
| 1307 | self.optionflags &= ~optionflag |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1308 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1309 | # If 'SKIP' is set, then skip this example. |
| 1310 | if self.optionflags & SKIP: |
| 1311 | continue |
| 1312 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1313 | # Record that we started this example. |
| 1314 | tries += 1 |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1315 | if not quiet: |
| 1316 | self.report_start(out, test, example) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1317 | |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1318 | # Use a special filename for compile(), so we can retrieve |
| 1319 | # the source code during interactive debugging (see |
| 1320 | # __patched_linecache_getlines). |
| 1321 | filename = '<doctest %s[%d]>' % (test.name, examplenum) |
| 1322 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1323 | # Run the example in the given context (globs), and record |
| 1324 | # any exception that gets raised. (But don't intercept |
| 1325 | # keyboard interrupts.) |
| 1326 | try: |
Tim Peters | 208ca70 | 2004-08-09 04:12:36 +0000 | [diff] [blame] | 1327 | # Don't blink! This is where the user's code gets run. |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 1328 | exec(compile(example.source, filename, "single", |
| 1329 | compileflags, 1), test.globs) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1330 | self.debugger.set_continue() # ==== Example Finished ==== |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1331 | exception = None |
| 1332 | except KeyboardInterrupt: |
| 1333 | raise |
| 1334 | except: |
| 1335 | exception = sys.exc_info() |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1336 | self.debugger.set_continue() # ==== Example Finished ==== |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1337 | |
Tim Peters | 208ca70 | 2004-08-09 04:12:36 +0000 | [diff] [blame] | 1338 | got = self._fakeout.getvalue() # the actual output |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1339 | self._fakeout.truncate(0) |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1340 | outcome = FAILURE # guilty until proved innocent or insane |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1341 | |
| 1342 | # If the example executed without raising any exceptions, |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1343 | # verify its output. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1344 | if exception is None: |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1345 | if check(example.want, got, self.optionflags): |
| 1346 | outcome = SUCCESS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1347 | |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1348 | # The example raised an exception: check if it was expected. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1349 | else: |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1350 | exc_msg = traceback.format_exception_only(*exception[:2])[-1] |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1351 | if not quiet: |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1352 | got += _exception_traceback(exception) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1353 | |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1354 | # If `example.exc_msg` is None, then we weren't expecting |
| 1355 | # an exception. |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 1356 | if example.exc_msg is None: |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1357 | outcome = BOOM |
| 1358 | |
| 1359 | # We expected an exception: see whether it matches. |
| 1360 | elif check(example.exc_msg, exc_msg, self.optionflags): |
| 1361 | outcome = SUCCESS |
| 1362 | |
| 1363 | # Another chance if they didn't care about the detail. |
| 1364 | elif self.optionflags & IGNORE_EXCEPTION_DETAIL: |
Tim Peters | f9a07f2 | 2013-12-03 21:02:05 -0600 | [diff] [blame] | 1365 | if check(_strip_exception_details(example.exc_msg), |
| 1366 | _strip_exception_details(exc_msg), |
| 1367 | self.optionflags): |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1368 | outcome = SUCCESS |
| 1369 | |
| 1370 | # Report the outcome. |
| 1371 | if outcome is SUCCESS: |
| 1372 | if not quiet: |
| 1373 | self.report_success(out, test, example, got) |
| 1374 | elif outcome is FAILURE: |
| 1375 | if not quiet: |
| 1376 | self.report_failure(out, test, example, got) |
| 1377 | failures += 1 |
| 1378 | elif outcome is BOOM: |
| 1379 | if not quiet: |
| 1380 | self.report_unexpected_exception(out, test, example, |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1381 | exception) |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1382 | failures += 1 |
| 1383 | else: |
| 1384 | assert False, ("unknown outcome", outcome) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1385 | |
R David Murray | 5a9d706 | 2012-11-21 15:09:21 -0500 | [diff] [blame] | 1386 | if failures and self.optionflags & FAIL_FAST: |
| 1387 | break |
| 1388 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1389 | # Restore the option flags (in case they were modified) |
| 1390 | self.optionflags = original_optionflags |
| 1391 | |
| 1392 | # Record and return the number of failures and tries. |
| 1393 | self.__record_outcome(test, failures, tries) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1394 | return TestResults(failures, tries) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1395 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1396 | def __record_outcome(self, test, f, t): |
| 1397 | """ |
| 1398 | Record the fact that the given DocTest (`test`) generated `f` |
| 1399 | failures out of `t` tried examples. |
| 1400 | """ |
| 1401 | f2, t2 = self._name2ft.get(test.name, (0,0)) |
| 1402 | self._name2ft[test.name] = (f+f2, t+t2) |
| 1403 | self.failures += f |
| 1404 | self.tries += t |
| 1405 | |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1406 | __LINECACHE_FILENAME_RE = re.compile(r'<doctest ' |
Florent Xicluna | d9f5763 | 2010-10-14 20:56:20 +0000 | [diff] [blame] | 1407 | r'(?P<name>.+)' |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1408 | r'\[(?P<examplenum>\d+)\]>$') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1409 | def __patched_linecache_getlines(self, filename, module_globals=None): |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1410 | m = self.__LINECACHE_FILENAME_RE.match(filename) |
| 1411 | if m and m.group('name') == self.test.name: |
| 1412 | example = self.test.examples[int(m.group('examplenum'))] |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 1413 | return example.source.splitlines(keepends=True) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1414 | else: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1415 | return self.save_linecache_getlines(filename, module_globals) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1416 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1417 | def run(self, test, compileflags=None, out=None, clear_globs=True): |
| 1418 | """ |
| 1419 | Run the examples in `test`, and display the results using the |
| 1420 | writer function `out`. |
| 1421 | |
| 1422 | The examples are run in the namespace `test.globs`. If |
| 1423 | `clear_globs` is true (the default), then this namespace will |
| 1424 | be cleared after the test runs, to help with garbage |
| 1425 | collection. If you would like to examine the namespace after |
| 1426 | the test completes, then use `clear_globs=False`. |
| 1427 | |
| 1428 | `compileflags` gives the set of flags that should be used by |
| 1429 | the Python compiler when running the examples. If not |
| 1430 | specified, then it will default to the set of future-import |
| 1431 | flags that apply to `globs`. |
| 1432 | |
| 1433 | The output of each example is checked using |
| 1434 | `DocTestRunner.check_output`, and the results are formatted by |
| 1435 | the `DocTestRunner.report_*` methods. |
| 1436 | """ |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1437 | self.test = test |
| 1438 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1439 | if compileflags is None: |
| 1440 | compileflags = _extract_future_flags(test.globs) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1441 | |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1442 | save_stdout = sys.stdout |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1443 | if out is None: |
Florent Xicluna | 5925085 | 2010-02-27 14:21:57 +0000 | [diff] [blame] | 1444 | encoding = save_stdout.encoding |
| 1445 | if encoding is None or encoding.lower() == 'utf-8': |
| 1446 | out = save_stdout.write |
| 1447 | else: |
| 1448 | # Use backslashreplace error handling on write |
| 1449 | def out(s): |
| 1450 | s = str(s.encode(encoding, 'backslashreplace'), encoding) |
| 1451 | save_stdout.write(s) |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1452 | sys.stdout = self._fakeout |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1453 | |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1454 | # Patch pdb.set_trace to restore sys.stdout during interactive |
| 1455 | # debugging (so it's not still redirected to self._fakeout). |
| 1456 | # Note that the interactive output will go to *our* |
| 1457 | # save_stdout, even if that's not the real sys.stdout; this |
| 1458 | # allows us to write test cases for the set_trace behavior. |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1459 | save_trace = sys.gettrace() |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1460 | save_set_trace = pdb.set_trace |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1461 | self.debugger = _OutputRedirectingPdb(save_stdout) |
| 1462 | self.debugger.reset() |
| 1463 | pdb.set_trace = self.debugger.set_trace |
| 1464 | |
| 1465 | # Patch linecache.getlines, so we can see the example's source |
| 1466 | # when we're inside the debugger. |
| 1467 | self.save_linecache_getlines = linecache.getlines |
| 1468 | linecache.getlines = self.__patched_linecache_getlines |
| 1469 | |
Georg Brandl | 25fbb89 | 2010-07-30 09:23:23 +0000 | [diff] [blame] | 1470 | # Make sure sys.displayhook just prints the value to stdout |
| 1471 | save_displayhook = sys.displayhook |
| 1472 | sys.displayhook = sys.__displayhook__ |
| 1473 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1474 | try: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1475 | return self.__run(test, compileflags, out) |
| 1476 | finally: |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1477 | sys.stdout = save_stdout |
| 1478 | pdb.set_trace = save_set_trace |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1479 | sys.settrace(save_trace) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1480 | linecache.getlines = self.save_linecache_getlines |
Georg Brandl | 25fbb89 | 2010-07-30 09:23:23 +0000 | [diff] [blame] | 1481 | sys.displayhook = save_displayhook |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1482 | if clear_globs: |
| 1483 | test.globs.clear() |
Benjamin Peterson | fbf66bd | 2008-07-29 15:55:50 +0000 | [diff] [blame] | 1484 | import builtins |
| 1485 | builtins._ = None |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1486 | |
| 1487 | #///////////////////////////////////////////////////////////////// |
| 1488 | # Summarization |
| 1489 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1490 | def summarize(self, verbose=None): |
| 1491 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1492 | Print a summary of all the test cases that have been run by |
| 1493 | this DocTestRunner, and return a tuple `(f, t)`, where `f` is |
| 1494 | the total number of failed examples, and `t` is the total |
| 1495 | number of tried examples. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1496 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1497 | The optional `verbose` argument controls how detailed the |
| 1498 | summary is. If the verbosity is not specified, then the |
| 1499 | DocTestRunner's verbosity is used. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1500 | """ |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1501 | if verbose is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1502 | verbose = self._verbose |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1503 | notests = [] |
| 1504 | passed = [] |
| 1505 | failed = [] |
| 1506 | totalt = totalf = 0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1507 | for x in self._name2ft.items(): |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1508 | name, (f, t) = x |
| 1509 | assert f <= t |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1510 | totalt += t |
| 1511 | totalf += f |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1512 | if t == 0: |
| 1513 | notests.append(name) |
| 1514 | elif f == 0: |
| 1515 | passed.append( (name, t) ) |
| 1516 | else: |
| 1517 | failed.append(x) |
| 1518 | if verbose: |
| 1519 | if notests: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1520 | print(len(notests), "items had no tests:") |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1521 | notests.sort() |
| 1522 | for thing in notests: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1523 | print(" ", thing) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1524 | if passed: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1525 | print(len(passed), "items passed all tests:") |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1526 | passed.sort() |
| 1527 | for thing, count in passed: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1528 | print(" %3d tests in %s" % (count, thing)) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1529 | if failed: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1530 | print(self.DIVIDER) |
| 1531 | print(len(failed), "items had failures:") |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1532 | failed.sort() |
| 1533 | for thing, (f, t) in failed: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1534 | print(" %3d of %3d in %s" % (f, t, thing)) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1535 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1536 | print(totalt, "tests in", len(self._name2ft), "items.") |
| 1537 | print(totalt - totalf, "passed and", totalf, "failed.") |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1538 | if totalf: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1539 | print("***Test Failed***", totalf, "failures.") |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1540 | elif verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1541 | print("Test passed.") |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1542 | return TestResults(totalf, totalt) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1543 | |
Tim Peters | 82076ef | 2004-09-13 00:52:51 +0000 | [diff] [blame] | 1544 | #///////////////////////////////////////////////////////////////// |
| 1545 | # Backward compatibility cruft to maintain doctest.master. |
| 1546 | #///////////////////////////////////////////////////////////////// |
| 1547 | def merge(self, other): |
| 1548 | d = self._name2ft |
| 1549 | for name, (f, t) in other._name2ft.items(): |
| 1550 | if name in d: |
Nick Coghlan | 3862200 | 2008-12-15 12:01:34 +0000 | [diff] [blame] | 1551 | # Don't print here by default, since doing |
| 1552 | # so breaks some of the buildbots |
| 1553 | #print("*** DocTestRunner.merge: '" + name + "' in both" \ |
| 1554 | # " testers; summing outcomes.") |
Tim Peters | 82076ef | 2004-09-13 00:52:51 +0000 | [diff] [blame] | 1555 | f2, t2 = d[name] |
| 1556 | f = f + f2 |
| 1557 | t = t + t2 |
| 1558 | d[name] = f, t |
| 1559 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1560 | class OutputChecker: |
| 1561 | """ |
| 1562 | A class used to check the whether the actual output from a doctest |
| 1563 | example matches the expected output. `OutputChecker` defines two |
| 1564 | methods: `check_output`, which compares a given pair of outputs, |
| 1565 | and returns true if they match; and `output_difference`, which |
| 1566 | returns a string describing the differences between two outputs. |
| 1567 | """ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 1568 | def _toAscii(self, s): |
| 1569 | """ |
| 1570 | Convert string to hex-escaped ASCII string. |
| 1571 | """ |
| 1572 | return str(s.encode('ASCII', 'backslashreplace'), "ASCII") |
| 1573 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1574 | def check_output(self, want, got, optionflags): |
| 1575 | """ |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1576 | Return True iff the actual output from an example (`got`) |
| 1577 | matches the expected output (`want`). These strings are |
| 1578 | always considered to match if they are identical; but |
| 1579 | depending on what option flags the test runner is using, |
| 1580 | several non-exact match types are also possible. See the |
| 1581 | documentation for `TestRunner` for more information about |
| 1582 | option flags. |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1583 | """ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 1584 | |
| 1585 | # If `want` contains hex-escaped character such as "\u1234", |
| 1586 | # then `want` is a string of six characters(e.g. [\,u,1,2,3,4]). |
Martin Panter | 2e4571a | 2015-11-14 01:07:43 +0000 | [diff] [blame] | 1587 | # On the other hand, `got` could be another sequence of |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 1588 | # characters such as [\u1234], so `want` and `got` should |
| 1589 | # be folded to hex-escaped ASCII string to compare. |
| 1590 | got = self._toAscii(got) |
| 1591 | want = self._toAscii(want) |
| 1592 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1593 | # Handle the common case first, for efficiency: |
| 1594 | # if they're string-identical, always return true. |
| 1595 | if got == want: |
| 1596 | return True |
| 1597 | |
| 1598 | # The values True and False replaced 1 and 0 as the return |
| 1599 | # value for boolean comparisons in Python 2.3. |
| 1600 | if not (optionflags & DONT_ACCEPT_TRUE_FOR_1): |
| 1601 | if (got,want) == ("True\n", "1\n"): |
| 1602 | return True |
| 1603 | if (got,want) == ("False\n", "0\n"): |
| 1604 | return True |
| 1605 | |
| 1606 | # <BLANKLINE> can be used as a special sequence to signify a |
| 1607 | # blank line, unless the DONT_ACCEPT_BLANKLINE flag is used. |
| 1608 | if not (optionflags & DONT_ACCEPT_BLANKLINE): |
| 1609 | # Replace <BLANKLINE> in want with a blank line. |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 1610 | want = re.sub(r'(?m)^%s\s*?$' % re.escape(BLANKLINE_MARKER), |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1611 | '', want) |
| 1612 | # If a line in got contains only spaces, then remove the |
| 1613 | # spaces. |
Serhiy Storchaka | 70d56fb | 2017-12-04 14:29:05 +0200 | [diff] [blame^] | 1614 | got = re.sub(r'(?m)^[^\S\n]+$', '', got) |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1615 | if got == want: |
| 1616 | return True |
| 1617 | |
| 1618 | # This flag causes doctest to ignore any differences in the |
| 1619 | # contents of whitespace strings. Note that this can be used |
Tim Peters | 3fa8c20 | 2004-08-23 21:43:39 +0000 | [diff] [blame] | 1620 | # in conjunction with the ELLIPSIS flag. |
Tim Peters | 1cf3aa6 | 2004-08-19 06:49:33 +0000 | [diff] [blame] | 1621 | if optionflags & NORMALIZE_WHITESPACE: |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1622 | got = ' '.join(got.split()) |
| 1623 | want = ' '.join(want.split()) |
| 1624 | if got == want: |
| 1625 | return True |
| 1626 | |
| 1627 | # The ELLIPSIS flag says to let the sequence "..." in `want` |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 1628 | # match any substring in `got`. |
Tim Peters | 1cf3aa6 | 2004-08-19 06:49:33 +0000 | [diff] [blame] | 1629 | if optionflags & ELLIPSIS: |
Tim Peters | b0a04e1 | 2004-08-20 02:08:04 +0000 | [diff] [blame] | 1630 | if _ellipsis_match(want, got): |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1631 | return True |
| 1632 | |
| 1633 | # We didn't find any match; return false. |
| 1634 | return False |
| 1635 | |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1636 | # Should we do a fancy diff? |
| 1637 | def _do_a_fancy_diff(self, want, got, optionflags): |
| 1638 | # Not unless they asked for a fancy diff. |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1639 | if not optionflags & (REPORT_UDIFF | |
| 1640 | REPORT_CDIFF | |
| 1641 | REPORT_NDIFF): |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1642 | return False |
Tim Peters | 5b799c1 | 2004-08-26 05:21:59 +0000 | [diff] [blame] | 1643 | |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1644 | # If expected output uses ellipsis, a meaningful fancy diff is |
Tim Peters | 5b799c1 | 2004-08-26 05:21:59 +0000 | [diff] [blame] | 1645 | # too hard ... or maybe not. In two real-life failures Tim saw, |
| 1646 | # a diff was a major help anyway, so this is commented out. |
| 1647 | # [todo] _ellipsis_match() knows which pieces do and don't match, |
| 1648 | # and could be the basis for a kick-ass diff in this case. |
| 1649 | ##if optionflags & ELLIPSIS and ELLIPSIS_MARKER in want: |
| 1650 | ## return False |
| 1651 | |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1652 | # ndiff does intraline difference marking, so can be useful even |
Tim Peters | 5b799c1 | 2004-08-26 05:21:59 +0000 | [diff] [blame] | 1653 | # for 1-line differences. |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1654 | if optionflags & REPORT_NDIFF: |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1655 | return True |
Tim Peters | 5b799c1 | 2004-08-26 05:21:59 +0000 | [diff] [blame] | 1656 | |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1657 | # The other diff types need at least a few lines to be helpful. |
| 1658 | return want.count('\n') > 2 and got.count('\n') > 2 |
| 1659 | |
Edward Loper | ca9111e | 2004-08-26 03:00:24 +0000 | [diff] [blame] | 1660 | def output_difference(self, example, got, optionflags): |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1661 | """ |
| 1662 | Return a string describing the differences between the |
Edward Loper | ca9111e | 2004-08-26 03:00:24 +0000 | [diff] [blame] | 1663 | expected output for a given example (`example`) and the actual |
| 1664 | output (`got`). `optionflags` is the set of option flags used |
| 1665 | to compare `want` and `got`. |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1666 | """ |
Edward Loper | ca9111e | 2004-08-26 03:00:24 +0000 | [diff] [blame] | 1667 | want = example.want |
Edward Loper | 68ba9a6 | 2004-08-12 02:43:49 +0000 | [diff] [blame] | 1668 | # If <BLANKLINE>s are being used, then replace blank lines |
| 1669 | # with <BLANKLINE> in the actual output string. |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1670 | if not (optionflags & DONT_ACCEPT_BLANKLINE): |
Edward Loper | 68ba9a6 | 2004-08-12 02:43:49 +0000 | [diff] [blame] | 1671 | got = re.sub('(?m)^[ ]*(?=\n)', BLANKLINE_MARKER, got) |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1672 | |
Tim Peters | 5b799c1 | 2004-08-26 05:21:59 +0000 | [diff] [blame] | 1673 | # Check if we should use diff. |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1674 | if self._do_a_fancy_diff(want, got, optionflags): |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1675 | # Split want & got into lines. |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 1676 | want_lines = want.splitlines(keepends=True) |
| 1677 | got_lines = got.splitlines(keepends=True) |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1678 | # Use difflib to find their differences. |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1679 | if optionflags & REPORT_UDIFF: |
Edward Loper | 5662929 | 2004-08-26 01:31:56 +0000 | [diff] [blame] | 1680 | diff = difflib.unified_diff(want_lines, got_lines, n=2) |
| 1681 | diff = list(diff)[2:] # strip the diff header |
| 1682 | kind = 'unified diff with -expected +actual' |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1683 | elif optionflags & REPORT_CDIFF: |
Edward Loper | 5662929 | 2004-08-26 01:31:56 +0000 | [diff] [blame] | 1684 | diff = difflib.context_diff(want_lines, got_lines, n=2) |
| 1685 | diff = list(diff)[2:] # strip the diff header |
| 1686 | kind = 'context diff with expected followed by actual' |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1687 | elif optionflags & REPORT_NDIFF: |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1688 | engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK) |
| 1689 | diff = list(engine.compare(want_lines, got_lines)) |
| 1690 | kind = 'ndiff with -expected +actual' |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1691 | else: |
| 1692 | assert 0, 'Bad diff option' |
| 1693 | # Remove trailing whitespace on diff output. |
| 1694 | diff = [line.rstrip() + '\n' for line in diff] |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 1695 | return 'Differences (%s):\n' % kind + _indent(''.join(diff)) |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1696 | |
| 1697 | # If we're not using diff, then simply list the expected |
| 1698 | # output followed by the actual output. |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 1699 | if want and got: |
| 1700 | return 'Expected:\n%sGot:\n%s' % (_indent(want), _indent(got)) |
| 1701 | elif want: |
| 1702 | return 'Expected:\n%sGot nothing\n' % _indent(want) |
| 1703 | elif got: |
| 1704 | return 'Expected nothing\nGot:\n%s' % _indent(got) |
| 1705 | else: |
| 1706 | return 'Expected nothing\nGot nothing\n' |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1707 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1708 | class DocTestFailure(Exception): |
| 1709 | """A DocTest example has failed in debugging mode. |
| 1710 | |
| 1711 | The exception instance has variables: |
| 1712 | |
| 1713 | - test: the DocTest object being run |
| 1714 | |
Neal Norwitz | c082cb7 | 2006-08-29 05:40:08 +0000 | [diff] [blame] | 1715 | - example: the Example object that failed |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1716 | |
| 1717 | - got: the actual output |
| 1718 | """ |
| 1719 | def __init__(self, test, example, got): |
| 1720 | self.test = test |
| 1721 | self.example = example |
| 1722 | self.got = got |
| 1723 | |
| 1724 | def __str__(self): |
| 1725 | return str(self.test) |
| 1726 | |
| 1727 | class UnexpectedException(Exception): |
| 1728 | """A DocTest example has encountered an unexpected exception |
| 1729 | |
| 1730 | The exception instance has variables: |
| 1731 | |
| 1732 | - test: the DocTest object being run |
| 1733 | |
Guido van Rossum | 6a2a2a0 | 2006-08-26 20:37:44 +0000 | [diff] [blame] | 1734 | - example: the Example object that failed |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1735 | |
| 1736 | - exc_info: the exception info |
| 1737 | """ |
| 1738 | def __init__(self, test, example, exc_info): |
| 1739 | self.test = test |
| 1740 | self.example = example |
| 1741 | self.exc_info = exc_info |
| 1742 | |
| 1743 | def __str__(self): |
| 1744 | return str(self.test) |
Tim Peters | d1b7827 | 2004-08-07 06:03:09 +0000 | [diff] [blame] | 1745 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1746 | class DebugRunner(DocTestRunner): |
| 1747 | r"""Run doc tests but raise an exception as soon as there is a failure. |
| 1748 | |
| 1749 | If an unexpected exception occurs, an UnexpectedException is raised. |
| 1750 | It contains the test, the example, and the original exception: |
| 1751 | |
| 1752 | >>> runner = DebugRunner(verbose=False) |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1753 | >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42', |
| 1754 | ... {}, 'foo', 'foo.py', 0) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1755 | >>> try: |
| 1756 | ... runner.run(test) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1757 | ... except UnexpectedException as f: |
| 1758 | ... failure = f |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1759 | |
| 1760 | >>> failure.test is test |
| 1761 | True |
| 1762 | |
| 1763 | >>> failure.example.want |
| 1764 | '42\n' |
| 1765 | |
| 1766 | >>> exc_info = failure.exc_info |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 1767 | >>> raise exc_info[1] # Already has the traceback |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1768 | Traceback (most recent call last): |
| 1769 | ... |
| 1770 | KeyError |
| 1771 | |
| 1772 | We wrap the original exception to give the calling application |
| 1773 | access to the test and example information. |
| 1774 | |
| 1775 | If the output doesn't match, then a DocTestFailure is raised: |
| 1776 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1777 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1778 | ... >>> x = 1 |
| 1779 | ... >>> x |
| 1780 | ... 2 |
| 1781 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1782 | |
| 1783 | >>> try: |
| 1784 | ... runner.run(test) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1785 | ... except DocTestFailure as f: |
| 1786 | ... failure = f |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1787 | |
| 1788 | DocTestFailure objects provide access to the test: |
| 1789 | |
| 1790 | >>> failure.test is test |
| 1791 | True |
| 1792 | |
| 1793 | As well as to the example: |
| 1794 | |
| 1795 | >>> failure.example.want |
| 1796 | '2\n' |
| 1797 | |
| 1798 | and the actual output: |
| 1799 | |
| 1800 | >>> failure.got |
| 1801 | '1\n' |
| 1802 | |
| 1803 | If a failure or error occurs, the globals are left intact: |
| 1804 | |
| 1805 | >>> del test.globs['__builtins__'] |
| 1806 | >>> test.globs |
| 1807 | {'x': 1} |
| 1808 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1809 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1810 | ... >>> x = 2 |
| 1811 | ... >>> raise KeyError |
| 1812 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1813 | |
| 1814 | >>> runner.run(test) |
| 1815 | Traceback (most recent call last): |
| 1816 | ... |
Guido van Rossum | 6a2a2a0 | 2006-08-26 20:37:44 +0000 | [diff] [blame] | 1817 | doctest.UnexpectedException: <DocTest foo from foo.py:0 (2 examples)> |
Tim Peters | d1b7827 | 2004-08-07 06:03:09 +0000 | [diff] [blame] | 1818 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1819 | >>> del test.globs['__builtins__'] |
| 1820 | >>> test.globs |
| 1821 | {'x': 2} |
| 1822 | |
| 1823 | But the globals are cleared if there is no error: |
| 1824 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1825 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1826 | ... >>> x = 2 |
| 1827 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1828 | |
| 1829 | >>> runner.run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1830 | TestResults(failed=0, attempted=1) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1831 | |
| 1832 | >>> test.globs |
| 1833 | {} |
| 1834 | |
| 1835 | """ |
| 1836 | |
| 1837 | def run(self, test, compileflags=None, out=None, clear_globs=True): |
| 1838 | r = DocTestRunner.run(self, test, compileflags, out, False) |
| 1839 | if clear_globs: |
| 1840 | test.globs.clear() |
| 1841 | return r |
| 1842 | |
| 1843 | def report_unexpected_exception(self, out, test, example, exc_info): |
| 1844 | raise UnexpectedException(test, example, exc_info) |
| 1845 | |
| 1846 | def report_failure(self, out, test, example, got): |
| 1847 | raise DocTestFailure(test, example, got) |
| 1848 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1849 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 1850 | ## 6. Test Functions |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1851 | ###################################################################### |
| 1852 | # These should be backwards compatible. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1853 | |
Tim Peters | 82076ef | 2004-09-13 00:52:51 +0000 | [diff] [blame] | 1854 | # For backward compatibility, a global instance of a DocTestRunner |
| 1855 | # class, updated by testmod. |
| 1856 | master = None |
| 1857 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1858 | def testmod(m=None, name=None, globs=None, verbose=None, |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1859 | report=True, optionflags=0, extraglobs=None, |
Tim Peters | 958cc89 | 2004-09-13 14:53:28 +0000 | [diff] [blame] | 1860 | raise_on_error=False, exclude_empty=False): |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1861 | """m=None, name=None, globs=None, verbose=None, report=True, |
| 1862 | optionflags=0, extraglobs=None, raise_on_error=False, |
Tim Peters | 958cc89 | 2004-09-13 14:53:28 +0000 | [diff] [blame] | 1863 | exclude_empty=False |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1864 | |
Martin v. Löwis | 4581cfa | 2002-11-22 08:23:09 +0000 | [diff] [blame] | 1865 | Test examples in docstrings in functions and classes reachable |
| 1866 | from module m (or the current module if m is not supplied), starting |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1867 | with m.__doc__. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1868 | |
| 1869 | Also test examples reachable from dict m.__test__ if it exists and is |
Tim Peters | c2388a2 | 2004-08-10 01:41:28 +0000 | [diff] [blame] | 1870 | not None. m.__test__ maps names to functions, classes and strings; |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1871 | function and class docstrings are tested even if the name is private; |
| 1872 | strings are tested directly, as if they were docstrings. |
| 1873 | |
| 1874 | Return (#failures, #tests). |
| 1875 | |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame] | 1876 | See help(doctest) for an overview. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1877 | |
| 1878 | Optional keyword arg "name" gives the name of the module; by default |
| 1879 | use m.__name__. |
| 1880 | |
| 1881 | Optional keyword arg "globs" gives a dict to be used as the globals |
| 1882 | when executing examples; by default, use m.__dict__. A copy of this |
| 1883 | dict is actually used for each docstring, so that each docstring's |
| 1884 | examples start with a clean slate. |
| 1885 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1886 | Optional keyword arg "extraglobs" gives a dictionary that should be |
| 1887 | merged into the globals that are used to execute examples. By |
| 1888 | default, no extra globals are used. This is new in 2.4. |
| 1889 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1890 | Optional keyword arg "verbose" prints lots of stuff if true, prints |
| 1891 | only failures if false; by default, it's true iff "-v" is in sys.argv. |
| 1892 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1893 | Optional keyword arg "report" prints a summary at the end when true, |
| 1894 | else prints nothing at the end. In verbose mode, the summary is |
| 1895 | detailed, else very brief (in fact, empty if all tests passed). |
| 1896 | |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 1897 | Optional keyword arg "optionflags" or's together module constants, |
Tim Peters | f82a9de | 2004-08-22 20:51:53 +0000 | [diff] [blame] | 1898 | and defaults to 0. This is new in 2.3. Possible values (see the |
| 1899 | docs for details): |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 1900 | |
| 1901 | DONT_ACCEPT_TRUE_FOR_1 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1902 | DONT_ACCEPT_BLANKLINE |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1903 | NORMALIZE_WHITESPACE |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1904 | ELLIPSIS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1905 | SKIP |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 1906 | IGNORE_EXCEPTION_DETAIL |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1907 | REPORT_UDIFF |
| 1908 | REPORT_CDIFF |
| 1909 | REPORT_NDIFF |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1910 | REPORT_ONLY_FIRST_FAILURE |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1911 | |
| 1912 | Optional keyword arg "raise_on_error" raises an exception on the |
| 1913 | first unexpected exception or failure. This allows failures to be |
| 1914 | post-mortem debugged. |
| 1915 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1916 | Advanced tomfoolery: testmod runs methods of a local instance of |
| 1917 | class doctest.Tester, then merges the results into (or creates) |
| 1918 | global Tester instance doctest.master. Methods of doctest.master |
| 1919 | can be called directly too, if you want to do something unusual. |
| 1920 | Passing report=0 to testmod is especially useful then, to delay |
| 1921 | displaying a summary. Invoke doctest.master.summarize(verbose) |
| 1922 | when you're done fiddling. |
| 1923 | """ |
Tim Peters | 82076ef | 2004-09-13 00:52:51 +0000 | [diff] [blame] | 1924 | global master |
| 1925 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1926 | # If no module was given, then use __main__. |
Martin v. Löwis | 4581cfa | 2002-11-22 08:23:09 +0000 | [diff] [blame] | 1927 | if m is None: |
Martin v. Löwis | 4581cfa | 2002-11-22 08:23:09 +0000 | [diff] [blame] | 1928 | # DWA - m will still be None if this wasn't invoked from the command |
| 1929 | # line, in which case the following TypeError is about as good an error |
| 1930 | # as we should expect |
| 1931 | m = sys.modules.get('__main__') |
| 1932 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1933 | # Check that we were actually given a module. |
| 1934 | if not inspect.ismodule(m): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1935 | raise TypeError("testmod: module required; %r" % (m,)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1936 | |
| 1937 | # If no name was given, then use the module's name. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1938 | if name is None: |
| 1939 | name = m.__name__ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1940 | |
| 1941 | # Find, parse, and run all tests in the given module. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1942 | finder = DocTestFinder(exclude_empty=exclude_empty) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1943 | |
| 1944 | if raise_on_error: |
| 1945 | runner = DebugRunner(verbose=verbose, optionflags=optionflags) |
| 1946 | else: |
| 1947 | runner = DocTestRunner(verbose=verbose, optionflags=optionflags) |
| 1948 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1949 | for test in finder.find(m, name, globs=globs, extraglobs=extraglobs): |
| 1950 | runner.run(test) |
| 1951 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1952 | if report: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1953 | runner.summarize() |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1954 | |
Tim Peters | 82076ef | 2004-09-13 00:52:51 +0000 | [diff] [blame] | 1955 | if master is None: |
| 1956 | master = runner |
| 1957 | else: |
| 1958 | master.merge(runner) |
| 1959 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1960 | return TestResults(runner.failures, runner.tries) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1961 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 1962 | def testfile(filename, module_relative=True, name=None, package=None, |
| 1963 | globs=None, verbose=None, report=True, optionflags=0, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1964 | extraglobs=None, raise_on_error=False, parser=DocTestParser(), |
| 1965 | encoding=None): |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 1966 | """ |
| 1967 | Test examples in the given file. Return (#failures, #tests). |
| 1968 | |
| 1969 | Optional keyword arg "module_relative" specifies how filenames |
| 1970 | should be interpreted: |
| 1971 | |
| 1972 | - If "module_relative" is True (the default), then "filename" |
| 1973 | specifies a module-relative path. By default, this path is |
| 1974 | relative to the calling module's directory; but if the |
| 1975 | "package" argument is specified, then it is relative to that |
| 1976 | package. To ensure os-independence, "filename" should use |
| 1977 | "/" characters to separate path segments, and should not |
| 1978 | be an absolute path (i.e., it may not begin with "/"). |
| 1979 | |
| 1980 | - If "module_relative" is False, then "filename" specifies an |
| 1981 | os-specific path. The path may be absolute or relative (to |
| 1982 | the current working directory). |
| 1983 | |
Edward Loper | a2fc7ec | 2004-09-21 03:24:24 +0000 | [diff] [blame] | 1984 | Optional keyword arg "name" gives the name of the test; by default |
| 1985 | use the file's basename. |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 1986 | |
| 1987 | Optional keyword argument "package" is a Python package or the |
| 1988 | name of a Python package whose directory should be used as the |
| 1989 | base directory for a module relative filename. If no package is |
| 1990 | specified, then the calling module's directory is used as the base |
| 1991 | directory for module relative filenames. It is an error to |
| 1992 | specify "package" if "module_relative" is False. |
| 1993 | |
| 1994 | Optional keyword arg "globs" gives a dict to be used as the globals |
| 1995 | when executing examples; by default, use {}. A copy of this dict |
| 1996 | is actually used for each docstring, so that each docstring's |
| 1997 | examples start with a clean slate. |
| 1998 | |
| 1999 | Optional keyword arg "extraglobs" gives a dictionary that should be |
| 2000 | merged into the globals that are used to execute examples. By |
| 2001 | default, no extra globals are used. |
| 2002 | |
| 2003 | Optional keyword arg "verbose" prints lots of stuff if true, prints |
| 2004 | only failures if false; by default, it's true iff "-v" is in sys.argv. |
| 2005 | |
| 2006 | Optional keyword arg "report" prints a summary at the end when true, |
| 2007 | else prints nothing at the end. In verbose mode, the summary is |
| 2008 | detailed, else very brief (in fact, empty if all tests passed). |
| 2009 | |
| 2010 | Optional keyword arg "optionflags" or's together module constants, |
| 2011 | and defaults to 0. Possible values (see the docs for details): |
| 2012 | |
| 2013 | DONT_ACCEPT_TRUE_FOR_1 |
| 2014 | DONT_ACCEPT_BLANKLINE |
| 2015 | NORMALIZE_WHITESPACE |
| 2016 | ELLIPSIS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2017 | SKIP |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2018 | IGNORE_EXCEPTION_DETAIL |
| 2019 | REPORT_UDIFF |
| 2020 | REPORT_CDIFF |
| 2021 | REPORT_NDIFF |
| 2022 | REPORT_ONLY_FIRST_FAILURE |
| 2023 | |
| 2024 | Optional keyword arg "raise_on_error" raises an exception on the |
| 2025 | first unexpected exception or failure. This allows failures to be |
| 2026 | post-mortem debugged. |
| 2027 | |
Edward Loper | 498a186 | 2004-09-27 03:42:58 +0000 | [diff] [blame] | 2028 | Optional keyword arg "parser" specifies a DocTestParser (or |
| 2029 | subclass) that should be used to extract tests from the files. |
| 2030 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2031 | Optional keyword arg "encoding" specifies an encoding that should |
| 2032 | be used to convert the file to unicode. |
| 2033 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2034 | Advanced tomfoolery: testmod runs methods of a local instance of |
| 2035 | class doctest.Tester, then merges the results into (or creates) |
| 2036 | global Tester instance doctest.master. Methods of doctest.master |
| 2037 | can be called directly too, if you want to do something unusual. |
| 2038 | Passing report=0 to testmod is especially useful then, to delay |
| 2039 | displaying a summary. Invoke doctest.master.summarize(verbose) |
| 2040 | when you're done fiddling. |
| 2041 | """ |
| 2042 | global master |
| 2043 | |
| 2044 | if package and not module_relative: |
| 2045 | raise ValueError("Package may only be specified for module-" |
| 2046 | "relative paths.") |
Tim Peters | bab3e99 | 2004-09-20 19:52:34 +0000 | [diff] [blame] | 2047 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2048 | # Relativize the path |
Guido van Rossum | 1b81e7b | 2007-08-29 03:53:53 +0000 | [diff] [blame] | 2049 | text, filename = _load_testfile(filename, package, module_relative, |
| 2050 | encoding or "utf-8") |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2051 | |
| 2052 | # If no name was given, then use the file's name. |
| 2053 | if name is None: |
Edward Loper | a2fc7ec | 2004-09-21 03:24:24 +0000 | [diff] [blame] | 2054 | name = os.path.basename(filename) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2055 | |
| 2056 | # Assemble the globals. |
| 2057 | if globs is None: |
| 2058 | globs = {} |
| 2059 | else: |
| 2060 | globs = globs.copy() |
| 2061 | if extraglobs is not None: |
| 2062 | globs.update(extraglobs) |
Raymond Hettinger | 0f05517 | 2009-01-27 10:06:09 +0000 | [diff] [blame] | 2063 | if '__name__' not in globs: |
| 2064 | globs['__name__'] = '__main__' |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2065 | |
| 2066 | if raise_on_error: |
| 2067 | runner = DebugRunner(verbose=verbose, optionflags=optionflags) |
| 2068 | else: |
| 2069 | runner = DocTestRunner(verbose=verbose, optionflags=optionflags) |
| 2070 | |
| 2071 | # Read the file, convert it to a test, and run it. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2072 | test = parser.get_doctest(text, globs, name, filename, 0) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2073 | runner.run(test) |
| 2074 | |
| 2075 | if report: |
| 2076 | runner.summarize() |
| 2077 | |
| 2078 | if master is None: |
| 2079 | master = runner |
| 2080 | else: |
| 2081 | master.merge(runner) |
| 2082 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2083 | return TestResults(runner.failures, runner.tries) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2084 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2085 | def run_docstring_examples(f, globs, verbose=False, name="NoName", |
| 2086 | compileflags=None, optionflags=0): |
| 2087 | """ |
| 2088 | Test examples in the given object's docstring (`f`), using `globs` |
| 2089 | as globals. Optional argument `name` is used in failure messages. |
| 2090 | If the optional argument `verbose` is true, then generate output |
| 2091 | even if there are no failures. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2092 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2093 | `compileflags` gives the set of flags that should be used by the |
| 2094 | Python compiler when running the examples. If not specified, then |
| 2095 | it will default to the set of future-import flags that apply to |
| 2096 | `globs`. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2097 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2098 | Optional keyword arg `optionflags` specifies options for the |
| 2099 | testing and output. See the documentation for `testmod` for more |
| 2100 | information. |
| 2101 | """ |
| 2102 | # Find, parse, and run all tests in the given module. |
| 2103 | finder = DocTestFinder(verbose=verbose, recurse=False) |
| 2104 | runner = DocTestRunner(verbose=verbose, optionflags=optionflags) |
| 2105 | for test in finder.find(f, name, globs=globs): |
| 2106 | runner.run(test, compileflags=compileflags) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2107 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2108 | ###################################################################### |
Georg Brandl | 3183585 | 2008-05-12 17:38:56 +0000 | [diff] [blame] | 2109 | ## 7. Unittest Support |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2110 | ###################################################################### |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2111 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2112 | _unittest_reportflags = 0 |
Tim Peters | 38330fe | 2004-08-30 16:19:24 +0000 | [diff] [blame] | 2113 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2114 | def set_unittest_reportflags(flags): |
Tim Peters | 38330fe | 2004-08-30 16:19:24 +0000 | [diff] [blame] | 2115 | """Sets the unittest option flags. |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2116 | |
| 2117 | The old flag is returned so that a runner could restore the old |
| 2118 | value if it wished to: |
| 2119 | |
Tim Peters | b7e99b6 | 2005-03-28 23:50:54 +0000 | [diff] [blame] | 2120 | >>> import doctest |
| 2121 | >>> old = doctest._unittest_reportflags |
| 2122 | >>> doctest.set_unittest_reportflags(REPORT_NDIFF | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2123 | ... REPORT_ONLY_FIRST_FAILURE) == old |
| 2124 | True |
| 2125 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2126 | >>> doctest._unittest_reportflags == (REPORT_NDIFF | |
| 2127 | ... REPORT_ONLY_FIRST_FAILURE) |
| 2128 | True |
Tim Peters | df7a208 | 2004-08-29 00:38:17 +0000 | [diff] [blame] | 2129 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2130 | Only reporting flags can be set: |
| 2131 | |
Tim Peters | b7e99b6 | 2005-03-28 23:50:54 +0000 | [diff] [blame] | 2132 | >>> doctest.set_unittest_reportflags(ELLIPSIS) |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2133 | Traceback (most recent call last): |
| 2134 | ... |
Tim Peters | 38330fe | 2004-08-30 16:19:24 +0000 | [diff] [blame] | 2135 | ValueError: ('Only reporting flags allowed', 8) |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2136 | |
Tim Peters | b7e99b6 | 2005-03-28 23:50:54 +0000 | [diff] [blame] | 2137 | >>> doctest.set_unittest_reportflags(old) == (REPORT_NDIFF | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2138 | ... REPORT_ONLY_FIRST_FAILURE) |
| 2139 | True |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2140 | """ |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2141 | global _unittest_reportflags |
Tim Peters | 38330fe | 2004-08-30 16:19:24 +0000 | [diff] [blame] | 2142 | |
| 2143 | if (flags & REPORTING_FLAGS) != flags: |
| 2144 | raise ValueError("Only reporting flags allowed", flags) |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2145 | old = _unittest_reportflags |
| 2146 | _unittest_reportflags = flags |
| 2147 | return old |
Tim Peters | df7a208 | 2004-08-29 00:38:17 +0000 | [diff] [blame] | 2148 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2149 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2150 | class DocTestCase(unittest.TestCase): |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2151 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 2152 | def __init__(self, test, optionflags=0, setUp=None, tearDown=None, |
| 2153 | checker=None): |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 2154 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2155 | unittest.TestCase.__init__(self) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2156 | self._dt_optionflags = optionflags |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 2157 | self._dt_checker = checker |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2158 | self._dt_test = test |
| 2159 | self._dt_setUp = setUp |
| 2160 | self._dt_tearDown = tearDown |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2161 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2162 | def setUp(self): |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2163 | test = self._dt_test |
Tim Peters | df7a208 | 2004-08-29 00:38:17 +0000 | [diff] [blame] | 2164 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2165 | if self._dt_setUp is not None: |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2166 | self._dt_setUp(test) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2167 | |
| 2168 | def tearDown(self): |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2169 | test = self._dt_test |
| 2170 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2171 | if self._dt_tearDown is not None: |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2172 | self._dt_tearDown(test) |
| 2173 | |
| 2174 | test.globs.clear() |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2175 | |
| 2176 | def runTest(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2177 | test = self._dt_test |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2178 | old = sys.stdout |
| 2179 | new = StringIO() |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2180 | optionflags = self._dt_optionflags |
Tim Peters | df7a208 | 2004-08-29 00:38:17 +0000 | [diff] [blame] | 2181 | |
Tim Peters | 38330fe | 2004-08-30 16:19:24 +0000 | [diff] [blame] | 2182 | if not (optionflags & REPORTING_FLAGS): |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2183 | # The option flags don't include any reporting flags, |
| 2184 | # so add the default reporting flags |
| 2185 | optionflags |= _unittest_reportflags |
Tim Peters | df7a208 | 2004-08-29 00:38:17 +0000 | [diff] [blame] | 2186 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2187 | runner = DocTestRunner(optionflags=optionflags, |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 2188 | checker=self._dt_checker, verbose=False) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2189 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2190 | try: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2191 | runner.DIVIDER = "-"*70 |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2192 | failures, tries = runner.run( |
| 2193 | test, out=new.write, clear_globs=False) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2194 | finally: |
| 2195 | sys.stdout = old |
| 2196 | |
| 2197 | if failures: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2198 | raise self.failureException(self.format_failure(new.getvalue())) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2199 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2200 | def format_failure(self, err): |
| 2201 | test = self._dt_test |
| 2202 | if test.lineno is None: |
| 2203 | lineno = 'unknown line number' |
| 2204 | else: |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 2205 | lineno = '%s' % test.lineno |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2206 | lname = '.'.join(test.name.split('.')[-1:]) |
| 2207 | return ('Failed doctest test for %s\n' |
| 2208 | ' File "%s", line %s, in %s\n\n%s' |
| 2209 | % (test.name, test.filename, lineno, lname, err) |
| 2210 | ) |
| 2211 | |
| 2212 | def debug(self): |
| 2213 | r"""Run the test case without results and without catching exceptions |
| 2214 | |
| 2215 | The unit test framework includes a debug method on test cases |
| 2216 | and test suites to support post-mortem debugging. The test code |
| 2217 | is run in such a way that errors are not caught. This way a |
| 2218 | caller can catch the errors and initiate post-mortem debugging. |
| 2219 | |
| 2220 | The DocTestCase provides a debug method that raises |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2221 | UnexpectedException errors if there is an unexpected |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2222 | exception: |
| 2223 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 2224 | >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2225 | ... {}, 'foo', 'foo.py', 0) |
| 2226 | >>> case = DocTestCase(test) |
| 2227 | >>> try: |
| 2228 | ... case.debug() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2229 | ... except UnexpectedException as f: |
| 2230 | ... failure = f |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2231 | |
| 2232 | The UnexpectedException contains the test, the example, and |
| 2233 | the original exception: |
| 2234 | |
| 2235 | >>> failure.test is test |
| 2236 | True |
| 2237 | |
| 2238 | >>> failure.example.want |
| 2239 | '42\n' |
| 2240 | |
| 2241 | >>> exc_info = failure.exc_info |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 2242 | >>> raise exc_info[1] # Already has the traceback |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2243 | Traceback (most recent call last): |
| 2244 | ... |
| 2245 | KeyError |
| 2246 | |
| 2247 | If the output doesn't match, then a DocTestFailure is raised: |
| 2248 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 2249 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2250 | ... >>> x = 1 |
| 2251 | ... >>> x |
| 2252 | ... 2 |
| 2253 | ... ''', {}, 'foo', 'foo.py', 0) |
| 2254 | >>> case = DocTestCase(test) |
| 2255 | |
| 2256 | >>> try: |
| 2257 | ... case.debug() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2258 | ... except DocTestFailure as f: |
| 2259 | ... failure = f |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2260 | |
| 2261 | DocTestFailure objects provide access to the test: |
| 2262 | |
| 2263 | >>> failure.test is test |
| 2264 | True |
| 2265 | |
| 2266 | As well as to the example: |
| 2267 | |
| 2268 | >>> failure.example.want |
| 2269 | '2\n' |
| 2270 | |
| 2271 | and the actual output: |
| 2272 | |
| 2273 | >>> failure.got |
| 2274 | '1\n' |
| 2275 | |
| 2276 | """ |
| 2277 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2278 | self.setUp() |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 2279 | runner = DebugRunner(optionflags=self._dt_optionflags, |
| 2280 | checker=self._dt_checker, verbose=False) |
Alexandre Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 2281 | runner.run(self._dt_test, clear_globs=False) |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2282 | self.tearDown() |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2283 | |
| 2284 | def id(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2285 | return self._dt_test.name |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2286 | |
Antoine Pitrou | 2bc801c | 2011-12-18 19:27:45 +0100 | [diff] [blame] | 2287 | def __eq__(self, other): |
| 2288 | if type(self) is not type(other): |
| 2289 | return NotImplemented |
| 2290 | |
| 2291 | return self._dt_test == other._dt_test and \ |
| 2292 | self._dt_optionflags == other._dt_optionflags and \ |
| 2293 | self._dt_setUp == other._dt_setUp and \ |
| 2294 | self._dt_tearDown == other._dt_tearDown and \ |
| 2295 | self._dt_checker == other._dt_checker |
| 2296 | |
Antoine Pitrou | 165b128 | 2011-12-18 20:20:17 +0100 | [diff] [blame] | 2297 | def __hash__(self): |
| 2298 | return hash((self._dt_optionflags, self._dt_setUp, self._dt_tearDown, |
| 2299 | self._dt_checker)) |
| 2300 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2301 | def __repr__(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2302 | name = self._dt_test.name.split('.') |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2303 | return "%s (%s)" % (name[-1], '.'.join(name[:-1])) |
| 2304 | |
| 2305 | __str__ = __repr__ |
| 2306 | |
| 2307 | def shortDescription(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2308 | return "Doctest: " + self._dt_test.name |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2309 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 2310 | class SkipDocTestCase(DocTestCase): |
R David Murray | e112153 | 2012-03-21 14:53:42 -0400 | [diff] [blame] | 2311 | def __init__(self, module): |
| 2312 | self.module = module |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 2313 | DocTestCase.__init__(self, None) |
| 2314 | |
| 2315 | def setUp(self): |
| 2316 | self.skipTest("DocTestSuite will not work with -O2 and above") |
| 2317 | |
| 2318 | def test_skip(self): |
| 2319 | pass |
| 2320 | |
| 2321 | def shortDescription(self): |
R David Murray | e112153 | 2012-03-21 14:53:42 -0400 | [diff] [blame] | 2322 | return "Skipping tests from %s" % self.module.__name__ |
| 2323 | |
| 2324 | __str__ = shortDescription |
| 2325 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 2326 | |
Andrew Svetlov | 7c1017b | 2013-08-29 01:24:39 +0300 | [diff] [blame] | 2327 | class _DocTestSuite(unittest.TestSuite): |
| 2328 | |
| 2329 | def _removeTestAtIndex(self, index): |
| 2330 | pass |
| 2331 | |
| 2332 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2333 | def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None, |
| 2334 | **options): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2335 | """ |
Tim Peters | 75dc5e1 | 2004-08-22 17:50:45 +0000 | [diff] [blame] | 2336 | Convert doctest tests for a module to a unittest test suite. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2337 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2338 | This converts each documentation string in a module that |
| 2339 | contains doctest tests to a unittest test case. If any of the |
| 2340 | tests in a doc string fail, then the test case fails. An exception |
| 2341 | is raised showing the name of the file containing the test and a |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2342 | (sometimes approximate) line number. |
| 2343 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2344 | The `module` argument provides the module to be tested. The argument |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2345 | can be either a module or a module name. |
| 2346 | |
| 2347 | If no argument is given, the calling module is used. |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2348 | |
| 2349 | A number of options may be provided as keyword arguments: |
| 2350 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2351 | setUp |
Edward Loper | a2fc7ec | 2004-09-21 03:24:24 +0000 | [diff] [blame] | 2352 | A set-up function. This is called before running the |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2353 | tests in each file. The setUp function will be passed a DocTest |
| 2354 | object. The setUp function can access the test globals as the |
| 2355 | globs attribute of the test passed. |
| 2356 | |
| 2357 | tearDown |
Edward Loper | a2fc7ec | 2004-09-21 03:24:24 +0000 | [diff] [blame] | 2358 | A tear-down function. This is called after running the |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2359 | tests in each file. The tearDown function will be passed a DocTest |
| 2360 | object. The tearDown function can access the test globals as the |
| 2361 | globs attribute of the test passed. |
| 2362 | |
| 2363 | globs |
| 2364 | A dictionary containing initial global variables for the tests. |
| 2365 | |
| 2366 | optionflags |
| 2367 | A set of doctest option flags expressed as an integer. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2368 | """ |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2369 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2370 | if test_finder is None: |
| 2371 | test_finder = DocTestFinder() |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2372 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2373 | module = _normalize_module(module) |
| 2374 | tests = test_finder.find(module, globs=globs, extraglobs=extraglobs) |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 2375 | |
| 2376 | if not tests and sys.flags.optimize >=2: |
| 2377 | # Skip doctests when running with -O2 |
Andrew Svetlov | 7c1017b | 2013-08-29 01:24:39 +0300 | [diff] [blame] | 2378 | suite = _DocTestSuite() |
R David Murray | e112153 | 2012-03-21 14:53:42 -0400 | [diff] [blame] | 2379 | suite.addTest(SkipDocTestCase(module)) |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 2380 | return suite |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2381 | |
| 2382 | tests.sort() |
Andrew Svetlov | 7c1017b | 2013-08-29 01:24:39 +0300 | [diff] [blame] | 2383 | suite = _DocTestSuite() |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 2384 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2385 | for test in tests: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2386 | if len(test.examples) == 0: |
| 2387 | continue |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2388 | if not test.filename: |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2389 | filename = module.__file__ |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 2390 | if filename[-4:] == ".pyc": |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2391 | filename = filename[:-1] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2392 | test.filename = filename |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2393 | suite.addTest(DocTestCase(test, **options)) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2394 | |
| 2395 | return suite |
| 2396 | |
| 2397 | class DocFileCase(DocTestCase): |
| 2398 | |
| 2399 | def id(self): |
| 2400 | return '_'.join(self._dt_test.name.split('.')) |
| 2401 | |
| 2402 | def __repr__(self): |
| 2403 | return self._dt_test.filename |
| 2404 | __str__ = __repr__ |
| 2405 | |
| 2406 | def format_failure(self, err): |
| 2407 | return ('Failed doctest test for %s\n File "%s", line 0\n\n%s' |
| 2408 | % (self._dt_test.name, self._dt_test.filename, err) |
| 2409 | ) |
| 2410 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2411 | def DocFileTest(path, module_relative=True, package=None, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2412 | globs=None, parser=DocTestParser(), |
| 2413 | encoding=None, **options): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2414 | if globs is None: |
| 2415 | globs = {} |
Fred Drake | 7c404a4 | 2004-12-21 23:46:34 +0000 | [diff] [blame] | 2416 | else: |
| 2417 | globs = globs.copy() |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2418 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2419 | if package and not module_relative: |
| 2420 | raise ValueError("Package may only be specified for module-" |
| 2421 | "relative paths.") |
Tim Peters | bab3e99 | 2004-09-20 19:52:34 +0000 | [diff] [blame] | 2422 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2423 | # Relativize the path. |
Guido van Rossum | 1b81e7b | 2007-08-29 03:53:53 +0000 | [diff] [blame] | 2424 | doc, path = _load_testfile(path, package, module_relative, |
| 2425 | encoding or "utf-8") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2426 | |
Fred Drake | 7c404a4 | 2004-12-21 23:46:34 +0000 | [diff] [blame] | 2427 | if "__file__" not in globs: |
| 2428 | globs["__file__"] = path |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2429 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2430 | # Find the file and read it. |
Edward Loper | a2fc7ec | 2004-09-21 03:24:24 +0000 | [diff] [blame] | 2431 | name = os.path.basename(path) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2432 | |
| 2433 | # Convert it to a test, and wrap it in a DocFileCase. |
Edward Loper | 498a186 | 2004-09-27 03:42:58 +0000 | [diff] [blame] | 2434 | test = parser.get_doctest(doc, globs, name, path, 0) |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2435 | return DocFileCase(test, **options) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2436 | |
| 2437 | def DocFileSuite(*paths, **kw): |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2438 | """A unittest suite for one or more doctest files. |
Tim Peters | bab3e99 | 2004-09-20 19:52:34 +0000 | [diff] [blame] | 2439 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2440 | The path to each doctest file is given as a string; the |
| 2441 | interpretation of that string depends on the keyword argument |
| 2442 | "module_relative". |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2443 | |
| 2444 | A number of options may be provided as keyword arguments: |
| 2445 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2446 | module_relative |
| 2447 | If "module_relative" is True, then the given file paths are |
| 2448 | interpreted as os-independent module-relative paths. By |
| 2449 | default, these paths are relative to the calling module's |
| 2450 | directory; but if the "package" argument is specified, then |
| 2451 | they are relative to that package. To ensure os-independence, |
| 2452 | "filename" should use "/" characters to separate path |
| 2453 | segments, and may not be an absolute path (i.e., it may not |
| 2454 | begin with "/"). |
Tim Peters | bab3e99 | 2004-09-20 19:52:34 +0000 | [diff] [blame] | 2455 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2456 | If "module_relative" is False, then the given file paths are |
| 2457 | interpreted as os-specific paths. These paths may be absolute |
| 2458 | or relative (to the current working directory). |
| 2459 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2460 | package |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2461 | A Python package or the name of a Python package whose directory |
| 2462 | should be used as the base directory for module relative paths. |
| 2463 | If "package" is not specified, then the calling module's |
| 2464 | directory is used as the base directory for module relative |
| 2465 | filenames. It is an error to specify "package" if |
| 2466 | "module_relative" is False. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2467 | |
| 2468 | setUp |
Edward Loper | a2fc7ec | 2004-09-21 03:24:24 +0000 | [diff] [blame] | 2469 | A set-up function. This is called before running the |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2470 | tests in each file. The setUp function will be passed a DocTest |
| 2471 | object. The setUp function can access the test globals as the |
| 2472 | globs attribute of the test passed. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2473 | |
| 2474 | tearDown |
Edward Loper | a2fc7ec | 2004-09-21 03:24:24 +0000 | [diff] [blame] | 2475 | A tear-down function. This is called after running the |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2476 | tests in each file. The tearDown function will be passed a DocTest |
| 2477 | object. The tearDown function can access the test globals as the |
| 2478 | globs attribute of the test passed. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2479 | |
| 2480 | globs |
| 2481 | A dictionary containing initial global variables for the tests. |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2482 | |
| 2483 | optionflags |
Edward Loper | 498a186 | 2004-09-27 03:42:58 +0000 | [diff] [blame] | 2484 | A set of doctest option flags expressed as an integer. |
| 2485 | |
| 2486 | parser |
| 2487 | A DocTestParser (or subclass) that should be used to extract |
| 2488 | tests from the files. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2489 | |
| 2490 | encoding |
| 2491 | An encoding that will be used to convert the files to unicode. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2492 | """ |
Andrew Svetlov | 7c1017b | 2013-08-29 01:24:39 +0300 | [diff] [blame] | 2493 | suite = _DocTestSuite() |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2494 | |
| 2495 | # We do this here so that _normalize_module is called at the right |
| 2496 | # level. If it were called in DocFileTest, then this function |
| 2497 | # would be the caller and we might guess the package incorrectly. |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2498 | if kw.get('module_relative', True): |
| 2499 | kw['package'] = _normalize_module(kw.get('package')) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2500 | |
| 2501 | for path in paths: |
| 2502 | suite.addTest(DocFileTest(path, **kw)) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2503 | |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2504 | return suite |
| 2505 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2506 | ###################################################################### |
Georg Brandl | 3183585 | 2008-05-12 17:38:56 +0000 | [diff] [blame] | 2507 | ## 8. Debugging Support |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2508 | ###################################################################### |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2509 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2510 | def script_from_examples(s): |
| 2511 | r"""Extract script from text with examples. |
| 2512 | |
| 2513 | Converts text with examples to a Python script. Example input is |
| 2514 | converted to regular code. Example output and all other words |
| 2515 | are converted to comments: |
| 2516 | |
| 2517 | >>> text = ''' |
| 2518 | ... Here are examples of simple math. |
| 2519 | ... |
| 2520 | ... Python has super accurate integer addition |
| 2521 | ... |
| 2522 | ... >>> 2 + 2 |
| 2523 | ... 5 |
| 2524 | ... |
| 2525 | ... And very friendly error messages: |
| 2526 | ... |
| 2527 | ... >>> 1/0 |
| 2528 | ... To Infinity |
| 2529 | ... And |
| 2530 | ... Beyond |
| 2531 | ... |
| 2532 | ... You can use logic if you want: |
| 2533 | ... |
| 2534 | ... >>> if 0: |
| 2535 | ... ... blah |
| 2536 | ... ... blah |
| 2537 | ... ... |
| 2538 | ... |
| 2539 | ... Ho hum |
| 2540 | ... ''' |
| 2541 | |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2542 | >>> print(script_from_examples(text)) |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2543 | # Here are examples of simple math. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2544 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2545 | # Python has super accurate integer addition |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2546 | # |
| 2547 | 2 + 2 |
| 2548 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2549 | ## 5 |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2550 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2551 | # And very friendly error messages: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2552 | # |
| 2553 | 1/0 |
| 2554 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2555 | ## To Infinity |
| 2556 | ## And |
| 2557 | ## Beyond |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2558 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2559 | # You can use logic if you want: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2560 | # |
| 2561 | if 0: |
| 2562 | blah |
| 2563 | blah |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2564 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2565 | # Ho hum |
Georg Brandl | ecf93c7 | 2005-06-26 23:09:51 +0000 | [diff] [blame] | 2566 | <BLANKLINE> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2567 | """ |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 2568 | output = [] |
| 2569 | for piece in DocTestParser().parse(s): |
| 2570 | if isinstance(piece, Example): |
| 2571 | # Add the example's source code (strip trailing NL) |
| 2572 | output.append(piece.source[:-1]) |
| 2573 | # Add the expected output: |
| 2574 | want = piece.want |
| 2575 | if want: |
| 2576 | output.append('# Expected:') |
| 2577 | output += ['## '+l for l in want.split('\n')[:-1]] |
| 2578 | else: |
| 2579 | # Add non-example text. |
| 2580 | output += [_comment_line(l) |
| 2581 | for l in piece.split('\n')[:-1]] |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2582 | |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 2583 | # Trim junk on both ends. |
| 2584 | while output and output[-1] == '#': |
| 2585 | output.pop() |
| 2586 | while output and output[0] == '#': |
| 2587 | output.pop(0) |
| 2588 | # Combine the output, and return it. |
Georg Brandl | 1f14964 | 2005-06-26 22:22:31 +0000 | [diff] [blame] | 2589 | # Add a courtesy newline to prevent exec from choking (see bug #1172785) |
| 2590 | return '\n'.join(output) + '\n' |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2591 | |
| 2592 | def testsource(module, name): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2593 | """Extract the test sources from a doctest docstring as a script. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2594 | |
| 2595 | Provide the module (or dotted name of the module) containing the |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2596 | test to be debugged and the name (within the module) of the object |
| 2597 | with the doc string with tests to be debugged. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2598 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2599 | module = _normalize_module(module) |
| 2600 | tests = DocTestFinder().find(module) |
| 2601 | test = [t for t in tests if t.name == name] |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2602 | if not test: |
| 2603 | raise ValueError(name, "not found in tests") |
| 2604 | test = test[0] |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2605 | testsrc = script_from_examples(test.docstring) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2606 | return testsrc |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2607 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2608 | def debug_src(src, pm=False, globs=None): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2609 | """Debug a single doctest docstring, in argument `src`'""" |
| 2610 | testsrc = script_from_examples(src) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2611 | debug_script(testsrc, pm, globs) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2612 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2613 | def debug_script(src, pm=False, globs=None): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2614 | "Debug a test script. `src` is the script, as a string." |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2615 | import pdb |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2616 | |
Victor Stinner | 12b8d14 | 2011-06-30 17:35:55 +0200 | [diff] [blame] | 2617 | if globs: |
| 2618 | globs = globs.copy() |
| 2619 | else: |
| 2620 | globs = {} |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2621 | |
Victor Stinner | 12b8d14 | 2011-06-30 17:35:55 +0200 | [diff] [blame] | 2622 | if pm: |
| 2623 | try: |
| 2624 | exec(src, globs, globs) |
| 2625 | except: |
| 2626 | print(sys.exc_info()[1]) |
| 2627 | p = pdb.Pdb(nosigint=True) |
| 2628 | p.reset() |
| 2629 | p.interaction(None, sys.exc_info()[2]) |
| 2630 | else: |
| 2631 | pdb.Pdb(nosigint=True).run("exec(%r)" % src, globs, globs) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2632 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2633 | def debug(module, name, pm=False): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2634 | """Debug a single doctest docstring. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2635 | |
| 2636 | Provide the module (or dotted name of the module) containing the |
| 2637 | test to be debugged and the name (within the module) of the object |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2638 | with the docstring with tests to be debugged. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2639 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2640 | module = _normalize_module(module) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2641 | testsrc = testsource(module, name) |
| 2642 | debug_script(testsrc, pm, module.__dict__) |
| 2643 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2644 | ###################################################################### |
Georg Brandl | 3183585 | 2008-05-12 17:38:56 +0000 | [diff] [blame] | 2645 | ## 9. Example Usage |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2646 | ###################################################################### |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2647 | class _TestClass: |
| 2648 | """ |
| 2649 | A pointless class, for sanity-checking of docstring testing. |
| 2650 | |
| 2651 | Methods: |
| 2652 | square() |
| 2653 | get() |
| 2654 | |
| 2655 | >>> _TestClass(13).get() + _TestClass(-12).get() |
| 2656 | 1 |
| 2657 | >>> hex(_TestClass(13).square().get()) |
| 2658 | '0xa9' |
| 2659 | """ |
| 2660 | |
| 2661 | def __init__(self, val): |
| 2662 | """val -> _TestClass object with associated value val. |
| 2663 | |
| 2664 | >>> t = _TestClass(123) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2665 | >>> print(t.get()) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2666 | 123 |
| 2667 | """ |
| 2668 | |
| 2669 | self.val = val |
| 2670 | |
| 2671 | def square(self): |
| 2672 | """square() -> square TestClass's associated value |
| 2673 | |
| 2674 | >>> _TestClass(13).square().get() |
| 2675 | 169 |
| 2676 | """ |
| 2677 | |
| 2678 | self.val = self.val ** 2 |
| 2679 | return self |
| 2680 | |
| 2681 | def get(self): |
| 2682 | """get() -> return TestClass's associated value. |
| 2683 | |
| 2684 | >>> x = _TestClass(-42) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2685 | >>> print(x.get()) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2686 | -42 |
| 2687 | """ |
| 2688 | |
| 2689 | return self.val |
| 2690 | |
| 2691 | __test__ = {"_TestClass": _TestClass, |
| 2692 | "string": r""" |
| 2693 | Example of a string object, searched as-is. |
| 2694 | >>> x = 1; y = 2 |
| 2695 | >>> x + y, x * y |
| 2696 | (3, 2) |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 2697 | """, |
Tim Peters | 3fa8c20 | 2004-08-23 21:43:39 +0000 | [diff] [blame] | 2698 | |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 2699 | "bool-int equivalence": r""" |
| 2700 | In 2.2, boolean expressions displayed |
| 2701 | 0 or 1. By default, we still accept |
| 2702 | them. This can be disabled by passing |
| 2703 | DONT_ACCEPT_TRUE_FOR_1 to the new |
| 2704 | optionflags argument. |
| 2705 | >>> 4 == 4 |
| 2706 | 1 |
| 2707 | >>> 4 == 4 |
| 2708 | True |
| 2709 | >>> 4 > 4 |
| 2710 | 0 |
| 2711 | >>> 4 > 4 |
| 2712 | False |
| 2713 | """, |
Tim Peters | 3fa8c20 | 2004-08-23 21:43:39 +0000 | [diff] [blame] | 2714 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2715 | "blank lines": r""" |
Tim Peters | 3fa8c20 | 2004-08-23 21:43:39 +0000 | [diff] [blame] | 2716 | Blank lines can be marked with <BLANKLINE>: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2717 | >>> print('foo\n\nbar\n') |
Tim Peters | 3fa8c20 | 2004-08-23 21:43:39 +0000 | [diff] [blame] | 2718 | foo |
| 2719 | <BLANKLINE> |
| 2720 | bar |
| 2721 | <BLANKLINE> |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2722 | """, |
Tim Peters | 3fa8c20 | 2004-08-23 21:43:39 +0000 | [diff] [blame] | 2723 | |
| 2724 | "ellipsis": r""" |
| 2725 | If the ellipsis flag is used, then '...' can be used to |
| 2726 | elide substrings in the desired output: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 2727 | >>> print(list(range(1000))) #doctest: +ELLIPSIS |
Tim Peters | 3fa8c20 | 2004-08-23 21:43:39 +0000 | [diff] [blame] | 2728 | [0, 1, 2, ..., 999] |
| 2729 | """, |
| 2730 | |
| 2731 | "whitespace normalization": r""" |
| 2732 | If the whitespace normalization flag is used, then |
| 2733 | differences in whitespace are ignored. |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 2734 | >>> print(list(range(30))) #doctest: +NORMALIZE_WHITESPACE |
Tim Peters | 3fa8c20 | 2004-08-23 21:43:39 +0000 | [diff] [blame] | 2735 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 2736 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, |
| 2737 | 27, 28, 29] |
| 2738 | """, |
| 2739 | } |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2740 | |
R. David Murray | 445448c | 2009-12-20 17:28:31 +0000 | [diff] [blame] | 2741 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2742 | def _test(): |
Serhiy Storchaka | 7e4db2f | 2017-05-04 08:17:47 +0300 | [diff] [blame] | 2743 | import argparse |
| 2744 | |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2745 | parser = argparse.ArgumentParser(description="doctest runner") |
| 2746 | parser.add_argument('-v', '--verbose', action='store_true', default=False, |
| 2747 | help='print very verbose output for all tests') |
| 2748 | parser.add_argument('-o', '--option', action='append', |
| 2749 | choices=OPTIONFLAGS_BY_NAME.keys(), default=[], |
| 2750 | help=('specify a doctest option flag to apply' |
| 2751 | ' to the test run; may be specified more' |
| 2752 | ' than once to apply multiple options')) |
| 2753 | parser.add_argument('-f', '--fail-fast', action='store_true', |
| 2754 | help=('stop running tests after first failure (this' |
| 2755 | ' is a shorthand for -o FAIL_FAST, and is' |
| 2756 | ' in addition to any other -o options)')) |
| 2757 | parser.add_argument('file', nargs='+', |
| 2758 | help='file containing the tests to run') |
| 2759 | args = parser.parse_args() |
| 2760 | testfiles = args.file |
| 2761 | # Verbose used to be handled by the "inspect argv" magic in DocTestRunner, |
| 2762 | # but since we are using argparse we are passing it manually now. |
| 2763 | verbose = args.verbose |
| 2764 | options = 0 |
| 2765 | for option in args.option: |
| 2766 | options |= OPTIONFLAGS_BY_NAME[option] |
| 2767 | if args.fail_fast: |
| 2768 | options |= FAIL_FAST |
R. David Murray | 445448c | 2009-12-20 17:28:31 +0000 | [diff] [blame] | 2769 | for filename in testfiles: |
| 2770 | if filename.endswith(".py"): |
| 2771 | # It is a module -- insert its dir into sys.path and try to |
| 2772 | # import it. If it is part of a package, that possibly |
| 2773 | # won't work because of package imports. |
| 2774 | dirname, filename = os.path.split(filename) |
| 2775 | sys.path.insert(0, dirname) |
| 2776 | m = __import__(filename[:-3]) |
| 2777 | del sys.path[0] |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2778 | failures, _ = testmod(m, verbose=verbose, optionflags=options) |
R. David Murray | 445448c | 2009-12-20 17:28:31 +0000 | [diff] [blame] | 2779 | else: |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2780 | failures, _ = testfile(filename, module_relative=False, |
| 2781 | verbose=verbose, optionflags=options) |
R. David Murray | 445448c | 2009-12-20 17:28:31 +0000 | [diff] [blame] | 2782 | if failures: |
| 2783 | return 1 |
Christian Heimes | 895627f | 2007-12-08 17:28:33 +0000 | [diff] [blame] | 2784 | return 0 |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2785 | |
R. David Murray | 445448c | 2009-12-20 17:28:31 +0000 | [diff] [blame] | 2786 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2787 | if __name__ == "__main__": |
Christian Heimes | 895627f | 2007-12-08 17:28:33 +0000 | [diff] [blame] | 2788 | sys.exit(_test()) |