-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-143636: fix a crash when calling __replace__ on invalid SimpleNamespace instances
#143655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix a crash when calling :class:`SimpleNamespace.__replace__() | ||
| <types.SimpleNamespace>` on non-namespace instances. Patch by Bénédikt Tran. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ typedef struct { | |
| } _PyNamespaceObject; | ||
|
|
||
| #define _PyNamespace_CAST(op) _Py_CAST(_PyNamespaceObject*, (op)) | ||
| #define _PyNamespace_Check(op) PyObject_TypeCheck((op), &_PyNamespace_Type) | ||
|
|
||
|
|
||
| static PyMemberDef namespace_members[] = { | ||
|
|
@@ -234,6 +235,14 @@ namespace_replace(PyObject *self, PyObject *args, PyObject *kwargs) | |
| if (!result) { | ||
| return NULL; | ||
| } | ||
| if (!_PyNamespace_Check(result)) { | ||
| PyErr_Format(PyExc_TypeError, | ||
| "%T.__new__() must return an instance of a subclass of %s", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think about something like "%T() returned %T object".
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had this locally but I wondered whether this could be confused with |
||
| self, _PyNamespace_Type.tp_name); | ||
| Py_DECREF(result); | ||
| return NULL; | ||
| } | ||
|
|
||
| if (PyDict_Update(((_PyNamespaceObject*)result)->ns_dict, | ||
| ((_PyNamespaceObject*)self)->ns_dict) < 0) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
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 incopy.copy()). It is equivalent to: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.There was a problem hiding this comment.
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?