/usr/share/pyshared/nibabel/checkwarns.py is in python-nibabel 1.3.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | # emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the NiBabel package for the
# copyright and license terms.
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
''' Contexts for *with* statement allowing checks for warnings
When we give up 2.5 compatibility we can use python's own
``tests.test_support.check_warnings``
'''
from __future__ import with_statement
import warnings
class ErrorWarnings(object):
""" Context manager to check for warnings as errors. Usually used with
``assert_raises`` in the with block
Examples
--------
>>> with ErrorWarnings():
... try:
... warnings.warn('Message', UserWarning)
... except UserWarning:
... print 'I consider myself warned'
I consider myself warned
Notes
-----
The manager will raise a RuntimeError if another warning filter gets put on
top of the one it has just added.
"""
def __init__(self):
self.added = None
def __enter__(self):
warnings.simplefilter('error')
self.added = warnings.filters[0]
def __exit__(self, exc, value, tb):
if warnings.filters[0] != self.added:
raise RuntimeError('Somone has done something to the filters')
warnings.filters.pop(0)
return False # allow any exceptions to propagate
class IgnoreWarnings(ErrorWarnings):
""" Context manager to ignore warnings
Examples
--------
>>> with IgnoreWarnings():
... warnings.warn('Message', UserWarning)
(and you get no warning)
Notes
-----
The manager will raise a RuntimeError if another warning filter gets put on
top of the one it has just added.
"""
def __enter__(self):
warnings.simplefilter('ignore')
self.added = warnings.filters[0]
|