473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 1992
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Trader | last post by:
Hi, I'm trying to use Mark Hammond's win32clipboard module to extract more complex data than just plain ASCII text from the Windows clipboard. For instance, when you select all the content on...
7
by: Ryan Swift | last post by:
Hi, I'm new to Python, so this may be an easy solution. I'm having trouble extracting TIFF files from incoming emails. Actually, I think the root of my problem is that I'm having trouble reading...
7
by: Robin Becker | last post by:
def raise_an_error(): a = 3 b = 4 c = 0 try: a = a/c except: import sys, cgitb, traceback, inspect tbt,tbv,tb = sys.exc_info() print...
16
by: Daniel Klein | last post by:
Hello, I'm quite new to Python, and since a not-so-superficial look into the docs didn't answer my question (although it still feels quite basic), I decided to turn to this place: Is there a...
1
by: Thomas Guettler | last post by:
Hi, the line numbers of inspect.getinnerframes are different from traceback.format_exception. This results in wrong lines being shown in cgitb. An example is below. I looked at the...
1
by: Hari Sekhon | last post by:
Hi, I've written a script to run on windows to extract all zips under a given directory path to another directory path as such: python extractzips.py <fetch all zips under this dir> <put all...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
8
by: gregpinero | last post by:
I'm running code via the "exec in context" statement within a much larger program. What I would like to do is capture any possible errors and show a pretty traceback just like the Python...
14
by: Gordon Allott | last post by:
Hello :) The result of various incompatibilities has left me needing to somehow extract the address that a null pointer is pointing to with the null pointer being exposed to python via...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.