[Python-Dev] Raising assertions on wrong element types in ElementTree

Stefan Behnel stefan_ml at behnel.de
Fri Mar 16 08:51:07 CET 2012


Eli Bendersky, 16.03.2012 08:38:
> The Element class in ElementTree
> (http://docs.python.org/py3k/library/xml.etree.elementtree.html) has
> some methods for adding new children: append, insert and extend.
> Currently the documentation states that extend raises AssertionError
> when something that's not an Element is being passed to it, and the
> others don't mention mention this case.

AssertionError is clearly the wrong thing to raise for user input.


> There are a number of problems with this:
> 
> 1. The behavior of append, insert and extend should be similar in this respect
> 2. AssertionError is not the customary error in such case - TypeError
> is much more suitable
> 3. The C implementation of ElementTree actually raises TypeError in
> all these methods, by virtue of using PyArg_ParseTuple
> 4. The Python implementation (at least in 3.2) actually doesn't raise
> even AssertionError in extend - this was commented out
> 
> The suggestion for 3.3 (where compatibility between the C and Python
> implementations gets even more important, since the C one is now being
> imported by default when available) is to raise TypeError in all 3
> methods in the Python implementation, to match the C implementation,
> and to modify the documentation accordingly.

+1

Stefan



More information about the Python-Dev mailing list