mirror of
https://github.com/python/cpython.git
synced 2025-07-07 11:25:30 +00:00
gh-135640: Adds more type checking to ElementTree (GH-135643)
Some checks are pending
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Undefined behavior sanitizer (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run
Some checks are pending
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Undefined behavior sanitizer (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run
This commit is contained in:
parent
9084b15156
commit
e0245c789f
3 changed files with 39 additions and 2 deletions
|
@ -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.
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
@ -543,7 +545,9 @@ class ElementTree:
|
|||
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):
|
||||
|
@ -709,6 +713,8 @@ class ElementTree:
|
|||
of start/end tags
|
||||
|
||||
"""
|
||||
if self._root is None:
|
||||
raise TypeError('ElementTree not initialized')
|
||||
if not method:
|
||||
method = "xml"
|
||||
elif method not in _serialize:
|
||||
|
|
|
@ -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…
Add table
Add a link
Reference in a new issue