Skip to content

gh-136231: Document xml.etree.ElementTree.iselement behavior when used with types #136482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions Doc/library/xml.etree.elementtree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,11 @@ Functions

.. function:: iselement(element)

Check if an object appears to be a valid element object. *element* is an
element instance. Return ``True`` if this is an element object.
Check if *element* appears to be a valid element object or type. Return
``True`` if this is an element object or type.

Because ``iselement`` behaves identically for both objects and types, code
requiring an object should check for this, see :func:`iselement`.


.. function:: iterparse(source, events=None, parser=None)
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
10 changes: 8 additions & 2 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,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('expected an Element, not %s' %
type(element).__name__)
self._root = element # first node
if file:
self.parse(file)
Expand All @@ -543,7 +545,9 @@ def _setroot(self, element):
with the given element. Use with care!

"""
# assert iselement(element)
if not iselement(element):
raise TypeError('expected an Element, not %s'
% type(element).__name__)
self._root = element

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

"""
if self._root is None:
raise TypeError('ElementTree not initialized')
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,2 @@
Document that :func:`xml.etree.ElementTree.iselement` works identically on
both object instances and types.
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