[Python-checkins] CVS: python/dist/src/Lib/xml/dom minidom.py,1.38,1.39

Fred L. Drake fdrake@users.sourceforge.net
Fri, 28 Sep 2001 21:58:34 -0700


Update of /cvsroot/python/python/dist/src/Lib/xml/dom
In directory usw-pr-cvs1:/tmp/cvs-serv23226/xml/dom

Modified Files:
	minidom.py 
Log Message:
For Python 2.2, do not use __getattr__(), only use computed properties.
This is probably a little bit faster, but mostly is just cleaner code.
The old-style support is still used for Python versions < 2.2 so this
source file can be shared with PyXML.


Index: minidom.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xml/dom/minidom.py,v
retrieving revision 1.38
retrieving revision 1.39
diff -C2 -d -r1.38 -r1.39
*** minidom.py	2001/09/28 20:25:45	1.38
--- minidom.py	2001/09/29 04:58:32	1.39
***************
*** 69,99 ****
              Node.debug.write("create %s\n" % index)
  
-     def __getattr__(self, key):
-         if key[0:2] == "__":
-             raise AttributeError, key
-         # getattr should never call getattr!
-         if self.__dict__.has_key("inGetAttr"):
-             del self.inGetAttr
-             raise AttributeError, key
- 
-         prefix, attrname = key[:5], key[5:]
-         if prefix == "_get_":
-             self.inGetAttr = 1
-             if hasattr(self, attrname):
-                 del self.inGetAttr
-                 return (lambda self=self, attrname=attrname:
-                                 getattr(self, attrname))
-             else:
-                 del self.inGetAttr
-                 raise AttributeError, key
-         else:
-             self.inGetAttr = 1
-             try:
-                 func = getattr(self, "_get_" + key)
-             except AttributeError:
-                 raise AttributeError, key
-             del self.inGetAttr
-             return func()
- 
      def __nonzero__(self):
          return 1
--- 69,72 ----
***************
*** 125,128 ****
--- 98,136 ----
              return self.childNodes[-1]
  
+     try:
+         property
+     except NameError:
+         def __getattr__(self, key):
+             if key[0:2] == "__":
+                 raise AttributeError, key
+             # getattr should never call getattr!
+             if self.__dict__.has_key("inGetAttr"):
+                 del self.inGetAttr
+                 raise AttributeError, key
+ 
+             prefix, attrname = key[:5], key[5:]
+             if prefix == "_get_":
+                 self.inGetAttr = 1
+                 if hasattr(self, attrname):
+                     del self.inGetAttr
+                     return (lambda self=self, attrname=attrname:
+                                     getattr(self, attrname))
+                 else:
+                     del self.inGetAttr
+                     raise AttributeError, key
+             else:
+                 self.inGetAttr = 1
+                 try:
+                     func = getattr(self, "_get_" + key)
+                 except AttributeError:
+                     raise AttributeError, key
+                 del self.inGetAttr
+                 return func()
+     else:
+         firstChild = property(_get_firstChild,
+                               doc="First child node, or None.")
+         lastChild = property(_get_lastChild,
+                              doc="Last child node, or None.")
+ 
      def insertBefore(self, newChild, refChild):
          if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE:
***************
*** 363,370 ****
          self._attrsNS = attrsNS
  
!     def __getattr__(self, name):
!         if name == "length":
!             return len(self._attrs)
!         raise AttributeError, name
  
      def item(self, index):
--- 371,384 ----
          self._attrsNS = attrsNS
  
!     try:
!         property
!     except NameError:
!         def __getattr__(self, name):
!             if name == "length":
!                 return len(self._attrs)
!             raise AttributeError, name
!     else:
!         length = property(lambda self: len(self._attrs),
!                           doc="Number of nodes in the NamedNodeMap.")
  
      def item(self, index):
***************
*** 599,602 ****
--- 613,624 ----
          return AttributeList(self._attrs, self._attrsNS)
  
+     try:
+         property
+     except NameError:
+         pass
+     else:
+         attributes = property(_get_attributes,
+                               doc="NamedNodeMap of attributes on the element.")
+ 
      def hasAttributes(self):
          if self._attrs or self._attrsNS:
***************
*** 841,844 ****
--- 863,874 ----
              if node.nodeType == Node.ELEMENT_NODE:
                  return node
+ 
+     try:
+         property
+     except NameError:
+         pass
+     else:
+         documentElement = property(_get_documentElement,
+                                    doc="Top-level element of this document.")
  
      def unlink(self):