Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-135640: Adds type checking to ElementTree.ElementTree constructor #135643

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
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):
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)

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__))

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.
Loading