Skip to content

Commit 2096feb

Browse files
authored
Merge pull request #244 from davidbrochart/jupyter_client
Prepare for jupyter_client 7.0
2 parents 64f1695 + 7f2dc91 commit 2096feb

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

jupyter_console/completer.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
from traitlets.config import Configurable
88
from traitlets import Float
99

10+
import jupyter_client
11+
12+
13+
# jupyter_client 7.0+ has async channel methods that we expect to be sync here
14+
if jupyter_client.version_info >= (7,):
15+
from jupyter_client.utils import run_sync
16+
else:
17+
run_sync = lambda x: x
18+
19+
1020
class ZMQCompleter(Configurable):
1121
"""Client-side completion machinery.
1222
@@ -31,7 +41,7 @@ def complete_request(self, code, cursor_pos):
3141
cursor_pos=cursor_pos,
3242
)
3343

34-
msg = self.client.shell_channel.get_msg(timeout=self.timeout)
44+
msg = run_sync(self.client.shell_channel.get_msg)(timeout=self.timeout)
3545
if msg['parent_header']['msg_id'] == msg_id:
3646
return msg['content']
3747

jupyter_console/ptshell.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@
7676
from pygments.util import ClassNotFound
7777
from pygments.token import Token
7878

79+
import jupyter_client
80+
81+
82+
# jupyter_client 7.0+ has async channel methods that we expect to be sync here
83+
# also, `block` was removed from `get_msg()`
84+
if jupyter_client._version.version_info[0] >= 7:
85+
from jupyter_client.utils import run_sync
86+
JUPYTER_CLIENT_7 = True
87+
else:
88+
run_sync = lambda x: x
89+
JUPYTER_CLIENT_7 = False
90+
7991

8092
def ask_yes_no(prompt, default=None, interrupt=None):
8193
"""Asks a question and returns a boolean (y/n) answer.
@@ -705,8 +717,8 @@ def run_cell(self, cell, store_history=True):
705717
return
706718

707719
# flush stale replies, which could have been ignored, due to missed heartbeats
708-
while self.client.shell_channel.msg_ready():
709-
self.client.shell_channel.get_msg()
720+
while run_sync(self.client.shell_channel.msg_ready)():
721+
run_sync(self.client.shell_channel.get_msg)()
710722
# execute takes 'hidden', which is the inverse of store_hist
711723
msg_id = self.client.execute(cell, not store_history)
712724

@@ -739,7 +751,10 @@ def run_cell(self, cell, store_history=True):
739751
#-----------------
740752

741753
def handle_execute_reply(self, msg_id, timeout=None):
742-
msg = self.client.shell_channel.get_msg(block=False, timeout=timeout)
754+
kwargs = {"timeout": timeout}
755+
if not JUPYTER_CLIENT_7:
756+
kwargs["block"] = False
757+
msg = run_sync(self.client.shell_channel.get_msg)(**kwargs)
743758
if msg["parent_header"].get("msg_id", None) == msg_id:
744759

745760
self.handle_iopub(msg_id)
@@ -778,7 +793,10 @@ def handle_is_complete_reply(self, msg_id, timeout=None):
778793
## Get the is_complete response:
779794
msg = None
780795
try:
781-
msg = self.client.shell_channel.get_msg(block=True, timeout=timeout)
796+
kwargs = {"timeout": timeout}
797+
if not JUPYTER_CLIENT_7:
798+
kwargs["block"] = True
799+
msg = run_sync(self.client.shell_channel.get_msg)(**kwargs)
782800
except Empty:
783801
warn('The kernel did not respond to an is_complete_request. '
784802
'Setting `use_kernel_is_complete` to False.')
@@ -849,8 +867,8 @@ def handle_iopub(self, msg_id=''):
849867
850868
It only displays output that is caused by this session.
851869
"""
852-
while self.client.iopub_channel.msg_ready():
853-
sub_msg = self.client.iopub_channel.get_msg()
870+
while run_sync(self.client.iopub_channel.msg_ready)():
871+
sub_msg = run_sync(self.client.iopub_channel.get_msg)()
854872
msg_type = sub_msg['header']['msg_type']
855873

856874
# Update execution_count in case it changed in another session
@@ -1003,7 +1021,7 @@ def handle_image_callable(self, data, mime):
10031021
def handle_input_request(self, msg_id, timeout=0.1):
10041022
""" Method to capture raw_input
10051023
"""
1006-
req = self.client.stdin_channel.get_msg(timeout=timeout)
1024+
req = run_sync(self.client.stdin_channel.get_msg)(timeout=timeout)
10071025
# in case any iopub came while we were waiting:
10081026
self.handle_iopub(msg_id)
10091027
if msg_id == req["parent_header"].get("msg_id"):
@@ -1032,6 +1050,6 @@ def double_int(sig, frame):
10321050

10331051
# only send stdin reply if there *was not* another request
10341052
# or execution finished while we were reading.
1035-
if not (self.client.stdin_channel.msg_ready() or
1036-
self.client.shell_channel.msg_ready()):
1053+
if not (run_sync(self.client.stdin_channel.msg_ready)() or
1054+
run_sync(self.client.shell_channel.msg_ready)()):
10371055
self.client.input(raw_data)

0 commit comments

Comments
 (0)