Commit dd22ae08 authored by di68kap's avatar di68kap
Browse files

bugfix

parent 090c325b
......@@ -1304,9 +1304,12 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
or a criterion for selecting child-nodes for deletion.
"""
if isinstance(key, int):
if 0 <= key < len(self._children):
L = len(self._children)
k = L + key if key < 0 else key
if 0 <= k < L:
self.result = self._children[:k] + self._children[k + 1:]
else:
raise IndexError("index %s out of range [0, %i[" % (key, len(self._children)))
self.result = self._children[:key] + self._children[key + 1:]
elif isinstance(key, slice):
children = list(self.children)
for i in range(*key.indices(len(children))):
......
......@@ -465,6 +465,36 @@ class TestNode:
node = Node.from_etree(et)
assert node.as_sxpr() == expected_sxpr
def test_delete_item(self):
sxpr = '(root (A "0") (B "1") (C "2") (D "3"))'
node = parse_sxpr(sxpr)
try:
_ = node.index('E')
assert False, 'ValueError expected'
except ValueError:
pass
assert node[-1].tag_name == "D"
try:
del node[4]
assert False, 'IndexError expected'
except IndexError:
pass
try:
del node[-5]
assert False, 'IndexError expected'
except IndexError:
pass
del node[-2]
assert 'C' not in node
del node[0]
assert 'A' not in node
sxpr = '(root (A "0") (B "1") (C "2") (D "3"))'
node = parse_sxpr(sxpr)
del node[1:3]
assert str(node) == "03"
class TestRootNode:
def test_error_handling(self):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment