@@ -5348,6 +5348,40 @@ get_mapping_items(PyObject* self, PyObject *obj)
5348
5348
return PyMapping_Items (obj );
5349
5349
}
5350
5350
5351
+ static PyObject *
5352
+ test_mapping_has_key_string (PyObject * self , PyObject * Py_UNUSED (args ))
5353
+ {
5354
+ PyObject * context = PyDict_New ();
5355
+ PyObject * val = PyLong_FromLong (1 );
5356
+
5357
+ // Since this uses `const char*` it is easier to test this in C:
5358
+ PyDict_SetItemString (context , "a" , val );
5359
+ if (!PyMapping_HasKeyString (context , "a" )) {
5360
+ PyErr_SetString (PyExc_RuntimeError ,
5361
+ "Existing mapping key does not exist" );
5362
+ return NULL ;
5363
+ }
5364
+ if (PyMapping_HasKeyString (context , "b" )) {
5365
+ PyErr_SetString (PyExc_RuntimeError ,
5366
+ "Missing mapping key exists" );
5367
+ return NULL ;
5368
+ }
5369
+
5370
+ Py_DECREF (val );
5371
+ Py_DECREF (context );
5372
+ Py_RETURN_NONE ;
5373
+ }
5374
+
5375
+ static PyObject *
5376
+ mapping_has_key (PyObject * self , PyObject * args )
5377
+ {
5378
+ PyObject * context , * key ;
5379
+ if (!PyArg_ParseTuple (args , "OO" , & context , & key )) {
5380
+ return NULL ;
5381
+ }
5382
+ return PyLong_FromLong (PyMapping_HasKey (context , key ));
5383
+ }
5384
+
5351
5385
5352
5386
static PyObject *
5353
5387
test_pythread_tss_key_state (PyObject * self , PyObject * args )
@@ -6382,6 +6416,8 @@ static PyMethodDef TestMethods[] = {
6382
6416
{"get_mapping_keys" , get_mapping_keys , METH_O },
6383
6417
{"get_mapping_values" , get_mapping_values , METH_O },
6384
6418
{"get_mapping_items" , get_mapping_items , METH_O },
6419
+ {"test_mapping_has_key_string" , test_mapping_has_key_string , METH_NOARGS },
6420
+ {"mapping_has_key" , mapping_has_key , METH_VARARGS },
6385
6421
{"test_pythread_tss_key_state" , test_pythread_tss_key_state , METH_VARARGS },
6386
6422
{"hamt" , new_hamt , METH_NOARGS },
6387
6423
{"bad_get" , _PyCFunction_CAST (bad_get ), METH_FASTCALL },
0 commit comments