Skip to content

Commit 7972ed2

Browse files
[3.6] bpo-31411: Prevent raising a SystemError in case warnings.onceregistry is not a dictionary. (GH-3485). (#3494)
(cherry picked from commit 252033d)
1 parent c67838d commit 7972ed2

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/test/test_warnings/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,17 @@ def test_stderr_none(self):
794794
self.assertNotIn(b'Warning!', stderr)
795795
self.assertNotIn(b'Error', stderr)
796796

797+
@support.cpython_only
798+
def test_issue31411(self):
799+
# warn_explicit() shouldn't raise a SystemError in case
800+
# warnings.onceregistry isn't a dictionary.
801+
wmod = self.module
802+
with original_warnings.catch_warnings(module=wmod):
803+
wmod.filterwarnings('once')
804+
with support.swap_attr(wmod, 'onceregistry', None):
805+
with self.assertRaises(TypeError):
806+
wmod.warn_explicit('foo', Warning, 'bar', 1, registry=None)
807+
797808

798809
class WarningsDisplayTests(BaseTest):
799810

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise a TypeError instead of SystemError in case warnings.onceregistry is
2+
not a dictionary. Patch by Oren Milman.

Python/_warnings.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ get_once_registry(void)
9494
return NULL;
9595
return _once_registry;
9696
}
97+
if (!PyDict_Check(registry)) {
98+
PyErr_SetString(PyExc_TypeError,
99+
"warnings.onceregistry must be a dict");
100+
Py_DECREF(registry);
101+
return NULL;
102+
}
97103
Py_DECREF(_once_registry);
98104
_once_registry = registry;
99105
return registry;
@@ -449,7 +455,7 @@ warn_explicit(PyObject *category, PyObject *message,
449455
Py_RETURN_NONE;
450456

451457
if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
452-
PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
458+
PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
453459
return NULL;
454460
}
455461

0 commit comments

Comments
 (0)