AW: traceback as string

Oliver Walczak oliver.walczak at momatec.de
Thu Dec 18 11:44:46 CET 2003


This seems to be a quite difficult approach. Try this:
#####################################################################
import traceback

class MyTraceback:
    def __init__(self):
        self.clear()
    def clear(self):
        self.s = ''
    def write(self, s):
        self.s += s
    def read(self):
        return self.s
    def catch(self):
        traceback.print_exc(None, self)

if __name__ == '__main__':
    myTcb = MyTraceback()
    try:
        a = 1/0
    except:
        myTcb.clear()
        myTcb.catch()
        print myTcb.read()
######################################################################

Call clear() each time before you expect a new traceback output. You can
import this class and don't need to import traceback in your main project.
Oliver



"John Hunter" <jdhunter at ace.bsd.uchicago.edu> wrote in message
news:mailman.202.1071593230.9307.python-list at python.org...
>
> What is the best way to get the traceback as a string.

I define the following module "Excepts.py" for logging exceptions in daemon
processes:

------------------------------------------
import sys,traceback

def error():
    tb =
traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info(
)[2])
    return tb[len(tb)-1].replace('\n','')

def errorstack():
    return
''.join(traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.e
xc_info()[2]))
-------------------------------------------

Colin Brown
PyNZ


-- 
http://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list