Skip to content

Commit 3abc03c

Browse files
authored
gh-132888: Fix Windows API error checking in pyrepl.windows_console (#144248)
1 parent 93b27e7 commit 3abc03c

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

Lib/_pyrepl/windows_console.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@
4343
from .windows_eventqueue import EventQueue
4444

4545
try:
46-
from ctypes import get_last_error, GetLastError, WinDLL, windll, WinError # type: ignore[attr-defined]
46+
from ctypes import get_last_error, WinDLL, windll, WinError # type: ignore[attr-defined]
4747
except:
4848
# Keep MyPy happy off Windows
4949
from ctypes import CDLL as WinDLL, cdll as windll
5050

51-
def GetLastError() -> int:
52-
return 42
53-
5451
def get_last_error() -> int:
5552
return 42
5653

@@ -149,16 +146,18 @@ def __init__(
149146

150147
# Save original console modes so we can recover on cleanup.
151148
original_input_mode = DWORD()
152-
GetConsoleMode(InHandle, original_input_mode)
149+
if not GetConsoleMode(InHandle, original_input_mode):
150+
raise WinError(get_last_error())
153151
trace(f'saved original input mode 0x{original_input_mode.value:x}')
154152
self.__original_input_mode = original_input_mode.value
155153

156-
SetConsoleMode(
154+
if not SetConsoleMode(
157155
OutHandle,
158156
ENABLE_WRAP_AT_EOL_OUTPUT
159157
| ENABLE_PROCESSED_OUTPUT
160158
| ENABLE_VIRTUAL_TERMINAL_PROCESSING,
161-
)
159+
):
160+
raise WinError(get_last_error())
162161

163162
self.screen: list[str] = []
164163
self.width = 80
@@ -301,7 +300,7 @@ def _scroll(
301300
if not ScrollConsoleScreenBuffer(
302301
OutHandle, scroll_rect, None, destination_origin, fill_info
303302
):
304-
raise WinError(GetLastError())
303+
raise WinError(get_last_error())
305304

306305
def _hide_cursor(self):
307306
self.__write("\x1b[?25l")
@@ -335,7 +334,7 @@ def __write(self, text: str) -> None:
335334
def screen_xy(self) -> tuple[int, int]:
336335
info = CONSOLE_SCREEN_BUFFER_INFO()
337336
if not GetConsoleScreenBufferInfo(OutHandle, info):
338-
raise WinError(GetLastError())
337+
raise WinError(get_last_error())
339338
return info.dwCursorPosition.X, info.dwCursorPosition.Y
340339

341340
def _erase_to_end(self) -> None:
@@ -350,14 +349,16 @@ def prepare(self) -> None:
350349
self.__offset = 0
351350

352351
if self.__vt_support:
353-
SetConsoleMode(InHandle, self.__original_input_mode | ENABLE_VIRTUAL_TERMINAL_INPUT)
352+
if not SetConsoleMode(InHandle, self.__original_input_mode | ENABLE_VIRTUAL_TERMINAL_INPUT):
353+
raise WinError(get_last_error())
354354
self._enable_bracketed_paste()
355355

356356
def restore(self) -> None:
357357
if self.__vt_support:
358358
# Recover to original mode before running REPL
359359
self._disable_bracketed_paste()
360-
SetConsoleMode(InHandle, self.__original_input_mode)
360+
if not SetConsoleMode(InHandle, self.__original_input_mode):
361+
raise WinError(get_last_error())
361362

362363
def _move_relative(self, x: int, y: int) -> None:
363364
"""Moves relative to the current posxy"""
@@ -394,7 +395,7 @@ def getheightwidth(self) -> tuple[int, int]:
394395
and width of the terminal window in characters."""
395396
info = CONSOLE_SCREEN_BUFFER_INFO()
396397
if not GetConsoleScreenBufferInfo(OutHandle, info):
397-
raise WinError(GetLastError())
398+
raise WinError(get_last_error())
398399
return (
399400
info.srWindow.Bottom - info.srWindow.Top + 1,
400401
info.srWindow.Right - info.srWindow.Left + 1,
@@ -403,15 +404,15 @@ def getheightwidth(self) -> tuple[int, int]:
403404
def _getscrollbacksize(self) -> int:
404405
info = CONSOLE_SCREEN_BUFFER_INFO()
405406
if not GetConsoleScreenBufferInfo(OutHandle, info):
406-
raise WinError(GetLastError())
407+
raise WinError(get_last_error())
407408

408409
return info.srWindow.Bottom # type: ignore[no-any-return]
409410

410411
def _read_input(self) -> INPUT_RECORD | None:
411412
rec = INPUT_RECORD()
412413
read = DWORD()
413414
if not ReadConsoleInput(InHandle, rec, 1, read):
414-
raise WinError(GetLastError())
415+
raise WinError(get_last_error())
415416

416417
return rec
417418

@@ -421,7 +422,7 @@ def _read_input_bulk(
421422
rec = (n * INPUT_RECORD)()
422423
read = DWORD()
423424
if not ReadConsoleInput(InHandle, rec, n, read):
424-
raise WinError(GetLastError())
425+
raise WinError(get_last_error())
425426

426427
return rec, read.value
427428

@@ -523,7 +524,7 @@ def flushoutput(self) -> None:
523524
def forgetinput(self) -> None:
524525
"""Forget all pending, but not yet processed input."""
525526
if not FlushConsoleInputBuffer(InHandle):
526-
raise WinError(GetLastError())
527+
raise WinError(get_last_error())
527528

528529
def getpending(self) -> Event:
529530
"""Return the characters that have been typed but not yet

Lib/test/test_pyrepl/test_windows_console.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from test.support import force_not_colorized_test_class
1111
from typing import Iterable
1212
from unittest import TestCase
13-
from unittest.mock import MagicMock, call
13+
from unittest.mock import MagicMock, call, patch
1414

1515
from .support import handle_all_events, code_to_events
1616
from .support import prepare_reader as default_prepare_reader
@@ -30,7 +30,21 @@
3030
pass
3131

3232

33+
def _mock_console_init(self, f_in=0, f_out=1, term="", encoding="utf-8"):
34+
"""Mock __init__ to avoid real Windows API calls in headless environments."""
35+
super(WindowsConsole, self).__init__(f_in, f_out, term, encoding)
36+
self.screen = []
37+
self.width = 80
38+
self.height = 25
39+
self._WindowsConsole__offset = 0
40+
self.posxy = (0, 0)
41+
self._WindowsConsole__vt_support = False
42+
self._WindowsConsole_original_input_mode = 0
43+
self.event_queue = wc.EventQueue('utf-8')
44+
45+
3346
@force_not_colorized_test_class
47+
@patch.object(WindowsConsole, '__init__', _mock_console_init)
3448
class WindowsConsoleTests(TestCase):
3549
def console(self, events, **kwargs) -> Console:
3650
console = WindowsConsole()
@@ -373,6 +387,7 @@ def test_multiline_ctrl_z(self):
373387
con.restore()
374388

375389

390+
@patch.object(WindowsConsole, '__init__', _mock_console_init)
376391
class WindowsConsoleGetEventTests(TestCase):
377392
# Virtual-Key Codes: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
378393
VK_BACK = 0x08
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix incorrect use of :func:`ctypes.GetLastError` and add missing error
2+
checks for Windows API calls in :mod:`!_pyrepl.windows_console`.

0 commit comments

Comments
 (0)