Skip to content

Commit fa626b8

Browse files
authored
gh-107178: Remove _testcapi.test_dict_capi() (#108436)
The new _testcapi.test_dict tests are more complete, _testcapi.test_dict_capi() became redundant.
1 parent 480a337 commit fa626b8

File tree

1 file changed

+0
-191
lines changed

1 file changed

+0
-191
lines changed

Modules/_testcapimodule.c

-191
Original file line numberDiff line numberDiff line change
@@ -3318,196 +3318,6 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
33183318
}
33193319

33203320

3321-
static PyObject *
3322-
test_dict_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
3323-
{
3324-
assert(!PyErr_Occurred());
3325-
3326-
PyObject *dict= NULL, *key = NULL, *missing_key = NULL, *value = NULL;
3327-
PyObject *invalid_key = NULL;
3328-
int res;
3329-
3330-
// test PyDict_New()
3331-
dict = PyDict_New();
3332-
if (dict == NULL) {
3333-
goto error;
3334-
}
3335-
3336-
key = PyUnicode_FromString("key");
3337-
if (key == NULL) {
3338-
goto error;
3339-
}
3340-
3341-
missing_key = PyUnicode_FromString("missing_key");
3342-
if (missing_key == NULL) {
3343-
goto error;
3344-
}
3345-
3346-
value = PyUnicode_FromString("value");
3347-
if (value == NULL) {
3348-
goto error;
3349-
}
3350-
3351-
// test PyDict_SetItem()
3352-
Py_ssize_t key_refcnt = Py_REFCNT(key);
3353-
Py_ssize_t value_refcnt = Py_REFCNT(value);
3354-
res = PyDict_SetItem(dict, key, value);
3355-
if (res < 0) {
3356-
goto error;
3357-
}
3358-
assert(res == 0);
3359-
assert(Py_REFCNT(key) == (key_refcnt + 1));
3360-
assert(Py_REFCNT(value) == (value_refcnt + 1));
3361-
3362-
// test PyDict_SetItemString()
3363-
res = PyDict_SetItemString(dict, "key", value);
3364-
if (res < 0) {
3365-
goto error;
3366-
}
3367-
assert(res == 0);
3368-
assert(Py_REFCNT(key) == (key_refcnt + 1));
3369-
assert(Py_REFCNT(value) == (value_refcnt + 1));
3370-
3371-
// test PyDict_Size()
3372-
assert(PyDict_Size(dict) == 1);
3373-
3374-
// test PyDict_Contains(), key is present
3375-
assert(PyDict_Contains(dict, key) == 1);
3376-
3377-
// test PyDict_GetItem(), key is present
3378-
assert(PyDict_GetItem(dict, key) == value);
3379-
3380-
// test PyDict_GetItemString(), key is present
3381-
assert(PyDict_GetItemString(dict, "key") == value);
3382-
3383-
// test PyDict_GetItemWithError(), key is present
3384-
assert(PyDict_GetItemWithError(dict, key) == value);
3385-
assert(!PyErr_Occurred());
3386-
3387-
// test PyDict_GetItemRef(), key is present
3388-
PyObject *get_value = Py_Ellipsis; // marker value
3389-
assert(PyDict_GetItemRef(dict, key, &get_value) == 1);
3390-
assert(get_value == value);
3391-
Py_DECREF(get_value);
3392-
3393-
// test PyDict_GetItemStringRef(), key is present
3394-
get_value = Py_Ellipsis; // marker value
3395-
assert(PyDict_GetItemStringRef(dict, "key", &get_value) == 1);
3396-
assert(get_value == value);
3397-
Py_DECREF(get_value);
3398-
3399-
// test PyDict_Contains(), missing key
3400-
assert(PyDict_Contains(dict, missing_key) == 0);
3401-
3402-
// test PyDict_GetItem(), missing key
3403-
assert(PyDict_GetItem(dict, missing_key) == NULL);
3404-
assert(!PyErr_Occurred());
3405-
3406-
// test PyDict_GetItemString(), missing key
3407-
assert(PyDict_GetItemString(dict, "missing_key") == NULL);
3408-
assert(!PyErr_Occurred());
3409-
3410-
// test PyDict_GetItemWithError(), missing key
3411-
assert(PyDict_GetItem(dict, missing_key) == NULL);
3412-
assert(!PyErr_Occurred());
3413-
3414-
// test PyDict_GetItemRef(), missing key
3415-
get_value = Py_Ellipsis; // marker value
3416-
assert(PyDict_GetItemRef(dict, missing_key, &get_value) == 0);
3417-
assert(!PyErr_Occurred());
3418-
assert(get_value == NULL);
3419-
3420-
// test PyDict_GetItemStringRef(), missing key
3421-
get_value = Py_Ellipsis; // marker value
3422-
assert(PyDict_GetItemStringRef(dict, "missing_key", &get_value) == 0);
3423-
assert(!PyErr_Occurred());
3424-
assert(get_value == NULL);
3425-
3426-
// test PyDict_GetItem(), invalid dict
3427-
PyObject *invalid_dict = key; // borrowed reference
3428-
assert(PyDict_GetItem(invalid_dict, key) == NULL);
3429-
assert(!PyErr_Occurred());
3430-
3431-
// test PyDict_GetItemWithError(), invalid dict
3432-
assert(PyDict_GetItemWithError(invalid_dict, key) == NULL);
3433-
assert(PyErr_ExceptionMatches(PyExc_SystemError));
3434-
PyErr_Clear();
3435-
3436-
// test PyDict_GetItemRef(), invalid dict
3437-
get_value = Py_Ellipsis; // marker value
3438-
assert(PyDict_GetItemRef(invalid_dict, key, &get_value) == -1);
3439-
assert(PyErr_ExceptionMatches(PyExc_SystemError));
3440-
PyErr_Clear();
3441-
assert(get_value == NULL);
3442-
3443-
// test PyDict_GetItemStringRef(), invalid dict
3444-
get_value = Py_Ellipsis; // marker value
3445-
assert(PyDict_GetItemStringRef(invalid_dict, "key", &get_value) == -1);
3446-
assert(PyErr_ExceptionMatches(PyExc_SystemError));
3447-
PyErr_Clear();
3448-
assert(get_value == NULL);
3449-
3450-
invalid_key = PyList_New(0);
3451-
if (invalid_key == NULL) {
3452-
goto error;
3453-
}
3454-
3455-
// test PyDict_Contains(), invalid key
3456-
assert(PyDict_Contains(dict, invalid_key) == -1);
3457-
assert(PyErr_ExceptionMatches(PyExc_TypeError));
3458-
PyErr_Clear();
3459-
3460-
// test PyDict_GetItem(), invalid key
3461-
assert(PyDict_GetItem(dict, invalid_key) == NULL);
3462-
assert(!PyErr_Occurred());
3463-
3464-
// test PyDict_GetItemWithError(), invalid key
3465-
assert(PyDict_GetItemWithError(dict, invalid_key) == NULL);
3466-
assert(PyErr_ExceptionMatches(PyExc_TypeError));
3467-
PyErr_Clear();
3468-
3469-
// test PyDict_GetItemRef(), invalid key
3470-
get_value = Py_Ellipsis; // marker value
3471-
assert(PyDict_GetItemRef(dict, invalid_key, &get_value) == -1);
3472-
assert(PyErr_ExceptionMatches(PyExc_TypeError));
3473-
PyErr_Clear();
3474-
assert(get_value == NULL);
3475-
3476-
// test PyDict_DelItem(), key is present
3477-
assert(PyDict_DelItem(dict, key) == 0);
3478-
assert(PyDict_Size(dict) == 0);
3479-
3480-
// test PyDict_DelItem(), missing key
3481-
assert(PyDict_DelItem(dict, missing_key) == -1);
3482-
assert(PyErr_ExceptionMatches(PyExc_KeyError));
3483-
PyErr_Clear();
3484-
3485-
// test PyDict_DelItem(), invalid key
3486-
assert(PyDict_DelItem(dict, invalid_key) == -1);
3487-
assert(PyErr_ExceptionMatches(PyExc_TypeError));
3488-
PyErr_Clear();
3489-
3490-
// test PyDict_Clear()
3491-
PyDict_Clear(dict);
3492-
3493-
Py_DECREF(dict);
3494-
Py_DECREF(key);
3495-
Py_DECREF(missing_key);
3496-
Py_DECREF(value);
3497-
Py_DECREF(invalid_key);
3498-
3499-
Py_RETURN_NONE;
3500-
3501-
error:
3502-
Py_XDECREF(dict);
3503-
Py_XDECREF(key);
3504-
Py_XDECREF(missing_key);
3505-
Py_XDECREF(value);
3506-
Py_XDECREF(invalid_key);
3507-
return NULL;
3508-
}
3509-
3510-
35113321
static PyObject *
35123322
sys_getobject(PyObject *Py_UNUSED(module), PyObject *arg)
35133323
{
@@ -3670,7 +3480,6 @@ static PyMethodDef TestMethods[] = {
36703480
{"function_set_kw_defaults", function_set_kw_defaults, METH_VARARGS, NULL},
36713481
{"check_pyimport_addmodule", check_pyimport_addmodule, METH_VARARGS},
36723482
{"test_weakref_capi", test_weakref_capi, METH_NOARGS},
3673-
{"test_dict_capi", test_dict_capi, METH_NOARGS},
36743483
{"sys_getobject", sys_getobject, METH_O},
36753484
{"sys_setobject", sys_setobject, METH_VARARGS},
36763485
{NULL, NULL} /* sentinel */

0 commit comments

Comments
 (0)