diff options
author | ptmcg <ptmcg@austin.rr.com> | 2018-09-30 23:21:01 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2018-09-30 23:21:01 -0500 |
commit | c2842b1cae58c09c58110159c91ff06c1a22400b (patch) | |
tree | df263fc96fb7b983c84824be5895dea2f2415d3e /unitTests.py | |
parent | d5e471b87f2c9f0450ed9dc4895a0366ee81d74f (diff) | |
download | pyparsing-git-c2842b1cae58c09c58110159c91ff06c1a22400b.tar.gz |
Issue #4 - add special handling for IndexError exceptions raised in user parse actions
Diffstat (limited to 'unitTests.py')
-rw-r--r-- | unitTests.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/unitTests.py b/unitTests.py index 9966012..ba84898 100644 --- a/unitTests.py +++ b/unitTests.py @@ -3489,6 +3489,28 @@ class LiteralExceptionTest(ParseTestCase): print(cls.__name__, str(e))
assert isinstance(e, pp.ParseBaseException), "class {} raised wrong exception type {}".format(cls.__name__, type(e).__name__)
+class ParseActionExceptionTest(ParseTestCase):
+ def runTest(self):
+ import pyparsing as pp
+ import traceback
+
+ number = pp.Word(pp.nums)
+ def number_action():
+ raise IndexError # this is the important line!
+
+ number.setParseAction(number_action)
+ symbol = pp.Word('abcd', max=1)
+ expr = number | symbol
+ with self.assertRaises(pp.ParseException):
+ try:
+ expr.parseString('1 + 2')
+ except Exception as e:
+ assert hasattr(e, '__cause__'), "no __cause__ attribute in the raised exception"
+ assert e.__cause__ is not None, "__cause__ not propagated to outer exception"
+ assert type(e.__cause__) == IndexError, "__cause__ references wrong exception"
+ traceback.print_exc()
+ raise
+
class MiscellaneousParserTests(ParseTestCase):
def runTest(self):
import pyparsing
|