By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

get_traceback

P: n/a
Hi,

Is there a function or idoim for returning an exception/traceback
rather than just printing it to stdout? I'm running a deamon where
stdout is going to /dev/null, and I'm not even watching it..until
now. All the functions I found in traceback and sys seemed only to
print the error rather than just returning it, so I resorted to this:

def get_traceback():
import traceback, tempfile
stdout = sys.stdout

f = tempfile.TemporaryFile(mode='w+')
sys.stdout = f

traceback.print_tb(sys.exc_info()[2])
error = f.read()
f.close()

sys.stdout = stdout
return error

Whats the right function?!? Thanks.

~Sean

Jun 4 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Jun 4, 12:23 pm, half.ital...@gmail.com wrote:
Hi,

Is there a function or idoim for returning an exception/traceback
rather than just printing it to stdout? I'm running a deamon where
stdout is going to /dev/null, and I'm not even watching it..until
now. All the functions I found in traceback and sys seemed only to
print the error rather than just returning it, so I resorted to this:

def get_traceback():
import traceback, tempfile
stdout = sys.stdout

f = tempfile.TemporaryFile(mode='w+')
sys.stdout = f

traceback.print_tb(sys.exc_info()[2])
error = f.read()
f.close()

sys.stdout = stdout
return error

Whats the right function?!? Thanks.

~Sean
I use the traceback module as you do and I usually have it email the
traceback to me if there's an error. You could also have the script
redirect stdout to a file object.

Mike

Jun 4 '07 #2

P: n/a
En Mon, 04 Jun 2007 14:23:00 -0300, <ha**********@gmail.comescribió:
Is there a function or idoim for returning an exception/traceback
rather than just printing it to stdout? I'm running a deamon where
stdout is going to /dev/null, and I'm not even watching it..until
now. All the functions I found in traceback and sys seemed only to
print the error rather than just returning it, so I resorted to this:
Read again the docs for the traceback module.
Maybe you are looking for traceback.format_exception(*sys.exc_info()) or
traceback.format_exc()
f = tempfile.TemporaryFile(mode='w+')
sys.stdout = f
traceback.print_tb(sys.exc_info()[2])
In this case you can use StringIO instead of a temporary file, and the
file argument to print_tb instead of swapping sys.stdout

--
Gabriel Genellina

Jun 4 '07 #3

P: n/a
On Jun 4, 3:51 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
En Mon, 04 Jun 2007 14:23:00 -0300, <half.ital...@gmail.comescribió:
Is there a function or idoim for returning an exception/traceback
rather than just printing it to stdout? I'm running a deamon where
stdout is going to /dev/null, and I'm not even watching it..until
now. All the functions I found in traceback and sys seemed only to
print the error rather than just returning it, so I resorted to this:

Read again the docs for the traceback module.
Maybe you are looking for traceback.format_exception(*sys.exc_info()) or
traceback.format_exc()
f = tempfile.TemporaryFile(mode='w+')
sys.stdout = f
traceback.print_tb(sys.exc_info()[2])

In this case you can use StringIO instead of a temporary file, and the
file argument to print_tb instead of swapping sys.stdout

--
Gabriel Genellina
Thanks Gabriel. That was exactly what I was looking for. Also, I'm
glad to make a connection to the StringIO class. I'm sure I will
remember it the next time I need it.

~Sean

Jun 5 '07 #4

P: n/a
En Mon, 04 Jun 2007 23:03:04 -0300, <ha**********@gmail.comescribió:
Thanks Gabriel. That was exactly what I was looking for. Also, I'm
glad to make a connection to the StringIO class. I'm sure I will
remember it the next time I need it.
Glad to see it helped. Certainly StringIO is a good thing to carry in your
toolbox...

--
Gabriel Genellina

Jun 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.