]> granicus.if.org Git - python/commitdiff
bpo-33509: Fix _warnings for module_globals=None (#6833)
authorVictor Stinner <vstinner@redhat.com>
Tue, 15 May 2018 18:42:12 +0000 (20:42 +0200)
committerGitHub <noreply@github.com>
Tue, 15 May 2018 18:42:12 +0000 (20:42 +0200)
Don't crash on warnings.warn_explicit() if module_globals is not a dict.

Lib/test/test_warnings/__init__.py
Misc/NEWS.d/next/Core and Builtins/2018-05-14-17-31-02.bpo-33509.pIUfTd.rst [new file with mode: 0644]
Python/_warnings.c

index 31ab94b74716d008f2d36c5e7357afa1d68519b5..940db5c143c930cdaa3b6f3c57e08e9c913c8052 100644 (file)
@@ -218,6 +218,25 @@ class FilterTests(BaseTest):
                                     42)
             self.assertEqual(len(w), 0)
 
+    def test_module_globals(self):
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            # bpo-33509: module_globals=None must not crash
+            self.module.warn_explicit('msg', UserWarning, "filename", 42,
+                                      module_globals=None)
+            self.assertEqual(len(w), 1)
+
+            # Invalid module_globals type
+            with self.assertRaises(TypeError):
+                self.module.warn_explicit('msg', UserWarning, "filename", 42,
+                                          module_globals=True)
+            self.assertEqual(len(w), 1)
+
+            # Empty module_globals
+            self.module.warn_explicit('msg', UserWarning, "filename", 42,
+                                      module_globals={})
+            self.assertEqual(len(w), 2)
+
     def test_inheritance(self):
         with original_warnings.catch_warnings(module=self.module) as w:
             self.module.resetwarnings()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-05-14-17-31-02.bpo-33509.pIUfTd.rst b/Misc/NEWS.d/next/Core and Builtins/2018-05-14-17-31-02.bpo-33509.pIUfTd.rst
new file mode 100644 (file)
index 0000000..3d80a8c
--- /dev/null
@@ -0,0 +1,2 @@
+Fix module_globals parameter of warnings.warn_explicit(): don't crash if
+module_globals is not a dict.
index 0568af4df5a80b200c5d91ba8c1f5d435f27f3a6..29e475d67d1f61d59b69d6fe171fc26c3ba40aab 100644 (file)
@@ -951,7 +951,14 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
                 &registry, &module_globals, &sourceobj))
         return NULL;
 
-    if (module_globals) {
+    if (module_globals && module_globals != Py_None) {
+        if (!PyDict_Check(module_globals)) {
+            PyErr_Format(PyExc_TypeError,
+                         "module_globals must be a dict, not '%.200s'",
+                         Py_TYPE(module_globals)->tp_name);
+            return NULL;
+        }
+
         source_line = get_source_line(module_globals, lineno);
         if (source_line == NULL && PyErr_Occurred()) {
             return NULL;