Can I hide internal method calls from an exception stack trace?

P

Paul McGuire

Is there any way to hide portions of an exception stack trace? When
users get exceptions when using pyparsing, there are usually many
layers of pyparsing-internal stack messages that are not at all
helpful in diagnosing the problem - the intervening messages just
divert the user's attention from the most significant parts of the
stack, usually the user's call into my module, and the root exception
message. For instance, here is a stack trace:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 1065, in
parseString
loc, tokens = self._parse( instring, 0 )
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 998, in
_parseCache
value = self._parseNoCache( instring, loc, doActions,
callPreParse )
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 941, in
_parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 2577, in
parseImpl
return self.expr._parse( instring, loc, doActions,
callPreParse=False )
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 998, in
_parseCache
value = self._parseNoCache( instring, loc, doActions,
callPreParse )
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 941, in
_parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 2325, in
parseImpl
loc, exprtokens = e._parse( instring, loc, doActions )
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 998, in
_parseCache
value = self._parseNoCache( instring, loc, doActions,
callPreParse )
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 941, in
_parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 2577, in
parseImpl
return self.expr._parse( instring, loc, doActions,
callPreParse=False )
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 998, in
_parseCache
value = self._parseNoCache( instring, loc, doActions,
callPreParse )
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 943, in
_parseNoCache
raise ParseException( instring, len(instring), self.errmsg, self )
pyparsing.ParseException: Expected ")" (at char 82), (line:1, col:83)

The only helpful content here is just this much:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyparsing.ParseException: Expected ")" (at char 82), (line:1, col:83)

That is, the point in the program where the user's code called
parseString, and the exception raised deep down in the recursive
parser that determined there was a missing closing paren in the input
string being parsed.

Is there any way for me to suppress these non-value-added API-internal
traceback levels?

-- Paul
 
A

alex23

Is there any way for me to suppress these non-value-added API-internal
traceback levels?

Hey Paul,

Have you taken a look at the traceback module?

print_tb(sys.last_traceback, <limit>) or
extract_tb(sys.last_traceback, limit) could do the trick.
 
P

Peter Otten

Paul said:
Is there any way to hide portions of an exception stack trace?  When

Add a try...except at the appropriate level. Why do you want to do anything
more complex?

Peter
 
P

Paul McGuire

Add a try...except at the appropriate level. Why do you want to do anything
more complex?

Peter

I thought I tried that, and now in retrying, I learned a little about
exceptions.

My first attempt was to use this code in the entry method of the API
(parseString):

try:
loc, tokens = self._parse( instring, 0 )
if parseAll:
loc = self.preParse( instring, loc )
StringEnd()._parse( instring, loc )
except ParseBaseException, exc:
raise
else:
return tokens

This didn't change the stack trace at all. But when I change it to
this:

try:
loc, tokens = self._parse( instring, 0 )
if parseAll:
loc = self.preParse( instring, loc )
StringEnd()._parse( instring, loc )
except ParseBaseException, exc:
raise exc
else:
return tokens

Now the stack trace only shows my top-level API method. (I've also
added a little self-explanatory comment as to why I am catching an
exception, just to raise it again.)

Thanks for the nudge in the right direction,
-- Paul
 

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
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top