Skip to content

Commit 870b035

Browse files
authored
bpo-36763: Cleanup precmdline in _PyCoreConfig_Read() (GH-13371)
precmdline is only needed in _PyCoreConfig_Read(), not in config_read_cmdline(). Reorganize _PyCoreConfig_Read().
1 parent aee19f5 commit 870b035

4 files changed

Lines changed: 54 additions & 47 deletions

File tree

‎Include/internal/pycore_getopt.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ typedef struct {
1717
int val;
1818
} _PyOS_LongOption;
1919

20-
extern int _PyOS_GetOpt(Py_ssize_t argc, wchar_t **argv, int *longindex);
20+
extern int _PyOS_GetOpt(Py_ssize_t argc, wchar_t * const *argv, int *longindex);
2121

2222
#endif /* !Py_INTERNAL_PYGETOPT_H */

‎Python/coreconfig.c‎

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,21 +1486,11 @@ config_init_fs_encoding(_PyCoreConfig *config, const _PyPreConfig *preconfig)
14861486

14871487

14881488
static _PyInitError
1489-
config_read(_PyCoreConfig *config, _PyPreCmdline *cmdline)
1489+
config_read(_PyCoreConfig *config)
14901490
{
14911491
_PyInitError err;
14921492
const _PyPreConfig *preconfig = &_PyRuntime.preconfig;
14931493

1494-
if (_PyPreCmdline_SetCoreConfig(cmdline, config) < 0) {
1495-
return _Py_INIT_NO_MEMORY();
1496-
}
1497-
1498-
if (config->isolated > 0) {
1499-
/* _PyPreCmdline_Read() sets use_environment to 0 if isolated is set,
1500-
_PyPreCmdline_SetCoreConfig() overrides config->use_environment. */
1501-
config->user_site_directory = 0;
1502-
}
1503-
15041494
if (config->use_environment) {
15051495
err = config_read_env_vars(config);
15061496
if (_Py_INIT_FAILED(err)) {
@@ -1584,6 +1574,19 @@ config_read(_PyCoreConfig *config, _PyPreCmdline *cmdline)
15841574
return _Py_INIT_NO_MEMORY();
15851575
}
15861576
}
1577+
1578+
if (config->check_hash_pycs_mode == NULL) {
1579+
err = _PyCoreConfig_SetString(&config->check_hash_pycs_mode,
1580+
L"default");
1581+
if (_Py_INIT_FAILED(err)) {
1582+
return err;
1583+
}
1584+
}
1585+
1586+
if (config->configure_c_stdio < 0) {
1587+
config->configure_c_stdio = 1;
1588+
}
1589+
15871590
return _Py_INIT_OK();
15881591
}
15891592

@@ -1669,11 +1672,11 @@ config_usage(int error, const wchar_t* program)
16691672

16701673
/* Parse the command line arguments */
16711674
static _PyInitError
1672-
config_parse_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline,
1673-
_PyWstrList *warnoptions, int *opt_index)
1675+
config_parse_cmdline(_PyCoreConfig *config, _PyWstrList *warnoptions,
1676+
int *opt_index)
16741677
{
16751678
_PyInitError err;
1676-
const _PyWstrList *argv = &precmdline->argv;
1679+
const _PyWstrList *argv = &config->argv;
16771680
int print_version = 0;
16781681

16791682
_PyOS_ResetGetOpt();
@@ -1890,9 +1893,9 @@ config_init_env_warnoptions(const _PyCoreConfig *config, _PyWstrList *warnoption
18901893

18911894

18921895
static _PyInitError
1893-
config_init_program(_PyCoreConfig *config, const _PyPreCmdline *cmdline)
1896+
config_init_program(_PyCoreConfig *config)
18941897
{
1895-
const _PyWstrList *argv = &cmdline->argv;
1898+
const _PyWstrList *argv = &config->argv;
18961899
wchar_t *program;
18971900
if (argv->length >= 1) {
18981901
program = argv->items[0];
@@ -1987,10 +1990,9 @@ config_init_warnoptions(_PyCoreConfig *config,
19871990

19881991

19891992
static _PyInitError
1990-
config_update_argv(_PyCoreConfig *config, const _PyPreCmdline *cmdline,
1991-
int opt_index)
1993+
config_update_argv(_PyCoreConfig *config, int opt_index)
19921994
{
1993-
const _PyWstrList *cmdline_argv = &cmdline->argv;
1995+
const _PyWstrList *cmdline_argv = &config->argv;
19941996
_PyWstrList config_argv = _PyWstrList_INIT;
19951997

19961998
/* Copy argv to be able to modify it (to force -c/-m) */
@@ -2054,6 +2056,16 @@ core_read_precmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline)
20542056
_PyCoreConfig_GetCoreConfig(&preconfig, config);
20552057

20562058
err = _PyPreCmdline_Read(precmdline, &preconfig);
2059+
if (_Py_INIT_FAILED(err)) {
2060+
goto done;
2061+
}
2062+
2063+
if (_PyPreCmdline_SetCoreConfig(precmdline, config) < 0) {
2064+
err = _Py_INIT_NO_MEMORY();
2065+
goto done;
2066+
}
2067+
2068+
err = _Py_INIT_OK();
20572069

20582070
done:
20592071
_PyPreConfig_Clear(&preconfig);
@@ -2062,7 +2074,7 @@ core_read_precmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline)
20622074

20632075

20642076
static _PyInitError
2065-
config_read_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline)
2077+
config_read_cmdline(_PyCoreConfig *config)
20662078
{
20672079
_PyInitError err;
20682080
_PyWstrList cmdline_warnoptions = _PyWstrList_INIT;
@@ -2071,29 +2083,27 @@ config_read_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline)
20712083
if (config->parse_argv < 0) {
20722084
config->parse_argv = 1;
20732085
}
2074-
if (config->configure_c_stdio < 0) {
2075-
config->configure_c_stdio = 1;
2086+
2087+
if (config->program == NULL) {
2088+
err = config_init_program(config);
2089+
if (_Py_INIT_FAILED(err)) {
2090+
goto done;
2091+
}
20762092
}
20772093

20782094
if (config->parse_argv) {
20792095
int opt_index;
2080-
err = config_parse_cmdline(config, precmdline, &cmdline_warnoptions,
2081-
&opt_index);
2096+
err = config_parse_cmdline(config, &cmdline_warnoptions, &opt_index);
20822097
if (_Py_INIT_FAILED(err)) {
20832098
goto done;
20842099
}
20852100

2086-
err = config_update_argv(config, precmdline, opt_index);
2101+
err = config_update_argv(config, opt_index);
20872102
if (_Py_INIT_FAILED(err)) {
20882103
goto done;
20892104
}
20902105
}
20912106

2092-
err = config_read(config, precmdline);
2093-
if (_Py_INIT_FAILED(err)) {
2094-
goto done;
2095-
}
2096-
20972107
if (config->use_environment) {
20982108
err = config_init_env_warnoptions(config, &env_warnoptions);
20992109
if (_Py_INIT_FAILED(err)) {
@@ -2107,13 +2117,6 @@ config_read_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline)
21072117
goto done;
21082118
}
21092119

2110-
if (config->check_hash_pycs_mode == NULL) {
2111-
err = _PyCoreConfig_SetString(&config->check_hash_pycs_mode, L"default");
2112-
if (_Py_INIT_FAILED(err)) {
2113-
goto done;
2114-
}
2115-
}
2116-
21172120
err = _Py_INIT_OK();
21182121

21192122
done:
@@ -2199,14 +2202,18 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
21992202
goto done;
22002203
}
22012204

2202-
if (config->program == NULL) {
2203-
err = config_init_program(config, &precmdline);
2204-
if (_Py_INIT_FAILED(err)) {
2205-
goto done;
2206-
}
2205+
assert(config->isolated >= 0);
2206+
if (config->isolated) {
2207+
config->use_environment = 0;
2208+
config->user_site_directory = 0;
2209+
}
2210+
2211+
err = config_read_cmdline(config);
2212+
if (_Py_INIT_FAILED(err)) {
2213+
goto done;
22072214
}
22082215

2209-
err = config_read_cmdline(config, &precmdline);
2216+
err = config_read(config);
22102217
if (_Py_INIT_FAILED(err)) {
22112218
goto done;
22122219
}

‎Python/getopt.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int _PyOS_opterr = 1; /* generate error messages */
4141
Py_ssize_t _PyOS_optind = 1; /* index into argv array */
4242
const wchar_t *_PyOS_optarg = NULL; /* optional argument */
4343

44-
static wchar_t *opt_ptr = L"";
44+
static const wchar_t *opt_ptr = L"";
4545

4646
/* Python command line short and long options */
4747

@@ -61,7 +61,7 @@ void _PyOS_ResetGetOpt(void)
6161
opt_ptr = L"";
6262
}
6363

64-
int _PyOS_GetOpt(Py_ssize_t argc, wchar_t **argv, int *longindex)
64+
int _PyOS_GetOpt(Py_ssize_t argc, wchar_t * const *argv, int *longindex)
6565
{
6666
wchar_t *ptr;
6767
wchar_t option;

‎Python/preconfig.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ _PyPreCmdline_SetCoreConfig(const _PyPreCmdline *cmdline, _PyCoreConfig *config)
174174
static _PyInitError
175175
precmdline_parse_cmdline(_PyPreCmdline *cmdline)
176176
{
177-
_PyWstrList *argv = &cmdline->argv;
177+
const _PyWstrList *argv = &cmdline->argv;
178178

179179
_PyOS_ResetGetOpt();
180180
/* Don't log parsing errors into stderr here: _PyCoreConfig_Read()

0 commit comments

Comments
 (0)