[Python-checkins] CVS: python/dist/src/Lib doctest.py,1.16,1.17

Tim Peters tim_one@users.sourceforge.net
2001年8月17日 17:05:56 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv2375/python/Lib
Modified Files:
	doctest.py 
Log Message:
Remove the horrid generators hack from doctest.py. This relies on a
somewhat less horrid hack <wink>: if a module does
 from __future__ import X
then the module dict D is left in a state such that (viewing X as a
string)
 D[X] is getattr(__future__, X)
So by examining D for all the names of future features, and making that
test for each, we can make a darned good guess as to which future-features
were imported by the module. The appropriate flags are then sucked out
of the __future__ module, and passed on to compile()'s new optional 
arguments (PEP 264).
Also gave doctest a meaningful __all__, removed the history of changes
(CVS serves that purpose now), and removed the __version__ vrbl (similarly;
before CVS, it was a reasonable clue, but not anymore).
Index: doctest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/doctest.py,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** doctest.py	2001年07月16日 18:39:58	1.16
--- doctest.py	2001年08月18日 00:05:50	1.17
***************
*** 1,3 ****
! # Module doctest version 0.9.7
 # Released to the public domain 16-Jan-2001,
 # by Tim Peters (tim.one@home.com).
--- 1,3 ----
! # Module doctest.
 # Released to the public domain 16-Jan-2001,
 # by Tim Peters (tim.one@home.com).
***************
*** 294,362 ****
 Test passed.
 """
- 
- # 0,0,1 06-Mar-1999
- # initial version posted
- # 0,0,2 06-Mar-1999
- # loosened parsing:
- # cater to stinkin' tabs
- # don't insist on a blank after PS2 prefix
- # so trailing "... " line from a compound stmt no longer
- # breaks if the file gets whitespace-trimmed
- # better error msgs for inconsistent leading whitespace
- # 0,9,1 08-Mar-1999
- # exposed the Tester class and added client methods
- # plus docstring examples of their use (eww - head-twisting!)
- # fixed logic error in reporting total # of tests & failures
- # added __test__ support to testmod (a pale reflection of Christian
- # Tismer's vision ...)
- # removed the "deep" argument; fiddle __test__ instead
- # simplified endcase logic for extracting tests, and running them.
- # before, if no output was expected but some was produced
- # anyway via an eval'ed result, the discrepancy wasn't caught
- # made TestClass private and used __test__ to get at it
- # many doc updates
- # speed _SpoofOut for long expected outputs
- # 0,9,2 09-Mar-1999
- # throw out comments from examples, enabling use of the much simpler
- # exec compile(... "single") ...
- # for simulating the runtime; that barfs on comment-only lines
- # used the traceback module to do a much better job of reporting
- # exceptions
- # run __doc__ values thru str(), "just in case"
- # privateness of names now determined by an overridable "isprivate"
- # function
- # by default a name now considered to be private iff it begins with
- # an underscore but doesn't both begin & end with two of 'em; so
- # e.g. Class.__init__ etc are searched now -- as they always
- # should have been
- # 0,9,3 18-Mar-1999
- # added .flush stub to _SpoofOut (JPython buglet diagnosed by
- # Hugh Emberson)
- # repaired ridiculous docs about backslashes in examples
- # minor internal changes
- # changed source to Unix line-end conventions
- # moved __test__ logic into new Tester.run__test__ method
- # 0,9,4 27-Mar-1999
- # report item name and line # in failing examples
- # 0,9,5 29-Jun-1999
- # allow straightforward exceptions in examples - thanks to Mark Hammond!
- # 0,9,6 16-Jan-2001
- # fiddling for changes in Python 2.0: some of the embedded docstring
- # examples no longer worked *exactly* as advertised, due to minor
- # language changes, and running doctest on itself pointed that out.
- # Hard to think of a better example of why this is useful <wink>.
- # 0,9,7 9-Feb-2001
- # string method conversion
 
! # XXX Until generators are part of the language, examples in doctest'ed
! # modules will inherit doctest's __future__ settings (see PEP 236 for
! # more on that). In the absence of a better working idea, the std
! # test suite needs generators, while the set of doctest'ed modules that
! # don't use "yield" in a generator context may well be empty. So
! # enable generators here. This can go away when generators are no
! # longer optional.
! from __future__ import generators
 
! __version__ = 0, 9, 7
 
 import types
--- 294,306 ----
 Test passed.
 """
 
! __all__ = [
! 'testmod',
! 'run_docstring_examples',
! 'is_private',
! 'Tester',
! ]
 
! import __future__
 
 import types
***************
*** 376,381 ****
 del re
 
- __all__ = []
- 
 # Extract interactive examples from a string. Return a list of triples,
 # (source, outcome, lineno). "source" is the source code, and ends
--- 320,323 ----
***************
*** 488,492 ****
 # that captures the examples' std output. Return (#failures, #tries).
 
! def _run_examples_inner(out, fakeout, examples, globs, verbose, name):
 import sys, traceback
 OK, BOOM, FAIL = range(3)
--- 430,435 ----
 # that captures the examples' std output. Return (#failures, #tries).
 
! def _run_examples_inner(out, fakeout, examples, globs, verbose, name,
! compileflags):
 import sys, traceback
 OK, BOOM, FAIL = range(3)
***************
*** 500,504 ****
 fakeout.clear()
 try:
! exec compile(source, "<string>", "single") in globs
 got = fakeout.get()
 state = OK
--- 443,448 ----
 fakeout.clear()
 try:
! exec compile(source, "<string>", "single",
! compileflags, 1) in globs
 got = fakeout.get()
 state = OK
***************
*** 539,546 ****
 return failures, len(examples)
 
 # Run list of examples, in a shallow copy of context (dict) globs.
 # Return (#failures, #tries).
 
! def _run_examples(examples, globs, verbose, name):
 import sys
 saveout = sys.stdout
--- 483,501 ----
 return failures, len(examples)
 
+ # Get the future-flags associated with the future features that have been
+ # imported into globs.
+ 
+ def _extract_future_flags(globs):
+ flags = 0
+ for fname in __future__.all_feature_names:
+ feature = globs.get(fname, None)
+ if feature is getattr(__future__, fname):
+ flags |= feature.compiler_flag
+ return flags
+ 
 # Run list of examples, in a shallow copy of context (dict) globs.
 # Return (#failures, #tries).
 
! def _run_examples(examples, globs, verbose, name, compileflags):
 import sys
 saveout = sys.stdout
***************
*** 549,553 ****
 sys.stdout = fakeout = _SpoofOut()
 x = _run_examples_inner(saveout.write, fakeout, examples,
! globs, verbose, name)
 finally:
 sys.stdout = saveout
--- 504,508 ----
 sys.stdout = fakeout = _SpoofOut()
 x = _run_examples_inner(saveout.write, fakeout, examples,
! globs, verbose, name, compileflags)
 finally:
 sys.stdout = saveout
***************
*** 563,567 ****
 return x
 
! def run_docstring_examples(f, globs, verbose=0, name="NoName"):
 """f, globs, verbose=0, name="NoName" -> run examples from f.__doc__.
 
--- 518,523 ----
 return x
 
! def run_docstring_examples(f, globs, verbose=0, name="NoName",
! compileflags=None):
 """f, globs, verbose=0, name="NoName" -> run examples from f.__doc__.
 
***************
*** 588,592 ****
 if not e:
 return 0, 0
! return _run_examples(e, globs, verbose, name)
 
 def is_private(prefix, base):
--- 544,550 ----
 if not e:
 return 0, 0
! if compileflags is None:
! compileflags = _extract_future_flags(globs)
! return _run_examples(e, globs, verbose, name, compileflags)
 
 def is_private(prefix, base):
***************
*** 725,728 ****
--- 683,688 ----
 self.name2ft = {} # map name to (#failures, #trials) pair
 
+ self.compileflags = _extract_future_flags(globs)
+ 
 def runstring(self, s, name):
 """
***************
*** 756,760 ****
 e = _extract_examples(s)
 if e:
! f, t = _run_examples(e, self.globs, self.verbose, name)
 if self.verbose:
 print f, "of", t, "examples failed in string", name
--- 716,721 ----
 e = _extract_examples(s)
 if e:
! f, t = _run_examples(e, self.globs, self.verbose, name,
! self.compileflags)
 if self.verbose:
 print f, "of", t, "examples failed in string", name
***************
*** 794,798 ****
 if self.verbose:
 print "Running", name + ".__doc__"
! f, t = run_docstring_examples(object, self.globs, self.verbose, name)
 if self.verbose:
 print f, "of", t, "examples failed in", name + ".__doc__"
--- 755,760 ----
 if self.verbose:
 print "Running", name + ".__doc__"
! f, t = run_docstring_examples(object, self.globs, self.verbose, name,
! self.compileflags)
 if self.verbose:
 print f, "of", t, "examples failed in", name + ".__doc__"

AltStyle によって変換されたページ (->オリジナル) /