Skip to content

Commit 85a7a3a

Browse files
authored
** now considers root element part of iteration (#250)
* ** now considers root element part of iteration * fix py2 test
1 parent eea1510 commit 85a7a3a

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

glom/core.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1602,12 +1602,13 @@ def _t_eval(target, _t, scope):
16021602
# TODO: so many try/except -- could scope[TargetRegistry] stuff be cached on type?
16031603
_extend_children(nxt, cur, get_handler)
16041604
elif op == 'X':
1605-
sofar = {id(cur)}
1605+
sofar = set()
16061606
_extend_children(nxt, cur, get_handler)
16071607
for item in nxt:
16081608
if id(item) not in sofar:
16091609
sofar.add(id(item))
16101610
_extend_children(nxt, item, get_handler)
1611+
nxt.insert(0, cur)
16111612
# handle the rest of the t_path in recursive calls
16121613
cur = []
16131614
todo = TType()

glom/test/test_path_and_t.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,18 @@ def test_path_star():
229229
# multi-paths eat errors
230230
assert glom(val, Path('a', T.__star__(), T.b)) == []
231231
val = [[[1]]]
232-
assert glom(val, '**') == [ [[1]], [1], 1]
233-
val = {'a': [{'b': [{'c': 1}, {'c': 2}, {'d': {'c': 3}}]}]}
234-
assert glom(val, '**.c') == [1, 2, 3]
232+
assert glom(val, '**') == [val, [[1]], [1], 1]
233+
val = {'a': [{'b': [{'c': 1}, {'c': 2}, {'d': {'c': 3}}]}], 'c': 4}
234+
assert glom(val, '**.c') == [4, 1, 2, 3]
235235
assert glom(val, 'a.**.c') == [1, 2, 3]
236236
assert glom(val, T['a'].__starstar__()['c']) == [1, 2, 3]
237237
assert glom(val, 'a.*.b.*.c') == [[1, 2]]
238238
# errors
239239
class ErrDict(dict):
240240
def __getitem__(key): 1/0
241241
assert ErrDict(val).keys() # it will try to iterate
242-
assert glom(ErrDict(val), '**') == []
242+
assert glom(ErrDict(val), '**') == [val]
243+
assert glom(ErrDict(val), '*') == []
243244
# object access
244245
class A:
245246
def __init__(self):
@@ -248,10 +249,10 @@ def __init__(self):
248249
val = A()
249250
if core.PY2: # compensate for unpredictable attribute order
250251
assert sorted(glom(val, '*')) == sorted([1, {'c': 2}])
251-
assert sorted(glom(val, '**')) == sorted([1, {'c': 2}, 2])
252+
assert sorted(glom(val, '**')) == sorted([val, 1, {'c': 2}, 2])
252253
else:
253254
assert glom(val, '*') == [1, {'c': 2}]
254-
assert glom(val, '**') == [1, {'c': 2}, 2]
255+
assert glom(val, '**') == [val, 1, {'c': 2}, 2]
255256
core.PATH_STAR = False
256257

257258

0 commit comments

Comments
 (0)