gh-132637: Fix positional predicates in xpath when a default namespace is provided #132822
+77
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
xpaths with a positional predicate (
.//b[1]
) would fail when the namespaces argument offind()
orfindall()
supplied a default namespace.This was due to
xpath_tokenizer()
erroneously prepending the default namespace to these positional predicates..//b[1]
would have a positional predicate of{the-supplied-default-namespace}1
and.//b[last()]
would be{the-supplied-default-namespace}last()
. The integer case would cause the xpath not to match the intended elements and thelast()
predicate would raise an exception as the xpath function{the-supplied-default-namespace}last()
is not supported (because it doesn't exist).This PR fixes the issue by altering the regex that parses the xpath expression to pick out
last()
explicitly as that is the only function that is supported in Python's imlementation and we only know that it is a function, rather than a tag name, when we reach()
so it was easier to grablast
and()
together which were previously parsed separately (the generator still returns them separately for compatibility).We also test if the tag is a number possibly preceded by a
-
or+
and exclude prepending the namespace in those cases.Ultimately a comment in the code suggests the proper fix:
cpython/Lib/xml/etree/ElementPath.py
Lines 228 to 229 in b5bf8c8
but despite the known preferences of some contributors, replacing this regex parser with a real parser seems unlikely at this time.