`_preformat_levels` in `utils/loggable.py` assumes that the fields in its `TerminalController` are uniformly `bytes` or uniformly `string`s
In _preformat_levels
, which builds the list _FORMATTED_LEVELS
, the code may need to convert up to three TerminalController
fields from bytes
to str
s on each iteration:
-
terminal_controller.BOLD
, -
getattr(terminal_controller, COLORS[level])
, and -
terminal_controller.NORMAL
.
The current implementation, however, only checks one field's type, and then assumes that the other fields have the same type:
if isinstance(terminal_controller.BOLD, bytes):
formatter = ''.join(
(terminal_controller.BOLD.decode(),
getattr(terminal_controller, COLORS[level]).decode(),
log_level_name(level),
terminal_controller.NORMAL.decode()))
else:
formatter = ''.join(
(terminal_controller.BOLD,
getattr(terminal_controller, COLORS[level]),
log_level_name(level),
terminal_controller.NORMAL))
In some environvents (such as my own), this leads to a crash when terminal_controller.BOLD
has type bytes
, but getattr(terminal_controller, COLORS[level])
has type str
, as Python 3 strings have no decode
method to call.