Browse Source

added StackTrace

Dave Schuyler 21 years ago
parent
commit
094df9253d
1 changed files with 29 additions and 2 deletions
  1. 29 2
      direct/src/showbase/PythonUtil.py

+ 29 - 2
direct/src/showbase/PythonUtil.py

@@ -7,6 +7,8 @@ import inspect
 import os
 import os
 import sys
 import sys
 import random
 import random
+if __debug__:
+    import traceback
 
 
 from direct.directutil import Verify
 from direct.directutil import Verify
 
 
@@ -44,6 +46,33 @@ def writeFsmTree(instance, indent = 0):
         
         
 
 
 
 
+
+if __debug__:
+    class StackTrace:
+        def __init__(self, label="", start=0, limit=None):
+            """
+            label is a string (or anything that be be a string)
+                that is printed as part of the trace back.
+                This is just to make it easier to tell what the
+                stack trace is referring to.
+            start is an integer number of stack frames back 
+                from the most recent.  (This is automatically
+                bumped up by one to skip the __init__ call
+                to the StackTrace).
+            limit is an integer number of stack frames
+                to record (or None for unlimited).
+            """
+            self.label = label
+            self.trace = traceback.extract_stack(sys._getframe(1+start), limit=10)
+
+        def __str__(self):
+            r = "Debug stack trace of %s (back %s frames):\n"%(
+                self.label, len(self.trace),)
+            for i in traceback.format_list(self.trace):
+                r+=i
+            return r
+
+
 
 
 def traceFunctionCall(frame):
 def traceFunctionCall(frame):
     """
     """
@@ -87,7 +116,6 @@ def printThisCall():
     return 1 # to allow "assert printThisCall()"
     return 1 # to allow "assert printThisCall()"
 
 
 
 
-
 def tron():
 def tron():
     sys.settrace(trace)
     sys.settrace(trace)
 def trace(frame, event, arg):
 def trace(frame, event, arg):
@@ -106,7 +134,6 @@ def troff():
 
 
 
 
 
 
-
 def apropos(obj, *args):
 def apropos(obj, *args):
     """
     """
     Obsolete, use pdir
     Obsolete, use pdir