summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-11-21 01:32:40 (GMT)
committerGitHub <noreply@github.com>2017-11-21 01:32:40 (GMT)
commit09f3a8a1249308a104a89041d82fe99e6c087043 (patch)
tree2d494c186b4aadfb6fe630f8ac9fc7e66e1906f5 /Python
parentf39b674876d2bd47ec7fc106d673b60ff24092ca (diff)
downloadcpython-09f3a8a1249308a104a89041d82fe99e6c087043.zip
cpython-09f3a8a1249308a104a89041d82fe99e6c087043.tar.gz
cpython-09f3a8a1249308a104a89041d82fe99e6c087043.tar.bz2
bpo-32089: Fix warnings filters in dev mode (#4482)
The developer mode (-X dev) now creates all default warnings filters to order filters in the correct order to always show ResourceWarning and make BytesWarning depend on the -b option. Write a functional test to make sure that ResourceWarning is logged twice at the same location in the developer mode. Add a new 'dev_mode' field to _PyCoreConfig.
Diffstat (limited to 'Python')
-rw-r--r--Python/_warnings.c37
1 files changed, 27 insertions, 10 deletions
diff --git a/Python/_warnings.c b/Python/_warnings.c
index 8cfae76..f2110ed 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -1196,11 +1196,19 @@ create_filter(PyObject *category, const char *action)
static PyObject *
init_filters(void)
{
+ PyInterpreterState *interp = PyThreadState_GET()->interp;
+ int dev_mode = interp->core_config.dev_mode;
+
+ Py_ssize_t count = 2;
+ if (dev_mode) {
+ count++;
+ }
#ifndef Py_DEBUG
- PyObject *filters = PyList_New(5);
-#else
- PyObject *filters = PyList_New(2);
+ if (!dev_mode) {
+ count += 3;
+ }
#endif
+ PyObject *filters = PyList_New(count);
unsigned int pos = 0; /* Post-incremented in each use. */
unsigned int x;
const char *bytes_action, *resource_action;
@@ -1209,12 +1217,14 @@ init_filters(void)
return NULL;
#ifndef Py_DEBUG
- PyList_SET_ITEM(filters, pos++,
- create_filter(PyExc_DeprecationWarning, "ignore"));
- PyList_SET_ITEM(filters, pos++,
- create_filter(PyExc_PendingDeprecationWarning, "ignore"));
- PyList_SET_ITEM(filters, pos++,
- create_filter(PyExc_ImportWarning, "ignore"));
+ if (!dev_mode) {
+ PyList_SET_ITEM(filters, pos++,
+ create_filter(PyExc_DeprecationWarning, "ignore"));
+ PyList_SET_ITEM(filters, pos++,
+ create_filter(PyExc_PendingDeprecationWarning, "ignore"));
+ PyList_SET_ITEM(filters, pos++,
+ create_filter(PyExc_ImportWarning, "ignore"));
+ }
#endif
if (Py_BytesWarningFlag > 1)
@@ -1225,14 +1235,21 @@ init_filters(void)
bytes_action = "ignore";
PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
bytes_action));
+
/* resource usage warnings are enabled by default in pydebug mode */
#ifdef Py_DEBUG
resource_action = "always";
#else
- resource_action = "ignore";
+ resource_action = (dev_mode ? "always" : "ignore");
#endif
PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
resource_action));
+
+ if (dev_mode) {
+ PyList_SET_ITEM(filters, pos++,
+ create_filter(PyExc_Warning, "default"));
+ }
+
for (x = 0; x < pos; x += 1) {
if (PyList_GET_ITEM(filters, x) == NULL) {
Py_DECREF(filters);