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

Lib/test/test_warnings.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 607d9f9b7220c855f10a3419b30cb57498e5df3f..0023363832c9e98927535540938696a29d4a5bdd 100644 (file)
@@ -584,6 +584,17 @@ class _WarningsTests(BaseTest):
         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 (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 8e8c0cceb311df6752f4ebe751318128c38f723d..dd168f92593f4707225283c86e1f60f4889ed3f9 100644 (file)
@@ -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;
     }