get_traceback

H

half.italian

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
 
K

kyosohma

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
 
G

Gabriel Genellina

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
 
H

half.italian

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

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
 
G

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.

Glad to see it helped. Certainly StringIO is a good thing to carry in your
toolbox...
 

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

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top