Skip to content

Commit 252033d

Browse files
orenmnserhiy-storchaka
authored andcommitted
bpo-31411: Prevent raising a SystemError in case warnings.onceregistry is not a dictionary. (#3485)
1 parent 3866d9b commit 252033d

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
@@ -86,6 +86,12 @@ get_once_registry(void)
8686
return NULL;
8787
return _PyRuntime.warnings.once_registry;
8888
}
89+
if (!PyDict_Check(registry)) {
90+
PyErr_SetString(PyExc_TypeError,
91+
"warnings.onceregistry must be a dict");
92+
Py_DECREF(registry);
93+
return NULL;
94+
}
8995
Py_DECREF(_PyRuntime.warnings.once_registry);
9096
_PyRuntime.warnings.once_registry = registry;
9197
return registry;
@@ -437,7 +443,7 @@ warn_explicit(PyObject *category, PyObject *message,
437443
Py_RETURN_NONE;
438444

439445
if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
440-
PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
446+
PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
441447
return NULL;
442448
}
443449

0 commit comments

Comments
 (0)