From aa8d421bbcf5e218c611e444d0297f6855148964 Mon Sep 17 00:00:00 2001 From: ptmcg Date: Sun, 14 Aug 2016 08:49:38 +0000 Subject: enhanced runTests to better handle non-parsing exceptions modified pyparsing_common.convertToDate and convertToDatetime parse actions to convert ValueErrors raised by strptime to ParseExceptions git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@419 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b --- src/pyparsing.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/pyparsing.py b/src/pyparsing.py index a99140c..8935cb8 100644 --- a/src/pyparsing.py +++ b/src/pyparsing.py @@ -58,7 +58,7 @@ The pyparsing module handles some of the problems that are typically vexing when """ __version__ = "2.1.8" -__versionTime__ = "13 Aug 2016 02:22 UTC" +__versionTime__ = "14 Aug 2016 08:43 UTC" __author__ = "Paul McGuire " import string @@ -2284,6 +2284,10 @@ class ParserElement(object): out.append("FAIL: " + str(pe)) success = success and failureTests result = pe + except Exception as exc: + out.append("FAIL-EXCEPTION: " + str(exc)) + success = success and failureTests + result = exc if printResults: if fullDump: @@ -5443,7 +5447,12 @@ class pyparsing_common: prints:: [datetime.date(1999, 12, 31)] """ - return lambda s,l,t: datetime.strptime(t[0], fmt).date() + def cvt_fn(s,l,t): + try: + return datetime.strptime(t[0], fmt).date() + except ValueError as ve: + raise ParseException(s, l, str(ve)) + return cvt_fn @staticmethod def convertToDatetime(fmt="%Y-%m-%dT%H:%M:%S.%f"): @@ -5460,7 +5469,12 @@ class pyparsing_common: prints:: [datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)] """ - return lambda s,l,t: datetime.strptime(t[0], fmt) + def cvt_fn(s,l,t): + try: + return datetime.strptime(t[0], fmt) + except ValueError as ve: + raise ParseException(s, l, str(ve)) + return cvt_fn iso8601_date = Regex(r'(?P\d{4})(?:-(?P\d\d)(?:-(?P\d\d))?)?').setName("ISO8601 date") "ISO8601 date (C{yyyy-mm-dd})" -- cgit v1.2.1