mirror of https://github.com/python/cpython
Patch #103052: Restore non-cyclic operation of pulldom.PullDOM
This commit is contained in:
parent
2efc3238d7
commit
04a1a542cb
|
@ -24,9 +24,21 @@ class PullDOM(xml.sax.ContentHandler):
|
||||||
self.documentFactory = documentFactory
|
self.documentFactory = documentFactory
|
||||||
self.firstEvent = [None, None]
|
self.firstEvent = [None, None]
|
||||||
self.lastEvent = self.firstEvent
|
self.lastEvent = self.firstEvent
|
||||||
|
self.elementStack = []
|
||||||
|
self.push = self.elementStack.append
|
||||||
|
try:
|
||||||
|
self.pop = self.elementStack.pop
|
||||||
|
except AttributeError:
|
||||||
|
# use class' pop instead
|
||||||
|
pass
|
||||||
self._ns_contexts = [{}] # contains uri -> prefix dicts
|
self._ns_contexts = [{}] # contains uri -> prefix dicts
|
||||||
self._current_context = self._ns_contexts[-1]
|
self._current_context = self._ns_contexts[-1]
|
||||||
|
|
||||||
|
def pop(self):
|
||||||
|
result = self.elementStack[-1]
|
||||||
|
del self.elementStack[-1]
|
||||||
|
return result
|
||||||
|
|
||||||
def setDocumentLocator(self, locator):
|
def setDocumentLocator(self, locator):
|
||||||
self._locator = locator
|
self._locator = locator
|
||||||
|
|
||||||
|
@ -61,21 +73,13 @@ class PullDOM(xml.sax.ContentHandler):
|
||||||
attr.value = value
|
attr.value = value
|
||||||
node.setAttributeNode(attr)
|
node.setAttributeNode(attr)
|
||||||
|
|
||||||
## print self.curNode, self.curNode.childNodes, node, node.parentNode
|
|
||||||
self.curNode.appendChild(node)
|
|
||||||
# node.parentNode = self.curNode
|
|
||||||
self.curNode = node
|
|
||||||
|
|
||||||
self.lastEvent[1] = [(START_ELEMENT, node), None]
|
self.lastEvent[1] = [(START_ELEMENT, node), None]
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
#self.events.append((START_ELEMENT, node))
|
self.push(node)
|
||||||
|
|
||||||
def endElementNS(self, name, tagName):
|
def endElementNS(self, name, tagName):
|
||||||
node = self.curNode
|
self.lastEvent[1] = [(END_ELEMENT, self.pop()), None]
|
||||||
self.lastEvent[1] = [(END_ELEMENT, node), None]
|
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
#self.events.append((END_ELEMENT, node))
|
|
||||||
self.curNode = self.curNode.parentNode
|
|
||||||
|
|
||||||
def startElement(self, name, attrs):
|
def startElement(self, name, attrs):
|
||||||
node = self.document.createElement(name)
|
node = self.document.createElement(name)
|
||||||
|
@ -85,54 +89,32 @@ class PullDOM(xml.sax.ContentHandler):
|
||||||
attr.value = value
|
attr.value = value
|
||||||
node.setAttributeNode(attr)
|
node.setAttributeNode(attr)
|
||||||
|
|
||||||
#node.parentNode = self.curNode
|
|
||||||
self.curNode.appendChild(node)
|
|
||||||
self.curNode = node
|
|
||||||
|
|
||||||
self.lastEvent[1] = [(START_ELEMENT, node), None]
|
self.lastEvent[1] = [(START_ELEMENT, node), None]
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
#self.events.append((START_ELEMENT, node))
|
self.push(node)
|
||||||
|
|
||||||
def endElement(self, name):
|
def endElement(self, name):
|
||||||
node = self.curNode
|
self.lastEvent[1] = [(END_ELEMENT, self.pop()), None]
|
||||||
self.lastEvent[1] = [(END_ELEMENT, node), None]
|
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
#self.events.append((END_ELEMENT, node))
|
|
||||||
self.curNode = node.parentNode
|
|
||||||
|
|
||||||
def comment(self, s):
|
def comment(self, s):
|
||||||
node = self.document.createComment(s)
|
node = self.document.createComment(s)
|
||||||
self.curNode.appendChild(node)
|
|
||||||
# parent = self.curNode
|
|
||||||
# node.parentNode = parent
|
|
||||||
self.lastEvent[1] = [(COMMENT, node), None]
|
self.lastEvent[1] = [(COMMENT, node), None]
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
#self.events.append((COMMENT, node))
|
|
||||||
|
|
||||||
def processingInstruction(self, target, data):
|
def processingInstruction(self, target, data):
|
||||||
node = self.document.createProcessingInstruction(target, data)
|
node = self.document.createProcessingInstruction(target, data)
|
||||||
|
|
||||||
self.curNode.appendChild(node)
|
|
||||||
# parent = self.curNode
|
|
||||||
# node.parentNode = parent
|
|
||||||
self.lastEvent[1] = [(PROCESSING_INSTRUCTION, node), None]
|
self.lastEvent[1] = [(PROCESSING_INSTRUCTION, node), None]
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
#self.events.append((PROCESSING_INSTRUCTION, node))
|
|
||||||
|
|
||||||
def ignorableWhitespace(self, chars):
|
def ignorableWhitespace(self, chars):
|
||||||
node = self.document.createTextNode(chars)
|
node = self.document.createTextNode(chars)
|
||||||
self.curNode.appendChild(node)
|
|
||||||
# parent = self.curNode
|
|
||||||
# node.parentNode = parent
|
|
||||||
self.lastEvent[1] = [(IGNORABLE_WHITESPACE, node), None]
|
self.lastEvent[1] = [(IGNORABLE_WHITESPACE, node), None]
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
#self.events.append((IGNORABLE_WHITESPACE, node))
|
|
||||||
|
|
||||||
def characters(self, chars):
|
def characters(self, chars):
|
||||||
node = self.document.createTextNode(chars)
|
node = self.document.createTextNode(chars)
|
||||||
self.curNode.appendChild(node)
|
|
||||||
# parent = self.curNode
|
|
||||||
# node.parentNode = parent
|
|
||||||
self.lastEvent[1] = [(CHARACTERS, node), None]
|
self.lastEvent[1] = [(CHARACTERS, node), None]
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
|
|
||||||
|
@ -145,19 +127,14 @@ class PullDOM(xml.sax.ContentHandler):
|
||||||
import xml.dom.minidom
|
import xml.dom.minidom
|
||||||
self.documentFactory = xml.dom.minidom.Document.implementation
|
self.documentFactory = xml.dom.minidom.Document.implementation
|
||||||
node = self.documentFactory.createDocument(None, publicId, systemId)
|
node = self.documentFactory.createDocument(None, publicId, systemId)
|
||||||
self.curNode = self.document = node
|
self.document = node
|
||||||
self.lastEvent[1] = [(START_DOCUMENT, node), None]
|
self.lastEvent[1] = [(START_DOCUMENT, node), None]
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
#self.events.append((START_DOCUMENT, node))
|
self.push(node)
|
||||||
|
|
||||||
def endDocument(self):
|
def endDocument(self):
|
||||||
assert self.curNode.parentNode is None, \
|
self.lastEvent[1] = [(END_DOCUMENT, self.document), None]
|
||||||
"not all elements have been properly closed"
|
self.pop()
|
||||||
assert self.curNode.documentElement is not None, \
|
|
||||||
"document does not contain a root element"
|
|
||||||
node = self.curNode.documentElement
|
|
||||||
self.lastEvent[1] = [(END_DOCUMENT, node), None]
|
|
||||||
#self.events.append((END_DOCUMENT, self.curNode))
|
|
||||||
|
|
||||||
class ErrorHandler:
|
class ErrorHandler:
|
||||||
def warning(self, exception):
|
def warning(self, exception):
|
||||||
|
@ -188,12 +165,17 @@ class DOMEventStream:
|
||||||
|
|
||||||
def expandNode(self, node):
|
def expandNode(self, node):
|
||||||
event = self.getEvent()
|
event = self.getEvent()
|
||||||
|
parents = [node]
|
||||||
while event:
|
while event:
|
||||||
token, cur_node = event
|
token, cur_node = event
|
||||||
if cur_node is node:
|
if cur_node is node:
|
||||||
return
|
return
|
||||||
if token != END_ELEMENT:
|
if token != END_ELEMENT:
|
||||||
cur_node.parentNode.appendChild(cur_node)
|
parents[-1].appendChild(cur_node)
|
||||||
|
if token == START_ELEMENT:
|
||||||
|
parents.append(cur_node)
|
||||||
|
elif token == END_ELEMENT:
|
||||||
|
del parents[-1]
|
||||||
event = self.getEvent()
|
event = self.getEvent()
|
||||||
|
|
||||||
def getEvent(self):
|
def getEvent(self):
|
||||||
|
@ -214,26 +196,33 @@ class SAX2DOM(PullDOM):
|
||||||
|
|
||||||
def startElementNS(self, name, tagName , attrs):
|
def startElementNS(self, name, tagName , attrs):
|
||||||
PullDOM.startElementNS(self, name, tagName, attrs)
|
PullDOM.startElementNS(self, name, tagName, attrs)
|
||||||
self.curNode.parentNode.appendChild(self.curNode)
|
curNode = self.elementStack[-1]
|
||||||
|
parentNode = self.elementStack[-2]
|
||||||
|
parentNode.appendChild(curNode)
|
||||||
|
|
||||||
def startElement(self, name, attrs):
|
def startElement(self, name, attrs):
|
||||||
PullDOM.startElement(self, name, attrs)
|
PullDOM.startElement(self, name, attrs)
|
||||||
self.curNode.parentNode.appendChild(self.curNode)
|
curNode = self.elementStack[-1]
|
||||||
|
parentNode = self.elementStack[-2]
|
||||||
|
parentNode.appendChild(curNode)
|
||||||
|
|
||||||
def processingInstruction(self, target, data):
|
def processingInstruction(self, target, data):
|
||||||
PullDOM.processingInstruction(self, target, data)
|
PullDOM.processingInstruction(self, target, data)
|
||||||
node = self.lastEvent[0][1]
|
node = self.lastEvent[0][1]
|
||||||
node.parentNode.appendChild(node)
|
parentNode = self.elementStack[-1]
|
||||||
|
parentNode.appendChild(node)
|
||||||
|
|
||||||
def ignorableWhitespace(self, chars):
|
def ignorableWhitespace(self, chars):
|
||||||
PullDOM.ignorableWhitespace(self, chars)
|
PullDOM.ignorableWhitespace(self, chars)
|
||||||
node = self.lastEvent[0][1]
|
node = self.lastEvent[0][1]
|
||||||
node.parentNode.appendChild(node)
|
parentNode = self.elementStack[-1]
|
||||||
|
parentNode.appendChild(node)
|
||||||
|
|
||||||
def characters(self, chars):
|
def characters(self, chars):
|
||||||
PullDOM.characters(self, chars)
|
PullDOM.characters(self, chars)
|
||||||
node = self.lastEvent[0][1]
|
node = self.lastEvent[0][1]
|
||||||
node.parentNode.appendChild(node)
|
parentNode = self.elementStack[-1]
|
||||||
|
parentNode.appendChild(node)
|
||||||
|
|
||||||
|
|
||||||
default_bufsize = (2 ** 14) - 20
|
default_bufsize = (2 ** 14) - 20
|
||||||
|
|
Loading…
Reference in New Issue