[Python-checkins] cpython: Make warnings accept a callable for showwarnings instead of

Brett Cannon brett at python.org
Mon Jul 18 04:21:12 CEST 2011


Just realized I missed the Misc/NEWS entry, so I'm on it.

And I also realized I am sneaking in a minor tweak to an unrelated test, but
it was simply easier to do this than revert the change and do a separate
commit. May the buildbots be kind to me. =)

On Sun, Jul 17, 2011 at 19:18, brett.cannon <python-checkins at python.org>wrote:

> http://hg.python.org/cpython/rev/aaced3dcb858
> changeset:   71400:aaced3dcb858
> parent:      71398:e5e8796b68b8
> user:        Brett Cannon <brett at python.org>
> date:        Sun Jul 17 19:17:55 2011 -0700
> summary:
>  Make warnings accept a callable for showwarnings instead of
> 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.
>
> files:
>  Lib/test/test_warnings.py |  14 ++++++++------
>  Python/_warnings.c        |   4 ++--
>  2 files changed, 10 insertions(+), 8 deletions(-)
>
>
> diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
> --- a/Lib/test/test_warnings.py
> +++ b/Lib/test/test_warnings.py
> @@ -512,12 +512,11 @@
>     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 @@
>         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
>
> diff --git a/Python/_warnings.c b/Python/_warnings.c
> --- a/Python/_warnings.c
> +++ b/Python/_warnings.c
> @@ -409,10 +409,10 @@
>         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;
>             }
>
> --
> Repository URL: http://hg.python.org/cpython
>
> _______________________________________________
> Python-checkins mailing list
> Python-checkins at python.org
> http://mail.python.org/mailman/listinfo/python-checkins
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-checkins/attachments/20110717/7a01b05d/attachment-0001.html>


More information about the Python-checkins mailing list