5353from .windows_eventqueue import EventQueue
5454
5555try :
56- from ctypes import get_last_error , WinDLL , windll , WinError # type: ignore[attr-defined]
56+ from ctypes import get_last_error , WinDLL , WinError # type: ignore[attr-defined]
5757except :
5858 # Keep MyPy happy off Windows
59- from ctypes import CDLL as WinDLL , cdll as windll
59+ from ctypes import CDLL as WinDLL
6060
6161 def get_last_error () -> int :
6262 return 42
@@ -66,11 +66,14 @@ def __init__(self, err: int | None, descr: str | None = None) -> None:
6666 self .err = err
6767 self .descr = descr
6868
69- # declare nt optional to allow None assignment on other platforms
69+ # declare optional to allow None assignment on other platforms
70+ _winapi : types .ModuleType | None
7071nt : types .ModuleType | None
7172try :
73+ import _winapi
7274 import nt
7375except ImportError :
76+ _winapi = None
7477 nt = None
7578
7679lazy from typing import IO
@@ -121,7 +124,6 @@ def __init__(self, err: int | None, descr: str | None = None) -> None:
121124CTRL_ACTIVE = 0x04 | 0x08
122125
123126WAIT_TIMEOUT = 0x102
124- WAIT_FAILED = 0xFFFFFFFF
125127
126128# from winbase.h
127129INFINITE = 0xFFFFFFFF
@@ -697,10 +699,9 @@ def wait_for_event(self, timeout: float | None) -> bool:
697699 timeout = INFINITE
698700 else :
699701 timeout = int (timeout )
700- ret = WaitForSingleObject (InHandle , timeout )
701- if ret == WAIT_FAILED :
702- raise WinError (get_last_error ())
703- elif ret == WAIT_TIMEOUT :
702+ assert _winapi is not None # to make mypy happy
703+ ret = _winapi .WaitForSingleObject (InHandle , timeout )
704+ if ret == WAIT_TIMEOUT :
704705 return False
705706 return True
706707
@@ -793,16 +794,10 @@ class INPUT_RECORD(Structure):
793794ENABLE_WRAP_AT_EOL_OUTPUT = 0x02
794795ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x04
795796
796- STD_INPUT_HANDLE = - 10
797- STD_OUTPUT_HANDLE = - 11
798-
799797if sys .platform == "win32" :
798+ assert _winapi is not None # to make mypy happy
800799 _KERNEL32 = WinDLL ("kernel32" , use_last_error = True )
801800
802- GetStdHandle = windll .kernel32 .GetStdHandle
803- GetStdHandle .argtypes = [DWORD ]
804- GetStdHandle .restype = HANDLE
805-
806801 GetConsoleScreenBufferInfo = _KERNEL32 .GetConsoleScreenBufferInfo
807802 GetConsoleScreenBufferInfo .argtypes = [
808803 HANDLE ,
@@ -837,24 +832,18 @@ class INPUT_RECORD(Structure):
837832 FlushConsoleInputBuffer .argtypes = [HANDLE ]
838833 FlushConsoleInputBuffer .restype = BOOL
839834
840- WaitForSingleObject = _KERNEL32 .WaitForSingleObject
841- WaitForSingleObject .argtypes = [HANDLE , DWORD ]
842- WaitForSingleObject .restype = DWORD
843-
844- OutHandle = GetStdHandle (STD_OUTPUT_HANDLE )
845- InHandle = GetStdHandle (STD_INPUT_HANDLE )
835+ OutHandle = _winapi .GetStdHandle (_winapi .STD_OUTPUT_HANDLE )
836+ InHandle = _winapi .GetStdHandle (_winapi .STD_INPUT_HANDLE )
846837else :
847838
848839 def _win_only (* args , ** kwargs ):
849840 raise NotImplementedError ("Windows only" )
850841
851- GetStdHandle = _win_only
852842 GetConsoleScreenBufferInfo = _win_only
853843 ScrollConsoleScreenBuffer = _win_only
854844 GetConsoleMode = _win_only
855845 SetConsoleMode = _win_only
856846 ReadConsoleInput = _win_only
857847 FlushConsoleInputBuffer = _win_only
858- WaitForSingleObject = _win_only
859848 OutHandle = 0
860849 InHandle = 0
0 commit comments