From 328a8c47a6e8d844fae022ca3c6558e24269b267 Mon Sep 17 00:00:00 2001 From: Jace Browning Date: Thu, 19 Oct 2023 15:35:49 -0400 Subject: [PATCH 1/2] Ensure exceptions are pickleable for multiprocess support --- pytest_socket.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/pytest_socket.py b/pytest_socket.py index 96611e3..0440f13 100644 --- a/pytest_socket.py +++ b/pytest_socket.py @@ -11,18 +11,11 @@ class SocketBlockedError(RuntimeError): - def __init__(self, *_args, **_kwargs): - super().__init__("A test tried to use socket.socket.") + pass class SocketConnectBlockedError(RuntimeError): - def __init__(self, allowed, host, *_args, **_kwargs): - if allowed: - allowed = ",".join(allowed) - super().__init__( - "A test tried to use socket.socket.connect() " - f'with host "{host}" (allowed: "{allowed}").' - ) + pass def pytest_addoption(parser): @@ -86,7 +79,7 @@ def __new__(cls, family=-1, type=-1, proto=-1, fileno=None): if _is_unix_socket(family) and allow_unix_socket: return super().__new__(cls, family, type, proto, fileno) - raise SocketBlockedError() + raise SocketBlockedError("A test tried to use socket.socket.") socket.socket = GuardedSocket @@ -247,7 +240,10 @@ def guarded_connect(inst, *args): ): return _true_connect(inst, *args) - raise SocketConnectBlockedError(allowed_list, host) + allowed = ",".join(allowed_list) if allowed_list else "[]" + message = ("A test tried to use socket.socket.connect() " + f'with host "{host}" (allowed: "{allowed}").') + raise SocketConnectBlockedError(message) socket.socket.connect = guarded_connect From 345b5c63b4315198018dc3344fcbdd7b8b23430b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 21:11:03 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytest_socket.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pytest_socket.py b/pytest_socket.py index 0440f13..a07738c 100644 --- a/pytest_socket.py +++ b/pytest_socket.py @@ -241,8 +241,10 @@ def guarded_connect(inst, *args): return _true_connect(inst, *args) allowed = ",".join(allowed_list) if allowed_list else "[]" - message = ("A test tried to use socket.socket.connect() " - f'with host "{host}" (allowed: "{allowed}").') + message = ( + "A test tried to use socket.socket.connect() " + f'with host "{host}" (allowed: "{allowed}").' + ) raise SocketConnectBlockedError(message) socket.socket.connect = guarded_connect