log: Set line number to -1 if it's None for Qt messages

If lineno is set to None in the LogRecord, pytest's logging formatting fails:

    tests/unit/utils/test_log.py:418: in test_empty_message
        log.qt_message_handler(QtCore.QtDebugMsg, self.Context(), "")
    qutebrowser/utils/log.py:508: in qt_message_handler
        qt.handle(record)
    /usr/lib/python3.8/logging/__init__.py:1587: in handle
        self.callHandlers(record)
    /usr/lib/python3.8/logging/__init__.py:1649: in callHandlers
        hdlr.handle(record)
    /usr/lib/python3.8/logging/__init__.py:950: in handle
        self.emit(record)
    ../../pytest/src/_pytest/logging.py:326: in emit
        super().emit(record)
    /usr/lib/python3.8/logging/__init__.py:1089: in emit
        self.handleError(record)
    /usr/lib/python3.8/logging/__init__.py:1081: in emit
        msg = self.format(record)
    /usr/lib/python3.8/logging/__init__.py:925: in format
        return fmt.format(record)
    ../../pytest/src/_pytest/logging.py:89: in format
        return super().format(record)
    /usr/lib/python3.8/logging/__init__.py:667: in format
        s = self.formatMessage(record)
    /usr/lib/python3.8/logging/__init__.py:636: in formatMessage
        return self._style.format(record)
    ../../pytest/src/_pytest/logging.py:185: in format
        return self._fmt % record.__dict__
    E   TypeError: %d format: a number is required, not NoneType

According to typeshed, lineno should never be None:
028f0d5293/stdlib/2and3/logging/__init__.pyi (L386)

Thus, this is our fault, not pytest's. However, before pytest 6.0.0, pytest did
not surface logging errors:

https://github.com/pytest-dev/pytest/pull/7231
b13fcb23d7

Thus, we never noticed something was going wrong here.
This commit is contained in:
Florian Bruhin 2020-07-10 14:40:33 +02:00
parent 75945e68cf
commit e206d346ce
1 changed files with 6 additions and 1 deletions

View File

@ -477,6 +477,11 @@ def qt_message_handler(msg_type: QtCore.QtMsgType,
else:
level = qt_to_logging[msg_type]
if context.line is None:
lineno = -1
else:
lineno = context.line
if context.function is None:
func = 'none' # type: ignore[unreachable]
elif ':' in context.function:
@ -503,7 +508,7 @@ def qt_message_handler(msg_type: QtCore.QtMsgType,
else:
stack = None
record = qt.makeRecord(name, level, context.file, context.line, msg, (),
record = qt.makeRecord(name, level, context.file, lineno, msg, (),
None, func, sinfo=stack)
qt.handle(record)