Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ipykernel/inprocess/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ def kernel_info(self):
self._dispatch_to_kernel(msg)
return msg['header']['msg_id']

def comm_info(self, target_name=None):
"""Request a dictionary of valid comms and their targets."""
if target_name is None:
content = {}
else:
content = dict(target_name=target_name)
msg = self.session.msg('comm_info_request', content)
self._dispatch_to_kernel(msg)
return msg['header']['msg_id']

def input(self, string):
if self.kernel is None:
raise RuntimeError('Cannot send input reply. No kernel exists.')
Expand Down
22 changes: 20 additions & 2 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def __init__(self, **kwargs):
# Build dict of handlers for message types
msg_types = [ 'execute_request', 'complete_request',
'inspect_request', 'history_request',
'kernel_info_request',
'comm_info_request', 'kernel_info_request',
'connect_request', 'shutdown_request',
'apply_request', 'is_complete_request',
]
Expand Down Expand Up @@ -427,7 +427,7 @@ def inspect_request(self, stream, ident, parent):
def do_inspect(self, code, cursor_pos, detail_level=0):
"""Override in subclasses to allow introspection.
"""
return {'status': 'ok', 'data':{}, 'metadata':{}, 'found':False}
return {'status': 'ok', 'data': {}, 'metadata': {}, 'found': False}

def history_request(self, stream, ident, parent):
content = parent['content']
Expand Down Expand Up @@ -470,6 +470,24 @@ def kernel_info_request(self, stream, ident, parent):
self.kernel_info, parent, ident)
self.log.debug("%s", msg)

def comm_info_request(self, stream, ident, parent):
content = parent['content']
target_name = content.get('target_name', None)

# Should this be moved to ipkernel?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ipykernel? Actually, we should just answer the question now...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it is @minrk's call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a fine place for it.

if hasattr(self, 'comm_manager'):
comms = {
k: dict(target_name=v.target_name)
for (k, v) in self.comm_manager.comms.items()
if v.target_name == target_name or target_name is None
}
else:
comms = {}
reply_content = dict(comms=comms)
msg = self.session.send(stream, 'comm_info_reply',
reply_content, parent, ident)
self.log.debug("%s", msg)

def shutdown_request(self, stream, ident, parent):
content = self.do_shutdown(parent['content']['restart'])
self.session.send(stream, u'shutdown_reply', content, parent, ident=ident)
Expand Down
14 changes: 14 additions & 0 deletions ipykernel/tests/test_message_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from Queue import Empty # Py 2

import nose.tools as nt
from nose.plugins.skip import SkipTest

from traitlets import (
HasTraits, TraitError, Bool, Unicode, Dict, Integer, List, Enum,
Expand Down Expand Up @@ -163,6 +164,8 @@ def check(self, d):
Reference.check(self, d)
LanguageInfo().check(d['language_info'])

class CommInfoReply(Reference):
comms = Dict()

class IsCompleteReply(Reference):
status = Enum((u'complete', u'incomplete', u'invalid', u'unknown'), default_value=u'complete')
Expand Down Expand Up @@ -198,6 +201,7 @@ class DisplayData(MimeBundle):
class ExecuteResult(MimeBundle):
execution_count = Integer()


class HistoryReply(Reference):
history = List(List())

Expand All @@ -208,6 +212,7 @@ class HistoryReply(Reference):
'status' : Status(),
'complete_reply' : CompleteReply(),
'kernel_info_reply': KernelInfoReply(),
'comm_info_reply': CommInfoReply(),
'is_complete_reply': IsCompleteReply(),
'execute_input' : ExecuteInput(),
'execute_result' : ExecuteResult(),
Expand Down Expand Up @@ -419,6 +424,15 @@ def test_kernel_info_request():
validate_message(reply, 'kernel_info_reply', msg_id)


def test_comm_info_request():
flush_channels()
if not hasattr(KC, 'comm_info'):
raise SkipTest()
msg_id = KC.comm_info()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a if not hasattr(KC, 'comm_info'): raise SkipTest()? Then this test will run while comm_info is defined, but skip while jupyter_client is < 4.1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

reply = KC.get_shell_msg(timeout=TIMEOUT)
validate_message(reply, 'comm_info_reply', msg_id)


def test_single_payload():
flush_channels()
msg_id, reply = execute(code="for i in range(3):\n"+
Expand Down