◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
15 changes: 15 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,21 @@ static inline int Py_IsFinalizing(void)
#endif


#ifdef __cplusplus
}
#endif
Expand Down
27 changes: 19 additions & 8 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ test_getitem(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))


static PyObject *
test_dict_getitemref(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
assert(!PyErr_Occurred());

Expand All @@ -1112,12 +1112,18 @@ test_dict_getitemref(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
if (key == NULL) {
goto error;
}

missing_key = PyUnicode_FromString("missing_key");
if (missing_key == NULL) {
goto error;
}

value = PyUnicode_FromString("value");
if (value == NULL) {
goto error;
Expand All @@ -1129,6 +1135,17 @@ test_dict_getitemref(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
}
assert(res == 0);

// test PyDict_GetItemRef(), key is present
get_value = Py_Ellipsis; // marker value
assert(PyDict_GetItemRef(dict, key, &get_value) == 1);
Expand All @@ -1154,7 +1171,6 @@ test_dict_getitemref(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
assert(get_value == NULL);

// test PyDict_GetItemRef(), invalid dict
invalid_dict = key; // borrowed reference
get_value = Py_Ellipsis; // marker value
assert(PyDict_GetItemRef(invalid_dict, key, &get_value) == -1);
assert(PyErr_ExceptionMatches(PyExc_SystemError));
Expand All @@ -1168,11 +1184,6 @@ test_dict_getitemref(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
PyErr_Clear();
assert(get_value == NULL);

invalid_key = PyList_New(0); // not hashable key
if (invalid_key == NULL) {
goto error;
}

// test PyDict_GetItemRef(), invalid key
get_value = Py_Ellipsis; // marker value
assert(PyDict_GetItemRef(dict, invalid_key, &get_value) == -1);
Expand Down Expand Up @@ -1222,7 +1233,7 @@ static struct PyMethodDef methods[] = {
{"test_vectorcall", test_vectorcall, METH_NOARGS, _Py_NULL},
{"test_getattr", test_getattr, METH_NOARGS, _Py_NULL},
{"test_getitem", test_getitem, METH_NOARGS, _Py_NULL},
{"test_dict_getitemref", test_dict_getitemref, METH_NOARGS, _Py_NULL},
{_Py_NULL, _Py_NULL, 0, _Py_NULL}
};

Expand Down
Toggle all file notes Toggle all file annotations