diff options
-rw-r--r-- | CHANGES | 4 | ||||
-rw-r--r-- | pyparsing.py | 14 | ||||
-rw-r--r-- | unitTests.py | 14 |
3 files changed, 29 insertions, 3 deletions
@@ -9,6 +9,10 @@ Version 2.4.1 - example, which makes the bug-fixing process a lot easier, thanks! +- Modified setParseAction to accept None as an argument, + indicating that all previously-defined parse actions for the + expression should be cleared. + - Modified runTests to call postParse function before dumping out the parsed results - allows for postParse to add further results, such as indications of additional validation success/failure. diff --git a/pyparsing.py b/pyparsing.py index 4cb96df..b94040a 100644 --- a/pyparsing.py +++ b/pyparsing.py @@ -96,7 +96,7 @@ classes inherit from. Use the docstrings for examples of how to: """ __version__ = "2.4.1" -__versionTime__ = "29 Jun 2019 05:25 UTC" +__versionTime__ = "29 Jun 2019 06:17 UTC" __author__ = "Paul McGuire <ptmcg@users.sourceforge.net>" import string @@ -1418,6 +1418,9 @@ class ParserElement(object): value from fn, and the modified list of tokens will replace the original. Otherwise, fn does not need to return any value. + If None is passed as the parse action, all previously added parse actions for this + expression are cleared. + Optional keyword arguments: - callDuringTry = (default= ``False`` ) indicate if parse action should be run during lookaheads and alternate testing @@ -1441,8 +1444,13 @@ class ParserElement(object): # note that integer fields are now ints, not strings date_str.parseString("1999/12/31") # -> [1999, '/', 12, '/', 31] """ - self.parseAction = list(map(_trim_arity, list(fns))) - self.callDuringTry = kwargs.get("callDuringTry", False) + if list(fns) == [None, ]: + self.parseAction = None + else: + if not all(callable(fn) for fn in fns): + raise TypeError("parse actions must be callable") + self.parseAction = list(map(_trim_arity, list(fns))) + self.callDuringTry = kwargs.get("callDuringTry", False) return self def addParseAction( self, *fns, **kwargs ): diff --git a/unitTests.py b/unitTests.py index e14016a..4c6c9ab 100644 --- a/unitTests.py +++ b/unitTests.py @@ -3009,6 +3009,20 @@ class TrimArityExceptionMaskingTest2(ParseTestCase): K() + +class ClearParseActionsTest(ParseTestCase): + def runTest(self): + import pyparsing as pp + ppc = pp.pyparsing_common + + realnum = ppc.real() + self.assertEqual(realnum.parseString("3.14159")[0], 3.14159, "failed basic real number parsing") + + # clear parse action that converts to float + realnum.setParseAction(None) + self.assertEqual(realnum.parseString("3.14159")[0], "3.14159", "failed clearing parse action") + + class OneOrMoreStopTest(ParseTestCase): def runTest(self): from pyparsing import (Word, OneOrMore, alphas, Keyword, CaselessKeyword, |