Skip to content

Commit b75d7e2

Browse files
authored
bpo-34170: Add _PyCoreConfig._frozen parameter (pythonGH-8591)
Modify frozenmain.c to use _Py_InitializeFromConfig().
1 parent 8ed317f commit b75d7e2

File tree

7 files changed

+37
-8
lines changed

7 files changed

+37
-8
lines changed

Include/pystate.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ typedef struct {
235235
236236
See PEP 552 "Deterministic pycs" for more details. */
237237
const char *_check_hash_pycs_mode;
238+
239+
/* If greater than 0, suppress _PyPathConfig_Calculate() warnings.
240+
241+
If set to -1 (default), inherit Py_FrozenFlag value. */
242+
int _frozen;
243+
238244
} _PyCoreConfig;
239245

240246
#ifdef MS_WINDOWS
@@ -269,7 +275,8 @@ typedef struct {
269275
.user_site_directory = -1, \
270276
.unbuffered_stdio = -1, \
271277
_PyCoreConfig_WINDOWS_INIT \
272-
._install_importlib = 1}
278+
._install_importlib = 1, \
279+
._frozen = -1}
273280
/* Note: _PyCoreConfig_INIT sets other fields to 0/NULL */
274281

275282
/* Placeholders while working on the new configuration API

Lib/test/test_embed.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
289289

290290
'_install_importlib': 1,
291291
'_check_hash_pycs_mode': 'default',
292+
'_frozen': 0,
292293
}
293294

294295
def check_config(self, testname, expected):
@@ -330,6 +331,7 @@ def test_init_global_config(self):
330331
'unbuffered_stdio': 1,
331332
'utf8_mode': 1,
332333
'user_site_directory': 0,
334+
'_frozen': 1,
333335
}
334336
self.check_config("init_global_config", config)
335337

@@ -362,7 +364,9 @@ def test_init_from_config(self):
362364
'unbuffered_stdio': 1,
363365
'user_site_directory': 0,
364366
'faulthandler': 1,
367+
365368
'_check_hash_pycs_mode': 'always',
369+
'_frozen': 1,
366370
}
367371
self.check_config("init_from_config", config)
368372

Modules/getpath.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ calculate_prefix(const _PyCoreConfig *core_config,
372372
{
373373
calculate->prefix_found = search_for_prefix(core_config, calculate, prefix);
374374
if (!calculate->prefix_found) {
375-
if (!Py_FrozenFlag) {
375+
if (!core_config->_frozen) {
376376
fprintf(stderr,
377377
"Could not find platform independent libraries <prefix>\n");
378378
}
@@ -495,7 +495,7 @@ calculate_exec_prefix(const _PyCoreConfig *core_config,
495495
calculate,
496496
exec_prefix);
497497
if (!calculate->exec_prefix_found) {
498-
if (!Py_FrozenFlag) {
498+
if (!core_config->_frozen) {
499499
fprintf(stderr,
500500
"Could not find platform dependent libraries <exec_prefix>\n");
501501
}
@@ -915,7 +915,7 @@ calculate_path_impl(const _PyCoreConfig *core_config,
915915
calculate_exec_prefix(core_config, calculate, exec_prefix);
916916

917917
if ((!calculate->prefix_found || !calculate->exec_prefix_found) &&
918-
!Py_FrozenFlag)
918+
!core_config->_frozen)
919919
{
920920
fprintf(stderr,
921921
"Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");

Modules/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ _PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config)
573573
COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
574574
COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
575575
#endif
576+
COPY_FLAG(_frozen, Py_FrozenFlag);
576577

577578
COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
578579
COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
@@ -759,6 +760,7 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
759760
COPY_ATTR(legacy_windows_stdio);
760761
#endif
761762
COPY_ATTR(_check_hash_pycs_mode);
763+
COPY_ATTR(_frozen);
762764

763765
#undef COPY_ATTR
764766
#undef COPY_STR_ATTR
@@ -2281,6 +2283,9 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
22812283
if (config->utf8_mode < 0) {
22822284
config->utf8_mode = 0;
22832285
}
2286+
if (config->_frozen < 0) {
2287+
config->_frozen = 0;
2288+
}
22842289

22852290
return _Py_INIT_OK();
22862291
}

Programs/_freeze_importlib.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ main(int argc, char *argv[])
8282
/* Don't install importlib, since it could execute outdated bytecode. */
8383
config._install_importlib = 0;
8484
config.install_signal_handlers = 1;
85-
86-
Py_FrozenFlag++;
85+
config._frozen = 1;
8786

8887
_PyInitError err = _Py_InitializeFromConfig(&config);
8988
/* No need to call _PyCoreConfig_Clear() since we didn't allocate any

Programs/_testembed.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ dump_config(void)
368368

369369
printf("_install_importlib = %i\n", config->_install_importlib);
370370
printf("_check_hash_pycs_mode = %s\n", config->_check_hash_pycs_mode);
371+
printf("_frozen = %i\n", config->_frozen);
371372

372373
#undef ASSERT_EQUAL
373374
#undef ASSERT_STR_EQUAL
@@ -420,6 +421,8 @@ static int test_init_global_config(void)
420421
putenv("PYTHONUNBUFFERED=");
421422
Py_UnbufferedStdioFlag = 1;
422423

424+
Py_FrozenFlag = 1;
425+
423426
/* FIXME: test Py_LegacyWindowsFSEncodingFlag */
424427
/* FIXME: test Py_LegacyWindowsStdioFlag */
425428

@@ -525,6 +528,9 @@ static int test_init_from_config(void)
525528

526529
config._check_hash_pycs_mode = "always";
527530

531+
Py_FrozenFlag = 0;
532+
config._frozen = 1;
533+
528534
_PyInitError err = _Py_InitializeFromConfig(&config);
529535
/* Don't call _PyCoreConfig_Clear() since all strings are static */
530536
if (_Py_INIT_FAILED(err)) {

Python/frozenmain.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ Py_FrozenMain(int argc, char **argv)
4141
}
4242
}
4343

44-
Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
44+
_PyCoreConfig config = _PyCoreConfig_INIT;
45+
config._frozen = 1; /* Suppress errors from getpath.c */
4546

4647
if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
4748
inspect = 1;
@@ -80,7 +81,14 @@ Py_FrozenMain(int argc, char **argv)
8081
#endif /* MS_WINDOWS */
8182
if (argc >= 1)
8283
Py_SetProgramName(argv_copy[0]);
83-
Py_Initialize();
84+
85+
err = _Py_InitializeFromConfig(&config);
86+
/* No need to call _PyCoreConfig_Clear() since we didn't allocate any
87+
memory: program_name is a constant string. */
88+
if (_Py_INIT_FAILED(err)) {
89+
_Py_FatalInitError(err);
90+
}
91+
8492
#ifdef MS_WINDOWS
8593
PyWinFreeze_ExeInit();
8694
#endif

0 commit comments

Comments
 (0)