diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-02 06:23:49 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-02 06:23:49 -0500 |
commit | 0d88a303a7f7e574bfc0c06ad6f84ca8c9d4d248 (patch) | |
tree | ba1c29a715148700bf1a10ee94e8db4186e3d7c8 /unitTests.py | |
parent | 639e64d5da626a12f01d077797587b0cc31c231b (diff) | |
download | pyparsing-git-0d88a303a7f7e574bfc0c06ad6f84ca8c9d4d248.tar.gz |
Fix tests for Py2 when using explain
Diffstat (limited to 'unitTests.py')
-rw-r--r-- | unitTests.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/unitTests.py b/unitTests.py index b1c4477..20847d4 100644 --- a/unitTests.py +++ b/unitTests.py @@ -3995,6 +3995,11 @@ class EmptyDictDoesNotRaiseException(ParseTestCase): try: print_(key_value_dict.parseString("").dump()) except pp.ParseException as pe: + exc = pe + if not hasattr(exc, '__traceback__'): + # Python 2 compatibility + etype, value, traceback = sys.exc_info() + exc.__traceback__ = traceback print_(pp.ParseException.explain(pe)) else: self.assertTrue(False, "failed to raise exception when matching empty string") @@ -4007,12 +4012,22 @@ class ExplainExceptionTest(ParseTestCase): try: expr.parseString("123 355") except pp.ParseException as pe: + exc = pe + if not hasattr(exc, '__traceback__'): + # Python 2 compatibility + etype, value, traceback = sys.exc_info() + exc.__traceback__ = traceback print_(pp.ParseException.explain(pe, depth=0)) expr = pp.Word(pp.nums).setName("int") - pp.Word(pp.alphas).setName("word") try: expr.parseString("123 355 (test using ErrorStop)") except pp.ParseSyntaxException as pe: + exc = pe + if not hasattr(exc, '__traceback__'): + # Python 2 compatibility + etype, value, traceback = sys.exc_info() + exc.__traceback__ = traceback print_(pp.ParseException.explain(pe)) integer = pp.Word(pp.nums).setName("int").addParseAction(lambda t: int(t[0])) @@ -4030,8 +4045,17 @@ class ExplainExceptionTest(ParseTestCase): try: expr.parseString("123 0") except pp.ParseException as pe: + exc = pe + if not hasattr(exc, '__traceback__'): + # Python 2 compatibility + etype, value, traceback = sys.exc_info() + exc.__traceback__ = traceback print_(pp.ParseException.explain(pe)) except Exception as exc: + if not hasattr(exc, '__traceback__'): + # Python 2 compatibility + etype, value, traceback = sys.exc_info() + exc.__traceback__ = traceback print_(pp.ParseException.explain(exc)) raise |