-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
base: main
Are you sure you want to change the base?
Changes from all commits
b0a8a64
f0356c8
06eaff5
6d4d65f
84b22d4
1d2f0d9
d5bbe23
4e2f822
8b588f3
e1e489e
28fcc69
78118ea
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe test also _setroot() here?
Comment on lines
+246
to
+247
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not needed. |
||
|
||
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__}") | ||||||||||||||
Comment on lines
+531
to
+533
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
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__}") | ||||||||||||||
Comment on lines
+550
to
+552
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
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 " | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need for f-strings. |
||||||||||||||
f"xml.etree.ElementTree.Element " | ||||||||||||||
f"or Element-like object") | ||||||||||||||
Comment on lines
+718
to
+721
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add also a test for writing uninitialized ElementTree: def test_write_uninitialized(self):
tree = ET.ElementTree()
self.assertRaises(TypeError, tree.write, TESTFN)
self.assertFalse(os.path.exists(TESTFN)) |
||||||||||||||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test also
_setroot(None)
._setroot()
needs a separate test. It should also be tested with valid argument.getroot()
should be used to check its effect.