From 1d596b27a707414145793b16ad1b1f2ffbc02799 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Wed, 29 Apr 2020 16:17:44 +0300 Subject: [PATCH] terminalwriter: move Win32ConsoleWriter definition under win32 conditional This way non-Windows platforms skip it. It also uses things defined inside the `if`. --- src/_pytest/_io/terminalwriter.py | 67 +++++++++++++++---------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/_pytest/_io/terminalwriter.py b/src/_pytest/_io/terminalwriter.py index 609d4418c..17740e83e 100644 --- a/src/_pytest/_io/terminalwriter.py +++ b/src/_pytest/_io/terminalwriter.py @@ -300,40 +300,6 @@ class TerminalWriter: self.write(" " * diff2last) -class Win32ConsoleWriter(TerminalWriter): - def write(self, msg, **kw): - if msg: - if not isinstance(msg, (bytes, str)): - msg = str(msg) - - self._update_chars_on_current_line(msg) - - oldcolors = None - if self.hasmarkup and kw: - handle = GetStdHandle(STD_OUTPUT_HANDLE) - oldcolors = GetConsoleInfo(handle).wAttributes - default_bg = oldcolors & 0x00F0 - attr = default_bg - if kw.pop("bold", False): - attr |= FOREGROUND_INTENSITY - - if kw.pop("red", False): - attr |= FOREGROUND_RED - elif kw.pop("blue", False): - attr |= FOREGROUND_BLUE - elif kw.pop("green", False): - attr |= FOREGROUND_GREEN - elif kw.pop("yellow", False): - attr |= FOREGROUND_GREEN | FOREGROUND_RED - else: - attr |= oldcolors & 0x0007 - - SetConsoleTextAttribute(handle, attr) - write_out(self._file, msg) - if oldcolors: - SetConsoleTextAttribute(handle, oldcolors) - - class WriteFile: def __init__(self, writemethod, encoding=None): self.encoding = encoding @@ -353,6 +319,39 @@ if win32_and_ctypes: from ctypes import wintypes from ctypes import windll # type: ignore[attr-defined] # noqa: F821 + class Win32ConsoleWriter(TerminalWriter): + def write(self, msg, **kw): + if msg: + if not isinstance(msg, (bytes, str)): + msg = str(msg) + + self._update_chars_on_current_line(msg) + + oldcolors = None + if self.hasmarkup and kw: + handle = GetStdHandle(STD_OUTPUT_HANDLE) + oldcolors = GetConsoleInfo(handle).wAttributes + default_bg = oldcolors & 0x00F0 + attr = default_bg + if kw.pop("bold", False): + attr |= FOREGROUND_INTENSITY + + if kw.pop("red", False): + attr |= FOREGROUND_RED + elif kw.pop("blue", False): + attr |= FOREGROUND_BLUE + elif kw.pop("green", False): + attr |= FOREGROUND_GREEN + elif kw.pop("yellow", False): + attr |= FOREGROUND_GREEN | FOREGROUND_RED + else: + attr |= oldcolors & 0x0007 + + SetConsoleTextAttribute(handle, attr) + write_out(self._file, msg) + if oldcolors: + SetConsoleTextAttribute(handle, oldcolors) + TerminalWriter = Win32ConsoleWriter # type: ignore[misc] # noqa: F821 # ctypes access to the Windows console