Django

Code

root/django/branches/0.95-bugfixes/tests/doctest.py

Revision 2687, 97.1 kB (checked in by adrian, 3 years ago)

Fixed #1626 -- Fixed a bunch of typos in comments and docs. Thanks, Dexter

Line 
1 # Module doctest.
2 # Released to the public domain 16-Jan-2001, by Tim Peters (tim@python.org).
3 # Major enhancements and refactoring by:
4 #     Jim Fulton
5 #     Edward Loper
6
7 # Provided as-is; use at your own risk; no warranty; no promises; enjoy!
8
9 r"""Module doctest -- a framework for running examples in docstrings.
10
11 In simplest use, end each module M to be tested with:
12
13 def _test():
14     import doctest
15     doctest.testmod()
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
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
39 examined by testmod.
40
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.
46 """
47
48 __docformat__ = 'reStructuredText en'
49
50 __all__ = [
51     # 0, Option Flags
52     'register_optionflag',
53     'DONT_ACCEPT_TRUE_FOR_1',
54     'DONT_ACCEPT_BLANKLINE',
55     'NORMALIZE_WHITESPACE',
56     'ELLIPSIS',
57     'IGNORE_EXCEPTION_DETAIL',
58     'COMPARISON_FLAGS',
59     'REPORT_UDIFF',
60     'REPORT_CDIFF',
61     'REPORT_NDIFF',
62     'REPORT_ONLY_FIRST_FAILURE',
63     'REPORTING_FLAGS',
64     # 1. Utility Functions
65     'is_private',
66     # 2. Example & DocTest
67     'Example',
68     'DocTest',
69     # 3. Doctest Parser
70     'DocTestParser',
71     # 4. Doctest Finder
72     'DocTestFinder',
73     # 5. Doctest Runner
74     'DocTestRunner',
75     'OutputChecker',
76     'DocTestFailure',
77     'UnexpectedException',
78     'DebugRunner',
79     # 6. Test Functions
80     'testmod',
81     'testfile',
82     'run_docstring_examples',
83     # 7. Tester
84     'Tester',
85     # 8. Unittest Support
86     'DocTestSuite',
87     'DocFileSuite',
88     'set_unittest_reportflags',
89     # 9. Debugging Support
90     'script_from_examples',
91     'testsource',
92     'debug_src',
93     'debug',
94 ]
95
96 import __future__
97
98 import sys, traceback, inspect, linecache, os, re, types
99 import unittest, difflib, pdb, tempfile
100 import warnings
101 from StringIO import StringIO
102
103 # Don't whine about the deprecated is_private function in this
104 # module's tests.
105 warnings.filterwarnings("ignore", "is_private", DeprecationWarning,
106                         __name__, 0)
107
108 # There are 4 basic classes:
109 #  - Example: a <source, want> pair, plus an intra-docstring line number.
110 #  - DocTest: a collection of examples, parsed from a docstring, plus
111 #    info about where the docstring came from (name, filename, lineno).
112 #  - DocTestFinder: extracts DocTests from a given object's docstring and
113 #    its contained objects' docstrings.
114 #  - DocTestRunner: runs DocTest cases, and accumulates statistics.
115 #
116 # So the basic picture is:
117 #
118 #                             list of:
119 # +------+                   +---------+                   +-------+
120 # |object| --DocTestFinder-> | DocTest | --DocTestRunner-> |results|
121 # +------+                   +---------+                   +-------+
122 #                            | Example |
123 #                            |   ...   |
124 #                            | Example |
125 #                            +---------+
126
127 # Option constants.
128
129 OPTIONFLAGS_BY_NAME = {}
130 def register_optionflag(name):
131     flag = 1 << len(OPTIONFLAGS_BY_NAME)
132     OPTIONFLAGS_BY_NAME[name] = flag
133     return flag
134
135 DONT_ACCEPT_TRUE_FOR_1 = register_optionflag('DONT_ACCEPT_TRUE_FOR_1')
136 DONT_ACCEPT_BLANKLINE = register_optionflag('DONT_ACCEPT_BLANKLINE')
137 NORMALIZE_WHITESPACE = register_optionflag('NORMALIZE_WHITESPACE')
138 ELLIPSIS = register_optionflag('ELLIPSIS')
139 IGNORE_EXCEPTION_DETAIL = register_optionflag('IGNORE_EXCEPTION_DETAIL')
140
141 COMPARISON_FLAGS = (DONT_ACCEPT_TRUE_FOR_1 |
142                     DONT_ACCEPT_BLANKLINE |
143                     NORMALIZE_WHITESPACE |
144                     ELLIPSIS |
145                     IGNORE_EXCEPTION_DETAIL)
146
147 REPORT_UDIFF = register_optionflag('REPORT_UDIFF')
148 REPORT_CDIFF = register_optionflag('REPORT_CDIFF')
149 REPORT_NDIFF = register_optionflag('REPORT_NDIFF')
150 REPORT_ONLY_FIRST_FAILURE = register_optionflag('REPORT_ONLY_FIRST_FAILURE')
151
152 REPORTING_FLAGS = (REPORT_UDIFF |
153                    REPORT_CDIFF |
154                    REPORT_NDIFF |
155                    REPORT_ONLY_FIRST_FAILURE)
156
157 # Special string markers for use in `want` strings:
158 BLANKLINE_MARKER = '<BLANKLINE>'
159 ELLIPSIS_MARKER = '...'
160
161 ######################################################################
162 ## Table of Contents
163 ######################################################################
164 #  1. Utility Functions
165 #  2. Example & DocTest -- store test cases
166 #  3. DocTest Parser -- extracts examples from strings
167 #  4. DocTest Finder -- extracts test cases from objects
168 #  5. DocTest Runner -- runs test cases
169 #  6. Test Functions -- convenient wrappers for testing
170 #  7. Tester Class -- for backwards compatibility
171 #  8. Unittest Support
172 #  9. Debugging Support
173 # 10. Example Usage
174
175 ######################################################################
176 ## 1. Utility Functions
177 ######################################################################
178
179 def is_private(prefix, base):
180     """prefix, base -> true iff name prefix + "." + base is "private".
181
182     Prefix may be an empty string, and base does not contain a period.
183     Prefix is ignored (although functions you write conforming to this
184     protocol may make use of it).
185     Return true iff base begins with an (at least one) underscore, but
186     does not both begin and end with (at least) two underscores.
187
188     >>> is_private("a.b", "my_func")
189     False
190     >>> is_private("____", "_my_func")
191     True
192     >>> is_private("someclass", "__init__")
193     False
194     >>> is_private("sometypo", "__init_")
195     True
196     >>> is_private("x.y.z", "_")
197     True
198     >>> is_private("_x.y.z", "__")
199     False
200     >>> is_private("", "")  # senseless but consistent
201     False
202     """
203     warnings.warn("is_private is deprecated; it wasn't useful; "
204                   "examine DocTestFinder.find() lists instead",
205                   DeprecationWarning, stacklevel=2)
206     return base[:1] == "_" and not base[:2] == "__" == base[-2:]
207
208 def _extract_future_flags(globs):
209     """
210     Return the compiler-flags associated with the future features that
211     have been imported into the given namespace (globs).
212     """
213     flags = 0
214     for fname in __future__.all_feature_names:
215         feature = globs.get(fname, None)
216         if feature is getattr(__future__, fname):
217             flags |= feature.compiler_flag
218     return flags
219
220 def _normalize_module(module, depth=2):
221     """
222     Return the module specified by `module`.  In particular:
223       - If `module` is a module, then return module.
224       - If `module` is a string, then import and return the
225         module with that name.
226       - If `module` is None, then return the calling module.
227         The calling module is assumed to be the module of
228         the stack frame at the given depth in the call stack.
229     """
230     if inspect.ismodule(module):
231         return module
232     elif isinstance(module, (str, unicode)):
233         return __import__(module, globals(), locals(), ["*"])
234     elif module is None:
235         return sys.modules[sys._getframe(depth).f_globals['__name__']]
236     else:
237         raise TypeError("Expected a module, string, or None")
238
239 def _indent(s, indent=4):
240     """
241     Add the given number of space characters to the beginning every
242     non-blank line in `s`, and return the result.
243     """
244     # This regexp matches the start of non-blank lines:
245     return re.sub('(?m)^(?!$)', indent*' ', s)
246
247 def _exception_traceback(exc_info):
248     """
249     Return a string containing a traceback message for the given
250     exc_info tuple (as returned by sys.exc_info()).
251     """
252     # Get a traceback message.
253     excout = StringIO()
254     exc_type, exc_val, exc_tb = exc_info
255     traceback.print_exception(exc_type, exc_val, exc_tb, file=excout)
256     return excout.getvalue()
257
258 # Override some StringIO methods.
259 class _SpoofOut(StringIO):
260     def getvalue(self):
261         result = StringIO.getvalue(self)
262         # If anything at all was written, make sure there's a trailing
263         # newline.  There's no way for the expected output to indicate
264         # that a trailing newline is missing.
265         if result and not result.endswith("\n"):
266             result += "\n"
267         # Prevent softspace from screwing up the next test case, in
268         # case they used print with a trailing comma in an example.
269         if hasattr(self, "softspace"):
270             del self.softspace
271         return result
272
273     def truncate(self,   size=None):
274         StringIO.truncate(self, size)
275         if hasattr(self, "softspace"):
276             del self.softspace
277
278 # Worst-case linear-time ellipsis matching.
279 def _ellipsis_match(want, got):
280     """
281     Essentially the only subtle case:
282     >>> _ellipsis_match('aa...aa', 'aaa')
283     False
284     """
285     if ELLIPSIS_MARKER not in want:
286         return want == got
287
288     # Find "the real" strings.
289     ws = want.split(ELLIPSIS_MARKER)
290     assert len(ws) >= 2
291
292     # Deal with exact matches possibly needed at one or both ends.
293     startpos, endpos = 0, len(got)
294     w = ws[0]
295     if w:   # starts with exact match
296         if got.startswith(w):
297             startpos = len(w)
298             del ws[0]
299         else:
300             return False
301     w = ws[-1]
302     if w:   # ends with exact match
303         if got.endswith(w):
304             endpos -= len(w)
305             del ws[-1]
306         else:
307             return False
308
309     if startpos > endpos:
310         # Exact end matches required more characters than we have, as in
311         # _ellipsis_match('aa...aa', 'aaa')
312         return False
313
314     # For the rest, we only need to find the leftmost non-overlapping
315     # match for each piece.  If there's no overall match that way alone,
316     # there's no overall match period.
317     for w in ws:
318         # w may be '' at times, if there are consecutive ellipses, or
319         # due to an ellipsis at the start or end of `want`.  That's OK.
320         # Search for an empty string succeeds, and doesn't change startpos.
321         startpos = got.find(w, startpos, endpos)
322         if startpos < 0:
323             return False
324         startpos += len(w)
325
326     return True
327
328 def _comment_line(line):
329     "Return a commented form of the given line"
330     line = line.rstrip()
331     if line:
332         return '# '+line
333     else:
334         return '#'
335
336 class _OutputRedirectingPdb(pdb.Pdb):
337     """
338     A specialized version of the python debugger that redirects stdout
339     to a given stream when interacting with the user.  Stdout is *not*
340     redirected when traced code is executed.
341     """
342     def __init__(self, out):
343         self.__out = out
344         pdb.Pdb.__init__(self)
345
346     def trace_dispatch(self, *args):
347         # Redirect stdout to the given stream.
348         save_stdout = sys.stdout
349         sys.stdout = self.__out
350         # Call Pdb's trace dispatch method.
351         try:
352             return pdb.Pdb.trace_dispatch(self, *args)
353         finally:
354             sys.stdout = save_stdout
355
356 # [XX] Normalize with respect to os.path.pardir?
357 def _module_relative_path(module, path):
358     if not inspect.ismodule(module):
359         raise TypeError, 'Expected a module: %r' % module
360     if path.startswith('/'):
361         raise ValueError, 'Module-relative files may not have absolute paths'
362
363     # Find the base directory for the path.
364     if hasattr(module, '__file__'):
365         # A normal module/package
366         basedir = os.path.split(module.__file__)[0]
367     elif module.__name__ == '__main__':
368         # An interactive session.
369         if len(sys.argv)>0 and sys.argv[0] != '':
370             basedir = os.path.split(sys.argv[0])[0]
371         else:
372             basedir = os.curdir
373     else:
374         # A module w/o __file__ (this includes builtins)
375         raise ValueError("Can't resolve paths relative to the module " +
376                          module + " (it has no __file__)")
377
378     # Combine the base directory and the path.
379     return os.path.join(basedir, *(path.split('/')))
380
381 ######################################################################
382 ## 2. Example & DocTest
383 ######################################################################
384 ## - An "example" is a <source, want> pair, where "source" is a
385 ##   fragment of source code, and "want" is the expected output for
386 ##   "source."  The Example class also includes information about
387 ##   where the example was extracted from.
388 ##
389 ## - A "doctest" is a collection of examples, typically extracted from
390 ##   a string (such as an object's docstring).  The DocTest class also
391 ##   includes information about where the string was extracted from.
392
393 class Example:
394     """
395     A single doctest example, consisting of source code and expected
396     output.  `Example` defines the following attributes:
397
398       - source: A single Python statement, always ending with a newline.
399         The constructor adds a newline if needed.
400
401       - want: The expected output from running the source code (either
402         from stdout, or a traceback in case of exception).  `want` ends
403         with a newline unless it's empty, in which case it's an empty
404         string.  The constructor adds a newline if needed.
405
406       - exc_msg: The exception message generated by the example, if
407         the example is expected to generate an exception; or `None` if
408         it is not expected to generate an exception.  This exception
409         message is compared against the return value of
410         `traceback.format_exception_only()`.  `exc_msg` ends with a
411         newline unless it's `None`.  The constructor adds a newline
412         if needed.
413
414       - lineno: The line number within the DocTest string containing
415         this Example where the Example begins.  This line number is
416         zero-based, with respect to the beginning of the DocTest.
417
418       - indent: The example's indentation in the DocTest string.
419         I.e., the number of space characters that preceed the
420         example's first prompt.
421
422       - options: A dictionary mapping from option flags to True or
423         False, which is used to override default options for this
424         example.  Any option flags not contained in this dictionary
425         are left at their default value (as specified by the
426         DocTestRunner's optionflags).  By default, no options are set.
427     """
428     def __init__(self, source, want, exc_msg=None, lineno=0, indent=0,
429                  options=None):
430         # Normalize inputs.
431         if not source.endswith('\n'):
432             source += '\n'
433         if want and not want.endswith('\n'):
434             want += '\n'
435         if exc_msg is not None and not exc_msg.endswith('\n'):
436             exc_msg += '\n'
437         # Store properties.
438         self.source = source
439         self.want = want
440         self.lineno = lineno
441         self.indent = indent
442         if options is None: options = {}
443         self.options = options
444         self.exc_msg = exc_msg
445
446 class DocTest:
447     """
448     A collection of doctest examples that should be run in a single
449     namespace.  Each `DocTest` defines the following attributes:
450
451       - examples: the list of examples.
452
453       - globs: The namespace (aka globals) that the examples should
454         be run in.
455
456       - name: A name identifying the DocTest (typically, the name of
457         the object whose docstring this DocTest was extracted from).
458
459       - filename: The name of the file that this DocTest was extracted
460         from, or `None` if the filename is unknown.
461
462       - lineno: The line number within filename where this DocTest
463         begins, or `None` if the line number is unavailable.  This
464         line number is zero-based, with respect to the beginning of
465         the file.
466
467       - docstring: The string that the examples were extracted from,
468         or `None` if the string is unavailable.
469     """
470     def __init__(self, examples, globs, name, filename, lineno, docstring):
471         """
472         Create a new DocTest containing the given examples.  The
473         DocTest's globals are initialized with a copy of `globs`.
474         """
475         assert not isinstance(examples, basestring), \
476                "DocTest no longer accepts str; use DocTestParser instead"
477         self.examples = examples
478         self.docstring = docstring
479         self.globs = globs.copy()
480         self.name = name
481         self.filename = filename
482         self.lineno = lineno
483
484     def __repr__(self):
485         if len(self.examples) == 0:
486             examples = 'no examples'
487         elif len(self.examples) == 1:
488             examples = '1 example'
489         else:
490             examples = '%d examples' % len(self.examples)
491         return ('<DocTest %s from %s:%s (%s)>' %
492                 (self.name, self.filename, self.lineno, examples))
493
494
495     # This lets us sort tests by name:
496     def __cmp__(self, other):
497         if not isinstance(other, DocTest):
498             return -1
499         return cmp((self.name, self.filename, self.lineno, id(self)),
500                    (other.name, other.filename, other.lineno, id(other)))
501
502 ######################################################################
503 ## 3. DocTestParser
504 ######################################################################
505
506 class DocTestParser:
507     """
508     A class used to parse strings containing doctest examples.
509     """
510     # This regular expression is used to find doctest examples in a
511     # string.  It defines three groups: `source` is the source code
512     # (including leading indentation and prompts); `indent` is the
513     # indentation of the first (PS1) line of the source code; and
514     # `want` is the expected output (including leading indentation).
515     _EXAMPLE_RE = re.compile(r'''
516         # Source consists of a PS1 line followed by zero or more PS2 lines.
517         (?P<source>
518             (?:^(?P<indent> [ ]*) >>>    .*)    # PS1 line
519             (?:\n           [ ]*  \.\.\. .*)*)  # PS2 lines
520         \n?
521         # Want consists of any non-blank lines that do not start with PS1.
522         (?P<want> (?:(?![ ]*$)    # Not a blank line
523                      (?![ ]*>>>)  # Not a line starting with PS1
524                      .*$\n?       # But any other line
525                   )*)
526         ''', re.MULTILINE | re.VERBOSE)
527
528     # A regular expression for handling `want` strings that contain
529     # expected exceptions.  It divides `want` into three pieces:
530     #    - the traceback header line (`hdr`)
531     #    - the traceback stack (`stack`)
532     #    - the exception message (`msg`), as generated by
533     #      traceback.format_exception_only()
534     # `msg` may have multiple lines.  We assume/require that the
535     # exception message is the first non-indented line starting with a word
536     # character following the traceback header line.
537     _EXCEPTION_RE = re.compile(r"""
538         # Grab the traceback header.  Different versions of Python have
539         # said different things on the first traceback line.
540         ^(?P<hdr> Traceback\ \(
541             (?: most\ recent\ call\ last
542             |   innermost\ last
543             ) \) :
544         )
545         \s* $                # toss trailing whitespace on the header.
546         (?P<stack> .*?)      # don't blink: absorb stuff until...
547         ^ (?P<msg> \w+ .*)   #     a line *starts* with alphanum.
548         """, re.VERBOSE | re.MULTILINE | re.DOTALL)
549
550     # A callable returning a true value iff its argument is a blank line
551     # or contains a single comment.
552     _IS_BLANK_OR_COMMENT = re.compile(r'^[ ]*(#.*)?$').match
553
554     def parse(self, string, name='<string>'):
555         """
556         Divide the given string into examples and intervening text,
557         and return them as a list of alternating Examples and strings.
558         Line numbers for the Examples are 0-based.  The optional
559         argument `name` is a name identifying this string, and is only
560         used for error messages.
561         """
562         string = string.expandtabs()
563         # If all lines begin with the same indentation, then strip it.
564         min_indent = self._min_indent(string)
565         if min_indent > 0:
566             string = '\n'.join([l[min_indent:] for l in string.split('\n')])
567
568         output = []
569         charno, lineno = 0, 0
570         # Find all doctest examples in the string:
571         for m in self._EXAMPLE_RE.finditer(string):
572             # Add the pre-example text to `output`.
573             output.append(string[charno:m.start()])
574             # Update lineno (lines before this example)
575             lineno += string.count('\n', charno, m.start())
576             # Extract info from the regexp match.
577             (source, options, want, exc_msg) = \
578                      self._parse_example(m, name, lineno)
579             # Create an Example, and add it to the list.
580             if not self._IS_BLANK_OR_COMMENT(source):
581                 output.append( Example(source, want, exc_msg,
582                                     lineno=lineno,
583                                     indent=min_indent+len(m.group('indent')),
584                                     options=options) )
585             # Update lineno (lines inside this example)
586             lineno += string.count('\n', m.start(), m.end())
587             # Update charno.
588             charno = m.end()
589         # Add any remaining post-example text to `output`.
590         output.append(string[charno:])
591         return output
592
593     def get_doctest(self, string, globs, name, filename, lineno):
594         """
595         Extract all doctest examples from the given string, and
596         collect them into a `DocTest` object.
597
598         `globs`, `name`, `filename`, and `lineno` are attributes for
599         the new `DocTest` object.  See the documentation for `DocTest`
600         for more information.
601         """
602         return DocTest(self.get_examples(string, name), globs,
603                        name, filename, lineno, string)
604
605     def get_examples(self, string, name='<string>'):
606         """
607         Extract all doctest examples from the given string, and return
608         them as a list of `Example` objects.  Line numbers are
609         0-based, because it's most common in doctests that nothing
610         interesting appears on the same line as opening triple-quote,
611         and so the first interesting line is called \"line 1\" then.
612
613         The optional argument `name` is a name identifying this
614         string, and is only used for error messages.
615         """
616         return [x for x in self.parse(string, name)
617                 if isinstance(x, Example)]
618
619     def _parse_example(self, m, name, lineno):
620         """
621         Given a regular expression match from `_EXAMPLE_RE` (`m`),
622         return a pair `(source, want)`, where `source` is the matched
623         example's source code (with prompts and indentation stripped);
624         and `want` is the example's expected output (with indentation
625         stripped).
626
627         `name` is the string's name, and `lineno` is the line number
628         where the example starts; both are used for error messages.
629         """
630         # Get the example's indentation level.
631         indent = len(m.group('indent'))
632
633         # Divide source into lines; check that they're properly
634         # indented; and then strip their indentation & prompts.
635         source_lines = m.group('source').split('\n')
636         self._check_prompt_blank(source_lines, indent, name, lineno)
637         self._check_prefix(source_lines[1:], ' '*indent + '.', name, lineno)
638         source = '\n'.join([sl[indent+4:] for sl in source_lines])
639
640         # Divide want into lines; check that it's properly indented; and
641         # then strip the indentation.  Spaces before the last newline should
642         # be preserved, so plain rstrip() isn't good enough.
643         want = m.group('want')
644         want_lines = want.split('\n')
645         if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]):
646             del want_lines[-1]  # forget final newline & spaces after it
647         self._check_prefix(want_lines, ' '*