[Python-checkins] python/dist/src/Lib doctest.py,1.25,1.26

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Fri, 27 Jun 2003 13:48:07 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv21606/Lib

Modified Files:
	doctest.py 
Log Message:
A hack to ease compatibility with pre-2.3 Pythons:  by default, doctest
now accepts "True" when a test expects "1", and similarly for "False"
versus "0".  This is un-doctest-like, but on balance makes it much
more pleasant to write doctests that pass under 2.2 and 2.3.  I expect
it to go away again, when 2.2 is forgotten.  In the meantime, there's
a new doctest module constant that can be passed to a new optional
argument, if you want to turn this behavior off.

Note that this substitution is very simple-minded:  the expected and
actual outputs have to consist of single tokens.  No attempt is made,
e.g., to accept [True, False] when a test expects [1, 0].  This is a
simple hack for simple tests, and I intend to keep it that way.


Index: doctest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/doctest.py,v
retrieving revision 1.25
retrieving revision 1.26
diff -C2 -d -r1.25 -r1.26
*** doctest.py	22 Nov 2002 08:23:09 -0000	1.25
--- doctest.py	27 Jun 2003 20:48:05 -0000	1.26
***************
*** 298,301 ****
--- 298,304 ----
  from inspect import classify_class_attrs as _classify_class_attrs
  
+ # Option constants.
+ DONT_ACCEPT_TRUE_FOR_1 = 1 << 0
+ 
  # Extract interactive examples from a string.  Return a list of triples,
  # (source, outcome, lineno).  "source" is the source code, and ends
***************
*** 415,419 ****
  
  def _run_examples_inner(out, fakeout, examples, globs, verbose, name,
!                         compileflags):
      import sys, traceback
      OK, BOOM, FAIL = range(3)
--- 418,422 ----
  
  def _run_examples_inner(out, fakeout, examples, globs, verbose, name,
!                         compileflags, optionflags):
      import sys, traceback
      OK, BOOM, FAIL = range(3)
***************
*** 450,454 ****
  
          if state == OK:
!             if got == want:
                  if verbose:
                      out("ok\n")
--- 453,461 ----
  
          if state == OK:
!             if (got == want or
!                 (not (optionflags & DONT_ACCEPT_TRUE_FOR_1) and
!                  (got, want) in (("True\n", "1\n"), ("False\n", "0\n"))
!                 )
!                ):
                  if verbose:
                      out("ok\n")
***************
*** 483,487 ****
  # Return (#failures, #tries).
  
! def _run_examples(examples, globs, verbose, name, compileflags):
      import sys
      saveout = sys.stdout
--- 490,495 ----
  # Return (#failures, #tries).
  
! def _run_examples(examples, globs, verbose, name, compileflags,
!                   optionflags):
      import sys
      saveout = sys.stdout
***************
*** 490,494 ****
          sys.stdout = fakeout = _SpoofOut()
          x = _run_examples_inner(saveout.write, fakeout, examples,
!                                 globs, verbose, name, compileflags)
      finally:
          sys.stdout = saveout
--- 498,503 ----
          sys.stdout = fakeout = _SpoofOut()
          x = _run_examples_inner(saveout.write, fakeout, examples,
!                                 globs, verbose, name, compileflags,
!                                 optionflags)
      finally:
          sys.stdout = saveout
***************
*** 505,509 ****
  
  def run_docstring_examples(f, globs, verbose=0, name="NoName",
!                            compileflags=None):
      """f, globs, verbose=0, name="NoName" -> run examples from f.__doc__.
  
--- 514,518 ----
  
  def run_docstring_examples(f, globs, verbose=0, name="NoName",
!                            compileflags=None, optionflags=0):
      """f, globs, verbose=0, name="NoName" -> run examples from f.__doc__.
  
***************
*** 534,538 ****
      if compileflags is None:
          compileflags = _extract_future_flags(globs)
!     return _run_examples(e, globs, verbose, name, compileflags)
  
  def is_private(prefix, base):
--- 543,547 ----
      if compileflags is None:
          compileflags = _extract_future_flags(globs)
!     return _run_examples(e, globs, verbose, name, compileflags, optionflags)
  
  def is_private(prefix, base):
***************
*** 638,643 ****
  
      def __init__(self, mod=None, globs=None, verbose=None,
!                  isprivate=None):
!         """mod=None, globs=None, verbose=None, isprivate=None
  
  See doctest.__doc__ for an overview.
--- 647,653 ----
  
      def __init__(self, mod=None, globs=None, verbose=None,
!                  isprivate=None, optionflags=0):
!         """mod=None, globs=None, verbose=None, isprivate=None,
! optionflags=0
  
  See doctest.__doc__ for an overview.
***************
*** 659,662 ****
--- 669,674 ----
  whether a name is private.  The default function is doctest.is_private;
  see its docs for details.
+ 
+ See doctest.testmod docs for the meaning of optionflags.
  """
  
***************
*** 679,682 ****
--- 691,696 ----
          self.isprivate = isprivate
  
+         self.optionflags = optionflags
+ 
          self.name2ft = {}   # map name to (#failures, #trials) pair
  
***************
*** 715,719 ****
          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
--- 729,733 ----
          if e:
              f, t = _run_examples(e, self.globs, self.verbose, name,
!                                  self.compileflags, self.optionflags)
          if self.verbose:
              print f, "of", t, "examples failed in string", name
***************
*** 1046,1051 ****
  
  def testmod(m=None, name=None, globs=None, verbose=None, isprivate=None,
!                report=1):
!     """m=None, name=None, globs=None, verbose=None, isprivate=None, report=1
  
      Test examples in docstrings in functions and classes reachable
--- 1060,1066 ----
  
  def testmod(m=None, name=None, globs=None, verbose=None, isprivate=None,
!                report=True, optionflags=0):
!     """m=None, name=None, globs=None, verbose=None, isprivate=None,
!        report=True, optionflags=0
  
      Test examples in docstrings in functions and classes reachable
***************
*** 1081,1084 ****
--- 1096,1109 ----
      detailed, else very brief (in fact, empty if all tests passed).
  
+     Optional keyword arg "optionflags" or's together module constants,
+     and defaults to 0.  This is new in 2.3.  Possible values:
+ 
+         DONT_ACCEPT_TRUE_FOR_1
+             By default, if an expected output block contains just "1",
+             an actual output block containing just "True" is considered
+             to be a match, and similarly for "0" versus "False".  When
+             DONT_ACCEPT_TRUE_FOR_1 is specified, neither substitution
+             is allowed.
+ 
      Advanced tomfoolery:  testmod runs methods of a local instance of
      class doctest.Tester, then merges the results into (or creates)
***************
*** 1103,1111 ****
      if name is None:
          name = m.__name__
!     tester = Tester(m, globs=globs, verbose=verbose, isprivate=isprivate)
      failures, tries = tester.rundoc(m, name)
      f, t = tester.rundict(m.__dict__, name, m)
!     failures = failures + f
!     tries = tries + t
      if hasattr(m, "__test__"):
          testdict = m.__test__
--- 1128,1137 ----
      if name is None:
          name = m.__name__
!     tester = Tester(m, globs=globs, verbose=verbose, isprivate=isprivate,
!                     optionflags=optionflags)
      failures, tries = tester.rundoc(m, name)
      f, t = tester.rundict(m.__dict__, name, m)
!     failures += f
!     tries += t
      if hasattr(m, "__test__"):
          testdict = m.__test__
***************
*** 1115,1120 ****
                                  ".items(); " + `testdict`)
              f, t = tester.run__test__(testdict, name + ".__test__")
!             failures = failures + f
!             tries = tries + t
      if report:
          tester.summarize()
--- 1141,1146 ----
                                  ".items(); " + `testdict`)
              f, t = tester.run__test__(testdict, name + ".__test__")
!             failures += f
!             tries += t
      if report:
          tester.summarize()
***************
*** 1175,1179 ****
                        >>> x + y, x * y
                        (3, 2)
!                       """
             }
  
--- 1201,1220 ----
                        >>> x + y, x * y
                        (3, 2)
!                       """,
!             "bool-int equivalence": r"""
!                                     In 2.2, boolean expressions displayed
!                                     0 or 1.  By default, we still accept
!                                     them.  This can be disabled by passing
!                                     DONT_ACCEPT_TRUE_FOR_1 to the new
!                                     optionflags argument.
!                                     >>> 4 == 4
!                                     1
!                                     >>> 4 == 4
!                                     True
!                                     >>> 4 > 4
!                                     0
!                                     >>> 4 > 4
!                                     False
!                                     """,
             }