This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: cElementTree iterparse does not support "parser" argument
Type: behavior Stage: resolved
Components: Documentation, XML Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Shugyousha, adrian_nye, docs@python, effbot, eli.bendersky, ezio.melotti, flox, python-dev
Priority: normal Keywords: patch

Created on 2010-08-28 21:51 by adrian_nye, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
issue9708.Python330a0.patch Shugyousha, 2011-11-25 15:22 Simple patch in c module glue code review
Messages (10)
msg115173 - (view) Author: Adrian Nye (adrian_nye) Date: 2010-08-28 21:51
The (python) ElementTree library began in 2.7 to support the "parser" argument, but cElementTree does not support it.

Either cElementTree should support it, or the documentation should mention that it does not.
msg146596 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2011-10-29 03:07
Documentation should be fixed for 3.2 and 2.7.
Patch welcomed.
msg148324 - (view) Author: Silvan Jegen (Shugyousha) Date: 2011-11-25 15:22
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.
msg166042 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2012-07-21 15:23
Either the documentation or the implementation should be fixed in 3.3, because they do not match.
msg180387 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2013-01-22 13:52
Could you point out specifically which methods in ET don't work with the argument, and describe the problem in general?
msg180421 - (view) Author: Silvan Jegen (Shugyousha) Date: 2013-01-22 19:02
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 given

A 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

Please feel free to ask away if you have more questions.
msg180527 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-01-24 15:17
New changeset cce526a28f81 by Eli Bendersky in branch '3.3':
Issue #9708: Fix support for iterparse(parser=...) argument per documentation.

New changeset 0c9268ac3ffa by Eli Bendersky in branch 'default':
Issue #9708: Fix support for iterparse(parser=...) argument per documentation.
msg180528 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2013-01-24 15:19
Support added in 3.3 and default

Documentation patches should be done for 2.7 and 3.2
msg180529 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2013-01-24 15:26
Fix to 3.2 committed in 5b02d622d625
msg180531 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-01-24 15:28
New changeset 8f2edea69d5d by Eli Bendersky in branch '2.7':
Issue #9708: clarify doc of iterparse - cElementTree doesn't support the parser argument
Date User Action Args
2022-04-11 14:57:05adminsetgithub: 53917
2013-01-24 15:29:16eli.benderskysetstatus: open -> closed
resolution: fixed
stage: needs patch -> resolved
2013-01-24 15:28:46python-devsetmessages: + msg180531
2013-01-24 15:26:23eli.benderskysetmessages: + msg180529
2013-01-24 15:19:56eli.benderskysetmessages: + msg180528
2013-01-24 15:17:44python-devsetnosy: + python-dev
messages: + msg180527
2013-01-22 19:02:47Shugyoushasetmessages: + msg180421
2013-01-22 13:52:11eli.benderskysetmessages: + msg180387
2013-01-22 12:54:43ezio.melottisetnosy: + ezio.melotti

versions: + Python 3.4
2012-07-21 15:23:37floxsetnosy: + eli.bendersky

messages: + msg166042
versions: + Python 3.3
2011-11-25 15:22:08Shugyoushasetfiles: + issue9708.Python330a0.patch

nosy: + Shugyousha
messages: + msg148324

keywords: + patch
2011-10-29 03:07:06floxsetnosy: + docs@python
messages: + msg146596

assignee: docs@python
components: + Documentation
stage: test needed -> needs patch
2010-09-03 17:00:04floxsetnosy: + effbot
stage: test needed
type: behavior

versions: + Python 3.2
2010-08-28 23:53:21r.david.murraysetnosy: + flox
2010-08-28 21:51:34adrian_nyecreate