summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2019-08-23 17:03:08 (GMT)
committerGitHub <noreply@github.com>2019-08-23 17:03:08 (GMT)
commit120b707a6d43452e067daa55a8fdca69f9424abc (patch)
tree1a58f651b55fde0a820646fdbf8b568657883273 /Python
parent1beb7c3de9c3d0e802e4267a11d937e8f024d661 (diff)
downloadcpython-120b707a6d43452e067daa55a8fdca69f9424abc.zip
cpython-120b707a6d43452e067daa55a8fdca69f9424abc.tar.gz
cpython-120b707a6d43452e067daa55a8fdca69f9424abc.tar.bz2
bpo-36763: PyConfig_Read() handles PySys_AddXOption() (GH-15431)
PyConfig_Read() is now responsible to handle early calls to PySys_AddXOption() and PySys_AddWarnOption(). Options added by PySys_AddXOption() are now handled the same way than PyConfig.xoptions and command line -X options. For example, PySys_AddXOption(L"faulthandler") enables faulthandler as expected.
Diffstat (limited to 'Python')
-rw-r--r--Python/initconfig.c17
-rw-r--r--Python/sysmodule.c58
2 files changed, 45 insertions, 30 deletions
diff --git a/Python/initconfig.c b/Python/initconfig.c
index b706f4c..87dea5e 100644
--- a/Python/initconfig.c
+++ b/Python/initconfig.c
@@ -2069,6 +2069,7 @@ config_init_warnoptions(PyConfig *config,
/* The priority order for warnings configuration is (highest precedence
* first):
*
+ * - early PySys_AddWarnOption() calls
* - the BytesWarning filter, if needed ('-b', '-bb')
* - any '-W' command line options; then
* - the 'PYTHONWARNINGS' environment variable; then
@@ -2124,6 +2125,13 @@ config_init_warnoptions(PyConfig *config,
return status;
}
}
+
+ /* Handle early PySys_AddWarnOption() calls */
+ status = _PySys_ReadPreinitWarnOptions(config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
return _PyStatus_OK();
}
@@ -2293,7 +2301,8 @@ config_read_cmdline(PyConfig *config)
}
status = config_init_warnoptions(config,
- &cmdline_warnoptions, &env_warnoptions);
+ &cmdline_warnoptions,
+ &env_warnoptions);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
@@ -2403,6 +2412,12 @@ PyConfig_Read(PyConfig *config)
goto done;
}
+ /* Handle early PySys_AddXOption() calls */
+ status = _PySys_ReadPreinitXOptions(config);
+ if (_PyStatus_EXCEPTION(status)) {
+ goto done;
+ }
+
status = config_read(config);
if (_PyStatus_EXCEPTION(status)) {
goto done;
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 738bbc8..0635e9d 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -2037,36 +2037,43 @@ _clear_preinit_entries(_Py_PreInitEntry *optionlist)
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
}
-static void
-_clear_all_preinit_options(void)
+
+PyStatus
+_PySys_ReadPreinitWarnOptions(PyConfig *config)
{
+ PyStatus status;
+ _Py_PreInitEntry entry;
+
+ for (entry = _preinit_warnoptions; entry != NULL; entry = entry->next) {
+ status = PyWideStringList_Append(&config->warnoptions, entry->value);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ }
+
_clear_preinit_entries(&_preinit_warnoptions);
- _clear_preinit_entries(&_preinit_xoptions);
+ return _PyStatus_OK();
}
-static int
-sys_read_preinit_options(PyThreadState *tstate)
+
+PyStatus
+_PySys_ReadPreinitXOptions(PyConfig *config)
{
- /* Rerun the add commands with the actual sys module available */
- if (tstate == NULL) {
- /* Still don't have a thread state, so something is wrong! */
- return -1;
- }
- _Py_PreInitEntry entry = _preinit_warnoptions;
- while (entry != NULL) {
- PySys_AddWarnOption(entry->value);
- entry = entry->next;
- }
- entry = _preinit_xoptions;
- while (entry != NULL) {
- PySys_AddXOption(entry->value);
- entry = entry->next;
+ PyStatus status;
+ _Py_PreInitEntry entry;
+
+ for (entry = _preinit_xoptions; entry != NULL; entry = entry->next) {
+ status = PyWideStringList_Append(&config->xoptions, entry->value);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
}
- _clear_all_preinit_options();
- return 0;
+ _clear_preinit_entries(&_preinit_xoptions);
+ return _PyStatus_OK();
}
+
static PyObject *
get_warnoptions(PyThreadState *tstate)
{
@@ -2235,9 +2242,7 @@ PySys_AddXOption(const wchar_t *s)
}
if (_PySys_AddXOptionWithError(s) < 0) {
/* No return value, therefore clear error state if possible */
- if (tstate) {
- _PyErr_Clear(tstate);
- }
+ _PyErr_Clear(tstate);
}
}
@@ -2898,11 +2903,6 @@ _PySys_InitMain(_PyRuntimeState *runtime, PyThreadState *tstate)
if (get_xoptions(tstate) == NULL)
return -1;
- /* Transfer any sys.warnoptions and sys._xoptions set directly
- * by an embedding application from the linked list to the module. */
- if (sys_read_preinit_options(tstate) != 0)
- return -1;
-
if (_PyErr_Occurred(tstate)) {
goto err_occurred;
}