Skip to content
Merged
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions Lib/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import signal
import sys
import textwrap
import threading
import unittest
import weakref
import tkinter
Expand Down Expand Up @@ -597,6 +598,31 @@ def callback(event):
user32.SendMessageW(hwnd, WM_EXITSIZEMOVE, 0, 0)
self.assertTrue(sizes)

def test_nested_mainloop(self):
# gh-162650: Calls from other thread are still dispatched after
# a nested mainloop() exits.
root = self.root
results = []
def call():
try:
results.append(root.tk.call('expr', '6*7'))
except RuntimeError as e:
results.append(e)
def start():
root.after(0, root.quit)
root.mainloop() # nested
thread = threading.Thread(target=call)
thread.start()
def wait():
if thread.is_alive():
root.after(10, wait)
else:
root.quit()
wait()
root.after(0, start)
root.mainloop()
self.assertEqual(results, [42])

def test_wait_variable(self):
var = tkinter.StringVar(self.root)
self.assertEqual(self.root.waitvar, self.root.wait_variable)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :mod:`tkinter` calls from other threads failing with :exc:`RuntimeError`
after a nested :meth:`!mainloop` exited, for example after
:meth:`!SimpleDialog.go` returned.
7 changes: 5 additions & 2 deletions Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3005,6 +3005,9 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
PyThreadState *tstate = PyThreadState_Get();

CHECK_TCL_APPARTMENT(self);
/* mainloop() can be nested, the outer mainloop() still dispatches
events after the inner one exits. */
int dispatching = self->dispatching;
self->dispatching = 1;

quitMainLoop = 0;
Expand Down Expand Up @@ -3033,13 +3036,13 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
}

if (PyErr_CheckSignals() != 0) {
self->dispatching = 0;
self->dispatching = dispatching;
return NULL;
}
if (result < 0)
break;
}
self->dispatching = 0;
self->dispatching = dispatching;
quitMainLoop = 0;

if (errorInCmd) {
Expand Down
Loading