diff --git a/qutebrowser/utils/log.py b/qutebrowser/utils/log.py index 0d87a5f7c..97013a82b 100644 --- a/qutebrowser/utils/log.py +++ b/qutebrowser/utils/log.py @@ -422,6 +422,9 @@ def qt_message_handler(msg_type, context, msg): 'with: -9805', # flake8: disable=E131 ] + if not msg: + msg = "Logged empty message!" + if any(msg.strip().startswith(pattern) for pattern in suppressed_msgs): level = logging.DEBUG else: diff --git a/tests/unit/utils/test_log.py b/tests/unit/utils/test_log.py index 147e760bb..30dd5d634 100644 --- a/tests/unit/utils/test_log.py +++ b/tests/unit/utils/test_log.py @@ -25,12 +25,15 @@ import itertools import sys import warnings +import attr import pytest import pytest_catchlog from qutebrowser.utils import log from qutebrowser.misc import utilcmds +from PyQt5 import QtCore + @pytest.fixture(autouse=True) def restore_loggers(): @@ -252,3 +255,22 @@ def test_ignore_py_warnings(caplog): assert len(caplog.records) == 1 msg = caplog.records[0].message.splitlines()[0] assert msg.endswith("UserWarning: not hidden") + + +class TestQtMessageHandler: + + @attr.s + class Context: + + """Fake QMessageLogContext.""" + + function = attr.ib(default=None) + category = attr.ib(default=None) + file = attr.ib(default=None) + line = attr.ib(default=None) + + def test_empty_message(self, caplog): + """Make sure there's no crash with an empty message.""" + log.qt_message_handler(QtCore.QtDebugMsg, self.Context(), "") + assert len(caplog.records) == 1 + assert caplog.records[0].msg == "Logged empty message!"