mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-31416: Fix assertion failures in case of a bad warnings.filters or warnings.defaultaction. (#3496)
Patch by Oren Milman.
This commit is contained in:
parent
8239fd7046
commit
9d984fd2b0
3 changed files with 33 additions and 3 deletions
|
@ -805,6 +805,21 @@ class _WarningsTests(BaseTest, unittest.TestCase):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
wmod.warn_explicit('foo', Warning, 'bar', 1, registry=None)
|
wmod.warn_explicit('foo', Warning, 'bar', 1, registry=None)
|
||||||
|
|
||||||
|
@support.cpython_only
|
||||||
|
def test_issue31416(self):
|
||||||
|
# warn_explicit() shouldn't cause an assertion failure in case of a
|
||||||
|
# bad warnings.filters or warnings.defaultaction.
|
||||||
|
wmod = self.module
|
||||||
|
with original_warnings.catch_warnings(module=wmod):
|
||||||
|
wmod.filters = [(None, None, Warning, None, 0)]
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
wmod.warn_explicit('foo', Warning, 'bar', 1)
|
||||||
|
|
||||||
|
wmod.filters = []
|
||||||
|
with support.swap_attr(wmod, 'defaultaction', None), \
|
||||||
|
self.assertRaises(TypeError):
|
||||||
|
wmod.warn_explicit('foo', Warning, 'bar', 1)
|
||||||
|
|
||||||
|
|
||||||
class WarningsDisplayTests(BaseTest):
|
class WarningsDisplayTests(BaseTest):
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix assertion failures in case of a bad warnings.filters or
|
||||||
|
warnings.defaultaction. Patch by Oren Milman.
|
|
@ -110,7 +110,14 @@ get_default_action(void)
|
||||||
}
|
}
|
||||||
return _PyRuntime.warnings.default_action;
|
return _PyRuntime.warnings.default_action;
|
||||||
}
|
}
|
||||||
|
if (!PyUnicode_Check(default_action)) {
|
||||||
|
PyErr_Format(PyExc_TypeError,
|
||||||
|
MODULE_NAME ".defaultaction must be a string, "
|
||||||
|
"not '%.200s'",
|
||||||
|
Py_TYPE(default_action)->tp_name);
|
||||||
|
Py_DECREF(default_action);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
Py_DECREF(_PyRuntime.warnings.default_action);
|
Py_DECREF(_PyRuntime.warnings.default_action);
|
||||||
_PyRuntime.warnings.default_action = default_action;
|
_PyRuntime.warnings.default_action = default_action;
|
||||||
return default_action;
|
return default_action;
|
||||||
|
@ -164,6 +171,14 @@ get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
|
||||||
mod = PyTuple_GET_ITEM(tmp_item, 3);
|
mod = PyTuple_GET_ITEM(tmp_item, 3);
|
||||||
ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
|
ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
|
||||||
|
|
||||||
|
if (!PyUnicode_Check(action)) {
|
||||||
|
PyErr_Format(PyExc_TypeError,
|
||||||
|
"action must be a string, not '%.200s'",
|
||||||
|
Py_TYPE(action)->tp_name);
|
||||||
|
Py_DECREF(tmp_item);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
good_msg = check_matched(msg, text);
|
good_msg = check_matched(msg, text);
|
||||||
if (good_msg == -1) {
|
if (good_msg == -1) {
|
||||||
Py_DECREF(tmp_item);
|
Py_DECREF(tmp_item);
|
||||||
|
@ -203,8 +218,6 @@ get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
|
||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyErr_SetString(PyExc_ValueError,
|
|
||||||
MODULE_NAME ".defaultaction not found");
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue