gh-106672: C API: Report indiscriminately ignored errors (GH-106674)

Functions which indiscriminately ignore all errors now report them as
unraisable errors.
This commit is contained in:
Serhiy Storchaka 2023-11-07 15:58:04 +02:00 committed by GitHub
parent a077b2fbb8
commit f55cb44359
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 195 additions and 45 deletions

View file

@ -2468,31 +2468,53 @@ PyMapping_HasKeyWithError(PyObject *obj, PyObject *key)
}
int
PyMapping_HasKeyString(PyObject *o, const char *key)
PyMapping_HasKeyString(PyObject *obj, const char *key)
{
PyObject *v;
v = PyMapping_GetItemString(o, key);
if (v) {
Py_DECREF(v);
return 1;
PyObject *value;
int rc;
if (obj == NULL) {
// For backward compatibility.
// PyMapping_GetOptionalItemString() crashes if obj is NULL.
null_error();
rc = -1;
}
PyErr_Clear();
return 0;
else {
rc = PyMapping_GetOptionalItemString(obj, key, &value);
}
if (rc < 0) {
PyErr_FormatUnraisable(
"Exception ignored in PyMapping_HasKeyString(); consider using "
"PyMapping_HasKeyStringWithError(), "
"PyMapping_GetOptionalItemString() or PyMapping_GetItemString()");
return 0;
}
Py_XDECREF(value);
return rc;
}
int
PyMapping_HasKey(PyObject *o, PyObject *key)
PyMapping_HasKey(PyObject *obj, PyObject *key)
{
PyObject *v;
v = PyObject_GetItem(o, key);
if (v) {
Py_DECREF(v);
return 1;
PyObject *value;
int rc;
if (obj == NULL || key == NULL) {
// For backward compatibility.
// PyMapping_GetOptionalItem() crashes if any of them is NULL.
null_error();
rc = -1;
}
PyErr_Clear();
return 0;
else {
rc = PyMapping_GetOptionalItem(obj, key, &value);
}
if (rc < 0) {
PyErr_FormatUnraisable(
"Exception ignored in PyMapping_HasKey(); consider using "
"PyMapping_HasKeyWithError(), "
"PyMapping_GetOptionalItem() or PyObject_GetItem()");
return 0;
}
Py_XDECREF(value);
return rc;
}
/* This function is quite similar to PySequence_Fast(), but specialized to be