4343from .windows_eventqueue import EventQueue
4444
4545try :
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]
4747except :
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
0 commit comments