Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,34 @@ 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())

# Test _setroot as well, since it also sets the _root object.

tree = ET.ElementTree()
with self.assertRaises(TypeError):
tree._setroot("")
with self.assertRaises(TypeError):
tree._setroot(ET.ElementTree())

# Make sure it accepts an Element-like object.

class ElementLike:
def __init__(self):
self.tag = "tag"

element_like = ElementLike()
try:
tree = ET.ElementTree(element_like)
except Exception as err:
self.fail(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test also _setroot() here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed.


def test_interface(self):
# Test element tree interface.

Expand Down
13 changes: 11 additions & 2 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,10 @@ class ElementTree:

"""
def __init__(self, element=None, file=None):
# assert element is None or iselement(element)
if element is not None and not iselement(element):
raise TypeError(f"element must be xml.etree.Element or "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is xml.etree.ElementTree.Element.

Look at _assert_is_element(). We need a similar error message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my bad, I forgot the ElementTree part in my suggestion

f"Element-like object, not "
f"{type(element).__name__}")
self._root = element # first node
if file:
self.parse(file)
Expand All @@ -543,7 +546,10 @@ def _setroot(self, element):
with the given element. Use with care!

"""
# assert iselement(element)
if not iselement(element):
raise TypeError(f"element must be xml.etree.Element or "
f"Element-like object, not "
f"{type(element).__name__}")
self._root = element

def parse(self, source, parser=None):
Expand Down Expand Up @@ -709,6 +715,9 @@ def write(self, file_or_filename,
of start/end tags

"""
if not iselement(self._root):
raise TypeError(f"Root element must be of type xml.etree.Element "
f"or Element-like object")
if not method:
method = "xml"
elif method not in _serialize:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Address bug where it was possible to call
:func:`xml.etree.ElementTree.ElementTree.write` on an ElementTree object with
an invalid root element. This behavior blanked the file passed to ``write``
if it already existed.
Loading