From e206d346ced90aa4d8d8411baa1f9cc92406f16c Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 10 Jul 2020 14:40:33 +0200 Subject: 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: https://github.com/python/typeshed/blob/028f0d52931fe1f96bb25d066186961159c1f801/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 https://github.com/pytest-dev/pytest/commit/b13fcb23d79b3f38e497824c438c926a0a015561 Thus, we never noticed something was going wrong here. --- qutebrowser/utils/log.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/qutebrowser/utils/log.py b/qutebrowser/utils/log.py index 197f594f9..f82ba290e 100644 --- a/qutebrowser/utils/log.py +++ b/qutebrowser/utils/log.py @@ -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) -- cgit v1.2.3-54-g00ecf