summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOren Milman <orenmn@gmail.com>2017-09-11 09:28:39 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2017-09-11 09:28:39 +0300
commit252033d50effa08046ac34fcc406bc99796ab88b (patch)
treeda5a9acc3a8a57688769bba6bbaaebce741846a4
parent3866d9bbcf808cea98b3d00007f9f246b83858ce (diff)
downloadcpython-git-252033d50effa08046ac34fcc406bc99796ab88b.tar.gz
bpo-31411: Prevent raising a SystemError in case warnings.onceregistry is not a dictionary. (#3485)
-rw-r--r--Lib/test/test_warnings/__init__.py11
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2017-09-11-08-50-41.bpo-31411.HZz82I.rst2
-rw-r--r--Python/_warnings.c8
3 files changed, 20 insertions, 1 deletions
diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py
index 69623c40f4..75539cf1c8 100644
--- a/Lib/test/test_warnings/__init__.py
+++ b/Lib/test/test_warnings/__init__.py
@@ -794,6 +794,17 @@ class _WarningsTests(BaseTest, unittest.TestCase):
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):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-09-11-08-50-41.bpo-31411.HZz82I.rst b/Misc/NEWS.d/next/Core and Builtins/2017-09-11-08-50-41.bpo-31411.HZz82I.rst
new file mode 100644
index 0000000000..ad1b4b8870
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-09-11-08-50-41.bpo-31411.HZz82I.rst
@@ -0,0 +1,2 @@
+Raise a TypeError instead of SystemError in case warnings.onceregistry is
+not a dictionary. Patch by Oren Milman.
diff --git a/Python/_warnings.c b/Python/_warnings.c
index 5230318788..078bdcb039 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -86,6 +86,12 @@ get_once_registry(void)
return NULL;
return _PyRuntime.warnings.once_registry;
}
+ if (!PyDict_Check(registry)) {
+ PyErr_SetString(PyExc_TypeError,
+ "warnings.onceregistry must be a dict");
+ Py_DECREF(registry);
+ return NULL;
+ }
Py_DECREF(_PyRuntime.warnings.once_registry);
_PyRuntime.warnings.once_registry = registry;
return registry;
@@ -437,7 +443,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;
}