Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,18 @@ def items(self):
self.assertRaises(TypeError, _testcapi.get_mapping_values, bad_mapping)
self.assertRaises(TypeError, _testcapi.get_mapping_items, bad_mapping)

def test_mapping_has_key(self):
dct = {'a': 1}
self.assertTrue(_testcapi.mapping_has_key(dct, 'a'))
self.assertFalse(_testcapi.mapping_has_key(dct, 'b'))

class SubDict(dict):
pass

dct2 = SubDict({'a': 1})
self.assertTrue(_testcapi.mapping_has_key(dct2, 'a'))
self.assertFalse(_testcapi.mapping_has_key(dct2, 'b'))

@unittest.skipUnless(hasattr(_testcapi, 'negative_refcount'),
'need _testcapi.negative_refcount')
def test_negative_refcount(self):
Expand Down
36 changes: 36 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to call this function from test_capi.py? Or is that automatic?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is:

cpython/Lib/test/test_capi.py

Lines 1185 to 1189 in 964a92c

class Test_testcapi(unittest.TestCase):
locals().update((name, getattr(_testcapi, name))
for name in dir(_testcapi)
if name.startswith('test_') and not name.endswith('_code'))

{
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)
Expand Down Expand Up @@ -5932,6 +5966,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},
Expand Down