clearing all warning module caches in a session

jason-sage at creativetrax.com jason-sage at creativetrax.com
Sat Jul 26 09:58:43 EDT 2008


Hi all,

I just started using the warnings module in Python 2.5.2.  When I 
trigger a warning using the default warning options, an entry is created 
in a module-level cache so that the warning is ignored in the future.  
However, I don't see an easy way to clear or invalidate these 
module-level caches of previously triggered warnings.  That means that 
if I ever have a warning triggered with a "once" or a default warning 
level or filter, I can never see that warning again until I restart 
python (or figure out what module contains the cache and delete it 
manually).

I thought resetwarnings would invalidate the module-level caches, so 
that I could trigger a "once" warning, then do resetwarnings() and make 
the default an "always" action and see my warning displayed again.  At 
least, the name seems to indicate that all *warnings* would be reset, 
not just the filter list.  But apparently resetwarnings() just clears 
the filter list, which is helpful, but doesn't do anything for the 
already-cached warnings.

A very small change in the code would make this possible.  Instead of 
making the value of the warnings module-level cache just "1", make it an 
integer which is incremented each time resetwarnings() is called.  That 
way calling resetwarnings() can invalidate the cache (i.e., the warnings 
processing would ignore the cache record if the value wasn't the current 
integer).

Again, the question is: is there an easy way to invalidate all the 
module-level caches of warnings so that the entire warning system is 
truly reset?

Thanks,

Jason




More information about the Python-list mailing list