X Tutup
Skip to content

Commit b417be2

Browse files
committed
Do not allow empty qualifiedName in createDocument.
Rearrange pulldom to create documents with root element. Provide clear methods so that the ContentHandler releases its hold on the document.
1 parent 269b83b commit b417be2

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

Lib/xml/dom/minidom.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -651,17 +651,23 @@ def createDocument(self, namespaceURI, qualifiedName, doctype):
651651
doc = Document()
652652
if doctype is None:
653653
doctype = self.createDocumentType(qualifiedName, None, None)
654-
if qualifiedName:
655-
prefix, localname = _nssplit(qualifiedName)
656-
if prefix == "xml" \
657-
and namespaceURI != "http://www.w3.org/XML/1998/namespace":
658-
raise xml.dom.NamespaceErr("illegal use of 'xml' prefix")
659-
if prefix and not namespaceURI:
660-
raise xml.dom.NamespaceErr(
661-
"illegal use of prefix without namespaces")
662-
element = doc.createElementNS(namespaceURI, qualifiedName)
663-
doc.appendChild(element)
664-
# XXX else, raise an error? Empty qname is illegal in the DOM spec!
654+
if not qualifiedName:
655+
# The spec is unclear what to raise here; SyntaxErr
656+
# would be the other obvious candidate. Since Xerces raises
657+
# InvalidCharacterErr, and since SyntaxErr is not listed
658+
# for createDocument, that seems to be the better choice.
659+
# XXX: need to check for illegal characters here and in
660+
# createElement.
661+
raise xml.dom.InvalidCharacterErr("Element with no name")
662+
prefix, localname = _nssplit(qualifiedName)
663+
if prefix == "xml" \
664+
and namespaceURI != "http://www.w3.org/XML/1998/namespace":
665+
raise xml.dom.NamespaceErr("illegal use of 'xml' prefix")
666+
if prefix and not namespaceURI:
667+
raise xml.dom.NamespaceErr(
668+
"illegal use of prefix without namespaces")
669+
element = doc.createElementNS(namespaceURI, qualifiedName)
670+
doc.appendChild(element)
665671
doctype.parentNode = doc
666672
doc.doctype = doctype
667673
doc.implementation = self
@@ -761,6 +767,7 @@ def _doparse(func, args, kwargs):
761767
events = apply(func, args, kwargs)
762768
toktype, rootNode = events.getEvent()
763769
events.expandNode(rootNode)
770+
events.clear()
764771
return rootNode
765772

766773
def parse(*args, **kwargs):

Lib/xml/dom/pulldom.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,17 @@ def startElementNS(self, name, tagName , attrs):
6161
tagName = prefix + ":" + localname
6262
else:
6363
tagName = localname
64-
node = self.document.createElementNS(uri, tagName)
64+
if self.document:
65+
node = self.document.createElementNS(uri, tagName)
66+
else:
67+
node = self.buildDocument(uri, tagName)
6568
else:
6669
# When the tagname is not prefixed, it just appears as
6770
# localname
68-
node = self.document.createElement(localname)
71+
if self.document:
72+
node = self.document.createElement(localname)
73+
else:
74+
node = self.buildDocument(None, localname)
6975

7076
for aname,value in attrs.items():
7177
a_uri, a_localname = aname
@@ -90,7 +96,10 @@ def endElementNS(self, name, tagName):
9096
self.lastEvent = self.lastEvent[1]
9197

9298
def startElement(self, name, attrs):
93-
node = self.document.createElement(name)
99+
if self.document:
100+
node = self.document.createElement(name)
101+
else:
102+
node = self.buildDocument(None, name)
94103

95104
for aname,value in attrs.items():
96105
attr = self.document.createAttribute(aname)
@@ -127,23 +136,28 @@ def characters(self, chars):
127136
self.lastEvent = self.lastEvent[1]
128137

129138
def startDocument(self):
130-
publicId = systemId = None
131-
if self._locator:
132-
publicId = self._locator.getPublicId()
133-
systemId = self._locator.getSystemId()
134139
if self.documentFactory is None:
135140
import xml.dom.minidom
136141
self.documentFactory = xml.dom.minidom.Document.implementation
137-
node = self.documentFactory.createDocument(None, publicId, systemId)
142+
143+
def buildDocument(self, uri, tagname):
144+
# Can't do that in startDocument, since we need the tagname
145+
# XXX: obtain DocumentType
146+
node = self.documentFactory.createDocument(uri, tagname, None)
138147
self.document = node
139148
self.lastEvent[1] = [(START_DOCUMENT, node), None]
140149
self.lastEvent = self.lastEvent[1]
141150
self.push(node)
151+
return node.firstChild
142152

143153
def endDocument(self):
144154
self.lastEvent[1] = [(END_DOCUMENT, self.document), None]
145155
self.pop()
146156

157+
def clear(self):
158+
"clear(): Explicitly release parsing structures"
159+
self.document = None
160+
147161
class ErrorHandler:
148162
def warning(self, exception):
149163
print exception
@@ -199,6 +213,13 @@ def getEvent(self):
199213
self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
200214
return rc
201215

216+
def clear(self):
217+
"clear(): Explicitly release parsing objects"
218+
self.pulldom.clear()
219+
del self.pulldom
220+
self.parser = None
221+
self.stream = None
222+
202223
class SAX2DOM(PullDOM):
203224

204225
def startElementNS(self, name, tagName , attrs):

0 commit comments

Comments
 (0)
X Tutup