always exists and is never empty. */
_PyWstrList argv;
- /* Program: argv[0] or "".
- Used to display Python usage if parsing command line arguments fails.
- Used to initialize the default value of program_name */
- wchar_t *program;
- wchar_t *program_name; /* Program name, see also Py_GetProgramName() */
+ /* Program name:
+
+ - If Py_SetProgramName() was called, use its value.
+ - On macOS, use PYTHONEXECUTABLE environment variable if set.
+ - If WITH_NEXT_FRAMEWORK macro is defined, use __PYVENV_LAUNCHER__
+ environment variable is set.
+ - Use argv[0] if available and non-empty.
+ - Use "python" on Windows, or "python3 on other platforms. */
+ wchar_t *program_name;
_PyWstrList xoptions; /* Command line -X options */
_PyWstrList warnoptions; /* Warnings options */
'program_name': GET_DEFAULT_CONFIG,
'parse_argv': 1,
'argv': [""],
- 'program': '',
'xoptions': [],
'warnoptions': [],
'pycache_prefix': 'conf_pycache_prefix',
'program_name': './conf_program_name',
'argv': ['-c', 'arg2'],
- 'program': 'conf_program',
'xoptions': ['core_xoption1=3', 'core_xoption2=', 'core_xoption3'],
'warnoptions': ['error::ResourceWarning', 'default::BytesWarning'],
'run_command': 'pass\n',
'print(json.dumps(_testinternalcapi.get_configs()))')
core_config = {
'argv': ['-c', 'arg2'],
- 'program': 'python3',
'program_name': './python3',
'run_command': code + '\n',
}
'print(json.dumps(_testinternalcapi.get_configs()))')
core_config = {
'argv': ['-c', 'arg2'],
- 'program': 'python3',
'program_name': './python3',
'run_command': code + '\n',
'_init_main': 0,
core_config = {
'argv': ['-v', '-c', 'arg1', '-W', 'arg2'],
'parse_argv': 0,
- 'program': 'program',
}
self.check_config("init_dont_parse_argv", core_config, {})
CLEAR(config->module_search_path_env);
CLEAR(config->home);
CLEAR(config->program_name);
- CLEAR(config->program);
_PyWstrList_Clear(&config->argv);
_PyWstrList_Clear(&config->warnoptions);
COPY_WSTR_ATTR(module_search_path_env);
COPY_WSTR_ATTR(home);
COPY_WSTR_ATTR(program_name);
- COPY_WSTR_ATTR(program);
COPY_ATTR(parse_argv);
COPY_WSTRLIST(argv);
SET_ITEM_WSTR(program_name);
SET_ITEM_INT(parse_argv);
SET_ITEM_WSTRLIST(argv);
- SET_ITEM_WSTR(program);
SET_ITEM_WSTRLIST(xoptions);
SET_ITEM_WSTRLIST(warnoptions);
SET_ITEM_WSTR(module_search_path_env);
config_init_program_name(_PyCoreConfig *config)
{
_PyInitError err;
- assert(config->program_name == NULL);
/* If Py_SetProgramName() was called, use its value */
const wchar_t *program_name = _Py_path_config.program_name;
#endif /* WITH_NEXT_FRAMEWORK */
#endif /* __APPLE__ */
- /* Use argv[0] by default, if available */
- if (config->program != NULL) {
- err = _PyCoreConfig_SetString(&config->program_name, config->program);
- if (_Py_INIT_FAILED(err)) {
- return err;
+ /* Use argv[0] if available and non-empty */
+ const _PyWstrList *argv = &config->argv;
+ if (argv->length >= 1 && argv->items[0][0] != L'\0') {
+ config->program_name = _PyMem_RawWcsdup(argv->items[0]);
+ if (config->program_name == NULL) {
+ return _Py_INIT_NO_MEMORY();
}
return _Py_INIT_OK();
}
- /* Last fall back: hardcoded string */
+ /* Last fall back: hardcoded name */
#ifdef MS_WINDOWS
const wchar_t *default_program_name = L"python";
#else
}
}
- if (config->program_name == NULL) {
- err = config_init_program_name(config);
- if (_Py_INIT_FAILED(err)) {
- return err;
- }
- }
-
if (config->executable == NULL) {
err = config_init_executable(config);
if (_Py_INIT_FAILED(err)) {
_PyInitError err;
const _PyWstrList *argv = &config->argv;
int print_version = 0;
+ const wchar_t* program = config->program_name;
_PyOS_ResetGetOpt();
do {
} else {
fprintf(stderr, "--check-hash-based-pycs must be one of "
"'default', 'always', or 'never'\n");
- config_usage(1, config->program);
+ config_usage(1, program);
return _Py_INIT_EXIT(2);
}
break;
case 'h':
case '?':
- config_usage(0, config->program);
+ config_usage(0, program);
return _Py_INIT_EXIT(0);
case 'V':
default:
/* unknown argument: parsing failed */
- config_usage(1, config->program);
+ config_usage(1, program);
return _Py_INIT_EXIT(2);
}
} while (1);
}
-static _PyInitError
-config_init_program(_PyCoreConfig *config)
-{
- const _PyWstrList *argv = &config->argv;
- wchar_t *program;
- if (argv->length >= 1) {
- program = argv->items[0];
- }
- else {
- program = L"";
- }
- config->program = _PyMem_RawWcsdup(program);
- if (config->program == NULL) {
- return _Py_INIT_NO_MEMORY();
- }
-
- return _Py_INIT_OK();
-}
-
-
static int
config_add_warnoption(_PyCoreConfig *config, const wchar_t *option)
{
config->parse_argv = 1;
}
- if (config->program == NULL) {
- err = config_init_program(config);
+ if (config->program_name == NULL) {
+ err = config_init_program_name(config);
if (_Py_INIT_FAILED(err)) {
- goto done;
+ return err;
}
}
goto done;
}
+ /* precmdline.argv is a copy of config.argv which is modified
+ by config_read_cmdline() */
const _PyWstrList *argv = &precmdline.argv;
if (_Py_SetArgcArgv(argv->length, argv->items) < 0) {
err = _Py_INIT_NO_MEMORY();
assert(config->configure_c_stdio >= 0);
assert(config->buffered_stdio >= 0);
assert(config->program_name != NULL);
- assert(config->program != NULL);
assert(_PyWstrList_CheckConsistency(&config->argv));
/* sys.argv must be non-empty: empty argv is replaced with [''] */
assert(config->argv.length >= 1);