summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2020-06-26 00:26:56 -0500
committerptmcg <ptmcg@austin.rr.com>2020-06-26 00:26:56 -0500
commit56dee11e32a41a5032fa77655a303339b59ac2a1 (patch)
tree7cc3421756b4b95437ca3b71b12f3c0b1e929662
parentcfef3dfef746a902fb87a65f05b2401f335024a7 (diff)
downloadpyparsing-git-56dee11e32a41a5032fa77655a303339b59ac2a1.tar.gz
Revert to Python 3.7.1 for most compatibility; rewrite explain() unit test to be more tolerant of variations in TypeError str formatting
-rw-r--r--.travis.yml2
-rw-r--r--tests/test_unit.py31
2 files changed, 23 insertions, 10 deletions
diff --git a/.travis.yml b/.travis.yml
index 96ace16..505f9f9 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -10,7 +10,7 @@ matrix:
env: TOXENV=py35
- python: 3.6
env: TOXENV=py36
- - python: 3.7.7
+ - python: 3.7
env: TOXENV=py37
- python: 3.8
env: TOXENV=py38
diff --git a/tests/test_unit.py b/tests/test_unit.py
index 317be2f..719bf04 100644
--- a/tests/test_unit.py
+++ b/tests/test_unit.py
@@ -7641,38 +7641,51 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
modder = Modifier()
+ # force an exception in the attached parse action
+ # integer has a parse action to convert to an int;
+ # this parse action should fail with a TypeError, since
+ # str.upper expects a str argument, not an int
grammar = ppc.integer().addParseAction(modder.modify_upper)
self_testcase_name = "tests.test_unit." + type(self).__name__
- # get Python version-specific TypeError str
- try:
- str.upper(1000)
- except TypeError as te:
- type_error_str = str(te)
-
try:
grammar.parseString("1000")
except Exception as e:
+ # extract the exception explanation
explain_str = ParseException.explain_exception(e)
print(explain_str)
+ explain_str_lines = explain_str.splitlines()
+
expected = [
- "TypeError: " + type_error_str,
self_testcase_name,
"pyparsing.core._WordRegex - integer",
"tests.test_unit.Modifier",
"pyparsing.results.ParseResults",
]
+
+ # verify the list of names shown in the explain "stack"
self.assertEqual(
expected,
- explain_str.splitlines()[-len(expected) :],
- "invalid explain str",
+ explain_str_lines[-len(expected) :],
+ msg="invalid explain str",
+ )
+
+ # check type of raised exception matches explain output
+ # (actual exception text varies by Python version, and even
+ # by how the exception is raised, so we can only check the
+ # type name)
+ exception_line = explain_str_lines[-(len(expected) + 1)]
+ self.assertTrue(
+ exception_line.startswith("TypeError:"),
+ msg="unexpected exception line ({!r})".format(exception_line),
)
def testMiscellaneousExceptionBits(self):
self_testcase_name = "tests.test_unit." + type(self).__name__
+ # force a parsing exception - match an integer against "ABC"
try:
pp.Word(pp.nums).parseString("ABC")
except pp.ParseException as pe: