[Python-checkins] CVS: python/dist/src/Lib/xml/dom minidom.py,1.1,1.2 pulldom.py,1.1,1.2

Paul Prescod python-dev@python.org
2000年6月30日 21:58:50 -0700


Update of /cvsroot/python/python/dist/src/Lib/xml/dom
In directory slayer.i.sourceforge.net:/tmp/cvs-serv21246/dom
Modified Files:
	minidom.py pulldom.py 
Log Message:
Reference cycle fixes
Index: minidom.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xml/dom/minidom.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** minidom.py	2000年06月29日 19:39:57	1.1
--- minidom.py	2000年07月01日 04:58:47	1.2
***************
*** 30,38 ****
 NOTATION_NODE = 12
 
! allnodes=[]
 
 def __init__( self ):
 self.childNodes=[]
! Node.allnodes.append( repr( id( self ))+repr( self.__class__ ))
 
 def __getattr__( self, key ):
--- 30,46 ----
 NOTATION_NODE = 12
 
! allnodes={}
! _debug=0
! _makeParentNodes=1
! debug=None
 
 def __init__( self ):
 self.childNodes=[]
! if Node._debug: 
! index=repr( id( self ))+repr( self.__class__ )
! Node.allnodes[index]=repr( self.__dict__ )
! if Node.debug==None:
! Node.debug=open( "debug4.out", "w" )
! Node.debug.write( "create %s\n"%index )
 
 def __getattr__( self, key ):
***************
*** 73,82 ****
--- 81,117 ----
 else: return 0
 
+ def _get_firstChild( self ):
+ return self.childNodes[0]
+ 
+ def _get_lastChild( self ):
+ return self.childNodes[-1]
+ 
 def insertBefore( self, newChild, refChild):
 index=self.childNodes.index( refChild )
 self.childNodes.insert( index, newChild )
+ if self._makeParentNodes:
+ newChild.parentNode=self
 
 def appendChild( self, node ):
 self.childNodes.append( node )
+ return node
+ 
+ def replaceChild( self, newChild, oldChild ):
+ index=self.childNodes.index( oldChild )
+ self.childNodes[index]=oldChild
+ 
+ def removeChild( self, oldChild ):
+ index=self.childNodes.index( oldChild )
+ del self.childNodes[index]
+ 
+ def cloneNode( self, deep ):
+ import new
+ clone=new.instance( self.__class__, self.__dict__ )
+ clone.attributes=self.attributes.copy()
+ if not deep:
+ clone.childNodes=[]
+ else:
+ clone.childNodes=map( lambda x: x.cloneNode, self.childNodes )
+ return clone
 
 def unlink( self ):
***************
*** 87,95 ****
 self.childNodes=None
 if self.attributes:
! for attr in self.attributes.values():
! attr.unlink()
! self.attributes=None
! index=Node.allnodes.index( repr( id( self ))+repr( self.__class__ ))
! del Node.allnodes[index]
 
 def _write_data( writer, data):
--- 122,133 ----
 self.childNodes=None
 if self.attributes:
! for attr in self._attrs.values():
! self.removeAttributeNode( attr )
! assert not len( self._attrs )
! assert not len( self._attrsNS )
! if Node._debug:
! index=repr( id( self ))+repr( self.__class__ )
! self.debug.write( "Deleting: %s\n" % index )
! del Node.allnodes[index]
 
 def _write_data( writer, data):
***************
*** 101,109 ****
 writer.write(data)
 
- def _closeElement( element ):
- del element.parentNode
- for node in element.elements:
- _closeElement( node )
- 
 def _getElementsByTagNameHelper( parent, name, rc ):
 for node in parent.childNodes:
--- 139,142 ----
***************
*** 124,138 ****
 class Attr(Node):
 nodeType=Node.ATTRIBUTE_NODE
! def __init__( self, qName, namespaceURI="", prefix="",
! localName=None ):
! Node.__init__( self )
! assert qName
 # skip setattr for performance
- self.__dict__["nodeName"] = self.__dict__["name"] = qName
 self.__dict__["localName"]=localName or qName
! self.__dict__["prefix"]=prefix
 self.__dict__["namespaceURI"]=namespaceURI
! # nodeValue and value are set elsewhere
 self.attributes=None
 
 def __setattr__( self, name, value ):
--- 157,170 ----
 class Attr(Node):
 nodeType=Node.ATTRIBUTE_NODE
! def __init__( self, qName, namespaceURI="", localName=None,
! prefix=None ):
 # skip setattr for performance
 self.__dict__["localName"]=localName or qName
! self.__dict__["nodeName"] = self.__dict__["name"] = qName
 self.__dict__["namespaceURI"]=namespaceURI
! self.__dict__["prefix"]=prefix
 self.attributes=None
+ Node.__init__( self )
+ # nodeValue and value are set elsewhere
 
 def __setattr__( self, name, value ):
***************
*** 143,152 ****
 
 class AttributeList:
! # the attribute list is a transient interface to the underlying dictionaries
! # mutations here will change the underlying element's dictionary
 def __init__( self, attrs, attrsNS ):
! self.__attrs=attrs
! self.__attrsNS=attrs
! self.length=len( self.__attrs.keys() )
 
 def item( self, index ):
--- 175,185 ----
 
 class AttributeList:
! """the attribute list is a transient interface to the underlying
! dictionaries. mutations here will change the underlying element's
! dictionary"""
 def __init__( self, attrs, attrsNS ):
! self._attrs=attrs
! self._attrsNS=attrsNS
! self.length=len( self._attrs.keys() )
 
 def item( self, index ):
***************
*** 158,175 ****
 def items( self ):
 return map( lambda node: (node.tagName, node.value),
! self.__attrs.values() )
 
 def itemsNS( self ):
 return map( lambda node: ((node.URI, node.localName), node.value),
! self.__attrs.values() )
 
 def keys( self ):
! return self.__attrs.keys()
 
 def keysNS( self ):
! return self.__attrsNS.keys()
 
 def values( self ):
! return self.__attrs.values()
 
 def __len__( self ):
--- 191,208 ----
 def items( self ):
 return map( lambda node: (node.tagName, node.value),
! self._attrs.values() )
 
 def itemsNS( self ):
 return map( lambda node: ((node.URI, node.localName), node.value),
! self._attrs.values() )
 
 def keys( self ):
! return self._attrs.keys()
 
 def keysNS( self ):
! return self._attrsNS.keys()
 
 def values( self ):
! return self._attrs.values()
 
 def __len__( self ):
***************
*** 177,181 ****
 
 def __cmp__( self, other ):
! if self.__attrs is other.__attrs: 
 return 0
 else: 
--- 210,214 ----
 
 def __cmp__( self, other ):
! if self._attrs is getattr( other, "_attrs", None ):
 return 0
 else: 
***************
*** 184,195 ****
 #FIXME: is it appropriate to return .value?
 def __getitem__( self, attname_or_tuple ):
! if type( attname_or_tuple ) == type( (1,2) ):
! return self.__attrsNS[attname_or_tuple].value
 else:
! return self.__attrs[attname_or_tuple].value
 
 def __setitem__( self, attname ):
 raise TypeError, "object does not support item assignment"
! 
 class Element( Node ):
 nodeType=Node.ELEMENT_NODE
--- 217,234 ----
 #FIXME: is it appropriate to return .value?
 def __getitem__( self, attname_or_tuple ):
! if type( attname_or_tuple ) == type( () ):
! return self._attrsNS[attname_or_tuple]
 else:
! return self._attrs[attname_or_tuple]
 
 def __setitem__( self, attname ):
 raise TypeError, "object does not support item assignment"
! 
! def __delitem__( self, attname_or_tuple ):
! node=self[attname_or_tuple]
! node.unlink()
! del self._attrs[node.name]
! del self._attrsNS[(node.namespaceURI, node.localName)]
! 
 class Element( Node ):
 nodeType=Node.ELEMENT_NODE
***************
*** 203,208 ****
 self.nodeValue=None
 
! self.__attrs={} # attributes are double-indexed:
! self.__attrsNS={}# tagName -> Attribute
 # URI,localName -> Attribute
 # in the future: consider lazy generation of attribute objects
--- 242,247 ----
 self.nodeValue=None
 
! self._attrs={} # attributes are double-indexed:
! self._attrsNS={}# tagName -> Attribute
 # URI,localName -> Attribute
 # in the future: consider lazy generation of attribute objects
***************
*** 211,218 ****
 
 def getAttribute( self, attname ):
! return self.__attrs[attname].value
 
 def getAttributeNS( self, namespaceURI, localName ):
! return self.__attrsNS[(namespaceURI, localName)].value
 
 def setAttribute( self, attname, value ):
--- 250,257 ----
 
 def getAttribute( self, attname ):
! return self._attrs[attname].value
 
 def getAttributeNS( self, namespaceURI, localName ):
! return self._attrsNS[(namespaceURI, localName)].value
 
 def setAttribute( self, attname, value ):
***************
*** 223,246 ****
 
 def setAttributeNS( self, namespaceURI, qualifiedName, value ):
! attr=createAttributeNS( namespaceURI, qualifiedName )
 # for performance
 attr.__dict__["value"]=attr.__dict__["nodeValue"]=value
 self.setAttributeNode( attr )
 
 def setAttributeNode( self, attr ):
! self.__attrs[attr.name]=attr
! self.__attrsNS[(attr.namespaceURI,attr.localName)]=attr
 
 def removeAttribute( self, name ):
! attr = self.__attrs[name]
 self.removeAttributeNode( attr )
 
 def removeAttributeNS( self, namespaceURI, localName ):
! attr = self.__attrsNS[(uri, localName)]
 self.removeAttributeNode( attr )
 
 def removeAttributeNode( self, node ):
! del self.__attrs[node.name]
! del self.__attrsNS[(node.namespaceURI, node.localName)]
 
 def getElementsByTagName( self, name ):
--- 262,296 ----
 
 def setAttributeNS( self, namespaceURI, qualifiedName, value ):
! prefix,localname=_nssplit( qualifiedName )
 # for performance
+ attr = Attr( qualifiedName, namespaceURI, localname, prefix )
 attr.__dict__["value"]=attr.__dict__["nodeValue"]=value
 self.setAttributeNode( attr )
 
+ def getAttributeNode( self, attrname ):
+ return self._attrs.get( attrname )
+ 
+ def getAttributeNodeNS( self, namespaceURI, localName ):
+ return self._attrsNS[(namespaceURI, localName)]
+ 
 def setAttributeNode( self, attr ):
! old=self._attrs.get( attr.name, None)
! if old:
! old.unlink()
! self._attrs[attr.name]=attr
! self._attrsNS[(attr.namespaceURI,attr.localName)]=attr
 
 def removeAttribute( self, name ):
! attr = self._attrs[name]
 self.removeAttributeNode( attr )
 
 def removeAttributeNS( self, namespaceURI, localName ):
! attr = self._attrsNS[(namespaceURI, localName)]
 self.removeAttributeNode( attr )
 
 def removeAttributeNode( self, node ):
! node.unlink()
! del self._attrs[node.name]
! del self._attrsNS[(node.namespaceURI, node.localName)]
 
 def getElementsByTagName( self, name ):
***************
*** 272,276 ****
 
 def _get_attributes( self ):
! return AttributeList( self.__attrs, self.__attrsNS )
 
 class Comment( Node ):
--- 322,326 ----
 
 def _get_attributes( self ):
! return AttributeList( self._attrs, self._attrsNS )
 
 class Comment( Node ):
***************
*** 314,326 ****
 _write_data( writer, self.data )
 
 class Document( Node ):
 nodeType=Node.DOCUMENT_NODE
 def __init__( self ):
 Node.__init__( self )
- self.documentElement=None
 self.attributes=None
 self.nodeName="#document"
 self.nodeValue=None
 
 createElement=Element
 
--- 364,391 ----
 _write_data( writer, self.data )
 
+ def _nssplit( qualifiedName ):
+ fields = string.split(qualifiedName, ':')
+ if len(fields) == 2:
+ return fields
+ elif len(fields) == 1:
+ return( '', fields[0] )
+ 
 class Document( Node ):
 nodeType=Node.DOCUMENT_NODE
+ documentElement=None
 def __init__( self ):
 Node.__init__( self )
 self.attributes=None
 self.nodeName="#document"
 self.nodeValue=None
 
+ def appendChild( self, node ):
+ if node.nodeType==Node.ELEMENT_NODE and self.documentElement:
+ raise TypeError, "Two document elements disallowed"
+ else:
+ self.documentElement=node
+ Node.appendChild( self, node )
+ return node
+ 
 createElement=Element
 
***************
*** 334,362 ****
 
 def createElementNS(self, namespaceURI, qualifiedName):
! fields = string.split(qualifiedName, ':')
! if len(fields) == 2:
! prefix = fields[0]
! localName = fields[1]
! elif len(fields) == 1:
! prefix = ''
! localName = fields[0] 
! return Element(self, qualifiedName, namespaceURI, prefix, localName)
 
 def createAttributeNS(self, namespaceURI, qualifiedName):
! fields = string.split(qualifiedName,':')
! if len(fields) == 2:
! localName = fields[1]
! prefix = fields[0]
! elif len(fields) == 1:
! localName = fields[0]
! prefix = None
! return Attr(qualifiedName, namespaceURI, prefix, localName)
 
 def getElementsByTagNameNS(self,namespaceURI,localName):
 _getElementsByTagNameNSHelper( self, namespaceURI, localName )
- 
- def close( self ):
- for node in self.elements:
- _closeElement( node )
 
 def unlink( self ):
--- 399,411 ----
 
 def createElementNS(self, namespaceURI, qualifiedName):
! prefix,localName=_nssplit( qualifiedName )
! return Element(qualifiedName, namespaceURI, prefix, localName)
 
 def createAttributeNS(self, namespaceURI, qualifiedName):
! prefix,localName=_nssplit( qualifiedName )
! return Attr(namespaceURI, qualifiedName, localName, prefix)
 
 def getElementsByTagNameNS(self,namespaceURI,localName):
 _getElementsByTagNameNSHelper( self, namespaceURI, localName )
 
 def unlink( self ):
Index: pulldom.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xml/dom/pulldom.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** pulldom.py	2000年06月29日 19:39:57	1.1
--- pulldom.py	2000年07月01日 04:58:47	1.2
***************
*** 3,7 ****
 import string
 import sys
- import pyexpat
 from xml.sax import ExpatParser
 
--- 3,6 ----
***************
*** 141,150 ****
 
 if token !=END_ELEMENT:
! cur_node.parentNode.childNodes.append( cur_node )
 event=self.getEvent()
- if node.nodeType==minidom.Node.DOCUMENT_NODE:
- for child in node.childNodes:
- if child.nodeType==minidom.Node.ELEMENT_NODE:
- node.documentElement=child
 
 def getEvent( self ):
--- 140,145 ----
 
 if token !=END_ELEMENT:
! cur_node.parentNode.appendChild( cur_node )
 event=self.getEvent()
 
 def getEvent( self ):
***************
*** 194,267 ****
 
 bufsize=len( string )
! stringio( string )
 parser=_getParser()
 return DOMEventStream( buf, parser, bufsize )
 
- #FIXME: Use Lars' instead!!!
- class SAX_expat:
- "SAX driver for the Pyexpat C module."
- 
- def __init__(self):
- self.parser=pyexpat.ParserCreate()
- self.started=0
- 
- def setDocumentHandler( self, handler ):
- self.parser.StartElementHandler = handler.startElement
- self.parser.EndElementHandler = handler.endElement
- self.parser.CharacterDataHandler = handler.datachars
- self.parser.ProcessingInstructionHandler = handler.processingInstruction
- self.doc_handler=handler
- 
- def setErrorHandler( self, handler ):
- self.err_handler=handler
- 
- # --- Locator methods. Only usable after errors.
- 
- def getLineNumber(self):
- return self.parser.ErrorLineNumber
- 
- def getColumnNumber(self):
- return self.parser.ErrorColumnNumber 
- 
- # --- Internal
- 
- def __report_error(self):
- msg=pyexpat.ErrorString(self.parser.ErrorCode)
- self.err_handler.fatalError(msg)
- 
- # --- EXPERIMENTAL PYTHON SAX EXTENSIONS
- 
- def get_parser_name(self):
- return "pyexpat"
- 
- def get_parser_version(self):
- return "Unknown"
- 
- def get_driver_version(self):
- return version
- 
- def is_validating(self):
- return 0
- 
- def is_dtd_reading(self):
- return 0
- 
- def reset(self):
- self.parser=pyexpat.ParserCreate()
- self.parser.StartElementHandler = self.startElement
- self.parser.EndElementHandler = self.endElement
- self.parser.CharacterDataHandler = self.characters
- self.parser.ProcessingInstructionHandler = self.processingInstruction
- 
- def feed(self,data):
- if not self.started:
- self.doc_handler.startDocument()
- self.started=1 
- if not self.parser.Parse(data):
- self.__report_error()
- 
- def close(self):
- if not self.parser.Parse("",1):
- self.__report_error()
- self.doc_handler.endDocument()
- self.parser = None
--- 189,194 ----
 
 bufsize=len( string )
! buf=stringio( string )
 parser=_getParser()
 return DOMEventStream( buf, parser, bufsize )
 

AltStyle によって変換されたページ (->オリジナル) /