classification
Title: XML NamedNodeMap ( attribName in NamedNodeMap fails )
Type: crash Stage:
Components: XML Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, spolematt
Priority: normal Keywords:

Created on 2011-09-14 23:11 by spolematt, last changed 2013-07-08 17:03 by christian.heimes. This issue is now closed.

Files
File name Uploaded Description Edit
show_bug.py spolematt, 2011-09-14 23:11 script to demonstrate the problem
Messages (2)
msg144060 - (view) Author: Matthew Newcomb (spolematt) Date: 2011-09-14 23:11
I was cleaning up some old code to make it pep8 compliant and came across this bug.  Switching from 'has_key' to 'in' does not work with a xml.dom.minidom.NamedNodeMap.  An easy solution appears to be to add a '__contains__' method to NamedNodeMap.  

This is with:
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2

I've tried it with 2.4.3 as well ( on some older machines ).

import xml.dom.minidom

def show_bug():
    document = '<dom mbid="5">foobar_attributes</dom>'
    dom = xml.dom.minidom.parseString(document)
    attribs = dom.getElementsByTagName('dom')[0].attributes

    if attribs.has_key('mbid'):
        print "This works..  found 'mbid' attribute"

    if 'mbid' in attribs:
        print "Will never get here, the above will throw an exception"


>>> show_bug()
This works..  found 'mbid' attribute
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "show_bug.py", line 11, in show_bug
    if 'mbid' in attribs:
  File "/usr/lib/python2.7/xml/dom/minidom.py", line 524, in __getitem__
    return self._attrs[attname_or_tuple]
KeyError: 0
msg192671 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-08 17:03
Thanks for your report. The issue has been address in Python 3.x. NamedNodeMap properly implements __contains__(). Python 2.7 is in feature freeze mode which means we can't backport the function.
History
Date User Action Args
2013-07-08 17:03:33christian.heimessetstatus: open -> closed

nosy: + christian.heimes
messages: + msg192671

resolution: fixed
2011-09-14 23:11:47spolemattcreate