@@ -4690,6 +4690,40 @@ get_mapping_items(PyObject* self, PyObject *obj)
4690
4690
return PyMapping_Items (obj );
4691
4691
}
4692
4692
4693
+ static PyObject *
4694
+ test_mapping_has_key_string (PyObject * self , PyObject * Py_UNUSED (args ))
4695
+ {
4696
+ PyObject * context = PyDict_New ();
4697
+ PyObject * val = PyLong_FromLong (1 );
4698
+
4699
+ // Since this uses `const char*` it is easier to test this in C:
4700
+ PyDict_SetItemString (context , "a" , val );
4701
+ if (!PyMapping_HasKeyString (context , "a" )) {
4702
+ PyErr_SetString (PyExc_RuntimeError ,
4703
+ "Existing mapping key does not exist" );
4704
+ return NULL ;
4705
+ }
4706
+ if (PyMapping_HasKeyString (context , "b" )) {
4707
+ PyErr_SetString (PyExc_RuntimeError ,
4708
+ "Missing mapping key exists" );
4709
+ return NULL ;
4710
+ }
4711
+
4712
+ Py_DECREF (val );
4713
+ Py_DECREF (context );
4714
+ Py_RETURN_NONE ;
4715
+ }
4716
+
4717
+ static PyObject *
4718
+ mapping_has_key (PyObject * self , PyObject * args )
4719
+ {
4720
+ PyObject * context , * key ;
4721
+ if (!PyArg_ParseTuple (args , "OO" , & context , & key )) {
4722
+ return NULL ;
4723
+ }
4724
+ return PyLong_FromLong (PyMapping_HasKey (context , key ));
4725
+ }
4726
+
4693
4727
4694
4728
static PyObject *
4695
4729
test_pythread_tss_key_state (PyObject * self , PyObject * args )
@@ -6054,6 +6088,8 @@ static PyMethodDef TestMethods[] = {
6054
6088
{"get_mapping_keys" , get_mapping_keys , METH_O },
6055
6089
{"get_mapping_values" , get_mapping_values , METH_O },
6056
6090
{"get_mapping_items" , get_mapping_items , METH_O },
6091
+ {"test_mapping_has_key_string" , test_mapping_has_key_string , METH_NOARGS },
6092
+ {"mapping_has_key" , mapping_has_key , METH_VARARGS },
6057
6093
{"test_pythread_tss_key_state" , test_pythread_tss_key_state , METH_VARARGS },
6058
6094
{"hamt" , new_hamt , METH_NOARGS },
6059
6095
{"bad_get" , _PyCFunction_CAST (bad_get ), METH_FASTCALL },
0 commit comments