]> granicus.if.org Git - python/commitdiff
[3.6] bpo-31411: Prevent raising a SystemError in case warnings.onceregistry is not...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 11 Sep 2017 07:01:47 +0000 (10:01 +0300)
committerGitHub <noreply@github.com>
Mon, 11 Sep 2017 07:01:47 +0000 (10:01 +0300)
(cherry picked from commit 252033d50effa08046ac34fcc406bc99796ab88b)

Lib/test/test_warnings/__init__.py
Misc/NEWS.d/next/Core and Builtins/2017-09-11-08-50-41.bpo-31411.HZz82I.rst [new file with mode: 0644]
Python/_warnings.c

index 755ee65438cadd2d473163b0f4a0e8a1fd95d3d6..c66fe3aa878a548f2a417b90e182fb803dfb11d6 100644 (file)
@@ -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 (file)
index 0000000..ad1b4b8
--- /dev/null
@@ -0,0 +1,2 @@
+Raise a TypeError instead of SystemError in case warnings.onceregistry is
+not a dictionary. Patch by Oren Milman.
index 6cfae77f126bdd260d8430c517f7e4995887fe56..2b04b9081e2c387791e06f6a1621cc1caa4f8d55 100644 (file)
@@ -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;
@@ -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;
     }