/usr/lib/python3/dist-packages/pyfits/tests/util.py is in python3-pyfits 1:3.4-4.
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 71 | """Test utility functions."""
from __future__ import with_statement
import functools
import sys
import warnings
from ..extern.six import StringIO
class CaptureStdio(object):
"""
A simple context manager for redirecting stdout and stderr to a StringIO
buffer.
"""
def __init__(self, stdout=True, stderr=True):
self.stdout = StringIO()
self.stderr = StringIO()
def __enter__(self):
self._original_stdout = sys.stdout
self._original_stderr = sys.stderr
sys.stdout = self.stdout
sys.stderr = self.stderr
return self.stdout, self.stderr
def __exit__(self, *args, **kwargs):
sys.stdout = self._original_stdout
sys.stderr = self._original_stderr
self.stdout.close()
self.stderr.close()
class ignore_warnings(warnings.catch_warnings):
"""
This can be used either as a context manager or function decorator to
ignore all warnings that occur within a function or block of code.
An optional category option can be supplied to only ignore warnings of a
certain category or categories (if a list is provided).
"""
def __init__(self, category=None):
super(ignore_warnings, self).__init__()
if isinstance(category, type) and issubclass(category, Warning):
self.category = [category]
else:
self.category = category
def __call__(self, func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Originally this just reused self, but that doesn't work if the
# function is called more than once so we need to make a new
# context manager instance for each call
with self.__class__(category=self.category):
return func(*args, **kwargs)
return wrapper
def __enter__(self):
retval = super(ignore_warnings, self).__enter__()
if self.category is not None:
for category in self.category:
warnings.simplefilter('ignore', category)
else:
warnings.simplefilter('ignore')
return retval
|