473,895 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.String IO()
traceback.print _exc(file=f)
print f.getvalue()

.... but this seems a little poor to me since I first put output into
the StringIO.String IO(), 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 2014
On 8/21/07, billiejoex <gn****@gmail.c omwrote:
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.String IO()
traceback.print _exc(file=f)
print f.getvalue()

... but this seems a little poor to me since I first put output into
the StringIO.String IO(), 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.co m>
Aug 22 '07 #2
On 22 Ago, 02:09, "Evan Klitzke" <e...@yelp.comw rote:
On 8/21/07, billiejoex <gne...@gmail.c omwrote:
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.String IO()
traceback.print _exc(file=f)
print f.getvalue()
... but this seems a little poor to me since I first put output into
the StringIO.String IO(), 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.co m>
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********@gma il.comwrote:
On 22 Ago, 02:09, "Evan Klitzke" <e...@yelp.comw rote:
On 8/21/07, billiejoex <gne...@gmail.c omwrote:
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.String IO()
traceback.print _exc(file=f)
print f.getvalue()
... but this seems a little poor to me since I first put output into
the StringIO.String IO(), 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.co m>

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.co m>
Aug 22 '07 #4
On 22 Ago, 03:11, "Evan Klitzke" <e...@yelp.comw rote:
On 8/21/07, codesite-noreply <billiej...@gma il.comwrote:


On 22 Ago, 02:09, "Evan Klitzke" <e...@yelp.comw rote:
On 8/21/07, billiejoex <gne...@gmail.c omwrote:
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.String IO()
traceback.print _exc(file=f)
print f.getvalue()
... but this seems a little poor to me since I first put output into
the StringIO.String IO(), 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.co m>
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.co m>- 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
3762
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 web page, you can paste it into an app like Frontpage, or something Rich Text-aware, and it will preserve all the formatting, HTML, etc. I'd like to include that behavior in the application I'm writing. In the interactive session below, before I...
7
1864
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 the email header. Does anyone have an easy solution? Thanks in advance.
7
2241
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 'traceback\n',''.join(traceback.format_exception(tbt,tbv,tb))
16
2396
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 way to 'extract' a dictionary into the current namespace? That is, if you have {'foo' : 23, 'bar' : 42} you would get a variable foo with value 23 and a variable bar with value
1
2566
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 source of both methods. One uses f_lineno (wrong)
1
1772
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 extracted files under this dir> The purpose of this script is to retrieve backup files which are individually zipped under a backup directory tree on a backup server.
6
4461
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 some one experience with other libaries to
8
2011
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 interactive interpreter does, but only show the part of the traceback relating to the code sent to exec. For example here is the code I'm using: try: exec code
14
2991
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 PyCObject_FromVoidPtr the code that creates the PyCObject is as follows: tmp = PyCObject_FromVoidPtr (info.info.x11.display, NULL); PyDict_SetItemString (dict, "display", tmp); Py_DECREF (tmp);
0
9988
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9833
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11247
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10841
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9648
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8025
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7175
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5860
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4684
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.