Skip to content

Commit

Permalink
pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient al…
Browse files Browse the repository at this point in the history
…ternatives in tkinter module
  • Loading branch information
iritkatriel committed Feb 28, 2023
1 parent 6b2d7c0 commit e68fa8b
Showing 1 changed file with 24 additions and 34 deletions.
58 changes: 24 additions & 34 deletions Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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));
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();;
}

}
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2628,7 +2618,7 @@ TimerHandler(ClientData clientData)

if (res == NULL) {
errorInCmd = 1;
PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
excInCmd = PyErr_GetRaisedException();
}
else
Py_DECREF(res);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e68fa8b

Please sign in to comment.