Skip to content

Commit 06c073a

Browse files
committed
pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives in tkinter module
1 parent 6b2d7c0 commit 06c073a

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

Modules/_tkinter.c

+23-33
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,6 @@ static PyObject *Tkinter_TclError;
321321
static int quitMainLoop = 0;
322322
static int errorInCmd = 0;
323323
static PyObject *excInCmd;
324-
static PyObject *valInCmd;
325-
static PyObject *trbInCmd;
326324

327325
#ifdef TKINTER_PROTECT_LOADTK
328326
static int tk_load_failed = 0;
@@ -1222,7 +1220,7 @@ typedef struct Tkapp_CallEvent {
12221220
PyObject *args;
12231221
int flags;
12241222
PyObject **res;
1225-
PyObject **exc_type, **exc_value, **exc_tb;
1223+
PyObject **exc;
12261224
Tcl_Condition *done;
12271225
} Tkapp_CallEvent;
12281226

@@ -1339,7 +1337,7 @@ Tkapp_CallProc(Tkapp_CallEvent *e, int flags)
13391337
ENTER_PYTHON
13401338
objv = Tkapp_CallArgs(e->args, objStore, &objc);
13411339
if (!objv) {
1342-
PyErr_Fetch(e->exc_type, e->exc_value, e->exc_tb);
1340+
*(e->exc) = PyErr_GetRaisedException();
13431341
*(e->res) = NULL;
13441342
}
13451343
LEAVE_PYTHON
@@ -1354,7 +1352,7 @@ Tkapp_CallProc(Tkapp_CallEvent *e, int flags)
13541352
*(e->res) = Tkapp_ObjectResult(e->self);
13551353
}
13561354
if (*(e->res) == NULL) {
1357-
PyErr_Fetch(e->exc_type, e->exc_value, e->exc_tb);
1355+
*(e->exc) = PyErr_GetRaisedException();
13581356
}
13591357
LEAVE_PYTHON
13601358

@@ -1401,7 +1399,7 @@ Tkapp_Call(PyObject *selfptr, PyObject *args)
14011399
marshal the parameters to the interpreter thread. */
14021400
Tkapp_CallEvent *ev;
14031401
Tcl_Condition cond = NULL;
1404-
PyObject *exc_type, *exc_value, *exc_tb;
1402+
PyObject *exc;
14051403
if (!WaitForMainloop(self))
14061404
return NULL;
14071405
ev = (Tkapp_CallEvent*)attemptckalloc(sizeof(Tkapp_CallEvent));
@@ -1413,18 +1411,18 @@ Tkapp_Call(PyObject *selfptr, PyObject *args)
14131411
ev->self = self;
14141412
ev->args = args;
14151413
ev->res = &res;
1416-
ev->exc_type = &exc_type;
1417-
ev->exc_value = &exc_value;
1418-
ev->exc_tb = &exc_tb;
1414+
ev->exc = &exc;
14191415
ev->done = &cond;
14201416

14211417
Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &call_mutex);
14221418

14231419
if (res == NULL) {
1424-
if (exc_type)
1425-
PyErr_Restore(exc_type, exc_value, exc_tb);
1426-
else
1427-
PyErr_SetObject(Tkinter_TclError, exc_value);
1420+
if (exc) {
1421+
PyErr_SetRaisedException(exc);
1422+
}
1423+
else {
1424+
PyErr_SetObject(Tkinter_TclError, exc);
1425+
}
14281426
}
14291427
Tcl_ConditionFinalize(&cond);
14301428
}
@@ -1578,8 +1576,7 @@ typedef struct VarEvent {
15781576
int flags;
15791577
EventFunc func;
15801578
PyObject **res;
1581-
PyObject **exc_type;
1582-
PyObject **exc_val;
1579+
PyObject **exc;
15831580
Tcl_Condition *cond;
15841581
} VarEvent;
15851582

@@ -1643,12 +1640,7 @@ var_perform(VarEvent *ev)
16431640
{
16441641
*(ev->res) = ev->func(ev->self, ev->args, ev->flags);
16451642
if (!*(ev->res)) {
1646-
PyObject *exc, *val, *tb;
1647-
PyErr_Fetch(&exc, &val, &tb);
1648-
PyErr_NormalizeException(&exc, &val, &tb);
1649-
*(ev->exc_type) = exc;
1650-
*(ev->exc_val) = val;
1651-
Py_XDECREF(tb);
1643+
*(ev->exc) = PyErr_GetRaisedException();;
16521644
}
16531645

16541646
}
@@ -1672,7 +1664,7 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
16721664
TkappObject *self = (TkappObject*)selfptr;
16731665
if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
16741666
VarEvent *ev;
1675-
PyObject *res, *exc_type, *exc_val;
1667+
PyObject *res, *exc;
16761668
Tcl_Condition cond = NULL;
16771669

16781670
/* The current thread is not the interpreter thread. Marshal
@@ -1691,16 +1683,14 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
16911683
ev->flags = flags;
16921684
ev->func = func;
16931685
ev->res = &res;
1694-
ev->exc_type = &exc_type;
1695-
ev->exc_val = &exc_val;
1686+
ev->exc = &exc;
16961687
ev->cond = &cond;
16971688
ev->ev.proc = (Tcl_EventProc*)var_proc;
16981689
Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex);
16991690
Tcl_ConditionFinalize(&cond);
17001691
if (!res) {
1701-
PyErr_SetObject(exc_type, exc_val);
1702-
Py_DECREF(exc_type);
1703-
Py_DECREF(exc_val);
1692+
PyErr_SetObject(Py_TYPE(exc), exc);
1693+
Py_DECREF(exc);
17041694
return NULL;
17051695
}
17061696
return res;
@@ -2188,7 +2178,7 @@ static int
21882178
PythonCmd_Error(Tcl_Interp *interp)
21892179
{
21902180
errorInCmd = 1;
2191-
PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
2181+
excInCmd = PyErr_GetRaisedException();
21922182
LEAVE_PYTHON
21932183
return TCL_ERROR;
21942184
}
@@ -2458,7 +2448,7 @@ FileHandler(ClientData clientData, int mask)
24582448
res = PyObject_CallFunction(func, "Oi", file, mask);
24592449
if (res == NULL) {
24602450
errorInCmd = 1;
2461-
PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
2451+
excInCmd = PyErr_GetRaisedException();
24622452
}
24632453
Py_XDECREF(res);
24642454
LEAVE_PYTHON
@@ -2628,7 +2618,7 @@ TimerHandler(ClientData clientData)
26282618

26292619
if (res == NULL) {
26302620
errorInCmd = 1;
2631-
PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
2621+
excInCmd = PyErr_GetRaisedException();
26322622
}
26332623
else
26342624
Py_DECREF(res);
@@ -2725,7 +2715,7 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
27252715

27262716
if (errorInCmd) {
27272717
errorInCmd = 0;
2728-
PyErr_Restore(excInCmd, valInCmd, trbInCmd);
2718+
PyErr_SetRaisedException(excInCmd);
27292719
excInCmd = valInCmd = trbInCmd = NULL;
27302720
return NULL;
27312721
}
@@ -3187,8 +3177,8 @@ EventHook(void)
31873177
#endif
31883178
if (errorInCmd) {
31893179
errorInCmd = 0;
3190-
PyErr_Restore(excInCmd, valInCmd, trbInCmd);
3191-
excInCmd = valInCmd = trbInCmd = NULL;
3180+
PyErr_SetRaisedException(excInCmd);
3181+
excInCmd = NULL;
31923182
PyErr_Print();
31933183
}
31943184
PyEval_SaveThread();

0 commit comments

Comments
 (0)