--- doctest.orig Sat Apr 10 13:46:44 2004 +++ doctest.py Sat Apr 10 14:14:53 2004 @@ -308,6 +308,7 @@ # Option constants. DONT_ACCEPT_TRUE_FOR_1 = 1 << 0 +END_WITH_DEDENT = 1 << 1 # Extract interactive examples from a string. Return a list of triples, # (source, outcome, lineno). "source" is the source code, and ends @@ -316,7 +317,7 @@ # outcome always ends with a newline. "lineno" is the line number, # 0-based wrt the start of the string, of the first source line. -def _extract_examples(s): +def _extract_examples(s, optionflags=0): isPS1, isPS2 = _isPS1, _isPS2 isEmpty, isComment = _isEmpty, _isComment examples = [] @@ -364,16 +365,25 @@ expect = "" else: expect = [] + end_with_dedent = ((optionflags & END_WITH_DEDENT) and + len(blanks)>0) while 1: if line[:nblanks] != blanks: - raise ValueError("inconsistent leading whitespace " - "in line %r of docstring: %s" % (i, line)) + if end_with_dedent: + # Only count it as a dedent if it's not a blank + # line. + if not isEmpty(line): + break + else: + raise ValueError("inconsistent leading whitespace " + "in line %r of docstring: %s" % (i, line)) expect.append(line[nblanks:]) i = i + 1 + if i >= n: break # End of docstring. line = lines[i] - if isPS1(line) or isEmpty(line): + if isPS1(line) or (isEmpty(line) and not end_with_dedent): break - expect = "\n".join(expect) + "\n" + expect = "\n".join(expect).rstrip() + "\n" examples.append( (source, expect, lineno) ) return examples @@ -546,7 +556,7 @@ except: return 0, 0 - e = _extract_examples(doc) + e = _extract_examples(doc, optionflags) if not e: return 0, 0 if compileflags is None: @@ -736,7 +746,7 @@ if self.verbose: print "Running string", name f = t = 0 - e = _extract_examples(s) + e = _extract_examples(s, self.optionflags) if e: f, t = _run_examples(e, self.globs, self.verbose, name, self.compileflags, self.optionflags) @@ -1118,6 +1128,17 @@ DONT_ACCEPT_TRUE_FOR_1 is specified, neither substitution is allowed. + END_WITH_DEDENT + By default, the end of each docstest example is marked by + a blank line. This means that the output may not contain + blank lines. When END_WITH_DEDENT is specified, the end + of each indented doctest example is marked by a dedent + past its original prompt (>>>). Thus, blank lines may + appear within the output. However, trailing blank lines + are still disallowed. Unindented docstring examples will + continue to be termined by a blank line (since dedenting + past the prompt is impossible). + Advanced tomfoolery: testmod runs methods of a local instance of class doctest.Tester, then merges the results into (or creates) global Tester instance doctest.master. Methods of doctest.master @@ -1370,7 +1391,7 @@ expect = "\n# Expect:\n# %s" % expect return expect -def testsource(module, name): +def testsource(module, name, optionflags=0): """Extract the doctest examples from a docstring. Provide the module (or dotted name of the module) containing the @@ -1390,7 +1411,8 @@ raise ValueError(name, "not found in tests") test = test[0] examples = [source + _expect(expect) - for source, expect, dummy in _extract_examples(test)] + for source, expect, dummy in + _extract_examples(test, optionflags)] return '\n'.join(examples) def debug(module, name):