@@ -426,13 +426,11 @@ init_runtime(_PyRuntimeState *runtime,
426
426
Py_ssize_t unicode_next_index ,
427
427
PyThread_type_lock locks [NUMLOCKS ])
428
428
{
429
- if (runtime -> _initialized ) {
430
- Py_FatalError ("runtime already initialized" );
431
- }
432
- assert (!runtime -> preinitializing &&
433
- !runtime -> preinitialized &&
434
- !runtime -> core_initialized &&
435
- !runtime -> initialized );
429
+ assert (!runtime -> preinitializing );
430
+ assert (!runtime -> preinitialized );
431
+ assert (!runtime -> core_initialized );
432
+ assert (!runtime -> initialized );
433
+ assert (!runtime -> _initialized );
436
434
437
435
runtime -> open_code_hook = open_code_hook ;
438
436
runtime -> open_code_userdata = open_code_userdata ;
@@ -476,6 +474,7 @@ _PyRuntimeState_Init(_PyRuntimeState *runtime)
476
474
// Py_Initialize() must be running again.
477
475
// Reset to _PyRuntimeState_INIT.
478
476
memcpy (runtime , & initial , sizeof (* runtime ));
477
+ assert (!runtime -> _initialized );
479
478
}
480
479
481
480
if (gilstate_tss_init (runtime ) != 0 ) {
@@ -647,14 +646,14 @@ free_interpreter(PyInterpreterState *interp)
647
646
main interpreter. We fix those fields here, in addition
648
647
to the other dynamically initialized fields.
649
648
*/
650
- static void
649
+ static PyStatus
651
650
init_interpreter (PyInterpreterState * interp ,
652
651
_PyRuntimeState * runtime , int64_t id ,
653
652
PyInterpreterState * next ,
654
653
PyThread_type_lock pending_lock )
655
654
{
656
655
if (interp -> _initialized ) {
657
- Py_FatalError ("interpreter already initialized" );
656
+ return _PyStatus_ERR ("interpreter already initialized" );
658
657
}
659
658
660
659
assert (runtime != NULL );
@@ -675,7 +674,10 @@ init_interpreter(PyInterpreterState *interp,
675
674
memcpy (& interp -> obmalloc .pools .used , temp , sizeof (temp ));
676
675
}
677
676
678
- _PyObject_InitState (interp );
677
+ PyStatus status = _PyObject_InitState (interp );
678
+ if (_PyStatus_EXCEPTION (status )) {
679
+ return status ;
680
+ }
679
681
680
682
_PyEval_InitState (interp , pending_lock );
681
683
_PyGC_InitState (& interp -> gc );
@@ -701,42 +703,44 @@ init_interpreter(PyInterpreterState *interp,
701
703
}
702
704
interp -> f_opcode_trace_set = false;
703
705
interp -> _initialized = 1 ;
706
+ return _PyStatus_OK ();
704
707
}
705
708
706
- PyInterpreterState *
707
- PyInterpreterState_New (void )
709
+
710
+ PyStatus
711
+ _PyInterpreterState_New (PyThreadState * tstate , PyInterpreterState * * pinterp )
708
712
{
709
- PyInterpreterState * interp ;
713
+ * pinterp = NULL ;
714
+
715
+ // Don't get runtime from tstate since tstate can be NULL
710
716
_PyRuntimeState * runtime = & _PyRuntime ;
711
- PyThreadState * tstate = current_fast_get (runtime );
712
717
713
- /* tstate is NULL when Py_InitializeFromConfig() calls
714
- PyInterpreterState_New() to create the main interpreter. */
715
- if (_PySys_Audit (tstate , "cpython.PyInterpreterState_New" , NULL ) < 0 ) {
716
- return NULL ;
718
+ // tstate is NULL when pycore_create_interpreter() calls
719
+ // _PyInterpreterState_New() to create the main interpreter.
720
+ if (tstate != NULL ) {
721
+ if (_PySys_Audit (tstate , "cpython.PyInterpreterState_New" , NULL ) < 0 ) {
722
+ return _PyStatus_ERR ("sys.audit failed" );
723
+ }
717
724
}
718
725
719
726
PyThread_type_lock pending_lock = PyThread_allocate_lock ();
720
727
if (pending_lock == NULL ) {
721
- if (tstate != NULL ) {
722
- _PyErr_NoMemory (tstate );
723
- }
724
- return NULL ;
728
+ return _PyStatus_NO_MEMORY ();
725
729
}
726
730
727
- /* Don't get runtime from tstate since tstate can be NULL. */
728
- struct pyinterpreters * interpreters = & runtime -> interpreters ;
729
-
730
731
/* We completely serialize creation of multiple interpreters, since
731
732
it simplifies things here and blocking concurrent calls isn't a problem.
732
733
Regardless, we must fully block subinterpreter creation until
733
734
after the main interpreter is created. */
734
735
HEAD_LOCK (runtime );
735
736
737
+ struct pyinterpreters * interpreters = & runtime -> interpreters ;
736
738
int64_t id = interpreters -> next_id ;
737
739
interpreters -> next_id += 1 ;
738
740
739
741
// Allocate the interpreter and add it to the runtime state.
742
+ PyInterpreterState * interp ;
743
+ PyStatus status ;
740
744
PyInterpreterState * old_head = interpreters -> head ;
741
745
if (old_head == NULL ) {
742
746
// We are creating the main interpreter.
@@ -755,36 +759,59 @@ PyInterpreterState_New(void)
755
759
756
760
interp = alloc_interpreter ();
757
761
if (interp == NULL ) {
762
+ status = _PyStatus_NO_MEMORY ();
758
763
goto error ;
759
764
}
760
765
// Set to _PyInterpreterState_INIT.
761
- memcpy (interp , & initial ._main_interpreter ,
762
- sizeof (* interp ));
766
+ memcpy (interp , & initial ._main_interpreter , sizeof (* interp ));
763
767
764
768
if (id < 0 ) {
765
769
/* overflow or Py_Initialize() not called yet! */
766
- if (tstate != NULL ) {
767
- _PyErr_SetString (tstate , PyExc_RuntimeError ,
768
- "failed to get an interpreter ID" );
769
- }
770
+ status = _PyStatus_ERR ("failed to get an interpreter ID" );
770
771
goto error ;
771
772
}
772
773
}
773
774
interpreters -> head = interp ;
774
775
775
- init_interpreter (interp , runtime , id , old_head , pending_lock );
776
+ status = init_interpreter (interp , runtime ,
777
+ id , old_head , pending_lock );
778
+ if (_PyStatus_EXCEPTION (status )) {
779
+ goto error ;
780
+ }
781
+ pending_lock = NULL ;
776
782
777
783
HEAD_UNLOCK (runtime );
778
- return interp ;
784
+
785
+ assert (interp != NULL );
786
+ * pinterp = interp ;
787
+ return _PyStatus_OK ();
779
788
780
789
error :
781
790
HEAD_UNLOCK (runtime );
782
791
783
- PyThread_free_lock (pending_lock );
792
+ if (pending_lock != NULL ) {
793
+ PyThread_free_lock (pending_lock );
794
+ }
784
795
if (interp != NULL ) {
785
796
free_interpreter (interp );
786
797
}
787
- return NULL ;
798
+ return status ;
799
+ }
800
+
801
+
802
+ PyInterpreterState *
803
+ PyInterpreterState_New (void )
804
+ {
805
+ // tstate can be NULL
806
+ PyThreadState * tstate = current_fast_get (& _PyRuntime );
807
+
808
+ PyInterpreterState * interp ;
809
+ PyStatus status = _PyInterpreterState_New (tstate , & interp );
810
+ if (_PyStatus_EXCEPTION (status )) {
811
+ Py_ExitStatusException (status );
812
+ }
813
+ assert (interp != NULL );
814
+ return interp ;
788
815
}
789
816
790
817
0 commit comments