Skip to content

xml.dom.Attr.name is not labeled as read-only #57336

Open

Description

@dillona
mannequin
BPO 13127
Nosy @terryjreedy, @scoder, @ezio-melotti, @csabella, @epicfaace, @potomak
PRs
  • gh-57336: Fix attr name setter behavior #14757
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2011-10-08.04:18:11.489>
    labels = ['easy', '3.8', 'expert-XML', '3.7', 'library', 'docs']
    title = 'xml.dom.Attr.name is not labeled as read-only'
    updated_at = <Date 2019-07-13.22:51:14.809>
    user = 'https://bugs.python.org/dillona'

    bugs.python.org fields:

    activity = <Date 2019-07-13.22:51:14.809>
    actor = 'potomak'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation', 'Library (Lib)', 'XML']
    creation = <Date 2011-10-08.04:18:11.489>
    creator = 'dillona'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 13127
    keywords = ['patch', 'easy']
    message_count = 10.0
    messages = ['145155', '145156', '145282', '145567', '148788', '333081', '340206', '340211', '346582', '347860']
    nosy_count = 9.0
    nosy_names = ['terry.reedy', 'scoder', 'ezio.melotti', 'docs@python', 'dillona', 'urjitsb87', 'cheryl.sabella', 'epicfaace', 'potomak']
    pr_nums = ['14757']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue13127'
    versions = ['Python 3.7', 'Python 3.8']

    Linked PRs

    Activity

    1. dillona commented on Oct 8, 2011

      dillonamannequin
      MannequinAuthor

      http://docs.python.org/library/xml.dom.html#dom-attr-objects does not label the name attribute as read-only (like localName) even though it is.

    2. dillona commented on Oct 8, 2011

      dillonamannequin
      MannequinAuthor

      The behavior can be verified as follows. Notice that despite changing the value, the output XML does not change

      >>> from xml.dom import minidom, Node
      >>> a = minidom.parseString("<a href=\"http://asd.com\">asd</a>")
      >>> a
      <xml.dom.minidom.Document instance at 0x22d7c68>
      >>> a.childNodes
      [<DOM Element: a at 0x22d7d88>]
      >>> a.childNodes[0]
      <DOM Element: a at 0x22d7d88>
      >>> a.childNodes[0].attributes
      <xml.dom.minidom.NamedNodeMap object at 0x21caa70>
      >>> a.childNodes[0].attributes.item(0)
      <xml.dom.minidom.Attr instance at 0x22d7f38>
      >>> a.childNodes[0].attributes.item(0).name
      u'href'
      >>> attr = a.childNodes[0].attributes.item(0)
      >>> attr.name = "ad"
      >>> a.toxml()
      u'<?xml version="1.0" ?><a href="http://asd.com">asd</a>'
      >>> attr.value = "asd"
      >>> a.toxml()
      u'<?xml version="1.0" ?><a href="asd">asd</a>'
    3. ezio-melotti commented on Oct 9, 2011

      @ezio-melotti
      Member

      localName is defined with defproperty() in Lib/xml/dom/minidom.py:464 and looking at the definition of defproperty() in Lib/xml/dom/minicompat.py:97 I think this is supposed to raise an xml.dom.NoModificationAllowedErr exception when someone tries to write on the attribute. This doesn't seem to happen though.
      OTOH 'name' doesn't use defproperty(), so technically it's writable, expect that writing on it has no effect.
      This should still be documented, and it would also be good to figure out what's going on with defproperty().

    4. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Oct 9, 2011
    5. terryjreedy commented on Oct 14, 2011

      @terryjreedy
      Member

      The docs are unchanged in 3.2. I presume the behavior is also.

    6. urjitsb87 commented on Dec 3, 2011

      urjitsb87mannequin
      Mannequin

      Using the same code example as above, I added a simple print statement in the set method being defined in the defproperty and it fired correctly for -

      a.childNodes[0].attributes='something'
      **My print statement:**in defproperty set for name: attributes
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/home/urjit/code/mercurial/python/cpython/Lib/xml/dom/minicompat.py", line 106, in set
          "attempt to modify read-only attribute " + repr(name))
      xml.dom.NoModificationAllowedErr: attempt to modify read-only attribute 'attributes'

      The getter seems to work fine for localName but the setter somehow does not fire.

      I have a fix for this but when I am running the test on my ubuntu vm, it doesnt go beyond test_sndhdr. However, when I run that test directly, it is successful. I can submit a patch if this problem is fixed.

    7. epicfaace commented on Jan 5, 2019

      epicfaacemannequin
      Mannequin

      This behavior appears to be working as expected per the documentation when using Python 3.7.1. I am able to change name, but changing localName gives me a NoModificationAllowedErr error.

    8. csabella commented on Apr 14, 2019

      @csabella
      Contributor

      Stefan,

      Do you think this should be documented?

    9. scoder commented on Apr 14, 2019

      @scoder
      Contributor

      The intended interface seems to be to change .name rather than .localName, so yes, that should be documented somewhere.

      The implementation seems a bit funny in that a "self._localName", if set, takes precedence, but there doesn't seem to be an official way to set it. The Attr() constructor even takes a "localname" argument, which it then ignores. o_O

    10. potomak commented on Jun 26, 2019

      potomakmannequin
      Mannequin

      I took a quick look at minidom.py and test_minidom.py. It seems that you should always use doc.renameNode(attr, namespace, new_name) for renaming an attribute (or an element).

      When doc.renameNode is applied on an attribute node, it removes the attribute from the ownerElement and re-set it after renaming it.

      For instance:

      >>> import xml.dom.minidom
      >>> from xml.dom import minidom
      >>> doc = minidom.parseString("<a href=\"http://foo.com\">foo</a>")
      >>> attr = doc.childNodes[0].attributes.item(0)
      >>> doc.renameNode(attr, xml.dom.EMPTY_NAMESPACE, "bar")
      <xml.dom.minidom.Attr object at 0x7fb27d7ddb00>
      >>> doc.toxml()
      '<?xml version="1.0" ?><a bar="http://foo.com">foo</a>'
      

      I agree that this behavior should be documented somewhere.

      Maybe there should be a note/warning in the name attribute description. It should says that resetting an attribute name won't change the document representation and that doc.renameNode should be used instead.

      Another approach may be to update the name setter implementation to remove and then re-set the attribute in the ownerElement.

      What do you think it's the best approach:

      1. update the doc
      2. update the name setter implementation

      I'd be happy to help to fix this issue.

    11. potomak commented on Jul 13, 2019

      potomakmannequin
      Mannequin

      In #14757 I tried updating the implementation of Attr._set_name to remove and reset the attr node in the owner element. So now Attr._set_name behaves similarly to Document.renameNode.

      All existing tests are still passing and I added one more test for checking the issue described here.

    12. transferred this issue fromon Apr 10, 2022
    13. serhiy-storchaka commented on Dec 26, 2023

      @serhiy-storchaka
      Member

      I think that it is better to document the limitation, and add a deprecation warning for name setter.

      In the DOM model Node attributes are readonly. https://dom.spec.whatwg.org/#interface-node

    14. added a commit that references this issue on Aug 12, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.16new features, bugs and security fixesdocsDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directorytopic-XMLtype-featureA feature request or enhancement

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions