bpo-36763: Add _PyCoreConfig.parse_argv by vstinner · Pull Request #13361 · python/cpython
Expand Up
@@ -628,6 +628,7 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
COPY_WSTR_ATTR(program_name);
COPY_WSTR_ATTR(program);
COPY_ATTR(parse_argv); COPY_WSTRLIST(argv); COPY_WSTRLIST(warnoptions); COPY_WSTRLIST(xoptions); Expand Down Expand Up @@ -727,6 +728,7 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config) SET_ITEM_WSTR(filesystem_errors); SET_ITEM_WSTR(pycache_prefix); SET_ITEM_WSTR(program_name); SET_ITEM_INT(parse_argv); SET_ITEM_WSTRLIST(argv); SET_ITEM_WSTR(program); SET_ITEM_WSTRLIST(xoptions); Expand Down Expand Up @@ -1490,6 +1492,8 @@ config_read(_PyCoreConfig *config, _PyPreCmdline *cmdline) }
if (config->isolated > 0) { /* _PyPreCmdline_Read() sets use_environment to 0 if isolated is set, _PyPreCmdline_SetCoreConfig() overrides config->use_environment. */ config->user_site_directory = 0; }
Expand Down Expand Up @@ -1660,7 +1664,7 @@ config_usage(int error, const wchar_t* program) /* Parse the command line arguments */ static _PyInitError config_parse_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline, _PyWstrList *warnoptions) _PyWstrList *warnoptions, int *opt_index) { _PyInitError err; const _PyWstrList *argv = &precmdline->argv; Expand Down Expand Up @@ -1833,8 +1837,7 @@ config_parse_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline, _PyOS_optind--; }
/* -c and -m options are exclusive */ assert(!(config->run_command != NULL && config->run_module != NULL)); *opt_index = _PyOS_optind;
return _Py_INIT_OK(); } Expand Down Expand Up @@ -1978,22 +1981,23 @@ config_init_warnoptions(_PyCoreConfig *config,
static _PyInitError config_init_argv(_PyCoreConfig *config, const _PyPreCmdline *cmdline) config_update_argv(_PyCoreConfig *config, const _PyPreCmdline *cmdline, int opt_index) { const _PyWstrList *cmdline_argv = &cmdline->argv; _PyWstrList config_argv = _PyWstrList_INIT;
/* Copy argv to be able to modify it (to force -c/-m) */ if (cmdline_argv->length <= _PyOS_optind) { if (cmdline_argv->length <= opt_index) { /* Ensure at least one (empty) argument is seen */ if (_PyWstrList_Append(&config_argv, L"") < 0) { return _Py_INIT_NO_MEMORY(); } } else { _PyWstrList slice; slice.length = cmdline_argv->length - _PyOS_optind; slice.items = &cmdline_argv->items[_PyOS_optind]; slice.length = cmdline_argv->length - opt_index; slice.items = &cmdline_argv->items[opt_index]; if (_PyWstrList_Copy(&config_argv, &slice) < 0) { return _Py_INIT_NO_MEMORY(); } Expand Down Expand Up @@ -2058,14 +2062,22 @@ config_read_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline) _PyWstrList cmdline_warnoptions = _PyWstrList_INIT; _PyWstrList env_warnoptions = _PyWstrList_INIT;
err = config_parse_cmdline(config, precmdline, &cmdline_warnoptions); if (_Py_INIT_FAILED(err)) { goto done; if (config->parse_argv < 0) { config->parse_argv = 1; }
err = config_init_argv(config, precmdline); if (_Py_INIT_FAILED(err)) { goto done; if (config->parse_argv) { int opt_index; err = config_parse_cmdline(config, precmdline, &cmdline_warnoptions, &opt_index); if (_Py_INIT_FAILED(err)) { goto done; }
err = config_update_argv(config, precmdline, opt_index); if (_Py_INIT_FAILED(err)) { goto done; } }
err = config_read(config, precmdline); Expand Down Expand Up @@ -2212,6 +2224,7 @@ _PyCoreConfig_Read(_PyCoreConfig *config) assert(config->verbose >= 0); assert(config->quiet >= 0); assert(config->user_site_directory >= 0); assert(config->parse_argv >= 0); assert(config->buffered_stdio >= 0); assert(config->program_name != NULL); assert(config->program != NULL); Expand All @@ -2236,6 +2249,8 @@ _PyCoreConfig_Read(_PyCoreConfig *config) #ifdef MS_WINDOWS assert(config->legacy_windows_stdio >= 0); #endif /* -c and -m options are exclusive */ assert(!(config->run_command != NULL && config->run_module != NULL)); assert(config->check_hash_pycs_mode != NULL); assert(config->_install_importlib >= 0); assert(config->_frozen >= 0); Expand Down
COPY_ATTR(parse_argv); COPY_WSTRLIST(argv); COPY_WSTRLIST(warnoptions); COPY_WSTRLIST(xoptions); Expand Down Expand Up @@ -727,6 +728,7 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config) SET_ITEM_WSTR(filesystem_errors); SET_ITEM_WSTR(pycache_prefix); SET_ITEM_WSTR(program_name); SET_ITEM_INT(parse_argv); SET_ITEM_WSTRLIST(argv); SET_ITEM_WSTR(program); SET_ITEM_WSTRLIST(xoptions); Expand Down Expand Up @@ -1490,6 +1492,8 @@ config_read(_PyCoreConfig *config, _PyPreCmdline *cmdline) }
if (config->isolated > 0) { /* _PyPreCmdline_Read() sets use_environment to 0 if isolated is set, _PyPreCmdline_SetCoreConfig() overrides config->use_environment. */ config->user_site_directory = 0; }
Expand Down Expand Up @@ -1660,7 +1664,7 @@ config_usage(int error, const wchar_t* program) /* Parse the command line arguments */ static _PyInitError config_parse_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline, _PyWstrList *warnoptions) _PyWstrList *warnoptions, int *opt_index) { _PyInitError err; const _PyWstrList *argv = &precmdline->argv; Expand Down Expand Up @@ -1833,8 +1837,7 @@ config_parse_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline, _PyOS_optind--; }
/* -c and -m options are exclusive */ assert(!(config->run_command != NULL && config->run_module != NULL)); *opt_index = _PyOS_optind;
return _Py_INIT_OK(); } Expand Down Expand Up @@ -1978,22 +1981,23 @@ config_init_warnoptions(_PyCoreConfig *config,
static _PyInitError config_init_argv(_PyCoreConfig *config, const _PyPreCmdline *cmdline) config_update_argv(_PyCoreConfig *config, const _PyPreCmdline *cmdline, int opt_index) { const _PyWstrList *cmdline_argv = &cmdline->argv; _PyWstrList config_argv = _PyWstrList_INIT;
/* Copy argv to be able to modify it (to force -c/-m) */ if (cmdline_argv->length <= _PyOS_optind) { if (cmdline_argv->length <= opt_index) { /* Ensure at least one (empty) argument is seen */ if (_PyWstrList_Append(&config_argv, L"") < 0) { return _Py_INIT_NO_MEMORY(); } } else { _PyWstrList slice; slice.length = cmdline_argv->length - _PyOS_optind; slice.items = &cmdline_argv->items[_PyOS_optind]; slice.length = cmdline_argv->length - opt_index; slice.items = &cmdline_argv->items[opt_index]; if (_PyWstrList_Copy(&config_argv, &slice) < 0) { return _Py_INIT_NO_MEMORY(); } Expand Down Expand Up @@ -2058,14 +2062,22 @@ config_read_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline) _PyWstrList cmdline_warnoptions = _PyWstrList_INIT; _PyWstrList env_warnoptions = _PyWstrList_INIT;
err = config_parse_cmdline(config, precmdline, &cmdline_warnoptions); if (_Py_INIT_FAILED(err)) { goto done; if (config->parse_argv < 0) { config->parse_argv = 1; }
err = config_init_argv(config, precmdline); if (_Py_INIT_FAILED(err)) { goto done; if (config->parse_argv) { int opt_index; err = config_parse_cmdline(config, precmdline, &cmdline_warnoptions, &opt_index); if (_Py_INIT_FAILED(err)) { goto done; }
err = config_update_argv(config, precmdline, opt_index); if (_Py_INIT_FAILED(err)) { goto done; } }
err = config_read(config, precmdline); Expand Down Expand Up @@ -2212,6 +2224,7 @@ _PyCoreConfig_Read(_PyCoreConfig *config) assert(config->verbose >= 0); assert(config->quiet >= 0); assert(config->user_site_directory >= 0); assert(config->parse_argv >= 0); assert(config->buffered_stdio >= 0); assert(config->program_name != NULL); assert(config->program != NULL); Expand All @@ -2236,6 +2249,8 @@ _PyCoreConfig_Read(_PyCoreConfig *config) #ifdef MS_WINDOWS assert(config->legacy_windows_stdio >= 0); #endif /* -c and -m options are exclusive */ assert(!(config->run_command != NULL && config->run_module != NULL)); assert(config->check_hash_pycs_mode != NULL); assert(config->_install_importlib >= 0); assert(config->_frozen >= 0); Expand Down