Skip to content

Commit

Permalink
Fix SyntaxError in Element.iterfind() that should have been a warning.
Browse files Browse the repository at this point in the history
  • Loading branch information
scoder committed Apr 2, 2024
1 parent 175c66a commit 9b8e36d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/lxml/etree.pyx
Expand Up @@ -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 "
Expand Down
31 changes: 26 additions & 5 deletions src/lxml/tests/test_elementpath.py
Expand Up @@ -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:
Expand All @@ -289,6 +284,32 @@ def test_find(self):
self.assertEqual(summarize_list(elem.findall(".//tag[@class][@id]")),
['tag', 'tag'])

def test_find_warning(self):
elem = etree.XML("""
<body>
<tag class='a'>text</tag>
<tag class='b' />
<section>
<tag class='b' id='inner'>subtext</tag>
</section>
</body>
""")

# 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).iterfind, "/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
Expand Down

0 comments on commit 9b8e36d

Please sign in to comment.