Skip to content

Commit d4ea7f3

Browse files
committed
pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives
1 parent 6716254 commit d4ea7f3

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

Modules/_ctypes/_ctypes.c

+2-9
Original file line numberDiff line numberDiff line change
@@ -2200,7 +2200,6 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)
22002200
struct fielddesc *fd;
22012201
PyObject *as_parameter;
22022202
int res;
2203-
PyObject *exc, *val, *tb;
22042203

22052204
/* If the value is already an instance of the requested type,
22062205
we can use it as is */
@@ -2234,33 +2233,27 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)
22342233
parg->obj = fd->setfunc(&parg->value, value, 0);
22352234
if (parg->obj)
22362235
return (PyObject *)parg;
2237-
PyErr_Fetch(&exc, &val, &tb);
2236+
PyObject *exc = PyErr_GetRaisedException();
22382237
Py_DECREF(parg);
22392238

22402239
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
22412240
Py_XDECREF(exc);
2242-
Py_XDECREF(val);
2243-
Py_XDECREF(tb);
22442241
return NULL;
22452242
}
22462243
if (as_parameter) {
22472244
if (_Py_EnterRecursiveCall("while processing _as_parameter_")) {
22482245
Py_DECREF(as_parameter);
22492246
Py_XDECREF(exc);
2250-
Py_XDECREF(val);
2251-
Py_XDECREF(tb);
22522247
return NULL;
22532248
}
22542249
value = PyCSimpleType_from_param(type, as_parameter);
22552250
_Py_LeaveRecursiveCall();
22562251
Py_DECREF(as_parameter);
22572252
Py_XDECREF(exc);
2258-
Py_XDECREF(val);
2259-
Py_XDECREF(tb);
22602253
return value;
22612254
}
22622255
if (exc) {
2263-
PyErr_Restore(exc, val, tb);
2256+
PyErr_SetRaisedException(exc);
22642257
}
22652258
else {
22662259
PyErr_SetString(PyExc_TypeError, "wrong type");

Modules/_ctypes/callproc.c

+19-17
Original file line numberDiff line numberDiff line change
@@ -1013,41 +1013,43 @@ static PyObject *GetResult(PyObject *restype, void *result, PyObject *checker)
10131013
void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...)
10141014
{
10151015
va_list vargs;
1016-
PyObject *tp, *v, *tb, *s, *cls_str, *msg_str;
10171016

10181017
va_start(vargs, fmt);
1019-
s = PyUnicode_FromFormatV(fmt, vargs);
1018+
PyObject *s = PyUnicode_FromFormatV(fmt, vargs);
10201019
va_end(vargs);
1021-
if (!s)
1020+
if (s == NULL) {
10221021
return;
1022+
}
10231023

1024-
PyErr_Fetch(&tp, &v, &tb);
1025-
PyErr_NormalizeException(&tp, &v, &tb);
1026-
if (PyType_Check(tp))
1027-
cls_str = PyType_GetName((PyTypeObject *)tp);
1028-
else
1029-
cls_str = PyObject_Str(tp);
1024+
assert(PyErr_Occurred());
1025+
PyObject *exc = PyErr_GetRaisedException();
1026+
assert(exc != NULL);
1027+
PyObject *cls_str = PyType_GetName(Py_TYPE(exc));
10301028
if (cls_str) {
10311029
PyUnicode_AppendAndDel(&s, cls_str);
10321030
PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": "));
1033-
if (s == NULL)
1031+
if (s == NULL) {
10341032
goto error;
1035-
} else
1033+
}
1034+
}
1035+
else {
10361036
PyErr_Clear();
1037-
msg_str = PyObject_Str(v);
1038-
if (msg_str)
1037+
}
1038+
1039+
PyObject *msg_str = PyObject_Str(exc);
1040+
if (msg_str) {
10391041
PyUnicode_AppendAndDel(&s, msg_str);
1042+
}
10401043
else {
10411044
PyErr_Clear();
10421045
PyUnicode_AppendAndDel(&s, PyUnicode_FromString("???"));
10431046
}
1044-
if (s == NULL)
1047+
if (s == NULL) {
10451048
goto error;
1049+
}
10461050
PyErr_SetObject(exc_class, s);
10471051
error:
1048-
Py_XDECREF(tp);
1049-
Py_XDECREF(v);
1050-
Py_XDECREF(tb);
1052+
Py_XDECREF(exc);
10511053
Py_XDECREF(s);
10521054
}
10531055

0 commit comments

Comments
 (0)