-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
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
Changes from 12 commits
b0a8a64
f0356c8
06eaff5
6d4d65f
84b22d4
1d2f0d9
d5bbe23
4e2f822
8b588f3
e1e489e
28fcc69
78118ea
bfd94cb
7c3a3b2
97d5fa0
c3fb864
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
|
||
def test_interface(self): | ||
# Test element tree interface. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"expected an xml.etree.ElementTree.Element or " | ||
f"Element-like object, not " | ||
f"{type(element).__name__}") | ||
abstractedfox marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
self._root = element # first node | ||
if file: | ||
self.parse(file) | ||
|
@@ -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"expected an xml.etree.ElementTree.Element or " | ||
f"Element-like object, not " | ||
f"{type(element).__name__}") | ||
abstractedfox marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
self._root = element | ||
|
||
def parse(self, source, parser=None): | ||
|
@@ -709,6 +715,10 @@ def write(self, file_or_filename, | |
of start/end tags | ||
|
||
""" | ||
if not iselement(self._root): | ||
raise TypeError(f"Root element must be " | ||
|
||
f"xml.etree.ElementTree.Element " | ||
f"or Element-like object") | ||
abstractedfox marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if not method: | ||
method = "xml" | ||
elif method not in _serialize: | ||
|
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. |
Uh oh!
There was an error while loading. Please reload this page.