Commit 004547f9 authored by Serhiy Storchaka's avatar Serhiy Storchaka Committed by GitHub

[2.7] bpo-31411: Prevent raising a SystemError in case warnings.onceregistry...

[2.7] bpo-31411: Prevent raising a SystemError in case warnings.onceregistry is not a dictionary. (GH-3485). (#3493)

(cherry picked from commit 252033d5)
parent 6ed7aff8
......@@ -584,6 +584,17 @@ class _WarningsTests(BaseTest):
self.assertNotIn(b'Warning!', stderr)
self.assertNotIn(b'Error', stderr)
@test_support.cpython_only
def test_issue31411(self):
# warn_explicit() shouldn't raise a SystemError in case
# warnings.onceregistry isn't a dictionary.
wmod = self.module
with original_warnings.catch_warnings(module=wmod):
wmod.filterwarnings('once')
with test_support.swap_attr(wmod, 'onceregistry', None):
with self.assertRaises(TypeError):
wmod.warn_explicit('foo', Warning, 'bar', 1, registry=None)
class WarningsDisplayTests(unittest.TestCase):
......
Raise a TypeError instead of SystemError in case warnings.onceregistry is
not a dictionary. Patch by Oren Milman.
......@@ -72,6 +72,12 @@ get_once_registry(void)
return NULL;
return _once_registry;
}
if (!PyDict_Check(registry)) {
PyErr_SetString(PyExc_TypeError,
"warnings.onceregistry must be a dict");
Py_DECREF(registry);
return NULL;
}
Py_DECREF(_once_registry);
_once_registry = registry;
return registry;
......@@ -296,7 +302,7 @@ warn_explicit(PyObject *category, PyObject *message,
int rc;
if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
return NULL;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment