summaryrefslogtreecommitdiff
path: root/src/_pytest/_io
diff options
context:
space:
mode:
authorRan Benita <ran@unusedvar.com>2020-04-29 16:17:44 +0300
committerRan Benita <ran@unusedvar.com>2020-04-30 16:44:02 +0300
commit1d596b27a707414145793b16ad1b1f2ffbc02799 (patch)
tree5fae0c3bfe5618ba54bb716db309f8853fcb4f4c /src/_pytest/_io
parent5e2d820308a0a78212efb6dadfdc7cbe02b7cbec (diff)
downloadpytest-1d596b27a707414145793b16ad1b1f2ffbc02799.tar.gz
terminalwriter: move Win32ConsoleWriter definition under win32 conditional
This way non-Windows platforms skip it. It also uses things defined inside the `if`.
Diffstat (limited to 'src/_pytest/_io')
-rw-r--r--src/_pytest/_io/terminalwriter.py67
1 files 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