def _exceptionStackBTT()

in scripts/yapl/Trace.py [0:0]


  def _exceptionStackBTT(self,methodName,exc,depth=10):
    """
      Return a string useable for output to stdout or a log file that provides a representation
      of the "exception stack" and the "frame stack" from "bottom to top" (BTT).
      The "exception stack" captures the code tree from main to where the exception was raised and 
      is usually the most interesting part of the stack.  The "frame stack" captures the code
      from the point to where the exception was caught. 
      
      Displaying the stack from bottom to top in an output log or stdout is the style in which
      Jython/Python displays the stack by default.
      
      There is another method named _exceptionStackTTB() that can be used to create a string that
      represents the execution stack from top to bottom, which is the style that Java uses.
    """
    stack = ""
    # Reconstruct the call stack from where the trace of the exception was initiated by invoking 
    # Trace.error() or Trace.severe().
    stackList = traceback.extract_stack()
    try:
      stack = "\tFrame stack (most recent call last):\n"
      for stackData in stackList:
        sourcefile,line,function,text = stackData
        if (sourcefile.endswith("Trace.py") and (function == "error" or function == "severe")): break
        sepIndex = sourcefile.rfind(os.sep)
        if (sepIndex >=0 and Trace.SourceFileStyle == Trace.NameOnly):
          sourcefile = sourcefile[sepIndex+1:]
        #endIf
        if (text == None):
          stack = "%s\t%s(%s) [%s]\n" % (stack,sourcefile,line,function)
        else:
          stack = "%s\t%s(%s) [%s] - %s\n" % (stack,sourcefile,line,function,text)
        #endIf
      #endFor
    except:
      # This shouldn't happen, but in case it does...
      exc_type,exc_value = sys.exc_info()[:2]
      stack = "%s\n\tException getting frame stack. Type: %s, Value: %s" % (stack,exc_type,exc_value)
    #endTry
    
    try:
      stack = "%s\tException stack (most recent call last):\n" % stack
      tb = sys.exc_info()[2]
      stackList = traceback.extract_tb(tb,depth)
      for stackData in stackList:
        sourcefile,line,function,text = stackData
        sepIndex = sourcefile.rfind(os.sep)
        if (sepIndex >=0 and Trace.SourceFileStyle == Trace.NameOnly):
          sourcefile = sourcefile[sepIndex+1:]
        #endIf
        if (text == None):
          stack = "%s\t%s(%s) [%s]\n" % (stack,sourcefile,line,function)
        else:        
          stack = "%s\t%s(%s) [%s] - %s\n" % (stack,sourcefile,line,function,text)
        #endIf
      #endFor
    except:
      # This shouldn't happen, but in case it does...
      exc_type,exc_value = sys.exc_info()[:2]
      stack = "%s\tException getting exception stack. Type: %s, Value: %s\n" % (stack,exc_type,exc_value)
    #endTry

    # At the very end - put the exception string
    stack = "%s\t%s" % (stack,exc)
    
    return stack