Skip to content

Commit

Permalink
gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternat…
Browse files Browse the repository at this point in the history
…ives in `_ctypes` (#102477)
  • Loading branch information
iritkatriel authored and pull[bot] committed Mar 12, 2023
1 parent a4e6dbd commit 2007092
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
11 changes: 2 additions & 9 deletions Modules/_ctypes/_ctypes.c
Expand Up @@ -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 */
Expand Down Expand Up @@ -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");
Expand Down
36 changes: 19 additions & 17 deletions Modules/_ctypes/callproc.c
Expand Up @@ -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);
}

Expand Down

0 comments on commit 2007092

Please sign in to comment.