Skip to content

[3.6] bpo-31411: Prevent raising a SystemError in case warnings.oncer… #3494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,17 @@ def test_stderr_none(self):
self.assertNotIn(b'Warning!', stderr)
self.assertNotIn(b'Error', stderr)

@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 support.swap_attr(wmod, 'onceregistry', None):
with self.assertRaises(TypeError):
wmod.warn_explicit('foo', Warning, 'bar', 1, registry=None)


class WarningsDisplayTests(BaseTest):

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 @@ -94,6 +94,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 @@ -449,7 +455,7 @@ warn_explicit(PyObject *category, PyObject *message,
Py_RETURN_NONE;

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