gh-141510: Add PyAnyDict_AsNewDict() function#145531
Conversation
* PyDict_Copy() no longer accepts frozendict. * Remove _PyDict_CopyAsDict() function. * Fix frozendict.items() ^ frozendict.items(). Add non-regression test.
|
Example of code to copy a PyObject *dict;
if (PyFrozenDict_Check(ctx->orig_dict)) {
dict = PyFrozenDict_AsDict(ctx->orig_dict);
}
else {
dict = PyDict_Copy(ctx->orig_dict);
}
if (dict == NULL) {
goto error;
} |
Sorry, something went wrong.
|
What's the motivation for this change? Would something like
That's for a vote; I can't speak for the entire WG. |
Sorry, something went wrong.
|
This PR was big, so I merged unrelated changes as separated changes:
@ZeroIntensity convinced me that supporting
When a function using PyObject *dict;
if (PyFrozenDict_Check(orig_dict)) {
dict = PyDict_New();
if (dict == NULL) {
goto error;
}
if (PyDict_Merge(dict, orig_dict, 1) < 0) {
Py_DECREF(dict);
goto error;
}
}
else {
dict = PyDict_Copy(orig_dict);
if (dict == NULL) {
goto error;
}
}I have to copy/paste this code. I would prefer to have a function doing that: convert a
Oh, I prefer this function over @ZeroIntensity: What do you think of adding a new |
Sorry, something went wrong.
|
I think this will be useful. When 3.15 comes out, there will be plenty of APIs that assume an input dictionary is mutable, and thus can't be used with Is there an east way to convert a dictionary into a def adjust_frozendict(data: frozendict) -> frozendict:
mutable = dict(data)
mutable["whatever"] = 123
return frozendict(mutable) |
Sorry, something went wrong.
|
Ok, I renamed the function to
Yes, call |
Sorry, something went wrong.
|
Currently, |
Sorry, something went wrong.
|
This PR is stale because it has been open for 30 days with no activity. |
Sorry, something went wrong.
|
I lost track of this function and Python 3.15 beta1 has been released without it. We are now past the feature freeze, so I don't think that it's worth it to add such function. We can revisit this function later if there is a need for it. I close th PR. |
Sorry, something went wrong.
Remove internal _PyDict_CopyAsDict() function.
📚 Documentation preview 📚: https://cpython-previews--145531.org.readthedocs.build/