Skip to content

Commit 004547f

Browse files
[2.7] bpo-31411: Prevent raising a SystemError in case warnings.onceregistry is not a dictionary. (GH-3485). (#3493)
(cherry picked from commit 252033d)
1 parent 6ed7aff commit 004547f

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/test/test_warnings.py

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

587+
@test_support.cpython_only
588+
def test_issue31411(self):
589+
# warn_explicit() shouldn't raise a SystemError in case
590+
# warnings.onceregistry isn't a dictionary.
591+
wmod = self.module
592+
with original_warnings.catch_warnings(module=wmod):
593+
wmod.filterwarnings('once')
594+
with test_support.swap_attr(wmod, 'onceregistry', None):
595+
with self.assertRaises(TypeError):
596+
wmod.warn_explicit('foo', Warning, 'bar', 1, registry=None)
597+
587598

588599
class WarningsDisplayTests(unittest.TestCase):
589600

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
@@ -72,6 +72,12 @@ get_once_registry(void)
7272
return NULL;
7373
return _once_registry;
7474
}
75+
if (!PyDict_Check(registry)) {
76+
PyErr_SetString(PyExc_TypeError,
77+
"warnings.onceregistry must be a dict");
78+
Py_DECREF(registry);
79+
return NULL;
80+
}
7581
Py_DECREF(_once_registry);
7682
_once_registry = registry;
7783
return registry;
@@ -296,7 +302,7 @@ warn_explicit(PyObject *category, PyObject *message,
296302
int rc;
297303

298304
if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
299-
PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
305+
PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
300306
return NULL;
301307
}
302308

0 commit comments

Comments
 (0)