@@ -480,16 +480,6 @@ _Py_Initialize_ReconfigureCore(PyInterpreterState **interp_p,
480
480
}
481
481
* interp_p = interp ;
482
482
483
- /* bpo-34008: For backward compatibility reasons, calling Py_Main() after
484
- Py_Initialize() ignores the new configuration. */
485
- if (core_config -> preconfig .allocator != NULL ) {
486
- const char * allocator = _PyMem_GetAllocatorsName ();
487
- if (allocator == NULL || strcmp (core_config -> preconfig .allocator , allocator ) != 0 ) {
488
- return _Py_INIT_USER_ERR ("cannot modify memory allocator "
489
- "after first Py_Initialize()" );
490
- }
491
- }
492
-
493
483
_PyCoreConfig_SetGlobalConfig (core_config );
494
484
495
485
if (_PyCoreConfig_Copy (& interp -> core_config , core_config ) < 0 ) {
@@ -521,12 +511,6 @@ pycore_init_runtime(const _PyCoreConfig *core_config)
521
511
return err ;
522
512
}
523
513
524
- if (core_config -> preconfig .allocator != NULL ) {
525
- if (_PyMem_SetupAllocators (core_config -> preconfig .allocator ) < 0 ) {
526
- return _Py_INIT_USER_ERR ("Unknown PYTHONMALLOC allocator" );
527
- }
528
- }
529
-
530
514
/* Py_Finalize leaves _Py_Finalizing set in order to help daemon
531
515
* threads behave a little more gracefully at interpreter shutdown.
532
516
* We clobber it here so the new interpreter can start with a clean
@@ -728,6 +712,65 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p,
728
712
return _Py_INIT_OK ();
729
713
}
730
714
715
+
716
+ static _PyInitError
717
+ pyinit_preconfig (_PyPreConfig * preconfig , const _PyPreConfig * src_preconfig )
718
+ {
719
+ _PyInitError err ;
720
+ PyMemAllocatorEx old_alloc ;
721
+
722
+ /* Set LC_CTYPE to the user preferred locale */
723
+ _Py_SetLocaleFromEnv (LC_CTYPE );
724
+
725
+ _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
726
+ if (_PyPreConfig_Copy (preconfig , src_preconfig ) >= 0 ) {
727
+ err = _PyPreConfig_Read (preconfig );
728
+ }
729
+ else {
730
+ err = _Py_INIT_ERR ("failed to copy pre config" );
731
+ }
732
+ PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
733
+
734
+ if (_Py_INIT_FAILED (err )) {
735
+ return err ;
736
+ }
737
+
738
+ return _PyPreConfig_Write (preconfig );
739
+ }
740
+
741
+
742
+ static _PyInitError
743
+ pyinit_coreconfig (_PyCoreConfig * config , const _PyCoreConfig * src_config ,
744
+ PyInterpreterState * * interp_p )
745
+ {
746
+ PyMemAllocatorEx old_alloc ;
747
+ _PyInitError err ;
748
+
749
+ /* Set LC_CTYPE to the user preferred locale */
750
+ _Py_SetLocaleFromEnv (LC_CTYPE );
751
+
752
+ _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
753
+ if (_PyCoreConfig_Copy (config , src_config ) >= 0 ) {
754
+ err = _PyCoreConfig_Read (config , NULL );
755
+ }
756
+ else {
757
+ err = _Py_INIT_ERR ("failed to copy core config" );
758
+ }
759
+ PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
760
+
761
+ if (_Py_INIT_FAILED (err )) {
762
+ return err ;
763
+ }
764
+
765
+ if (!_PyRuntime .core_initialized ) {
766
+ return _Py_InitializeCore_impl (interp_p , config );
767
+ }
768
+ else {
769
+ return _Py_Initialize_ReconfigureCore (interp_p , config );
770
+ }
771
+ }
772
+
773
+
731
774
/* Begin interpreter initialization
732
775
*
733
776
* On return, the first thread and interpreter state have been created,
@@ -749,41 +792,23 @@ _PyInitError
749
792
_Py_InitializeCore (PyInterpreterState * * interp_p ,
750
793
const _PyCoreConfig * src_config )
751
794
{
752
- assert (src_config != NULL );
753
-
754
795
PyMemAllocatorEx old_alloc ;
755
796
_PyInitError err ;
756
797
757
- /* Copy the configuration, since _PyCoreConfig_Read() modifies it
758
- (and the input configuration is read only). */
759
- _PyCoreConfig config = _PyCoreConfig_INIT ;
760
-
761
- /* Set LC_CTYPE to the user preferred locale */
762
- _Py_SetLocaleFromEnv (LC_CTYPE );
798
+ assert (src_config != NULL );
763
799
764
- _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
765
- if (_PyCoreConfig_Copy (& config , src_config ) >= 0 ) {
766
- err = _PyCoreConfig_Read (& config , NULL );
767
- }
768
- else {
769
- err = _Py_INIT_ERR ("failed to copy core config" );
770
- }
771
- PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
800
+ _PyCoreConfig local_config = _PyCoreConfig_INIT ;
772
801
802
+ err = pyinit_preconfig (& local_config .preconfig , & src_config -> preconfig );
773
803
if (_Py_INIT_FAILED (err )) {
774
804
goto done ;
775
805
}
776
806
777
- if (!_PyRuntime .core_initialized ) {
778
- err = _Py_InitializeCore_impl (interp_p , & config );
779
- }
780
- else {
781
- err = _Py_Initialize_ReconfigureCore (interp_p , & config );
782
- }
807
+ err = pyinit_coreconfig (& local_config , src_config , interp_p );
783
808
784
809
done :
785
810
_PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
786
- _PyCoreConfig_Clear (& config );
811
+ _PyCoreConfig_Clear (& local_config );
787
812
PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
788
813
789
814
return err ;
0 commit comments