bpo-43687: Py_Initialize() creates singletons earlier (GH-25147) · python/cpython@442ad74 (original) (raw)
`@@ -628,61 +628,75 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
`
628
628
``
629
629
``
630
630
`static PyStatus
`
631
``
`-
pycore_init_types(PyInterpreterState *interp)
`
``
631
`+
pycore_init_singletons(PyInterpreterState *interp)
`
632
632
`{
`
633
633
`PyStatus status;
`
634
``
`-
int is_main_interp = _Py_IsMainInterpreter(interp);
`
635
634
``
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);
`
637
649
`if (_PyStatus_EXCEPTION(status)) {
`
638
650
`return status;
`
639
651
` }
`
640
652
``
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.
`
644
653
`status = _PyTuple_Init(interp);
`
645
654
`if (_PyStatus_EXCEPTION(status)) {
`
646
655
`return status;
`
647
656
` }
`
648
657
``
``
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
+
649
668
`if (is_main_interp) {
`
``
669
`+
if (_PyStructSequence_Init() < 0) {
`
``
670
`+
return _PyStatus_ERR("can't initialize structseq");
`
``
671
`+
}
`
``
672
+
650
673
`status = _PyTypes_Init();
`
651
674
`if (_PyStatus_EXCEPTION(status)) {
`
652
675
`return status;
`
653
676
` }
`
654
``
`-
}
`
655
677
``
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
`+
}
`
659
681
``
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
`+
}
`
663
686
` }
`
664
687
``
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
`+
}
`
668
692
` }
`
669
693
``
670
694
`status = _PyExc_Init(interp);
`
671
695
`if (_PyStatus_EXCEPTION(status)) {
`
672
696
`return status;
`
673
697
` }
`
674
698
``
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();
`
686
700
`if (_PyStatus_EXCEPTION(status)) {
`
687
701
`return status;
`
688
702
` }
`
`@@ -693,22 +707,15 @@ pycore_init_types(PyInterpreterState *interp)
`
693
707
` }
`
694
708
` }
`
695
709
``
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
``
-
705
710
`return _PyStatus_OK();
`
706
711
`}
`
707
712
``
708
713
``
709
714
`static PyStatus
`
710
``
`-
pycore_init_builtins(PyInterpreterState *interp)
`
``
715
`+
pycore_init_builtins(PyThreadState *tstate)
`
711
716
`{
`
``
717
`+
PyInterpreterState *interp = tstate->interp;
`
``
718
+
712
719
`PyObject *bimod = _PyBuiltin_Init(interp);
`
713
720
`if (bimod == NULL) {
`
714
721
` goto error;
`
`@@ -744,6 +751,7 @@ pycore_init_builtins(PyInterpreterState *interp)
`
744
751
` }
`
745
752
`interp->import_func = Py_NewRef(import_func);
`
746
753
``
``
754
`+
assert(!_PyErr_Occurred(tstate));
`
747
755
`return _PyStatus_OK();
`
748
756
``
749
757
`error:
`
`@@ -755,29 +763,49 @@ pycore_init_builtins(PyInterpreterState *interp)
`
755
763
`static PyStatus
`
756
764
`pycore_interp_init(PyThreadState *tstate)
`
757
765
`{
`
``
766
`+
PyInterpreterState *interp = tstate->interp;
`
758
767
`PyStatus status;
`
759
768
`PyObject *sysmod = NULL;
`
760
769
``
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);
`
762
774
`if (_PyStatus_EXCEPTION(status)) {
`
763
``
`-
goto done;
`
``
775
`+
return status;
`
764
776
` }
`
765
777
``
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);
`
767
785
`if (_PyStatus_EXCEPTION(status)) {
`
768
786
` goto done;
`
769
787
` }
`
770
788
``
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
`+
}
`
772
797
``
773
``
`-
status = pycore_init_builtins(tstate->interp);
`
``
798
`+
status = _PySys_Create(tstate, &sysmod);
`
774
799
`if (_PyStatus_EXCEPTION(status)) {
`
775
800
` goto done;
`
776
801
` }
`
777
802
``
778
``
`-
assert(!_PyErr_Occurred(tstate));
`
``
803
`+
status = pycore_init_builtins(tstate);
`
``
804
`+
if (_PyStatus_EXCEPTION(status)) {
`
``
805
`+
goto done;
`
``
806
`+
}
`
779
807
``
780
``
`-
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
`
``
808
`+
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
`
781
809
`if (config->_install_importlib) {
`
782
810
`/* This call sets up builtin and frozen import support */
`
783
811
`if (init_importlib(tstate, sysmod) < 0) {
`