[Python-checkins] cpython: Issue #14168: Check for presence of _attrs before accessing it.

martin.v.loewis python-checkins at python.org
Mon Mar 5 07:01:57 CET 2012


http://hg.python.org/cpython/rev/73c76466cf44
changeset:   75407:73c76466cf44
user:        Martin v. Löwis <martin at v.loewis.de>
date:        Mon Mar 05 07:01:49 2012 +0100
summary:
  Issue #14168: Check for presence of _attrs before accessing it.

files:
  Lib/test/test_minidom.py |  15 ++++++++++++---
  Lib/xml/dom/minidom.py   |   4 ++++
  Misc/NEWS                |   2 ++
  3 files changed, 18 insertions(+), 3 deletions(-)


diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py
--- a/Lib/test/test_minidom.py
+++ b/Lib/test/test_minidom.py
@@ -362,11 +362,17 @@
     def testGetAttrList(self):
         pass
 
-    def testGetAttrValues(self): pass
+    def testGetAttrValues(self):
+        pass
 
-    def testGetAttrLength(self): pass
+    def testGetAttrLength(self):
+        pass
 
-    def testGetAttribute(self): pass
+    def testGetAttribute(self):
+        dom = Document()
+        child = dom.appendChild(
+            dom.createElementNS("http://www.python.org", "python:abc"))
+        self.assertEqual(child.getAttribute('missing'), '')
 
     def testGetAttributeNS(self):
         dom = Document()
@@ -378,6 +384,9 @@
             'http://www.python.org')
         self.assertEqual(child.getAttributeNS("http://www.w3.org", "other"),
             '')
+        child2 = child.appendChild(dom.createElement('abc'))
+        self.assertEqual(child2.getAttributeNS("http://www.python.org", "missing"),
+                         '')
 
     def testGetAttributeNode(self): pass
 
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
--- a/Lib/xml/dom/minidom.py
+++ b/Lib/xml/dom/minidom.py
@@ -723,12 +723,16 @@
         Node.unlink(self)
 
     def getAttribute(self, attname):
+        if self._attrs is None:
+            return ""
         try:
             return self._attrs[attname].value
         except KeyError:
             return ""
 
     def getAttributeNS(self, namespaceURI, localName):
+        if self._attrsNS is None:
+            return ""
         try:
             return self._attrsNS[(namespaceURI, localName)].value
         except KeyError:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -511,6 +511,8 @@
 Library
 -------
 
+- Issue #14168: Check for presence of _attrs before accessing it.
+
 - Issue #14195: An issue that caused weakref.WeakSet instances to incorrectly
   return True for a WeakSet instance 'a' in both 'a < a' and 'a > a' has been
   fixed.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list