[SciPy-User] catching warnings without error/raise
Thu Sep 22 13:49:07 CDT 2011
On Thu, Sep 22, 2011 at 2:04 PM, David Cournapeau <firstname.lastname@example.org> wrote:
> On Thu, Sep 22, 2011 at 12:16 PM, <email@example.com> wrote:
>> A python question, but hopefully someone can answer
>> I would like wrap functions that sometimes issue warnings, I want to
>> suppress the warning but I want to record whether a warning has been
>> issued, for later display.
>> python 2.6 has "with warnings.catch_warnings(record=True) as w:" that
>> seems to do what I want (from reading the description).
> The with statement is "just" syntax sugar, so if you read the sources
> for the corresponding context manager, you should be able to reproduce
> the code in a 2.5-compatible way.
I forgot we can sometimes look at the python source
cache_showwarning = warnings.showwarning
log = 
##def showwarning(*args, **kwargs):
## log.append(warnings.WarningMessage(*args, **kwargs))
#python 2.5 : no warnings.WarningMessage
def showwarning(*args, **kwargs):
warnings.showwarning = showwarning
import numpy as np
from scipy import stats
warnings.simplefilter("always") #stats doesn't always warn ?
warnings.showwarning = cache_showwarning
After looking at the source, I see in the documentation for
showwarning for both 2.5 and 2.6
"You may replace this function with an alternative implementation by
assigning to warnings.showwarning"
which sounded too "monkey" for me to pay attention.
> SciPy-User mailing list
More information about the SciPy-User