]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Make warnings accept a callable for showwarnings instead of
authorBrett Cannon <brett@python.org>
Mon, 18 Jul 2011 02:17:55 +0000 (19:17 -0700)
committerBrett Cannon <brett@python.org>
Mon, 18 Jul 2011 02:17:55 +0000 (19:17 -0700)
restricting itself to just functions and methods (which allows
built-in functions to be used, etc.).

Closes issue #10271. Thanks to lekma for the bug report.

Lib/test/test_warnings.py
Python/_warnings.c

index 79be835bbdd992a5064d36510ab910b25ba4fbc7..953b2827be2dd02b9963e0748f66967ae43a11cd 100644 (file)
@@ -512,12 +512,11 @@ class _WarningsTests(BaseTest):
     def test_showwarning_not_callable(self):
         with original_warnings.catch_warnings(module=self.module):
             self.module.filterwarnings("always", category=UserWarning)
-            old_showwarning = self.module.showwarning
+            self.module.showwarning = print
+            with support.captured_output('stdout'):
+                self.module.warn('Warning!')
             self.module.showwarning = 23
-            try:
-                self.assertRaises(TypeError, self.module.warn, "Warning!")
-            finally:
-                self.module.showwarning = old_showwarning
+            self.assertRaises(TypeError, self.module.warn, "Warning!")
 
     def test_show_warning_output(self):
         # With showarning() missing, make sure that output is okay.
@@ -547,10 +546,13 @@ class _WarningsTests(BaseTest):
         globals_dict = globals()
         oldfile = globals_dict['__file__']
         try:
-            with original_warnings.catch_warnings(module=self.module) as w:
+            catch = original_warnings.catch_warnings(record=True,
+                                                     module=self.module)
+            with catch as w:
                 self.module.filterwarnings("always", category=UserWarning)
                 globals_dict['__file__'] = None
                 original_warnings.warn('test', UserWarning)
+                self.assertTrue(len(w))
         finally:
             globals_dict['__file__'] = oldfile
 
index 615a2d3217a034d1cae8eabd206a8ced02935a74..f8a7175f920a90eccdc4b52ab704d1f30061b617 100644 (file)
@@ -409,10 +409,10 @@ warn_explicit(PyObject *category, PyObject *message,
         else {
             PyObject *res;
 
-            if (!PyMethod_Check(show_fxn) && !PyFunction_Check(show_fxn)) {
+            if (!PyCallable_Check(show_fxn)) {
                 PyErr_SetString(PyExc_TypeError,
                                 "warnings.showwarning() must be set to a "
-                                "function or method");
+                                "callable");
                 Py_DECREF(show_fxn);
                 goto cleanup;
             }