gh-108511: Add C API functions which do not silently ignore errors (GH-109025)

Add the following functions:

* PyObject_HasAttrWithError()
* PyObject_HasAttrStringWithError()
* PyMapping_HasKeyWithError()
* PyMapping_HasKeyStringWithError()
This commit is contained in:
Serhiy Storchaka 2023-09-17 14:23:31 +03:00 committed by GitHub
parent e57ecf6bbc
commit add16f1a5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 330 additions and 111 deletions

View file

@ -55,8 +55,7 @@ ga_repr_item(_PyUnicodeWriter *writer, PyObject *p)
PyObject *qualname = NULL;
PyObject *module = NULL;
PyObject *r = NULL;
PyObject *tmp;
int err;
int rc;
if (p == Py_Ellipsis) {
// The Ellipsis object
@ -64,19 +63,14 @@ ga_repr_item(_PyUnicodeWriter *writer, PyObject *p)
goto done;
}
if (PyObject_GetOptionalAttr(p, &_Py_ID(__origin__), &tmp) < 0) {
goto done;
if ((rc = PyObject_HasAttrWithError(p, &_Py_ID(__origin__))) > 0 &&
(rc = PyObject_HasAttrWithError(p, &_Py_ID(__args__))) > 0)
{
// It looks like a GenericAlias
goto use_repr;
}
if (tmp != NULL) {
Py_DECREF(tmp);
if (PyObject_GetOptionalAttr(p, &_Py_ID(__args__), &tmp) < 0) {
goto done;
}
if (tmp != NULL) {
Py_DECREF(tmp);
// It looks like a GenericAlias
goto use_repr;
}
if (rc < 0) {
goto done;
}
if (PyObject_GetOptionalAttr(p, &_Py_ID(__qualname__), &qualname) < 0) {
@ -113,13 +107,13 @@ ga_repr_item(_PyUnicodeWriter *writer, PyObject *p)
Py_XDECREF(module);
if (r == NULL) {
// error if any of the above PyObject_Repr/PyUnicode_From* fail
err = -1;
rc = -1;
}
else {
err = _PyUnicodeWriter_WriteStr(writer, r);
rc = _PyUnicodeWriter_WriteStr(writer, r);
Py_DECREF(r);
}
return err;
return rc;
}
static int
@ -253,18 +247,17 @@ _Py_make_parameters(PyObject *args)
Py_ssize_t iparam = 0;
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
PyObject *t = PyTuple_GET_ITEM(args, iarg);
PyObject *subst;
// We don't want __parameters__ descriptor of a bare Python class.
if (PyType_Check(t)) {
continue;
}
if (PyObject_GetOptionalAttr(t, &_Py_ID(__typing_subst__), &subst) < 0) {
int rc = PyObject_HasAttrWithError(t, &_Py_ID(__typing_subst__));
if (rc < 0) {
Py_DECREF(parameters);
return NULL;
}
if (subst) {
if (rc) {
iparam += tuple_add(parameters, iparam, t);
Py_DECREF(subst);
}
else {
PyObject *subparams;