Skip to content

Commit 106a3a2

Browse files
committed
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: pytest-dev/pytest#7231 pytest-dev/pytest@b13fcb2 Thus, we never noticed something was going wrong here. (cherry picked from commit e206d34) Make mypy happy It doesn't know about QMessageLogContext.lineno being Optional[int] rather than int. (cherry picked from commit 4136c84)
1 parent 58765f2 commit 106a3a2

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

qutebrowser/utils/log.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ def qt_message_handler(msg_type: QtCore.QtMsgType,
477477
else:
478478
level = qt_to_logging[msg_type]
479479

480+
if context.line is None:
481+
lineno = -1 # type: ignore[unreachable]
482+
else:
483+
lineno = context.line
484+
480485
if context.function is None:
481486
func = 'none' # type: ignore[unreachable]
482487
elif ':' in context.function:
@@ -503,7 +508,7 @@ def qt_message_handler(msg_type: QtCore.QtMsgType,
503508
else:
504509
stack = None
505510

506-
record = qt.makeRecord(name, level, context.file, context.line, msg, (),
511+
record = qt.makeRecord(name, level, context.file, lineno, msg, (),
507512
None, func, sinfo=stack)
508513
qt.handle(record)
509514

0 commit comments

Comments
 (0)