Skip to content

Commit

Permalink
refactor: extract a method to reduce complexity
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
  • Loading branch information
miketheman committed Dec 23, 2021
1 parent 1d3755f commit 1e80c73
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pytest_socket.py
Expand Up @@ -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)
Expand Down

0 comments on commit 1e80c73

Please sign in to comment.