diff --git a/pytest_socket.py b/pytest_socket.py index a2fb3d4..e47355f 100644 --- a/pytest_socket.py +++ b/pytest_socket.py @@ -159,17 +159,17 @@ def socket_allow_hosts(allowed=None, allow_unix_socket=False): if not isinstance(allowed, list): return - def guarded_connect(inst, *args): - host = host_from_connect_args(args) - if host in allowed: - return _true_connect(inst, *args) - + def _is_unix_socket(inst) -> bool: try: is_unix_socket = inst.family == socket.AF_UNIX except AttributeError: # AF_UNIX not supported on Windows https://bugs.python.org/issue33408 is_unix_socket = False - if allow_unix_socket and is_unix_socket: + return is_unix_socket + + def guarded_connect(inst, *args): + host = host_from_connect_args(args) + if host in allowed or (_is_unix_socket(inst) and allow_unix_socket): return _true_connect(inst, *args) raise SocketConnectBlockedError(allowed, host)