icmp_win: guard windows-only imports so the module is importable cross-platform

This commit is contained in:
Andrew Pimenov 2026-08-09 02:30:15 +03:00
parent 717068fa06
commit c020936d7f

View file

@ -51,9 +51,12 @@ import ctypes
import platform import platform
import socket import socket
import time import time
from ctypes import wintypes
_REPLY_BUFFER_SIZE = 4096 _REPLY_BUFFER_SIZE = 4096
_IS_WINDOWS = platform.system() == "Windows"
if _IS_WINDOWS:
from ctypes import wintypes
class _IpOptionInformation(ctypes.Structure): class _IpOptionInformation(ctypes.Structure):
@ -77,6 +80,8 @@ def win_icmp_available() -> bool:
def _handle(): def _handle():
if not _IS_WINDOWS:
raise OSError("icmp_win backend is only available on Windows")
iphlpapi = ctypes.WinDLL("Iphlpapi.dll") iphlpapi = ctypes.WinDLL("Iphlpapi.dll")
iphlpapi.IcmpCreateFile.restype = wintypes.HANDLE iphlpapi.IcmpCreateFile.restype = wintypes.HANDLE
# argtypes must be declared for HANDLE-typed args: ctypes otherwise # argtypes must be declared for HANDLE-typed args: ctypes otherwise