[Python-Dev] Py3k DeprecationWarning in stdlib

Nick Coghlan ncoghlan at gmail.com
Wed Jun 25 12:24:06 CEST 2008


Jean-Paul Calderone wrote:
> On Tue, 24 Jun 2008 23:03:33 -0000, glyph at divmod.com wrote:
>> It's also nice to have this facility in the test harness itself, so 
>> that you don't run the additional risk of accidentally (and silently) 
>> leaving warning suppression in place for subsequent tests.
> 
> It would be *extra* nice to have this facility added to the standard
> library, since assertWarns in Twisted is broken by changes in Python
> 2.6 (ie, our tests for warnings all fail with trunk at HEAD).  For now,
> we will probably address this by switching to a different warning
> API.  In the long term, it'd be better for us, other Python developers,
> and the standard library if there were an API in the standard library
> which facilitated testing of warnings.

I forgot this had already been added to the Python regression test 
machinery, so it will just be a matter of updating the relevant tests to 
use it:

http://docs.python.org/dev/library/test.html#module-test.test_support

test.test_support.catch_warning(record=True)¶

     Return a context manager that guards the warnings filter from being 
permanently changed and records the data of the last warning that has 
been issued. The record argument specifies whether any raised warnings 
are captured by the object returned by warnings.catch_warning() or 
allowed to propagate as normal.

     The context manager is typically used like this:

     with catch_warning() as w:
         warnings.warn("foo")
         assert str(w.message) == "foo"

     New in version 2.6.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list