diff options
author | ptmcg <ptmcg@austin.rr.com> | 2018-12-28 14:55:39 -0600 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2018-12-28 14:55:39 -0600 |
commit | c50c84bac138f176259cf745e628000fd8539330 (patch) | |
tree | 5151f3d22bee45609a9824d91c339613ae1cd8f8 | |
parent | 45b78401e8c224619e1b18b9cf43fc02b196676e (diff) | |
download | pyparsing-git-c50c84bac138f176259cf745e628000fd8539330.tar.gz |
Fix matching of dictOf with empty contents (Issue #53)
-rw-r--r-- | CHANGES | 3 | ||||
-rw-r--r-- | pyparsing.py | 4 | ||||
-rw-r--r-- | unitTests.py | 19 |
3 files changed, 24 insertions, 2 deletions
@@ -12,6 +12,9 @@ Version 2.3.1 - changes in the tokens returned if you have this condition in your pyparsing scripts. +- Fix bug in dictOf which could match an empty sequence, making it + infinitely loop if wrapped in a OneOrMore. + - Added unicode sets to pyparsing_unicode for Latin-A and Latin-B ranges. - Added ability to define custom unicode sets as combinations of other sets diff --git a/pyparsing.py b/pyparsing.py index 48e5e23..975846c 100644 --- a/pyparsing.py +++ b/pyparsing.py @@ -94,7 +94,7 @@ classes inherit from. Use the docstrings for examples of how to: """ __version__ = "2.3.1" -__versionTime__ = "28 Dec 2018 20:39 UTC" +__versionTime__ = "28 Dec 2018 20:52 UTC" __author__ = "Paul McGuire <ptmcg@users.sourceforge.net>" import string @@ -5076,7 +5076,7 @@ def dictOf( key, value ): SQUARE {'color': 'light blue', 'shape': 'SQUARE', 'posn': 'upper left', 'texture': 'burlap'} """ - return Dict( ZeroOrMore( Group ( key + value ) ) ) + return Dict(OneOrMore(Group(key + value))) def originalTextFor(expr, asString=True): """Helper to return the original, untokenized text for a given diff --git a/unitTests.py b/unitTests.py index cd219bf..9d2731a 100644 --- a/unitTests.py +++ b/unitTests.py @@ -3851,6 +3851,25 @@ class ParseResultsWithNameOr(ParseTestCase): self.assertEqual(list(expr.parseString('not the bird')['rexp']), 'not the bird'.split())
self.assertEqual(list(expr.parseString('the bird')['rexp']), 'the bird'.split())
+class EmptyDictDoesNotRaiseException(ParseTestCase):
+ def runTest(self):
+ import pyparsing as pp
+
+ key = pp.Word(pp.alphas)
+ value = pp.Word(pp.nums)
+ EQ = pp.Suppress('=')
+ key_value_dict = pp.dictOf(key, EQ + value)
+ try:
+ print(key_value_dict.parseString("""\
+ a = 10
+ b = 20
+ """).dump())
+ print(key_value_dict.parseString("").dump())
+ except pp.ParseException:
+ pass
+ else:
+ self.assertTrue(False, "failed to raise exception when matching empty string")
+
class MiscellaneousParserTests(ParseTestCase):
def runTest(self):
|