mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
bpo-32230: Set sys.warnoptions with -X dev (#4820)
Rather than supporting dev mode directly in the warnings module, this instead adjusts the initialisation code to add an extra 'default' entry to sys.warnoptions when dev mode is enabled. This ensures that dev mode behaves *exactly* as if `-Wdefault` had been passed on the command line, including in the way it interacts with `sys.warnoptions`, and with other command line flags like `-bb`. Fix also bpo-20361: have -b & -bb options take precedence over any other warnings options. Patch written by Nick Coghlan, with minor modifications of Victor Stinner.
This commit is contained in:
parent
b748e3b258
commit
747f48e2e9
12 changed files with 224 additions and 124 deletions
|
@ -774,11 +774,73 @@ pymain_add_warnings_optlist(_Py_OptList *warnings)
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
pymain_add_warning_dev_mode(_PyCoreConfig *core_config)
|
||||
{
|
||||
if (core_config->dev_mode) {
|
||||
PyObject *option = PyUnicode_FromString("default");
|
||||
if (option == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (_PySys_AddWarnOptionWithError(option)) {
|
||||
Py_DECREF(option);
|
||||
return -1;
|
||||
}
|
||||
Py_DECREF(option);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
pymain_add_warning_bytes_flag(int bytes_warning_flag)
|
||||
{
|
||||
/* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
|
||||
* don't even try to emit a warning, so we skip setting the filter in that
|
||||
* case.
|
||||
*/
|
||||
if (bytes_warning_flag) {
|
||||
const char *filter = (bytes_warning_flag > 1) ? "error::BytesWarning":
|
||||
"default::BytesWarning";
|
||||
PyObject *option = PyUnicode_FromString(filter);
|
||||
if (option == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (_PySys_AddWarnOptionWithError(option)) {
|
||||
Py_DECREF(option);
|
||||
return -1;
|
||||
}
|
||||
Py_DECREF(option);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
pymain_add_warnings_options(_PyMain *pymain)
|
||||
{
|
||||
PySys_ResetWarnOptions();
|
||||
|
||||
/* The priority order for warnings configuration is (highest precedence
|
||||
* first):
|
||||
*
|
||||
* - the BytesWarning filter, if needed ('-b', '-bb')
|
||||
* - any '-W' command line options; then
|
||||
* - the 'PYTHONWARNINGS' environment variable; then
|
||||
* - the dev mode filter ('-X dev', 'PYTHONDEVMODE'); then
|
||||
* - any implicit filters added by _warnings.c/warnings.py
|
||||
*
|
||||
* All settings except the last are passed to the warnings module via
|
||||
* the `sys.warnoptions` list. Since the warnings module works on the basis
|
||||
* of "the most recently added filter will be checked first", we add
|
||||
* the lowest precedence entries first so that later entries override them.
|
||||
*/
|
||||
|
||||
if (pymain_add_warning_dev_mode(&pymain->core_config) < 0) {
|
||||
pymain->err = _Py_INIT_NO_MEMORY();
|
||||
return -1;
|
||||
}
|
||||
if (pymain_add_warnings_optlist(&pymain->env_warning_options) < 0) {
|
||||
pymain->err = _Py_INIT_NO_MEMORY();
|
||||
return -1;
|
||||
|
@ -787,6 +849,10 @@ pymain_add_warnings_options(_PyMain *pymain)
|
|||
pymain->err = _Py_INIT_NO_MEMORY();
|
||||
return -1;
|
||||
}
|
||||
if (pymain_add_warning_bytes_flag(pymain->cmdline.bytes_warning) < 0) {
|
||||
pymain->err = _Py_INIT_NO_MEMORY();
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue