[Python-checkins] bpo-33509: Fix _warnings for module_globals=None (#6833)

Victor Stinner webhook-mailer at python.org
Tue May 15 14:42:15 EDT 2018


https://github.com/python/cpython/commit/b056562860c227bad2e0ba7cd3130e115c007768
commit: b056562860c227bad2e0ba7cd3130e115c007768
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-05-15T20:42:12+02:00
summary:

bpo-33509: Fix _warnings for module_globals=None (#6833)

Don't crash on warnings.warn_explicit() if module_globals is not a dict.

files:
A Misc/NEWS.d/next/Core and Builtins/2018-05-14-17-31-02.bpo-33509.pIUfTd.rst
M Lib/test/test_warnings/__init__.py
M Python/_warnings.c

diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py
index 31ab94b74716..940db5c143c9 100644
--- a/Lib/test/test_warnings/__init__.py
+++ b/Lib/test/test_warnings/__init__.py
@@ -218,6 +218,25 @@ def test_once(self):
                                     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
index 000000000000..3d80a8c7f3eb
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-05-14-17-31-02.bpo-33509.pIUfTd.rst	
@@ -0,0 +1,2 @@
+Fix module_globals parameter of warnings.warn_explicit(): don't crash if
+module_globals is not a dict.
diff --git a/Python/_warnings.c b/Python/_warnings.c
index 0568af4df5a8..29e475d67d1f 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -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;



More information about the Python-checkins mailing list