summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2019-02-28 16:33:58 +0100
committerStefan Behnel <stefan_ml@behnel.de>2019-02-28 16:33:58 +0100
commit3f47dac3a33d1731937223cb1b5b0fbda2d98eac (patch)
treed16d962e0d3a3b9a2ba00d3a9d8dec63ba16a413 /src
parentc5b06c45122f4084ccc826ee2828ed3cbe16ea24 (diff)
downloadpython-lxml-3f47dac3a33d1731937223cb1b5b0fbda2d98eac.tar.gz
Add some tests for tree modification while iterating.
Diffstat (limited to 'src')
-rw-r--r--src/lxml/tests/test_elementtree.py63
1 files changed, 56 insertions, 7 deletions
diff --git a/src/lxml/tests/test_elementtree.py b/src/lxml/tests/test_elementtree.py
index 0b82a574..7bd33252 100644
--- a/src/lxml/tests/test_elementtree.py
+++ b/src/lxml/tests/test_elementtree.py
@@ -789,13 +789,20 @@ class _ETreeTestCaseBase(HelperTestCase):
result.append(el.tag)
self.assertEqual([], result)
- def test_iteration_crash(self):
+ def test_iteration_set_tail_empty(self):
# this would cause a crash in the past
fromstring = self.etree.fromstring
- root = etree.fromstring('<html><p></p>x</html>')
+ root = fromstring('<html><p></p>x</html>')
for elem in root:
elem.tail = ''
+ def test_iteration_clear_tail(self):
+ # this would cause a crash in the past
+ fromstring = self.etree.fromstring
+ root = fromstring('<html><p></p>x</html>')
+ for elem in root:
+ elem.tail = None
+
def test_iteration_reversed(self):
XML = self.etree.XML
root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>'))
@@ -1735,7 +1742,21 @@ class _ETreeTestCaseBase(HelperTestCase):
a)
self.assertEqual('b2', b.tail)
- def _test_getchildren(self):
+ def test_remove_while_iterating(self):
+ # There is no guarantee that this "works", but it should
+ # remove at least one child and not crash.
+ Element = self.etree.Element
+ SubElement = self.etree.SubElement
+
+ a = Element('a')
+ SubElement(a, 'b')
+ SubElement(a, 'c')
+ SubElement(a, 'd')
+ for el in a:
+ a.remove(el)
+ self.assertLess(len(a), 3)
+
+ def test_getchildren(self):
Element = self.etree.Element
SubElement = self.etree.SubElement
@@ -1784,6 +1805,34 @@ class _ETreeTestCaseBase(HelperTestCase):
[d],
list(d.iter()))
+ def test_iter_remove_tail(self):
+ Element = self.etree.Element
+ SubElement = self.etree.SubElement
+
+ a = Element('a')
+ a.text = 'a'
+ a.tail = 'a1' * 100
+ b = SubElement(a, 'b')
+ b.text = 'b'
+ b.tail = 'b1' * 100
+ c = SubElement(a, 'c')
+ c.text = 'c'
+ c.tail = 'c1' * 100
+ d = SubElement(b, 'd')
+ d.text = 'd'
+ d.tail = 'd1' * 100
+ e = SubElement(c, 'e')
+ e.text = 'e'
+ e.tail = 'e1' * 100
+
+ for el in a.iter():
+ el.tail = None
+ el = None
+
+ self.assertEqual(
+ [None] * 5,
+ [el.tail for el in a.iter()])
+
def test_getiterator(self):
Element = self.etree.Element
SubElement = self.etree.SubElement
@@ -1919,8 +1968,8 @@ class _ETreeTestCaseBase(HelperTestCase):
c.text = 'c'
c.tail = 'c1'
d = SubElement(b, 'd')
- c.text = 'd'
- c.tail = 'd1'
+ d.text = 'd'
+ d.tail = 'd1'
e = SubElement(c, 'e')
e.text = 'e'
e.tail = 'e1'
@@ -1945,8 +1994,8 @@ class _ETreeTestCaseBase(HelperTestCase):
c.text = 'c'
c.tail = 'c1'
d = SubElement(b, 'd')
- c.text = 'd'
- c.tail = 'd1'
+ d.text = 'd'
+ d.tail = 'd1'
e = SubElement(c, 'e')
e.text = 'e'
e.tail = 'e1'