Skip to content

Commit

Permalink
Use PyDict_Pop()
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Nov 12, 2023
1 parent 8fb0bfb commit 3d37d99
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -967,12 +967,12 @@ local_clear(localobject *self)
HEAD_UNLOCK(runtime);
while (tstate) {
if (tstate->dict) {
PyObject *v = _PyDict_Pop(tstate->dict, self->key, Py_None);
if (v != NULL) {
Py_DECREF(v);
PyObject *v;
if (PyDict_Pop(tstate->dict, self->key, Py_None, &v) < 0) {
PyErr_Clear();
}
else {
PyErr_Clear();
Py_DECREF(v);
}
}
HEAD_LOCK(runtime);
Expand Down
7 changes: 4 additions & 3 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,12 @@ remove_unusable_flags(PyObject *m)
if (flag_name == NULL) {
return -1;
}
PyObject *v = _PyDict_Pop(dict, flag_name, Py_None);
Py_DECREF(flag_name);
if (v == NULL) {
PyObject *v;
if (PyDict_Pop(dict, flag_name, Py_None, &v) < 0) {
Py_DECREF(flag_name);
return -1;
}
Py_DECREF(flag_name);
Py_DECREF(v);
}
}
Expand Down
4 changes: 3 additions & 1 deletion Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3493,7 +3493,9 @@ static PyObject *
dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
/*[clinic end generated code: output=3abb47b89f24c21c input=e221baa01044c44c]*/
{
return _PyDict_Pop((PyObject*)self, key, default_value);
PyObject *result;
PyDict_Pop((PyObject*)self, key, default_value, &result);
return result;
}

/*[clinic input]
Expand Down
3 changes: 2 additions & 1 deletion Objects/odictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,8 @@ _odict_popkey_hash(PyObject *od, PyObject *key, PyObject *failobj,
return NULL;
}
/* Now delete the value from the dict. */
(void)_PyDict_Pop_KnownHash((PyDictObject *)od, key, hash, failobj, &value);
(void)_PyDict_Pop_KnownHash((PyDictObject *)od, key, hash,
failobj, &value);
}
else if (value == NULL && !PyErr_Occurred()) {
/* Apply the fallback value, if necessary. */
Expand Down
8 changes: 4 additions & 4 deletions Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

#include "Python.h"
#include "pycore_dict.h" // _PyDict_Pop()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_modsupport.h" // _PyArg_NoPositional()
#include "pycore_object.h" // _PyObject_GC_TRACK()
Expand Down Expand Up @@ -421,11 +420,12 @@ structseq_replace(PyStructSequence *self, PyObject *args, PyObject *kwargs)
if (!key) {
goto error;
}
PyObject *ob = _PyDict_Pop(kwargs, key, self->ob_item[i]);
Py_DECREF(key);
if (!ob) {
PyObject *ob;
if (PyDict_Pop(kwargs, key, self->ob_item[i], &ob) < 0) {
Py_DECREF(key);
goto error;
}
Py_DECREF(key);
result->ob_item[i] = ob;
}
// Check if there are any unexpected fields.
Expand Down
3 changes: 2 additions & 1 deletion Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ remove_module(PyThreadState *tstate, PyObject *name)

PyObject *modules = MODULES(tstate->interp);
if (PyDict_CheckExact(modules)) {
PyObject *mod = _PyDict_Pop(modules, name, Py_None);
PyObject *mod;
(void)PyDict_Pop(modules, name, Py_None, &mod);
Py_XDECREF(mod);
}
else if (PyMapping_DelItem(modules, name) < 0) {
Expand Down
3 changes: 1 addition & 2 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ sys_set_object(PyInterpreterState *interp, PyObject *key, PyObject *v)
}
PyObject *sd = interp->sysdict;
if (v == NULL) {
v = _PyDict_Pop(sd, key, Py_None);
if (v == NULL) {
if (PyDict_Pop(sd, key, Py_None, &v) < 0) {
return -1;
}
Py_DECREF(v);
Expand Down

0 comments on commit 3d37d99

Please sign in to comment.