AW: traceback as string

O

Oliver Walczak

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 said:
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]))
 
B

Bengt Richter

This seems to be a quite difficult approach. Try this:
[ 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.append(s)
if s[-1:] == '\n':
s = ''.join(self.line)
self.line = []
self.lines.append(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.extend(self.lines[-2:])
else:
self.lines.insert(-1,
' *** previous two lines repeated %s times ***\n\n'% self.repeat)
self.repeat = 0
def read(self):
return ''.join(self.lines + 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>mytracebk.py
---> foo(5)
---> foo(4)
---> foo(3)
---> foo(2)
---> foo(1)
---> foo(0)
Traceback (most recent call last):
File "C:\pywk\clp\mytracebk.py", line 41, in ?
foo(5)
File "C:\pywk\clp\mytracebk.py", line 38, in foo
if n>0: foo(n-1)
*** previous two lines repeated 4 times ***

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

Traceback (most recent call last):
File "C:\pywk\clp\mytracebk.py", line 47, in ?
foo(-5)
File "C:\pywk\clp\mytracebk.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top