Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were unused before this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were used in a couple of Restore() calls, scroll down.

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