summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapi
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-11-14 12:51:00 (GMT)
committerGitHub <noreply@github.com>2023-11-14 12:51:00 (GMT)
commit4f04172c9287c507f1426e02ddfc432f1f3ade54 (patch)
tree800ac400c3776336e1f52b49a0f10b4dae9af8c1 /Modules/_testcapi
parentf44d6ff6e0c9eeb0bb246a3dd8f99d40b7050054 (diff)
downloadcpython-4f04172c9287c507f1426e02ddfc432f1f3ade54.zip
cpython-4f04172c9287c507f1426e02ddfc432f1f3ade54.tar.gz
cpython-4f04172c9287c507f1426e02ddfc432f1f3ade54.tar.bz2
gh-111262: Add PyDict_Pop() function (#112028)
_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>
Diffstat (limited to 'Modules/_testcapi')
-rw-r--r--Modules/_testcapi/dict.c87
1 files changed, 86 insertions, 1 deletions
diff --git a/Modules/_testcapi/dict.c b/Modules/_testcapi/dict.c
index 5f6a1a0..42e056b 100644
--- a/Modules/_testcapi/dict.c
+++ b/Modules/_testcapi/dict.c
@@ -331,6 +331,88 @@ dict_mergefromseq2(PyObject *self, PyObject *args)
}
+static PyObject *
+dict_pop(PyObject *self, PyObject *args)
+{
+ // Test PyDict_Pop(dict, key, &value)
+ PyObject *dict, *key;
+ if (!PyArg_ParseTuple(args, "OO", &dict, &key)) {
+ return NULL;
+ }
+ NULLABLE(dict);
+ NULLABLE(key);
+ PyObject *result = UNINITIALIZED_PTR;
+ int res = PyDict_Pop(dict, key, &result);
+ if (res < 0) {
+ assert(result == NULL);
+ return NULL;
+ }
+ if (res == 0) {
+ assert(result == NULL);
+ result = Py_NewRef(Py_None);
+ }
+ else {
+ assert(result != NULL);
+ }
+ return Py_BuildValue("iN", res, result);
+}
+
+
+static PyObject *
+dict_pop_null(PyObject *self, PyObject *args)
+{
+ // Test PyDict_Pop(dict, key, NULL)
+ PyObject *dict, *key;
+ if (!PyArg_ParseTuple(args, "OO", &dict, &key)) {
+ return NULL;
+ }
+ NULLABLE(dict);
+ NULLABLE(key);
+ RETURN_INT(PyDict_Pop(dict, key, NULL));
+}
+
+
+static PyObject *
+dict_popstring(PyObject *self, PyObject *args)
+{
+ PyObject *dict;
+ const char *key;
+ Py_ssize_t key_size;
+ if (!PyArg_ParseTuple(args, "Oz#", &dict, &key, &key_size)) {
+ return NULL;
+ }
+ NULLABLE(dict);
+ PyObject *result = UNINITIALIZED_PTR;
+ int res = PyDict_PopString(dict, key, &result);
+ if (res < 0) {
+ assert(result == NULL);
+ return NULL;
+ }
+ if (res == 0) {
+ assert(result == NULL);
+ result = Py_NewRef(Py_None);
+ }
+ else {
+ assert(result != NULL);
+ }
+ return Py_BuildValue("iN", res, result);
+}
+
+
+static PyObject *
+dict_popstring_null(PyObject *self, PyObject *args)
+{
+ PyObject *dict;
+ const char *key;
+ Py_ssize_t key_size;
+ if (!PyArg_ParseTuple(args, "Oz#", &dict, &key, &key_size)) {
+ return NULL;
+ }
+ NULLABLE(dict);
+ RETURN_INT(PyDict_PopString(dict, key, NULL));
+}
+
+
static PyMethodDef test_methods[] = {
{"dict_check", dict_check, METH_O},
{"dict_checkexact", dict_checkexact, METH_O},
@@ -358,7 +440,10 @@ static PyMethodDef test_methods[] = {
{"dict_merge", dict_merge, METH_VARARGS},
{"dict_update", dict_update, METH_VARARGS},
{"dict_mergefromseq2", dict_mergefromseq2, METH_VARARGS},
-
+ {"dict_pop", dict_pop, METH_VARARGS},
+ {"dict_pop_null", dict_pop_null, METH_VARARGS},
+ {"dict_popstring", dict_popstring, METH_VARARGS},
+ {"dict_popstring_null", dict_popstring_null, METH_VARARGS},
{NULL},
};