Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/10813.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in the logging checker when a logging call uses a ``bytes`` format string that is not valid UTF-8.

Closes #10813
5 changes: 4 additions & 1 deletion pylint/checkers/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,10 @@ def _check_format_string(self, node: nodes.Call, format_arg: Literal[0, 1]) -> N
format_string = node.args[format_arg].value
required_num_args = 0
if isinstance(format_string, bytes):
format_string = format_string.decode()
# ``logging`` applies ``str()`` to the message before interpolation,
# so mirror that here. ``bytes.decode()`` would raise
# ``UnicodeDecodeError`` on non-UTF-8 bytes and crash the checker.
format_string = str(format_string)
if isinstance(format_string, str):
try:
if self._format_style == "old":
Expand Down
13 changes: 13 additions & 0 deletions tests/functional/l/logging/logging_bytes_format_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# pylint: disable=missing-module-docstring
import logging

# Regression test for https://github.com/pylint-dev/pylint/issues/10813
# A bytes format string that is not valid UTF-8 used to crash the checker
# with a ``UnicodeDecodeError``.
logging.critical(b"\xc0\xc0")

# Valid bytes format strings should still have their arguments checked, as
# ``logging`` applies ``str()`` to the message before interpolation.
logging.error(b"%s", "arg")
logging.error(b"%s") # [logging-too-few-args]
logging.error(b"%s", 1, 2) # [logging-too-many-args]
2 changes: 2 additions & 0 deletions tests/functional/l/logging/logging_bytes_format_string.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
logging-too-few-args:12:0:12:20::Not enough arguments for logging format string:UNDEFINED
logging-too-many-args:13:0:13:26::Too many arguments for logging format string:HIGH