In Python 2.6 it is possible to suppress warnings from the warnings module by using
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fxn()
Versions of Python before 2.6 don’t support with however, so I’m wondering if there alternatives to the above that would work with pre-2.6 versions?
This is similar:
Edit: Without the
try/finally, the original warning filters would not be restored if fxn() threw an exception. See PEP 343 for more discussion on how thewithstatement serves to replacetry/finallywhen used like this.