From 5c9aa6a21a9555f383d67aebe884d3d766929f20 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 7 Nov 2023 17:24:02 +0530 Subject: [PATCH] json.loads() stupidly does not accept memoryview --- kitty/boss.py | 2 +- kitty/remote_control.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index fe2a4dafe..e0a7c5e98 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -775,7 +775,7 @@ class Boss: cmd_prefix = b'\x1bP@kitty-cmd' terminator = b'\x1b\\' if msg_bytes.startswith(cmd_prefix) and msg_bytes.endswith(terminator): - cmd = memoryview(msg_bytes[len(cmd_prefix):-len(terminator)]) + cmd = memoryview(msg_bytes)[len(cmd_prefix):-len(terminator)] response = self._handle_remote_command(cmd, peer_id=peer_id) if response is None: return None diff --git a/kitty/remote_control.py b/kitty/remote_control.py index 1c168d5b4..f24396db3 100644 --- a/kitty/remote_control.py +++ b/kitty/remote_control.py @@ -53,8 +53,10 @@ def encode_response_for_peer(response: Any) -> bytes: def parse_cmd(serialized_cmd: memoryview, encryption_key: EllipticCurveKey) -> Dict[str, Any]: + # See https://github.com/python/cpython/issues/74379 for why we cant use + # memoryview directly :(( try: - pcmd = json.loads(serialized_cmd) + pcmd = json.loads(bytes(serialized_cmd)) except Exception: return {} if not isinstance(pcmd, dict) or 'version' not in pcmd: