Issue 9708: cElementTree iterparse does not support "parser" argument (original) (raw)
I changed a few lines in the glue code of the _elementtree.c Module of Python 3.3.0a0 to add support for the "parser" argument. I do have to admit though that I am not familiar with the Python/C-API so this solution may not be ideal (i. e. it may be falling back to the Python implementation of iterparse without me realizing).
Please note that it is not possible to give an ElementTree.XMLParser instance as a parameter to the cElementTree.iterparse function (which may not be desirable in any case) using this patch.
I assume that the patch will be applicable to Python 2.7.x as well, but I did not try it. I can apply/adapt it to Python 2.7.x, if you think that would be useful.
The situation is as follows.
According to the online documentation of Python 2.7 the xml.etree.ElementTree.iterparse() function takes up to three arguments, two of them named ones:
xml.etree.ElementTree.iterparse(source, events=None, parser=None)In the C implementation of the function however, the "parser" argument does not seem to be supported:
>>> import xml.etree.ElementTree as ET
>>> import xml.etree.cElementTree as ETc # C version of the library
>>> result = ET.iterparse("xmltest.xml", None, ET.XMLParser()) # Works
>>>
>>> result = ETc.iterparse("xmltest.xml", None, ET.XMLParser()) # C version does'nt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes at most 3 arguments (4 given)The documentation does not mention the C version of the function not taking the "parser" argument as far as I know.
Additionally, the xml.etree.ElementTree.iterparse() online documentation for Python 3.3 mentions the "parser" argument as well. When using this argument however, Python throws an error:
Python 3.3.0 (default, Dec 22 2012, 21:02:07)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as ET
>>> ET.iterparse("xmltest.xml", None, ET.XMLParser())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes from 2 to 3 positional arguments but 4 were givenA look at the pydoc output for xml.etree.ElementTree actually does not mention a named "parser" argument to iterparse() at all (Please note that iterparse seems to be a constructor in Python 3.3):
class iterparse(builtins.object)
| Methods defined here:
|
| init(self, file, events=None)
In these cases either the implementation or the documentation should be changed.
Please feel free to ask away if you have more questions.