AW: traceback as string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oliver Walczak

    AW: traceback as string

    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@ace.b sd.uchicago.edu > wrote in message
    news:mailman.20 2.1071593230.93 07.python-list@python.org ...[color=blue]
    >
    > What is the best way to get the traceback as a string.[/color]

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

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

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

    def errorstack():
    return
    ''.join(traceba ck.format_excep tion(sys.exc_in fo()[0],sys.exc_info()[1],sys.e
    xc_info()[2]))
    -------------------------------------------

    Colin Brown
    PyNZ


    --



  • Bengt Richter

    #2
    Re: AW: traceback as string

    On Thu, 18 Dec 2003 11:44:46 +0100, "Oliver Walczak" <oliver.walczak @momatec.de> wrote:
    [color=blue]
    >This seems to be a quite difficult approach. Try this:[/color]
    [ snip nice code]
    Here is a variant with repeat counting (may be a bit format-sensitive):
    I rather wish something like it was built into the traceback print itself,
    especially when recursing forever interactively, and one loses initial output context.

    ############### ############### ############### ############### #########
    import traceback

    class MyTraceback:
    def __init__(self):
    self.clear()
    def clear(self):
    self.line = []
    self.lines = []
    self.repeat = 0
    def write(self, s):
    self.line.appen d(s)
    if s[-1:] == '\n':
    s = ''.join(self.li ne)
    self.line = []
    self.lines.appe nd(s)
    if len(self.lines) >=4 and self.lines[-4:-2] == self.lines[-2:]:
    self.repeat += 1
    del self.lines[-2:]
    else:
    if self.repeat and s!= self.lines[-3]:
    if self.repeat==1:
    self.lines.exte nd(self.lines[-2:])
    else:
    self.lines.inse rt(-1,
    ' *** previous two lines repeated %s times ***\n\n'% self.repeat)
    self.repeat = 0
    def read(self):
    return ''.join(self.li nes + self.line)
    def catch(self):
    traceback.print _exc(None, self)

    if __name__ == '__main__':
    myTcb = MyTraceback()
    def foo(n):
    if n<0: foo(n) # blow stack
    print '---> foo(%s)'%n
    if n>0: foo(n-1)
    1/0
    try:
    foo(5)
    except:
    myTcb.clear()
    myTcb.catch()
    print myTcb.read()
    try:
    foo(-5)
    except:
    myTcb.clear()
    myTcb.catch()
    print myTcb.read()
    ############### ############### ############### ############### ##########

    Result:

    [13:19] C:\pywk\clp>myt racebk.py
    ---> foo(5)
    ---> foo(4)
    ---> foo(3)
    ---> foo(2)
    ---> foo(1)
    ---> foo(0)
    Traceback (most recent call last):
    File "C:\pywk\clp\my tracebk.py", line 41, in ?
    foo(5)
    File "C:\pywk\clp\my tracebk.py", line 38, in foo
    if n>0: foo(n-1)
    *** previous two lines repeated 4 times ***

    File "C:\pywk\clp\my tracebk.py", line 39, in foo
    1/0
    ZeroDivisionErr or: integer division or modulo by zero

    Traceback (most recent call last):
    File "C:\pywk\clp\my tracebk.py", line 47, in ?
    foo(-5)
    File "C:\pywk\clp\my tracebk.py", line 36, in foo
    if n<0: foo(n) # blow stack
    *** previous two lines repeated 998 times ***

    RuntimeError: maximum recursion depth exceeded

    Regards,
    Bengt Richter

    Comment

    Working...