mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives in _ctypes
(#102477)
This commit is contained in:
parent
71cf7c3ddd
commit
2999e02836
2 changed files with 21 additions and 26 deletions
|
@ -2200,7 +2200,6 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)
|
|||
struct fielddesc *fd;
|
||||
PyObject *as_parameter;
|
||||
int res;
|
||||
PyObject *exc, *val, *tb;
|
||||
|
||||
/* If the value is already an instance of the requested type,
|
||||
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);
|
||||
if (parg->obj)
|
||||
return (PyObject *)parg;
|
||||
PyErr_Fetch(&exc, &val, &tb);
|
||||
PyObject *exc = PyErr_GetRaisedException();
|
||||
Py_DECREF(parg);
|
||||
|
||||
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
|
||||
Py_XDECREF(exc);
|
||||
Py_XDECREF(val);
|
||||
Py_XDECREF(tb);
|
||||
return NULL;
|
||||
}
|
||||
if (as_parameter) {
|
||||
if (_Py_EnterRecursiveCall("while processing _as_parameter_")) {
|
||||
Py_DECREF(as_parameter);
|
||||
Py_XDECREF(exc);
|
||||
Py_XDECREF(val);
|
||||
Py_XDECREF(tb);
|
||||
return NULL;
|
||||
}
|
||||
value = PyCSimpleType_from_param(type, as_parameter);
|
||||
_Py_LeaveRecursiveCall();
|
||||
Py_DECREF(as_parameter);
|
||||
Py_XDECREF(exc);
|
||||
Py_XDECREF(val);
|
||||
Py_XDECREF(tb);
|
||||
return value;
|
||||
}
|
||||
if (exc) {
|
||||
PyErr_Restore(exc, val, tb);
|
||||
PyErr_SetRaisedException(exc);
|
||||
}
|
||||
else {
|
||||
PyErr_SetString(PyExc_TypeError, "wrong type");
|
||||
|
|
|
@ -1013,41 +1013,43 @@ static PyObject *GetResult(PyObject *restype, void *result, PyObject *checker)
|
|||
void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...)
|
||||
{
|
||||
va_list vargs;
|
||||
PyObject *tp, *v, *tb, *s, *cls_str, *msg_str;
|
||||
|
||||
va_start(vargs, fmt);
|
||||
s = PyUnicode_FromFormatV(fmt, vargs);
|
||||
PyObject *s = PyUnicode_FromFormatV(fmt, vargs);
|
||||
va_end(vargs);
|
||||
if (!s)
|
||||
if (s == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
PyErr_Fetch(&tp, &v, &tb);
|
||||
PyErr_NormalizeException(&tp, &v, &tb);
|
||||
if (PyType_Check(tp))
|
||||
cls_str = PyType_GetName((PyTypeObject *)tp);
|
||||
else
|
||||
cls_str = PyObject_Str(tp);
|
||||
assert(PyErr_Occurred());
|
||||
PyObject *exc = PyErr_GetRaisedException();
|
||||
assert(exc != NULL);
|
||||
PyObject *cls_str = PyType_GetName(Py_TYPE(exc));
|
||||
if (cls_str) {
|
||||
PyUnicode_AppendAndDel(&s, cls_str);
|
||||
PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": "));
|
||||
if (s == NULL)
|
||||
if (s == NULL) {
|
||||
goto error;
|
||||
} else
|
||||
}
|
||||
}
|
||||
else {
|
||||
PyErr_Clear();
|
||||
msg_str = PyObject_Str(v);
|
||||
if (msg_str)
|
||||
}
|
||||
|
||||
PyObject *msg_str = PyObject_Str(exc);
|
||||
if (msg_str) {
|
||||
PyUnicode_AppendAndDel(&s, msg_str);
|
||||
}
|
||||
else {
|
||||
PyErr_Clear();
|
||||
PyUnicode_AppendAndDel(&s, PyUnicode_FromString("???"));
|
||||
}
|
||||
if (s == NULL)
|
||||
if (s == NULL) {
|
||||
goto error;
|
||||
}
|
||||
PyErr_SetObject(exc_class, s);
|
||||
error:
|
||||
Py_XDECREF(tp);
|
||||
Py_XDECREF(v);
|
||||
Py_XDECREF(tb);
|
||||
Py_XDECREF(exc);
|
||||
Py_XDECREF(s);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue