bpo-33509: Fix _warnings for module_globals=None (#6833)
Don't crash on warnings.warn_explicit() if module_globals is not a dict.
This commit is contained in:
parent
8709b236fc
commit
b056562860
|
@ -218,6 +218,25 @@ class FilterTests(BaseTest):
|
||||||
42)
|
42)
|
||||||
self.assertEqual(len(w), 0)
|
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):
|
def test_inheritance(self):
|
||||||
with original_warnings.catch_warnings(module=self.module) as w:
|
with original_warnings.catch_warnings(module=self.module) as w:
|
||||||
self.module.resetwarnings()
|
self.module.resetwarnings()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix module_globals parameter of warnings.warn_explicit(): don't crash if
|
||||||
|
module_globals is not a dict.
|
|
@ -951,7 +951,14 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
|
||||||
®istry, &module_globals, &sourceobj))
|
®istry, &module_globals, &sourceobj))
|
||||||
return NULL;
|
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);
|
source_line = get_source_line(module_globals, lineno);
|
||||||
if (source_line == NULL && PyErr_Occurred()) {
|
if (source_line == NULL && PyErr_Occurred()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Reference in New Issue