from .windows_eventqueue import EventQueue
try:
- from ctypes import get_last_error, GetLastError, WinDLL, windll, WinError # type: ignore[attr-defined]
+ from ctypes import get_last_error, WinDLL, windll, WinError # type: ignore[attr-defined]
except:
# Keep MyPy happy off Windows
from ctypes import CDLL as WinDLL, cdll as windll
- def GetLastError() -> int:
- return 42
-
def get_last_error() -> int:
return 42
# Save original console modes so we can recover on cleanup.
original_input_mode = DWORD()
- GetConsoleMode(InHandle, original_input_mode)
+ if not GetConsoleMode(InHandle, original_input_mode):
+ raise WinError(get_last_error())
trace(f'saved original input mode 0x{original_input_mode.value:x}')
self.__original_input_mode = original_input_mode.value
- SetConsoleMode(
+ if not SetConsoleMode(
OutHandle,
ENABLE_WRAP_AT_EOL_OUTPUT
| ENABLE_PROCESSED_OUTPUT
| ENABLE_VIRTUAL_TERMINAL_PROCESSING,
- )
+ ):
+ raise WinError(get_last_error())
self.screen: list[str] = []
self.width = 80
if not ScrollConsoleScreenBuffer(
OutHandle, scroll_rect, None, destination_origin, fill_info
):
- raise WinError(GetLastError())
+ raise WinError(get_last_error())
def _hide_cursor(self):
self.__write("\x1b[?25l")
def screen_xy(self) -> tuple[int, int]:
info = CONSOLE_SCREEN_BUFFER_INFO()
if not GetConsoleScreenBufferInfo(OutHandle, info):
- raise WinError(GetLastError())
+ raise WinError(get_last_error())
return info.dwCursorPosition.X, info.dwCursorPosition.Y
def _erase_to_end(self) -> None:
self.__offset = 0
if self.__vt_support:
- SetConsoleMode(InHandle, self.__original_input_mode | ENABLE_VIRTUAL_TERMINAL_INPUT)
+ if not SetConsoleMode(InHandle, self.__original_input_mode | ENABLE_VIRTUAL_TERMINAL_INPUT):
+ raise WinError(get_last_error())
self._enable_bracketed_paste()
def restore(self) -> None:
if self.__vt_support:
# Recover to original mode before running REPL
self._disable_bracketed_paste()
- SetConsoleMode(InHandle, self.__original_input_mode)
+ if not SetConsoleMode(InHandle, self.__original_input_mode):
+ raise WinError(get_last_error())
def _move_relative(self, x: int, y: int) -> None:
"""Moves relative to the current posxy"""
and width of the terminal window in characters."""
info = CONSOLE_SCREEN_BUFFER_INFO()
if not GetConsoleScreenBufferInfo(OutHandle, info):
- raise WinError(GetLastError())
+ raise WinError(get_last_error())
return (
info.srWindow.Bottom - info.srWindow.Top + 1,
info.srWindow.Right - info.srWindow.Left + 1,
def _getscrollbacksize(self) -> int:
info = CONSOLE_SCREEN_BUFFER_INFO()
if not GetConsoleScreenBufferInfo(OutHandle, info):
- raise WinError(GetLastError())
+ raise WinError(get_last_error())
return info.srWindow.Bottom # type: ignore[no-any-return]
rec = INPUT_RECORD()
read = DWORD()
if not ReadConsoleInput(InHandle, rec, 1, read):
- raise WinError(GetLastError())
+ raise WinError(get_last_error())
return rec
rec = (n * INPUT_RECORD)()
read = DWORD()
if not ReadConsoleInput(InHandle, rec, n, read):
- raise WinError(GetLastError())
+ raise WinError(get_last_error())
return rec, read.value
def forgetinput(self) -> None:
"""Forget all pending, but not yet processed input."""
if not FlushConsoleInputBuffer(InHandle):
- raise WinError(GetLastError())
+ raise WinError(get_last_error())
def getpending(self) -> Event:
"""Return the characters that have been typed but not yet
from test.support import force_not_colorized_test_class
from typing import Iterable
from unittest import TestCase
-from unittest.mock import MagicMock, call
+from unittest.mock import MagicMock, call, patch
from .support import handle_all_events, code_to_events
from .support import prepare_reader as default_prepare_reader
pass
+def _mock_console_init(self, f_in=0, f_out=1, term="", encoding="utf-8"):
+ """Mock __init__ to avoid real Windows API calls in headless environments."""
+ super(WindowsConsole, self).__init__(f_in, f_out, term, encoding)
+ self.screen = []
+ self.width = 80
+ self.height = 25
+ self._WindowsConsole__offset = 0
+ self.posxy = (0, 0)
+ self._WindowsConsole__vt_support = False
+ self._WindowsConsole_original_input_mode = 0
+ self.event_queue = wc.EventQueue('utf-8')
+
+
@force_not_colorized_test_class
+@patch.object(WindowsConsole, '__init__', _mock_console_init)
class WindowsConsoleTests(TestCase):
def console(self, events, **kwargs) -> Console:
console = WindowsConsole()
con.restore()
+@patch.object(WindowsConsole, '__init__', _mock_console_init)
class WindowsConsoleGetEventTests(TestCase):
# Virtual-Key Codes: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
VK_BACK = 0x08