summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-12-12 21:59:48 (GMT)
committerGitHub <noreply@github.com>2017-12-12 21:59:48 (GMT)
commit747f48e2e92390c44c72f52a1239959601cde157 (patch)
tree502e53b129aee7a393ca6d05e4f93751919a5e1b /Modules
parentb748e3b2586e44bfc7011b601bce9cc6d16d89f1 (diff)
downloadcpython-747f48e2e92390c44c72f52a1239959601cde157.zip
cpython-747f48e2e92390c44c72f52a1239959601cde157.tar.gz
cpython-747f48e2e92390c44c72f52a1239959601cde157.tar.bz2
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.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/main.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/Modules/main.c b/Modules/main.c
index 68ee616..ac8a38c 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -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;
}