2019-03-01 16:52:56 +00:00
|
|
|
#include "Python.h"
|
|
|
|
#include "pycore_coreconfig.h"
|
2019-03-05 01:44:12 +00:00
|
|
|
#include "pycore_getopt.h"
|
2019-03-05 11:32:09 +00:00
|
|
|
#include "pycore_pystate.h" /* _PyRuntime_Initialize() */
|
|
|
|
#include <locale.h> /* setlocale() */
|
2019-03-01 16:52:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define DECODE_LOCALE_ERR(NAME, LEN) \
|
|
|
|
(((LEN) == -2) \
|
|
|
|
? _Py_INIT_USER_ERR("cannot decode " NAME) \
|
|
|
|
: _Py_INIT_NO_MEMORY())
|
|
|
|
|
|
|
|
|
|
|
|
/* --- File system encoding/errors -------------------------------- */
|
|
|
|
|
|
|
|
/* The filesystem encoding is chosen by config_init_fs_encoding(),
|
|
|
|
see also initfsencoding(). */
|
|
|
|
const char *Py_FileSystemDefaultEncoding = NULL;
|
|
|
|
int Py_HasFileSystemDefaultEncoding = 0;
|
|
|
|
const char *Py_FileSystemDefaultEncodeErrors = NULL;
|
|
|
|
int _Py_HasFileSystemDefaultEncodeErrors = 0;
|
|
|
|
|
|
|
|
void
|
|
|
|
_Py_ClearFileSystemEncoding(void)
|
|
|
|
{
|
|
|
|
if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
|
|
|
|
PyMem_RawFree((char*)Py_FileSystemDefaultEncoding);
|
|
|
|
Py_FileSystemDefaultEncoding = NULL;
|
|
|
|
}
|
|
|
|
if (!_Py_HasFileSystemDefaultEncodeErrors && Py_FileSystemDefaultEncodeErrors) {
|
|
|
|
PyMem_RawFree((char*)Py_FileSystemDefaultEncodeErrors);
|
|
|
|
Py_FileSystemDefaultEncodeErrors = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors
|
|
|
|
global configuration variables. */
|
|
|
|
int
|
|
|
|
_Py_SetFileSystemEncoding(const char *encoding, const char *errors)
|
|
|
|
{
|
|
|
|
char *encoding2 = _PyMem_RawStrdup(encoding);
|
|
|
|
if (encoding2 == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *errors2 = _PyMem_RawStrdup(errors);
|
|
|
|
if (errors2 == NULL) {
|
|
|
|
PyMem_RawFree(encoding2);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Py_ClearFileSystemEncoding();
|
|
|
|
|
|
|
|
Py_FileSystemDefaultEncoding = encoding2;
|
|
|
|
Py_HasFileSystemDefaultEncoding = 0;
|
|
|
|
|
|
|
|
Py_FileSystemDefaultEncodeErrors = errors2;
|
|
|
|
_Py_HasFileSystemDefaultEncodeErrors = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- _PyArgv ---------------------------------------------------- */
|
|
|
|
|
|
|
|
_PyInitError
|
2019-03-15 14:08:05 +00:00
|
|
|
_PyArgv_AsWstrList(const _PyArgv *args, _PyWstrList *list)
|
2019-03-01 16:52:56 +00:00
|
|
|
{
|
2019-03-15 14:08:05 +00:00
|
|
|
_PyWstrList wargv = _PyWstrList_INIT;
|
2019-03-01 16:52:56 +00:00
|
|
|
if (args->use_bytes_argv) {
|
2019-03-15 14:08:05 +00:00
|
|
|
size_t size = sizeof(wchar_t*) * args->argc;
|
|
|
|
wargv.items = (wchar_t **)PyMem_RawMalloc(size);
|
|
|
|
if (wargv.items == NULL) {
|
2019-03-01 16:52:56 +00:00
|
|
|
return _Py_INIT_NO_MEMORY();
|
|
|
|
}
|
|
|
|
|
2019-03-15 14:08:05 +00:00
|
|
|
for (Py_ssize_t i = 0; i < args->argc; i++) {
|
2019-03-01 16:52:56 +00:00
|
|
|
size_t len;
|
|
|
|
wchar_t *arg = Py_DecodeLocale(args->bytes_argv[i], &len);
|
|
|
|
if (arg == NULL) {
|
2019-03-15 14:08:05 +00:00
|
|
|
_PyWstrList_Clear(&wargv);
|
2019-03-01 16:52:56 +00:00
|
|
|
return DECODE_LOCALE_ERR("command line arguments",
|
|
|
|
(Py_ssize_t)len);
|
|
|
|
}
|
2019-03-15 14:08:05 +00:00
|
|
|
wargv.items[i] = arg;
|
|
|
|
wargv.length++;
|
2019-03-01 16:52:56 +00:00
|
|
|
}
|
2019-03-15 14:08:05 +00:00
|
|
|
|
|
|
|
_PyWstrList_Clear(list);
|
|
|
|
*list = wargv;
|
2019-03-01 16:52:56 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-03-15 14:08:05 +00:00
|
|
|
wargv.length = args->argc;
|
|
|
|
wargv.items = args->wchar_argv;
|
|
|
|
if (_PyWstrList_Copy(list, &wargv) < 0) {
|
|
|
|
return _Py_INIT_NO_MEMORY();
|
|
|
|
}
|
2019-03-01 16:52:56 +00:00
|
|
|
}
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
2019-03-05 01:01:27 +00:00
|
|
|
|
|
|
|
|
2019-03-05 01:44:12 +00:00
|
|
|
/* --- _PyPreCmdline ------------------------------------------------- */
|
|
|
|
|
2019-03-20 03:25:38 +00:00
|
|
|
void
|
|
|
|
_PyPreCmdline_Clear(_PyPreCmdline *cmdline)
|
2019-03-05 01:44:12 +00:00
|
|
|
{
|
2019-03-15 14:08:05 +00:00
|
|
|
_PyWstrList_Clear(&cmdline->argv);
|
|
|
|
_PyWstrList_Clear(&cmdline->xoptions);
|
2019-03-05 01:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 17:37:10 +00:00
|
|
|
int
|
|
|
|
_PyPreCmdline_Copy(_PyPreCmdline *cmdline, const _PyPreCmdline *cmdline2)
|
|
|
|
{
|
|
|
|
_PyPreCmdline_Clear(cmdline);
|
|
|
|
if (_PyWstrList_Copy(&cmdline->argv, &cmdline2->argv) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (_PyWstrList_Copy(&cmdline->xoptions, &cmdline2->xoptions) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cmdline->use_environment = cmdline2->use_environment;
|
|
|
|
cmdline->isolated = cmdline2->isolated;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-20 03:25:38 +00:00
|
|
|
_PyInitError
|
2019-03-25 16:54:58 +00:00
|
|
|
_PyPreCmdline_SetArgv(_PyPreCmdline *cmdline, const _PyArgv *args)
|
2019-03-20 03:25:38 +00:00
|
|
|
{
|
|
|
|
return _PyArgv_AsWstrList(args, &cmdline->argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 17:37:10 +00:00
|
|
|
void
|
2019-03-25 16:54:58 +00:00
|
|
|
_PyPreCmdline_GetPreConfig(_PyPreCmdline *cmdline, const _PyPreConfig *config)
|
|
|
|
{
|
|
|
|
#define COPY_ATTR(ATTR) \
|
|
|
|
if (config->ATTR != -1) { \
|
|
|
|
cmdline->ATTR = config->ATTR; \
|
|
|
|
}
|
|
|
|
|
|
|
|
COPY_ATTR(use_environment);
|
|
|
|
COPY_ATTR(isolated);
|
|
|
|
|
|
|
|
#undef COPY_ATTR
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 17:37:10 +00:00
|
|
|
void
|
|
|
|
_PyPreCmdline_GetCoreConfig(_PyPreCmdline *cmdline, const _PyCoreConfig *config)
|
|
|
|
{
|
|
|
|
#define COPY_ATTR(ATTR) \
|
|
|
|
if (config->preconfig.ATTR != -1) { \
|
|
|
|
cmdline->ATTR = config->preconfig.ATTR; \
|
|
|
|
}
|
|
|
|
|
|
|
|
COPY_ATTR(use_environment);
|
|
|
|
COPY_ATTR(isolated);
|
|
|
|
|
|
|
|
#undef COPY_ATTR
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
_PyPreCmdline_SetCoreConfig(const _PyPreCmdline *cmdline, _PyCoreConfig *config)
|
|
|
|
{
|
|
|
|
#define COPY_ATTR(ATTR) \
|
|
|
|
if (config->preconfig.ATTR == -1 && cmdline->ATTR != -1) { \
|
|
|
|
config->preconfig.ATTR = cmdline->ATTR; \
|
|
|
|
}
|
|
|
|
|
|
|
|
COPY_ATTR(use_environment);
|
|
|
|
COPY_ATTR(isolated);
|
|
|
|
|
|
|
|
#undef COPY_ATTR
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-05 01:01:27 +00:00
|
|
|
/* --- _PyPreConfig ----------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
|
|
|
_PyPreConfig_Clear(_PyPreConfig *config)
|
|
|
|
{
|
2019-03-06 00:13:43 +00:00
|
|
|
PyMem_RawFree(config->allocator);
|
|
|
|
config->allocator = NULL;
|
2019-03-05 01:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
_PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2)
|
|
|
|
{
|
|
|
|
_PyPreConfig_Clear(config);
|
|
|
|
|
|
|
|
#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
|
2019-03-05 16:37:44 +00:00
|
|
|
#define COPY_STR_ATTR(ATTR) \
|
|
|
|
do { \
|
|
|
|
if (config2->ATTR != NULL) { \
|
|
|
|
config->ATTR = _PyMem_RawStrdup(config2->ATTR); \
|
|
|
|
if (config->ATTR == NULL) { \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2019-03-05 01:01:27 +00:00
|
|
|
|
|
|
|
COPY_ATTR(isolated);
|
|
|
|
COPY_ATTR(use_environment);
|
2019-03-05 11:32:09 +00:00
|
|
|
COPY_ATTR(coerce_c_locale);
|
|
|
|
COPY_ATTR(coerce_c_locale_warn);
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
COPY_ATTR(legacy_windows_fs_encoding);
|
|
|
|
#endif
|
|
|
|
COPY_ATTR(utf8_mode);
|
2019-03-05 16:37:44 +00:00
|
|
|
COPY_ATTR(dev_mode);
|
|
|
|
COPY_STR_ATTR(allocator);
|
2019-03-05 01:01:27 +00:00
|
|
|
|
|
|
|
#undef COPY_ATTR
|
2019-03-05 16:37:44 +00:00
|
|
|
#undef COPY_STR_ATTR
|
2019-03-05 01:01:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
_PyPreConfig_GetGlobalConfig(_PyPreConfig *config)
|
|
|
|
{
|
|
|
|
#define COPY_FLAG(ATTR, VALUE) \
|
|
|
|
if (config->ATTR == -1) { \
|
|
|
|
config->ATTR = VALUE; \
|
|
|
|
}
|
|
|
|
#define COPY_NOT_FLAG(ATTR, VALUE) \
|
|
|
|
if (config->ATTR == -1) { \
|
|
|
|
config->ATTR = !(VALUE); \
|
|
|
|
}
|
|
|
|
|
|
|
|
COPY_FLAG(isolated, Py_IsolatedFlag);
|
|
|
|
COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
|
2019-03-05 11:32:09 +00:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
|
|
|
|
#endif
|
|
|
|
COPY_FLAG(utf8_mode, Py_UTF8Mode);
|
2019-03-05 01:01:27 +00:00
|
|
|
|
|
|
|
#undef COPY_FLAG
|
|
|
|
#undef COPY_NOT_FLAG
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
_PyPreConfig_SetGlobalConfig(const _PyPreConfig *config)
|
|
|
|
{
|
|
|
|
#define COPY_FLAG(ATTR, VAR) \
|
|
|
|
if (config->ATTR != -1) { \
|
|
|
|
VAR = config->ATTR; \
|
|
|
|
}
|
|
|
|
#define COPY_NOT_FLAG(ATTR, VAR) \
|
|
|
|
if (config->ATTR != -1) { \
|
|
|
|
VAR = !config->ATTR; \
|
|
|
|
}
|
|
|
|
|
|
|
|
COPY_FLAG(isolated, Py_IsolatedFlag);
|
|
|
|
COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
|
2019-03-05 11:32:09 +00:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag);
|
|
|
|
#endif
|
|
|
|
COPY_FLAG(utf8_mode, Py_UTF8Mode);
|
2019-03-05 01:01:27 +00:00
|
|
|
|
|
|
|
#undef COPY_FLAG
|
|
|
|
#undef COPY_NOT_FLAG
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-05 11:32:09 +00:00
|
|
|
const char*
|
2019-03-25 23:03:15 +00:00
|
|
|
_Py_GetEnv(int use_environment, const char *name)
|
2019-03-05 11:32:09 +00:00
|
|
|
{
|
2019-03-25 23:03:15 +00:00
|
|
|
assert(use_environment >= 0);
|
2019-03-05 11:32:09 +00:00
|
|
|
|
2019-03-25 23:03:15 +00:00
|
|
|
if (!use_environment) {
|
2019-03-05 11:32:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *var = getenv(name);
|
|
|
|
if (var && var[0] != '\0') {
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 23:03:15 +00:00
|
|
|
static const char*
|
|
|
|
_PyPreConfig_GetEnv(const _PyPreConfig *config, const char *name)
|
|
|
|
{
|
|
|
|
return _Py_GetEnv(config->use_environment, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-05 11:32:09 +00:00
|
|
|
int
|
|
|
|
_Py_str_to_int(const char *str, int *result)
|
|
|
|
{
|
|
|
|
const char *endptr = str;
|
|
|
|
errno = 0;
|
|
|
|
long value = strtol(str, (char **)&endptr, 10);
|
|
|
|
if (*endptr != '\0' || errno == ERANGE) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value < INT_MIN || value > INT_MAX) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = (int)value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2019-03-25 23:03:15 +00:00
|
|
|
_Py_get_env_flag(int use_environment, int *flag, const char *name)
|
2019-03-05 11:32:09 +00:00
|
|
|
{
|
2019-03-25 23:03:15 +00:00
|
|
|
const char *var = _Py_GetEnv(use_environment, name);
|
2019-03-05 11:32:09 +00:00
|
|
|
if (!var) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int value;
|
|
|
|
if (_Py_str_to_int(var, &value) < 0 || value < 0) {
|
|
|
|
/* PYTHONDEBUG=text and PYTHONDEBUG=-2 behave as PYTHONDEBUG=1 */
|
|
|
|
value = 1;
|
|
|
|
}
|
|
|
|
if (*flag < value) {
|
|
|
|
*flag = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const wchar_t*
|
2019-03-15 14:08:05 +00:00
|
|
|
_Py_get_xoption(const _PyWstrList *xoptions, const wchar_t *name)
|
2019-03-05 11:32:09 +00:00
|
|
|
{
|
2019-03-15 14:08:05 +00:00
|
|
|
for (Py_ssize_t i=0; i < xoptions->length; i++) {
|
|
|
|
const wchar_t *option = xoptions->items[i];
|
2019-03-05 11:32:09 +00:00
|
|
|
size_t len;
|
|
|
|
wchar_t *sep = wcschr(option, L'=');
|
|
|
|
if (sep != NULL) {
|
|
|
|
len = (sep - option);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = wcslen(option);
|
|
|
|
}
|
|
|
|
if (wcsncmp(option, name, len) == 0 && name[len] == L'\0') {
|
|
|
|
return option;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static _PyInitError
|
|
|
|
preconfig_init_utf8_mode(_PyPreConfig *config, const _PyPreCmdline *cmdline)
|
|
|
|
{
|
|
|
|
const wchar_t *xopt;
|
|
|
|
if (cmdline) {
|
2019-03-15 14:08:05 +00:00
|
|
|
xopt = _Py_get_xoption(&cmdline->xoptions, L"utf8");
|
2019-03-05 11:32:09 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
xopt = NULL;
|
|
|
|
}
|
|
|
|
if (xopt) {
|
|
|
|
wchar_t *sep = wcschr(xopt, L'=');
|
|
|
|
if (sep) {
|
|
|
|
xopt = sep + 1;
|
|
|
|
if (wcscmp(xopt, L"1") == 0) {
|
|
|
|
config->utf8_mode = 1;
|
|
|
|
}
|
|
|
|
else if (wcscmp(xopt, L"0") == 0) {
|
|
|
|
config->utf8_mode = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return _Py_INIT_USER_ERR("invalid -X utf8 option value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
config->utf8_mode = 1;
|
|
|
|
}
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *opt = _PyPreConfig_GetEnv(config, "PYTHONUTF8");
|
|
|
|
if (opt) {
|
|
|
|
if (strcmp(opt, "1") == 0) {
|
|
|
|
config->utf8_mode = 1;
|
|
|
|
}
|
|
|
|
else if (strcmp(opt, "0") == 0) {
|
|
|
|
config->utf8_mode = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return _Py_INIT_USER_ERR("invalid PYTHONUTF8 environment "
|
|
|
|
"variable value");
|
|
|
|
}
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
preconfig_init_locale(_PyPreConfig *config)
|
|
|
|
{
|
2019-03-25 16:54:58 +00:00
|
|
|
/* The C locale enables the C locale coercion (PEP 538) */
|
|
|
|
if (_Py_LegacyLocaleDetected()) {
|
|
|
|
config->coerce_c_locale = 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
config->coerce_c_locale = 0;
|
2019-03-05 11:32:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static _PyInitError
|
2019-03-25 16:54:58 +00:00
|
|
|
preconfig_read(_PyPreConfig *config, _PyPreCmdline *cmdline)
|
2019-03-05 01:01:27 +00:00
|
|
|
{
|
2019-03-25 16:54:58 +00:00
|
|
|
_PyInitError err;
|
|
|
|
|
|
|
|
err = _PyPreCmdline_Read(cmdline);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
_PyPreCmdline_SetPreConfig(cmdline, config);
|
|
|
|
|
2019-03-05 01:01:27 +00:00
|
|
|
_PyPreConfig_GetGlobalConfig(config);
|
|
|
|
|
2019-03-05 16:37:44 +00:00
|
|
|
/* isolated and use_environment */
|
2019-03-05 01:01:27 +00:00
|
|
|
if (config->isolated > 0) {
|
|
|
|
config->use_environment = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
if (config->use_environment < 0) {
|
|
|
|
config->use_environment = 0;
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:37:44 +00:00
|
|
|
/* legacy_windows_fs_encoding, utf8_mode, coerce_c_locale */
|
2019-03-05 11:32:09 +00:00
|
|
|
if (config->use_environment) {
|
|
|
|
#ifdef MS_WINDOWS
|
2019-03-25 23:03:15 +00:00
|
|
|
_Py_get_env_flag(config->use_environment,
|
|
|
|
&config->legacy_windows_fs_encoding,
|
|
|
|
"PYTHONLEGACYWINDOWSFSENCODING");
|
2019-03-05 11:32:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
const char *env = _PyPreConfig_GetEnv(config, "PYTHONCOERCECLOCALE");
|
|
|
|
if (env) {
|
|
|
|
if (strcmp(env, "0") == 0) {
|
|
|
|
if (config->coerce_c_locale < 0) {
|
|
|
|
config->coerce_c_locale = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(env, "warn") == 0) {
|
|
|
|
config->coerce_c_locale_warn = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (config->coerce_c_locale < 0) {
|
|
|
|
config->coerce_c_locale = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
if (config->legacy_windows_fs_encoding) {
|
|
|
|
config->utf8_mode = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (config->utf8_mode < 0) {
|
2019-03-25 16:54:58 +00:00
|
|
|
err = preconfig_init_utf8_mode(config, cmdline);
|
2019-03-05 11:32:09 +00:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 16:54:58 +00:00
|
|
|
/* Test also if coerce_c_locale equals 1: PYTHONCOERCECLOCALE=1 doesn't
|
|
|
|
imply that the C locale is always coerced. It is only coerced if
|
|
|
|
if the LC_CTYPE locale is "C". */
|
|
|
|
if (config->coerce_c_locale != 0 && config->coerce_c_locale != 2) {
|
2019-03-05 11:32:09 +00:00
|
|
|
preconfig_init_locale(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
if (config->utf8_mode < 0) {
|
|
|
|
/* The C locale and the POSIX locale enable the UTF-8 Mode (PEP 540) */
|
|
|
|
const char *ctype_loc = setlocale(LC_CTYPE, NULL);
|
|
|
|
if (ctype_loc != NULL
|
|
|
|
&& (strcmp(ctype_loc, "C") == 0
|
|
|
|
|| strcmp(ctype_loc, "POSIX") == 0))
|
|
|
|
{
|
|
|
|
config->utf8_mode = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (config->coerce_c_locale < 0) {
|
|
|
|
config->coerce_c_locale = 0;
|
|
|
|
}
|
|
|
|
if (config->utf8_mode < 0) {
|
|
|
|
config->utf8_mode = 0;
|
|
|
|
}
|
2019-03-05 16:37:44 +00:00
|
|
|
if (config->coerce_c_locale < 0) {
|
|
|
|
config->coerce_c_locale = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dev_mode */
|
2019-03-15 14:08:05 +00:00
|
|
|
if ((cmdline && _Py_get_xoption(&cmdline->xoptions, L"dev"))
|
2019-03-05 16:37:44 +00:00
|
|
|
|| _PyPreConfig_GetEnv(config, "PYTHONDEVMODE"))
|
|
|
|
{
|
|
|
|
config->dev_mode = 1;
|
|
|
|
}
|
|
|
|
if (config->dev_mode < 0) {
|
|
|
|
config->dev_mode = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocator */
|
|
|
|
if (config->allocator == NULL) {
|
2019-03-06 11:51:53 +00:00
|
|
|
/* bpo-34247. The PYTHONMALLOC environment variable has the priority
|
|
|
|
over PYTHONDEV env var and "-X dev" command line option.
|
|
|
|
For example, PYTHONMALLOC=malloc PYTHONDEVMODE=1 sets the memory
|
|
|
|
allocators to "malloc" (and not to "debug"). */
|
2019-03-05 16:37:44 +00:00
|
|
|
const char *allocator = _PyPreConfig_GetEnv(config, "PYTHONMALLOC");
|
|
|
|
if (allocator) {
|
|
|
|
config->allocator = _PyMem_RawStrdup(allocator);
|
|
|
|
if (config->allocator == NULL) {
|
|
|
|
return _Py_INIT_NO_MEMORY();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 11:32:09 +00:00
|
|
|
|
2019-03-06 11:51:53 +00:00
|
|
|
if (config->dev_mode && config->allocator == NULL) {
|
|
|
|
config->allocator = _PyMem_RawStrdup("debug");
|
|
|
|
if (config->allocator == NULL) {
|
|
|
|
return _Py_INIT_NO_MEMORY();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 11:32:09 +00:00
|
|
|
assert(config->coerce_c_locale >= 0);
|
|
|
|
assert(config->utf8_mode >= 0);
|
2019-03-05 01:44:12 +00:00
|
|
|
assert(config->isolated >= 0);
|
2019-03-05 01:01:27 +00:00
|
|
|
assert(config->use_environment >= 0);
|
2019-03-05 16:37:44 +00:00
|
|
|
assert(config->dev_mode >= 0);
|
2019-03-05 01:01:27 +00:00
|
|
|
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-06 00:44:31 +00:00
|
|
|
static _PyInitError
|
|
|
|
get_ctype_locale(char **locale_p)
|
|
|
|
{
|
|
|
|
const char *loc = setlocale(LC_CTYPE, NULL);
|
|
|
|
if (loc == NULL) {
|
|
|
|
return _Py_INIT_ERR("failed to LC_CTYPE locale");
|
|
|
|
}
|
|
|
|
|
|
|
|
char *copy = _PyMem_RawStrdup(loc);
|
|
|
|
if (copy == NULL) {
|
|
|
|
return _Py_INIT_NO_MEMORY();
|
|
|
|
}
|
|
|
|
|
|
|
|
*locale_p = copy;
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-20 03:25:38 +00:00
|
|
|
void
|
|
|
|
_PyPreCmdline_SetPreConfig(const _PyPreCmdline *cmdline, _PyPreConfig *config)
|
|
|
|
{
|
|
|
|
#define COPY_ATTR(ATTR) \
|
|
|
|
if (cmdline->ATTR != -1) { \
|
|
|
|
config->ATTR = cmdline->ATTR; \
|
|
|
|
}
|
|
|
|
|
|
|
|
COPY_ATTR(use_environment);
|
|
|
|
COPY_ATTR(isolated);
|
|
|
|
|
|
|
|
#undef COPY_ATTR
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 22:19:57 +00:00
|
|
|
PyObject*
|
|
|
|
_PyPreConfig_AsDict(const _PyPreConfig *config)
|
2019-03-05 01:01:27 +00:00
|
|
|
{
|
2019-03-25 22:19:57 +00:00
|
|
|
PyObject *dict;
|
|
|
|
|
|
|
|
dict = PyDict_New();
|
|
|
|
if (dict == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:01:27 +00:00
|
|
|
#define SET_ITEM(KEY, EXPR) \
|
|
|
|
do { \
|
|
|
|
PyObject *obj = (EXPR); \
|
|
|
|
if (obj == NULL) { \
|
|
|
|
goto fail; \
|
|
|
|
} \
|
|
|
|
int res = PyDict_SetItemString(dict, (KEY), obj); \
|
|
|
|
Py_DECREF(obj); \
|
|
|
|
if (res < 0) { \
|
|
|
|
goto fail; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define SET_ITEM_INT(ATTR) \
|
|
|
|
SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR))
|
2019-03-05 16:37:44 +00:00
|
|
|
#define FROM_STRING(STR) \
|
|
|
|
((STR != NULL) ? \
|
|
|
|
PyUnicode_FromString(STR) \
|
|
|
|
: (Py_INCREF(Py_None), Py_None))
|
|
|
|
#define SET_ITEM_STR(ATTR) \
|
|
|
|
SET_ITEM(#ATTR, FROM_STRING(config->ATTR))
|
2019-03-05 01:01:27 +00:00
|
|
|
|
|
|
|
SET_ITEM_INT(isolated);
|
|
|
|
SET_ITEM_INT(use_environment);
|
2019-03-05 11:32:09 +00:00
|
|
|
SET_ITEM_INT(coerce_c_locale);
|
|
|
|
SET_ITEM_INT(coerce_c_locale_warn);
|
|
|
|
SET_ITEM_INT(utf8_mode);
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
SET_ITEM_INT(legacy_windows_fs_encoding);
|
|
|
|
#endif
|
2019-03-05 16:37:44 +00:00
|
|
|
SET_ITEM_INT(dev_mode);
|
|
|
|
SET_ITEM_STR(allocator);
|
2019-03-25 22:19:57 +00:00
|
|
|
return dict;
|
2019-03-05 01:01:27 +00:00
|
|
|
|
|
|
|
fail:
|
2019-03-25 22:19:57 +00:00
|
|
|
Py_DECREF(dict);
|
|
|
|
return NULL;
|
2019-03-05 01:01:27 +00:00
|
|
|
|
2019-03-05 16:37:44 +00:00
|
|
|
#undef FROM_STRING
|
2019-03-05 01:01:27 +00:00
|
|
|
#undef SET_ITEM
|
|
|
|
#undef SET_ITEM_INT
|
2019-03-05 16:37:44 +00:00
|
|
|
#undef SET_ITEM_STR
|
2019-03-05 01:01:27 +00:00
|
|
|
}
|
2019-03-05 01:44:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Parse the command line arguments */
|
2019-03-20 03:25:38 +00:00
|
|
|
_PyInitError
|
|
|
|
_PyPreCmdline_Read(_PyPreCmdline *cmdline)
|
2019-03-05 01:44:12 +00:00
|
|
|
{
|
2019-03-20 03:25:38 +00:00
|
|
|
_PyWstrList *argv = &cmdline->argv;
|
|
|
|
|
2019-03-05 01:44:12 +00:00
|
|
|
_PyOS_ResetGetOpt();
|
|
|
|
/* Don't log parsing errors into stderr here: _PyCoreConfig_ReadFromArgv()
|
|
|
|
is responsible for that */
|
|
|
|
_PyOS_opterr = 0;
|
|
|
|
do {
|
|
|
|
int longindex = -1;
|
2019-03-20 03:25:38 +00:00
|
|
|
int c = _PyOS_GetOpt(argv->length, argv->items, &longindex);
|
2019-03-05 01:44:12 +00:00
|
|
|
|
|
|
|
if (c == EOF || c == 'c' || c == 'm') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'E':
|
2019-03-20 03:25:38 +00:00
|
|
|
cmdline->use_environment = 0;
|
2019-03-05 01:44:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'I':
|
2019-03-20 03:25:38 +00:00
|
|
|
cmdline->isolated = 1;
|
2019-03-05 01:44:12 +00:00
|
|
|
break;
|
|
|
|
|
2019-03-05 11:32:09 +00:00
|
|
|
case 'X':
|
|
|
|
{
|
2019-03-15 14:08:05 +00:00
|
|
|
if (_PyWstrList_Append(&cmdline->xoptions, _PyOS_optarg) < 0) {
|
|
|
|
return _Py_INIT_NO_MEMORY();
|
2019-03-05 11:32:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:44:12 +00:00
|
|
|
default:
|
|
|
|
/* ignore other argument:
|
|
|
|
handled by _PyCoreConfig_ReadFromArgv() */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 16:54:58 +00:00
|
|
|
/* Read the configuration from:
|
|
|
|
|
|
|
|
- environment variables
|
|
|
|
- Py_xxx global configuration variables
|
|
|
|
- the LC_CTYPE locale
|
|
|
|
|
|
|
|
See _PyPreConfig_ReadFromArgv() to parse also command line arguments. */
|
|
|
|
_PyInitError
|
2019-03-25 17:37:10 +00:00
|
|
|
_PyPreConfig_Read(_PyPreConfig *config, const _PyArgv *args,
|
|
|
|
const _PyCoreConfig *coreconfig)
|
2019-03-05 01:44:12 +00:00
|
|
|
{
|
|
|
|
_PyInitError err;
|
2019-03-20 03:25:38 +00:00
|
|
|
_PyPreCmdline cmdline = _PyPreCmdline_INIT;
|
2019-03-25 16:54:58 +00:00
|
|
|
char *old_loc = NULL;
|
2019-03-05 01:44:12 +00:00
|
|
|
|
2019-03-25 16:54:58 +00:00
|
|
|
err = get_ctype_locale(&old_loc);
|
2019-03-05 01:44:12 +00:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-03-25 16:54:58 +00:00
|
|
|
/* Set LC_CTYPE to the user preferred locale */
|
|
|
|
_Py_SetLocaleFromEnv(LC_CTYPE);
|
2019-03-05 01:44:12 +00:00
|
|
|
|
2019-03-25 17:37:10 +00:00
|
|
|
_PyPreConfig_GetGlobalConfig(config);
|
|
|
|
|
2019-03-25 16:54:58 +00:00
|
|
|
_PyPreCmdline_GetPreConfig(&cmdline, config);
|
2019-03-20 03:25:38 +00:00
|
|
|
|
2019-03-25 17:37:10 +00:00
|
|
|
if (coreconfig) {
|
|
|
|
_PyPreCmdline_GetCoreConfig(&cmdline, coreconfig);
|
|
|
|
if (config->dev_mode == -1) {
|
|
|
|
config->dev_mode = coreconfig->preconfig.dev_mode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 16:54:58 +00:00
|
|
|
if (args) {
|
|
|
|
err = _PyPreCmdline_SetArgv(&cmdline, args);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
goto done;
|
|
|
|
}
|
2019-03-05 01:44:12 +00:00
|
|
|
}
|
2019-03-25 16:54:58 +00:00
|
|
|
|
|
|
|
err = preconfig_read(config, &cmdline);
|
2019-03-05 01:44:12 +00:00
|
|
|
|
|
|
|
done:
|
2019-03-25 16:54:58 +00:00
|
|
|
if (old_loc != NULL) {
|
|
|
|
setlocale(LC_CTYPE, old_loc);
|
|
|
|
PyMem_RawFree(old_loc);
|
|
|
|
}
|
2019-03-20 03:25:38 +00:00
|
|
|
_PyPreCmdline_Clear(&cmdline);
|
2019-03-25 16:54:58 +00:00
|
|
|
|
2019-03-05 01:44:12 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-06 00:44:31 +00:00
|
|
|
/* Read the configuration from:
|
|
|
|
|
|
|
|
- command line arguments
|
|
|
|
- environment variables
|
|
|
|
- Py_xxx global configuration variables
|
|
|
|
- the LC_CTYPE locale
|
|
|
|
|
|
|
|
See _PyPreConfig_ReadFromArgv() to parse also command line arguments. */
|
2019-03-05 11:32:09 +00:00
|
|
|
_PyInitError
|
|
|
|
_PyPreConfig_ReadFromArgv(_PyPreConfig *config, const _PyArgv *args)
|
|
|
|
{
|
|
|
|
_PyInitError err;
|
|
|
|
|
|
|
|
err = _PyRuntime_Initialize();
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *init_ctype_locale = NULL;
|
|
|
|
int init_utf8_mode = Py_UTF8Mode;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
int init_legacy_encoding = Py_LegacyWindowsFSEncodingFlag;
|
|
|
|
#endif
|
|
|
|
_PyPreConfig save_config = _PyPreConfig_INIT;
|
|
|
|
int locale_coerced = 0;
|
|
|
|
int loops = 0;
|
|
|
|
|
2019-03-06 00:44:31 +00:00
|
|
|
err = get_ctype_locale(&init_ctype_locale);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2019-03-05 11:32:09 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-03-20 01:20:13 +00:00
|
|
|
_PyPreConfig_GetGlobalConfig(config);
|
|
|
|
|
2019-03-05 11:32:09 +00:00
|
|
|
if (_PyPreConfig_Copy(&save_config, config) < 0) {
|
|
|
|
err = _Py_INIT_NO_MEMORY();
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set LC_CTYPE to the user preferred locale */
|
|
|
|
_Py_SetLocaleFromEnv(LC_CTYPE);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int utf8_mode = config->utf8_mode;
|
|
|
|
|
|
|
|
/* Watchdog to prevent an infinite loop */
|
|
|
|
loops++;
|
|
|
|
if (loops == 3) {
|
|
|
|
err = _Py_INIT_ERR("Encoding changed twice while "
|
|
|
|
"reading the configuration");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
|
|
|
|
on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */
|
|
|
|
Py_UTF8Mode = config->utf8_mode;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
|
|
|
|
#endif
|
|
|
|
|
2019-03-25 17:37:10 +00:00
|
|
|
err = _PyPreConfig_Read(config, args, NULL);
|
2019-03-05 11:32:09 +00:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The legacy C locale assumes ASCII as the default text encoding, which
|
|
|
|
* causes problems not only for the CPython runtime, but also other
|
|
|
|
* components like GNU readline.
|
|
|
|
*
|
|
|
|
* Accordingly, when the CLI detects it, it attempts to coerce it to a
|
|
|
|
* more capable UTF-8 based alternative.
|
|
|
|
*
|
|
|
|
* See the documentation of the PYTHONCOERCECLOCALE setting for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
int encoding_changed = 0;
|
|
|
|
if (config->coerce_c_locale && !locale_coerced) {
|
|
|
|
locale_coerced = 1;
|
|
|
|
_Py_CoerceLegacyLocale(0);
|
|
|
|
encoding_changed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (utf8_mode == -1) {
|
|
|
|
if (config->utf8_mode == 1) {
|
|
|
|
/* UTF-8 Mode enabled */
|
|
|
|
encoding_changed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (config->utf8_mode != utf8_mode) {
|
|
|
|
encoding_changed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!encoding_changed) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the configuration before reading again the configuration,
|
|
|
|
just keep UTF-8 Mode value. */
|
|
|
|
int new_utf8_mode = config->utf8_mode;
|
|
|
|
int new_coerce_c_locale = config->coerce_c_locale;
|
|
|
|
if (_PyPreConfig_Copy(config, &save_config) < 0) {
|
|
|
|
err = _Py_INIT_NO_MEMORY();
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
config->utf8_mode = new_utf8_mode;
|
|
|
|
config->coerce_c_locale = new_coerce_c_locale;
|
|
|
|
|
|
|
|
/* The encoding changed: read again the configuration
|
|
|
|
with the new encoding */
|
|
|
|
}
|
|
|
|
err = _Py_INIT_OK();
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (init_ctype_locale != NULL) {
|
|
|
|
setlocale(LC_CTYPE, init_ctype_locale);
|
2019-03-18 21:24:28 +00:00
|
|
|
PyMem_RawFree(init_ctype_locale);
|
2019-03-05 11:32:09 +00:00
|
|
|
}
|
|
|
|
_PyPreConfig_Clear(&save_config);
|
|
|
|
Py_UTF8Mode = init_utf8_mode ;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
Py_LegacyWindowsFSEncodingFlag = init_legacy_encoding;
|
|
|
|
#endif
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-05 23:36:56 +00:00
|
|
|
static _PyInitError
|
2019-03-06 00:13:43 +00:00
|
|
|
_PyPreConfig_SetAllocator(_PyPreConfig *config)
|
2019-03-05 23:36:56 +00:00
|
|
|
{
|
2019-03-06 00:13:43 +00:00
|
|
|
assert(!_PyRuntime.core_initialized);
|
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
if (_PyMem_SetupAllocators(config->allocator) < 0) {
|
|
|
|
return _Py_INIT_USER_ERR("Unknown PYTHONMALLOC allocator");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the pre-configuration with the new allocator */
|
|
|
|
_PyPreConfig config2 = _PyPreConfig_INIT;
|
|
|
|
if (_PyPreConfig_Copy(&config2, config) < 0) {
|
|
|
|
_PyPreConfig_Clear(&config2);
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
return _Py_INIT_NO_MEMORY();
|
2019-03-05 23:36:56 +00:00
|
|
|
}
|
2019-03-06 00:13:43 +00:00
|
|
|
|
|
|
|
/* Free the old config and replace config with config2. Since config now
|
|
|
|
owns the data, don't free config2. */
|
|
|
|
PyMemAllocatorEx new_alloc;
|
|
|
|
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &new_alloc);
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
_PyPreConfig_Clear(config);
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &new_alloc);
|
|
|
|
|
|
|
|
*config = config2;
|
|
|
|
|
2019-03-05 23:36:56 +00:00
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-06 00:44:31 +00:00
|
|
|
/* Write the pre-configuration:
|
|
|
|
|
|
|
|
- set the memory allocators
|
|
|
|
- set Py_xxx global configuration variables
|
|
|
|
- set the LC_CTYPE locale (coerce C locale, PEP 538) and set the UTF-8 mode
|
|
|
|
(PEP 540)
|
2019-03-06 00:13:43 +00:00
|
|
|
|
|
|
|
If the memory allocator is changed, config is re-allocated with new
|
2019-03-06 00:44:31 +00:00
|
|
|
allocator. So calling _PyPreConfig_Clear(config) is safe after this call.
|
|
|
|
|
|
|
|
Do nothing if called after Py_Initialize(): ignore the new
|
|
|
|
pre-configuration. */
|
2019-03-05 23:36:56 +00:00
|
|
|
_PyInitError
|
2019-03-06 00:13:43 +00:00
|
|
|
_PyPreConfig_Write(_PyPreConfig *config)
|
2019-03-05 01:44:12 +00:00
|
|
|
{
|
2019-03-05 23:36:56 +00:00
|
|
|
if (_PyRuntime.core_initialized) {
|
2019-03-06 00:44:31 +00:00
|
|
|
/* bpo-34008: Calling this functions after Py_Initialize() ignores
|
2019-03-05 23:36:56 +00:00
|
|
|
the new configuration. */
|
2019-03-06 00:13:43 +00:00
|
|
|
return _Py_INIT_OK();
|
2019-03-05 23:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (config->allocator != NULL) {
|
2019-03-06 00:13:43 +00:00
|
|
|
_PyInitError err = _PyPreConfig_SetAllocator(config);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
2019-03-05 23:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 11:32:09 +00:00
|
|
|
_PyPreConfig_SetGlobalConfig(config);
|
|
|
|
|
|
|
|
if (config->coerce_c_locale) {
|
|
|
|
_Py_CoerceLegacyLocale(config->coerce_c_locale_warn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set LC_CTYPE to the user preferred locale */
|
|
|
|
_Py_SetLocaleFromEnv(LC_CTYPE);
|
2019-03-05 23:36:56 +00:00
|
|
|
|
2019-03-23 11:05:43 +00:00
|
|
|
/* Write the new pre-configuration into _PyRuntime */
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
int res = _PyPreConfig_Copy(&_PyRuntime.preconfig, config);
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
if (res < 0) {
|
|
|
|
return _Py_INIT_NO_MEMORY();
|
|
|
|
}
|
|
|
|
|
2019-03-05 23:36:56 +00:00
|
|
|
return _Py_INIT_OK();
|
2019-03-05 01:44:12 +00:00
|
|
|
}
|