Skip to content

Commit 5bf7b98

Browse files
committed
pytestadapter: Remove unused code for connecting to a unix socket.
1 parent ee981bd commit 5bf7b98

1 file changed

Lines changed: 26 additions & 91 deletions

File tree

python_files/tests/pytestadapter/helpers.py

Lines changed: 26 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -144,57 +144,30 @@ def _listen_on_fifo(pipe_name: str, result: List[str], completed: threading.Even
144144
result.append(data)
145145

146146

147-
def _listen_on_pipe_new(listener, result: List[str], completed: threading.Event):
148-
"""Listen on the named pipe or Unix domain socket for JSON data from the server.
149-
150-
Created as a separate function for clarity in threading context.
151-
"""
152-
# Windows design
153-
if sys.platform == "win32":
154-
all_data: list = []
155-
stream = listener.wait()
156-
while True:
157-
# Read data from collection
158-
close = stream.closed
159-
if close:
160-
break
161-
data = stream.readlines()
162-
if not data:
163-
if completed.is_set():
164-
break # Exit loop if completed event is set
165-
else:
166-
try:
167-
# Attempt to accept another connection if the current one closes unexpectedly
168-
print("attempt another connection")
169-
except socket.timeout:
170-
# On timeout, append all collected data to result and return
171-
# result.append("".join(all_data))
172-
return
173-
data_decoded = "".join(data)
174-
all_data.append(data_decoded)
175-
# Append all collected data to result array
176-
result.append("".join(all_data))
177-
else: # Unix design
178-
connection, _ = listener.socket.accept()
179-
listener.socket.settimeout(1)
180-
all_data: list = []
181-
while True:
182-
# Reading from connection
183-
data: bytes = connection.recv(1024 * 1024)
184-
if not data:
185-
if completed.is_set():
186-
break # Exit loop if completed event is set
187-
else:
188-
try:
189-
# Attempt to accept another connection if the current one closes unexpectedly
190-
connection, _ = listener.socket.accept()
191-
except socket.timeout:
192-
# On timeout, append all collected data to result and return
193-
result.append("".join(all_data))
194-
return
195-
all_data.append(data.decode("utf-8"))
196-
# Append all collected data to result array
197-
result.append("".join(all_data))
147+
def _listen_win_named_pipe(listener, result: List[str], completed: threading.Event):
148+
all_data: list = []
149+
stream = listener.wait()
150+
while True:
151+
# Read data from collection
152+
close = stream.closed
153+
if close:
154+
break
155+
data = stream.readlines()
156+
if not data:
157+
if completed.is_set():
158+
break # Exit loop if completed event is set
159+
else:
160+
try:
161+
# Attempt to accept another connection if the current one closes unexpectedly
162+
print("attempt another connection")
163+
except socket.timeout:
164+
# On timeout, append all collected data to result and return
165+
# result.append("".join(all_data))
166+
return
167+
data_decoded = "".join(data)
168+
all_data.append(data_decoded)
169+
# Append all collected data to result array
170+
result.append("".join(all_data))
198171

199172

200173
def _run_test_code(proc_args: List[str], proc_env, proc_cwd: str, completed: threading.Event):
@@ -314,7 +287,7 @@ def runner_with_cwd_env(
314287

315288
result = [] # result is a string array to store the data during threading
316289
t1: threading.Thread = threading.Thread(
317-
target=_listen_on_pipe_new, args=(pipe, result, completed)
290+
target=_listen_win_named_pipe, args=(pipe, result, completed)
318291
)
319292
t1.start()
320293

@@ -340,13 +313,10 @@ def runner_with_cwd_env(
340313
# if additional environment variables are passed, add them to the environment
341314
if env_add:
342315
env.update(env_add)
343-
# server = UnixPipeServer(pipe_name)
344-
# server.start()
345-
#################
316+
346317
# Create the FIFO (named pipe) if it doesn't exist
347318
# if not pathlib.Path.exists(pipe_name):
348319
os.mkfifo(pipe_name)
349-
#################
350320

351321
completed = threading.Event()
352322

@@ -432,38 +402,3 @@ def generate_random_pipe_name(prefix=""):
432402
return os.path.join(xdg_runtime_dir, f"{prefix}-{random_suffix}") # noqa: PTH118
433403
else:
434404
return os.path.join(tempfile.gettempdir(), f"{prefix}-{random_suffix}") # noqa: PTH118
435-
436-
437-
class UnixPipeServer:
438-
def __init__(self, name):
439-
self.name = name
440-
self.is_windows = sys.platform == "win32"
441-
if self.is_windows:
442-
raise NotImplementedError(
443-
"This class is only intended for Unix-like systems, not Windows."
444-
)
445-
else:
446-
# For Unix-like systems, use a Unix domain socket.
447-
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
448-
# Ensure the socket does not already exist
449-
try:
450-
os.unlink(self.name) # noqa: PTH108
451-
except OSError:
452-
if os.path.exists(self.name): # noqa: PTH110
453-
raise
454-
455-
def start(self):
456-
if self.is_windows:
457-
raise NotImplementedError(
458-
"This class is only intended for Unix-like systems, not Windows."
459-
)
460-
else:
461-
# Bind the socket to the address and listen for incoming connections.
462-
self.socket.bind(self.name)
463-
self.socket.listen(1)
464-
print(f"Server listening on {self.name}")
465-
466-
def stop(self):
467-
# Clean up the server socket.
468-
self.socket.close()
469-
print("Server stopped.")

0 commit comments

Comments
 (0)