gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives in tkinter module (#102319)

This commit is contained in:
Irit Katriel 2023-03-01 09:49:23 +00:00 committed by GitHub
parent 7d1d663418
commit f91846ba39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 34 deletions

View File

@ -321,8 +321,6 @@ static PyObject *Tkinter_TclError;
static int quitMainLoop = 0;
static int errorInCmd = 0;
static PyObject *excInCmd;
static PyObject *valInCmd;
static PyObject *trbInCmd;
#ifdef TKINTER_PROTECT_LOADTK
static int tk_load_failed = 0;
@ -1222,7 +1220,7 @@ typedef struct Tkapp_CallEvent {
PyObject *args;
int flags;
PyObject **res;
PyObject **exc_type, **exc_value, **exc_tb;
PyObject **exc;
Tcl_Condition *done;
} Tkapp_CallEvent;
@ -1339,7 +1337,7 @@ Tkapp_CallProc(Tkapp_CallEvent *e, int flags)
ENTER_PYTHON
objv = Tkapp_CallArgs(e->args, objStore, &objc);
if (!objv) {
PyErr_Fetch(e->exc_type, e->exc_value, e->exc_tb);
*(e->exc) = PyErr_GetRaisedException();
*(e->res) = NULL;
}
LEAVE_PYTHON
@ -1354,7 +1352,7 @@ Tkapp_CallProc(Tkapp_CallEvent *e, int flags)
*(e->res) = Tkapp_ObjectResult(e->self);
}
if (*(e->res) == NULL) {
PyErr_Fetch(e->exc_type, e->exc_value, e->exc_tb);
*(e->exc) = PyErr_GetRaisedException();
}
LEAVE_PYTHON
@ -1401,7 +1399,7 @@ Tkapp_Call(PyObject *selfptr, PyObject *args)
marshal the parameters to the interpreter thread. */
Tkapp_CallEvent *ev;
Tcl_Condition cond = NULL;
PyObject *exc_type, *exc_value, *exc_tb;
PyObject *exc;
if (!WaitForMainloop(self))
return NULL;
ev = (Tkapp_CallEvent*)attemptckalloc(sizeof(Tkapp_CallEvent));
@ -1413,18 +1411,18 @@ Tkapp_Call(PyObject *selfptr, PyObject *args)
ev->self = self;
ev->args = args;
ev->res = &res;
ev->exc_type = &exc_type;
ev->exc_value = &exc_value;
ev->exc_tb = &exc_tb;
ev->exc = &exc;
ev->done = &cond;
Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &call_mutex);
if (res == NULL) {
if (exc_type)
PyErr_Restore(exc_type, exc_value, exc_tb);
else
PyErr_SetObject(Tkinter_TclError, exc_value);
if (exc) {
PyErr_SetRaisedException(exc);
}
else {
PyErr_SetObject(Tkinter_TclError, exc);
}
}
Tcl_ConditionFinalize(&cond);
}
@ -1578,8 +1576,7 @@ typedef struct VarEvent {
int flags;
EventFunc func;
PyObject **res;
PyObject **exc_type;
PyObject **exc_val;
PyObject **exc;
Tcl_Condition *cond;
} VarEvent;
@ -1643,12 +1640,7 @@ var_perform(VarEvent *ev)
{
*(ev->res) = ev->func(ev->self, ev->args, ev->flags);
if (!*(ev->res)) {
PyObject *exc, *val, *tb;
PyErr_Fetch(&exc, &val, &tb);
PyErr_NormalizeException(&exc, &val, &tb);
*(ev->exc_type) = exc;
*(ev->exc_val) = val;
Py_XDECREF(tb);
*(ev->exc) = PyErr_GetRaisedException();;
}
}
@ -1672,7 +1664,7 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
TkappObject *self = (TkappObject*)selfptr;
if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
VarEvent *ev;
PyObject *res, *exc_type, *exc_val;
PyObject *res, *exc;
Tcl_Condition cond = NULL;
/* The current thread is not the interpreter thread. Marshal
@ -1691,16 +1683,14 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
ev->flags = flags;
ev->func = func;
ev->res = &res;
ev->exc_type = &exc_type;
ev->exc_val = &exc_val;
ev->exc = &exc;
ev->cond = &cond;
ev->ev.proc = (Tcl_EventProc*)var_proc;
Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex);
Tcl_ConditionFinalize(&cond);
if (!res) {
PyErr_SetObject(exc_type, exc_val);
Py_DECREF(exc_type);
Py_DECREF(exc_val);
PyErr_SetObject((PyObject*)Py_TYPE(exc), exc);
Py_DECREF(exc);
return NULL;
}
return res;
@ -2188,7 +2178,7 @@ static int
PythonCmd_Error(Tcl_Interp *interp)
{
errorInCmd = 1;
PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
excInCmd = PyErr_GetRaisedException();
LEAVE_PYTHON
return TCL_ERROR;
}
@ -2458,7 +2448,7 @@ FileHandler(ClientData clientData, int mask)
res = PyObject_CallFunction(func, "Oi", file, mask);
if (res == NULL) {
errorInCmd = 1;
PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
excInCmd = PyErr_GetRaisedException();
}
Py_XDECREF(res);
LEAVE_PYTHON
@ -2628,7 +2618,7 @@ TimerHandler(ClientData clientData)
if (res == NULL) {
errorInCmd = 1;
PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
excInCmd = PyErr_GetRaisedException();
}
else
Py_DECREF(res);
@ -2725,8 +2715,8 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
if (errorInCmd) {
errorInCmd = 0;
PyErr_Restore(excInCmd, valInCmd, trbInCmd);
excInCmd = valInCmd = trbInCmd = NULL;
PyErr_SetRaisedException(excInCmd);
excInCmd = NULL;
return NULL;
}
Py_RETURN_NONE;
@ -3187,8 +3177,8 @@ EventHook(void)
#endif
if (errorInCmd) {
errorInCmd = 0;
PyErr_Restore(excInCmd, valInCmd, trbInCmd);
excInCmd = valInCmd = trbInCmd = NULL;
PyErr_SetRaisedException(excInCmd);
excInCmd = NULL;
PyErr_Print();
}
PyEval_SaveThread();