From 76fd4f944403852f39acfc8a858d0081100e03fe Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Tue, 2 Apr 2024 09:34:38 +0200 Subject: [PATCH 1/2] Fix SyntaxError in Element.iterfind() that should have been a warning. Closes https://bugs.launchpad.net/lxml/+bug/2059977 --- src/lxml/etree.pyx | 1 + src/lxml/tests/test_elementpath.py | 32 +++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/lxml/etree.pyx b/src/lxml/etree.pyx index 1c15fb29a..31b2c52da 100644 --- a/src/lxml/etree.pyx +++ b/src/lxml/etree.pyx @@ -2306,6 +2306,7 @@ cdef public class _ElementTree [ type LxmlElementTreeType, root = self.getroot() if _isString(path): if path[:1] == "/": + path = "." + path from warnings import warn warn( "This search incorrectly ignores the root element, and will be " diff --git a/src/lxml/tests/test_elementpath.py b/src/lxml/tests/test_elementpath.py index 23fb289f4..14d48e344 100644 --- a/src/lxml/tests/test_elementpath.py +++ b/src/lxml/tests/test_elementpath.py @@ -268,11 +268,6 @@ def test_find(self): self.assertEqual(summarize_list(etree.ElementTree(elem).findall("./tag")), ['tag', 'tag']) - # FIXME: ET's Path module handles this case incorrectly; this gives - # a warning in 1.3, and the behaviour will be modified in 1.4. - self.assertWarnsRegex( - FutureWarning, ".*If you rely on the current behaviour, change it to './tag'", - etree.ElementTree(elem).findall, "/tag") self.assertEqual(summarize_list(etree.ElementTree(elem).findall("/tag")), ['tag', 'tag']) # This would be correct: @@ -289,6 +284,33 @@ def test_find(self): self.assertEqual(summarize_list(elem.findall(".//tag[@class][@id]")), ['tag', 'tag']) + def test_find_warning(self): + etree = self.etree + elem = etree.XML(""" + + text + +
+ subtext +
+ + """) + + # FIXME: ET's Path module handles this case incorrectly; this gives + # a warning in 1.3, and the behaviour will be modified in the future. + self.assertWarnsRegex( + FutureWarning, ".*If you rely on the current behaviour, change it to './tag'", + etree.ElementTree(elem).findall, "/tag") + self.assertWarnsRegex( + FutureWarning, ".*If you rely on the current behaviour, change it to './tag'", + etree.ElementTree(elem).findtext, "/tag") + self.assertWarnsRegex( + FutureWarning, ".*If you rely on the current behaviour, change it to './tag'", + etree.ElementTree(elem).find, "/tag") + self.assertWarnsRegex( + FutureWarning, ".*If you rely on the current behaviour, change it to './tag'", + etree.ElementTree(elem).iterfind, "/tag") + class ElementTreeElementPathTestCase(EtreeElementPathTestCase): import xml.etree.ElementTree as etree From fcf00fbdac7d68fc86e9712f8efaa07f955d1e4c Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Tue, 2 Apr 2024 09:50:14 +0200 Subject: [PATCH 2/2] Update changelog. --- CHANGES.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index c4bfc1472..5cd454b87 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -2,6 +2,16 @@ lxml changelog ============== +5.1.2 (2024-??-??) +================== + +Bugs fixed +---------- + +* LP#2059977: ``Element.iterfind("//absolute_path")`` failed with a ``SyntaxError`` + where it should have issued a warning. + + 5.1.1 (2024-03-28) ==================