Skip to content

Commit

Permalink
Code refactoring for other files (#522)
Browse files Browse the repository at this point in the history
* Minor code refactoring for other files
* Make identifier_chars a set
  • Loading branch information
InSyncWithFoo committed Nov 20, 2023
1 parent 8c8332d commit 0b6ec8e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 92 deletions.
66 changes: 34 additions & 32 deletions pyparsing/exceptions.py
Expand Up @@ -86,41 +86,43 @@ def explain_exception(exc, depth=16):
ret.append(" " * (exc.column - 1) + "^")
ret.append(f"{type(exc).__name__}: {exc}")

if depth > 0:
callers = inspect.getinnerframes(exc.__traceback__, context=depth)
seen = set()
for i, ff in enumerate(callers[-depth:]):
frm = ff[0]

f_self = frm.f_locals.get("self", None)
if isinstance(f_self, ParserElement):
if not frm.f_code.co_name.startswith(
("parseImpl", "_parseNoCache")
):
continue
if id(f_self) in seen:
continue
seen.add(id(f_self))

self_type = type(f_self)
ret.append(
f"{self_type.__module__}.{self_type.__name__} - {f_self}"
)

elif f_self is not None:
self_type = type(f_self)
ret.append(f"{self_type.__module__}.{self_type.__name__}")
if depth <= 0:
return "\n".join(ret)

callers = inspect.getinnerframes(exc.__traceback__, context=depth)
seen = set()
for ff in callers[-depth:]:
frm = ff[0]

f_self = frm.f_locals.get("self", None)
if isinstance(f_self, ParserElement):
if not frm.f_code.co_name.startswith(
("parseImpl", "_parseNoCache")
):
continue
if id(f_self) in seen:
continue
seen.add(id(f_self))

self_type = type(f_self)
ret.append(
f"{self_type.__module__}.{self_type.__name__} - {f_self}"
)

elif f_self is not None:
self_type = type(f_self)
ret.append(f"{self_type.__module__}.{self_type.__name__}")

else:
code = frm.f_code
if code.co_name in ("wrapper", "<module>"):
continue
else:
code = frm.f_code
if code.co_name in ("wrapper", "<module>"):
continue

ret.append(code.co_name)
ret.append(code.co_name)

depth -= 1
if not depth:
break
depth -= 1
if not depth:
break

return "\n".join(ret)

Expand Down
22 changes: 12 additions & 10 deletions pyparsing/helpers.py
Expand Up @@ -95,15 +95,17 @@ def match_previous_literal(expr: ParserElement) -> ParserElement:
rep = Forward()

def copy_token_to_repeater(s, l, t):
if t:
if len(t) == 1:
rep << t[0]
else:
# flatten t tokens
tflat = _flatten(t.as_list())
rep << And(Literal(tt) for tt in tflat)
else:
if not t:
rep << Empty()
return

if len(t) == 1:
rep << t[0]
return

# flatten t tokens
tflat = _flatten(t.as_list())
rep << And(Literal(tt) for tt in tflat)

expr.add_parse_action(copy_token_to_repeater, callDuringTry=True)
rep.set_name("(prev) " + str(expr))
Expand Down Expand Up @@ -230,7 +232,7 @@ def one_of(
if isequal(other, cur):
del symbols[i + j + 1]
break
elif masks(cur, other):
if masks(cur, other):
del symbols[i + j + 1]
symbols.insert(i, other)
break
Expand Down Expand Up @@ -787,7 +789,7 @@ def parseImpl(self, instring, loc, doActions=True):
pa: typing.Optional[ParseAction]
opExpr1: ParserElement
opExpr2: ParserElement
for i, operDef in enumerate(op_list):
for operDef in op_list:
opExpr, arity, rightLeftAssoc, pa = (operDef + (None,))[:4] # type: ignore[assignment]
if isinstance(opExpr, str_type):
opExpr = ParserElement._literalStringClass(opExpr)
Expand Down
85 changes: 45 additions & 40 deletions pyparsing/testing.py
Expand Up @@ -180,49 +180,54 @@ def assertRunTestResults(
"""
run_test_success, run_test_results = run_tests_report

if expected_parse_results is not None:
merged = [
(*rpt, expected)
for rpt, expected in zip(run_test_results, expected_parse_results)
]
for test_string, result, expected in merged:
# expected should be a tuple containing a list and/or a dict or an exception,
# and optional failure message string
# an empty tuple will skip any result validation
fail_msg = next(
(exp for exp in expected if isinstance(exp, str)), None
if expected_parse_results is None:
self.assertTrue(
run_test_success, msg=msg if msg is not None else "failed runTests"
)
return

merged = [
(*rpt, expected)
for rpt, expected in zip(run_test_results, expected_parse_results)
]
for test_string, result, expected in merged:
# expected should be a tuple containing a list and/or a dict or an exception,
# and optional failure message string
# an empty tuple will skip any result validation
fail_msg = next(
(exp for exp in expected if isinstance(exp, str)), None
)
expected_exception = next(
(
exp
for exp in expected
if isinstance(exp, type) and issubclass(exp, Exception)
),
None,
)
if expected_exception is not None:
with self.assertRaises(
expected_exception=expected_exception, msg=fail_msg or msg
):
if isinstance(result, Exception):
raise result
else:
expected_list = next(
(exp for exp in expected if isinstance(exp, list)), None
)
expected_exception = next(
(
exp
for exp in expected
if isinstance(exp, type) and issubclass(exp, Exception)
),
None,
expected_dict = next(
(exp for exp in expected if isinstance(exp, dict)), None
)
if expected_exception is not None:
with self.assertRaises(
expected_exception=expected_exception, msg=fail_msg or msg
):
if isinstance(result, Exception):
raise result
else:
expected_list = next(
(exp for exp in expected if isinstance(exp, list)), None
if (expected_list, expected_dict) != (None, None):
self.assertParseResultsEquals(
result,
expected_list=expected_list,
expected_dict=expected_dict,
msg=fail_msg or msg,
)
expected_dict = next(
(exp for exp in expected if isinstance(exp, dict)), None
)
if (expected_list, expected_dict) != (None, None):
self.assertParseResultsEquals(
result,
expected_list=expected_list,
expected_dict=expected_dict,
msg=fail_msg or msg,
)
else:
# warning here maybe?
print(f"no validation for {test_string!r}")
else:
# warning here maybe?
print(f"no validation for {test_string!r}")

# do this last, in case some specific test results can be reported instead
self.assertTrue(
Expand Down
14 changes: 4 additions & 10 deletions pyparsing/unicode.py
Expand Up @@ -102,17 +102,11 @@ def identbodychars(cls):
all characters in this range that are valid identifier body characters,
plus the digits 0-9, and · (Unicode MIDDLE DOT)
"""
return "".join(
sorted(
set(
cls.identchars
+ "0123456789·"
+ "".join(
[c for c in cls._chars_for_ranges if ("_" + c).isidentifier()]
)
)
)
identifier_chars = set(
c for c in cls._chars_for_ranges
if ("_" + c).isidentifier()
)
return "".join(sorted(identifier_chars | set(cls.identchars + "0123456789·")))

@_lazyclassproperty
def identifier(cls):
Expand Down

0 comments on commit 0b6ec8e

Please sign in to comment.