[New-bugs-announce] [issue17901] _elementtree.TreeBuilder raises IndexError on end if constructed with element_factory=None

Aaron Oakley report at bugs.python.org
Fri May 3 21:56:56 CEST 2013


New submission from Aaron Oakley:

When the _elementtree module is in use, the TreeBuilder class will raise an IndexError in treebuilder_handle_end if __init__ was passed "None".

I discovered this while writing a subclass of TreeBuilder with a modified __init__ method that delegated to TreeBuilder:

    class MyTreeBuilder(ET.TreeBuilder):
        def __init__(self, element_factory=None):
            super().__init__(element_factory)

Used as a target, this class (and also simply "TreeBuilder(None)") will cause the IndexError when "parser.feed(data)" is called.

>>> import xml.etree.ElementTree as ET
>>> parser = ET.XMLParser(target=ET.TreeBuilder(None))
>>> parser.feed('<file><line>22</line></file>')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop from empty stack

The error is raised from treebuilder_handle_end, but the cause appears to be in treebuilder_handle_start.

    if (self->element_factory) {
        node = PyObject_CallFunction(self->element_factory, "OO", tag, attrib);
    } else {
        node = create_new_element(tag, attrib);
    }

I included a patch adding a check against Py_None to the "if" test above which seems to fix the issue. I also included a simple test case for it.

----------
components: XML
files: _elementtree.c-340a0.patch
keywords: patch
messages: 188326
nosy: Aaron.Oakley
priority: normal
severity: normal
status: open
title: _elementtree.TreeBuilder raises IndexError on end if constructed with element_factory=None
type: behavior
versions: Python 3.2, Python 3.3, Python 3.4
Added file: http://bugs.python.org/file30117/_elementtree.c-340a0.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17901>
_______________________________________


More information about the New-bugs-announce mailing list