[Python-checkins] cpython (merge default -> default): Merge.

stefan.krah python-checkins at python.org
Thu Jan 24 15:40:36 CET 2013


http://hg.python.org/cpython/rev/c6b0b96451c6
changeset:   81684:c6b0b96451c6
parent:      81683:364921b06c5f
parent:      81681:56a4561600ad
user:        Stefan Krah <skrah at bytereef.org>
date:        Thu Jan 24 15:39:55 2013 +0100
summary:
  Merge.

files:
  Lib/test/test_xml_etree.py   |  5 +++++
  Lib/xml/etree/ElementPath.py |  9 +++++++--
  2 files changed, 12 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -1793,6 +1793,11 @@
         self.assertEqual(e.find('./tag[last()-1]').attrib['class'], 'c')
         self.assertEqual(e.find('./tag[last()-2]').attrib['class'], 'b')
 
+        self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[0]')
+        self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[-1]')
+        self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[last()-0]')
+        self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[last()+1]')
+
     def test_findall(self):
         e = ET.XML(SAMPLE_XML)
         e[2] = ET.XML(SAMPLE_SECTION)
diff --git a/Lib/xml/etree/ElementPath.py b/Lib/xml/etree/ElementPath.py
--- a/Lib/xml/etree/ElementPath.py
+++ b/Lib/xml/etree/ElementPath.py
@@ -174,7 +174,7 @@
                 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 @@
                 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 @@
     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 @@
                     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):

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list