@@ -628,61 +628,75 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
628628
629629
630630static PyStatus
631- pycore_init_types (PyInterpreterState * interp )
631+ pycore_init_singletons (PyInterpreterState * interp )
632632{
633633 PyStatus status ;
634- int is_main_interp = _Py_IsMainInterpreter (interp );
635634
636- status = _PyGC_Init (interp );
635+ if (_PyLong_Init (interp ) < 0 ) {
636+ return _PyStatus_ERR ("can't init longs" );
637+ }
638+
639+ if (_Py_IsMainInterpreter (interp )) {
640+ _PyFloat_Init ();
641+ }
642+
643+ status = _PyBytes_Init (interp );
644+ if (_PyStatus_EXCEPTION (status )) {
645+ return status ;
646+ }
647+
648+ status = _PyUnicode_Init (interp );
637649 if (_PyStatus_EXCEPTION (status )) {
638650 return status ;
639651 }
640652
641- // Create the empty tuple singleton. It must be created before the first
642- // PyType_Ready() call since PyType_Ready() creates tuples, for tp_bases
643- // for example.
644653 status = _PyTuple_Init (interp );
645654 if (_PyStatus_EXCEPTION (status )) {
646655 return status ;
647656 }
648657
658+ return _PyStatus_OK ();
659+ }
660+
661+
662+ static PyStatus
663+ pycore_init_types (PyInterpreterState * interp )
664+ {
665+ PyStatus status ;
666+ int is_main_interp = _Py_IsMainInterpreter (interp );
667+
649668 if (is_main_interp ) {
669+ if (_PyStructSequence_Init () < 0 ) {
670+ return _PyStatus_ERR ("can't initialize structseq" );
671+ }
672+
650673 status = _PyTypes_Init ();
651674 if (_PyStatus_EXCEPTION (status )) {
652675 return status ;
653676 }
654- }
655677
656- if (! _PyLong_Init ( interp ) ) {
657- return _PyStatus_ERR ("can't init longs " );
658- }
678+ if (_PyLong_InitTypes () < 0 ) {
679+ return _PyStatus_ERR ("can't init int type " );
680+ }
659681
660- status = _PyUnicode_Init (interp );
661- if (_PyStatus_EXCEPTION (status )) {
662- return status ;
682+ status = _PyUnicode_InitTypes ();
683+ if (_PyStatus_EXCEPTION (status )) {
684+ return status ;
685+ }
663686 }
664687
665- status = _PyBytes_Init (interp );
666- if (_PyStatus_EXCEPTION (status )) {
667- return status ;
688+ if (is_main_interp ) {
689+ if (_PyFloat_InitTypes () < 0 ) {
690+ return _PyStatus_ERR ("can't init float" );
691+ }
668692 }
669693
670694 status = _PyExc_Init (interp );
671695 if (_PyStatus_EXCEPTION (status )) {
672696 return status ;
673697 }
674698
675- if (is_main_interp ) {
676- if (!_PyFloat_Init ()) {
677- return _PyStatus_ERR ("can't init float" );
678- }
679-
680- if (_PyStructSequence_Init () < 0 ) {
681- return _PyStatus_ERR ("can't initialize structseq" );
682- }
683- }
684-
685- status = _PyErr_Init ();
699+ status = _PyErr_InitTypes ();
686700 if (_PyStatus_EXCEPTION (status )) {
687701 return status ;
688702 }
@@ -693,22 +707,15 @@ pycore_init_types(PyInterpreterState *interp)
693707 }
694708 }
695709
696- if (_PyWarnings_InitState (interp ) < 0 ) {
697- return _PyStatus_ERR ("can't initialize warnings" );
698- }
699-
700- status = _PyAtExit_Init (interp );
701- if (_PyStatus_EXCEPTION (status )) {
702- return status ;
703- }
704-
705710 return _PyStatus_OK ();
706711}
707712
708713
709714static PyStatus
710- pycore_init_builtins (PyInterpreterState * interp )
715+ pycore_init_builtins (PyThreadState * tstate )
711716{
717+ PyInterpreterState * interp = tstate -> interp ;
718+
712719 PyObject * bimod = _PyBuiltin_Init (interp );
713720 if (bimod == NULL ) {
714721 goto error ;
@@ -744,6 +751,7 @@ pycore_init_builtins(PyInterpreterState *interp)
744751 }
745752 interp -> import_func = Py_NewRef (import_func );
746753
754+ assert (!_PyErr_Occurred (tstate ));
747755 return _PyStatus_OK ();
748756
749757error :
@@ -755,29 +763,49 @@ pycore_init_builtins(PyInterpreterState *interp)
755763static PyStatus
756764pycore_interp_init (PyThreadState * tstate )
757765{
766+ PyInterpreterState * interp = tstate -> interp ;
758767 PyStatus status ;
759768 PyObject * sysmod = NULL ;
760769
761- status = pycore_init_types (tstate -> interp );
770+ // Create singletons before the first PyType_Ready() call, since
771+ // PyType_Ready() uses singletons like the Unicode empty string (tp_doc)
772+ // and the empty tuple singletons (tp_bases).
773+ status = pycore_init_singletons (interp );
762774 if (_PyStatus_EXCEPTION (status )) {
763- goto done ;
775+ return status ;
764776 }
765777
766- status = _PySys_Create (tstate , & sysmod );
778+ // The GC must be initialized before the first GC collection.
779+ status = _PyGC_Init (interp );
780+ if (_PyStatus_EXCEPTION (status )) {
781+ return status ;
782+ }
783+
784+ status = pycore_init_types (interp );
767785 if (_PyStatus_EXCEPTION (status )) {
768786 goto done ;
769787 }
770788
771- assert (!_PyErr_Occurred (tstate ));
789+ if (_PyWarnings_InitState (interp ) < 0 ) {
790+ return _PyStatus_ERR ("can't initialize warnings" );
791+ }
792+
793+ status = _PyAtExit_Init (interp );
794+ if (_PyStatus_EXCEPTION (status )) {
795+ return status ;
796+ }
772797
773- status = pycore_init_builtins (tstate -> interp );
798+ status = _PySys_Create (tstate , & sysmod );
774799 if (_PyStatus_EXCEPTION (status )) {
775800 goto done ;
776801 }
777802
778- assert (!_PyErr_Occurred (tstate ));
803+ status = pycore_init_builtins (tstate );
804+ if (_PyStatus_EXCEPTION (status )) {
805+ goto done ;
806+ }
779807
780- const PyConfig * config = _PyInterpreterState_GetConfig (tstate -> interp );
808+ const PyConfig * config = _PyInterpreterState_GetConfig (interp );
781809 if (config -> _install_importlib ) {
782810 /* This call sets up builtin and frozen import support */
783811 if (init_importlib (tstate , sysmod ) < 0 ) {
0 commit comments