From 1e80c73b518c7f543cb5c7b3251a8f8a38041192 Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Thu, 23 Dec 2021 14:44:15 -0500 Subject: [PATCH] refactor: extract a method to reduce complexity Signed-off-by: Mike Fiedler --- pytest_socket.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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)