@@ -1086,6 +1086,113 @@ test_getitem(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
10861086}
10871087
10881088
1089+ static PyObject *
1090+ test_dict_getitemref (PyObject * Py_UNUSED (module ), PyObject * Py_UNUSED (args ))
1091+ {
1092+ assert (!PyErr_Occurred ());
1093+
1094+ PyObject * dict = NULL , * key = NULL , * missing_key = NULL , * value = NULL ;
1095+ PyObject * invalid_key = NULL ;
1096+ PyObject * invalid_dict = NULL ;
1097+ PyObject * get_value = NULL ;
1098+ int res ;
1099+
1100+ // test PyDict_New()
1101+ dict = PyDict_New ();
1102+ if (dict == NULL ) {
1103+ goto error ;
1104+ }
1105+
1106+ key = PyUnicode_FromString ("key" );
1107+ if (key == NULL ) {
1108+ goto error ;
1109+ }
1110+
1111+ missing_key = PyUnicode_FromString ("missing_key" );
1112+ if (missing_key == NULL ) {
1113+ goto error ;
1114+ }
1115+
1116+ value = PyUnicode_FromString ("value" );
1117+ if (value == NULL ) {
1118+ goto error ;
1119+ }
1120+
1121+ res = PyDict_SetItemString (dict , "key" , value );
1122+ if (res < 0 ) {
1123+ goto error ;
1124+ }
1125+ assert (res == 0 );
1126+
1127+ // test PyDict_GetItemRef(), key is present
1128+ get_value = Py_Ellipsis ; // marker value
1129+ assert (PyDict_GetItemRef (dict , key , & get_value ) == 1 );
1130+ assert (get_value == value );
1131+ Py_DECREF (get_value );
1132+
1133+ // test PyDict_GetItemStringRef(), key is present
1134+ get_value = Py_Ellipsis ; // marker value
1135+ assert (PyDict_GetItemStringRef (dict , "key" , & get_value ) == 1 );
1136+ assert (get_value == value );
1137+ Py_DECREF (get_value );
1138+
1139+ // test PyDict_GetItemRef(), missing key
1140+ get_value = Py_Ellipsis ; // marker value
1141+ assert (PyDict_GetItemRef (dict , missing_key , & get_value ) == 0 );
1142+ assert (!PyErr_Occurred ());
1143+ assert (get_value == NULL );
1144+
1145+ // test PyDict_GetItemStringRef(), missing key
1146+ get_value = Py_Ellipsis ; // marker value
1147+ assert (PyDict_GetItemStringRef (dict , "missing_key" , & get_value ) == 0 );
1148+ assert (!PyErr_Occurred ());
1149+ assert (get_value == NULL );
1150+
1151+ // test PyDict_GetItemRef(), invalid dict
1152+ invalid_dict = key ; // borrowed reference
1153+ get_value = Py_Ellipsis ; // marker value
1154+ assert (PyDict_GetItemRef (invalid_dict , key , & get_value ) == -1 );
1155+ assert (PyErr_ExceptionMatches (PyExc_SystemError ));
1156+ PyErr_Clear ();
1157+ assert (get_value == NULL );
1158+
1159+ // test PyDict_GetItemStringRef(), invalid dict
1160+ get_value = Py_Ellipsis ; // marker value
1161+ assert (PyDict_GetItemStringRef (invalid_dict , "key" , & get_value ) == -1 );
1162+ assert (PyErr_ExceptionMatches (PyExc_SystemError ));
1163+ PyErr_Clear ();
1164+ assert (get_value == NULL );
1165+
1166+ invalid_key = PyList_New (0 ); // not hashable key
1167+ if (invalid_key == NULL ) {
1168+ goto error ;
1169+ }
1170+
1171+ // test PyDict_GetItemRef(), invalid key
1172+ get_value = Py_Ellipsis ; // marker value
1173+ assert (PyDict_GetItemRef (dict , invalid_key , & get_value ) == -1 );
1174+ assert (PyErr_ExceptionMatches (PyExc_TypeError ));
1175+ PyErr_Clear ();
1176+ assert (get_value == NULL );
1177+
1178+ Py_DECREF (dict );
1179+ Py_DECREF (key );
1180+ Py_DECREF (missing_key );
1181+ Py_DECREF (value );
1182+ Py_DECREF (invalid_key );
1183+
1184+ Py_RETURN_NONE ;
1185+
1186+ error :
1187+ Py_XDECREF (dict );
1188+ Py_XDECREF (key );
1189+ Py_XDECREF (missing_key );
1190+ Py_XDECREF (value );
1191+ Py_XDECREF (invalid_key );
1192+ return NULL ;
1193+ }
1194+
1195+
10891196static struct PyMethodDef methods [] = {
10901197 {"test_object" , test_object , METH_NOARGS , _Py_NULL },
10911198 {"test_py_is" , test_py_is , METH_NOARGS , _Py_NULL },
@@ -1110,6 +1217,7 @@ static struct PyMethodDef methods[] = {
11101217 {"test_vectorcall" , test_vectorcall , METH_NOARGS , _Py_NULL },
11111218 {"test_getattr" , test_getattr , METH_NOARGS , _Py_NULL },
11121219 {"test_getitem" , test_getitem , METH_NOARGS , _Py_NULL },
1220+ {"test_dict_getitemref" , test_dict_getitemref , METH_NOARGS , _Py_NULL },
11131221 {_Py_NULL , _Py_NULL , 0 , _Py_NULL }
11141222};
11151223
0 commit comments