Inconsistent behavior deleting slice objects

Bug #1857794 reported by Chris Jerdonek
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Fix Released

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'


del parent[0]
print(child.tail) # outputs: None


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__():

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():

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