Skip to content

Commit d637af0

Browse files
committed
Revert "Try with shell execute"
This reverts commit 548140f.
1 parent 548140f commit d637af0

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

pythonFiles/tests/pytestadapter/helpers.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import pathlib
99
import random
10+
import socket
1011
import subprocess
1112
import sys
1213
import uuid
@@ -30,7 +31,56 @@ def test_output_file(root: pathlib.Path, ext: str = ".txt"):
3031
os.unlink(str(fullpath))
3132

3233

34+
def create_server(
35+
host: str = "127.0.0.1",
36+
port: int = 0,
37+
backlog: int = socket.SOMAXCONN,
38+
timeout: int = 1000,
39+
) -> socket.socket:
40+
"""Return a local server socket listening on the given port."""
41+
server: socket.socket = _new_sock()
42+
if port:
43+
# If binding to a specific port, make sure that the user doesn't have
44+
# to wait until the OS times out waiting for socket in order to use
45+
# that port again if the server or the adapter crash or are force-killed.
46+
if sys.platform == "win32":
47+
server.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
48+
else:
49+
try:
50+
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
51+
except (AttributeError, OSError):
52+
pass # Not available everywhere
53+
server.bind((host, port))
54+
if timeout:
55+
server.settimeout(timeout)
56+
server.listen(backlog)
57+
return server
58+
59+
60+
def _new_sock() -> socket.socket:
61+
sock: socket.socket = socket.socket(
62+
socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP
63+
)
64+
options = [
65+
("SOL_SOCKET", "SO_KEEPALIVE", 1),
66+
("IPPROTO_TCP", "TCP_KEEPIDLE", 1),
67+
("IPPROTO_TCP", "TCP_KEEPINTVL", 3),
68+
("IPPROTO_TCP", "TCP_KEEPCNT", 5),
69+
]
70+
71+
for level, name, value in options:
72+
try:
73+
sock.setsockopt(getattr(socket, level), getattr(socket, name), value)
74+
except (AttributeError, OSError):
75+
pass # May not be available everywhere.
76+
77+
return sock
78+
79+
3380
CONTENT_LENGTH: str = "Content-Length:"
81+
Env_Dict = TypedDict(
82+
"Env_Dict", {"TEST_UUID": str, "TEST_PORT": str, "PYTHONPATH": str}
83+
)
3484

3585

3686
def process_rpc_json(data: str) -> Dict[str, str]:
@@ -61,6 +111,7 @@ def runner(args: List[str]) -> Union[Dict[str, str], None]:
61111
"""Run the pytest discovery and return the JSON data from the server."""
62112
process_args: List[str] = [
63113
sys.executable,
114+
"-m",
64115
"pytest",
65116
"-p",
66117
"vscode_pytest",
@@ -80,9 +131,7 @@ def runner(args: List[str]) -> Union[Dict[str, str], None]:
80131
cwd=os.fspath(TEST_DATA_PATH),
81132
stdout=subprocess.PIPE,
82133
stderr=subprocess.PIPE,
83-
shell=True,
84134
)
85-
86135
if result.returncode != 0:
87136
print("Subprocess Run failed with:")
88137
print(result.stdout.decode(encoding="utf-8"))

0 commit comments

Comments
 (0)