@@ -380,19 +380,6 @@ read_command_line(int argc, wchar_t **argv, _Py_CommandLineDetails *cmdline)
380380 wchar_t * command = NULL ;
381381 wchar_t * module = NULL ;
382382 int c ;
383- char * opt ;
384-
385- opt = Py_GETENV ("PYTHONMALLOC" );
386- if (_PyMem_SetupAllocators (opt ) < 0 ) {
387- fprintf (stderr ,
388- "Error in PYTHONMALLOC: unknown allocator \"%s\"!\n" , opt );
389- exit (1 );
390- }
391-
392- // TODO: Move these to core runtime init.
393- Py_HashRandomizationFlag = 1 ;
394- _Py_HashRandomization_Init ();
395- PySys_ResetWarnOptions ();
396383
397384 _PyOS_ResetGetOpt ();
398385
@@ -584,6 +571,7 @@ Py_Main(int argc, wchar_t **argv)
584571#endif
585572 int stdin_is_interactive = 0 ;
586573 _Py_CommandLineDetails cmdline = _Py_CommandLineDetails_INIT ;
574+ _PyCoreConfig core_config = _PyCoreConfig_INIT ;
587575 PyCompilerFlags cf ;
588576 PyObject * main_importer_path = NULL ;
589577
@@ -602,11 +590,23 @@ Py_Main(int argc, wchar_t **argv)
602590 break ;
603591 }
604592 if (c == 'E' || c == 'I' ) {
605- Py_IgnoreEnvironmentFlag ++ ;
593+ core_config . ignore_environment ++ ;
606594 break ;
607595 }
608596 }
609597
598+ char * pymalloc = Py_GETENV ("PYTHONMALLOC" );
599+ if (_PyMem_SetupAllocators (pymalloc ) < 0 ) {
600+ fprintf (stderr ,
601+ "Error in PYTHONMALLOC: unknown allocator \"%s\"!\n" , pymalloc );
602+ exit (1 );
603+ }
604+
605+ /* Initialize the core language runtime */
606+ Py_IgnoreEnvironmentFlag = core_config .ignore_environment ;
607+ core_config ._disable_importlib = 0 ;
608+ _Py_InitializeCore (& core_config );
609+
610610 /* Reprocess the command line with the language runtime available */
611611 if (read_command_line (argc , argv , & cmdline )) {
612612 return usage (2 , argv [0 ]);
@@ -680,6 +680,7 @@ Py_Main(int argc, wchar_t **argv)
680680 for (i = 0 ; i < PyList_GET_SIZE (cmdline .warning_options ); i ++ ) {
681681 PySys_AddWarnOptionUnicode (PyList_GET_ITEM (cmdline .warning_options , i ));
682682 }
683+ Py_DECREF (cmdline .warning_options );
683684 }
684685
685686 stdin_is_interactive = Py_FdIsInteractive (stdin , (char * )0 );
@@ -767,9 +768,10 @@ Py_Main(int argc, wchar_t **argv)
767768#else
768769 Py_SetProgramName (argv [0 ]);
769770#endif
770- Py_Initialize ();
771- Py_XDECREF ( cmdline . warning_options );
771+ if ( _Py_InitializeMainInterpreter ( 1 ))
772+ Py_FatalError ( "Py_Main: Py_InitializeMainInterpreter failed" );
772773
774+ /* TODO: Move this to _PyRun_PrepareMain */
773775 if (!Py_QuietFlag && (Py_VerboseFlag ||
774776 (cmdline .command == NULL && cmdline .filename == NULL &&
775777 cmdline .module == NULL && stdin_is_interactive ))) {
@@ -779,6 +781,7 @@ Py_Main(int argc, wchar_t **argv)
779781 fprintf (stderr , "%s\n" , COPYRIGHT );
780782 }
781783
784+ /* TODO: Move this to _Py_InitializeMainInterpreter */
782785 if (cmdline .command != NULL ) {
783786 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
784787 _PyOS_optind -- ;
0 commit comments