Skip to content

Commit d3a519d

Browse files
committed
restore existing error handling code style
by introducing a non-bracket Tcl lock release macro
1 parent fe397b8 commit d3a519d

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

Modules/_tkinter.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ _get_tcl_lib_path(void)
205205
waiting for events.
206206
207207
To solve this problem, a separate lock for Tcl is introduced.
208-
We normally treat holding it as incompatible with holding Python's
208+
We strive to not hold it at the same time as Python's
209209
interpreter lock. The following macros manipulate both locks together.
210210
211211
ENTER_TCL and LEAVE_TCL are brackets, just like Py_BEGIN_ALLOW_THREADS and
@@ -222,7 +222,9 @@ _get_tcl_lib_path(void)
222222
finer lock control are: ENTER_OVERLAP acquires the Python lock (and restores
223223
the thread state) when already holding the Tcl lock; LEAVE_OVERLAP releases
224224
the Python lock and keeps the Tcl lock; and LEAVE_OVERLAP_TCL releases the
225-
Tcl lock and keeps the Python lock.
225+
Tcl lock and keeps the Python lock. LEAVE_OVERLAP_TCL_ON_ERROR is the same
226+
as LEAVE_OVERLAP_TCL but is not a bracket; it is to be used in error handling
227+
blocks that exit a function early.
226228
227229
By contrast, ENTER_PYTHON and LEAVE_PYTHON are used in Tcl event
228230
handlers when the handler needs to use Python. Such event handlers
@@ -322,7 +324,10 @@ if (tcl_lock) { \
322324
Py_BEGIN_ALLOW_THREADS
323325

324326
#define LEAVE_OVERLAP_TCL \
325-
tcl_tstate = NULL; RELEASE_TCL_LOCK }
327+
LEAVE_OVERLAP_TCL_ON_ERROR }
328+
329+
#define LEAVE_OVERLAP_TCL_ON_ERROR \
330+
tcl_tstate = NULL; RELEASE_TCL_LOCK
326331

327332
#define ENTER_PYTHON \
328333
{ PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; PyEval_RestoreThread((tstate)); }
@@ -1568,10 +1573,12 @@ Tkapp_Call(PyObject *selfptr, PyObject *args)
15681573
ENTER_OVERLAP
15691574

15701575
objv = Tkapp_CallArgs(args, objStore, &objc);
1576+
if (!objv) {
1577+
LEAVE_OVERLAP_TCL_ON_ERROR
1578+
return NULL;
1579+
}
15711580

1572-
if (objv) {
1573-
1574-
LEAVE_OVERLAP
1581+
LEAVE_OVERLAP
15751582

15761583
i = Tcl_EvalObjv(self->interp, objc, objv, flags);
15771584

@@ -1582,14 +1589,9 @@ Tkapp_Call(PyObject *selfptr, PyObject *args)
15821589
else
15831590
res = Tkapp_ObjectResult(self);
15841591

1585-
LEAVE_OVERLAP
1592+
LEAVE_OVERLAP_TCL
15861593

15871594
Tkapp_CallDeallocArgs(objv, objStore, objc);
1588-
1589-
ENTER_OVERLAP
1590-
1591-
}
1592-
LEAVE_OVERLAP_TCL
15931595
}
15941596
return res;
15951597
}
@@ -1866,10 +1868,10 @@ SetVar(TkappObject *self, PyObject *args, int flags)
18661868

18671869
ENTER_TCL
18681870
ENTER_OVERLAP
1871+
18691872
newval = AsObj(newValue);
18701873
if (newval == NULL) {
1871-
{
1872-
LEAVE_OVERLAP_TCL
1874+
LEAVE_OVERLAP_TCL_ON_ERROR
18731875
return NULL;
18741876
}
18751877

@@ -1882,15 +1884,14 @@ SetVar(TkappObject *self, PyObject *args, int flags)
18821884
}
18831885
LEAVE_OVERLAP
18841886

1885-
ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, NULL,
1886-
newval, flags);
1887+
ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, NULL,
1888+
newval, flags);
18871889
ENTER_OVERLAP
18881890
if (!ok)
18891891
Tkinter_Error(self);
18901892
else {
18911893
res = Py_NewRef(Py_None);
18921894
}
1893-
}
18941895
LEAVE_OVERLAP_TCL
18951896
break;
18961897
case 3:
@@ -1903,8 +1904,7 @@ SetVar(TkappObject *self, PyObject *args, int flags)
19031904
ENTER_OVERLAP
19041905
newval = AsObj(newValue);
19051906
if (newval == NULL) {
1906-
{
1907-
LEAVE_OVERLAP_TCL
1907+
LEAVE_OVERLAP_TCL_ON_ERROR
19081908
return NULL;
19091909
}
19101910

@@ -1930,7 +1930,6 @@ SetVar(TkappObject *self, PyObject *args, int flags)
19301930
else {
19311931
res = Py_NewRef(Py_None);
19321932
}
1933-
}
19341933
LEAVE_OVERLAP_TCL
19351934
break;
19361935
default:

0 commit comments

Comments
 (0)