Inconsistent behavior deleting slice objects
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
lxml |
Fix Released
|
Low
|
scoder |
Bug Description
When deleting child elements from a parent element using `del parent[x]`, there is an inconsistency in behavior depending on whether x is an integer or a slice object.
For example, starting with:
from lxml.etree import Element
parent = Element('div')
child = Element('span')
child.tail = 'tail-text'
parent.
Then:
del parent[0]
print(child.tail) # outputs: None
But:
del parent[slice(0, 1)])]
print(child.tail) # outputs: 'tail-text'
It seems like both should have identical behavior and/or be using the same code path. It could also be a sign that something else might not be right.
Here is the version info:
sys.version_
lxml.etree : (4, 3, 4, 0)
libxml used : (2, 9, 9)
libxml compiled : (2, 9, 9)
libxslt used : (1, 1, 33)
libxslt compiled : (1, 1, 33)
Changed in lxml: | |
status: | Fix Committed → Fix Released |
Here is the code for _Element. __delitem_ _():
https:/ /github. com/lxml/ lxml/blob/ caf911e43194502 c06f02be992abe9 efd3fc19cd/ src/lxml/ etree.pyx# L766-L786
It looks like this issue might be because the "subelement" deletion case calls _removeText() in addition to _removeNode(), whereas the "slice" case only calls _removeNode():
https:/ /github. com/lxml/ lxml/blob/ caf911e43194502 c06f02be992abe9 efd3fc19cd/ src/lxml/ apihelpers. pxi#L1168- L1175
I haven't confirmed for sure. It's just a difference I noticed when looking at the code superficially.