-
-
Notifications
You must be signed in to change notification settings - Fork 33.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
Changes from 4 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 |
---|---|---|
|
@@ -247,6 +247,13 @@ def check_element(element): | |
self.assertRegex(repr(element), r"^<Element 't\xe4g' at 0x.*>$") | ||
element = ET.Element("tag", key="value") | ||
|
||
# Verify type checking for ElementTree constructor | ||
|
||
|
||
with self.assertRaises(TypeError): | ||
tree = ET.ElementTree("") | ||
with self.assertRaises(TypeError): | ||
tree = ET.ElementTree(ET.ElementTree()) | ||
|
||
# Make sure all standard element methods exist. | ||
|
||
def check_method(method): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(f"element must be etree.Element, " | ||
|
||
f"not {type(element).__name__}") | ||
self._root = element # first node | ||
if file: | ||
self.parse(file) | ||
|
@@ -543,7 +545,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 etree.Element, " | ||
f"not {type(element).__name__}") | ||
self._root = element | ||
|
||
def parse(self, source, parser=None): | ||
|
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.
Can you make a separate test method, say "test_constructor" and put that test before test_interface? TiA