Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > handy stacktrace class

Reply
Thread Tools

handy stacktrace class

 
 
Will Ware
Guest
Posts: n/a
 
      12-12-2004
I was fooling with some Python code, and starting to miss the
Exception.printStackTrace() feature in Java. Here is a stab at
something roughly analogous, which puts together a stacktrace
as an XML document.

import xml.dom.minidom

class Stacktrace(xml.dom.minidom.Document):
def __init__(self):
import sys
xml.dom.minidom.Document.__init__(self)
stacktrace = self.createElement("stacktrace")
self.appendChild(stacktrace)
try:
raise Exception
except:
tb = sys.exc_traceback
x = tb.tb_frame.f_back
while x != None:
f = x.f_code
frame = self.createElement("frame")
frame.setAttribute("func", f.co_name)
frame.setAttribute("file", f.co_filename)
frame.setAttribute("line", repr(f.co_firstlineno))
stacktrace.appendChild(frame)
x = x.f_back
def __repr__(self):
import xml.dom.ext
class MyStream:
def __init__(self):
self.str = ""
def write(self, x):
self.str += x
stream = MyStream()
xml.dom.ext.PrettyPrint(self, stream)
return stream.str[:-1] # trim trailing newline

The rational for doing this as an XML document was, uh, gee, I
thought I had a good reason at the time. I think I've seen XML
sequences of stacktraces elsewhere and it looked like a good idea.
My brief time of muddling about with xml.dom.minidom makes me
think that elements are inherently tied to a particular document
and can't just be lifted and moved to another document, as one
might want to do to, say, build a sequence of stacktraces while
debugging something. But I'm sure there's some kind of workaround
for that.
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-12-2004
Will Ware wrote:

>I was fooling with some Python code, and starting to miss the
> Exception.printStackTrace() feature in Java.


traceback.print_exc()

</F>



 
Reply With Quote
 
 
 
 
will.ware@gmail.com
Guest
Posts: n/a
 
      12-13-2004
Oh. Never mind, then.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Re: StackTrace Line Numbers Missing in ASP.NET deployment... Ken Dopierala Jr. ASP .Net 0 09-21-2004 05:53 PM
StackTrace Line Numbers Missing in ASP.NET deployment... =?Utf-8?B?U3RldmUgS2FsbGFs?= ASP .Net 0 09-20-2004 05:33 PM
Stacktrace with line numbers Karsten Grombach ASP .Net 1 08-08-2003 01:59 AM
Re: Occasionally no line numbers in Exception.StackTrace S. Justin Gengo ASP .Net 0 08-05-2003 08:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57