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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul McGuire

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

    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._parseNoCa che( 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._pars e( instring, loc, doActions,
    callPreParse=Fa lse )
    File "/usr/lib/python2.5/site-packages/pyparsing.py", line 998, in
    _parseCache
    value = self._parseNoCa che( 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._parseNoCa che( 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._pars e( instring, loc, doActions,
    callPreParse=Fa lse )
    File "/usr/lib/python2.5/site-packages/pyparsing.py", line 998, in
    _parseCache
    value = self._parseNoCa che( 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.Parse Exception: 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.Parse Exception: 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
  • alex23

    #2
    Re: Can I hide internal method calls from an exception stack trace?

    On Nov 8, 1:25 am, Paul McGuire <pt...@austin.r r.comwrote:
    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.la st_traceback, <limit>) or
    extract_tb(sys. last_traceback, limit) could do the trick.

    Comment

    • Peter Otten

      #3
      Re: Can I hide internal method calls from an exception stack trace?

      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



      Comment

      • Paul McGuire

        #4
        Re: Can I hide internal method calls from an exception stack trace?

        On Nov 7, 10:30 am, Peter Otten <__pete...@web. dewrote:
        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()._pa rse( instring, loc )
        except ParseBaseExcept ion, 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()._pa rse( instring, loc )
        except ParseBaseExcept ion, 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

        Comment

        Working...