forked from datalayer/jupyter-kernel-client
-
Notifications
You must be signed in to change notification settings - Fork 1
Add serializers and deserializers for Jupyter's default websocket protocol #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b9b1cef
Add serializers and deserializers for Jupyter's default websocket
5184ea4
didn't realize that was a required arg for
63af587
ugh from_bytes as well
wh1ter4bb1t-js 62961bb
in tests as well
wh1ter4bb1t-js 97488c5
address comments
wh1ter4bb1t-js a5bfff5
merge
wh1ter4bb1t-js c7831de
fix uv.lock
wh1ter4bb1t-js 137dee2
fix uv.lock
wh1ter4bb1t-js File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| # Copyright (c) 2023-2024 Datalayer, Inc. | ||
| # Copyright (c) 2025 Google | ||
| # | ||
| # BSD 3-Clause License | ||
|
|
||
| import json | ||
| from jupyter_kernel_client.utils import serialize_msg_to_ws_json, serialize_msg_to_ws_default, deserialize_msg_from_ws_default, serialize_msg_to_ws_v1, deserialize_msg_from_ws_v1 | ||
|
|
||
| def test_serialize_msg_to_ws_json(): | ||
| src_msg = { | ||
| "header": { | ||
| "msg_id": "c0a8012e-1f3b-4c3a-9c77-123456789abc", | ||
| "username": "test-user", | ||
| "session": "1c2d3e4f-aaaa-bbbb-cccc-0123456789ab", | ||
| "date": "2026-01-26T12:34:56.789Z", | ||
| "msg_type": "execute_request", | ||
| "version": "5.3", | ||
| }, | ||
| "parent_header": {}, | ||
| "metadata": {}, | ||
| "content": { | ||
| "code": "print('hello world')", | ||
| "silent": False, | ||
| "store_history": True, | ||
| "user_expressions": {}, | ||
| "allow_stdin": True, | ||
| "stop_on_error": True, | ||
| }, | ||
| "buffers": [], | ||
| } | ||
|
|
||
| expected_output = json.dumps(src_msg) | ||
| serialized_msg = serialize_msg_to_ws_json(src_msg) | ||
| assert expected_output == serialized_msg | ||
|
|
||
| def test_serialize_and_deserialize_msg_to_ws_default(): | ||
| src_msg = { | ||
| "header": { | ||
| "msg_id": "7f3b9d8d-2c6c-4b71-9b64-111111111111", | ||
| "username": "test-user", | ||
| "session": "1c2d3e4f-aaaa-bbbb-cccc-0123456789ab", | ||
| "date": "2026-01-26T12:35:10.123Z", | ||
| "msg_type": "comm_msg", | ||
| "version": "5.3", | ||
| }, | ||
| "parent_header": {}, | ||
| "metadata": { | ||
| "buffer_paths": [ | ||
| ["content", "data", "payload"], | ||
| ["content", "data", "extra_blob"] | ||
| ] | ||
| }, | ||
| "content": { | ||
| "comm_id": "abc123abc123abc123abc123abc123ab", | ||
| "target_name": "my_binary_comm", | ||
| "data": { | ||
| "dtype": "uint8", | ||
| "shape": [4], | ||
| "payload": None, | ||
| "extra_blob": None, | ||
| "note": "payload + extra_blob come from buffers", | ||
| }, | ||
| }, | ||
| "buffers": [ | ||
| b"\x01\x02\x03\x04", | ||
| b"\xde\xad\xbe\xef\x00\xff", | ||
| ], | ||
| } | ||
|
|
||
| serialized_msg = serialize_msg_to_ws_default(src_msg) | ||
| bufn = int.from_bytes(serialized_msg[0:4], byteorder="big") | ||
| buffers = src_msg['buffers'] or [] | ||
|
|
||
| for i in range(1, bufn): | ||
| # ignore the json message for now, it's tested the deserialized msg | ||
| start = (i+1) * 4 | ||
| offset = int.from_bytes(serialized_msg[start:start+4], byteorder="big") | ||
| buf = buffers[i-1] | ||
| serialized_buf_val = serialized_msg[offset:offset+len(buf)] | ||
| assert serialized_buf_val == buf | ||
|
|
||
| deserialized_msg = deserialize_msg_from_ws_default(serialized_msg) | ||
| assert deserialized_msg == src_msg | ||
|
|
||
| def test_serialize_and_deserialize_msg_to_ws_v1(): | ||
| def pack(obj) -> bytes: | ||
| return json.dumps(obj, separators=(",", ":"), sort_keys=True).encode("utf-8") | ||
|
|
||
| src_msg = { | ||
| "channel": "shell", | ||
| "header": { | ||
| "msg_id": "7f3b9d8d-2c6c-4b71-9b64-111111111111", | ||
| "username": "test-user", | ||
| "session": "1c2d3e4f-aaaa-bbbb-cccc-0123456789ab", | ||
| "date": "2026-01-26T12:35:10.123Z", | ||
| "msg_type": "comm_msg", | ||
| "version": "5.3", | ||
| }, | ||
| "parent_header": {}, | ||
| "metadata": { | ||
| "buffer_paths": [ | ||
| ["content", "data", "payload"], | ||
| ["content", "data", "extra_blob"] | ||
| ] | ||
| }, | ||
| "content": { | ||
| "comm_id": "abc123abc123abc123abc123abc123ab", | ||
| "target_name": "my_binary_comm", | ||
| "data": { | ||
| "dtype": "uint8", | ||
| "shape": [4], | ||
| "payload": None, | ||
| "extra_blob": None, | ||
| "note": "payload + extra_blob come from buffers", | ||
| }, | ||
| }, | ||
| "buffers": [ | ||
| b"\x01\x02\x03\x04", | ||
| b"\xde\xad\xbe\xef\x00\xff", | ||
| ], | ||
| } | ||
|
|
||
| serialized_msg = serialize_msg_to_ws_v1(src_msg, channel="shell", pack=pack) | ||
| # construct the msg lists for the serialized msg | ||
| offset = int.from_bytes(serialized_msg[:8], byteorder="little") | ||
| offsets = [ | ||
| int.from_bytes(serialized_msg[8 * (i + 1) : 8 * (i + 2)], byteorder="little") for i in range(offset) | ||
| ] | ||
| serialized_list = [serialized_msg[offsets[i]:offsets[i+1]] for i in range(1, offset-1)] | ||
|
|
||
| _, deserialized_msg = deserialize_msg_from_ws_v1(serialized_msg) | ||
| assert serialized_list == deserialized_msg | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.