@@ -576,44 +576,33 @@ init_interp_settings(PyInterpreterState *interp,
576576 interp -> feature_flags |= Py_RTFLAGS_MULTI_INTERP_EXTENSIONS ;
577577 }
578578
579- /* We check "gil" in init_interp_create_gil(). */
579+ switch (config -> gil ) {
580+ case PyInterpreterConfig_DEFAULT_GIL : break ;
581+ case PyInterpreterConfig_SHARED_GIL : break ;
582+ case PyInterpreterConfig_OWN_GIL : break ;
583+ default :
584+ return _PyStatus_ERR ("invalid interpreter config 'gil' value" );
585+ }
580586
581587 return _PyStatus_OK ();
582588}
583589
584590
585- static PyStatus
591+ static void
586592init_interp_create_gil (PyThreadState * tstate , int gil )
587593{
588- PyStatus status ;
589-
590594 /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
591595 only called here. */
592596 // XXX This is broken with a per-interpreter GIL.
593597 _PyEval_FiniGIL (tstate -> interp );
594598
595599 /* Auto-thread-state API */
596- status = _PyGILState_SetTstate (tstate );
597- if (_PyStatus_EXCEPTION (status )) {
598- return status ;
599- }
600+ _PyGILState_SetTstate (tstate );
600601
601- int own_gil ;
602- switch (gil ) {
603- case PyInterpreterConfig_DEFAULT_GIL : own_gil = 0 ; break ;
604- case PyInterpreterConfig_SHARED_GIL : own_gil = 0 ; break ;
605- case PyInterpreterConfig_OWN_GIL : own_gil = 1 ; break ;
606- default :
607- return _PyStatus_ERR ("invalid interpreter config 'gil' value" );
608- }
602+ int own_gil = (gil == PyInterpreterConfig_OWN_GIL );
609603
610604 /* Create the GIL and take it */
611- status = _PyEval_InitGIL (tstate , own_gil );
612- if (_PyStatus_EXCEPTION (status )) {
613- return status ;
614- }
615-
616- return _PyStatus_OK ();
605+ _PyEval_InitGIL (tstate , own_gil );
617606}
618607
619608
@@ -657,10 +646,7 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
657646 }
658647 _PyThreadState_Bind (tstate );
659648
660- status = init_interp_create_gil (tstate , config .gil );
661- if (_PyStatus_EXCEPTION (status )) {
662- return status ;
663- }
649+ init_interp_create_gil (tstate , config .gil );
664650
665651 * tstate_p = tstate ;
666652 return _PyStatus_OK ();
@@ -2099,28 +2085,21 @@ new_interpreter(PyThreadState **tstate_p, const PyInterpreterConfig *config)
20992085 return _PyStatus_OK ();
21002086 }
21012087
2102- PyThreadState * tstate = _PyThreadState_New (interp ,
2103- _PyThreadState_WHENCE_INTERP );
2104- if (tstate == NULL ) {
2105- PyInterpreterState_Delete (interp );
2106- * tstate_p = NULL ;
2107- return _PyStatus_OK ();
2108- }
2109- _PyThreadState_Bind (tstate );
2110-
2088+ // XXX Might new_interpreter() have been called without the GIL held?
21112089 PyThreadState * save_tstate = _PyThreadState_GET ();
2112- int has_gil = 0 ;
2090+ PyThreadState * tstate = NULL ;
21132091
21142092 /* From this point until the init_interp_create_gil() call,
21152093 we must not do anything that requires that the GIL be held
21162094 (or otherwise exist). That applies whether or not the new
21172095 interpreter has its own GIL (e.g. the main interpreter). */
2096+ if (save_tstate != NULL ) {
2097+ _PyThreadState_Detach (save_tstate );
2098+ }
21182099
21192100 /* Copy the current interpreter config into the new interpreter */
21202101 const PyConfig * src_config ;
21212102 if (save_tstate != NULL ) {
2122- // XXX Might new_interpreter() have been called without the GIL held?
2123- _PyThreadState_Detach (save_tstate );
21242103 src_config = _PyInterpreterState_GetConfig (save_tstate -> interp );
21252104 }
21262105 else
@@ -2142,11 +2121,14 @@ new_interpreter(PyThreadState **tstate_p, const PyInterpreterConfig *config)
21422121 goto error ;
21432122 }
21442123
2145- status = init_interp_create_gil (tstate , config -> gil );
2146- if (_PyStatus_EXCEPTION (status )) {
2124+ tstate = _PyThreadState_New (interp , _PyThreadState_WHENCE_INTERP );
2125+ if (tstate == NULL ) {
2126+ status = _PyStatus_NO_MEMORY ();
21472127 goto error ;
21482128 }
2149- has_gil = 1 ;
2129+
2130+ _PyThreadState_Bind (tstate );
2131+ init_interp_create_gil (tstate , config -> gil );
21502132
21512133 /* No objects have been created yet. */
21522134
@@ -2165,16 +2147,14 @@ new_interpreter(PyThreadState **tstate_p, const PyInterpreterConfig *config)
21652147
21662148error :
21672149 * tstate_p = NULL ;
2168-
2169- /* Oops, it didn't work. Undo it all. */
2170- if (has_gil ) {
2150+ if (tstate != NULL ) {
2151+ PyThreadState_Clear (tstate );
21712152 _PyThreadState_Detach (tstate );
2153+ PyThreadState_Delete (tstate );
21722154 }
21732155 if (save_tstate != NULL ) {
21742156 _PyThreadState_Attach (save_tstate );
21752157 }
2176- PyThreadState_Clear (tstate );
2177- PyThreadState_Delete (tstate );
21782158 PyInterpreterState_Delete (interp );
21792159
21802160 return status ;
0 commit comments