summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2022-10-26 04:33:32 (GMT)
committerGitHub <noreply@github.com>2022-10-26 04:33:32 (GMT)
commit5d30544485dc56ab999ad7656ef6559306fd013f (patch)
treec704df6bb4de2807c0cdb14d97eeb018d326e324 /Modules
parent216f45e4fec42407ff744b915523a226a0070ff1 (diff)
downloadcpython-5d30544485dc56ab999ad7656ef6559306fd013f.zip
cpython-5d30544485dc56ab999ad7656ef6559306fd013f.tar.gz
cpython-5d30544485dc56ab999ad7656ef6559306fd013f.tar.bz2
gh-94808: cover `PyMapping_HasKeyString` and `PyMapping_HasKey` (#98486)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapimodule.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 194b2de..e792a2c 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -4690,6 +4690,40 @@ get_mapping_items(PyObject* self, PyObject *obj)
return PyMapping_Items(obj);
}
+static PyObject *
+test_mapping_has_key_string(PyObject *self, PyObject *Py_UNUSED(args))
+{
+ PyObject *context = PyDict_New();
+ PyObject *val = PyLong_FromLong(1);
+
+ // Since this uses `const char*` it is easier to test this in C:
+ PyDict_SetItemString(context, "a", val);
+ if (!PyMapping_HasKeyString(context, "a")) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "Existing mapping key does not exist");
+ return NULL;
+ }
+ if (PyMapping_HasKeyString(context, "b")) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "Missing mapping key exists");
+ return NULL;
+ }
+
+ Py_DECREF(val);
+ Py_DECREF(context);
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+mapping_has_key(PyObject* self, PyObject *args)
+{
+ PyObject *context, *key;
+ if (!PyArg_ParseTuple(args, "OO", &context, &key)) {
+ return NULL;
+ }
+ return PyLong_FromLong(PyMapping_HasKey(context, key));
+}
+
static PyObject *
test_pythread_tss_key_state(PyObject *self, PyObject *args)
@@ -6054,6 +6088,8 @@ static PyMethodDef TestMethods[] = {
{"get_mapping_keys", get_mapping_keys, METH_O},
{"get_mapping_values", get_mapping_values, METH_O},
{"get_mapping_items", get_mapping_items, METH_O},
+ {"test_mapping_has_key_string", test_mapping_has_key_string, METH_NOARGS},
+ {"mapping_has_key", mapping_has_key, METH_VARARGS},
{"test_pythread_tss_key_state", test_pythread_tss_key_state, METH_VARARGS},
{"hamt", new_hamt, METH_NOARGS},
{"bad_get", _PyCFunction_CAST(bad_get), METH_FASTCALL},