Absolute path allowed from ElementTree but fails to find root element

Bug #2043361 reported by masklinn
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lxml
Fix Released
Low
scoder

Bug Description

While absolute ElementPaths are forbidden on Element, lxml does allow them on ElementTree. However they are incorrect, as they fail to find the root element itself (unlike xpath).

CPython's elementtree has a similar issue however it triggers a FutureWarning

Test script:
```
import contextlib
from xml.etree import ElementTree as ET
from lxml import etree

doc = ET.fromstring("<a><b/></a>")
# raises SyntaxError
with contextlib.suppress(SyntaxError):
    print(doc.find("/a"))
    raise Exception()
# triggers FutureWarning: This search is broken in 1.3 and earlier, and will be fixed in a future version.
# does not find anything
print(ET.ElementTree(doc).find("/a"))

doc = etree.fromstring("<a><b/></a>")

# raises SyntaxError
with contextlib.suppress(SyntaxError):
    print(doc.find("/a"))
    raise Exception()

# doesn't find anything, no warning
print(doc.getroottree().find("/a"))
# by comparison xpath finds the root, whether from an Element or an
# ElementTree
print(doc.xpath("/a"))
print(doc.getroottree().xpath("/a"))
```

system info:
Python : sys.version_info(major=3, minor=12, micro=0, releaselevel='final', serial=0)
lxml.etree : (4, 9, 3, 0)
libxml used : (2, 10, 3)
libxml compiled : (2, 10, 3)
libxslt used : (1, 1, 38)
libxslt compiled : (1, 1, 38)

Tags: elementpath
Revision history for this message
scoder (scoder) wrote :

I agree that this is incorrect. I added the same FutureWarning as in Python's xml.etree. Changing this requires a deprecation period, and preferably CPython going there first.

https://github.com/lxml/lxml/commit/3a8caecc0f94d8e00ab116a4b32e57675140eb4d

Changed in lxml:
importance: Undecided → Low
milestone: none → 5.1.1
status: New → Fix Committed
scoder (scoder)
Changed in lxml:
status: Fix Committed → Fix Released
assignee: nobody → scoder (scoder)
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.