@@ -31,6 +31,7 @@ Copyright (C) 1994 Steen Lumholt.
3131#endif
3232
3333#include "pycore_long.h" // _PyLong_IsNegative()
34+ #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_INT_RELAXED()
3435#include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8String
3536
3637#include <signal.h> // SIGINT
@@ -382,6 +383,7 @@ typedef struct {
382383 int threaded ; /* True if tcl_platform[threaded] */
383384 Tcl_ThreadId thread_id ;
384385 int dispatching ;
386+ int quitting ; /* Set by quit() to stop mainloop() */
385387 int event_hook ; /* True if counted by EnableEventHook() */
386388 PyObject * trace ;
387389 /* We cannot include tclInt.h, as this is internal.
@@ -433,9 +435,14 @@ check_tcl_appartment(TkappObject *app)
433435/**** Error Handling ****/
434436
435437static PyObject * Tkinter_TclError ;
436- static int quitMainLoop = 0 ;
437- static int errorInCmd = 0 ;
438- static PyObject * excInCmd ;
438+
439+ /* An exception raised in a Python callback, which should be raised by the
440+ event loop (mainloop() or the event hook) that called the callback.
441+ Callbacks are only called in the thread of their Tcl interpreter, so it
442+ is thread-local. */
443+ static Tcl_ThreadDataKey excInCmd_key ;
444+ #define excInCmd \
445+ (*(PyObject **)Tcl_GetThreadData(&excInCmd_key, sizeof(PyObject *)))
439446
440447
441448static PyObject * Tkapp_UnicodeResult (TkappObject * );
@@ -2664,8 +2671,7 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp,
26642671 goto done ;
26652672
26662673error :
2667- errorInCmd = 1 ;
2668- excInCmd = PyErr_GetRaisedException ();
2674+ Py_XSETREF (excInCmd , PyErr_GetRaisedException ());
26692675done :
26702676 LEAVE_PYTHON
26712677
@@ -2903,8 +2909,7 @@ FileHandler(ClientData clientData, int mask)
29032909
29042910 res = PyObject_CallFunction (func , "Oi" , file , mask );
29052911 if (res == NULL ) {
2906- errorInCmd = 1 ;
2907- excInCmd = PyErr_GetRaisedException ();
2912+ Py_XSETREF (excInCmd , PyErr_GetRaisedException ());
29082913 }
29092914 Py_XDECREF (res );
29102915 LEAVE_PYTHON
@@ -3108,8 +3113,7 @@ TimerHandler(ClientData clientData)
31083113 Py_DECREF (v ); /* See Tktt_New() */
31093114
31103115 if (res == NULL ) {
3111- errorInCmd = 1 ;
3112- excInCmd = PyErr_GetRaisedException ();
3116+ Py_XSETREF (excInCmd , PyErr_GetRaisedException ());
31133117 }
31143118 else
31153119 Py_DECREF (res );
@@ -3174,10 +3178,10 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
31743178 int dispatching = self -> dispatching ;
31753179 self -> dispatching = 1 ;
31763180
3177- quitMainLoop = 0 ;
3181+ FT_ATOMIC_STORE_INT_RELAXED ( self -> quitting , 0 ) ;
31783182 while (Tk_GetNumMainWindows () > threshold &&
3179- !quitMainLoop &&
3180- ! errorInCmd )
3183+ !FT_ATOMIC_LOAD_INT_RELAXED ( self -> quitting ) &&
3184+ excInCmd == NULL )
31813185 {
31823186 int result ;
31833187
@@ -3207,12 +3211,12 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
32073211 break ;
32083212 }
32093213 self -> dispatching = dispatching ;
3210- quitMainLoop = 0 ;
3214+ FT_ATOMIC_STORE_INT_RELAXED ( self -> quitting , 0 ) ;
32113215
3212- if (errorInCmd ) {
3213- errorInCmd = 0 ;
3214- PyErr_SetRaisedException (excInCmd );
3216+ PyObject * exc = excInCmd ;
3217+ if (exc != NULL ) {
32153218 excInCmd = NULL ;
3219+ PyErr_SetRaisedException (exc );
32163220 return NULL ;
32173221 }
32183222 Py_RETURN_NONE ;
@@ -3238,6 +3242,13 @@ _tkinter_tkapp_dooneevent_impl(TkappObject *self, int flags)
32383242 return PyLong_FromLong (rv );
32393243}
32403244
3245+ static int
3246+ QuitEventProc (Tcl_Event * evPtr , int flags )
3247+ {
3248+ /* Nothing to do: the event only wakes up the event loop. */
3249+ return 1 ;
3250+ }
3251+
32413252/*[clinic input]
32423253_tkinter.tkapp.quit
32433254[clinic start generated code]*/
@@ -3246,7 +3257,21 @@ static PyObject *
32463257_tkinter_tkapp_quit_impl (TkappObject * self )
32473258/*[clinic end generated code: output=7f21eeff481f754f input=e03020dc38aff23c]*/
32483259{
3249- quitMainLoop = 1 ;
3260+ FT_ATOMIC_STORE_INT_RELAXED (self -> quitting , 1 );
3261+ if (self -> threaded && self -> dispatching &&
3262+ self -> thread_id != Tcl_GetCurrentThread ())
3263+ {
3264+ /* mainloop() is running in other thread and can wait for an event.
3265+ Send it an event, so that it checks the flag. */
3266+ Tcl_Event * ev = (Tcl_Event * )attemptckalloc (sizeof (Tcl_Event ));
3267+ if (ev == NULL ) {
3268+ return PyErr_NoMemory ();
3269+ }
3270+ memset (ev , 0 , sizeof (Tcl_Event ));
3271+ ev -> proc = QuitEventProc ;
3272+ Tcl_ThreadQueueEvent (self -> thread_id , ev , TCL_QUEUE_TAIL );
3273+ Tcl_ThreadAlert (self -> thread_id );
3274+ }
32503275 Py_RETURN_NONE ;
32513276}
32523277
@@ -3774,7 +3799,6 @@ EventHook(void)
37743799 stdin_ready = 0 ;
37753800#endif
37763801 PyEval_RestoreThread (tstate );
3777- errorInCmd = 0 ;
37783802#ifdef MS_WINDOWS
37793803 hStdin = GetStdHandle (STD_INPUT_HANDLE );
37803804 type = GetFileType (hStdin );
@@ -3832,10 +3856,10 @@ EventHook(void)
38323856 instead of returning to the prompt: without readline there is no
38333857 input waiting on stdin yet, so returning here would block in fgets
38343858 until the user hits enter, freezing later callbacks. */
3835- if (errorInCmd ) {
3836- errorInCmd = 0 ;
3837- PyErr_SetRaisedException (excInCmd );
3859+ PyObject * exc = excInCmd ;
3860+ if (exc != NULL ) {
38383861 excInCmd = NULL ;
3862+ PyErr_SetRaisedException (exc );
38393863 PyErr_Print ();
38403864 }
38413865 if (result < 0 )
0 commit comments