diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-10-19 09:12:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-19 09:12:57 (GMT) |
commit | f081fd83032be48aefdb1bbcc38ab5deb03785d5 (patch) | |
tree | 9696b61136dbfe1bae2edbb249b25cd7143defea /Lib/test/test_xml_etree.py | |
parent | 68def052dcd41313eff2bd9f269e22c5a941db4d (diff) | |
download | cpython-f081fd83032be48aefdb1bbcc38ab5deb03785d5.zip cpython-f081fd83032be48aefdb1bbcc38ab5deb03785d5.tar.gz cpython-f081fd83032be48aefdb1bbcc38ab5deb03785d5.tar.bz2 |
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.
Diffstat (limited to 'Lib/test/test_xml_etree.py')
-rw-r--r-- | Lib/test/test_xml_etree.py | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 61416ba..8b16905 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -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: |