diff --git a/Misc/NEWS.d/next/Library/2020-09-29-17-13-58.bpo-40075.pQAxXp.rst b/Misc/NEWS.d/next/Library/2020-09-29-17-13-58.bpo-40075.pQAxXp.rst new file mode 100644 index 000000000000000..20255395518f125 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-09-29-17-13-58.bpo-40075.pQAxXp.rst @@ -0,0 +1 @@ +Fix a crash in ``_tkinter`` on Windows where ``ENTER_PYTHON`` may try to restore a NULL state. diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 793c5e715488463..6bb7ac3bf7a0c5d 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -261,6 +261,10 @@ static PyThreadState *tcl_tstate = NULL; #define ENTER_PYTHON \ { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \ + if(tstate == NULL) { \ + PyGILState_STATE gstate = PyGILState_Ensure(); \ + tstate = PyThreadState_Get(); \ + PyGILState_Release(gstate); } \ if(tcl_lock) \ PyThread_release_lock(tcl_lock); PyEval_RestoreThread((tstate)); }