Skip to content

gh-143636: fix a crash when calling __replace__ on invalid SimpleNamespace instances#143655

Merged
picnixz merged 4 commits intopython:mainfrom
picnixz:fix/crash/simple-namespace-new-143636
Mar 14, 2026
Merged

gh-143636: fix a crash when calling __replace__ on invalid SimpleNamespace instances#143655
picnixz merged 4 commits intopython:mainfrom
picnixz:fix/crash/simple-namespace-new-143636

Conversation

@picnixz
Copy link
Member

@picnixz picnixz commented Jan 10, 2026

if (!result) {
return NULL;
}
if (!_PyNamespace_Check(result)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many ways to clone an object, you should make a decision whether call __init__ and/or __new__bor just create an instance of the base class. This is done differently for other types.

The following code follows the way of __reduce__() (which is also used in copy.copy()). It is equivalent to:

new_ns = type(ns)()
new_dict = dict(ns.__dict__)
for k, v in kwargs.items():
    new_dict[k] = v
for k, v in new_dict.items():
    new_ns.__dict__[k] = v

but more efficient.

This is what copy.copy() will execute.

We can implement this for the case when the result of type(ns)() is not a SimpleNamespace, but this will add an amount of code, and all this for the case which perhaps never occurs in real code. So, I am fine with simply raising an error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to change the existing behavior. Maybe we could decide in a follow-up PR how to change this?

@picnixz
Copy link
Member Author

picnixz commented Feb 7, 2026

@serhiy-storchaka Do you want me to address your concerns or are you fine with the current approach I took? I didn't want to change the way we construct the instances so that I can backport this fix and I suggest that we address the construction alternatives in a follow-up PR. WDYT?

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Adding _PyNamespace_Check() test sounds like the right fix for this problem. I don't have a strong opinion on the error message.

@picnixz picnixz enabled auto-merge (squash) March 14, 2026 09:20
@picnixz picnixz disabled auto-merge March 14, 2026 09:27
@picnixz picnixz enabled auto-merge (squash) March 14, 2026 09:46
@picnixz picnixz merged commit 9796856 into python:main Mar 14, 2026
50 checks passed
@miss-islington-app
Copy link

Thanks @picnixz for the PR 🌮🎉.. 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 Mar 14, 2026
…`SimpleNamespace` instances (pythonGH-143655)

(cherry picked from commit 9796856)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@miss-islington-app
Copy link

Sorry, @picnixz, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 97968564b61965f2a65a9be8af731cee6913eb7a 3.13

@bedevere-app
Copy link

bedevere-app bot commented Mar 14, 2026

GH-145938 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 Mar 14, 2026
@picnixz picnixz deleted the fix/crash/simple-namespace-new-143636 branch March 14, 2026 10:18
@bedevere-app
Copy link

bedevere-app bot commented Mar 14, 2026

GH-145940 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 Mar 14, 2026
picnixz added a commit that referenced this pull request Mar 14, 2026
… `SimpleNamespace` instances (GH-143655) (#145938)

gh-143636: fix a crash when calling ``__replace__`` on invalid `SimpleNamespace` instances (GH-143655)
(cherry picked from commit 9796856)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
picnixz added a commit that referenced this pull request Mar 14, 2026
… `SimpleNamespace` instances (GH-143655) (#145940)

[3.13] gh-143636: fix a crash when calling ``__replace__`` on invalid `SimpleNamespace` instances (GH-143655)

(cherry picked from commit 9796856)
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