summaryrefslogtreecommitdiff
path: root/Python/initconfig.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/initconfig.c')
-rw-r--r--Python/initconfig.c455
1 files changed, 384 insertions, 71 deletions
diff --git a/Python/initconfig.c b/Python/initconfig.c
index 7bb28ed01f..62087fb420 100644
--- a/Python/initconfig.c
+++ b/Python/initconfig.c
@@ -38,7 +38,8 @@ Options and arguments (and corresponding environment variables):\n\
and comparing bytes/bytearray with str. (-bb: issue errors)\n\
-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
-c cmd : program passed in as string (terminates option list)\n\
--d : debug output from parser; also PYTHONDEBUG=x\n\
+-d : turn on parser debugging output (for experts only, only works on\n\
+ debug builds); also PYTHONDEBUG=x\n\
-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
-h : print this help message and exit (also --help)\n\
";
@@ -242,8 +243,9 @@ PyStatus PyStatus_Ok(void)
PyStatus PyStatus_Error(const char *err_msg)
{
+ assert(err_msg != NULL);
return (PyStatus){._type = _PyStatus_TYPE_ERROR,
- .err_msg = err_msg};
+ .err_msg = err_msg};
}
PyStatus PyStatus_NoMemory(void)
@@ -262,6 +264,23 @@ int PyStatus_IsExit(PyStatus status)
int PyStatus_Exception(PyStatus status)
{ return _PyStatus_EXCEPTION(status); }
+PyObject*
+_PyErr_SetFromPyStatus(PyStatus status)
+{
+ if (!_PyStatus_IS_ERROR(status)) {
+ PyErr_Format(PyExc_SystemError,
+ "%s() expects an error PyStatus",
+ _PyStatus_GET_FUNC());
+ }
+ else if (status.func) {
+ PyErr_Format(PyExc_ValueError, "%s: %s", status.func, status.err_msg);
+ }
+ else {
+ PyErr_Format(PyExc_ValueError, "%s", status.err_msg);
+ }
+ return NULL;
+}
+
/* --- PyWideStringList ------------------------------------------------ */
@@ -559,6 +578,65 @@ Py_GetArgcArgv(int *argc, wchar_t ***argv)
? _PyStatus_ERR("cannot decode " NAME) \
: _PyStatus_NO_MEMORY())
+#define MAX_HASH_SEED 4294967295UL
+
+
+#ifndef NDEBUG
+static int
+config_check_consistency(const PyConfig *config)
+{
+ /* Check config consistency */
+ assert(config->isolated >= 0);
+ assert(config->use_environment >= 0);
+ assert(config->dev_mode >= 0);
+ assert(config->install_signal_handlers >= 0);
+ assert(config->use_hash_seed >= 0);
+ assert(config->hash_seed <= MAX_HASH_SEED);
+ assert(config->faulthandler >= 0);
+ assert(config->tracemalloc >= 0);
+ assert(config->import_time >= 0);
+ assert(config->show_ref_count >= 0);
+ assert(config->dump_refs >= 0);
+ assert(config->malloc_stats >= 0);
+ assert(config->site_import >= 0);
+ assert(config->bytes_warning >= 0);
+ assert(config->inspect >= 0);
+ assert(config->interactive >= 0);
+ assert(config->optimization_level >= 0);
+ assert(config->parser_debug >= 0);
+ assert(config->write_bytecode >= 0);
+ assert(config->verbose >= 0);
+ assert(config->quiet >= 0);
+ assert(config->user_site_directory >= 0);
+ assert(config->parse_argv >= 0);
+ assert(config->configure_c_stdio >= 0);
+ assert(config->buffered_stdio >= 0);
+ assert(config->program_name != NULL);
+ assert(_PyWideStringList_CheckConsistency(&config->orig_argv));
+ assert(_PyWideStringList_CheckConsistency(&config->argv));
+ /* sys.argv must be non-empty: empty argv is replaced with [''] */
+ assert(config->argv.length >= 1);
+ assert(_PyWideStringList_CheckConsistency(&config->xoptions));
+ assert(_PyWideStringList_CheckConsistency(&config->warnoptions));
+ assert(_PyWideStringList_CheckConsistency(&config->module_search_paths));
+ assert(config->module_search_paths_set >= 0);
+ assert(config->platlibdir != NULL);
+ assert(config->filesystem_encoding != NULL);
+ assert(config->filesystem_errors != NULL);
+ assert(config->stdio_encoding != NULL);
+ assert(config->stdio_errors != NULL);
+#ifdef MS_WINDOWS
+ assert(config->legacy_windows_stdio >= 0);
+#endif
+ /* -c and -m options are exclusive */
+ assert(!(config->run_command != NULL && config->run_module != NULL));
+ assert(config->check_hash_pycs_mode != NULL);
+ assert(config->_install_importlib >= 0);
+ assert(config->pathconfig_warnings >= 0);
+ return 1;
+}
+#endif
+
/* Free memory allocated in config, but don't clear all attributes */
void
@@ -862,8 +940,8 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
}
-static PyObject *
-config_as_dict(const PyConfig *config)
+PyObject *
+_PyConfig_AsDict(const PyConfig *config)
{
PyObject *dict = PyDict_New();
if (dict == NULL) {
@@ -918,6 +996,7 @@ config_as_dict(const PyConfig *config)
SET_ITEM_WSTRLIST(warnoptions);
SET_ITEM_WSTR(pythonpath_env);
SET_ITEM_WSTR(home);
+ SET_ITEM_INT(module_search_paths_set);
SET_ITEM_WSTRLIST(module_search_paths);
SET_ITEM_WSTR(executable);
SET_ITEM_WSTR(base_executable);
@@ -969,6 +1048,279 @@ fail:
}
+static PyObject*
+config_dict_get(PyObject *dict, const char *name)
+{
+ PyObject *item = _PyDict_GetItemStringWithError(dict, name);
+ if (item == NULL && !PyErr_Occurred()) {
+ PyErr_Format(PyExc_ValueError, "missing config key: %s", name);
+ return NULL;
+ }
+ return item;
+}
+
+
+static void
+config_dict_invalid_value(const char *name)
+{
+ PyErr_Format(PyExc_ValueError, "invalid config value: %s", name);
+}
+
+
+static void
+config_dict_invalid_type(const char *name)
+{
+ PyErr_Format(PyExc_TypeError, "invalid config type: %s", name);
+}
+
+
+static int
+config_dict_get_int(PyObject *dict, const char *name, int *result)
+{
+ PyObject *item = config_dict_get(dict, name);
+ if (item == NULL) {
+ return -1;
+ }
+ int value = _PyLong_AsInt(item);
+ if (value == -1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError)) {
+ config_dict_invalid_type(name);
+ }
+ else if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ config_dict_invalid_value(name);
+ }
+ return -1;
+ }
+ *result = value;
+ return 0;
+}
+
+
+static int
+config_dict_get_ulong(PyObject *dict, const char *name, unsigned long *result)
+{
+ PyObject *item = config_dict_get(dict, name);
+ if (item == NULL) {
+ return -1;
+ }
+ unsigned long value = PyLong_AsUnsignedLong(item);
+ if (value == (unsigned long)-1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError)) {
+ config_dict_invalid_type(name);
+ }
+ else if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ config_dict_invalid_value(name);
+ }
+ return -1;
+ }
+ *result = value;
+ return 0;
+}
+
+
+static int
+config_dict_get_wstr(PyObject *dict, const char *name, PyConfig *config,
+ wchar_t **result)
+{
+ PyObject *item = config_dict_get(dict, name);
+ if (item == NULL) {
+ return -1;
+ }
+ PyStatus status;
+ if (item == Py_None) {
+ status = PyConfig_SetString(config, result, NULL);
+ }
+ else if (!PyUnicode_Check(item)) {
+ config_dict_invalid_type(name);
+ return -1;
+ }
+ else {
+ wchar_t *wstr = PyUnicode_AsWideCharString(item, NULL);
+ if (wstr == NULL) {
+ return -1;
+ }
+ status = PyConfig_SetString(config, result, wstr);
+ PyMem_Free(wstr);
+ }
+ if (_PyStatus_EXCEPTION(status)) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ return 0;
+}
+
+
+static int
+config_dict_get_wstrlist(PyObject *dict, const char *name, PyConfig *config,
+ PyWideStringList *result)
+{
+ PyObject *list = config_dict_get(dict, name);
+ if (list == NULL) {
+ return -1;
+ }
+
+ if (!PyList_CheckExact(list)) {
+ config_dict_invalid_type(name);
+ return -1;
+ }
+
+ PyWideStringList wstrlist = _PyWideStringList_INIT;
+ for (Py_ssize_t i=0; i < PyList_GET_SIZE(list); i++) {
+ PyObject *item = PyList_GET_ITEM(list, i);
+
+ if (item == Py_None) {
+ config_dict_invalid_value(name);
+ goto error;
+ }
+ else if (!PyUnicode_Check(item)) {
+ config_dict_invalid_type(name);
+ goto error;
+ }
+ wchar_t *wstr = PyUnicode_AsWideCharString(item, NULL);
+ if (wstr == NULL) {
+ goto error;
+ }
+ PyStatus status = PyWideStringList_Append(&wstrlist, wstr);
+ PyMem_Free(wstr);
+ if (_PyStatus_EXCEPTION(status)) {
+ PyErr_NoMemory();
+ goto error;
+ }
+ }
+
+ if (_PyWideStringList_Copy(result, &wstrlist) < 0) {
+ PyErr_NoMemory();
+ goto error;
+ }
+ _PyWideStringList_Clear(&wstrlist);
+ return 0;
+
+error:
+ _PyWideStringList_Clear(&wstrlist);
+ return -1;
+}
+
+
+int
+_PyConfig_FromDict(PyConfig *config, PyObject *dict)
+{
+ if (!PyDict_Check(dict)) {
+ PyErr_SetString(PyExc_TypeError, "dict expected");
+ return -1;
+ }
+
+#define CHECK_VALUE(NAME, TEST) \
+ if (!(TEST)) { \
+ config_dict_invalid_value(NAME); \
+ return -1; \
+ }
+#define GET_UINT(KEY) \
+ do { \
+ if (config_dict_get_int(dict, #KEY, &config->KEY) < 0) { \
+ return -1; \
+ } \
+ CHECK_VALUE(#KEY, config->KEY >= 0); \
+ } while (0)
+#define GET_WSTR(KEY) \
+ do { \
+ if (config_dict_get_wstr(dict, #KEY, config, &config->KEY) < 0) { \
+ return -1; \
+ } \
+ CHECK_VALUE(#KEY, config->KEY != NULL); \
+ } while (0)
+#define GET_WSTR_OPT(KEY) \
+ do { \
+ if (config_dict_get_wstr(dict, #KEY, config, &config->KEY) < 0) { \
+ return -1; \
+ } \
+ } while (0)
+#define GET_WSTRLIST(KEY) \
+ do { \
+ if (config_dict_get_wstrlist(dict, #KEY, config, &config->KEY) < 0) { \
+ return -1; \
+ } \
+ } while (0)
+
+ GET_UINT(_config_init);
+ CHECK_VALUE("_config_init",
+ config->_config_init == _PyConfig_INIT_COMPAT
+ || config->_config_init == _PyConfig_INIT_PYTHON
+ || config->_config_init == _PyConfig_INIT_ISOLATED);
+ GET_UINT(isolated);
+ GET_UINT(use_environment);
+ GET_UINT(dev_mode);
+ GET_UINT(install_signal_handlers);
+ GET_UINT(use_hash_seed);
+ if (config_dict_get_ulong(dict, "hash_seed", &config->hash_seed) < 0) {
+ return -1;
+ }
+ CHECK_VALUE("hash_seed", config->hash_seed <= MAX_HASH_SEED);
+ GET_UINT(faulthandler);
+ GET_UINT(tracemalloc);
+ GET_UINT(import_time);
+ GET_UINT(show_ref_count);
+ GET_UINT(dump_refs);
+ GET_UINT(malloc_stats);
+ GET_WSTR(filesystem_encoding);
+ GET_WSTR(filesystem_errors);
+ GET_WSTR_OPT(pycache_prefix);
+ GET_UINT(parse_argv);
+ GET_WSTRLIST(orig_argv);
+ GET_WSTRLIST(argv);
+ GET_WSTRLIST(xoptions);
+ GET_WSTRLIST(warnoptions);
+ GET_UINT(site_import);
+ GET_UINT(bytes_warning);
+ GET_UINT(inspect);
+ GET_UINT(interactive);
+ GET_UINT(optimization_level);
+ GET_UINT(parser_debug);
+ GET_UINT(write_bytecode);
+ GET_UINT(verbose);
+ GET_UINT(quiet);
+ GET_UINT(user_site_directory);
+ GET_UINT(configure_c_stdio);
+ GET_UINT(buffered_stdio);
+ GET_WSTR(stdio_encoding);
+ GET_WSTR(stdio_errors);
+#ifdef MS_WINDOWS
+ GET_UINT(legacy_windows_stdio);
+#endif
+ GET_WSTR(check_hash_pycs_mode);
+
+ GET_UINT(pathconfig_warnings);
+ GET_WSTR(program_name);
+ GET_WSTR_OPT(pythonpath_env);
+ GET_WSTR_OPT(home);
+ GET_WSTR(platlibdir);
+
+ // Path configuration output
+ GET_UINT(module_search_paths_set);
+ GET_WSTRLIST(module_search_paths);
+ GET_WSTR_OPT(executable);
+ GET_WSTR_OPT(base_executable);
+ GET_WSTR_OPT(prefix);
+ GET_WSTR_OPT(base_prefix);
+ GET_WSTR_OPT(exec_prefix);
+ GET_WSTR_OPT(base_exec_prefix);
+
+ GET_UINT(skip_source_first_line);
+ GET_WSTR_OPT(run_command);
+ GET_WSTR_OPT(run_module);
+ GET_WSTR_OPT(run_filename);
+
+ GET_UINT(_install_importlib);
+ GET_UINT(_init_main);
+ GET_UINT(_isolated_interpreter);
+
+#undef CHECK_VALUE
+#undef GET_UINT
+#undef GET_WSTR
+#undef GET_WSTR_OPT
+ return 0;
+}
+
+
static const char*
config_get_env(const PyConfig *config, const char *name)
{
@@ -1236,7 +1588,6 @@ config_init_home(PyConfig *config)
L"PYTHONHOME", "PYTHONHOME");
}
-
static PyStatus
config_init_hash_seed(PyConfig *config)
{
@@ -1250,7 +1601,7 @@ config_init_hash_seed(PyConfig *config)
errno = 0;
seed = strtoul(seed_text, (char **)&endptr, 10);
if (*endptr != '\0'
- || seed > 4294967295UL
+ || seed > MAX_HASH_SEED
|| (errno == ERANGE && seed == ULONG_MAX))
{
return _PyStatus_ERR("PYTHONHASHSEED must be \"random\" "
@@ -1515,8 +1866,8 @@ config_init_stdio_encoding(PyConfig *config,
{
PyStatus status;
- /* If Py_SetStandardStreamEncoding() have been called, use these
- parameters. */
+ /* If Py_SetStandardStreamEncoding() has been called, use its
+ arguments if they are not NULL. */
if (config->stdio_encoding == NULL && _Py_StandardStreamEncoding != NULL) {
status = CONFIG_SET_BYTES_STR(config, &config->stdio_encoding,
_Py_StandardStreamEncoding,
@@ -1535,6 +1886,7 @@ config_init_stdio_encoding(PyConfig *config,
}
}
+ // Exit if encoding and errors are defined
if (config->stdio_encoding != NULL && config->stdio_errors != NULL) {
return _PyStatus_OK();
}
@@ -1634,12 +1986,12 @@ config_get_fs_encoding(PyConfig *config, const PyPreConfig *preconfig,
if (preconfig->utf8_mode) {
return PyConfig_SetString(config, fs_encoding, L"utf-8");
}
- else if (_Py_GetForceASCII()) {
+
+ if (_Py_GetForceASCII()) {
return PyConfig_SetString(config, fs_encoding, L"ascii");
}
- else {
- return config_get_locale_encoding(config, preconfig, fs_encoding);
- }
+
+ return config_get_locale_encoding(config, preconfig, fs_encoding);
#endif // !MS_WINDOWS
}
@@ -1679,7 +2031,7 @@ config_init_fs_encoding(PyConfig *config, const PyPreConfig *preconfig)
static PyStatus
-config_read(PyConfig *config)
+config_read(PyConfig *config, int compute_path_config)
{
PyStatus status;
const PyPreConfig *preconfig = &_PyRuntime.preconfig;
@@ -1724,7 +2076,7 @@ config_read(PyConfig *config)
}
if (config->_install_importlib) {
- status = _PyConfig_InitPathConfig(config);
+ status = _PyConfig_InitPathConfig(config, compute_path_config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
@@ -1779,6 +2131,11 @@ config_read(PyConfig *config)
config->configure_c_stdio = 1;
}
+ // Only parse arguments once.
+ if (config->parse_argv == 1) {
+ config->parse_argv = 2;
+ }
+
return _PyStatus_OK();
}
@@ -2269,7 +2626,7 @@ core_read_precmdline(PyConfig *config, _PyPreCmdline *precmdline)
{
PyStatus status;
- if (config->parse_argv) {
+ if (config->parse_argv == 1) {
if (_PyWideStringList_Copy(&precmdline->argv, &config->argv) < 0) {
return _PyStatus_NO_MEMORY();
}
@@ -2347,7 +2704,7 @@ config_read_cmdline(PyConfig *config)
}
}
- if (config->parse_argv) {
+ if (config->parse_argv == 1) {
Py_ssize_t opt_index;
status = config_parse_cmdline(config, &cmdline_warnoptions, &opt_index);
if (_PyStatus_EXCEPTION(status)) {
@@ -2465,7 +2822,7 @@ PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list,
The only side effects are to modify config and to call _Py_SetArgcArgv(). */
PyStatus
-PyConfig_Read(PyConfig *config)
+_PyConfig_Read(PyConfig *config, int compute_path_config)
{
PyStatus status;
@@ -2508,63 +2865,12 @@ PyConfig_Read(PyConfig *config)
goto done;
}
- status = config_read(config);
+ status = config_read(config, compute_path_config);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
- /* Check config consistency */
- assert(config->isolated >= 0);
- assert(config->use_environment >= 0);
- assert(config->dev_mode >= 0);
- assert(config->install_signal_handlers >= 0);
- assert(config->use_hash_seed >= 0);
- assert(config->faulthandler >= 0);
- assert(config->tracemalloc >= 0);
- assert(config->site_import >= 0);
- assert(config->bytes_warning >= 0);
- assert(config->inspect >= 0);
- assert(config->interactive >= 0);
- assert(config->optimization_level >= 0);
- assert(config->parser_debug >= 0);
- assert(config->write_bytecode >= 0);
- assert(config->verbose >= 0);
- assert(config->quiet >= 0);
- assert(config->user_site_directory >= 0);
- assert(config->parse_argv >= 0);
- assert(config->configure_c_stdio >= 0);
- assert(config->buffered_stdio >= 0);
- assert(config->program_name != NULL);
- assert(_PyWideStringList_CheckConsistency(&config->argv));
- /* sys.argv must be non-empty: empty argv is replaced with [''] */
- assert(config->argv.length >= 1);
- assert(_PyWideStringList_CheckConsistency(&config->xoptions));
- assert(_PyWideStringList_CheckConsistency(&config->warnoptions));
- assert(_PyWideStringList_CheckConsistency(&config->module_search_paths));
- if (config->_install_importlib) {
- assert(config->module_search_paths_set != 0);
- /* don't check config->module_search_paths */
- assert(config->executable != NULL);
- assert(config->base_executable != NULL);
- assert(config->prefix != NULL);
- assert(config->base_prefix != NULL);
- assert(config->exec_prefix != NULL);
- assert(config->base_exec_prefix != NULL);
- }
- assert(config->platlibdir != NULL);
- assert(config->filesystem_encoding != NULL);
- assert(config->filesystem_errors != NULL);
- assert(config->stdio_encoding != NULL);
- assert(config->stdio_errors != NULL);
-#ifdef MS_WINDOWS
- assert(config->legacy_windows_stdio >= 0);
-#endif
- /* -c and -m options are exclusive */
- assert(!(config->run_command != NULL && config->run_module != NULL));
- assert(config->check_hash_pycs_mode != NULL);
- assert(config->_install_importlib >= 0);
- assert(config->pathconfig_warnings >= 0);
- assert(_PyWideStringList_CheckConsistency(&config->orig_argv));
+ assert(config_check_consistency(config));
status = _PyStatus_OK();
@@ -2574,6 +2880,13 @@ done:
}
+PyStatus
+PyConfig_Read(PyConfig *config)
+{
+ return _PyConfig_Read(config, 1);
+}
+
+
PyObject*
_Py_GetConfigsAsDict(void)
{
@@ -2609,7 +2922,7 @@ _Py_GetConfigsAsDict(void)
/* core config */
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
- dict = config_as_dict(config);
+ dict = _PyConfig_AsDict(config);
if (dict == NULL) {
goto error;
}