Inconsistent behavior deleting slice objects

Bug #1857794 reported by Chris Jerdonek
6
This bug affects 1 person
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.append(child)

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_info(major=3, minor=7, micro=5, releaselevel='final', serial=0)
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)

Revision history for this message
Chris Jerdonek (chris-jerdonek) wrote :

Here is the code for _Element.__delitem__():

https://github.com/lxml/lxml/blob/caf911e43194502c06f02be992abe9efd3fc19cd/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/caf911e43194502c06f02be992abe9efd3fc19cd/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.

Revision history for this message
scoder (scoder) wrote :
Changed in lxml:
assignee: nobody → scoder (scoder)
importance: Undecided → Low
milestone: none → 4.5.0
status: New → Fix Committed
Revision history for this message
Chris Jerdonek (chris-jerdonek) wrote :

Thanks. Also, I'm glad you fixed this by removing the code that deleted the tail text, as I thought that was unexpected / strange behavior.

scoder (scoder)
Changed in lxml:
status: Fix Committed → Fix Released
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.