[3.13] gh-135640: Adds more type checking to ElementTree (GH-135643) (GH-136226)

(cherry picked from commit e0245c789f)

Co-authored-by: Kira <kirawhoprograms@fastmail.com>
This commit is contained in:
Miss Islington (bot) 2025-07-03 10:12:21 +02:00 committed by GitHub
parent d80df8c1a5
commit 1de8fc3e4b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 2 deletions

View file

@ -218,6 +218,33 @@ class ElementTreeTest(unittest.TestCase):
def serialize_check(self, elem, expected):
self.assertEqual(serialize(elem), expected)
def test_constructor(self):
# Test constructor behavior.
with self.assertRaises(TypeError):
tree = ET.ElementTree("")
with self.assertRaises(TypeError):
tree = ET.ElementTree(ET.ElementTree())
def test_setroot(self):
# Test _setroot behavior.
tree = ET.ElementTree()
element = ET.Element("tag")
tree._setroot(element)
self.assertEqual(tree.getroot().tag, "tag")
self.assertEqual(tree.getroot(), element)
# Test behavior with an invalid root element
tree = ET.ElementTree()
with self.assertRaises(TypeError):
tree._setroot("")
with self.assertRaises(TypeError):
tree._setroot(ET.ElementTree())
with self.assertRaises(TypeError):
tree._setroot(None)
def test_interface(self):
# Test element tree interface.