mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									33f7cdd975
								
							
						
					
					
						commit
						aaa9780fe1
					
				
					 2 changed files with 22 additions and 3 deletions
				
			
		|  | @ -1881,6 +1881,12 @@ def test_basic(self): | |||
|         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) | ||||
| 
 | ||||
|  |  | |||
|  | @ -1737,8 +1737,20 @@ def parse(self, source, parser=None): | |||
|                     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') | ||||
|  | @ -1748,8 +1760,9 @@ def __init__(self, file, events=None): | |||
|             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): | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Eli Bendersky
						Eli Bendersky