Add log.ignore_py_warnings()
This commit is contained in:
parent
36bb5cf285
commit
64f208486e
@ -167,9 +167,14 @@ def init_log(args):
|
||||
root.addHandler(ram)
|
||||
root.setLevel(logging.NOTSET)
|
||||
logging.captureWarnings(True)
|
||||
_init_py_warnings()
|
||||
QtCore.qInstallMessageHandler(qt_message_handler)
|
||||
|
||||
|
||||
def _init_py_warnings():
|
||||
"""Initialize Python warning handling."""
|
||||
warnings.simplefilter('default')
|
||||
warnings.filterwarnings('ignore', module='pdb', category=ResourceWarning)
|
||||
QtCore.qInstallMessageHandler(qt_message_handler)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
@ -182,6 +187,14 @@ def disable_qt_msghandler():
|
||||
QtCore.qInstallMessageHandler(old_handler)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def ignore_py_warnings(**kwargs):
|
||||
"""Contextmanager to temporarily hke certain Python warnings."""
|
||||
warnings.filterwarnings('ignore', **kwargs)
|
||||
yield
|
||||
_init_py_warnings()
|
||||
|
||||
|
||||
def _init_handlers(level, color, force_color, json_logging, ram_capacity):
|
||||
"""Init log handlers.
|
||||
|
||||
|
@ -23,6 +23,7 @@ import logging
|
||||
import argparse
|
||||
import itertools
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
import pytest
|
||||
import pytest_catchlog
|
||||
@ -36,6 +37,7 @@ def restore_loggers():
|
||||
|
||||
Based on CPython's Lib/test/test_logging.py.
|
||||
"""
|
||||
logging.captureWarnings(False)
|
||||
logger_dict = logging.getLogger().manager.loggerDict
|
||||
logging._acquireLock()
|
||||
try:
|
||||
@ -278,3 +280,14 @@ def test_stub(caplog, suffix, expected):
|
||||
log.stub(suffix)
|
||||
assert len(caplog.records) == 1
|
||||
assert caplog.records[0].message == expected
|
||||
|
||||
|
||||
def test_ignore_py_warnings(caplog):
|
||||
logging.captureWarnings(True)
|
||||
with log.ignore_py_warnings(category=UserWarning):
|
||||
warnings.warn("hidden", UserWarning)
|
||||
with caplog.at_level(logging.WARNING):
|
||||
warnings.warn("not hidden", UserWarning)
|
||||
assert len(caplog.records) == 1
|
||||
msg = caplog.records[0].message.splitlines()[0]
|
||||
assert msg.endswith("UserWarning: not hidden")
|
||||
|
Loading…
Reference in New Issue
Block a user