Skip to content

Commit 0e1aec1

Browse files
fix: if a comm target is not know, give a warning and continue
1 parent 24c9b03 commit 0e1aec1

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

nbclient/client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -853,11 +853,15 @@ def handle_comm_msg(self, outs, msg, cell_index):
853853
# There are cases where we need to mimic a frontend, to get similar behaviour as
854854
# when using the Output widget from Jupyter lab/notebook
855855
if msg['msg_type'] == 'comm_open':
856-
handler = self.comm_open_handlers.get(msg['content'].get('target_name'))
857-
comm_id = msg['content']['comm_id']
858-
comm_object = handler(msg)
859-
if comm_object:
860-
self.comm_objects[comm_id] = comm_object
856+
target = msg['content'].get('target_name')
857+
handler = self.comm_open_handlers.get(target)
858+
if handler:
859+
comm_id = msg['content']['comm_id']
860+
comm_object = handler(msg)
861+
if comm_object:
862+
self.comm_objects[comm_id] = comm_object
863+
else:
864+
self.log.warning(f'No handler found for comm target {target!r}')
861865
elif msg['msg_type'] == 'comm_msg':
862866
content = msg['content']
863867
comm_id = msg['content']['comm_id']
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"ExecuteTime": {
8+
"end_time": "2020-05-29T11:16:26.365338Z",
9+
"start_time": "2020-05-29T11:16:26.362047Z"
10+
}
11+
},
12+
"outputs": [],
13+
"source": [
14+
"from ipykernel.comm import Comm"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 2,
20+
"metadata": {
21+
"ExecuteTime": {
22+
"end_time": "2020-05-29T11:16:26.377700Z",
23+
"start_time": "2020-05-29T11:16:26.371603Z"
24+
}
25+
},
26+
"outputs": [],
27+
"source": [
28+
"comm = Comm('this-comm-tests-a-missing-handler', data={'id': 'foo'})"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 3,
34+
"metadata": {
35+
"ExecuteTime": {
36+
"end_time": "2020-05-29T11:16:26.584520Z",
37+
"start_time": "2020-05-29T11:16:26.581213Z"
38+
}
39+
},
40+
"outputs": [],
41+
"source": [
42+
"comm.send(data={'id': 'bar'})"
43+
]
44+
}
45+
],
46+
"metadata": {
47+
"kernelspec": {
48+
"display_name": "Python 3",
49+
"language": "python",
50+
"name": "python3"
51+
},
52+
"language_info": {
53+
"codemirror_mode": {
54+
"name": "ipython",
55+
"version": 3
56+
},
57+
"file_extension": ".py",
58+
"mimetype": "text/x-python",
59+
"name": "python",
60+
"nbconvert_exporter": "python",
61+
"pygments_lexer": "ipython3",
62+
"version": "3.7.3"
63+
}
64+
},
65+
"nbformat": 4,
66+
"nbformat_minor": 2
67+
}

nbclient/tests/test_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ def notebook_resources():
243243
@pytest.mark.parametrize(
244244
["input_name", "opts"],
245245
[
246+
("Other Comms.ipynb", dict(kernel_name="python")),
246247
("Clear Output.ipynb", dict(kernel_name="python")),
247248
("Empty Cell.ipynb", dict(kernel_name="python")),
248249
("Factorials.ipynb", dict(kernel_name="python")),

0 commit comments

Comments
 (0)