From 13badcbc60cdbfae1dba1683fd2fae9d70717143 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 2 Dec 2017 21:36:00 +0200 Subject: bpo-32197: Try to fix a compiler error on OS X introduced in bpo-32030. (#4681) * Revert "bpo-32030: _PyPathConfig_Init() sets home and program_name (#4673)" This reverts commit af5a895073c24637c094772b27526b94a12ec897. * Revert "bpo-32030: Fix config_get_program_name() on macOS (#4669)" This reverts commit e23c06e2b03452c9aaf0dae52296c85e572f9bcd. * Revert "bpo-32030: Add Python/pathconfig.c (#4668)" This reverts commit 0ea395ae964c9cd0f499e2ef0d0030c971201220. * Revert "bpo-32030: Don't call _PyPathConfig_Fini() in Py_FinalizeEx() (#4667)" This reverts commit ebac19dad6263141d5db0a2c923efe049dba99d2. * Revert "bpo-32030: Fix Py_GetPath(): init program_name (#4665)" This reverts commit 9ac3d8882712c9675c3d2f9f84af6b5729575cde. --- Python/pylifecycle.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 6 deletions(-) (limited to 'Python/pylifecycle.c') diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 523397f126..f0a49f91fb 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -804,12 +804,7 @@ _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *config) } if (config->program_name == NULL) { -#ifdef MS_WINDOWS - const wchar_t *program_name = L"python"; -#else - const wchar_t *program_name = L"python3"; -#endif - config->program_name = _PyMem_RawWcsdup(program_name); + config->program_name = _PyMem_RawWcsdup(Py_GetProgramName()); if (config->program_name == NULL) { return _Py_INIT_NO_MEMORY(); } @@ -1278,6 +1273,8 @@ Py_FinalizeEx(void) call_ll_exitfuncs(); + _PyPathConfig_Fini(); + _PyRuntime_Finalize(); return status; } @@ -1494,6 +1491,61 @@ Py_EndInterpreter(PyThreadState *tstate) PyInterpreterState_Delete(interp); } +#ifdef MS_WINDOWS +static wchar_t *progname = L"python"; +#else +static wchar_t *progname = L"python3"; +#endif + +void +Py_SetProgramName(wchar_t *pn) +{ + if (pn && *pn) + progname = pn; +} + +wchar_t * +Py_GetProgramName(void) +{ + return progname; +} + +static wchar_t *default_home = NULL; + +void +Py_SetPythonHome(wchar_t *home) +{ + default_home = home; +} + + +wchar_t* +Py_GetPythonHome(void) +{ + /* Use a static buffer to avoid heap memory allocation failure. + Py_GetPythonHome() doesn't allow to report error, and the caller + doesn't release memory. */ + static wchar_t buffer[MAXPATHLEN+1]; + + if (default_home) { + return default_home; + } + + char *home = Py_GETENV("PYTHONHOME"); + if (!home) { + return NULL; + } + + size_t size = Py_ARRAY_LENGTH(buffer); + size_t r = mbstowcs(buffer, home, size); + if (r == (size_t)-1 || r >= size) { + /* conversion failed or the static buffer is too small */ + return NULL; + } + + return buffer; +} + /* Add the __main__ module */ static _PyInitError -- cgit v1.2.1