Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7fd173b

Browse files
committed
Synchronize with pulldom from PyXML (revision 1.18).
1 parent 49a5d03 commit 7fd173b

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

Lib/xml/dom/pulldom.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def startPrefixMapping(self, prefix, uri):
4848
self._xmlns_attrs = []
4949
self._xmlns_attrs.append((prefix or 'xmlns', uri))
5050
self._ns_contexts.append(self._current_context.copy())
51-
self._current_context[uri] = prefix or ''
51+
self._current_context[uri] = prefix or None
5252

5353
def endPrefixMapping(self, prefix):
5454
self._current_context = self._ns_contexts.pop()
@@ -211,6 +211,8 @@ def __init__(self, stream, parser, bufsize):
211211
self.stream = stream
212212
self.parser = parser
213213
self.bufsize = bufsize
214+
if not hasattr(self.parser, 'feed'):
215+
self.getEvent = self._slurp
214216
self.reset()
215217

216218
def reset(self):
@@ -241,6 +243,8 @@ def expandNode(self, node):
241243
event = self.getEvent()
242244

243245
def getEvent(self):
246+
# use IncrementalParser interface, so we get the desired
247+
# pull effect
244248
if not self.pulldom.firstEvent[1]:
245249
self.pulldom.lastEvent = self.pulldom.firstEvent
246250
while not self.pulldom.firstEvent[1]:
@@ -253,8 +257,26 @@ def getEvent(self):
253257
self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
254258
return rc
255259

260+
def _slurp(self):
261+
""" Fallback replacement for getEvent() using the
262+
standard SAX2 interface, which means we slurp the
263+
SAX events into memory (no performance gain, but
264+
we are compatible to all SAX parsers).
265+
"""
266+
self.parser.parse(self.stream)
267+
self.getEvent = self._emit
268+
return self._emit()
269+
270+
def _emit(self):
271+
""" Fallback replacement for getEvent() that emits
272+
the events that _slurp() read previously.
273+
"""
274+
rc = self.pulldom.firstEvent[1][0]
275+
self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
276+
return rc
277+
256278
def clear(self):
257-
"clear(): Explicitly release parsing objects"
279+
"""clear(): Explicitly release parsing objects"""
258280
self.pulldom.clear()
259281
del self.pulldom
260282
self.parser = None

0 commit comments

Comments
 (0)