Issue 15290: setAttribute() can fail - Python tracker

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
process
Status: closed Resolution: duplicate
Dependencies: Superseder: xml.dom.minidom.Element.cloneNode fails with AttributeError
View: 4851
Assigned To: Nosy List: Craig Rodrigues, loewis, pitrou
Priority: high Keywords: patch

Created on 2012-07-07 23:32 by pitrou, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
minidom_setattribute.patch pitrou, 2012-07-08 12:09 review
Messages (7)
msg164954 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-07 23:32
This is a regression from 3.2:

Traceback (most recent call last):
  File "/home/antoine/3sted/twisted/lore/test/test_lore.py", line 542, in test_setTitle
    secondTitle.setAttribute('class', 'title')
  File "/home/antoine/opt/lib/python3.3/xml/dom/minidom.py", line 743, in setAttribute
    attr.ownerDocument = self.ownerDocument
builtins.AttributeError: ownerDocument
msg165007 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-08 11:57
Trivial reproducer:

>>> e = minidom.Element("span")
>>> e.setAttribute("class", "version")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/antoine/cpython/default/Lib/xml/dom/minidom.py", line 743, in setAttribute
    attr.ownerDocument = self.ownerDocument
AttributeError: ownerDocument
msg165008 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-08 12:09
Here is a patch.
msg165009 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-07-08 12:29
That's invalid usage. You are supposed to create new element nodes through the document's createElement method. "unattached" nodes are not supported.
msg165010 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-08 12:31
> That's invalid usage. You are supposed to create new element nodes
> through the document's createElement method. "unattached" nodes are
> not supported.

Well, I don't know how many third-party application rely on this, but
Twisted does it.
msg165011 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-07-08 12:58
Then Twisted will have to change. From

http://docs.python.org/library/xml.dom.minidom.html

"Applications should not instantiate the classes themselves; they should use the creator functions available on the Document object."

If they insist on not using createElement, they use internal API, and need to adjust that to new Python versions.

Closing as invalid.
msg289063 - (view) Author: Craig Rodrigues (Craig Rodrigues) Date: 2017-03-06 03:15
Same issue was brought up in http://bugs.python.org/issue4851