Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Last active July 18, 2020 23:31
Show Gist options
  • Save kurtbrose/e1af29ad65f4831cdace06c1ee5ea844 to your computer and use it in GitHub Desktop.
Save kurtbrose/e1af29ad65f4831cdace06c1ee5ea844 to your computer and use it in GitHub Desktop.
glom branch exception tracing
def _glom(spec, target, scope):
scope[CHILD_ERRORS] = []
try:
...
except Exception as e:
scope.parent[CHILD_ERRORS].append((scope, e))
raise
else:
del scope[CHILD_ERRORS]
# scope[CHILD_ERRORS][-1] is always the last branch
# scope[CHILD_ERRORS][:-1] are the side branches
assert glom(1, Or('a', 'b', 'c')) ==
"""
Target-Spec trace (most recent last):
- Target: 1
- Spec: Or('a', 'b', 'c')
Failed Branch:
- Spec: 'a'
glom.core.MatchError: 1 != 'a'
Failed Branch:
- Spec: 'b'
glom.core.MatchError: 1 != 'b'
- Spec: 'c'
glom.core.MatchError: 1 != 'c'
"""
# this may be worth reworking into debug helpers, depending on the amount of bugs in this area
def debug():
try:
glom([None], Match(Switch(
[(1, 1), ('a', 'a'), ([None], T.a)])))
except Exception as e:
import pprint
from glom.core import LAST_CHILD_SCOPE, CHILD_ERRORS, Spec, UP
print("")
def sub(s, key, sofar):
if key not in s:
return None
return scope_fmt(s[key].maps[0], sofar)
def multi_sub(s, key, sofar):
if key not in s:
return None
return [scope_fmt(sub_s.maps[0], sofar) for sub_s in s[key]]
sofar = set()
def scope_fmt(s, sofar):
if id(s) in sofar:
return "ref - " + str(id(s) % 1000)
sofar.add(id(s))
return {
"spec": s.get(Spec, "MISSING"),
"id": id(s) % 1000,
"CHILD_ERRORS": multi_sub(s, CHILD_ERRORS, sofar),
"LAST_CHILD_SCOPE": sub(s, LAST_CHILD_SCOPE, sofar),
"UP": sub(s, UP, sofar),
}
pprint.pprint([scope_fmt(s, sofar) for s in e._scope.maps[:-2]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment