gh-111262: Add PyDict_Pop() function [without default value nor KeyError]#112028
gh-111262: Add PyDict_Pop() function [without default value nor KeyError]#112028vstinner merged 9 commits into
Conversation
|
This PR is based on PR gh-111939 (which was based on PR gh-111263). This API is different:
|
Sorry, something went wrong.
|
Simplified example where static int
sys_set_object(PyInterpreterState *interp, PyObject *key, PyObject *v)
{
PyObject *sd = interp->sysdict;
if (v == NULL) {
if (PyDict_Pop(sd, key, NULL) < 0) {
return -1;
}
// no need to care about KeyError or Py_DECREF()
return 0;
}
else {
return PyDict_SetItem(sd, key, v);
}
}Example which uses the removed value: PyObject *ob;
if (PyDict_Pop(kwargs, key, &ob) < 0) {
goto error;
}
if (ob != NULL) {
result->ob_item[i] = ob;
}
else {
// no need to care about KeyError or Py_DECREF()
result->ob_item[i] = Py_NewRef(self->ob_item[i]);
} |
Sorry, something went wrong.
|
The function is not added to the limited C API: @encukou asks to wait until Python will have a C API Working Group. |
Sorry, something went wrong.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
This PR includes yet one change. It allows to pass NULL as a result address. It complicates the implementation, but makes the common use case simpler.
Sorry, something went wrong.
|
As requested, I added I added tests on PyDict_PopString() and I also addresssed @serhiy-storchaka's review. |
Sorry, something went wrong.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Sorry, something went wrong.
scoder
left a comment
There was a problem hiding this comment.
LGTM overall, just wording fixes.
Sorry, something went wrong.
At the beginning, I had no idea. It was very blurry. What helped me to make my own opinion was to actually use the different proposed API and look at the updated code. I really like this API without the default value: I'm convinced by these examples. In short, I prefer this PR. |
Sorry, something went wrong.
_PyDict_Pop_KnownHash(): remove the default value and the return type becomes an int. Co-Authored-By: Stefan Behnel <stefan_ml@behnel.de> Co-authored-by: Antoine Pitrou <pitrou@free.fr>
_PyDict_Pop_KnownHash(): remove the default value and the return type becomes an int. Co-authored-by: Stefan Behnel <stefan_ml@behnel.de> Co-authored-by: Antoine Pitrou <pitrou@free.fr>
_PyDict_Pop_KnownHash(): remove the default value and the return type becomes an int. Co-authored-by: Stefan Behnel <stefan_ml@behnel.de> Co-authored-by: Antoine Pitrou <pitrou@free.fr>
_PyDict_Pop_KnownHash(): remove the default value and the return type becomes an int.
📚 Documentation preview 📚: https://cpython-previews--112028.org.readthedocs.build/