diff --git a/Lib/test/test_tkinter/test_misc.py b/Lib/test/test_tkinter/test_misc.py index 63adbdbafbd723..706c680d3d1f52 100644 --- a/Lib/test/test_tkinter/test_misc.py +++ b/Lib/test/test_tkinter/test_misc.py @@ -14,6 +14,7 @@ from tkinter import TclError, ttk import enum from test import support +from test.support import import_helper from test.support import os_helper from test.support.isolation import runInSubprocess from test.support.script_helper import assert_python_ok @@ -749,6 +750,47 @@ def test_accessible(self): if self.root._windowingsystem != 'aqua': acc.emit_focus_change() + @unittest.skipUnless(sys.platform == 'win32', 'requires Windows') + def test_callback_from_foreign_event_loop(self): + # gh-84256: Tk can call an event handler when Tcl was not entered + # by tkinter, for example when a window message is dispatched + # from a foreign event loop while the GIL is released. + ctypes = import_helper.import_module('ctypes') + from ctypes import wintypes + user32 = ctypes.WinDLL('user32') + user32.SendMessageW.argtypes = [wintypes.HWND, wintypes.UINT, + wintypes.WPARAM, wintypes.LPARAM] + user32.SetWindowPos.argtypes = [wintypes.HWND, wintypes.HWND, + ctypes.c_int, ctypes.c_int, + ctypes.c_int, ctypes.c_int, + wintypes.UINT] + WM_ENTERSIZEMOVE = 0x0231 + WM_EXITSIZEMOVE = 0x0232 + SWP_NOMOVE = 0x0002 + SWP_NOZORDER = 0x0004 + SWP_NOACTIVATE = 0x0010 + + top = tkinter.Toplevel(self.root) + top.geometry('200x150') + if not wait_until_mapped(top): + self.skipTest('the window is not mapped') + sizes = [] + def callback(event): + # Call Tcl from the handler. + sizes.append((event.width, event.height, top.winfo_width())) + top.bind('', callback) + hwnd = int(top.wm_frame(), 16) + # Emulate resizing the window with the mouse: Tk handles the + # resulting window messages in its window procedure, outside + # of the Tcl event loop run by tkinter. + user32.SendMessageW(hwnd, WM_ENTERSIZEMOVE, 0, 0) + try: + user32.SetWindowPos(hwnd, None, 0, 0, 250, 180, + SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE) + finally: + user32.SendMessageW(hwnd, WM_EXITSIZEMOVE, 0, 0) + self.assertTrue(sizes) + def test_wait_variable(self): var = tkinter.StringVar(self.root) self.assertEqual(self.root.waitvar, self.root.wait_variable) diff --git a/Misc/NEWS.d/next/Library/2026-11-24-15-30-00.gh-issue-84256.Fz7Rk2.rst b/Misc/NEWS.d/next/Library/2026-11-24-15-30-00.gh-issue-84256.Fz7Rk2.rst new file mode 100644 index 00000000000000..85b5245b68f7e5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-24-15-30-00.gh-issue-84256.Fz7Rk2.rst @@ -0,0 +1,3 @@ +Fix a crash in :mod:`tkinter` on Windows when a Tk event handler, such as +a ```` binding, is called while another event loop is running, +for example when the window is resized while a native dialog is shown. diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index d0a03953a80d92..8c04c109cad77d 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -288,6 +288,14 @@ Tkinter_TkInit(Tcl_Interp *interp) ENTER_TCL/LEAVE_TCL pairs to be contained inside the code between ENTER_PYTHON and LEAVE_PYTHON. + An event handler can also be called when Tcl is not entered with + ENTER_TCL, for example from a foreign event loop on Windows, which + dispatches a window message to Tk while the Python interpreter lock + is released. Then there is no saved thread state and the lock for + Tcl is not held, so ENTER_PYTHON acquires the Python interpreter + lock with PyGILState_Ensure() and LEAVE_PYTHON releases it with + PyGILState_Release(). + These locks expand to several statements and brackets; they should not be used in branches of if statements and the like. @@ -337,15 +345,27 @@ static PyThreadState *tcl_tstate = NULL; tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); } #define ENTER_PYTHON \ - { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \ - if(tcl_lock) \ - PyThread_release_lock(tcl_lock); \ - PyEval_RestoreThread((tstate)); } + { PyThreadState *tstate = tcl_tstate; \ + PyGILState_STATE gilstate = PyGILState_UNLOCKED; \ + if (tstate == NULL) { \ + gilstate = PyGILState_Ensure(); \ + } \ + else { \ + tcl_tstate = NULL; \ + if(tcl_lock)PyThread_release_lock(tcl_lock); \ + PyEval_RestoreThread(tstate); \ + } #define LEAVE_PYTHON \ - { PyThreadState *tstate = PyEval_SaveThread(); \ - if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); \ - tcl_tstate = tstate; } + if (tstate == NULL) { \ + PyGILState_Release(gilstate); \ + } \ + else { \ + tstate = PyEval_SaveThread(); \ + if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); \ + tcl_tstate = tstate; \ + } \ + } #ifndef FREECAST #define FREECAST (char *) @@ -2589,15 +2609,6 @@ typedef struct { PyObject *func; } PythonCmd_ClientData; -static int -PythonCmd_Error(Tcl_Interp *interp) -{ - errorInCmd = 1; - excInCmd = PyErr_GetRaisedException(); - LEAVE_PYTHON - return TCL_ERROR; -} - /* This is the Tcl command that acts as a wrapper for Python * function or method. */ @@ -2610,19 +2621,20 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, int i; Tcl_Obj *obj_res; int objargs = data->self->wantobjects >= 2; + int result = TCL_ERROR; ENTER_PYTHON /* Create argument tuple (objv1, ..., objvN) */ if (!(args = PyTuple_New(objc - 1))) - return PythonCmd_Error(interp); + goto error; for (i = 0; i < (objc - 1); i++) { PyObject *s = objargs ? FromObj(data->self, objv[i + 1]) : unicodeFromTclObj(data->self, objv[i + 1]); if (!s) { Py_DECREF(args); - return PythonCmd_Error(interp); + goto error; } PyTuple_SET_ITEM(args, i, s); } @@ -2631,19 +2643,25 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, Py_DECREF(args); if (res == NULL) - return PythonCmd_Error(interp); + goto error; obj_res = AsObj(res); if (obj_res == NULL) { Py_DECREF(res); - return PythonCmd_Error(interp); + goto error; } Tcl_SetObjResult(interp, obj_res); Py_DECREF(res); + result = TCL_OK; + goto done; +error: + errorInCmd = 1; + excInCmd = PyErr_GetRaisedException(); +done: LEAVE_PYTHON - return TCL_OK; + return result; }