Skip to content

IndexErrors are handled differently than other exceptions #573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AugustKarlstedt opened this issue Oct 3, 2024 · 4 comments
Closed

IndexErrors are handled differently than other exceptions #573

AugustKarlstedt opened this issue Oct 3, 2024 · 4 comments
Assignees

Comments

@AugustKarlstedt
Copy link
Contributor

AugustKarlstedt commented Oct 3, 2024

When a parse action raises an IndexError, it does not bubble up the exception message as it would other types of exceptions. This makes it hard to debug how/why my parse action is failing if it raises an IndexError.

minimal example:

import pyparsing as pp

def parse_hello(
    _string: str, _location: int, _tokens: pp.ParseResults
) -> None:
    print(f"parsed {_tokens}")
    raise Exception('this works ok')

def parse_world(
    _string: str, _location: int, _tokens: pp.ParseResults
) -> None:
    print(f"parsed {_tokens}")
    raise IndexError('this does not')

hello = pp.Keyword("hello").set_parse_action(parse_hello)
world = pp.Keyword("world").set_parse_action(parse_world)
parser = pp.ZeroOrMore(hello | world)

parser.run_tests([
    "hello",
    "world"
])

outputs

parsed ['hello']

hello
FAIL-EXCEPTION: Exception: this works ok
parsed ['world']

world
world
^
ParseException: Expected end of text, found 'world'  (at char 0), (line:1, col:1)
FAIL: Expected end of text, found 'world'  (at char 0), (line:1, col:1)

which properly bubbles up the exception raised when parsing "hello" but fails with an ambiguous error when parsing "world".

It should print FAIL-EXCEPTION: IndexError: this does not even if the parse action raises an IndexError

@ptmcg ptmcg self-assigned this Oct 3, 2024
ptmcg added a commit that referenced this issue Oct 3, 2024
@ptmcg
Copy link
Member

ptmcg commented Oct 3, 2024

Thanks - I actually had some code around this as being expected behavior, but giving it a re-think, I worked out how to keep parse action IndexErrors from getting misread as ParserElement.parseImpl IndexErrors. In the bargain, I also enhanced the run_tests output to now indicate if an exception is raised, if it is raised in a parse action, and reports the parse action by name. For your case, I now get this output:

parsed ['hello']


hello
FAIL-EXCEPTION (raised in parse action 'parse_hello'): Exception: this works ok
parsed ['world']


world
FAIL-EXCEPTION (raised in parse action 'parse_world'): IndexError: this does not

This fix will go out in the next release.

@ptmcg
Copy link
Member

ptmcg commented Oct 5, 2024

Please try again using 3.2.0rc1, which I just pushed to PyPI.

@AugustKarlstedt
Copy link
Contributor Author

Just tested in a non-trivial codebase and the error message now shows exactly where the underlying IndexError happens. Much appreciated for the improvements here!

@ptmcg
Copy link
Member

ptmcg commented Oct 13, 2024

Released in 3.2.0, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants