Skip to content

Commit 6438bf5

Browse files
ncoghlanericsnowcurrently
authored andcommitted
Add _PyCoreConfig and _Py_InitializeCore().
1 parent bb1c034 commit 6438bf5

File tree

5 files changed

+240
-58
lines changed

5 files changed

+240
-58
lines changed

Include/pylifecycle.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ PyAPI_FUNC(wchar_t *) Py_GetPythonHome(void);
1919
*/
2020
PyAPI_FUNC(int) Py_SetStandardStreamEncoding(const char *encoding,
2121
const char *errors);
22+
23+
/* PEP 432 Multi-phase initialization API (Private while provisional!) */
24+
PyAPI_FUNC(void) _Py_InitializeCore(const _PyCoreConfig *);
25+
PyAPI_FUNC(int) _Py_IsCoreInitialized(void);
26+
PyAPI_FUNC(int) _Py_InitializeMainInterpreter(int install_sigs);
2227
#endif
2328

29+
/* Initialization and finalization */
2430
PyAPI_FUNC(void) Py_Initialize(void);
2531
PyAPI_FUNC(void) Py_InitializeEx(int);
2632
#ifndef Py_LIMITED_API
@@ -29,6 +35,8 @@ PyAPI_FUNC(void) _Py_InitializeEx_Private(int, int);
2935
PyAPI_FUNC(void) Py_Finalize(void);
3036
PyAPI_FUNC(int) Py_FinalizeEx(void);
3137
PyAPI_FUNC(int) Py_IsInitialized(void);
38+
39+
/* Subinterpreter support */
3240
PyAPI_FUNC(PyThreadState *) Py_NewInterpreter(void);
3341
PyAPI_FUNC(void) Py_EndInterpreter(PyThreadState *);
3442

@@ -85,7 +93,7 @@ PyAPI_FUNC(void) _PyImportHooks_Init(void);
8593
PyAPI_FUNC(int) _PyFrame_Init(void);
8694
PyAPI_FUNC(int) _PyFloat_Init(void);
8795
PyAPI_FUNC(int) PyByteArray_Init(void);
88-
PyAPI_FUNC(void) _Py_HashRandomization_Init(void);
96+
PyAPI_FUNC(void) _Py_HashRandomization_Init(_PyCoreConfig *core_config);
8997
#endif
9098

9199
/* Various internal finalizers */

Include/pystate.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ typedef struct _is PyInterpreterState;
2323
#else
2424
typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);
2525

26+
27+
typedef struct {
28+
int ignore_environment;
29+
int use_hash_seed;
30+
unsigned long hash_seed;
31+
int _disable_importlib; /* Needed by freeze_importlib */
32+
} _PyCoreConfig;
33+
34+
#define _PyCoreConfig_INIT {0, -1, 0, 0}
35+
2636
typedef struct _is {
2737

2838
struct _is *next;
@@ -42,6 +52,7 @@ typedef struct _is {
4252
int codecs_initialized;
4353
int fscodec_initialized;
4454

55+
_PyCoreConfig core_config;
4556
#ifdef HAVE_DLOPEN
4657
int dlopenflags;
4758
#endif

Modules/main.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,6 @@ read_command_line(int argc, wchar_t **argv, _Py_CommandLineDetails *cmdline)
389389
exit(1);
390390
}
391391

392-
// TODO: Move these to core runtime init.
393-
Py_HashRandomizationFlag = 1;
394-
_Py_HashRandomization_Init();
395-
PySys_ResetWarnOptions();
396-
397392
_PyOS_ResetGetOpt();
398393

399394
while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
@@ -584,6 +579,7 @@ Py_Main(int argc, wchar_t **argv)
584579
#endif
585580
int stdin_is_interactive = 0;
586581
_Py_CommandLineDetails cmdline = _Py_CommandLineDetails_INIT;
582+
_PyCoreConfig core_config = _PyCoreConfig_INIT;
587583
PyCompilerFlags cf;
588584
PyObject *main_importer_path = NULL;
589585

@@ -602,11 +598,16 @@ Py_Main(int argc, wchar_t **argv)
602598
break;
603599
}
604600
if (c == 'E' || c == 'I') {
605-
Py_IgnoreEnvironmentFlag++;
601+
core_config.ignore_environment++;
606602
break;
607603
}
608604
}
609605

606+
/* Initialize the core language runtime */
607+
Py_IgnoreEnvironmentFlag = core_config.ignore_environment;
608+
core_config._disable_importlib = 0;
609+
_Py_InitializeCore(&core_config);
610+
610611
/* Reprocess the command line with the language runtime available */
611612
if (read_command_line(argc, argv, &cmdline)) {
612613
return usage(2, argv[0]);
@@ -680,6 +681,7 @@ Py_Main(int argc, wchar_t **argv)
680681
for (i = 0; i < PyList_GET_SIZE(cmdline.warning_options); i++) {
681682
PySys_AddWarnOptionUnicode(PyList_GET_ITEM(cmdline.warning_options, i));
682683
}
684+
Py_DECREF(cmdline.warning_options);
683685
}
684686

685687
stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
@@ -767,9 +769,10 @@ Py_Main(int argc, wchar_t **argv)
767769
#else
768770
Py_SetProgramName(argv[0]);
769771
#endif
770-
Py_Initialize();
771-
Py_XDECREF(cmdline.warning_options);
772+
if (_Py_InitializeMainInterpreter(1))
773+
Py_FatalError("Py_Main: Py_InitializeMainInterpreter failed");
772774

775+
/* TODO: Move this to _PyRun_PrepareMain */
773776
if (!Py_QuietFlag && (Py_VerboseFlag ||
774777
(cmdline.command == NULL && cmdline.filename == NULL &&
775778
cmdline.module == NULL && stdin_is_interactive))) {
@@ -779,6 +782,7 @@ Py_Main(int argc, wchar_t **argv)
779782
fprintf(stderr, "%s\n", COPYRIGHT);
780783
}
781784

785+
/* TODO: Move this to _Py_InitializeMainInterpreter */
782786
if (cmdline.command != NULL) {
783787
/* Backup _PyOS_optind and force sys.argv[0] = '-c' */
784788
_PyOS_optind--;

Python/bootstrap_hash.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,18 +599,20 @@ init_hash_secret(int use_hash_seed,
599599
}
600600

601601
void
602-
_Py_HashRandomization_Init(void)
602+
_Py_HashRandomization_Init(_PyCoreConfig *core_config)
603603
{
604604
char *seed_text;
605-
int use_hash_seed = -1;
606-
unsigned long hash_seed;
605+
int use_hash_seed = core_config->use_hash_seed;
606+
unsigned long hash_seed = core_config->hash_seed;
607607

608608
if (use_hash_seed < 0) {
609609
seed_text = Py_GETENV("PYTHONHASHSEED");
610610
if (Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
611611
Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer "
612612
"in range [0; 4294967295]");
613613
}
614+
core_config->use_hash_seed = use_hash_seed;
615+
core_config->hash_seed = hash_seed;
614616
}
615617
init_hash_secret(use_hash_seed, hash_seed);
616618
}

0 commit comments

Comments
 (0)