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

Discussion in 'Python' started by Paul McGuire, Nov 7, 2008.

  1. Paul McGuire

    Paul McGuire Guest

    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
     
    Paul McGuire, Nov 7, 2008
    #1
    1. Advertising

  2. Paul McGuire

    alex23 Guest

    On Nov 8, 1:25 am, Paul McGuire <> wrote:
    > 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.
     
    alex23, Nov 7, 2008
    #2
    1. Advertising

  3. Paul McGuire

    Peter Otten Guest

    Paul McGuire wrote:

    > 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
     
    Peter Otten, Nov 7, 2008
    #3
  4. Paul McGuire

    Paul McGuire Guest

    On Nov 7, 10:30 am, Peter Otten <> wrote:
    > Paul McGuire wrote:
    > > 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


    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
     
    Paul McGuire, Nov 7, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus Dehmann

    Exception stack trace

    Markus Dehmann, Jul 5, 2004, in forum: C++
    Replies:
    7
    Views:
    9,995
    Pete Becker
    Jul 8, 2004
  2. TS
    Replies:
    4
    Views:
    965
  3. BCC
    Replies:
    1
    Views:
    431
    Alf P. Steinbach
    Sep 17, 2005
  4. laredotornado
    Replies:
    4
    Views:
    1,325
    Roedy Green
    Jul 29, 2008
  5. henq
    Replies:
    1
    Views:
    136
    Peter Scott
    Jan 2, 2004
Loading...

Share This Page