Skip to content

Conversation

@encukou
Copy link
Member

@encukouencukou 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.
@encukouencukou changed the title gh-142754: Ensure that Elements have the ownerDocument attributegh-142754: Ensure that Element & Attr instances have the ownerDocument attributeDec 16, 2025
@hugovk
Copy link
Member

Here's a test case:

deftestSetAttributeNodeWithoutOwnerDocument(self): elem=Element("test") attr=Attr("id") attr.value="test-id"elem.setAttributeNode(attr) self.assertEqual(elem.getAttribute("id"), "test-id")

@encukouencukou added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Dec 16, 2025
@hugovkhugovk enabled auto-merge (squash) December 16, 2025 11:59
@hugovkhugovk merged commit 1cc7551 into python:mainDec 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

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

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

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

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

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>
hroncok pushed a commit to fedora-python/cpython that referenced this pull request Jan 7, 2026
* pythongh-142145: Remove quadratic behavior in node ID cache clearing (pythonGH-142146) * pythongh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (pythonGH-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>
frenzymadness pushed a commit to frenzymadness/cpython that referenced this pull request Jan 8, 2026
* pythongh-142145: Remove quadratic behavior in node ID cache clearing (pythonGH-142146) * pythongh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (pythonGH-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>
frenzymadness pushed a commit to fedora-python/cpython that referenced this pull request Jan 12, 2026
* pythongh-142145: Remove quadratic behavior in node ID cache clearing (pythonGH-142146) * pythongh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (pythonGH-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>
@encukouencukou deleted the fix-142754 branch January 13, 2026 14:47
frenzymadness pushed a commit to frenzymadness/cpython that referenced this pull request Jan 14, 2026
* pythongh-142145: Remove quadratic behavior in node ID cache clearing (pythonGH-142146) * pythongh-142754: Ensure that Element & Attr instances have the ownerDocument attribute (pythonGH-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>
pablogsal pushed a commit that referenced this pull request Jan 25, 2026
…GH-142146) (#142213) * gh-142145: Remove quadratic behavior in node ID cache clearing (GH-142146) * Remove quadratic behavior in node ID cache clearing Co-authored-by: Jacob Walls <38668450+jacobtylerwalls@users.noreply.github.com> * Add news fragment --------- (cherry picked from commit 08d8e18) Co-authored-by: Seth Michael Larson <seth@python.org> Co-authored-by: Jacob Walls <38668450+jacobtylerwalls@users.noreply.github.com> * [3.14] gh-142754: Ensure that Element & Attr instances have the ownerDocument 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> * gh-142145: relax the no-longer-quadratic test timing (GH-143030) * gh-142145: relax the no-longer-quadratic test timing * require cpu resource (cherry picked from commit 8d2d7bb) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com> * merge NEWS entries into one --------- Co-authored-by: Seth Michael Larson <seth@python.org> Co-authored-by: Jacob Walls <38668450+jacobtylerwalls@users.noreply.github.com> 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: Gregory P. Smith <greg@krypto.org>
pablogsal pushed a commit that referenced this pull request Jan 25, 2026
…GH-142146) (#142212) * gh-142145: Remove quadratic behavior in node ID cache clearing (GH-142146) * Remove quadratic behavior in node ID cache clearing Co-authored-by: Jacob Walls <38668450+jacobtylerwalls@users.noreply.github.com> * Add news fragment --------- (cherry picked from commit 08d8e18) Co-authored-by: Seth Michael Larson <seth@python.org> Co-authored-by: Jacob Walls <38668450+jacobtylerwalls@users.noreply.github.com> * [3.14] gh-142754: Ensure that Element & Attr instances have the ownerDocument 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> * gh-142145: relax the no-longer-quadratic test timing (GH-143030) * gh-142145: relax the no-longer-quadratic test timing * require cpu resource (cherry picked from commit 8d2d7bb) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com> * merge NEWS entries into one --------- Co-authored-by: Seth Michael Larson <seth@python.org> Co-authored-by: Jacob Walls <38668450+jacobtylerwalls@users.noreply.github.com> 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: Gregory P. Smith <greg@krypto.org>
Sign up for freeto 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

@encukou@hugovk@jacobtylerwalls