Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
36 changes: 36 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,42 @@ 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"
self.text = None
self.tail = None
def iter(self):
Copy link
Member

Choose a reason for hiding this comment

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

It's iter, not __iter__?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the sake of testing what .write() needs to complete successfully, it is (used in _namespaces in ElementTree.py). But actually, that test should be changed anyway since it was made in anticipation of the changes to iselement that have been undone

pass
def items(self):
pass
def __len__(self):
pass

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: 9 additions & 4 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ class ParseError(SyntaxError):

def iselement(element):
"""Return True if *element* appears to be an Element."""
return hasattr(element, 'tag')

return (hasattr(element, 'tag') and hasattr(element, 'text') and
hasattr(element, 'tail') and callable(element.iter) and
callable(element.items) and callable(element.__len__))
Copy link
Member

Choose a reason for hiding this comment

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

Does this function raise an exception if iter(), items() or __len__() method doesn't exist?


class Element:
"""An XML element.
Expand Down Expand Up @@ -527,7 +528,9 @@ 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, "
f"not {type(element).__name__}")
self._root = element # first node
if file:
self.parse(file)
Expand All @@ -543,7 +546,9 @@ 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, "
f"not {type(element).__name__}")
self._root = element

def parse(self, source, parser=None):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Address bug where calling :func:`xml.etree.ElementTree.ElementTree.write` on
an ElementTree object with an invalid root element would blank the file
passed to ``write`` if it already existed.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe rewrite this in more simple manner? Split one sentence into two, for example.

Loading