diff options
author | Eli Bendersky <eliben@gmail.com> | 2012-03-23 12:24:20 (GMT) |
---|---|---|
committer | Eli Bendersky <eliben@gmail.com> | 2012-03-23 12:24:20 (GMT) |
commit | 396e8fcf36480dacaeeeb785a3912a565294d3b7 (patch) | |
tree | 173647551cef119cdf7644b8eb009b4a48314995 /Lib/xml | |
parent | 42243c4dcaee5fe6e680d1ea4b1b615dd0d18b10 (diff) | |
download | cpython-396e8fcf36480dacaeeeb785a3912a565294d3b7.zip cpython-396e8fcf36480dacaeeeb785a3912a565294d3b7.tar.gz cpython-396e8fcf36480dacaeeeb785a3912a565294d3b7.tar.bz2 |
Issue #13782: streamline argument type-checking in ET.Element
append, extend and insert now consistently type-check their argument in both
the C and Python implementations, and raise TypeError for non-Element
argument.
Added tests
Diffstat (limited to 'Lib/xml')
-rw-r--r-- | Lib/xml/etree/ElementTree.py | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index 10ee896..5f974f6 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -298,7 +298,7 @@ class Element: # @param element The element to add. def append(self, element): - # assert iselement(element) + self._assert_is_element(element) self._children.append(element) ## @@ -308,8 +308,8 @@ class Element: # @since 1.3 def extend(self, elements): - # for element in elements: - # assert iselement(element) + for element in elements: + self._assert_is_element(element) self._children.extend(elements) ## @@ -318,9 +318,13 @@ class Element: # @param index Where to insert the new subelement. def insert(self, index, element): - # assert iselement(element) + self._assert_is_element(element) self._children.insert(index, element) + def _assert_is_element(self, e): + if not isinstance(e, Element): + raise TypeError('expected an Element, not %s' % type(e).__name__) + ## # Removes a matching subelement. Unlike the <b>find</b> methods, # this method compares elements based on identity, not on tag |