Ver Fonte

added appendStr, converted exceptionLogged to use it so that info shows up after stack trace

Darren Ranalli há 19 anos atrás
pai
commit
60719cbf5c
1 ficheiros alterados com 21 adições e 5 exclusões
  1. 21 5
      direct/src/showbase/PythonUtil.py

+ 21 - 5
direct/src/showbase/PythonUtil.py

@@ -2084,6 +2084,20 @@ def tagWithCaller(obj):
 def isDefaultValue(x):
 def isDefaultValue(x):
     return x == type(x)()
     return x == type(x)()
 
 
+def appendStr(obj, st):
+    """adds a string onto the __str__ output of an instance"""
+    def appendedStr(oldStr, st, self):
+        return oldStr() + st
+    oldStr = getattr(obj, '__str__', None)
+    if oldStr is None:
+        def stringer(s):
+            return s
+        oldStr = Functor(stringer, str(obj))
+        stringer = None
+    obj.__str__ = new.instancemethod(Functor(appendedStr, oldStr, st), obj, obj.__class__)
+    appendedStr = None
+    return obj
+
 # debugging functions that conditionally bring up the debugger in __dev__
 # debugging functions that conditionally bring up the debugger in __dev__
 # all can be used with assert, as in 'assert _equal(a,b)'
 # all can be used with assert, as in 'assert _equal(a,b)'
 def setTrace():
 def setTrace():
@@ -2478,15 +2492,16 @@ def superFlattenShip(ship):
     return ship.flattenStrong()
     return ship.flattenStrong()
 
 
 def exceptionLogged(f):
 def exceptionLogged(f):
-    """decorator that prints the function name and all arguments if an
-    exception passes back through the stack frame
+    """decorator that appends the function name and all arguments to the
+    __str__ output of the exception if an exception passes back through
+    the stack frame
     """
     """
     def _exceptionLogged(*args, **kArgs):
     def _exceptionLogged(*args, **kArgs):
         try:
         try:
             return f(*args, **kArgs)
             return f(*args, **kArgs)
-        except:
+        except Exception, e:
             try:
             try:
-                s = 'STACK UNWIND: %s(' % f.func_name
+                s = '\nSTACK UNWIND: %s(' % f.func_name
                 for arg in args:
                 for arg in args:
                     s += '%s, ' % arg
                     s += '%s, ' % arg
                 for key, value in kArgs.items():
                 for key, value in kArgs.items():
@@ -2494,7 +2509,7 @@ def exceptionLogged(f):
                 if len(args) or len(kArgs):
                 if len(args) or len(kArgs):
                     s = s[:-2]
                     s = s[:-2]
                 s += ')'
                 s += ')'
-                print s
+                appendStr(e, s)
             except:
             except:
                 print 'exceptionLogged(%s): ERROR IN PRINTING' % f.func_name
                 print 'exceptionLogged(%s): ERROR IN PRINTING' % f.func_name
             raise
             raise
@@ -2564,3 +2579,4 @@ __builtin__._contains = _contains
 __builtin__._notIn = _notIn
 __builtin__._notIn = _notIn
 __builtin__.itype = itype
 __builtin__.itype = itype
 __builtin__.exceptionLogged = exceptionLogged
 __builtin__.exceptionLogged = exceptionLogged
+__builtin__.appendStr = appendStr