[Python-checkins] CVS: python/dist/src/Lib profile.py,1.27,1.27.2.1

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 19 Oct 2001 08:13:53 -0700


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

Modified Files:
      Tag: release21-maint
	profile.py 
Log Message:
Merge in selected changes from profile.py on the trunk.  Note that
this is *not* a simple-minded merge from the code on the trunk -- that
does too much other stuff to be 100% safe for the 2.1.2 release
(e.g. getting rid of HotProfile and OldProfile, changing some methods
into global functions, a new calibration API).

Add the test_profile.py module which verifies that the profiler works
as expected.


Index: profile.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/profile.py,v
retrieving revision 1.27
retrieving revision 1.27.2.1
diff -C2 -d -r1.27 -r1.27.2.1
*** profile.py	2001/03/14 20:01:19	1.27
--- profile.py	2001/10/19 15:13:51	1.27.2.1
***************
*** 105,111 ****
             timing data for the parent frame.
      [ 1] = Total time spent in this frame's function, excluding time in
!            subfunctions
!     [ 2] = Cumulative time spent in this frame's function, including time in
!            all subfunctions to this frame.
      [-3] = Name of the function that corresponds to this frame.
      [-2] = Actual frame that we correspond to (used to sync exception handling)
--- 105,111 ----
             timing data for the parent frame.
      [ 1] = Total time spent in this frame's function, excluding time in
!            subfunctions (this latter is tallied in cur[2]).
!     [ 2] = Total time spent in subfunctions, excluding time executing the
!            frame's function (this latter is tallied in cur[1]).
      [-3] = Name of the function that corresponds to this frame.
      [-2] = Actual frame that we correspond to (used to sync exception handling)
***************
*** 124,128 ****
            to finish of each invocation of a function, including time spent in
            all subfunctions.
!     [5] = A dictionary indicating for each function name, the number of times
            it was called by us.
      """
--- 124,128 ----
            to finish of each invocation of a function, including time spent in
            all subfunctions.
!     [4] = A dictionary indicating for each function name, the number of times
            it was called by us.
      """
***************
*** 229,250 ****
      def trace_dispatch_exception(self, frame, t):
          rt, rtt, rct, rfn, rframe, rcur = self.cur
!         if (not rframe is frame) and rcur:
              return self.trace_dispatch_return(rframe, t)
!         return 0
  
  
      def trace_dispatch_call(self, frame, t):
          fcode = frame.f_code
          fn = (fcode.co_filename, fcode.co_firstlineno, fcode.co_name)
          self.cur = (t, 0, 0, fn, frame, self.cur)
!         if self.timings.has_key(fn):
!             cc, ns, tt, ct, callers = self.timings[fn]
!             self.timings[fn] = cc, ns + 1, tt, ct, callers
          else:
!             self.timings[fn] = 0, 0, 0, 0, {}
          return 1
  
      def trace_dispatch_return(self, frame, t):
!         # if not frame is self.cur[-2]: raise "Bad return", self.cur[3]
  
          # Prefix "r" means part of the Returning or exiting frame
--- 229,266 ----
      def trace_dispatch_exception(self, frame, t):
          rt, rtt, rct, rfn, rframe, rcur = self.cur
!         if (rframe is not frame) and rcur:
              return self.trace_dispatch_return(rframe, t)
!         self.cur = rt, rtt+t, rct, rfn, rframe, rcur
!         return 1
  
  
      def trace_dispatch_call(self, frame, t):
+         if self.cur and frame.f_back is not self.cur[-2]:
+             rt, rtt, rct, rfn, rframe, rcur = self.cur
+             if not isinstance(rframe, Profile.fake_frame):
+                 if rframe.f_back is not frame.f_back:
+                     print rframe, rframe.f_back
+                     print frame, frame.f_back
+                     raise "Bad call", self.cur[-3]
+                 self.trace_dispatch_return(rframe, 0)
+                 if self.cur and frame.f_back is not self.cur[-2]:
+                     raise "Bad call[2]", self.cur[-3]
          fcode = frame.f_code
          fn = (fcode.co_filename, fcode.co_firstlineno, fcode.co_name)
          self.cur = (t, 0, 0, fn, frame, self.cur)
!         timings = self.timings
!         if timings.has_key(fn):
!             cc, ns, tt, ct, callers = timings[fn]
!             timings[fn] = cc, ns + 1, tt, ct, callers
          else:
!             timings[fn] = 0, 0, 0, 0, {}
          return 1
  
      def trace_dispatch_return(self, frame, t):
!         if frame is not self.cur[-2]:
!             if frame is self.cur[-2].f_back:
!                 self.trace_dispatch_return(self.cur[-2], 0)
!             else:
!                 raise "Bad return", self.cur[-3]
  
          # Prefix "r" means part of the Returning or exiting frame
***************
*** 258,262 ****
          self.cur = pt, ptt+rt, pct+sft, pfn, pframe, pcur
  
!         cc, ns, tt, ct, callers = self.timings[rfn]
          if not ns:
              ct = ct + sft
--- 274,279 ----
          self.cur = pt, ptt+rt, pct+sft, pfn, pframe, pcur
  
!         timings = self.timings
!         cc, ns, tt, ct, callers = timings[rfn]
          if not ns:
              ct = ct + sft
***************
*** 269,273 ****
          else:
              callers[pfn] = 1
!         self.timings[rfn] = cc, ns - 1, tt+rtt, ct, callers
  
          return 1
--- 286,290 ----
          else:
              callers[pfn] = 1
!         timings[rfn] = cc, ns - 1, tt+rtt, ct, callers
  
          return 1