@@ -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.
@@ -424,9 +426,14 @@ check_tcl_appartment(TkappObject *app)
424426/**** Error Handling ****/
425427
426428static PyObject * Tkinter_TclError ;
427- static int quitMainLoop = 0 ;
428- static int errorInCmd = 0 ;
429- static PyObject * excInCmd ;
429+
430+ /* An exception raised in a Python callback, which should be raised by the
431+ event loop (mainloop() or the event hook) that called the callback.
432+ Callbacks are only called in the thread of their Tcl interpreter, so it
433+ is thread-local. */
434+ static Tcl_ThreadDataKey excInCmd_key ;
435+ #define excInCmd \
436+ (*(PyObject **)Tcl_GetThreadData(&excInCmd_key, sizeof(PyObject *)))
430437
431438
432439static PyObject * Tkapp_UnicodeResult (TkappObject * );
@@ -2526,8 +2533,7 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp,
25262533 goto done ;
25272534
25282535error :
2529- errorInCmd = 1 ;
2530- excInCmd = PyErr_GetRaisedException ();
2536+ Py_XSETREF (excInCmd , PyErr_GetRaisedException ());
25312537done :
25322538 LEAVE_PYTHON
25332539
@@ -2765,8 +2771,7 @@ FileHandler(ClientData clientData, int mask)
27652771
27662772 res = PyObject_CallFunction (func , "Oi" , file , mask );
27672773 if (res == NULL ) {
2768- errorInCmd = 1 ;
2769- excInCmd = PyErr_GetRaisedException ();
2774+ Py_XSETREF (excInCmd , PyErr_GetRaisedException ());
27702775 }
27712776 Py_XDECREF (res );
27722777 LEAVE_PYTHON
@@ -2944,8 +2949,7 @@ TimerHandler(ClientData clientData)
29442949 Py_DECREF (v ); /* See Tktt_New() */
29452950
29462951 if (res == NULL ) {
2947- errorInCmd = 1 ;
2948- excInCmd = PyErr_GetRaisedException ();
2952+ Py_XSETREF (excInCmd , PyErr_GetRaisedException ());
29492953 }
29502954 else
29512955 Py_DECREF (res );
@@ -3010,10 +3014,10 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
30103014 int dispatching = self -> dispatching ;
30113015 self -> dispatching = 1 ;
30123016
3013- quitMainLoop = 0 ;
3017+ FT_ATOMIC_STORE_INT_RELAXED ( self -> quitting , 0 ) ;
30143018 while (Tk_GetNumMainWindows () > threshold &&
3015- !quitMainLoop &&
3016- ! errorInCmd )
3019+ !FT_ATOMIC_LOAD_INT_RELAXED ( self -> quitting ) &&
3020+ excInCmd == NULL )
30173021 {
30183022 int result ;
30193023
@@ -3043,12 +3047,12 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
30433047 break ;
30443048 }
30453049 self -> dispatching = dispatching ;
3046- quitMainLoop = 0 ;
3050+ FT_ATOMIC_STORE_INT_RELAXED ( self -> quitting , 0 ) ;
30473051
3048- if (errorInCmd ) {
3049- errorInCmd = 0 ;
3050- PyErr_SetRaisedException (excInCmd );
3052+ PyObject * exc = excInCmd ;
3053+ if (exc != NULL ) {
30513054 excInCmd = NULL ;
3055+ PyErr_SetRaisedException (exc );
30523056 return NULL ;
30533057 }
30543058 Py_RETURN_NONE ;
@@ -3074,6 +3078,13 @@ _tkinter_tkapp_dooneevent_impl(TkappObject *self, int flags)
30743078 return PyLong_FromLong (rv );
30753079}
30763080
3081+ static int
3082+ QuitEventProc (Tcl_Event * evPtr , int flags )
3083+ {
3084+ /* Nothing to do: the event only wakes up the event loop. */
3085+ return 1 ;
3086+ }
3087+
30773088/*[clinic input]
30783089_tkinter.tkapp.quit
30793090[clinic start generated code]*/
@@ -3082,7 +3093,21 @@ static PyObject *
30823093_tkinter_tkapp_quit_impl (TkappObject * self )
30833094/*[clinic end generated code: output=7f21eeff481f754f input=e03020dc38aff23c]*/
30843095{
3085- quitMainLoop = 1 ;
3096+ FT_ATOMIC_STORE_INT_RELAXED (self -> quitting , 1 );
3097+ if (self -> threaded && self -> dispatching &&
3098+ self -> thread_id != Tcl_GetCurrentThread ())
3099+ {
3100+ /* mainloop() is running in other thread and can wait for an event.
3101+ Send it an event, so that it checks the flag. */
3102+ Tcl_Event * ev = (Tcl_Event * )attemptckalloc (sizeof (Tcl_Event ));
3103+ if (ev == NULL ) {
3104+ return PyErr_NoMemory ();
3105+ }
3106+ memset (ev , 0 , sizeof (Tcl_Event ));
3107+ ev -> proc = QuitEventProc ;
3108+ Tcl_ThreadQueueEvent (self -> thread_id , ev , TCL_QUEUE_TAIL );
3109+ Tcl_ThreadAlert (self -> thread_id );
3110+ }
30863111 Py_RETURN_NONE ;
30873112}
30883113
@@ -3579,7 +3604,6 @@ EventHook(void)
35793604 stdin_ready = 0 ;
35803605#endif
35813606 PyEval_RestoreThread (tstate );
3582- errorInCmd = 0 ;
35833607#ifdef MS_WINDOWS
35843608 hStdin = GetStdHandle (STD_INPUT_HANDLE );
35853609 type = GetFileType (hStdin );
@@ -3637,10 +3661,10 @@ EventHook(void)
36373661 instead of returning to the prompt: without readline there is no
36383662 input waiting on stdin yet, so returning here would block in fgets
36393663 until the user hits enter, freezing later callbacks. */
3640- if (errorInCmd ) {
3641- errorInCmd = 0 ;
3642- PyErr_SetRaisedException (excInCmd );
3664+ PyObject * exc = excInCmd ;
3665+ if (exc != NULL ) {
36433666 excInCmd = NULL ;
3667+ PyErr_SetRaisedException (exc );
36443668 PyErr_Print ();
36453669 }
36463670 if (result < 0 )
0 commit comments