[Python-checkins] python/dist/src/Lib doctest.py, 1.36.2.9, 1.36.2.10

edloper at users.sourceforge.net edloper at users.sourceforge.net
Tue Aug 3 22:09:57 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1323
Modified Files:
 Tag: tim-doctest-branch
	doctest.py 
Log Message:
- Explicitly set verbose=False when running test cases for DocTestRunner
 (so that it doesn't pick up the verbosity from sys.argv)
- Simplified blankline handling in TestRunner.output_difference()
- Fixed the debugging support code
- Removed tests5() (now tested in test.test_doctest)
Index: doctest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/doctest.py,v
retrieving revision 1.36.2.9
retrieving revision 1.36.2.10
diff -C2 -d -r1.36.2.9 -r1.36.2.10
*** doctest.py	3 Aug 2004 19:52:48 -0000	1.36.2.9
--- doctest.py	3 Aug 2004 20:09:51 -0000	1.36.2.10
***************
*** 307,311 ****
 
 import sys, traceback, inspect, linecache, re, types
! import unittest, difflib
 from StringIO import StringIO
 
--- 307,311 ----
 
 import sys, traceback, inspect, linecache, re, types
! import unittest, difflib, tempfile
 from StringIO import StringIO
 
***************
*** 885,889 ****
 
 >>> tests = DocTestFinder().find(_TestClass)
! >>> runner = DocTestRunner()
 >>> for test in tests:
 ... print runner.run(test, globals())
--- 885,889 ----
 
 >>> tests = DocTestFinder().find(_TestClass)
! >>> runner = DocTestRunner(verbose=False)
 >>> for test in tests:
 ... print runner.run(test, globals())
***************
*** 1036,1039 ****
--- 1036,1044 ----
 expected output (`want`) and the actual output (`got`).
 """
+ # If <BLANKLINE>s are being used, then replace <BLANKLINE>
+ # with blank lines in the expected output string.
+ if not (self._optionflags & DONT_ACCEPT_BLANKLINE):
+ want = re.sub('(?m)^%s$' % re.escape(BLANKLINE_MARKER), '', want)
+ 
 # Check if we should use diff. Don't use diff if the actual
 # or expected outputs are too short, or if the expected output
***************
*** 1042,1050 ****
 want.count('\n') > 2 and got.count('\n') > 2 and
 not (self._optionflags & ELLIPSIS and '...' in want)):
- # Replace <BLANKLINE> with blank lines in the expected
- # output string (so they don't confuse difflib).
- if not (self._optionflags & DONT_ACCEPT_BLANKLINE):
- want = re.sub('(?m)^%s$' % re.escape(BLANKLINE_MARKER),
- '', want)
 # Split want & got into lines.
 want_lines = [l+'\n' for l in want.split('\n')]
--- 1047,1050 ----
***************
*** 1067,1074 ****
 
 # If we're not using diff, then simply list the expected
! # output followed by the actual output. But explicitly mark
! # blank lines in the actual output.
! if not (self._optionflags & DONT_ACCEPT_BLANKLINE):
! got = re.sub('(?m)^$(?!\Z)', '<BLANKLINE>', got)
 return (_tag_msg("Expected", want or "Nothing") +
 _tag_msg("Got", got))
--- 1067,1071 ----
 
 # If we're not using diff, then simply list the expected
! # output followed by the actual output.
 return (_tag_msg("Expected", want or "Nothing") +
 _tag_msg("Got", got))
***************
*** 1531,1534 ****
--- 1528,1533 ----
 ## 6. Tester
 ######################################################################
+ # This is provided only for backwards compatibility. It's not
+ # actually used in any way.
 
 class Tester:
***************
*** 1714,1725 ****
 ## 8. Debugging Support
 ######################################################################
- # [XX] This is currently broken:
 
! def _expect(expect):
 # Return the expected output, if any
! if expect:
! expect = "\n# ".join(expect.split("\n"))
! expect = "\n# Expect:\n# %s" % expect
! return expect
 
 def testsource(module, name):
--- 1713,1729 ----
 ## 8. Debugging Support
 ######################################################################
 
! def _want_comment(example):
! """
! Return a comment containing the expected output for the given
! example.
! """
 # Return the expected output, if any
! want = example.want
! if want:
! if want[-1] == '\n': want = want[:-1]
! want = "\n# ".join(want.split("\n"))
! want = "\n# Expected:\n# %s" % want
! return want
 
 def testsource(module, name):
***************
*** 1731,1745 ****
 
 """
! module = _normalizeModule(module)
! tests = _findTests(module, "")
! test = [doc for (tname, doc, f, l) in tests if tname == name]
 if not test:
 raise ValueError(name, "not found in tests")
 test = test[0]
- # XXX we rely on an internal doctest function:
- examples = _extract_examples(test)
 testsrc = '\n'.join([
! "%s%s" % (source, _expect(expect))
! for (source, expect, lineno) in examples
 ])
 return testsrc
--- 1735,1747 ----
 
 """
! module = _normalize_module(module)
! tests = DocTestFinder().find(module)
! test = [t for t in tests if t.name == name]
 if not test:
 raise ValueError(name, "not found in tests")
 test = test[0]
 testsrc = '\n'.join([
! "%s%s" % (example.source, _want_comment(example))
! for example in test.examples
 ])
 return testsrc
***************
*** 1750,1760 ****
 The string is provided directly
 """
! # XXX we rely on an internal doctest function:
! examples = _extract_examples(src)
! src = '\n'.join([
! "%s%s" % (source, _expect(expect))
! for (source, expect, lineno) in examples
 ])
! debug_script(src, pm, globs)
 
 def debug_script(src, pm=False, globs=None):
--- 1752,1762 ----
 The string is provided directly
 """
! test = DocTest(src, 'debug', None, None)
! 
! testsrc = '\n'.join([
! "%s%s" % (example.source, _want_comment(example))
! for example in test.examples
 ])
! debug_script(testsrc, pm, globs)
 
 def debug_script(src, pm=False, globs=None):
***************
*** 1762,1767 ****
 import pdb
 
! srcfilename = tempfile.mktemp("doctestdebug.py")
! open(srcfilename, 'w').write(src)
 if globs:
 globs = globs.copy()
--- 1764,1771 ----
 import pdb
 
! srcfile = tempfile.NamedTemporaryFile(prefix='doctestdebug-',
! suffix='.py', mode="w")
! srcfile.write(src)
! srcfile.flush()
 if globs:
 globs = globs.copy()
***************
*** 1772,1776 ****
 if pm:
 try:
! execfile(srcfilename, globs, globs)
 except:
 print sys.exc_info()[1]
--- 1776,1780 ----
 if pm:
 try:
! execfile(srcfile.name, globs, globs)
 except:
 print sys.exc_info()[1]
***************
*** 1779,1785 ****
 # Note that %r is vital here. '%s' instead can, e.g., cause
 # backslashes to get treated as metacharacters on Windows.
! pdb.run("execfile(%r)" % srcfilename, globs, globs)
 finally:
! os.remove(srcfilename)
 
 def debug(module, name, pm=False):
--- 1783,1789 ----
 # Note that %r is vital here. '%s' instead can, e.g., cause
 # backslashes to get treated as metacharacters on Windows.
! pdb.run("execfile(%r)" % srcfile.name, globs, globs)
 finally:
! srcfile.close() # Automatically deletes the file.
 
 def debug(module, name, pm=False):
***************
*** 1791,1801 ****
 
 """
! module = _normalizeModule(module)
 testsrc = testsource(module, name)
 debug_script(testsrc, pm, module.__dict__)
 
- 
- 
- 
 ######################################################################
 ## 9. Example Usage
--- 1795,1802 ----
 
 """
! module = _normalize_module(module)
 testsrc = testsource(module, name)
 debug_script(testsrc, pm, module.__dict__)
 
 ######################################################################
 ## 9. Example Usage
***************
*** 1999,2022 ****
 meant to be invoked automagically by testmod.
 
! >>> testmod(m1, isprivate=is_private)
 (0, 3)
 """
 
- def test5(): r"""
- 
- >>> raise ValueError('x')
- Traceback (most recent call last):
- [...]
- ValueError: x
- 
- >>> print 'x'; raise ValueError('\nfoo')
- x
- Traceback (most recent call last):
- [...]
- ValueError:
- foo
- 
- """
- 
 def _test():
 #import doctest
--- 2000,2007 ----
 meant to be invoked automagically by testmod.
 
! >>> testmod(m1, isprivate=is_private, verbose=False)
 (0, 3)
 """
 
 def _test():
 #import doctest


More information about the Python-checkins mailing list

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