gh-141510: Add PyAnyDict_AsNewDict() function by vstinner · Pull Request #145531 · python/cpython
This PR was big, so I merged unrelated changes as separated changes:
- commit 2cd0ddf fix
frozendict.items() ^ frozendict.items(). - commit 0c29f83 modify
PyDict_Copy()to no longer acceptfrozendict
@ZeroIntensity convinced me that supporting frozendict in PyDict_Copy() is a bad idea.
What's the motivation for this change?
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().
Would something like PyAnyDict_AsNewDict be more useful?
Oh, I prefer this function over PyFrozenDict_AsDict().
@ZeroIntensity: What do you think of adding a new PyAnyDict_AsNewDict() function?