diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-11-23 10:43:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-23 10:43:14 +0100 |
commit | 1f15111a6e15d52f6b08907576ec61493cd59358 (patch) | |
tree | b1a8fd55a1790e3dfd2026cc238e7344f293af90 /Python | |
parent | e32e79f7d8216b78ac9e61bb1f2eee693108d4ee (diff) | |
download | cpython-git-1f15111a6e15d52f6b08907576ec61493cd59358.tar.gz |
bpo-32030: Add _PyMainInterpreterConfig.pythonhome (#4513)
* Py_Main() now reads the PYTHONHOME environment variable
* Add _Py_GetPythonHomeWithConfig() private function
* Add _PyWarnings_InitWithConfig()
* init_filters() doesn't get the current core configuration from the
current interpreter or Python thread anymore. Pass explicitly the
configuration to _PyWarnings_InitWithConfig().
* _Py_InitializeCore() now fails on _PyWarnings_InitWithConfig()
failure.
* Pass configuration as constant
Diffstat (limited to 'Python')
-rw-r--r-- | Python/_warnings.c | 20 | ||||
-rw-r--r-- | Python/pylifecycle.c | 49 |
2 files changed, 49 insertions, 20 deletions
diff --git a/Python/_warnings.c b/Python/_warnings.c index d865f0ad2c..36d649fda1 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -1185,10 +1185,9 @@ create_filter(PyObject *category, const char *action) } static PyObject * -init_filters(void) +init_filters(const _PyCoreConfig *config) { - PyInterpreterState *interp = PyThreadState_GET()->interp; - int dev_mode = interp->core_config.dev_mode; + int dev_mode = config->dev_mode; Py_ssize_t count = 2; if (dev_mode) { @@ -1264,8 +1263,8 @@ static struct PyModuleDef warningsmodule = { }; -PyMODINIT_FUNC -_PyWarnings_Init(void) +PyObject* +_PyWarnings_InitWithConfig(const _PyCoreConfig *config) { PyObject *m; @@ -1274,7 +1273,7 @@ _PyWarnings_Init(void) return NULL; if (_PyRuntime.warnings.filters == NULL) { - _PyRuntime.warnings.filters = init_filters(); + _PyRuntime.warnings.filters = init_filters(config); if (_PyRuntime.warnings.filters == NULL) return NULL; } @@ -1305,3 +1304,12 @@ _PyWarnings_Init(void) _PyRuntime.warnings.filters_version = 0; return m; } + + +PyMODINIT_FUNC +_PyWarnings_Init(void) +{ + PyInterpreterState *interp = PyThreadState_GET()->interp; + const _PyCoreConfig *config = &interp->core_config; + return _PyWarnings_InitWithConfig(config); +} diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 552501d23c..5bbbbc68f0 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -767,7 +767,9 @@ _Py_InitializeCore(const _PyCoreConfig *config) } /* Initialize _warnings. */ - _PyWarnings_Init(); + if (_PyWarnings_InitWithConfig(&interp->core_config) == NULL) { + return _Py_INIT_ERR("can't initialize warnings"); + } /* This call sets up builtin and frozen import support */ if (!interp->core_config._disable_importlib) { @@ -880,7 +882,7 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config) return err; } - if (config->install_signal_handlers) { + if (interp->config.install_signal_handlers) { err = initsigs(); /* Signal handling stuff, including initintr() */ if (_Py_INIT_FAILED(err)) { return err; @@ -1468,7 +1470,6 @@ Py_GetProgramName(void) } static wchar_t *default_home = NULL; -static wchar_t env_home[MAXPATHLEN+1]; void Py_SetPythonHome(wchar_t *home) @@ -1477,20 +1478,40 @@ Py_SetPythonHome(wchar_t *home) } wchar_t * -Py_GetPythonHome(void) +_Py_GetPythonHomeWithConfig(const _PyMainInterpreterConfig *config) { - wchar_t *home = default_home; - if (home == NULL && !Py_IgnoreEnvironmentFlag) { - char* chome = Py_GETENV("PYTHONHOME"); - if (chome) { - size_t size = Py_ARRAY_LENGTH(env_home); - size_t r = mbstowcs(env_home, chome, size); - if (r != (size_t)-1 && r < size) - home = env_home; - } + /* 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; + } + if (config) { + return config->pythonhome; } - return 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; +} + +wchar_t * +Py_GetPythonHome(void) +{ + return _Py_GetPythonHomeWithConfig(NULL); } /* Add the __main__ module */ |