From b19016984f9790ce417e1a8b2ae21c11c0da2222 Mon Sep 17 00:00:00 2001 From: Tomoki Imai Date: Fri, 22 Nov 2024 22:05:32 +0900 Subject: [PATCH 1/2] Fix the wrong Content-Length in python-server.py for non-ascii characters. Content-Length is the data in bytes, not len of str. We should use sys.stdin.buffer.read instead of sys.stdin.read to receive bytes. _send_message should calculate "Content-Length" from bytes, not str. --- python_files/python_server.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/python_files/python_server.py b/python_files/python_server.py index 40133917a3ec..ebcb67d22a5e 100644 --- a/python_files/python_server.py +++ b/python_files/python_server.py @@ -14,7 +14,8 @@ def _send_message(msg: str): - length_msg = len(msg) + # Content-Length is the data size in bytes. + length_msg = len(msg.encode()) STDOUT.buffer.write(f"Content-Length: {length_msg}\r\n\r\n{msg}".encode()) STDOUT.buffer.flush() @@ -55,10 +56,11 @@ def custom_input(prompt=""): try: send_request({"prompt": prompt}) headers = get_headers() + # Content-Length is the data size in bytes. content_length = int(headers.get("Content-Length", 0)) if content_length: - message_text = STDIN.read(content_length) + message_text = STDIN.buffer.read(content_length).decode() message_json = json.loads(message_text) return message_json["result"]["userInput"] except Exception: @@ -74,10 +76,11 @@ def handle_response(request_id): while not STDIN.closed: try: headers = get_headers() + # Content-Length is the data size in bytes. content_length = int(headers.get("Content-Length", 0)) if content_length: - message_text = STDIN.read(content_length) + message_text = STDIN.buffer.read(content_length).decode() message_json = json.loads(message_text) our_user_input = message_json["result"]["userInput"] if message_json["id"] == request_id: @@ -172,10 +175,11 @@ def get_headers(): while not STDIN.closed: try: headers = get_headers() + # Content-Length is the data size in bytes. content_length = int(headers.get("Content-Length", 0)) if content_length: - request_text = STDIN.read(content_length) + request_text = STDIN.buffer.read(content_length).decode() request_json = json.loads(request_text) if request_json["method"] == "execute": execute(request_json, USER_GLOBALS) From 1c8e5f44802d69fdd5e7138c3ce43d8f2576ee5d Mon Sep 17 00:00:00 2001 From: Tomoki Imai Date: Sat, 23 Nov 2024 01:43:35 +0900 Subject: [PATCH 2/2] Use stdin.buffer in get_headers() We should use stdin.buffer.readline instead of stdin.readline because stdin.read* and stdin.buffer.read* should be used at the same time. (stdin.read* refers the internal buffer) --- python_files/python_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_files/python_server.py b/python_files/python_server.py index ebcb67d22a5e..1689d9b8f7f9 100644 --- a/python_files/python_server.py +++ b/python_files/python_server.py @@ -163,7 +163,7 @@ def get_value(self) -> str: def get_headers(): headers = {} while True: - line = STDIN.readline().strip() + line = STDIN.buffer.readline().decode().strip() if not line: break name, value = line.split(":", 1)