Skip to content

Commit 8acbcc8

Browse files
committed
Optimize writing to engine stdin
1 parent 489b49d commit 8acbcc8

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

chess/engine.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -936,13 +936,13 @@ def write(self, data: bytes) -> None:
936936

937937
if line.startswith("ping ") and self.expected_pings:
938938
self.expected_pings -= 1
939-
self.protocol.pipe_data_received(1, line.replace("ping ", "pong ").encode("utf-8") + b"\n")
939+
self.protocol.pipe_data_received(1, (line.replace("ping ", "pong ") + "\n").encode("utf-8"))
940940
else:
941941
assert self.expectations, f"unexpected: {line!r}"
942942
expectation, responses = self.expectations.popleft()
943943
assert expectation == line, f"expected {expectation}, got: {line}"
944944
if responses:
945-
self.protocol.pipe_data_received(1, "\n".join(responses).encode("utf-8") + b"\n")
945+
self.protocol.pipe_data_received(1, "\n".join(responses + [""]).encode("utf-8"))
946946

947947
def get_pid(self) -> int:
948948
return id(self)
@@ -1010,8 +1010,7 @@ def send_line(self, line: str) -> None:
10101010
assert self.transport is not None, "cannot send line before connection is made"
10111011
stdin = self.transport.get_pipe_transport(0)
10121012
# WriteTransport expected, but not checked to allow duck typing.
1013-
stdin.write(line.encode("utf-8")) # type: ignore
1014-
stdin.write(b"\n") # type: ignore
1013+
stdin.write((line + "\n").encode("utf-8")) # type: ignore
10151014

10161015
def pipe_data_received(self, fd: int, data: Union[bytes, str]) -> None:
10171016
self.buffer[fd].extend(data) # type: ignore

0 commit comments

Comments
 (0)