Skip to content

Commit 9158932

Browse files
encukoupicnixz
andcommitted
Only cache the element at the given index
Co-Authored-By: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent be31685 commit 9158932

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

Lib/xml/etree/ElementPath.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,22 @@ def select_negated(context, result):
324324
index = -1
325325
def select(context, result):
326326
parent_map = get_parent_map(context)
327-
sibling_cache = {}
327+
cache = {}
328328
for elem in result:
329329
try:
330330
parent = parent_map[elem]
331+
except KeyError:
332+
continue
333+
key = (parent, elem.tag)
334+
if key not in cache:
331335
# FIXME: what if the selector is "*" ?
332-
cache_key = (parent, elem.tag)
333-
if cache_key not in sibling_cache:
334-
sibling_cache[cache_key] = list(parent.findall(elem.tag))
335-
elems = sibling_cache[cache_key]
336-
if elems[index] is elem:
337-
yield elem
338-
except (IndexError, KeyError):
339-
pass
336+
elems = parent.findall(elem.tag)
337+
try:
338+
cache[key] = elems[index]
339+
except IndexError:
340+
cache[key] = None
341+
if cache[key] is elem:
342+
yield elem
340343
return select
341344
raise SyntaxError("invalid predicate")
342345

0 commit comments

Comments
 (0)