| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
* 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 frozendict or a dict (from Objects/typeobject.c): 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 PyAnyDict_AsNewDict be more useful?
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 frozendict in PyDict_Copy() is a bad idea.
When a function using PyDict_Copy() is modified to support frozendict, I have to write code like that: 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 frozendict to a dict, copy a dict, or fail if the argument is not a dict or a frozendict. A dedicated function may be more efficient than PyDict_New()+PyDict_Merge().
Oh, I prefer this function over PyFrozenDict_AsDict(). @ZeroIntensity: What do you think of adding a new PyAnyDict_AsNewDict() function? |
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 frozendict. Being able to get a dictionary out of a frozendict will help adoption in that sense. Is there an east way to convert a dictionary into a frozendict? Another potential use-case would be if you're trying to implement a function like this in C: 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 PyAnyDict_AsNewDict(). It accepts dict and frozendict and creates a new dict.
Yes, call PyFrozenDict_New(dict): https://docs.python.org/dev/c-api/dict.html#c.PyFrozenDict_New |
Sorry, something went wrong.
|
Currently, dict = PyObject_CallOneArg((PyObject*)&PyDict_Type, frozendict) can be used to convert a frozendict to a dict. The code is harder to read and harder to "discover", but it just works. |
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.
| Back | FazBrowse Home | New Git URL |
Remove internal _PyDict_CopyAsDict() function.
📚 Documentation preview 📚: https://cpython-previews--145531.org.readthedocs.build/