From 67253726fa82471abb0ac4e2e5ae5168b2f6575c Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 26 Nov 2017 17:31:07 +0100 Subject: [PATCH] Handle empty messages in qt_message_handler I can't reproduce this, but someone on KDE reported always getting a crash (as msg.splitlines()[0] gives an IndexError) when trying to select a file with Qt 5.9.3. --- qutebrowser/utils/log.py | 3 +++ tests/unit/utils/test_log.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) 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!"