Skip to content

Commit 65f7d50

Browse files
authored
Merge pull request #153 from ahoppen/6.1/read-binary-mode
[6.1] Read stdout from sourcekit-lsp in binary mode instead of text mode
2 parents 7b48faa + 2101095 commit 65f7d50

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

test-sourcekit-lsp/test-sourcekit-lsp.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ def __init__(self, server_path: str):
3030
self.process = subprocess.Popen(
3131
[server_path],
3232
stdin=subprocess.PIPE,
33-
stdout=subprocess.PIPE,
34-
encoding="utf-8",
33+
stdout=subprocess.PIPE
3534
)
3635

3736
def send_data(self, dict: Dict[str, object]):
@@ -40,8 +39,8 @@ def send_data(self, dict: Dict[str, object]):
4039
"""
4140
assert self.process.stdin
4241
body = json.dumps(dict)
43-
data = "Content-Length: {}\r\n\r\n{}".format(len(body), body)
44-
self.process.stdin.write(data)
42+
data = f"Content-Length: {len(body)}\r\n\r\n{body}"
43+
self.process.stdin.write(data.encode('utf-8'))
4544
self.process.stdin.flush()
4645

4746
def read_message_from_lsp_server(self) -> str:
@@ -51,17 +50,16 @@ def read_message_from_lsp_server(self) -> str:
5150
"""
5251
assert self.process.stdout
5352
# Read Content-Length: 123\r\n
54-
# Note: Even though the Content-Length header ends with \r\n, `readline` returns it with a single \n.
55-
header = self.process.stdout.readline()
56-
match = re.match(r"Content-Length: ([0-9]+)\n$", header)
53+
header = self.process.stdout.readline().decode('utf-8')
54+
match = re.match(r"Content-Length: ([0-9]+)\r\n$", header)
5755
assert match, f"Expected Content-Length header, got '{header}'"
5856

5957
# The Content-Length header is followed by an empty line
60-
empty_line = self.process.stdout.readline()
61-
assert empty_line == "\n", f"Expected empty line, got '{empty_line}'"
58+
empty_line = self.process.stdout.readline().decode('utf-8')
59+
assert empty_line == "\r\n", f"Expected empty line, got '{empty_line}'"
6260

6361
# Read the actual response
64-
return self.process.stdout.read(int(match.group(1)))
62+
return self.process.stdout.read(int(match.group(1))).decode('utf-8')
6563

6664
def read_request_reply_from_lsp_server(self, request_id: int) -> str:
6765
"""
@@ -71,7 +69,7 @@ def read_request_reply_from_lsp_server(self, request_id: int) -> str:
7169
message = self.read_message_from_lsp_server()
7270
message_obj = json.loads(message)
7371
if "result" not in message_obj:
74-
# We received a message that wasn't the request reply.
72+
# We received a message that wasn't the request reply.
7573
# Log it, ignore it and wait for the next message.
7674
print("Received message")
7775
print(message)

0 commit comments

Comments
 (0)