Skip to content

Commit f72346c

Browse files
authored
bpo-36301: Cleanup preconfig code (GH-12535)
Prepare code to move some _PyPreConfig parameters into _PyPreCmdline. Changes: * _PyCoreConfig_ReadFromArgv(): remove preconfig parameter, use _PyRuntime.preconfig. * Add _PyPreCmdline_GetPreConfig() (called by _PyPreConfig_Read()). * Rename _PyPreCmdline_Init() to _PyPreCmdline_SetArgv() * Factorize _Py_PreInitializeFromPreConfig() code: add pyinit_preinit(). * _PyPreConfig_Read() now sets coerce_c_locale to 2 if it must be coerced. * Remove _PyCoreConfig_ReadPreConfig(). * _PyCoreConfig_Write() now copies updated preconfig into _PyRuntime.
1 parent 68d228f commit f72346c

File tree

7 files changed

+152
-187
lines changed

7 files changed

+152
-187
lines changed

Include/cpython/coreconfig.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ typedef struct {
7373
set to !Py_IgnoreEnvironmentFlag. */
7474
int use_environment;
7575

76-
int coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
76+
/* PYTHONCOERCECLOCALE, -1 means unknown.
77+
78+
If it is equal to 1, LC_CTYPE locale is read to decide it it should be
79+
coerced or not (ex: PYTHONCOERCECLOCALE=1). Internally, it is set to 2
80+
if the LC_CTYPE locale must be coerced. */
81+
int coerce_c_locale;
7782
int coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */
7883

7984
#ifdef MS_WINDOWS

Include/internal/pycore_coreconfig.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ typedef struct {
2525
/* Note: _PyPreCmdline_INIT sets other fields to 0/NULL */
2626

2727
PyAPI_FUNC(void) _PyPreCmdline_Clear(_PyPreCmdline *cmdline);
28-
PyAPI_FUNC(_PyInitError) _PyPreCmdline_Init(_PyPreCmdline *cmdline,
28+
PyAPI_FUNC(_PyInitError) _PyPreCmdline_SetArgv(_PyPreCmdline *cmdline,
2929
const _PyArgv *args);
30-
PyAPI_FUNC(_PyInitError) _PyPreCmdline_Read(_PyPreCmdline *cmdline);
3130
PyAPI_FUNC(void) _PyPreCmdline_SetPreConfig(
3231
const _PyPreCmdline *cmdline,
3332
_PyPreConfig *config);
33+
PyAPI_FUNC(_PyInitError) _PyPreCmdline_Read(_PyPreCmdline *cmdline);
3434

3535

3636
/* --- _PyWstrList ------------------------------------------------ */
@@ -76,7 +76,8 @@ PyAPI_FUNC(const char*) _PyPreConfig_GetEnv(const _PyPreConfig *config,
7676
PyAPI_FUNC(void) _Py_get_env_flag(_PyPreConfig *config,
7777
int *flag,
7878
const char *name);
79-
PyAPI_FUNC(_PyInitError) _PyPreConfig_Read(_PyPreConfig *config);
79+
PyAPI_FUNC(_PyInitError) _PyPreConfig_Read(_PyPreConfig *config,
80+
const _PyArgv *args);
8081
PyAPI_FUNC(int) _PyPreConfig_AsDict(const _PyPreConfig *config,
8182
PyObject *dict);
8283
PyAPI_FUNC(_PyInitError) _PyPreConfig_ReadFromArgv(_PyPreConfig *config,
@@ -103,12 +104,10 @@ PyAPI_FUNC(int) _PyCoreConfig_GetEnvDup(
103104
wchar_t **dest,
104105
wchar_t *wname,
105106
char *name);
106-
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config,
107-
const _PyPreConfig *preconfig);
107+
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config);
108108
PyAPI_FUNC(_PyInitError) _PyCoreConfig_ReadFromArgv(_PyCoreConfig *config,
109-
const _PyArgv *args,
110-
const _PyPreConfig *preconfig);
111-
PyAPI_FUNC(void) _PyCoreConfig_Write(const _PyCoreConfig *config);
109+
const _PyArgv *args);
110+
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Write(const _PyCoreConfig *config);
112111

113112
#ifdef __cplusplus
114113
}

Modules/main.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -283,28 +283,38 @@ _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
283283
/* --- pymain_init() ---------------------------------------------- */
284284

285285
static _PyInitError
286-
pymain_init_preconfig(_PyPreConfig *config, const _PyArgv *args)
286+
pymain_init_preconfig(const _PyArgv *args)
287287
{
288-
_PyInitError err = _PyPreConfig_ReadFromArgv(config, args);
288+
_PyInitError err;
289+
290+
_PyPreConfig config = _PyPreConfig_INIT;
291+
292+
err = _PyPreConfig_ReadFromArgv(&config, args);
289293
if (_Py_INIT_FAILED(err)) {
290-
return err;
294+
goto done;
291295
}
292296

293-
return _Py_PreInitializeFromPreConfig(config);
297+
err = _Py_PreInitializeFromPreConfig(&config);
298+
299+
done:
300+
_PyPreConfig_Clear(&config);
301+
return err;
294302
}
295303

296304

297305
static _PyInitError
298306
pymain_init_coreconfig(_PyCoreConfig *config, const _PyArgv *args,
299-
const _PyPreConfig *preconfig,
300307
PyInterpreterState **interp_p)
301308
{
302-
_PyInitError err = _PyCoreConfig_ReadFromArgv(config, args, preconfig);
309+
_PyInitError err = _PyCoreConfig_ReadFromArgv(config, args);
303310
if (_Py_INIT_FAILED(err)) {
304311
return err;
305312
}
306313

307-
_PyCoreConfig_Write(config);
314+
err = _PyCoreConfig_Write(config);
315+
if (_Py_INIT_FAILED(err)) {
316+
return err;
317+
}
308318

309319
return _Py_InitializeCore(interp_p, config);
310320
}
@@ -348,18 +358,14 @@ pymain_init(const _PyArgv *args, PyInterpreterState **interp_p)
348358
fedisableexcept(FE_OVERFLOW);
349359
#endif
350360

351-
_PyPreConfig local_preconfig = _PyPreConfig_INIT;
352-
_PyPreConfig *preconfig = &local_preconfig;
353-
354-
_PyCoreConfig local_config = _PyCoreConfig_INIT;
355-
_PyCoreConfig *config = &local_config;
356-
357-
err = pymain_init_preconfig(preconfig, args);
361+
err = pymain_init_preconfig(args);
358362
if (_Py_INIT_FAILED(err)) {
359-
goto done;
363+
return err;
360364
}
361365

362-
err = pymain_init_coreconfig(config, args, preconfig, interp_p);
366+
_PyCoreConfig config = _PyCoreConfig_INIT;
367+
368+
err = pymain_init_coreconfig(&config, args, interp_p);
363369
if (_Py_INIT_FAILED(err)) {
364370
goto done;
365371
}
@@ -372,8 +378,7 @@ pymain_init(const _PyArgv *args, PyInterpreterState **interp_p)
372378
err = _Py_INIT_OK();
373379

374380
done:
375-
_PyPreConfig_Clear(preconfig);
376-
_PyCoreConfig_Clear(config);
381+
_PyCoreConfig_Clear(&config);
377382
return err;
378383
}
379384

Python/coreconfig.c

Lines changed: 21 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,54 +1330,14 @@ config_init_fs_encoding(_PyCoreConfig *config)
13301330
}
13311331

13321332

1333-
static _PyInitError
1334-
_PyCoreConfig_ReadPreConfig(_PyCoreConfig *config)
1335-
{
1336-
_PyInitError err;
1337-
_PyPreConfig local_preconfig = _PyPreConfig_INIT;
1338-
_PyPreConfig_GetGlobalConfig(&local_preconfig);
1339-
1340-
if (_PyPreConfig_Copy(&local_preconfig, &config->preconfig) < 0) {
1341-
err = _Py_INIT_NO_MEMORY();
1342-
goto done;
1343-
}
1344-
1345-
err = _PyPreConfig_Read(&local_preconfig);
1346-
if (_Py_INIT_FAILED(err)) {
1347-
goto done;
1348-
}
1349-
1350-
if (_PyPreConfig_Copy(&config->preconfig, &local_preconfig) < 0) {
1351-
err = _Py_INIT_NO_MEMORY();
1352-
goto done;
1353-
}
1354-
err = _Py_INIT_OK();
1355-
1356-
done:
1357-
_PyPreConfig_Clear(&local_preconfig);
1358-
return err;
1359-
}
1360-
1361-
1362-
static _PyInitError
1363-
_PyCoreConfig_GetPreConfig(_PyCoreConfig *config)
1364-
{
1365-
/* Read config written by _PyPreConfig_Write() */
1366-
if (_PyPreConfig_Copy(&config->preconfig, &_PyRuntime.preconfig) < 0) {
1367-
return _Py_INIT_NO_MEMORY();
1368-
}
1369-
return _Py_INIT_OK();
1370-
}
1371-
1372-
13731333
/* Read the configuration into _PyCoreConfig from:
13741334
13751335
* Environment variables
13761336
* Py_xxx global configuration variables
13771337
13781338
See _PyCoreConfig_ReadFromArgv() to parse also command line arguments. */
13791339
_PyInitError
1380-
_PyCoreConfig_Read(_PyCoreConfig *config, const _PyPreConfig *preconfig)
1340+
_PyCoreConfig_Read(_PyCoreConfig *config)
13811341
{
13821342
_PyInitError err;
13831343

@@ -1386,25 +1346,12 @@ _PyCoreConfig_Read(_PyCoreConfig *config, const _PyPreConfig *preconfig)
13861346
return err;
13871347
}
13881348

1389-
err = _PyCoreConfig_GetPreConfig(config);
1390-
if (_Py_INIT_FAILED(err)) {
1391-
return err;
1349+
if (_PyPreConfig_Copy(&config->preconfig, &_PyRuntime.preconfig) < 0) {
1350+
return _Py_INIT_NO_MEMORY();
13921351
}
13931352

13941353
_PyCoreConfig_GetGlobalConfig(config);
13951354

1396-
if (preconfig != NULL) {
1397-
if (_PyPreConfig_Copy(&config->preconfig, preconfig) < 0) {
1398-
return _Py_INIT_NO_MEMORY();
1399-
}
1400-
}
1401-
else {
1402-
err = _PyCoreConfig_ReadPreConfig(config);
1403-
if (_Py_INIT_FAILED(err)) {
1404-
return err;
1405-
}
1406-
}
1407-
14081355
assert(config->preconfig.use_environment >= 0);
14091356

14101357
if (config->preconfig.isolated > 0) {
@@ -1548,11 +1495,22 @@ config_init_stdio(const _PyCoreConfig *config)
15481495
15491496
- set Py_xxx global configuration variables
15501497
- initialize C standard streams (stdin, stdout, stderr) */
1551-
void
1498+
_PyInitError
15521499
_PyCoreConfig_Write(const _PyCoreConfig *config)
15531500
{
15541501
_PyCoreConfig_SetGlobalConfig(config);
15551502
config_init_stdio(config);
1503+
1504+
/* Write the new pre-configuration into _PyRuntime */
1505+
PyMemAllocatorEx old_alloc;
1506+
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
1507+
int res = _PyPreConfig_Copy(&_PyRuntime.preconfig, &config->preconfig);
1508+
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
1509+
if (res < 0) {
1510+
return _Py_INIT_NO_MEMORY();
1511+
}
1512+
1513+
return _Py_INIT_OK();
15561514
}
15571515

15581516

@@ -2047,8 +2005,7 @@ config_usage(int error, const wchar_t* program)
20472005

20482006
/* Parse command line options and environment variables. */
20492007
static _PyInitError
2050-
config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
2051-
const _PyPreConfig *preconfig)
2008+
config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline)
20522009
{
20532010
int need_usage = 0;
20542011
_PyInitError err;
@@ -2067,7 +2024,7 @@ config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
20672024
return err;
20682025
}
20692026

2070-
_PyPreCmdline_SetPreConfig(&cmdline->precmdline, &config->preconfig);
2027+
_PyPreCmdline_SetPreConfig(&cmdline->precmdline, &_PyRuntime.preconfig);
20712028
if (_PyWstrList_Extend(&config->xoptions, &cmdline->precmdline.xoptions) < 0) {
20722029
return _Py_INIT_NO_MEMORY();
20732030
}
@@ -2098,7 +2055,7 @@ config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
20982055
return err;
20992056
}
21002057

2101-
err = _PyCoreConfig_Read(config, preconfig);
2058+
err = _PyCoreConfig_Read(config);
21022059
if (_Py_INIT_FAILED(err)) {
21032060
return err;
21042061
}
@@ -2129,8 +2086,7 @@ config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
21292086
* Environment variables
21302087
* Py_xxx global configuration variables */
21312088
_PyInitError
2132-
_PyCoreConfig_ReadFromArgv(_PyCoreConfig *config, const _PyArgv *args,
2133-
const _PyPreConfig *preconfig)
2089+
_PyCoreConfig_ReadFromArgv(_PyCoreConfig *config, const _PyArgv *args)
21342090
{
21352091
_PyInitError err;
21362092

@@ -2141,12 +2097,12 @@ _PyCoreConfig_ReadFromArgv(_PyCoreConfig *config, const _PyArgv *args,
21412097

21422098
_PyCmdline cmdline = {.precmdline = _PyPreCmdline_INIT};
21432099

2144-
err = _PyPreCmdline_Init(&cmdline.precmdline, args);
2100+
err = _PyPreCmdline_SetArgv(&cmdline.precmdline, args);
21452101
if (_Py_INIT_FAILED(err)) {
21462102
goto done;
21472103
}
21482104

2149-
err = config_from_cmdline(config, &cmdline, preconfig);
2105+
err = config_from_cmdline(config, &cmdline);
21502106
if (_Py_INIT_FAILED(err)) {
21512107
goto done;
21522108
}

Python/pathconfig.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ pathconfig_global_init(void)
394394
_PyInitError err;
395395
_PyCoreConfig config = _PyCoreConfig_INIT;
396396

397-
err = _PyCoreConfig_Read(&config, NULL);
397+
err = _PyCoreConfig_Read(&config);
398398
if (_Py_INIT_FAILED(err)) {
399399
goto error;
400400
}

0 commit comments

Comments
 (0)