aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/coreconfig.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2019-05-02 15:25:34 -0400
committerGitHub <noreply@github.com>2019-05-02 15:25:34 -0400
commit70005ac0fddd8585725b92acd1bc2b8e7b81999c (patch)
treef1be156bc866978bc4d251879f1e285cfa2df7ee /Python/coreconfig.c
parent709d23dee69e700b87d5a4cb59e149d0e1af7993 (diff)
downloadcpython-70005ac0fddd8585725b92acd1bc2b8e7b81999c.tar.gz
cpython-70005ac0fddd8585725b92acd1bc2b8e7b81999c.zip
bpo-36763: _PyCoreConfig_SetPyArgv() preinitializes Python (GH-13037)
_PyCoreConfig_SetPyArgv() and _PyCoreConfig_SetWideString() now pre-initialize Python if needed to ensure that the locale encoding is properly configured. * Add _Py_PreInitializeFromPyArgv() internal function. * Add 'args' parameter to _Py_PreInitializeFromCoreConfig()
Diffstat (limited to 'Python/coreconfig.c')
-rw-r--r--Python/coreconfig.c31
1 files changed, 29 insertions, 2 deletions
diff --git a/Python/coreconfig.c b/Python/coreconfig.c
index 15643be3765..52026949e20 100644
--- a/Python/coreconfig.c
+++ b/Python/coreconfig.c
@@ -541,11 +541,15 @@ _PyCoreConfig_SetString(wchar_t **config_str, const wchar_t *str)
}
-/* Decode str using Py_DecodeLocale() and set the result into *config_str */
static _PyInitError
_PyCoreConfig_DecodeLocaleErr(wchar_t **config_str, const char *str,
const char *decode_err_msg)
{
+ _PyInitError err = _Py_PreInitialize(NULL);
+ if (_Py_INIT_FAILED(err)) {
+ return err;
+ }
+
wchar_t *str2;
if (str != NULL) {
size_t len;
@@ -572,6 +576,9 @@ _PyCoreConfig_DecodeLocaleErr(wchar_t **config_str, const char *str,
_PyCoreConfig_DecodeLocaleErr(config_str, str, "cannot decode " NAME)
+/* Decode str using Py_DecodeLocale() and set the result into *config_str.
+ Pre-initialize Python if needed to ensure that encodings are properly
+ configured. */
_PyInitError
_PyCoreConfig_DecodeLocale(wchar_t **config_str, const char *str)
{
@@ -2100,10 +2107,30 @@ done:
_PyInitError
_PyCoreConfig_SetPyArgv(_PyCoreConfig *config, const _PyArgv *args)
{
+ if (args->use_bytes_argv) {
+ _PyInitError err;
+
+ err = _PyRuntime_Initialize();
+ if (_Py_INIT_FAILED(err)) {
+ return err;
+ }
+ _PyRuntimeState *runtime = &_PyRuntime;
+
+ /* do nothing if Python is already pre-initialized:
+ _PyCoreConfig_Write() will update _PyRuntime.preconfig later */
+ if (!runtime->pre_initialized) {
+ err = _Py_PreInitializeFromCoreConfig(config, args);
+ if (_Py_INIT_FAILED(err)) {
+ return err;
+ }
+ }
+ }
return _PyArgv_AsWstrList(args, &config->argv);
}
+/* Set config.argv: decode argv using Py_DecodeLocale(). Pre-initialize Python
+ if needed to ensure that encodings are properly configured. */
_PyInitError
_PyCoreConfig_SetArgv(_PyCoreConfig *config, int argc, char **argv)
{
@@ -2138,7 +2165,7 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
{
_PyInitError err;
- err = _Py_PreInitializeFromCoreConfig(config);
+ err = _Py_PreInitializeFromCoreConfig(config, NULL);
if (_Py_INIT_FAILED(err)) {
return err;
}