472,119 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Extracting the traceback

Hi there,
I'm facing a case where I need to get the traceback outptut when
occurring an exception.
I solved such problem by using traceback module in conjunction with
StringIO:

import StringIO, traceback
try:
raise Exception
except:
f = StringIO.StringIO()
traceback.print_exc(file=f)
print f.getvalue()

.... but this seems a little poor to me since I first put output into
the StringIO.StringIO(), then I get it back by using getvalue() on
it.
Is there a way to avoid the use of StringIO and get such output
without using such (useless) additional step?

Thanks in advance

Aug 21 '07 #1
4 1904
On 8/21/07, billiejoex <gn****@gmail.comwrote:
Hi there,
I'm facing a case where I need to get the traceback outptut when
occurring an exception.
I solved such problem by using traceback module in conjunction with
StringIO:

import StringIO, traceback
try:
raise Exception
except:
f = StringIO.StringIO()
traceback.print_exc(file=f)
print f.getvalue()

... but this seems a little poor to me since I first put output into
the StringIO.StringIO(), then I get it back by using getvalue() on
it.
Is there a way to avoid the use of StringIO and get such output
without using such (useless) additional step?
If you just want to print the output (as in your example), you can use
file=sys.stdout or file=sys.stderr in the call to print_exc. If you
want to store the traceback into a string for some other purpose (e.g.
logging, email notifications) I believe that you must use a StringIO
object.

--
Evan Klitzke <ev**@yelp.com>
Aug 22 '07 #2
On 22 Ago, 02:09, "Evan Klitzke" <e...@yelp.comwrote:
On 8/21/07, billiejoex <gne...@gmail.comwrote:
Hi there,
I'm facing a case where I need to get the traceback outptut when
occurring an exception.
I solved such problem by using traceback module in conjunction with
StringIO:
import StringIO, traceback
try:
raise Exception
except:
f = StringIO.StringIO()
traceback.print_exc(file=f)
print f.getvalue()
... but this seems a little poor to me since I first put output into
the StringIO.StringIO(), then I get it back by using getvalue() on
it.
Is there a way to avoid the use of StringIO and get such output
without using such (useless) additional step?

If you just want to print the output (as in your example), you can use
file=sys.stdout or file=sys.stderr in the call to print_exc. If you
want to store the traceback into a string for some other purpose (e.g.
logging, email notifications) I believe that you must use a StringIO
object.

--
Evan Klitzke <e...@yelp.com>
Unfortunately I have to pass the output to a function which prints the
passed string both on screen and into a log file.
Anyway, not too much important. I'll use StringIO if there's no other
solution.

Thanks

Aug 22 '07 #3
On 8/21/07, codesite-noreply <bi********@gmail.comwrote:
On 22 Ago, 02:09, "Evan Klitzke" <e...@yelp.comwrote:
On 8/21/07, billiejoex <gne...@gmail.comwrote:
Hi there,
I'm facing a case where I need to get the traceback outptut when
occurring an exception.
I solved such problem by using traceback module in conjunction with
StringIO:
import StringIO, traceback
try:
raise Exception
except:
f = StringIO.StringIO()
traceback.print_exc(file=f)
print f.getvalue()
... but this seems a little poor to me since I first put output into
the StringIO.StringIO(), then I get it back by using getvalue() on
it.
Is there a way to avoid the use of StringIO and get such output
without using such (useless) additional step?
If you just want to print the output (as in your example), you can use
file=sys.stdout or file=sys.stderr in the call to print_exc. If you
want to store the traceback into a string for some other purpose (e.g.
logging, email notifications) I believe that you must use a StringIO
object.

--
Evan Klitzke <e...@yelp.com>

Unfortunately I have to pass the output to a function which prints the
passed string both on screen and into a log file.
Anyway, not too much important. I'll use StringIO if there's no other
solution.
Turns out I was wrong -- you can just get the string, using format_exc
rather than print_exc.

--
Evan Klitzke <ev**@yelp.com>
Aug 22 '07 #4
On 22 Ago, 03:11, "Evan Klitzke" <e...@yelp.comwrote:
On 8/21/07, codesite-noreply <billiej...@gmail.comwrote:


On 22 Ago, 02:09, "Evan Klitzke" <e...@yelp.comwrote:
On 8/21/07, billiejoex <gne...@gmail.comwrote:
Hi there,
I'm facing a case where I need to get the traceback outptut when
occurring an exception.
I solved such problem by using traceback module in conjunction with
StringIO:
import StringIO, traceback
try:
raise Exception
except:
f = StringIO.StringIO()
traceback.print_exc(file=f)
print f.getvalue()
... but this seems a little poor to me since I first put output into
the StringIO.StringIO(), then I get it back by using getvalue() on
it.
Is there a way to avoid the use of StringIO and get such output
without using such (useless) additional step?
If you just want to print the output (as in your example), you can use
file=sys.stdout or file=sys.stderr in the call to print_exc. If you
want to store the traceback into a string for some other purpose (e.g.
logging, email notifications) I believe that you must use a StringIO
object.
--
Evan Klitzke <e...@yelp.com>
Unfortunately I have to pass the output to a function which prints the
passed string both on screen and into a log file.
Anyway, not too much important. I'll use StringIO if there's no other
solution.

Turns out I was wrong -- you can just get the string, using format_exc
rather than print_exc.

--
Evan Klitzke <e...@yelp.com>- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -
Thanks

Aug 22 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Ryan Swift | last post: by
7 posts views Thread by Robin Becker | last post: by
16 posts views Thread by Daniel Klein | last post: by
8 posts views Thread by gregpinero | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.