Browse Source

write PythonUtil.describeException

David Rose 22 years ago
parent
commit
c0ace4ee5d
2 changed files with 34 additions and 0 deletions
  1. 5 0
      direct/src/distributed/ConnectionRepository.py
  2. 29 0
      direct/src/showbase/PythonUtil.py

+ 5 - 0
direct/src/distributed/ConnectionRepository.py

@@ -238,6 +238,11 @@ class ConnectionRepository(DirectObject.DirectObject):
                     return 1
             return 0
 
+    def flush(self):
+        # Ensure the latest has been sent to the server.
+        if self.tcpConn:
+            self.tcpConn.flush()
+
     def ensureValidConnection(self):
         # Was the connection reset?
         if self.connectHttp:

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

@@ -687,6 +687,34 @@ def findPythonModule(module):
         
     return None
 
+def describeException(backTrace = 4):
+    # When called in an exception handler, returns a string describing
+    # the current exception.
+    
+    infoArr = sys.exc_info()
+    exception = infoArr[0]
+    exceptionName = getattr(exception, '__name__', None)
+    extraInfo = infoArr[1]
+    trace = infoArr[2]
+
+    stack = []
+    while trace.tb_next:
+        module = trace.tb_frame.f_globals.get('__name__', None)
+        lineno = trace.tb_lineno
+        stack.append("%s:%s, " % (module, lineno))
+        trace = trace.tb_next
+
+    module = trace.tb_frame.f_globals.get('__name__', None)
+    lineno = trace.tb_lineno
+    stack.append("%s:%s, " % (module, lineno))
+
+    description = ""
+    for i in range(len(stack) - 1, max(len(stack) - backTrace, 0) - 1, -1):
+        description += stack[i]
+        
+    description += "%s: %s" % (exceptionName, extraInfo)
+    return description
+
 class PureVirtual:
     """ Python classes that want to have C++-style pure-virtual functions
     can derive from this class and call 'derivedMustOverride' from their
@@ -698,3 +726,4 @@ class PureVirtual:
         and are not meant to be chained down to. This simulates C++
         pure-virtual methods. """
         raise 'error: derived class must implement %s' % callerInfo()[2]
+