gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives in _ctypes (#102477)

This commit is contained in:
Irit Katriel 2023-03-10 09:02:32 +00:00 committed by GitHub
parent 71cf7c3ddd
commit 2999e02836
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 26 deletions

View file

@ -2200,7 +2200,6 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)
struct fielddesc *fd; struct fielddesc *fd;
PyObject *as_parameter; PyObject *as_parameter;
int res; int res;
PyObject *exc, *val, *tb;
/* If the value is already an instance of the requested type, /* If the value is already an instance of the requested type,
we can use it as is */ we can use it as is */
@ -2234,33 +2233,27 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)
parg->obj = fd->setfunc(&parg->value, value, 0); parg->obj = fd->setfunc(&parg->value, value, 0);
if (parg->obj) if (parg->obj)
return (PyObject *)parg; return (PyObject *)parg;
PyErr_Fetch(&exc, &val, &tb); PyObject *exc = PyErr_GetRaisedException();
Py_DECREF(parg); Py_DECREF(parg);
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) { if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
Py_XDECREF(exc); Py_XDECREF(exc);
Py_XDECREF(val);
Py_XDECREF(tb);
return NULL; return NULL;
} }
if (as_parameter) { if (as_parameter) {
if (_Py_EnterRecursiveCall("while processing _as_parameter_")) { if (_Py_EnterRecursiveCall("while processing _as_parameter_")) {
Py_DECREF(as_parameter); Py_DECREF(as_parameter);
Py_XDECREF(exc); Py_XDECREF(exc);
Py_XDECREF(val);
Py_XDECREF(tb);
return NULL; return NULL;
} }
value = PyCSimpleType_from_param(type, as_parameter); value = PyCSimpleType_from_param(type, as_parameter);
_Py_LeaveRecursiveCall(); _Py_LeaveRecursiveCall();
Py_DECREF(as_parameter); Py_DECREF(as_parameter);
Py_XDECREF(exc); Py_XDECREF(exc);
Py_XDECREF(val);
Py_XDECREF(tb);
return value; return value;
} }
if (exc) { if (exc) {
PyErr_Restore(exc, val, tb); PyErr_SetRaisedException(exc);
} }
else { else {
PyErr_SetString(PyExc_TypeError, "wrong type"); PyErr_SetString(PyExc_TypeError, "wrong type");

View file

@ -1013,41 +1013,43 @@ static PyObject *GetResult(PyObject *restype, void *result, PyObject *checker)
void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...) void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...)
{ {
va_list vargs; va_list vargs;
PyObject *tp, *v, *tb, *s, *cls_str, *msg_str;
va_start(vargs, fmt); va_start(vargs, fmt);
s = PyUnicode_FromFormatV(fmt, vargs); PyObject *s = PyUnicode_FromFormatV(fmt, vargs);
va_end(vargs); va_end(vargs);
if (!s) if (s == NULL) {
return; return;
}
PyErr_Fetch(&tp, &v, &tb); assert(PyErr_Occurred());
PyErr_NormalizeException(&tp, &v, &tb); PyObject *exc = PyErr_GetRaisedException();
if (PyType_Check(tp)) assert(exc != NULL);
cls_str = PyType_GetName((PyTypeObject *)tp); PyObject *cls_str = PyType_GetName(Py_TYPE(exc));
else
cls_str = PyObject_Str(tp);
if (cls_str) { if (cls_str) {
PyUnicode_AppendAndDel(&s, cls_str); PyUnicode_AppendAndDel(&s, cls_str);
PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": ")); PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": "));
if (s == NULL) if (s == NULL) {
goto error; goto error;
} else }
}
else {
PyErr_Clear(); PyErr_Clear();
msg_str = PyObject_Str(v); }
if (msg_str)
PyObject *msg_str = PyObject_Str(exc);
if (msg_str) {
PyUnicode_AppendAndDel(&s, msg_str); PyUnicode_AppendAndDel(&s, msg_str);
}
else { else {
PyErr_Clear(); PyErr_Clear();
PyUnicode_AppendAndDel(&s, PyUnicode_FromString("???")); PyUnicode_AppendAndDel(&s, PyUnicode_FromString("???"));
} }
if (s == NULL) if (s == NULL) {
goto error; goto error;
}
PyErr_SetObject(exc_class, s); PyErr_SetObject(exc_class, s);
error: error:
Py_XDECREF(tp); Py_XDECREF(exc);
Py_XDECREF(v);
Py_XDECREF(tb);
Py_XDECREF(s); Py_XDECREF(s);
} }