Skip to content

IndexErrors are handled differently than other exceptions #573

Closed
@AugustKarlstedt

Description

@AugustKarlstedt

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions