Show More
@@ -216,7 +216,7 class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): | |||||
216 |
|
216 | |||
217 | See parent class :meth:`execute` docstring for full details. |
|
217 | See parent class :meth:`execute` docstring for full details. | |
218 | """ |
|
218 | """ | |
219 |
msg_id = self.kernel_client. |
|
219 | msg_id = self.kernel_client.execute(source, hidden) | |
220 | self._request_info['execute'][msg_id] = self._ExecutionRequest(msg_id, 'user') |
|
220 | self._request_info['execute'][msg_id] = self._ExecutionRequest(msg_id, 'user') | |
221 | self._hidden = hidden |
|
221 | self._hidden = hidden | |
222 | if not hidden: |
|
222 | if not hidden: | |
@@ -358,7 +358,7 class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): | |||||
358 | # generate uuid, which would be used as an indication of whether or |
|
358 | # generate uuid, which would be used as an indication of whether or | |
359 | # not the unique request originated from here (can use msg id ?) |
|
359 | # not the unique request originated from here (can use msg id ?) | |
360 | local_uuid = str(uuid.uuid1()) |
|
360 | local_uuid = str(uuid.uuid1()) | |
361 |
msg_id = self.kernel_client. |
|
361 | msg_id = self.kernel_client.execute('', | |
362 | silent=True, user_expressions={ local_uuid:expr }) |
|
362 | silent=True, user_expressions={ local_uuid:expr }) | |
363 | self._callback_dict[local_uuid] = callback |
|
363 | self._callback_dict[local_uuid] = callback | |
364 | self._request_info['execute'][msg_id] = self._ExecutionRequest(msg_id, 'silent_exec_callback') |
|
364 | self._request_info['execute'][msg_id] = self._ExecutionRequest(msg_id, 'silent_exec_callback') | |
@@ -671,7 +671,7 class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): | |||||
671 |
|
671 | |||
672 | # Send the metadata request to the kernel |
|
672 | # Send the metadata request to the kernel | |
673 | name = '.'.join(context) |
|
673 | name = '.'.join(context) | |
674 |
msg_id = self.kernel_client. |
|
674 | msg_id = self.kernel_client.object_info(name) | |
675 | pos = self._get_cursor().position() |
|
675 | pos = self._get_cursor().position() | |
676 | self._request_info['call_tip'] = self._CallTipRequest(msg_id, pos) |
|
676 | self._request_info['call_tip'] = self._CallTipRequest(msg_id, pos) | |
677 | return True |
|
677 | return True | |
@@ -682,7 +682,7 class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): | |||||
682 | context = self._get_context() |
|
682 | context = self._get_context() | |
683 | if context: |
|
683 | if context: | |
684 | # Send the completion request to the kernel |
|
684 | # Send the completion request to the kernel | |
685 |
msg_id = self.kernel_client. |
|
685 | msg_id = self.kernel_client.complete( | |
686 | '.'.join(context), # text |
|
686 | '.'.join(context), # text | |
687 | self._get_input_buffer_cursor_line(), # line |
|
687 | self._get_input_buffer_cursor_line(), # line | |
688 | self._get_input_buffer_cursor_column(), # cursor_pos |
|
688 | self._get_input_buffer_cursor_column(), # cursor_pos |
@@ -81,6 +81,7 class ZMQSocketChannel(Thread): | |||||
81 | stream = None |
|
81 | stream = None | |
82 | _address = None |
|
82 | _address = None | |
83 | _exiting = False |
|
83 | _exiting = False | |
|
84 | proxy_methods = [] | |||
84 |
|
85 | |||
85 | def __init__(self, context, session, address): |
|
86 | def __init__(self, context, session, address): | |
86 | """Create a channel. |
|
87 | """Create a channel. | |
@@ -175,6 +176,14 class ShellChannel(ZMQSocketChannel): | |||||
175 | command_queue = None |
|
176 | command_queue = None | |
176 | # flag for whether execute requests should be allowed to call raw_input: |
|
177 | # flag for whether execute requests should be allowed to call raw_input: | |
177 | allow_stdin = True |
|
178 | allow_stdin = True | |
|
179 | proxy_methods = [ | |||
|
180 | 'execute', | |||
|
181 | 'complete', | |||
|
182 | 'object_info', | |||
|
183 | 'history', | |||
|
184 | 'kernel_info', | |||
|
185 | 'shutdown', | |||
|
186 | ] | |||
178 |
|
187 | |||
179 | def __init__(self, context, session, address): |
|
188 | def __init__(self, context, session, address): | |
180 | super(ShellChannel, self).__init__(context, session, address) |
|
189 | super(ShellChannel, self).__init__(context, session, address) | |
@@ -451,6 +460,7 class StdInChannel(ZMQSocketChannel): | |||||
451 | """The stdin channel to handle raw_input requests that the kernel makes.""" |
|
460 | """The stdin channel to handle raw_input requests that the kernel makes.""" | |
452 |
|
461 | |||
453 | msg_queue = None |
|
462 | msg_queue = None | |
|
463 | proxy_methods = ['input'] | |||
454 |
|
464 | |||
455 | def __init__(self, context, session, address): |
|
465 | def __init__(self, context, session, address): | |
456 | super(StdInChannel, self).__init__(context, session, address) |
|
466 | super(StdInChannel, self).__init__(context, session, address) |
@@ -69,6 +69,34 class KernelClient(LoggingConfigurable, ConnectionFileMixin): | |||||
69 | _stdin_channel = Any |
|
69 | _stdin_channel = Any | |
70 | _hb_channel = Any |
|
70 | _hb_channel = Any | |
71 |
|
71 | |||
|
72 | # def __init__(self, *args, **kwargs): | |||
|
73 | # super(KernelClient, self).__init__(*args, **kwargs) | |||
|
74 | # # setup channel proxy methods, e.g. | |||
|
75 | # # Client.execute => shell_channel.execute | |||
|
76 | # for channel in ['shell', 'iopub', 'stdin', 'hb']: | |||
|
77 | # cls = getattr(self, '%s_channel_class' % channel) | |||
|
78 | # for method in cls.proxy_methods: | |||
|
79 | # setattr(self, method, self._proxy_method(channel, method)) | |||
|
80 | # | |||
|
81 | #-------------------------------------------------------------------------- | |||
|
82 | # Channel proxy methods | |||
|
83 | #-------------------------------------------------------------------------- | |||
|
84 | ||||
|
85 | def _get_msg(channel, *args, **kwargs): | |||
|
86 | return channel.get_msg(*args, **kwargs) | |||
|
87 | ||||
|
88 | def get_shell_msg(self, *args, **kwargs): | |||
|
89 | """Get a message from the shell channel""" | |||
|
90 | return self.shell_channel.get_msg(*args, **kwargs) | |||
|
91 | ||||
|
92 | def get_iopub_msg(self, *args, **kwargs): | |||
|
93 | """Get a message from the iopub channel""" | |||
|
94 | return self.iopub_channel.get_msg(*args, **kwargs) | |||
|
95 | ||||
|
96 | def get_stdin_msg(self, *args, **kwargs): | |||
|
97 | """Get a message from the stdin channel""" | |||
|
98 | return self.stdin_channel.get_msg(*args, **kwargs) | |||
|
99 | ||||
72 | #-------------------------------------------------------------------------- |
|
100 | #-------------------------------------------------------------------------- | |
73 | # Channel management methods |
|
101 | # Channel management methods | |
74 | #-------------------------------------------------------------------------- |
|
102 | #-------------------------------------------------------------------------- | |
@@ -84,10 +112,16 class KernelClient(LoggingConfigurable, ConnectionFileMixin): | |||||
84 | """ |
|
112 | """ | |
85 | if shell: |
|
113 | if shell: | |
86 | self.shell_channel.start() |
|
114 | self.shell_channel.start() | |
|
115 | for method in self.shell_channel.proxy_methods: | |||
|
116 | setattr(self, method, getattr(self.shell_channel, method)) | |||
87 | if iopub: |
|
117 | if iopub: | |
88 | self.iopub_channel.start() |
|
118 | self.iopub_channel.start() | |
|
119 | for method in self.iopub_channel.proxy_methods: | |||
|
120 | setattr(self, method, getattr(self.iopub_channel, method)) | |||
89 | if stdin: |
|
121 | if stdin: | |
90 | self.stdin_channel.start() |
|
122 | self.stdin_channel.start() | |
|
123 | for method in self.stdin_channel.proxy_methods: | |||
|
124 | setattr(self, method, getattr(self.stdin_channel, method)) | |||
91 | self.shell_channel.allow_stdin = True |
|
125 | self.shell_channel.allow_stdin = True | |
92 | else: |
|
126 | else: | |
93 | self.shell_channel.allow_stdin = False |
|
127 | self.shell_channel.allow_stdin = False |
@@ -63,20 +63,15 def flush_channels(kc=None): | |||||
63 |
|
63 | |||
64 | def execute(code='', kc=None, **kwargs): |
|
64 | def execute(code='', kc=None, **kwargs): | |
65 | """wrapper for doing common steps for validating an execution request""" |
|
65 | """wrapper for doing common steps for validating an execution request""" | |
66 | if kc is None: |
|
66 | msg_id = KC.execute(code=code, **kwargs) | |
67 | kc = KC |
|
67 | reply = KC.get_shell_msg(timeout=2) | |
68 | shell = kc.shell_channel |
|
|||
69 | sub = kc.iopub_channel |
|
|||
70 |
|
||||
71 | msg_id = shell.execute(code=code, **kwargs) |
|
|||
72 | reply = shell.get_msg(timeout=2) |
|
|||
73 | list(validate_message(reply, 'execute_reply', msg_id)) |
|
68 | list(validate_message(reply, 'execute_reply', msg_id)) | |
74 |
busy = |
|
69 | busy = KC.get_iopub_msg(timeout=2) | |
75 | list(validate_message(busy, 'status', msg_id)) |
|
70 | list(validate_message(busy, 'status', msg_id)) | |
76 | nt.assert_equal(busy['content']['execution_state'], 'busy') |
|
71 | nt.assert_equal(busy['content']['execution_state'], 'busy') | |
77 |
|
72 | |||
78 | if not kwargs.get('silent'): |
|
73 | if not kwargs.get('silent'): | |
79 |
pyin = |
|
74 | pyin = KC.get_iopub_msg(timeout=2) | |
80 | list(validate_message(pyin, 'pyin', msg_id)) |
|
75 | list(validate_message(pyin, 'pyin', msg_id)) | |
81 | nt.assert_equal(pyin['content']['code'], code) |
|
76 | nt.assert_equal(pyin['content']['code'], code) | |
82 |
|
77 | |||
@@ -302,9 +297,8 def validate_message(msg, msg_type=None, parent=None): | |||||
302 | def test_execute(): |
|
297 | def test_execute(): | |
303 | flush_channels() |
|
298 | flush_channels() | |
304 |
|
299 | |||
305 | shell = KC.shell_channel |
|
300 | msg_id = KC.execute(code='x=1') | |
306 | msg_id = shell.execute(code='x=1') |
|
301 | reply = KC.get_shell_msg(timeout=2) | |
307 | reply = shell.get_msg(timeout=2) |
|
|||
308 | for tst in validate_message(reply, 'execute_reply', msg_id): |
|
302 | for tst in validate_message(reply, 'execute_reply', msg_id): | |
309 | yield tst |
|
303 | yield tst | |
310 |
|
304 | |||
@@ -383,10 +377,8 def test_user_expressions(): | |||||
383 | def test_oinfo(): |
|
377 | def test_oinfo(): | |
384 | flush_channels() |
|
378 | flush_channels() | |
385 |
|
379 | |||
386 | shell = KC.shell_channel |
|
380 | msg_id = KC.object_info('a') | |
387 |
|
381 | reply = KC.get_shell_msg(timeout=2) | ||
388 | msg_id = shell.object_info('a') |
|
|||
389 | reply = shell.get_msg(timeout=2) |
|
|||
390 | for tst in validate_message(reply, 'object_info_reply', msg_id): |
|
382 | for tst in validate_message(reply, 'object_info_reply', msg_id): | |
391 | yield tst |
|
383 | yield tst | |
392 |
|
384 | |||
@@ -395,12 +387,10 def test_oinfo(): | |||||
395 | def test_oinfo_found(): |
|
387 | def test_oinfo_found(): | |
396 | flush_channels() |
|
388 | flush_channels() | |
397 |
|
389 | |||
398 | shell = KC.shell_channel |
|
|||
399 |
|
||||
400 | msg_id, reply = execute(code='a=5') |
|
390 | msg_id, reply = execute(code='a=5') | |
401 |
|
391 | |||
402 |
msg_id = |
|
392 | msg_id = KC.object_info('a') | |
403 |
reply = |
|
393 | reply = KC.get_shell_msg(timeout=2) | |
404 | for tst in validate_message(reply, 'object_info_reply', msg_id): |
|
394 | for tst in validate_message(reply, 'object_info_reply', msg_id): | |
405 | yield tst |
|
395 | yield tst | |
406 | content = reply['content'] |
|
396 | content = reply['content'] | |
@@ -413,12 +403,10 def test_oinfo_found(): | |||||
413 | def test_oinfo_detail(): |
|
403 | def test_oinfo_detail(): | |
414 | flush_channels() |
|
404 | flush_channels() | |
415 |
|
405 | |||
416 | shell = KC.shell_channel |
|
|||
417 |
|
||||
418 | msg_id, reply = execute(code='ip=get_ipython()') |
|
406 | msg_id, reply = execute(code='ip=get_ipython()') | |
419 |
|
407 | |||
420 |
msg_id = |
|
408 | msg_id = KC.object_info('ip.object_inspect', detail_level=2) | |
421 |
reply = |
|
409 | reply = KC.get_shell_msg(timeout=2) | |
422 | for tst in validate_message(reply, 'object_info_reply', msg_id): |
|
410 | for tst in validate_message(reply, 'object_info_reply', msg_id): | |
423 | yield tst |
|
411 | yield tst | |
424 | content = reply['content'] |
|
412 | content = reply['content'] | |
@@ -432,10 +420,8 def test_oinfo_detail(): | |||||
432 | def test_oinfo_not_found(): |
|
420 | def test_oinfo_not_found(): | |
433 | flush_channels() |
|
421 | flush_channels() | |
434 |
|
422 | |||
435 | shell = KC.shell_channel |
|
423 | msg_id = KC.object_info('dne') | |
436 |
|
424 | reply = KC.get_shell_msg(timeout=2) | ||
437 | msg_id = shell.object_info('dne') |
|
|||
438 | reply = shell.get_msg(timeout=2) |
|
|||
439 | for tst in validate_message(reply, 'object_info_reply', msg_id): |
|
425 | for tst in validate_message(reply, 'object_info_reply', msg_id): | |
440 | yield tst |
|
426 | yield tst | |
441 | content = reply['content'] |
|
427 | content = reply['content'] | |
@@ -446,12 +432,10 def test_oinfo_not_found(): | |||||
446 | def test_complete(): |
|
432 | def test_complete(): | |
447 | flush_channels() |
|
433 | flush_channels() | |
448 |
|
434 | |||
449 | shell = KC.shell_channel |
|
|||
450 |
|
||||
451 | msg_id, reply = execute(code="alpha = albert = 5") |
|
435 | msg_id, reply = execute(code="alpha = albert = 5") | |
452 |
|
436 | |||
453 |
msg_id = |
|
437 | msg_id = KC.complete('al', 'al', 2) | |
454 |
reply = |
|
438 | reply = KC.get_shell_msg(timeout=2) | |
455 | for tst in validate_message(reply, 'complete_reply', msg_id): |
|
439 | for tst in validate_message(reply, 'complete_reply', msg_id): | |
456 | yield tst |
|
440 | yield tst | |
457 | matches = reply['content']['matches'] |
|
441 | matches = reply['content']['matches'] | |
@@ -463,10 +447,8 def test_complete(): | |||||
463 | def test_kernel_info_request(): |
|
447 | def test_kernel_info_request(): | |
464 | flush_channels() |
|
448 | flush_channels() | |
465 |
|
449 | |||
466 | shell = KC.shell_channel |
|
450 | msg_id = KC.kernel_info() | |
467 |
|
451 | reply = KC.get_shell_msg(timeout=2) | ||
468 | msg_id = shell.kernel_info() |
|
|||
469 | reply = shell.get_msg(timeout=2) |
|
|||
470 | for tst in validate_message(reply, 'kernel_info_reply', msg_id): |
|
452 | for tst in validate_message(reply, 'kernel_info_reply', msg_id): | |
471 | yield tst |
|
453 | yield tst | |
472 |
|
454 |
@@ -106,22 +106,20 def test_embed_kernel_basic(): | |||||
106 | ]) |
|
106 | ]) | |
107 |
|
107 | |||
108 | with setup_kernel(cmd) as client: |
|
108 | with setup_kernel(cmd) as client: | |
109 | shell = client.shell_channel |
|
|||
110 |
|
||||
111 | # oinfo a (int) |
|
109 | # oinfo a (int) | |
112 |
msg_id = |
|
110 | msg_id = client.object_info('a') | |
113 |
msg = |
|
111 | msg = client.get_shell_msg(block=True, timeout=2) | |
114 | content = msg['content'] |
|
112 | content = msg['content'] | |
115 | nt.assert_true(content['found']) |
|
113 | nt.assert_true(content['found']) | |
116 |
|
114 | |||
117 |
msg_id = |
|
115 | msg_id = client.execute("c=a*2") | |
118 |
msg = |
|
116 | msg = client.get_shell_msg(block=True, timeout=2) | |
119 | content = msg['content'] |
|
117 | content = msg['content'] | |
120 | nt.assert_equal(content['status'], u'ok') |
|
118 | nt.assert_equal(content['status'], u'ok') | |
121 |
|
119 | |||
122 | # oinfo c (should be 10) |
|
120 | # oinfo c (should be 10) | |
123 |
msg_id = |
|
121 | msg_id = client.object_info('c') | |
124 |
msg = |
|
122 | msg = client.get_shell_msg(block=True, timeout=2) | |
125 | content = msg['content'] |
|
123 | content = msg['content'] | |
126 | nt.assert_true(content['found']) |
|
124 | nt.assert_true(content['found']) | |
127 | nt.assert_equal(content['string_form'], u'10') |
|
125 | nt.assert_equal(content['string_form'], u'10') | |
@@ -139,25 +137,23 def test_embed_kernel_namespace(): | |||||
139 | ]) |
|
137 | ]) | |
140 |
|
138 | |||
141 | with setup_kernel(cmd) as client: |
|
139 | with setup_kernel(cmd) as client: | |
142 | shell = client.shell_channel |
|
|||
143 |
|
||||
144 | # oinfo a (int) |
|
140 | # oinfo a (int) | |
145 |
msg_id = |
|
141 | msg_id = client.object_info('a') | |
146 |
msg = |
|
142 | msg = client.get_shell_msg(block=True, timeout=2) | |
147 | content = msg['content'] |
|
143 | content = msg['content'] | |
148 | nt.assert_true(content['found']) |
|
144 | nt.assert_true(content['found']) | |
149 | nt.assert_equal(content['string_form'], u'5') |
|
145 | nt.assert_equal(content['string_form'], u'5') | |
150 |
|
146 | |||
151 | # oinfo b (str) |
|
147 | # oinfo b (str) | |
152 |
msg_id = |
|
148 | msg_id = client.object_info('b') | |
153 |
msg = |
|
149 | msg = client.get_shell_msg(block=True, timeout=2) | |
154 | content = msg['content'] |
|
150 | content = msg['content'] | |
155 | nt.assert_true(content['found']) |
|
151 | nt.assert_true(content['found']) | |
156 | nt.assert_equal(content['string_form'], u'hi there') |
|
152 | nt.assert_equal(content['string_form'], u'hi there') | |
157 |
|
153 | |||
158 | # oinfo c (undefined) |
|
154 | # oinfo c (undefined) | |
159 |
msg_id = |
|
155 | msg_id = client.object_info('c') | |
160 |
msg = |
|
156 | msg = client.get_shell_msg(block=True, timeout=2) | |
161 | content = msg['content'] |
|
157 | content = msg['content'] | |
162 | nt.assert_false(content['found']) |
|
158 | nt.assert_false(content['found']) | |
163 |
|
159 | |||
@@ -177,17 +173,16 def test_embed_kernel_reentrant(): | |||||
177 | ]) |
|
173 | ]) | |
178 |
|
174 | |||
179 | with setup_kernel(cmd) as client: |
|
175 | with setup_kernel(cmd) as client: | |
180 | shell = client.shell_channel |
|
|||
181 | for i in range(5): |
|
176 | for i in range(5): | |
182 |
msg_id = |
|
177 | msg_id = client.object_info('count') | |
183 |
msg = |
|
178 | msg = client.get_shell_msg(block=True, timeout=2) | |
184 | content = msg['content'] |
|
179 | content = msg['content'] | |
185 | nt.assert_true(content['found']) |
|
180 | nt.assert_true(content['found']) | |
186 | nt.assert_equal(content['string_form'], unicode(i)) |
|
181 | nt.assert_equal(content['string_form'], unicode(i)) | |
187 |
|
182 | |||
188 | # exit from embed_kernel |
|
183 | # exit from embed_kernel | |
189 |
|
|
184 | client.execute("get_ipython().exit_now = True") | |
190 |
msg = |
|
185 | msg = client.get_shell_msg(block=True, timeout=2) | |
191 | time.sleep(0.2) |
|
186 | time.sleep(0.2) | |
192 |
|
187 | |||
193 |
|
188 |
General Comments 0
You need to be logged in to leave comments.
Login now