def _check_modules(modules): """Make sure the given modules are available.""" from qutebrowser.utils import log for name, text in modules.items(): try: # pylint: disable=bad-continuation with log.py_warning_filter( category=DeprecationWarning, message=r'invalid escape sequence' ), log.py_warning_filter( category=ImportWarning, message=r'Not importing directory .*: missing __init__' ), log.py_warning_filter( category=DeprecationWarning, message=r'the imp module is deprecated', ), log.py_warning_filter( # WORKAROUND for https://github.com/pypa/setuptools/issues/2466 category=DeprecationWarning, message=r'Creating a LegacyVersion has been deprecated', ): # pylint: enable=bad-continuation importlib.import_module(name) except ImportError as e: _die(text, e)
def yaml_load(f: Union[str, IO[str]]) -> Any: """Wrapper over yaml.load using the C loader if possible.""" start = datetime.datetime.now() # WORKAROUND for https://github.com/yaml/pyyaml/pull/181 with log.py_warning_filter( category=DeprecationWarning, message=r"Using or importing the ABCs from 'collections' instead " r"of from 'collections\.abc' is deprecated.*"): try: data = yaml.load(f, Loader=YamlLoader) except ValueError as e: if str(e).startswith('could not convert string to float'): # WORKAROUND for https://github.com/yaml/pyyaml/issues/168 raise yaml.YAMLError(e) raise # pragma: no cover end = datetime.datetime.now() delta = (end - start).total_seconds() deadline = 10 if 'CI' in os.environ else 2 if delta > deadline: # pragma: no cover log.misc.warning( "YAML load took unusually long, please report this at " "https://github.com/qutebrowser/qutebrowser/issues/2777\n" "duration: {}s\n" "PyYAML version: {}\n" "C extension: {}\n" "Stack:\n\n" "{}".format(delta, yaml.__version__, YAML_C_EXT, ''.join(traceback.format_stack()))) return data
def test_py_warning_filter_error(caplog): warnings.simplefilter('ignore') warnings.warn("hidden", UserWarning) with log.py_warning_filter('error'): with pytest.raises(UserWarning): warnings.warn("error", UserWarning)
def ignore_bs4_warning(): """WORKAROUND for https://bugs.launchpad.net/beautifulsoup/+bug/1847592.""" with log.py_warning_filter( category=DeprecationWarning, message="Using or importing the ABCs from 'collections' instead " "of from 'collections.abc' is deprecated", module='bs4.element'): yield
def test_py_warning_filter(caplog): logging.captureWarnings(True) with log.py_warning_filter(category=UserWarning): warnings.warn("hidden", UserWarning) with caplog.at_level(logging.WARNING): warnings.warn("not hidden", UserWarning) assert len(caplog.records) == 1 msg = caplog.messages[0].splitlines()[0] assert msg.endswith("UserWarning: not hidden")
def _check_modules(modules): """Make sure the given modules are available.""" from qutebrowser.utils import log for name, text in modules.items(): try: # https://bitbucket.org/fdik/pypeg/commits/dd15ca462b532019c0a3be1d39b8ee2f3fa32f4e # pylint: disable=bad-continuation with log.py_warning_filter( category=DeprecationWarning, message=r'invalid escape sequence'), log.py_warning_filter( category=ImportWarning, message=r'Not importing directory .*: missing __init__' ), log.py_warning_filter( category=DeprecationWarning, message=r'the imp module is deprecated', ): # pylint: enable=bad-continuation importlib.import_module(name) except ImportError as e: _die(text, e)