Skip to content

Commit

Permalink
[2.7] bpo-31411: Prevent raising a SystemError in case warnings.oncer…
Browse files Browse the repository at this point in the history
…egistry is not a dictionary. (pythonGH-3485). (python#3493)

(cherry picked from commit 252033d)
  • Loading branch information
serhiy-storchaka authored Sep 11, 2017
1 parent 6ed7aff commit 004547f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Lib/test/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,17 @@ def test_stderr_none(self):
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):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise a TypeError instead of SystemError in case warnings.onceregistry is
not a dictionary. Patch by Oren Milman.
8 changes: 7 additions & 1 deletion Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 004547f

Please sign in to comment.