@@ -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_sysmodule.h" // _PySys_GetOptionalAttrString()
3536#include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8String
3637
@@ -407,6 +408,7 @@ typedef struct {
407408 int threaded ; /* True if tcl_platform[threaded] */
408409 Tcl_ThreadId thread_id ;
409410 int dispatching ;
411+ int quitting ; /* Set by quit() to stop mainloop() */
410412 int event_hook ; /* True if counted by EnableEventHook() */
411413 PyObject * trace ;
412414 /* We cannot include tclInt.h, as this is internal.
@@ -449,9 +451,14 @@ check_tcl_appartment(TkappObject *app)
449451/**** Error Handling ****/
450452
451453static PyObject * Tkinter_TclError ;
452- static int quitMainLoop = 0 ;
453- static int errorInCmd = 0 ;
454- static PyObject * excInCmd ;
454+
455+ /* An exception raised in a Python callback, which should be raised by the
456+ event loop (mainloop() or the event hook) that called the callback.
457+ Callbacks are only called in the thread of their Tcl interpreter, so it
458+ is thread-local. */
459+ static Tcl_ThreadDataKey excInCmd_key ;
460+ #define excInCmd \
461+ (*(PyObject **)Tcl_GetThreadData(&excInCmd_key, sizeof(PyObject *)))
455462
456463
457464static PyObject * Tkapp_UnicodeResult (TkappObject * );
@@ -2521,8 +2528,7 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp,
25212528 goto done ;
25222529
25232530error :
2524- errorInCmd = 1 ;
2525- excInCmd = PyErr_GetRaisedException ();
2531+ Py_XSETREF (excInCmd , PyErr_GetRaisedException ());
25262532done :
25272533 LEAVE_PYTHON
25282534
@@ -2760,8 +2766,7 @@ FileHandler(ClientData clientData, int mask)
27602766
27612767 res = PyObject_CallFunction (func , "Oi" , file , mask );
27622768 if (res == NULL ) {
2763- errorInCmd = 1 ;
2764- excInCmd = PyErr_GetRaisedException ();
2769+ Py_XSETREF (excInCmd , PyErr_GetRaisedException ());
27652770 }
27662771 Py_XDECREF (res );
27672772 LEAVE_PYTHON
@@ -2939,8 +2944,7 @@ TimerHandler(ClientData clientData)
29392944 Py_DECREF (v ); /* See Tktt_New() */
29402945
29412946 if (res == NULL ) {
2942- errorInCmd = 1 ;
2943- excInCmd = PyErr_GetRaisedException ();
2947+ Py_XSETREF (excInCmd , PyErr_GetRaisedException ());
29442948 }
29452949 else
29462950 Py_DECREF (res );
@@ -3005,10 +3009,10 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
30053009 int dispatching = self -> dispatching ;
30063010 self -> dispatching = 1 ;
30073011
3008- quitMainLoop = 0 ;
3012+ FT_ATOMIC_STORE_INT_RELAXED ( self -> quitting , 0 ) ;
30093013 while (Tk_GetNumMainWindows () > threshold &&
3010- !quitMainLoop &&
3011- ! errorInCmd )
3014+ !FT_ATOMIC_LOAD_INT_RELAXED ( self -> quitting ) &&
3015+ excInCmd == NULL )
30123016 {
30133017 int result ;
30143018
@@ -3038,12 +3042,12 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
30383042 break ;
30393043 }
30403044 self -> dispatching = dispatching ;
3041- quitMainLoop = 0 ;
3045+ FT_ATOMIC_STORE_INT_RELAXED ( self -> quitting , 0 ) ;
30423046
3043- if (errorInCmd ) {
3044- errorInCmd = 0 ;
3045- PyErr_SetRaisedException (excInCmd );
3047+ PyObject * exc = excInCmd ;
3048+ if (exc != NULL ) {
30463049 excInCmd = NULL ;
3050+ PyErr_SetRaisedException (exc );
30473051 return NULL ;
30483052 }
30493053 Py_RETURN_NONE ;
@@ -3069,6 +3073,13 @@ _tkinter_tkapp_dooneevent_impl(TkappObject *self, int flags)
30693073 return PyLong_FromLong (rv );
30703074}
30713075
3076+ static int
3077+ QuitEventProc (Tcl_Event * evPtr , int flags )
3078+ {
3079+ /* Nothing to do: the event only wakes up the event loop. */
3080+ return 1 ;
3081+ }
3082+
30723083/*[clinic input]
30733084_tkinter.tkapp.quit
30743085[clinic start generated code]*/
@@ -3077,7 +3088,21 @@ static PyObject *
30773088_tkinter_tkapp_quit_impl (TkappObject * self )
30783089/*[clinic end generated code: output=7f21eeff481f754f input=e03020dc38aff23c]*/
30793090{
3080- quitMainLoop = 1 ;
3091+ FT_ATOMIC_STORE_INT_RELAXED (self -> quitting , 1 );
3092+ if (self -> threaded && self -> dispatching &&
3093+ self -> thread_id != Tcl_GetCurrentThread ())
3094+ {
3095+ /* mainloop() is running in other thread and can wait for an event.
3096+ Send it an event, so that it checks the flag. */
3097+ Tcl_Event * ev = (Tcl_Event * )attemptckalloc (sizeof (Tcl_Event ));
3098+ if (ev == NULL ) {
3099+ return PyErr_NoMemory ();
3100+ }
3101+ memset (ev , 0 , sizeof (Tcl_Event ));
3102+ ev -> proc = QuitEventProc ;
3103+ Tcl_ThreadQueueEvent (self -> thread_id , ev , TCL_QUEUE_TAIL );
3104+ Tcl_ThreadAlert (self -> thread_id );
3105+ }
30813106 Py_RETURN_NONE ;
30823107}
30833108
@@ -3551,7 +3576,6 @@ EventHook(void)
35513576 stdin_ready = 0 ;
35523577#endif
35533578 PyEval_RestoreThread (tstate );
3554- errorInCmd = 0 ;
35553579#ifdef MS_WINDOWS
35563580 hStdin = GetStdHandle (STD_INPUT_HANDLE );
35573581 type = GetFileType (hStdin );
@@ -3609,10 +3633,10 @@ EventHook(void)
36093633 instead of returning to the prompt: without readline there is no
36103634 input waiting on stdin yet, so returning here would block in fgets
36113635 until the user hits enter, freezing later callbacks. */
3612- if (errorInCmd ) {
3613- errorInCmd = 0 ;
3614- PyErr_SetRaisedException (excInCmd );
3636+ PyObject * exc = excInCmd ;
3637+ if (exc != NULL ) {
36153638 excInCmd = NULL ;
3639+ PyErr_SetRaisedException (exc );
36163640 PyErr_Print ();
36173641 }
36183642 if (result < 0 )
0 commit comments