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.
This commit is contained in:
Florian Bruhin 2017-11-26 17:31:07 +01:00
parent 03a9cbdfb4
commit 67253726fa
2 changed files with 25 additions and 0 deletions

View File

@ -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:

View File

@ -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!"