Skip to content

Commit 9a9f12f

Browse files
jamesbrinkclaude
andcommitted
fix(windows): improve win32api handling for Windows tests
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e27c3a1 commit 9a9f12f

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

mcp_nixos/run.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,31 @@ def signal_handler(signum, frame):
195195
# Windows doesn't support all the same signals
196196
signal.signal(signal.SIGINT, signal_handler)
197197
signal.signal(signal.SIGTERM, signal_handler)
198+
198199
# Add Windows-specific handler for CTRL events
199-
# First check if win32api is available
200-
win32api_available = False
201-
try:
202-
import win32api
200+
win32api_module = sys.modules.get("win32api", None)
203201

204-
win32api_available = True
205-
except ImportError:
206-
# win32api not available, fallback to basic handling
207-
print("Warning: win32api not available, Windows CTRL event handling is limited")
202+
# If win32api is None in the module cache, try to import it
203+
if win32api_module is None:
204+
try:
205+
import win32api
208206

209-
# Only setup the handler if win32api is available
210-
if win32api_available:
211-
win32api.SetConsoleCtrlHandler(
212-
lambda ctrl_type: signal_handler(signal.SIGINT, None) if ctrl_type == 0 else None, True
213-
)
207+
win32api_module = win32api
208+
except ImportError:
209+
# win32api not available, fallback to basic handling
210+
print("Warning: win32api not available, Windows CTRL event handling is limited")
211+
212+
# Handle case where win32api is patched to None in tests
213+
if win32api_module is not None:
214+
try:
215+
win32api_module.SetConsoleCtrlHandler(
216+
lambda ctrl_type: signal_handler(signal.SIGINT, None) if ctrl_type == 0 else None, True
217+
)
218+
except AttributeError:
219+
# This can happen if win32api was mocked to None in tests
220+
print("Warning: win32api not available, Windows CTRL event handling is limited")
221+
else:
222+
print("Warning: win32api not available, Windows CTRL event handling is limited")
214223
else:
215224
# Unix signals
216225
for sig in (signal.SIGINT, signal.SIGTERM):

0 commit comments

Comments
 (0)