mirror of
https://github.com/python/cpython.git
synced 2025-10-18 04:38:07 +00:00
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.
This commit is contained in:
parent
f39b674876
commit
09f3a8a124
7 changed files with 97 additions and 39 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue