@@ -1050,6 +1050,30 @@ _PyInterpreterState_IsRunningMain(PyInterpreterState *interp)
10501050 return 0 ;
10511051}
10521052
1053+ #ifndef NDEBUG
1054+ static int
1055+ is_running_main (PyThreadState * tstate )
1056+ {
1057+ if (tstate -> interp -> threads .main != NULL ) {
1058+ return tstate == tstate -> interp -> threads .main ;
1059+ }
1060+ return 0 ;
1061+ }
1062+ #endif
1063+
1064+ int
1065+ _PyThreadState_IsRunningMain (PyThreadState * tstate )
1066+ {
1067+ PyInterpreterState * interp = tstate -> interp ;
1068+ if (interp -> threads .main != NULL ) {
1069+ return tstate == interp -> threads .main ;
1070+ }
1071+ if (_Py_IsMainInterpreter (interp )) {
1072+ return tstate -> thread_id == interp -> runtime -> main_thread ;
1073+ }
1074+ return 0 ;
1075+ }
1076+
10531077int
10541078_PyInterpreterState_FailIfRunningMain (PyInterpreterState * interp )
10551079{
@@ -1061,6 +1085,15 @@ _PyInterpreterState_FailIfRunningMain(PyInterpreterState *interp)
10611085 return 0 ;
10621086}
10631087
1088+ void
1089+ _PyInterpreterState_ReinitRunningMain (PyThreadState * tstate )
1090+ {
1091+ PyInterpreterState * interp = tstate -> interp ;
1092+ if (interp -> threads .main != tstate ) {
1093+ interp -> threads .main = NULL ;
1094+ }
1095+ }
1096+
10641097
10651098//----------
10661099// accessors
@@ -1543,6 +1576,7 @@ PyThreadState_Clear(PyThreadState *tstate)
15431576{
15441577 assert (tstate -> _status .initialized && !tstate -> _status .cleared );
15451578 assert (current_fast_get ()-> interp == tstate -> interp );
1579+ assert (!is_running_main (tstate ));
15461580 // XXX assert(!tstate->_status.bound || tstate->_status.unbound);
15471581 tstate -> _status .finalizing = 1 ; // just in case
15481582
@@ -1641,6 +1675,7 @@ tstate_delete_common(PyThreadState *tstate)
16411675 assert (tstate -> _status .cleared && !tstate -> _status .finalized );
16421676 assert (tstate -> state != _Py_THREAD_ATTACHED );
16431677 tstate_verify_not_active (tstate );
1678+ assert (!is_running_main (tstate ));
16441679
16451680 PyInterpreterState * interp = tstate -> interp ;
16461681 if (interp == NULL ) {
0 commit comments