Skip to content

Commit

Permalink
** now considers root element part of iteration (#250)
Browse files Browse the repository at this point in the history
* ** now considers root element part of iteration

* fix py2 test
  • Loading branch information
kurtbrose committed Jan 18, 2023
1 parent eea1510 commit 85a7a3a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion glom/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1602,12 +1602,13 @@ def _t_eval(target, _t, scope):
# TODO: so many try/except -- could scope[TargetRegistry] stuff be cached on type?
_extend_children(nxt, cur, get_handler)
elif op == 'X':
sofar = {id(cur)}
sofar = set()
_extend_children(nxt, cur, get_handler)
for item in nxt:
if id(item) not in sofar:
sofar.add(id(item))
_extend_children(nxt, item, get_handler)
nxt.insert(0, cur)
# handle the rest of the t_path in recursive calls
cur = []
todo = TType()
Expand Down
13 changes: 7 additions & 6 deletions glom/test/test_path_and_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,18 @@ def test_path_star():
# multi-paths eat errors
assert glom(val, Path('a', T.__star__(), T.b)) == []
val = [[[1]]]
assert glom(val, '**') == [ [[1]], [1], 1]
val = {'a': [{'b': [{'c': 1}, {'c': 2}, {'d': {'c': 3}}]}]}
assert glom(val, '**.c') == [1, 2, 3]
assert glom(val, '**') == [val, [[1]], [1], 1]
val = {'a': [{'b': [{'c': 1}, {'c': 2}, {'d': {'c': 3}}]}], 'c': 4}
assert glom(val, '**.c') == [4, 1, 2, 3]
assert glom(val, 'a.**.c') == [1, 2, 3]
assert glom(val, T['a'].__starstar__()['c']) == [1, 2, 3]
assert glom(val, 'a.*.b.*.c') == [[1, 2]]
# errors
class ErrDict(dict):
def __getitem__(key): 1/0
assert ErrDict(val).keys() # it will try to iterate
assert glom(ErrDict(val), '**') == []
assert glom(ErrDict(val), '**') == [val]
assert glom(ErrDict(val), '*') == []
# object access
class A:
def __init__(self):
Expand All @@ -248,10 +249,10 @@ def __init__(self):
val = A()
if core.PY2: # compensate for unpredictable attribute order
assert sorted(glom(val, '*')) == sorted([1, {'c': 2}])
assert sorted(glom(val, '**')) == sorted([1, {'c': 2}, 2])
assert sorted(glom(val, '**')) == sorted([val, 1, {'c': 2}, 2])
else:
assert glom(val, '*') == [1, {'c': 2}]
assert glom(val, '**') == [1, {'c': 2}, 2]
assert glom(val, '**') == [val, 1, {'c': 2}, 2]
core.PATH_STAR = False


Expand Down

0 comments on commit 85a7a3a

Please sign in to comment.