473,324 Members | 2,567 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,324 software developers and data experts.

Prepending to traceback

Hi!

I'm writing a parser using pyparsing and I would like to augment the
ParserException tracebacks with information about the actual error line *in
the parsed text*. Pyparsing provides me with everything I need (parsed line
and column), but is there a way to push that information on the traceback?
To make it a bit clearer, tracebacks currently look like this:

Traceback (most recent call last):
...
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 456, in parse
loc,tokens = self.parseImpl( instring, loc, doActions )
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 727, in parseImpl
raise exc
ParseException: Expected "SOMEOTHERTERM" (146), (5,9)
I want them to look like this:

Traceback (most recent call last):
...
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 456, in parse
loc,tokens = self.parseImpl( instring, loc, doActions )
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 727, in parseImpl
raise exc
PyParsing, line 5, in SomeStatement
PARSER TEST FOR TESTING MISSING TERM
ParseException: Expected "SOMEOTHERTERM" (146), (5,9)
where "PARSER TEST FOR TESTING MISSING TERM" is a line parsed by pyparsing
that leads to raising a ParseException. I wouldn't mind too much not having
the original traceback, though I'd prefer it. I remember having read somewhere
that there is a way to embed an exceptions in another one, that might help.

How can I alter or create such Tracebacks?

Thanks,
Stefan
Jul 18 '05 #1
3 1475
Am Wed, 02 Feb 2005 13:55:24 +0100 schrieb Stefan Behnel:
Hi!

I'm writing a parser using pyparsing and I would like to augment the
ParserException tracebacks with information about the actual error line *in
the parsed text*. Pyparsing provides me with everything I need (parsed line
and column), but is there a way to push that information on the traceback?


Hi,

have a look at the source of ParseException. I guess there
is attribute which holds the string. If the attribute is "msg",
you could do it like this:

try:
....
except ParseException, e:
e.msg="Textbefore %s" % e.msg
raise e

(code is not tested)

Thomas
--
Thomas Güttler, http://www.thomas-guettler.de/
Jul 18 '05 #2
Replace system exception hook with your on function that gets
called when exception is raised (not fully tested):

#
# Define a function that is called when system exception happens
#
def excepthook(self, type, value, tb):
#
# This function allows the user to redefine what happens if the program
# aborts due to an uncaught exception.
import traceback
#
# Prepend your lines to tblines list here
#
tblines=['PyParsing, line 5, in SomeStatement\n',
'PARSER TEST FOR TESTING MISSING TERM\n']

#
# Get traceback lines and append the current session log
#
tblines.extend(traceback.format_exception(type, value, tb))
map(sys.stdout.writelines, tblines) # Always write exceptions to screen
sys.exit(2)

#
# At top of your main program:
# Set the sys.excepthook so I can clean up properly if main program aborts
#
sys.excepthook=excepthook

Larry Bates

Stefan Behnel wrote:
Hi!

I'm writing a parser using pyparsing and I would like to augment the
ParserException tracebacks with information about the actual error line
*in the parsed text*. Pyparsing provides me with everything I need
(parsed line and column), but is there a way to push that information on
the traceback?
To make it a bit clearer, tracebacks currently look like this:

Traceback (most recent call last):
...
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 456, in parse
loc,tokens = self.parseImpl( instring, loc, doActions )
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 727, in
parseImpl
raise exc
ParseException: Expected "SOMEOTHERTERM" (146), (5,9)
I want them to look like this:

Traceback (most recent call last):
...
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 456, in parse
loc,tokens = self.parseImpl( instring, loc, doActions )
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 727, in
parseImpl
raise exc
PyParsing, line 5, in SomeStatement
PARSER TEST FOR TESTING MISSING TERM
ParseException: Expected "SOMEOTHERTERM" (146), (5,9)
where "PARSER TEST FOR TESTING MISSING TERM" is a line parsed by
pyparsing that leads to raising a ParseException. I wouldn't mind too
much not having the original traceback, though I'd prefer it. I remember
having read somewhere that there is a way to embed an exceptions in
another one, that might help.

How can I alter or create such Tracebacks?

Thanks,
Stefan

Jul 18 '05 #3
Stefan Behnel wrote:
I want them to look like this:

Traceback (most recent call last):
...
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 456, in parse
loc,tokens = self.parseImpl( instring, loc, doActions )
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 727, in
parseImpl
raise exc
PyParsing, line 5, in SomeStatement
PARSER TEST FOR TESTING MISSING TERM
ParseException: Expected "SOMEOTHERTERM" (146), (5,9)


This query's a little old now, but I found this through the Weekly URL.

Anyway, Zope's ExceptionFormatter has one answer to this; you put a
local variable like:

__traceback_info__ = ('PyParsing, line %i, in %s\n %s'
% (line, statement, message))

And then that will show up in the traceback, much like you want. The
formatter looks through all the frames for this local variable and
prints it out when found. It's used in Zope for showing line numbers
and other information when its interpreting scripts like Zope Page
Templates or Python Scripts -- so that instead of just seeing the
traceback from the interpreter, you also see information about what the
interpreter is doing. This sounds similar to what you want.

It's pretty simple to use and it doesn't depend on the rest of Zope:
http://cvs.zope.org/Products/ErrorRe...viewcvs-markup
--
Ian Bicking / ia**@colorstudy.com / http://blog.ianbicking.org
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oliver Walczak | last post by:
This seems to be a quite difficult approach. Try this: ##################################################################### import traceback class MyTraceback: def __init__(self):...
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...
1
by: Josh Close | last post by:
When sys.excepthook is called, type, value and traceback are passed into it. How do I get the values of traceback? I've tried printing it, but I get the memory location. I'm gussing there are...
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...
3
by: Tony | last post by:
Hi I am writing text to a fileusing the streamwriter writeline method. When creating the streamwriter it can be opened to append text to a file. Is there a way, not necessarily with...
5
by: Bob Greschke | last post by:
I want to cause any traceback output from my applications to show up in one of my dialog boxes, instead of in the command or terminal window (between running on Solaris, Linux, OSX and Windows...
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...
1
by: Sami Vaisanen | last post by:
Hello group, I'm trying to get the Python exception information (message and traceback) stored into a string in my C++ code. However all i get back is the string "None". All the checks pass and...
1
by: Sami Vaisanen | last post by:
This is becoming utterly painful process.... I found out that the return value from "format_exception" function is NOT a list, i.e. PyList_Check() fails. PySequence_Check() succeeds but then...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.