Skip to content

Commit e0245c7

Browse files
gh-135640: Adds more type checking to ElementTree (GH-135643)
1 parent 9084b15 commit e0245c7

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

Lib/test/test_xml_etree.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,33 @@ class ElementTreeTest(unittest.TestCase):
218218
def serialize_check(self, elem, expected):
219219
self.assertEqual(serialize(elem), expected)
220220

221+
def test_constructor(self):
222+
# Test constructor behavior.
223+
224+
with self.assertRaises(TypeError):
225+
tree = ET.ElementTree("")
226+
with self.assertRaises(TypeError):
227+
tree = ET.ElementTree(ET.ElementTree())
228+
229+
def test_setroot(self):
230+
# Test _setroot behavior.
231+
232+
tree = ET.ElementTree()
233+
element = ET.Element("tag")
234+
tree._setroot(element)
235+
self.assertEqual(tree.getroot().tag, "tag")
236+
self.assertEqual(tree.getroot(), element)
237+
238+
# Test behavior with an invalid root element
239+
240+
tree = ET.ElementTree()
241+
with self.assertRaises(TypeError):
242+
tree._setroot("")
243+
with self.assertRaises(TypeError):
244+
tree._setroot(ET.ElementTree())
245+
with self.assertRaises(TypeError):
246+
tree._setroot(None)
247+
221248
def test_interface(self):
222249
# Test element tree interface.
223250

Lib/xml/etree/ElementTree.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,9 @@ class ElementTree:
527527
528528
"""
529529
def __init__(self, element=None, file=None):
530-
# assert element is None or iselement(element)
530+
if element is not None and not iselement(element):
531+
raise TypeError('expected an Element, not %s' %
532+
type(element).__name__)
531533
self._root = element # first node
532534
if file:
533535
self.parse(file)
@@ -543,7 +545,9 @@ def _setroot(self, element):
543545
with the given element. Use with care!
544546
545547
"""
546-
# assert iselement(element)
548+
if not iselement(element):
549+
raise TypeError('expected an Element, not %s'
550+
% type(element).__name__)
547551
self._root = element
548552

549553
def parse(self, source, parser=None):
@@ -709,6 +713,8 @@ def write(self, file_or_filename,
709713
of start/end tags
710714
711715
"""
716+
if self._root is None:
717+
raise TypeError('ElementTree not initialized')
712718
if not method:
713719
method = "xml"
714720
elif method not in _serialize:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Address bug where it was possible to call
2+
:func:`xml.etree.ElementTree.ElementTree.write` on an ElementTree object with
3+
an invalid root element. This behavior blanked the file passed to ``write``
4+
if it already existed.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy