diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-10-26 04:57:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-26 04:57:49 (GMT) |
commit | db14a9d594e9d73b293916d6e52065a2d91f1c1f (patch) | |
tree | ec27d514203c8e6212aa550224b566e0485756d8 /Modules | |
parent | 2aedba59ecc7e89479226f0013c1771c4ef66413 (diff) | |
download | cpython-db14a9d594e9d73b293916d6e52065a2d91f1c1f.zip cpython-db14a9d594e9d73b293916d6e52065a2d91f1c1f.tar.gz cpython-db14a9d594e9d73b293916d6e52065a2d91f1c1f.tar.bz2 |
gh-94808: cover `PyMapping_HasKeyString` and `PyMapping_HasKey` (GH-98486)
(cherry picked from commit 5d30544485dc56ab999ad7656ef6559306fd013f)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_testcapimodule.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index bab8d60..1c085a4 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -5348,6 +5348,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) @@ -6382,6 +6416,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}, |