diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-12-04 12:39:15 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-04 12:39:15 (GMT) |
commit | 31a8393cf6a74c870c3484dd68500619f6232c6d (patch) | |
tree | 11a7d0b4f4df344e1dbe9798a7d48ca2acc8abae /Modules/main.c | |
parent | 70d56fb52582d9d3f7c00860d6e90570c6259371 (diff) | |
download | cpython-31a8393cf6a74c870c3484dd68500619f6232c6d.zip cpython-31a8393cf6a74c870c3484dd68500619f6232c6d.tar.gz cpython-31a8393cf6a74c870c3484dd68500619f6232c6d.tar.bz2 |
Revert "bpo-32197: Try to fix a compiler error on OS X introduced in bpo-32030. (#4681)" (#4694)
* Revert "bpo-32197: Try to fix a compiler error on OS X introduced in bpo-32030. (#4681)"
This reverts commit 13badcbc60cdbfae1dba1683fd2fae9d70717143.
Re-apply commits:
* "bpo-32030: _PyPathConfig_Init() sets home and program_name (#4673)"
commit af5a895073c24637c094772b27526b94a12ec897.
* "bpo-32030: Fix config_get_program_name() on macOS (#4669)"
commit e23c06e2b03452c9aaf0dae52296c85e572f9bcd.
* "bpo-32030: Add Python/pathconfig.c (#4668)"
commit 0ea395ae964c9cd0f499e2ef0d0030c971201220.
* "bpo-32030: Don't call _PyPathConfig_Fini() in Py_FinalizeEx() (#4667)"
commit ebac19dad6263141d5db0a2c923efe049dba99d2.
* "bpo-32030: Fix Py_GetPath(): init program_name (#4665)"
commit 9ac3d8882712c9675c3d2f9f84af6b5729575cde.
* Fix compilation error on macOS
Diffstat (limited to 'Modules/main.c')
-rw-r--r-- | Modules/main.c | 121 |
1 files changed, 63 insertions, 58 deletions
diff --git a/Modules/main.c b/Modules/main.c index e9d524a..4095259 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -412,7 +412,6 @@ typedef struct { /* non-zero if filename, command (-c) or module (-m) is set on the command line */ int run_code; - wchar_t *program_name; /* Error message if a function failed */ _PyInitError err; /* PYTHONWARNINGS env var */ @@ -429,7 +428,6 @@ typedef struct { .config = _PyMainInterpreterConfig_INIT, \ .main_importer_path = NULL, \ .run_code = -1, \ - .program_name = NULL, \ .err = _Py_INIT_OK(), \ .env_warning_options = {0, NULL}} @@ -455,7 +453,6 @@ pymain_free_impl(_PyMain *pymain) pymain_optlist_clear(&pymain->env_warning_options); Py_CLEAR(pymain->main_importer_path); - PyMem_RawFree(pymain->program_name); _PyMainInterpreterConfig_Clear(&pymain->config); @@ -874,14 +871,21 @@ pymain_init_stdio(_PyMain *pymain) /* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__ - environment variables on macOS if available, use argv[0] by default. - - Return 0 on success. - Set pymain->err and return -1 on error. */ -static int -pymain_get_program_name(_PyMain *pymain) + environment variables on macOS if available. */ +static _PyInitError +config_get_program_name(_PyMainInterpreterConfig *config) { - assert(pymain->program_name == NULL); + assert(config->program_name == NULL); + + /* If Py_SetProgramName() was called, use its value */ + wchar_t *program_name = _Py_path_config.program_name; + if (program_name != NULL) { + config->program_name = _PyMem_RawWcsdup(program_name); + if (config->program_name == NULL) { + return _Py_INIT_NO_MEMORY(); + } + } + #ifdef __APPLE__ char *p; /* On MacOS X, when the Python interpreter is embedded in an @@ -894,17 +898,13 @@ pymain_get_program_name(_PyMain *pymain) See Lib/plat-mac/bundlebuiler.py for details about the bootstrap script. */ if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0') { - wchar_t* buffer; - size_t len = strlen(p) + 1; - - buffer = PyMem_RawMalloc(len * sizeof(wchar_t)); - if (buffer == NULL) { - pymain->err = _Py_INIT_NO_MEMORY(); - return -1; + size_t len; + wchar_t* program_name = Py_DecodeLocale(p, &len); + if (program_name == NULL) { + return DECODE_LOCALE_ERR("PYTHONEXECUTABLE environment " + "variable", (Py_ssize_t)len); } - - mbstowcs(buffer, p, len); - pymain->program_name = buffer; + config->program_name = program_name; } #ifdef WITH_NEXT_FRAMEWORK else { @@ -914,21 +914,30 @@ pymain_get_program_name(_PyMain *pymain) * the argv0 of the stub executable */ size_t len; - wchar_t* wbuf = Py_DecodeLocale(pyvenv_launcher, &len); - if (wbuf == NULL) { - SET_DECODE_ERROR("__PYVENV_LAUNCHER__", len); - return -1; + wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len); + if (program_name == NULL) { + return DECODE_LOCALE_ERR("__PYVENV_LAUNCHER__ environment " + "variable", (Py_ssize_t)len); } - pymain->program_name = wbuf; + config->program_name = program_name; } } #endif /* WITH_NEXT_FRAMEWORK */ #endif /* __APPLE__ */ - if (pymain->program_name == NULL) { + return _Py_INIT_OK(); +} + + +/* If config_get_program_name() found no program name: use argv[0] by default. + Return 0 on success. Set pymain->err and return -1 on error. */ +static int +pymain_get_program_name(_PyMain *pymain) +{ + if (pymain->config.program_name == NULL) { /* Use argv[0] by default */ - pymain->program_name = pymain_wstrdup(pymain, pymain->argv[0]); - if (pymain->program_name == NULL) { + pymain->config.program_name = pymain_wstrdup(pymain, pymain->argv[0]); + if (pymain->config.program_name == NULL) { return -1; } } @@ -950,13 +959,6 @@ pymain_init_main_interpreter(_PyMain *pymain) { _PyInitError err; - /* TODO: Print any exceptions raised by these operations */ - err = _PyMainInterpreterConfig_Read(&pymain->config); - if (_Py_INIT_FAILED(err)) { - pymain->err = err; - return -1; - } - err = _Py_InitializeMainInterpreter(&pymain->config); if (_Py_INIT_FAILED(err)) { pymain->err = err; @@ -1414,14 +1416,13 @@ config_init_pythonpath(_PyMainInterpreterConfig *config) static _PyInitError -config_init_pythonhome(_PyMainInterpreterConfig *config) +config_init_home(_PyMainInterpreterConfig *config) { wchar_t *home; - home = Py_GetPythonHome(); + /* If Py_SetPythonHome() was called, use its value */ + home = _Py_path_config.home; if (home) { - /* Py_SetPythonHome() has been called before Py_Main(), - use its value */ config->home = _PyMem_RawWcsdup(home); if (config->home == NULL) { return _Py_INIT_NO_MEMORY(); @@ -1441,7 +1442,7 @@ config_init_pythonhome(_PyMainInterpreterConfig *config) _PyInitError _PyMainInterpreterConfig_ReadEnv(_PyMainInterpreterConfig *config) { - _PyInitError err = config_init_pythonhome(config); + _PyInitError err = config_init_home(config); if (_Py_INIT_FAILED(err)) { return err; } @@ -1451,11 +1452,9 @@ _PyMainInterpreterConfig_ReadEnv(_PyMainInterpreterConfig *config) return err; } - /* FIXME: _PyMainInterpreterConfig_Read() has the same code. Remove it - here? See also pymain_get_program_name() and pymain_parse_envvars(). */ - config->program_name = _PyMem_RawWcsdup(Py_GetProgramName()); - if (config->program_name == NULL) { - return _Py_INIT_NO_MEMORY(); + err = config_get_program_name(config); + if (_Py_INIT_FAILED(err)) { + return err; } return _Py_INIT_OK(); @@ -1481,25 +1480,17 @@ pymain_parse_envvars(_PyMain *pymain) if (pymain_warnings_envvar(pymain) < 0) { return -1; } - if (pymain_get_program_name(pymain) < 0) { - return -1; - } - core_config->allocator = Py_GETENV("PYTHONMALLOC"); - - /* FIXME: move pymain_get_program_name() code into - _PyMainInterpreterConfig_ReadEnv(). - Problem: _PyMainInterpreterConfig_ReadEnv() doesn't have access - to argv[0]. */ - Py_SetProgramName(pymain->program_name); - /* Don't free program_name here: the argument to Py_SetProgramName - must remain valid until Py_FinalizeEx is called. The string is freed - by pymain_free(). */ _PyInitError err = _PyMainInterpreterConfig_ReadEnv(&pymain->config); if (_Py_INIT_FAILED(pymain->err)) { pymain->err = err; return -1; } + if (pymain_get_program_name(pymain) < 0) { + return -1; + } + + core_config->allocator = Py_GETENV("PYTHONMALLOC"); /* -X options */ if (pymain_get_xoption(pymain, L"showrefcount")) { @@ -1555,6 +1546,12 @@ pymain_parse_cmdline_envvars_impl(_PyMain *pymain) return -1; } + _PyInitError err = _PyMainInterpreterConfig_Read(&pymain->config); + if (_Py_INIT_FAILED(err)) { + pymain->err = err; + return -1; + } + return 0; } @@ -1671,6 +1668,14 @@ pymain_impl(_PyMain *pymain) other special meaning */ pymain->status = 120; } + + /* _PyPathConfig_Clear() cannot be called in Py_FinalizeEx(). + Py_Initialize() and Py_Finalize() can be called multiple times, but it + must not "forget" parameters set by Py_SetProgramName(), Py_SetPath() or + Py_SetPythonHome(), whereas _PyPathConfig_Clear() clear all these + parameters. */ + _PyPathConfig_Clear(&_Py_path_config); + return 0; } |