summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2020-03-29 19:37:18 -0500
committerptmcg <ptmcg@austin.rr.com>2020-03-29 19:37:18 -0500
commitfcbad044c73a0c4006edd34e5586d8a60774ddf4 (patch)
tree67116b88f005123d6cf7b40d35d6e68d56d37377
parent96fe2688898c1846ea97e2039659b98ed6f71e4e (diff)
downloadpyparsing-git-fcbad044c73a0c4006edd34e5586d8a60774ddf4.tar.gz
Fix potential FutureWarning with generated regex; minor reformat of runTests output to break at test comments if any
-rw-r--r--CHANGES6
-rw-r--r--pyparsing/core.py44
-rw-r--r--pyparsing/util.py8
3 files changed, 35 insertions, 23 deletions
diff --git a/CHANGES b/CHANGES
index 822d220..fa47202 100644
--- a/CHANGES
+++ b/CHANGES
@@ -166,6 +166,12 @@ Version 3.0.0a1
- Fixed bug in regex definitions for real and sci_real expressions in
pyparsing_common. Issue #194, reported by Michael Wayne Goodman, thanks!
+- Fixed FutureWarning raised beginning in Python 3.7 for Regex expressions
+ containing '[' within a regex set.
+
+- Minor reformatting of output from runTests to make embedded
+ comments more visible.
+
Version 2.4.6 - December, 2019
------------------------------
diff --git a/pyparsing/core.py b/pyparsing/core.py
index d9f277c..f24369c 100644
--- a/pyparsing/core.py
+++ b/pyparsing/core.py
@@ -54,7 +54,7 @@ str_type = (str, bytes)
#
__version__ = "3.0.0a1"
-__versionTime__ = "24 Feb 2020 02:17 UTC"
+__versionTime__ = "30 Mar 2020 00:24 UTC"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
@@ -130,9 +130,12 @@ hexnums = nums + "ABCDEFabcdef"
alphanums = alphas + nums
printables = "".join(c for c in string.printable if c not in string.whitespace)
+_trim_arity_call_line = None
+
def _trim_arity(func, maxargs=2):
"decorator to trim function calls to match the arity of the target"
+ global _trim_arity_call_line
if func in singleArgBuiltins:
return lambda s, l, t: func(t)
@@ -148,11 +151,16 @@ def _trim_arity(func, maxargs=2):
# synthesize what would be returned by traceback.extract_stack at the call to
# user's parse action 'func', so that we don't incur call penalty at parse time
- LINE_DIFF = 7
+ LINE_DIFF = 11
# IF ANY CODE CHANGES, EVEN JUST COMMENTS OR BLANK LINES, BETWEEN THE NEXT LINE AND
# THE CALL TO FUNC INSIDE WRAPPER, LINE_DIFF MUST BE MODIFIED!!!!
- this_line = traceback.extract_stack(limit=2)[-1]
- pa_call_line_synth = (this_line[0], this_line[1] + LINE_DIFF)
+ _trim_arity_call_line = (
+ _trim_arity_call_line or traceback.extract_stack(limit=2)[-1]
+ )
+ pa_call_line_synth = (
+ _trim_arity_call_line[0],
+ _trim_arity_call_line[1] + LINE_DIFF,
+ )
def wrapper(*args):
nonlocal found_arity, limit
@@ -161,25 +169,23 @@ def _trim_arity(func, maxargs=2):
ret = func(*args[limit:])
found_arity = True
return ret
- except TypeError:
+ except TypeError as te:
# re-raise TypeErrors if they did not come from our arity testing
if found_arity:
raise
else:
- try:
- tb = sys.exc_info()[-1]
- if not extract_tb(tb, limit=2)[-1][:2] == pa_call_line_synth:
- raise
- finally:
- try:
- del tb
- except NameError:
- pass
+ tb = te.__traceback__
+ trim_arity_type_error = (
+ extract_tb(tb, limit=2)[-1][:2] == pa_call_line_synth
+ )
+ del tb
- if limit <= maxargs:
- limit += 1
- continue
- raise
+ if trim_arity_type_error:
+ if limit <= maxargs:
+ limit += 1
+ continue
+
+ raise
# copy func name to wrapper for sensible debug output
func_name = "<parse action>"
@@ -1655,7 +1661,7 @@ class ParserElement:
continue
if not t:
continue
- out = ["\n".join(comments), t]
+ out = ["\n" + "\n".join(comments) if comments else "", t]
comments = []
try:
# convert newline marks to actual newlines, and strip leading BOM if present
diff --git a/pyparsing/util.py b/pyparsing/util.py
index a80a6d3..0d5af76 100644
--- a/pyparsing/util.py
+++ b/pyparsing/util.py
@@ -43,7 +43,7 @@ def col(loc, strg):
Note: the default parsing behavior is to expand tabs in the input string
before starting the parsing process. See
:class:`ParserElement.parseString` for more
- information on parsing strings containing ``<TAB>``\ s, and suggested
+ information on parsing strings containing ``<TAB>`` s, and suggested
methods to maintain a consistent view of the parsed string, the parse
location, and line and column positions within the parsed string.
"""
@@ -57,7 +57,7 @@ def lineno(loc, strg):
Note - the default parsing behavior is to expand tabs in the input string
before starting the parsing process. See :class:`ParserElement.parseString`
- for more information on parsing strings containing ``<TAB>``\ s, and
+ for more information on parsing strings containing ``<TAB>`` s, and
suggested methods to maintain a consistent view of the parsed string, the
parse location, and line and column positions within the parsed string.
"""
@@ -126,8 +126,8 @@ class _FifoCache:
def _escapeRegexRangeChars(s):
- # ~ escape these chars: ^-]
- for c in r"\^-]":
+ # escape these chars: ^-[]
+ for c in r"\^-[]":
s = s.replace(c, _bslash + c)
s = s.replace("\n", r"\n")
s = s.replace("\t", r"\t")