summaryrefslogtreecommitdiffstats
path: root/Lib/xml
diff options
context:
space:
mode:
authorEli Bendersky <eliben@gmail.com>2013-01-24 14:29:26 (GMT)
committerEli Bendersky <eliben@gmail.com>2013-01-24 14:29:26 (GMT)
commit5c6198b3fdecc12cfb6b5bec4d83a1f0ba422b12 (patch)
tree34ff1c2e2f874bf8103dc01a7b01e962a092c737 /Lib/xml
parent7ff241d830ff3a840fea08aeeaf83c6535338fae (diff)
downloadcpython-5c6198b3fdecc12cfb6b5bec4d83a1f0ba422b12.zip
cpython-5c6198b3fdecc12cfb6b5bec4d83a1f0ba422b12.tar.gz
cpython-5c6198b3fdecc12cfb6b5bec4d83a1f0ba422b12.tar.bz2
Issue #12323: Strengthen error checking of the position XPath selectors
Diffstat (limited to 'Lib/xml')
-rw-r--r--Lib/xml/etree/ElementPath.py9
1 files changed, 7 insertions, 2 deletions
diff --git a/Lib/xml/etree/ElementPath.py b/Lib/xml/etree/ElementPath.py
index 341dac0..bf984b9 100644
--- a/Lib/xml/etree/ElementPath.py
+++ b/Lib/xml/etree/ElementPath.py
@@ -174,7 +174,7 @@ def prepare_predicate(next, token):
if elem.get(key) == value:
yield elem
return select
- if signature == "-" and not re.match("\d+$", predicate[0]):
+ if signature == "-" and not re.match("\-?\d+$", predicate[0]):
# [tag]
tag = predicate[0]
def select(context, result):
@@ -182,7 +182,7 @@ def prepare_predicate(next, token):
if elem.find(tag) is not None:
yield elem
return select
- if signature == "-='" and not re.match("\d+$", predicate[0]):
+ if signature == "-='" and not re.match("\-?\d+$", predicate[0]):
# [tag='value']
tag = predicate[0]
value = predicate[-1]
@@ -196,7 +196,10 @@ def prepare_predicate(next, token):
if signature == "-" or signature == "-()" or signature == "-()-":
# [index] or [last()] or [last()-index]
if signature == "-":
+ # [index]
index = int(predicate[0]) - 1
+ if index < 0:
+ raise SyntaxError("XPath position >= 1 expected")
else:
if predicate[0] != "last":
raise SyntaxError("unsupported function")
@@ -205,6 +208,8 @@ def prepare_predicate(next, token):
index = int(predicate[2]) - 1
except ValueError:
raise SyntaxError("unsupported expression")
+ if index > -2:
+ raise SyntaxError("XPath offset from last() must be negative")
else:
index = -1
def select(context, result):