[Python-checkins] cpython (merge 3.3 -> default): Issue #9708: Fix support for iterparse(parser=...) argument per documentation.
eli.bendersky
python-checkins at python.org
Thu Jan 24 16:17:47 CET 2013
http://hg.python.org/cpython/rev/0c9268ac3ffa
changeset: 81686:0c9268ac3ffa
parent: 81681:56a4561600ad
parent: 81685:cce526a28f81
user: Eli Bendersky <eliben at gmail.com>
date: Thu Jan 24 07:15:46 2013 -0800
summary:
Issue #9708: Fix support for iterparse(parser=...) argument per documentation.
When _elementtree is imported, iterparse is redefined as a class and the parser
argument was ommitted. Fix this, and add a docstring to the class.
files:
Lib/test/test_xml_etree.py | 6 ++++++
Lib/xml/etree/ElementTree.py | 19 ++++++++++++++++---
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -1886,6 +1886,12 @@
sourcefile = serialize(doc, to_string=False)
self.assertEqual(next(ET.iterparse(sourcefile))[0], 'end')
+ # With an explitit parser too (issue #9708)
+ sourcefile = serialize(doc, to_string=False)
+ parser = ET.XMLParser(target=ET.TreeBuilder())
+ self.assertEqual(next(ET.iterparse(sourcefile, parser=parser))[0],
+ 'end')
+
tree = ET.ElementTree(None)
self.assertRaises(AttributeError, tree.iter)
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -1743,8 +1743,20 @@
source.close()
class iterparse:
+ """Parses an XML section into an element tree incrementally.
+
+ Reports what’s going on to the user. 'source' is a filename or file
+ object containing XML data. 'events' is a list of events to report back.
+ The supported events are the strings "start", "end", "start-ns" and
+ "end-ns" (the "ns" events are used to get detailed namespace
+ information). If 'events' is omitted, only "end" events are reported.
+ 'parser' is an optional parser instance. If not given, the standard
+ XMLParser parser is used. Returns an iterator providing
+ (event, elem) pairs.
+ """
+
root = None
- def __init__(self, file, events=None):
+ def __init__(self, file, events=None, parser=None):
self._close_file = False
if not hasattr(file, 'read'):
file = open(file, 'rb')
@@ -1754,8 +1766,9 @@
self._index = 0
self._error = None
self.root = self._root = None
- b = TreeBuilder()
- self._parser = XMLParser(b)
+ if parser is None:
+ parser = XMLParser(target=TreeBuilder())
+ self._parser = parser
self._parser._setevents(self._events, events)
def __next__(self):
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list