[Python-checkins] [2.7] bpo-31411: Prevent raising a SystemError in case warnings.onceregistry is not a dictionary. (GH-3485). (#3493)

Serhiy Storchaka webhook-mailer at python.org
Mon Sep 11 03:01:34 EDT 2017


https://github.com/python/cpython/commit/004547f97067be2e23ae770f300c0c0d1db1ba27
commit: 004547f97067be2e23ae770f300c0c0d1db1ba27
branch: 2.7
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2017-09-11T10:01:31+03:00
summary:

[2.7] bpo-31411: Prevent raising a SystemError in case warnings.onceregistry is not a dictionary. (GH-3485). (#3493)

(cherry picked from commit 252033d50effa08046ac34fcc406bc99796ab88b)

files:
A Misc/NEWS.d/next/Core and Builtins/2017-09-11-08-50-41.bpo-31411.HZz82I.rst
M Lib/test/test_warnings.py
M Python/_warnings.c

diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
index 607d9f9b722..0023363832c 100644
--- a/Lib/test/test_warnings.py
+++ b/Lib/test/test_warnings.py
@@ -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):
 
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 00000000000..ad1b4b8870a
--- /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 8e8c0cceb31..dd168f92593 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -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;
@@ -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;
     }
 



More information about the Python-checkins mailing list