[issue3781] warnings.catch_warnings fails gracelessly when recording warnings but no warnings are emitted

Brett Cannon report at bugs.python.org
Fri Sep 5 00:22:33 CEST 2008


Brett Cannon <brett at python.org> added the comment:

On Thu, Sep 4, 2008 at 3:10 PM, Jean-Paul Calderone
<report at bugs.python.org> wrote:
>
> New submission from Jean-Paul Calderone <exarkun at divmod.com>:
>
> This example shows the behavior:
>
>    from warnings import catch_warnings
>
>    def test():
>        with catch_warnings(True) as w:
>            assert str(w.message) == "foo", "%r != %r" % (w.message, "foo")
>
>    test()
>
> This fails with an IndexError from the `w.message`.  That's a bit
> surprising, and since this is mostly an API useful for testing, it'd be
> much better if it had a well-defined, documented (ie, stable and likely
> to continue working in the next release of Python) error mode.
>

The question is what exception to raise when no warning has been
recorded. AttributeError goes with the idea that the attributes are
just not set since no warnings are there to set the attributes.
LookupError doesn't seem quite right. TypeError is definitely wrong
since it has nothing to do with the type of anything.

So unless someone comes up with a better suggestion I will change
__getattr__ on catch_warnings to raise AttributeError when IndexError
is raised because no warning is currently recorded.

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3781>
_______________________________________


More information about the Python-bugs-list mailing list