diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-03-29 14:13:46 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-29 14:13:46 (GMT) |
commit | 2f54908afc5665937d763510b4430f10cf764641 (patch) | |
tree | 8ceaf77d86f93ec2908129e03a4ad872d6e20c22 /Python | |
parent | 5f45979b63300338b68709bfe817ddae563b93fd (diff) | |
download | cpython-2f54908afc5665937d763510b4430f10cf764641.zip cpython-2f54908afc5665937d763510b4430f10cf764641.tar.gz cpython-2f54908afc5665937d763510b4430f10cf764641.tar.bz2 |
bpo-36471: Add _Py_RunMain() (GH-12618)
* Add config_read_cmdline() subfunction. Remove _PyCmdline structure.
* _PyCoreConfig_Read() now also parses config->argv command line
arguments
Diffstat (limited to 'Python')
-rw-r--r-- | Python/coreconfig.c | 187 |
1 files changed, 89 insertions, 98 deletions
diff --git a/Python/coreconfig.c b/Python/coreconfig.c index 13aa227..7b55b06 100644 --- a/Python/coreconfig.c +++ b/Python/coreconfig.c @@ -1618,33 +1618,34 @@ _PyCoreConfig_Write(const _PyCoreConfig *config) } -/* --- _PyCmdline ------------------------------------------------- */ - -typedef struct { - _PyWstrList cmdline_warnoptions; /* Command line -W options */ - _PyWstrList env_warnoptions; /* PYTHONWARNINGS environment variables */ - int print_help; /* -h, -? options */ - int print_version; /* -V option */ - int need_usage; -} _PyCmdline; - +/* --- _PyCoreConfig command line parser -------------------------- */ static void -cmdline_clear(_PyCmdline *cmdline) +config_usage(int error, const wchar_t* program) { - _PyWstrList_Clear(&cmdline->cmdline_warnoptions); - _PyWstrList_Clear(&cmdline->env_warnoptions); -} + FILE *f = error ? stderr : stdout; + fprintf(f, usage_line, program); + if (error) + fprintf(f, "Try `python -h' for more information.\n"); + else { + fputs(usage_1, f); + fputs(usage_2, f); + fputs(usage_3, f); + fprintf(f, usage_4, (wint_t)DELIM); + fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP); + fputs(usage_6, f); + } +} -/* --- _PyCoreConfig command line parser -------------------------- */ /* Parse the command line arguments */ static _PyInitError -config_parse_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline, - _PyPreCmdline *precmdline) +config_parse_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline, + _PyWstrList *warnoptions) { const _PyWstrList *argv = &precmdline->argv; + int print_version = 0; _PyOS_ResetGetOpt(); do { @@ -1698,8 +1699,8 @@ config_parse_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline, } else { fprintf(stderr, "--check-hash-based-pycs must be one of " "'default', 'always', or 'never'\n"); - cmdline->need_usage = 1; - return _Py_INIT_OK(); + config_usage(1, config->program); + return _Py_INIT_EXIT(2); } break; @@ -1758,15 +1759,15 @@ config_parse_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline, case 'h': case '?': - cmdline->print_help++; - break; + config_usage(0, config->program); + return _Py_INIT_EXIT(0); case 'V': - cmdline->print_version++; + print_version++; break; case 'W': - if (_PyWstrList_Append(&cmdline->cmdline_warnoptions, _PyOS_optarg) < 0) { + if (_PyWstrList_Append(warnoptions, _PyOS_optarg) < 0) { return _Py_INIT_NO_MEMORY(); } break; @@ -1783,11 +1784,17 @@ config_parse_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline, default: /* unknown argument: parsing failed */ - cmdline->need_usage = 1; - return _Py_INIT_OK(); + config_usage(1, config->program); + return _Py_INIT_EXIT(2); } } while (1); + if (print_version) { + printf("Python %s\n", + (print_version >= 2) ? Py_GetVersion() : PY_VERSION); + return _Py_INIT_EXIT(0); + } + if (config->run_command == NULL && config->run_module == NULL && _PyOS_optind < argv->length && wcscmp(argv->items[_PyOS_optind], L"-") != 0 @@ -1819,7 +1826,7 @@ config_parse_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline, /* Get warning options from PYTHONWARNINGS environment variable. */ static _PyInitError -cmdline_init_env_warnoptions(_PyCmdline *cmdline, const _PyCoreConfig *config) +config_init_env_warnoptions(const _PyCoreConfig *config, _PyWstrList *warnoptions) { wchar_t *env; int res = _PyCoreConfig_GetEnvDup(config, &env, @@ -1838,7 +1845,7 @@ cmdline_init_env_warnoptions(_PyCmdline *cmdline, const _PyCoreConfig *config) warning != NULL; warning = WCSTOK(NULL, L",", &context)) { - if (_PyWstrList_Append(&cmdline->env_warnoptions, warning) < 0) { + if (_PyWstrList_Append(warnoptions, warning) < 0) { PyMem_RawFree(env); return _Py_INIT_NO_MEMORY(); } @@ -1883,7 +1890,9 @@ config_add_warnoption(_PyCoreConfig *config, const wchar_t *option) static _PyInitError -config_init_warnoptions(_PyCoreConfig *config, const _PyCmdline *cmdline) +config_init_warnoptions(_PyCoreConfig *config, + const _PyWstrList *cmdline_warnoptions, + const _PyWstrList *env_warnoptions) { /* The priority order for warnings configuration is (highest precedence * first): @@ -1909,14 +1918,14 @@ config_init_warnoptions(_PyCoreConfig *config, const _PyCmdline *cmdline) Py_ssize_t i; const _PyWstrList *options; - options = &cmdline->env_warnoptions; + options = env_warnoptions; for (i = 0; i < options->length; i++) { if (config_add_warnoption(config, options->items[i]) < 0) { return _Py_INIT_NO_MEMORY(); } } - options = &cmdline->cmdline_warnoptions; + options = cmdline_warnoptions; for (i = 0; i < options->length; i++) { if (config_add_warnoption(config, options->items[i]) < 0) { return _Py_INIT_NO_MEMORY(); @@ -1992,36 +2001,14 @@ config_init_argv(_PyCoreConfig *config, const _PyPreCmdline *cmdline) } -static void -config_usage(int error, const wchar_t* program) -{ - FILE *f = error ? stderr : stdout; - - fprintf(f, usage_line, program); - if (error) - fprintf(f, "Try `python -h' for more information.\n"); - else { - fputs(usage_1, f); - fputs(usage_2, f); - fputs(usage_3, f); - fprintf(f, usage_4, (wint_t)DELIM); - fprintf(f, usage_5, (wint_t)DELIM, PYTHONHOMEHELP); - fputs(usage_6, f); - } -} - - static _PyInitError core_read_precmdline(_PyCoreConfig *config, const _PyArgv *args, _PyPreCmdline *precmdline) { _PyInitError err; - if (args) { - err = _PyPreCmdline_SetArgv(precmdline, args); - if (_Py_INIT_FAILED(err)) { - return err; - } + if (_PyWstrList_Copy(&precmdline->argv, &config->argv) < 0) { + return _Py_INIT_NO_MEMORY(); } _PyPreConfig preconfig = _PyPreConfig_INIT; @@ -2040,83 +2027,88 @@ done: } -/* Read the configuration into _PyCoreConfig from: - - * Command line arguments - * Environment variables - * Py_xxx global configuration variables */ -_PyInitError -_PyCoreConfig_Read(_PyCoreConfig *config, const _PyArgv *args) +static _PyInitError +config_read_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline) { _PyInitError err; + _PyWstrList cmdline_warnoptions = _PyWstrList_INIT; + _PyWstrList env_warnoptions = _PyWstrList_INIT; - err = _Py_PreInitializeFromCoreConfig(config); + err = config_parse_cmdline(config, precmdline, &cmdline_warnoptions); if (_Py_INIT_FAILED(err)) { - return err; + goto done; } - _PyCoreConfig_GetGlobalConfig(config); + err = config_init_argv(config, precmdline); + if (_Py_INIT_FAILED(err)) { + goto done; + } - _PyPreCmdline precmdline = _PyPreCmdline_INIT; - err = core_read_precmdline(config, args, &precmdline); + err = config_read(config, precmdline); if (_Py_INIT_FAILED(err)) { goto done; } - if (config->program == NULL) { - err = config_init_program(config, &precmdline); + if (config->use_environment) { + err = config_init_env_warnoptions(config, &env_warnoptions); if (_Py_INIT_FAILED(err)) { goto done; } } - _PyCmdline cmdline; - memset(&cmdline, 0, sizeof(cmdline)); + err = config_init_warnoptions(config, + &cmdline_warnoptions, &env_warnoptions); + if (_Py_INIT_FAILED(err)) { + goto done; + } - if (args) { - err = config_parse_cmdline(config, &cmdline, &precmdline); - if (_Py_INIT_FAILED(err)) { - goto done; - } + err = _Py_INIT_OK(); - if (cmdline.need_usage) { - config_usage(1, config->program); - err = _Py_INIT_EXIT(2); - goto done; - } +done: + _PyWstrList_Clear(&cmdline_warnoptions); + _PyWstrList_Clear(&env_warnoptions); + return err; +} - if (cmdline.print_help) { - config_usage(0, config->program); - err = _Py_INIT_EXIT(0); - goto done; - } - if (cmdline.print_version) { - printf("Python %s\n", - (cmdline.print_version >= 2) ? Py_GetVersion() : PY_VERSION); - err = _Py_INIT_EXIT(0); - goto done; - } +/* Read the configuration into _PyCoreConfig from: + + * Command line arguments + * Environment variables + * Py_xxx global configuration variables */ +_PyInitError +_PyCoreConfig_Read(_PyCoreConfig *config, const _PyArgv *args) +{ + _PyInitError err; - err = config_init_argv(config, &precmdline); + if (args) { + err = _PyArgv_AsWstrList(args, &config->argv); if (_Py_INIT_FAILED(err)) { - goto done; + return err; } } - err = config_read(config, &precmdline); + err = _Py_PreInitializeFromCoreConfig(config); + if (_Py_INIT_FAILED(err)) { + return err; + } + + _PyCoreConfig_GetGlobalConfig(config); + + _PyPreCmdline precmdline = _PyPreCmdline_INIT; + err = core_read_precmdline(config, args, &precmdline); if (_Py_INIT_FAILED(err)) { goto done; } - if (config->use_environment) { - err = cmdline_init_env_warnoptions(&cmdline, config); + if (config->program == NULL) { + err = config_init_program(config, &precmdline); if (_Py_INIT_FAILED(err)) { goto done; } } - err = config_init_warnoptions(config, &cmdline); + err = config_read_cmdline(config, &precmdline); if (_Py_INIT_FAILED(err)) { goto done; } @@ -2176,7 +2168,6 @@ _PyCoreConfig_Read(_PyCoreConfig *config, const _PyArgv *args) err = _Py_INIT_OK(); done: - cmdline_clear(&cmdline); _PyPreCmdline_Clear(&precmdline); return err; } |