summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapi
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-02-06 16:36:23 (GMT)
committerGitHub <noreply@github.com>2024-02-06 16:36:23 (GMT)
commitde61d4bd4db868ce49a729a283763b94f2fda961 (patch)
tree00f712e2df606a604cf86d1ce4d384db4d2f1478 /Modules/_testcapi
parent0e2ab73dc31e0b8ea1827ec24bae93ae2644c617 (diff)
downloadcpython-de61d4bd4db868ce49a729a283763b94f2fda961.zip
cpython-de61d4bd4db868ce49a729a283763b94f2fda961.tar.gz
cpython-de61d4bd4db868ce49a729a283763b94f2fda961.tar.bz2
gh-112066: Add `PyDict_SetDefaultRef` function. (#112123)
The `PyDict_SetDefaultRef` function is similar to `PyDict_SetDefault`, but returns a strong reference through the optional `**result` pointer instead of a borrowed reference. Co-authored-by: Petr Viktorin <encukou@gmail.com>
Diffstat (limited to 'Modules/_testcapi')
-rw-r--r--Modules/_testcapi/dict.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/Modules/_testcapi/dict.c b/Modules/_testcapi/dict.c
index 42e056b..fe03c24 100644
--- a/Modules/_testcapi/dict.c
+++ b/Modules/_testcapi/dict.c
@@ -226,6 +226,31 @@ dict_setdefault(PyObject *self, PyObject *args)
}
static PyObject *
+dict_setdefaultref(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *key, *default_value, *result = UNINITIALIZED_PTR;
+ if (!PyArg_ParseTuple(args, "OOO", &obj, &key, &default_value)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(key);
+ NULLABLE(default_value);
+ switch (PyDict_SetDefaultRef(obj, key, default_value, &result)) {
+ case -1:
+ assert(result == NULL);
+ return NULL;
+ case 0:
+ assert(result == default_value);
+ return result;
+ case 1:
+ return result;
+ default:
+ Py_FatalError("PyDict_SetDefaultRef() returned invalid code");
+ Py_UNREACHABLE();
+ }
+}
+
+static PyObject *
dict_delitem(PyObject *self, PyObject *args)
{
PyObject *mapping, *key;
@@ -433,6 +458,7 @@ static PyMethodDef test_methods[] = {
{"dict_delitem", dict_delitem, METH_VARARGS},
{"dict_delitemstring", dict_delitemstring, METH_VARARGS},
{"dict_setdefault", dict_setdefault, METH_VARARGS},
+ {"dict_setdefaultref", dict_setdefaultref, METH_VARARGS},
{"dict_keys", dict_keys, METH_O},
{"dict_values", dict_values, METH_O},
{"dict_items", dict_items, METH_O},