Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions glom/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,9 +1662,15 @@ def _t_eval(target, _t, scope):
def _assign_op(dest, op, arg, val, path, scope):
"""helper method for doing the assignment on a T operation"""
if op == '[':
dest[arg] = val
try:
dest[arg] = val
except Exception as e:
raise PathAssignError(e, path, arg)
elif op == '.':
setattr(dest, arg, val)
try:
setattr(dest, arg, val)
except Exception as e:
raise PathAssignError(e, path, arg)
elif op == 'P':
_assign = scope[TargetRegistry].get_handler('assign', dest)
try:
Expand Down
14 changes: 14 additions & 0 deletions glom/test/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ def test_sequence_assign():
return


def test_assign_t_form_raises_path_assign_error():
# a failing assignment reached through a T-expression destination
# (setitem/setattr ops) should raise PathAssignError, just like the
# string/Path form already does. Previously these leaked the raw
# IndexError/TypeError/AttributeError wrapped as a generic GlomError.
with pytest.raises(PathAssignError, match='could not assign'):
glom(['short', 'list'], Assign(T[5], 'x'))
with pytest.raises(PathAssignError, match='could not assign'):
glom(('a', 'b'), Assign(T[0], 'x'))
with pytest.raises(PathAssignError, match='could not assign'):
glom(5, Assign(T.foo, 1))
return


def test_invalid_assign_op_target():
target = {'afunc': lambda x: 'hi %s' % x}
spec = T['afunc'](x=1)
Expand Down