[Python-checkins] CVS: python/dist/src/Lib/test test_profilehooks.py,1.3,1.4

Fred L. Drake fdrake@users.sourceforge.net
Wed, 26 Sep 2001 14:00:35 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv32382

Modified Files:
	test_profilehooks.py 
Log Message:
More test cases, including something that simulates what the profiler
probably *should* be doing.


Index: test_profilehooks.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_profilehooks.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** test_profilehooks.py	2001/09/25 20:48:14	1.3
--- test_profilehooks.py	2001/09/26 21:00:33	1.4
***************
*** 35,46 ****
  
  
! class ProfileHookTestCase(unittest.TestCase):
  
      def check_events(self, callable, expected):
!         events = capture_events(callable)
          if events != expected:
              self.fail("Expected events:\n%s\nReceived events:\n%s"
                        % (pprint.pformat(expected), pprint.pformat(events)))
  
      def test_simple(self):
          def f(p):
--- 35,80 ----
  
  
! class ProfileSimulator(HookWatcher):
!     def __init__(self):
!         self.stack = []
!         HookWatcher.__init__(self)
! 
!     def callback(self, frame, event, arg):
!         self.dispatch[event](self, frame)
! 
!     def trace_call(self, frame):
!         self.add_event('call', frame)
!         self.stack.append(frame)
! 
!     def trace_return(self, frame):
!         self.add_event('return', frame)
!         self.stack.pop()
! 
!     def trace_exception(self, frame):
!         if len(self.stack) >= 2 and frame is self.stack[-2]:
!             self.add_event('propogate-from', self.stack[-1])
!             self.stack.pop()
!         else:
!             self.add_event('ignore', frame)
! 
!     dispatch = {
!         'call': trace_call,
!         'exception': trace_exception,
!         'return': trace_return,
!         }
! 
  
+ class TestCaseBase(unittest.TestCase):
      def check_events(self, callable, expected):
!         events = capture_events(callable, self.new_watcher())
          if events != expected:
              self.fail("Expected events:\n%s\nReceived events:\n%s"
                        % (pprint.pformat(expected), pprint.pformat(events)))
  
+ 
+ class ProfileHookTestCase(TestCaseBase):
+     def new_watcher(self):
+         return HookWatcher()
+ 
      def test_simple(self):
          def f(p):
***************
*** 160,163 ****
--- 194,219 ----
  
  
+ class ProfileSimulatorTestCase(TestCaseBase):
+     def new_watcher(self):
+         return ProfileSimulator()
+ 
+     def test_simple(self):
+         def f(p):
+             pass
+         f_ident = ident(f)
+         self.check_events(f, [(1, 'call', f_ident),
+                               (1, 'return', f_ident),
+                               ])
+ 
+     def test_basic_exception(self):
+         def f(p):
+             1/0
+         f_ident = ident(f)
+         self.check_events(f, [(1, 'call', f_ident),
+                               (1, 'ignore', f_ident),
+                               (1, 'propogate-from', f_ident),
+                               ])
+ 
+ 
  def ident(function):
      if hasattr(function, "f_code"):
***************
*** 175,180 ****
  
  
! def capture_events(callable):
!     p = HookWatcher()
      sys.setprofile(p.callback)
      protect(callable, p)
--- 231,237 ----
  
  
! def capture_events(callable, p=None):
!     if p is None:
!         p = HookWatcher()
      sys.setprofile(p.callback)
      protect(callable, p)
***************
*** 189,193 ****
  
  def test_main():
!     test_support.run_unittest(ProfileHookTestCase)
  
  
--- 246,254 ----
  
  def test_main():
!     loader = unittest.TestLoader()
!     suite = unittest.TestSuite()
!     suite.addTest(loader.loadTestsFromTestCase(ProfileHookTestCase))
!     suite.addTest(loader.loadTestsFromTestCase(ProfileSimulatorTestCase))
!     test_support.run_suite(suite)