Skip to content

Conversation

@encukou
Copy link
Member

@encukou encukou commented Dec 16, 2025

Element uses __slots__, which means that the ownerDocument
attribute is not inherited from the superclass.
All other slots (except _localName) are set in __init__; not
including ownerDocument was apparently an oversight.

Note that creating Element directly is not supported; you're supposed
to use Document APIs. AFAICS, all supported ways of creating elements do
set ownerDocument.
However, many projects ignore the documentation, and a security fix that
relies on ownerDocument can break them.

Element uses __slots__, which means that the attribute is not inherited
from the superclass.
All other slots (except _localName) are set in __init__; not including
ownerDocument was an oversight.

Note that creating Element directly is not supported; you're supposed
to use Document APIs. All supported ways of creating elements do set
ownerDocument.
However, many projects ignore the documentation.
@encukou encukou changed the title gh-142754: Ensure that Elements have the ownerDocument attribute gh-142754: Ensure that Element & Attr instances have the ownerDocument attribute Dec 16, 2025
@hugovk
Copy link
Member

hugovk commented Dec 16, 2025

Here's a test case:

    def testSetAttributeNodeWithoutOwnerDocument(self):
        elem = Element("test")
        attr = Attr("id")
        attr.value = "test-id"

        elem.setAttributeNode(attr)

        self.assertEqual(elem.getAttribute("id"), "test-id")

@encukou encukou added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Dec 16, 2025
@hugovk hugovk enabled auto-merge (squash) December 16, 2025 11:59
@hugovk hugovk merged commit 1cc7551 into python:main Dec 16, 2025
50 checks passed
@miss-islington-app
Copy link

Thanks @encukou for the PR, and @hugovk for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 16, 2025
…ocument attribute (pythonGH-142794)

(cherry picked from commit 1cc7551)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 16, 2025
…ocument attribute (pythonGH-142794)

(cherry picked from commit 1cc7551)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Dec 16, 2025

GH-142818 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Dec 16, 2025
@bedevere-app
Copy link

bedevere-app bot commented Dec 16, 2025

GH-142819 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Dec 16, 2025
@hugovk
Copy link
Member

hugovk commented Dec 16, 2025

Thanks for the fix!

@jacobtylerwalls
Copy link
Contributor

Lovely. Does this also close #67624?

gpshead pushed a commit that referenced this pull request Dec 20, 2025
…Document attribute (GH-142794) (#142818)

gh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (GH-142794)
(cherry picked from commit 1cc7551)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
gpshead pushed a commit that referenced this pull request Dec 20, 2025
…Document attribute (GH-142794) (#142819)

gh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (GH-142794)
(cherry picked from commit 1cc7551)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
gpshead pushed a commit to miss-islington/cpython that referenced this pull request Dec 21, 2025
… ownerDocument attribute (pythonGH-142794) (python#142818)

pythongh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (pythonGH-142794)
(cherry picked from commit 1cc7551)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Yhg1s pushed a commit that referenced this pull request Dec 22, 2025
…GH-142146) (#142211)

* gh-142145: Remove quadratic behavior in node ID cache clearing (GH-142146)
* gh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (GH-142794)
(cherry picked from commit 1cc7551)
(cherry picked from commit 08d8e18)
(cherry picked from commit 8d2d7bb)

Co-authored-by: Jacob Walls <38668450+jacobtylerwalls@users.noreply.github.com>
Co-authored-by: Seth Michael Larson <seth@python.org>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
gpshead pushed a commit to miss-islington/cpython that referenced this pull request Dec 22, 2025
… ownerDocument attribute (pythonGH-142794) (python#142818)

pythongh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (pythonGH-142794)
(cherry picked from commit 1cc7551)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
gpshead pushed a commit to miss-islington/cpython that referenced this pull request Dec 22, 2025
… ownerDocument attribute (pythonGH-142794) (python#142818)

pythongh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (pythonGH-142794)
(cherry picked from commit 1cc7551)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants