classification
Title: Bug in minidom 3.3 after optimization patch
Type: behavior Stage: needs patch
Components: Library (Lib), XML Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eric.araujo, loewis, python-dev, r.david.murray, vinay.sajip
Priority: normal Keywords: easy

Created on 2012-03-01 17:21 by vinay.sajip, last changed 2012-03-05 06:02 by loewis. This issue is now closed.

Messages (7)
msg154705 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2012-03-01 17:21
The following script, minidom_test.py,

from xml.dom import minidom

data = b'''
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <link>https://example.com/blog/</link>
    <atom:link href="https://example.com/rss2/" rel="self"></atom:link>
    <item>
      <link>https://example.com/blog/1/</link>
    </item>
  </channel>
</rss>'''

doc = minidom.parseString(data)
for link in doc.getElementsByTagName('link'):
    print(link._attrs)

produces different results in Python 3.2 and 3.3:

vinay@eta-oneiric64:~/projects/scratch$ python3.2 minidom_test.py 
{}
{}
vinay@eta-oneiric64:~/projects/scratch$ python3.3 minidom_test.py 
None
None
msg154722 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-03-02 03:34
That would be caused by Martin’s change in 5d27a32ebbcc.  I don’t see the docs marking _attrs public, so I think this is not a bug.
msg154751 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2012-03-02 08:34
The error which prompted this issue was not caused by external code peeking into the internals - it was just my toy test script which did that. The error came up in Django testing:

======================================================================
ERROR: test_secure_urls (regressiontests.syndication.tests.SyndicationFeedTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/vinay/projects/django3/tests/regressiontests/syndication/tests.py", line 255, in test_secure_urls
    if link.getAttribute('rel') == 'self':
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/xml/dom/minidom.py", line 727, in getAttribute
    return self._attrs[attname].value
TypeError: 'NoneType' object is not subscriptable

So, perhaps there is a missing _ensure_attributes() call somewhere.
msg154752 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2012-03-02 08:54
Upon inspection, _ensure_attributes() is only called in _get_attributes(), setAttributeNode() and _set_attribute_node(). The last of these is only called by setAttributeNode(), and it would appear that setAttributeNode() is only called if there *are* attributes.

_get_attributes() is only called by writexml().

So, calls to public methods getAttribute(), getAttributeNS() and a few others are likely to fail because, if there are no attributes in the XML being processed, _ensure_attributes() wouldn't get called.

It looks like a bug to me! ;-)
msg154753 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-03-02 09:05
Agreed :)  Needs unit tests that use the public attributes.
msg154911 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-03-04 20:51
Instead of calling _ensure_attributes (which creates the attribute dictionaries), getAttribute should return "" immediately if there are no attributes at all, to avoid creating those dictionaries.

I'll do a patch shortly, unless somebody beats me.
msg154927 - (view) Author: Roundup Robot (python-dev) Date: 2012-03-05 06:01
New changeset 73c76466cf44 by Martin v. Löwis in branch 'default':
Issue #14168: Check for presence of _attrs before accessing it.
http://hg.python.org/cpython/rev/73c76466cf44
History
Date User Action Args
2012-03-05 06:02:34loewissetstatus: open -> closed
resolution: fixed
2012-03-05 06:01:57python-devsetnosy: + python-dev
messages: + msg154927
2012-03-04 20:51:30loewissetmessages: + msg154911
2012-03-02 09:05:30eric.araujosetkeywords: + easy, - 3.2regression

title: minidom behaves differently in 3.3 compared to 3.2 -> Bug in minidom 3.3 after optimization patch
messages: + msg154753
stage: needs patch
2012-03-02 08:54:11vinay.sajipsetmessages: + msg154752
2012-03-02 08:34:42vinay.sajipsetmessages: + msg154751
2012-03-02 03:34:07eric.araujosetnosy: + eric.araujo
messages: + msg154722
2012-03-01 17:21:38vinay.sajipcreate