Commit d44c33ad authored by Brady J. Garvin's avatar Brady J. Garvin Committed by Alexandru Băluț

utils: Support mixed str/bytes control sequences.

It is not safe for `_preformat_levels` to assume that all of the fields in a
`TerminalController` have the same type; at least in my environment, some of
these fields are populated with `bytes` while others remain strings.

This change conditionally applies decoding to each control sequence separately
using a helper function `_as_string`.  As a side-effect, it also eliminates some
code repetition in `_preformat_levels`.

Closes #2407.
parent afa54b06
......@@ -591,22 +591,22 @@ def log_level_name(level):
return fmt % (_LEVEL_NAMES[level - 1], )
def _as_string(string_or_bytes):
if isinstance(string_or_bytes, bytes):
return string_or_bytes.decode()
else:
return string_or_bytes
def _preformat_levels(enable_color_output):
terminal_controller = TerminalController()
for level in ERROR, WARN, FIXME, INFO, DEBUG, LOG:
if enable_color_output:
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]),
(_as_string(terminal_controller.BOLD),
_as_string(getattr(terminal_controller, COLORS[level])),
log_level_name(level),
terminal_controller.NORMAL))
_as_string(terminal_controller.NORMAL)))
else:
formatter = log_level_name(level)
_FORMATTED_LEVELS.append(formatter)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment