diff options
author | ptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b> | 2016-08-11 19:59:56 +0000 |
---|---|---|
committer | ptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b> | 2016-08-11 19:59:56 +0000 |
commit | 0d9b57fe119eb7ebe40a34d67044477c22cbec76 (patch) | |
tree | b0bec6d87e1ff4cac8e201ed888c3bac7b17294b | |
parent | fc4697b083158381451a713305dd5a56497928ba (diff) | |
download | pyparsing-0d9b57fe119eb7ebe40a34d67044477c22cbec76.tar.gz |
Fixed bug in ParserElement.inlineLiteralsUsing, causing infinite loop with Suppress;
added AutoReset context manager class to unit tests, for saving/resetting global state when tests need to change globals
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@412 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r-- | src/CHANGES | 6 | ||||
-rw-r--r-- | src/pyparsing.py | 9 | ||||
-rw-r--r-- | src/unitTests.py | 49 |
3 files changed, 61 insertions, 3 deletions
diff --git a/src/CHANGES b/src/CHANGES index 63fef0c..ee70235 100644 --- a/src/CHANGES +++ b/src/CHANGES @@ -2,6 +2,12 @@ Change Log
==========
+Version 2.1.8 -
+------------------------------
+- Fixed bug in ParserElement.inlineLiteralsUsing, causing infinite
+ loop with Suppress.
+
+
Version 2.1.7 -
------------------------------
- Fixed regression reported by Andrea Censi (surfaced in PyContracts
diff --git a/src/pyparsing.py b/src/pyparsing.py index b335f95..86d5c21 100644 --- a/src/pyparsing.py +++ b/src/pyparsing.py @@ -57,8 +57,8 @@ The pyparsing module handles some of the problems that are typically vexing when - embedded comments
"""
-__version__ = "2.1.7"
-__versionTime__ = "11 Aug 2016 07:29 UTC"
+__version__ = "2.1.8"
+__versionTime__ = "11 Aug 2016 19:09 UTC"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -3557,7 +3557,10 @@ class ParseElementEnhance(ParserElement): def __init__( self, expr, savelist=False ):
super(ParseElementEnhance,self).__init__(savelist)
if isinstance( expr, basestring ):
- expr = ParserElement._literalStringClass(expr)
+ if issubclass(ParserElement._literalStringClass, Token):
+ expr = ParserElement._literalStringClass(expr)
+ else:
+ expr = ParserElement._literalStringClass(Literal(expr))
self.expr = expr
self.strRepr = None
if expr is not None:
diff --git a/src/unitTests.py b/src/unitTests.py index cb53509..87b4beb 100644 --- a/src/unitTests.py +++ b/src/unitTests.py @@ -2956,6 +2956,55 @@ class ParseFatalExceptionTest(ParseTestCase): assert success, "bad handling of syntax error"
+class InlineLiteralsUsingTest(ParseTestCase):
+ def runTest(self):
+
+ from pyparsing import ParserElement, Suppress, Literal, CaselessLiteral, Word, alphas, oneOf, CaselessKeyword, nums
+
+ class AutoReset(object):
+ def __init__(self, ob, attrname):
+ self.ob = ob
+ self.save_attr = attrname
+ self.save_value = getattr(ob, attrname)
+
+ def __enter__(self):
+ pass
+
+ def __exit__(self, *args):
+ setattr(self.ob, self.save_attr, self.save_value)
+
+ with AutoReset(ParserElement, "_literalStringClass"):
+ ParserElement.inlineLiteralsUsing(Suppress)
+ wd = Word(alphas)
+ result = (wd + ',' + wd + oneOf("! . ?")).parseString("Hello, World!")
+ assert len(result) == 3, "inlineLiteralsUsing(Suppress) failed!"
+
+ ParserElement.inlineLiteralsUsing(Literal)
+ result = (wd + ',' + wd + oneOf("! . ?")).parseString("Hello, World!")
+ assert len(result) == 4, "inlineLiteralsUsing(Literal) failed!"
+
+ ParserElement.inlineLiteralsUsing(CaselessKeyword)
+ result = ("SELECT" + wd + "FROM" + wd).parseString("select color from colors")
+ assert result.asList() == "SELECT color FROM colors".split(), "inlineLiteralsUsing(CaselessKeyword) failed!"
+
+ ParserElement.inlineLiteralsUsing(CaselessLiteral)
+ result = ("SELECT" + wd + "FROM" + wd).parseString("select color from colors")
+ assert result.asList() == "SELECT color FROM colors".split(), "inlineLiteralsUsing(CaselessLiteral) failed!"
+
+ integer = Word(nums)
+ ParserElement.inlineLiteralsUsing(Literal)
+ date_str = integer("year") + '/' + integer("month") + '/' + integer("day")
+ result = date_str.parseString("1999/12/31")
+ assert result.asList() == ['1999', '/', '12', '/', '31'], "inlineLiteralsUsing(example 1) failed!"
+
+ # change to Suppress
+ ParserElement.inlineLiteralsUsing(Suppress)
+ date_str = integer("year") + '/' + integer("month") + '/' + integer("day")
+
+ result = date_str.parseString("1999/12/31") # -> ['1999', '12', '31']
+ assert result.asList() == ['1999', '12', '31'], "inlineLiteralsUsing(example 2) failed!"
+
+
class MiscellaneousParserTests(ParseTestCase):
def runTest(self):
import pyparsing
|