Skip to content

Commit

Permalink
make PathAccessError.path a Path() object once more, and test as much.
Browse files Browse the repository at this point in the history
…Fixes #185.
  • Loading branch information
mahmoud committed Aug 11, 2020
1 parent f85bff6 commit 0d6a45b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 4 additions & 4 deletions glom/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ def _s_first_magic(scope, key, _t):
try:
cur = scope[key]
except KeyError as e:
err = PathAccessError(e, _t, 0) # always only one level depth, hence 0
err = PathAccessError(e, Path(_t), 0) # always only one level depth, hence 0
if err:
raise err
return cur
Expand Down Expand Up @@ -1499,19 +1499,19 @@ def _t_eval(target, _t, scope):
try:
cur = getattr(cur, arg)
except AttributeError as e:
pae = PathAccessError(e, _t, i // 2)
pae = PathAccessError(e, Path(_t), i // 2)
elif op == '[':
try:
cur = cur[arg]
except (KeyError, IndexError, TypeError) as e:
pae = PathAccessError(e, _t, i // 2)
pae = PathAccessError(e, Path(_t), i // 2)
elif op == 'P':
# Path type stuff (fuzzy match)
get = scope[TargetRegistry].get_handler('get', cur, path=t_path[2:i+2:2])
try:
cur = get(cur, arg)
except Exception as e:
pae = PathAccessError(e, _t, i // 2)
pae = PathAccessError(e, Path(_t), i // 2)
elif op == '(':
args, kwargs = arg
scope[Path] += t_path[2:i+2:2]
Expand Down
14 changes: 13 additions & 1 deletion glom/test/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

from glom import glom, S, T, GlomError, Switch, Coalesce, Or
from glom import glom, S, T, GlomError, Switch, Coalesce, Or, Path
from glom.core import format_oneline_trace, format_target_spec_trace, bbrepr, ROOT, LAST_CHILD_SCOPE
from glom.matching import M, MatchError, TypeMatchError, Match

Expand Down Expand Up @@ -35,6 +35,18 @@ def test_error():
glom(target, ('data', [(T.real, T.bit_length, T.image)]))


def test_pae_api():
target = {'value': {'data': [0, 1, 2]}}

with pytest.raises(GlomError) as exc_info:
glom(target, (T['value'], 'data.3'))

assert exc_info.value.path == Path('data', '3')
assert exc_info.value.path.__class__ is Path
assert exc_info.value.exc.__class__ is IndexError
assert exc_info.value.part_idx == 1


def test_unfinalized_glomerror_repr():
assert 'GlomError()' in repr(GlomError())

Expand Down

0 comments on commit 0d6a45b

Please sign in to comment.