[Python-checkins] python/dist/src/Lib doctest.py, 1.36.2.25, 1.36.2.26

dcjim at users.sourceforge.net dcjim at users.sourceforge.net
Fri Aug 6 22:26:49 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7551

Modified Files:
      Tag: tim-doctest-branch
	doctest.py 
Log Message:
Changed the way the debug runner handes unexpected exceptions.
Now wrap the original exception in an UnexpectedException, which gives
the caller access to test and example data.


Index: doctest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/doctest.py,v
retrieving revision 1.36.2.25
retrieving revision 1.36.2.26
diff -C2 -d -r1.36.2.25 -r1.36.2.26
*** doctest.py	6 Aug 2004 19:53:00 -0000	1.36.2.25
--- doctest.py	6 Aug 2004 20:26:46 -0000	1.36.2.26
***************
*** 1581,1599 ****
          return str(self.test)
  
  
  
  class DebugRunner(DocTestRunner):
      r"""Run doc tests but raise an exception as soon as there is a failure.
  
!        If an unexpected exception occurs, the exception is merely propagated
!        to the caller:
  
           >>> runner = DebugRunner(verbose=False)
!          >>> test = DocTest('>>> raise KeyError', {}, 'foo', 'foo.py', 0)
!          >>> runner.run(test)
           Traceback (most recent call last):
           ...
           KeyError
  
         If the output doesn't match, then a DocTestFailure is raised:
  
--- 1581,1631 ----
          return str(self.test)
  
+ class UnexpectedException(Exception):
+     """A DocTest example has encountered an unexpected exception
+ 
+     The exception instance has variables:
+ 
+     - test: the DocTest object being run
  
+     - excample: the Example object that failed
  
+     - exc_info: the exception info
+     """
+     def __init__(self, test, example, exc_info):
+         self.test = test
+         self.example = example
+         self.exc_info = exc_info
+ 
+     def __str__(self):
+         return str(self.test)
+     
  class DebugRunner(DocTestRunner):
      r"""Run doc tests but raise an exception as soon as there is a failure.
  
!        If an unexpected exception occurs, an UnexpectedException is raised.
!        It contains the test, the example, and the original exception:
  
           >>> runner = DebugRunner(verbose=False)
!          >>> test = DocTest('>>> raise KeyError\n42', {}, 'foo', 'foo.py', 0)
!          >>> try:
!          ...     runner.run(test)
!          ... except UnexpectedException, failure:
!          ...     pass
! 
!          >>> failure.test is test
!          True
! 
!          >>> failure.example.want
!          '42\n'
! 
!          >>> exc_info = failure.exc_info
!          >>> raise exc_info[0], exc_info[1], exc_info[2]
           Traceback (most recent call last):
           ...
           KeyError
  
+        We wrap the original exception to give the calling application
+        access to the test and example information.
+ 
         If the output doesn't match, then a DocTestFailure is raised:
  
***************
*** 1638,1643 ****
           Traceback (most recent call last):
           ...
!          KeyError
! 
           >>> del test.globs['__builtins__']
           >>> test.globs
--- 1670,1675 ----
           Traceback (most recent call last):
           ...
!          UnexpectedException: <DocTest foo from foo.py:0 (2 examples)>
!          
           >>> del test.globs['__builtins__']
           >>> test.globs
***************
*** 1656,1660 ****
           {}
  
- 
         """
  
--- 1688,1691 ----
***************
*** 1666,1670 ****
  
      def report_unexpected_exception(self, out, test, example, exc_info):
!         raise exc_info[0], exc_info[1], exc_info[2]
  
      def report_failure(self, out, test, example, got):
--- 1697,1701 ----
  
      def report_unexpected_exception(self, out, test, example, exc_info):
!         raise UnexpectedException(test, example, exc_info)
  
      def report_failure(self, out, test, example, got):
***************
*** 1953,1961 ****
             and test suites to support post-mortem debugging.  The test code
             is run in such a way that errors are not caught.  This way a
!            caller can catch the errors and initiate post-mortem debugging:
  
!              >>> test = DocTest('>>> raise KeyError', {}, 'foo', 'foo.py', 0)
               >>> case = DocTestCase(test)
!              >>> case.debug()
               Traceback (most recent call last):
               ...
--- 1984,2012 ----
             and test suites to support post-mortem debugging.  The test code
             is run in such a way that errors are not caught.  This way a
!            caller can catch the errors and initiate post-mortem debugging.
  
!            The DocTestCase provides a debug method that raises
!            UnexpectedException errors if there is an unexepcted
!            exception:
! 
!              >>> test = DocTest('>>> raise KeyError\n42',
!              ...                {}, 'foo', 'foo.py', 0)
               >>> case = DocTestCase(test)
!              >>> try:
!              ...     case.debug()
!              ... except UnexpectedException, failure:
!              ...     pass
! 
!            The UnexpectedException contains the test, the example, and
!            the original exception:
! 
!              >>> failure.test is test
!              True
! 
!              >>> failure.example.want
!              '42\n'
! 
!              >>> exc_info = failure.exc_info
!              >>> raise exc_info[0], exc_info[1], exc_info[2]
               Traceback (most recent call last):
               ...



More information about the Python-checkins mailing list