1- # Module doctest version 0.9.7
1+ # Module doctest.
22# Released to the public domain 16-Jan-2001,
33# by Tim Peters ([email protected] ). 44
@@ -294,70 +294,14 @@ def _test():
294294Test passed.
295295"""
296296
297- # 0,0,1 06-Mar-1999
298- # initial version posted
299- # 0,0,2 06-Mar-1999
300- # loosened parsing:
301- # cater to stinkin' tabs
302- # don't insist on a blank after PS2 prefix
303- # so trailing "... " line from a compound stmt no longer
304- # breaks if the file gets whitespace-trimmed
305- # better error msgs for inconsistent leading whitespace
306- # 0,9,1 08-Mar-1999
307- # exposed the Tester class and added client methods
308- # plus docstring examples of their use (eww - head-twisting!)
309- # fixed logic error in reporting total # of tests & failures
310- # added __test__ support to testmod (a pale reflection of Christian
311- # Tismer's vision ...)
312- # removed the "deep" argument; fiddle __test__ instead
313- # simplified endcase logic for extracting tests, and running them.
314- # before, if no output was expected but some was produced
315- # anyway via an eval'ed result, the discrepancy wasn't caught
316- # made TestClass private and used __test__ to get at it
317- # many doc updates
318- # speed _SpoofOut for long expected outputs
319- # 0,9,2 09-Mar-1999
320- # throw out comments from examples, enabling use of the much simpler
321- # exec compile(... "single") ...
322- # for simulating the runtime; that barfs on comment-only lines
323- # used the traceback module to do a much better job of reporting
324- # exceptions
325- # run __doc__ values thru str(), "just in case"
326- # privateness of names now determined by an overridable "isprivate"
327- # function
328- # by default a name now considered to be private iff it begins with
329- # an underscore but doesn't both begin & end with two of 'em; so
330- # e.g. Class.__init__ etc are searched now -- as they always
331- # should have been
332- # 0,9,3 18-Mar-1999
333- # added .flush stub to _SpoofOut (JPython buglet diagnosed by
334- # Hugh Emberson)
335- # repaired ridiculous docs about backslashes in examples
336- # minor internal changes
337- # changed source to Unix line-end conventions
338- # moved __test__ logic into new Tester.run__test__ method
339- # 0,9,4 27-Mar-1999
340- # report item name and line # in failing examples
341- # 0,9,5 29-Jun-1999
342- # allow straightforward exceptions in examples - thanks to Mark Hammond!
343- # 0,9,6 16-Jan-2001
344- # fiddling for changes in Python 2.0: some of the embedded docstring
345- # examples no longer worked *exactly* as advertised, due to minor
346- # language changes, and running doctest on itself pointed that out.
347- # Hard to think of a better example of why this is useful <wink>.
348- # 0,9,7 9-Feb-2001
349- # string method conversion
350-
351- # XXX Until generators are part of the language, examples in doctest'ed
352- # modules will inherit doctest's __future__ settings (see PEP 236 for
353- # more on that). In the absence of a better working idea, the std
354- # test suite needs generators, while the set of doctest'ed modules that
355- # don't use "yield" in a generator context may well be empty. So
356- # enable generators here. This can go away when generators are no
357- # longer optional.
358- from __future__ import generators
359-
360- __version__ = 0 , 9 , 7
297+ __all__ = [
298+ 'testmod' ,
299+ 'run_docstring_examples' ,
300+ 'is_private' ,
301+ 'Tester' ,
302+ ]
303+
304+ import __future__
361305
362306import types
363307_FunctionType = types .FunctionType
@@ -375,8 +319,6 @@ def _test():
375319_isComment = re .compile (r"\s*#" ).match
376320del re
377321
378- __all__ = []
379-
380322# Extract interactive examples from a string. Return a list of triples,
381323# (source, outcome, lineno). "source" is the source code, and ends
382324# with a newline iff the source spans more than one line. "outcome" is
@@ -487,7 +429,8 @@ def _tag_out(printer, *tag_msg_pairs):
487429# stuff to "the real" stdout, and fakeout is an instance of _SpoofOut
488430# that captures the examples' std output. Return (#failures, #tries).
489431
490- def _run_examples_inner (out , fakeout , examples , globs , verbose , name ):
432+ def _run_examples_inner (out , fakeout , examples , globs , verbose , name ,
433+ compileflags ):
491434 import sys , traceback
492435 OK , BOOM , FAIL = range (3 )
493436 NADA = "nothing"
@@ -499,7 +442,8 @@ def _run_examples_inner(out, fakeout, examples, globs, verbose, name):
499442 ("Expecting" , want or NADA ))
500443 fakeout .clear ()
501444 try :
502- exec compile (source , "<string>" , "single" ) in globs
445+ exec compile (source , "<string>" , "single" ,
446+ compileflags , 1 ) in globs
503447 got = fakeout .get ()
504448 state = OK
505449 except :
@@ -538,17 +482,28 @@ def _run_examples_inner(out, fakeout, examples, globs, verbose, name):
538482
539483 return failures , len (examples )
540484
485+ # Get the future-flags associated with the future features that have been
486+ # imported into globs.
487+
488+ def _extract_future_flags (globs ):
489+ flags = 0
490+ for fname in __future__ .all_feature_names :
491+ feature = globs .get (fname , None )
492+ if feature is getattr (__future__ , fname ):
493+ flags |= feature .compiler_flag
494+ return flags
495+
541496# Run list of examples, in a shallow copy of context (dict) globs.
542497# Return (#failures, #tries).
543498
544- def _run_examples (examples , globs , verbose , name ):
499+ def _run_examples (examples , globs , verbose , name , compileflags ):
545500 import sys
546501 saveout = sys .stdout
547502 globs = globs .copy ()
548503 try :
549504 sys .stdout = fakeout = _SpoofOut ()
550505 x = _run_examples_inner (saveout .write , fakeout , examples ,
551- globs , verbose , name )
506+ globs , verbose , name , compileflags )
552507 finally :
553508 sys .stdout = saveout
554509 # While Python gc can clean up most cycles on its own, it doesn't
@@ -562,7 +517,8 @@ def _run_examples(examples, globs, verbose, name):
562517 globs .clear ()
563518 return x
564519
565- def run_docstring_examples (f , globs , verbose = 0 , name = "NoName" ):
520+ def run_docstring_examples (f , globs , verbose = 0 , name = "NoName" ,
521+ compileflags = None ):
566522 """f, globs, verbose=0, name="NoName" -> run examples from f.__doc__.
567523
568524 Use (a shallow copy of) dict globs as the globals for execution.
@@ -587,7 +543,9 @@ def run_docstring_examples(f, globs, verbose=0, name="NoName"):
587543 e = _extract_examples (doc )
588544 if not e :
589545 return 0 , 0
590- return _run_examples (e , globs , verbose , name )
546+ if compileflags is None :
547+ compileflags = _extract_future_flags (globs )
548+ return _run_examples (e , globs , verbose , name , compileflags )
591549
592550def is_private (prefix , base ):
593551 """prefix, base -> true iff name prefix + "." + base is "private".
@@ -724,6 +682,8 @@ def __init__(self, mod=None, globs=None, verbose=None,
724682
725683 self .name2ft = {} # map name to (#failures, #trials) pair
726684
685+ self .compileflags = _extract_future_flags (globs )
686+
727687 def runstring (self , s , name ):
728688 """
729689 s, name -> search string s for examples to run, logging as name.
@@ -755,7 +715,8 @@ def runstring(self, s, name):
755715 f = t = 0
756716 e = _extract_examples (s )
757717 if e :
758- f , t = _run_examples (e , self .globs , self .verbose , name )
718+ f , t = _run_examples (e , self .globs , self .verbose , name ,
719+ self .compileflags )
759720 if self .verbose :
760721 print f , "of" , t , "examples failed in string" , name
761722 self .__record_outcome (name , f , t )
@@ -793,7 +754,8 @@ def rundoc(self, object, name=None):
793754 "when object.__name__ doesn't exist; " + `object` )
794755 if self .verbose :
795756 print "Running" , name + ".__doc__"
796- f , t = run_docstring_examples (object , self .globs , self .verbose , name )
757+ f , t = run_docstring_examples (object , self .globs , self .verbose , name ,
758+ self .compileflags )
797759 if self .verbose :
798760 print f , "of" , t , "examples failed in" , name + ".__doc__"
799761 self .__record_outcome (name , f , t )
0 commit comments