mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
bpo-35013: Add more type checks for children of Element. (GH-9944)
It is now guarantied that children of xml.etree.ElementTree.Element are Elements (at least in C implementation). Previously methods __setitem__(), __setstate__() and __deepcopy__() could be used for adding non-Element children.
This commit is contained in:
parent
68def052dc
commit
f081fd8303
3 changed files with 71 additions and 50 deletions
|
@ -1795,6 +1795,28 @@ class BasicElementTest(ElementTestCase, unittest.TestCase):
|
|||
self.assertRaises(TypeError, e.append, 'b')
|
||||
self.assertRaises(TypeError, e.extend, [ET.Element('bar'), 'foo'])
|
||||
self.assertRaises(TypeError, e.insert, 0, 'foo')
|
||||
e[:] = [ET.Element('bar')]
|
||||
with self.assertRaises(TypeError):
|
||||
e[0] = 'foo'
|
||||
with self.assertRaises(TypeError):
|
||||
e[:] = [ET.Element('bar'), 'foo']
|
||||
|
||||
if hasattr(e, '__setstate__'):
|
||||
state = {
|
||||
'tag': 'tag',
|
||||
'_children': [None], # non-Element
|
||||
'attrib': 'attr',
|
||||
'tail': 'tail',
|
||||
'text': 'text',
|
||||
}
|
||||
self.assertRaises(TypeError, e.__setstate__, state)
|
||||
|
||||
if hasattr(e, '__deepcopy__'):
|
||||
class E(ET.Element):
|
||||
def __deepcopy__(self, memo):
|
||||
return None # non-Element
|
||||
e[:] = [E('bar')]
|
||||
self.assertRaises(TypeError, copy.deepcopy, e)
|
||||
|
||||
def test_cyclic_gc(self):
|
||||
class Dummy:
|
||||
|
@ -1981,26 +2003,6 @@ class BadElementTest(ElementTestCase, unittest.TestCase):
|
|||
elem = b.close()
|
||||
self.assertEqual(elem[0].tail, 'ABCDEFGHIJKL')
|
||||
|
||||
def test_element_iter(self):
|
||||
# Issue #27863
|
||||
state = {
|
||||
'tag': 'tag',
|
||||
'_children': [None], # non-Element
|
||||
'attrib': 'attr',
|
||||
'tail': 'tail',
|
||||
'text': 'text',
|
||||
}
|
||||
|
||||
e = ET.Element('tag')
|
||||
try:
|
||||
e.__setstate__(state)
|
||||
except AttributeError:
|
||||
e.__dict__ = state
|
||||
|
||||
it = e.iter()
|
||||
self.assertIs(next(it), e)
|
||||
self.assertRaises(AttributeError, next, it)
|
||||
|
||||
def test_subscr(self):
|
||||
# Issue #27863
|
||||
class X:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue