Skip to content

Commit 9f78f55

Browse files
committed
Use channel get_msg helper method
1 parent 820c502 commit 9f78f55

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed

ipykernel/inprocess/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ def _dispatch_to_kernel(self, msg):
178178
idents, reply_msg = self.session.recv(stream, copy=False)
179179
self.shell_channel.call_handlers_later(reply_msg)
180180

181+
def get_shell_msg(self, block=True, timeout=None):
182+
return self.shell_channel.get_msg(block, timeout)
183+
184+
def get_iopub_msg(self, block=True, timeout=None):
185+
return self.iopub_channel.get_msg(block, timeout)
186+
187+
def get_stdin_msg(self, block=True, timeout=None):
188+
return self.stdin_channel.get_msg(block, timeout)
189+
190+
def get_control_msg(self, block=True, timeout=None):
191+
return self.control_channel.get_msg(block, timeout)
192+
181193

182194
#-----------------------------------------------------------------------------
183195
# ABC Registration

ipykernel/inprocess/tests/test_kernel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_pylab(self):
6565
"""Does %pylab work in the in-process kernel?"""
6666
kc = self.kc
6767
kc.execute('%pylab')
68-
out, err = assemble_output(kc.iopub_channel)
68+
out, err = assemble_output(kc.get_iopub_msg)
6969
self.assertIn('matplotlib', out)
7070

7171
def test_raw_input(self):
@@ -96,7 +96,7 @@ def test_stdout(self):
9696
kc = BlockingInProcessKernelClient(kernel=kernel, session=kernel.session)
9797
kernel.frontends.append(kc)
9898
kc.execute('print("bar")')
99-
out, err = assemble_output(kc.iopub_channel)
99+
out, err = assemble_output(kc.get_iopub_msg)
100100
assert out == 'bar\n'
101101

102102
def test_getpass_stream(self):

ipykernel/tests/test_kernel.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _check_master(kc, expected=True, stream="stdout"):
2929
execute(kc=kc, code="import sys")
3030
flush_channels(kc)
3131
msg_id, content = execute(kc=kc, code="print (sys.%s._is_master_process())" % stream)
32-
stdout, stderr = assemble_output(kc.iopub_channel)
32+
stdout, stderr = assemble_output(kc.get_iopub_msg)
3333
assert stdout.strip() == repr(expected)
3434

3535

@@ -46,7 +46,7 @@ def test_simple_print():
4646
with kernel() as kc:
4747
iopub = kc.iopub_channel
4848
msg_id, content = execute(kc=kc, code="print ('hi')")
49-
stdout, stderr = assemble_output(iopub)
49+
stdout, stderr = assemble_output(kc.get_iopub_msg)
5050
assert stdout == 'hi\n'
5151
assert stderr == ''
5252
_check_master(kc, expected=True)
@@ -56,7 +56,7 @@ def test_sys_path():
5656
"""test that sys.path doesn't get messed up by default"""
5757
with kernel() as kc:
5858
msg_id, content = execute(kc=kc, code="import sys; print(repr(sys.path))")
59-
stdout, stderr = assemble_output(kc.iopub_channel)
59+
stdout, stderr = assemble_output(kc.get_iopub_msg)
6060
# for error-output on failure
6161
sys.stderr.write(stderr)
6262

@@ -69,7 +69,7 @@ def test_sys_path_profile_dir():
6969

7070
with new_kernel(['--profile-dir', locate_profile('default')]) as kc:
7171
msg_id, content = execute(kc=kc, code="import sys; print(repr(sys.path))")
72-
stdout, stderr = assemble_output(kc.iopub_channel)
72+
stdout, stderr = assemble_output(kc.get_iopub_msg)
7373
# for error-output on failure
7474
sys.stderr.write(stderr)
7575

@@ -100,7 +100,7 @@ def test_subprocess_print():
100100
])
101101

102102
msg_id, content = execute(kc=kc, code=code)
103-
stdout, stderr = assemble_output(iopub)
103+
stdout, stderr = assemble_output(kc.get_iopub_msg)
104104
nt.assert_equal(stdout.count("hello"), np, stdout)
105105
for n in range(np):
106106
nt.assert_equal(stdout.count(str(n)), 1, stdout)
@@ -124,7 +124,7 @@ def test_subprocess_noprint():
124124
])
125125

126126
msg_id, content = execute(kc=kc, code=code)
127-
stdout, stderr = assemble_output(iopub)
127+
stdout, stderr = assemble_output(kc.get_iopub_msg)
128128
assert stdout == ''
129129
assert stderr == ''
130130

@@ -150,7 +150,7 @@ def test_subprocess_error():
150150
])
151151

152152
msg_id, content = execute(kc=kc, code=code)
153-
stdout, stderr = assemble_output(iopub)
153+
stdout, stderr = assemble_output(kc.get_iopub_msg)
154154
assert stdout == ''
155155
assert "ValueError" in stderr
156156

@@ -176,7 +176,7 @@ def test_raw_input():
176176
kc.input(text)
177177
reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
178178
assert reply['content']['status'] == 'ok'
179-
stdout, stderr = assemble_output(iopub)
179+
stdout, stderr = assemble_output(kc.get_iopub_msg)
180180
assert stdout == text + "\n"
181181

182182

@@ -318,14 +318,14 @@ def test_unc_paths():
318318
kc.execute("cd {0:s}".format(unc_file_path))
319319
reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
320320
assert reply['content']['status'] == 'ok'
321-
out, err = assemble_output(iopub)
321+
out, err = assemble_output(kc.get_iopub_msg)
322322
assert unc_file_path in out
323323

324324
flush_channels(kc)
325325
kc.execute(code="ls")
326326
reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
327327
assert reply['content']['status'] == 'ok'
328-
out, err = assemble_output(iopub)
328+
out, err = assemble_output(kc.get_iopub_msg)
329329
assert 'unc.txt' in out
330330

331331
kc.execute(code="cd")

ipykernel/tests/test_message_spec.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,21 +288,21 @@ def test_execute_silent():
288288
msg_id, reply = execute(code='x=1', silent=True)
289289

290290
# flush status=idle
291-
status = KC.iopub_channel.get_msg(timeout=TIMEOUT)
291+
status = KC.get_iopub_msg(timeout=TIMEOUT)
292292
validate_message(status, 'status', msg_id)
293293
assert status['content']['execution_state'] == 'idle'
294294

295-
nt.assert_raises(Empty, KC.iopub_channel.get_msg, timeout=0.1)
295+
nt.assert_raises(Empty, KC.get_iopub_msg, timeout=0.1)
296296
count = reply['execution_count']
297297

298298
msg_id, reply = execute(code='x=2', silent=True)
299299

300300
# flush status=idle
301-
status = KC.iopub_channel.get_msg(timeout=TIMEOUT)
301+
status = KC.get_iopub_msg(timeout=TIMEOUT)
302302
validate_message(status, 'status', msg_id)
303303
assert status['content']['execution_state'] == 'idle'
304304

305-
nt.assert_raises(Empty, KC.iopub_channel.get_msg, timeout=0.1)
305+
nt.assert_raises(Empty, KC.get_iopub_msg, timeout=0.1)
306306
count_2 = reply['execution_count']
307307
assert count_2 == count
308308

@@ -314,7 +314,7 @@ def test_execute_error():
314314
assert reply['status'] == 'error'
315315
assert reply['ename'] == 'ZeroDivisionError'
316316

317-
error = KC.iopub_channel.get_msg(timeout=TIMEOUT)
317+
error = KC.get_iopub_msg(timeout=TIMEOUT)
318318
validate_message(error, 'error', msg_id)
319319

320320

@@ -560,7 +560,7 @@ def test_stream():
560560

561561
msg_id, reply = execute("print('hi')")
562562

563-
stdout = KC.iopub_channel.get_msg(timeout=TIMEOUT)
563+
stdout = KC.get_iopub_msg(timeout=TIMEOUT)
564564
validate_message(stdout, 'stream', msg_id)
565565
content = stdout['content']
566566
assert content['text'] == 'hi\n'
@@ -571,7 +571,7 @@ def test_display_data():
571571

572572
msg_id, reply = execute("from IPython.display import display; display(1)")
573573

574-
display = KC.iopub_channel.get_msg(timeout=TIMEOUT)
574+
display = KC.get_iopub_msg(timeout=TIMEOUT)
575575
validate_message(display, 'display_data', parent=msg_id)
576576
data = display['content']['data']
577577
assert data['text/plain'] == '1'

ipykernel/tests/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ def flush_channels(kc=None):
4343

4444
if kc is None:
4545
kc = KC
46-
for channel in (kc.shell_channel, kc.iopub_channel):
46+
for get_msg in (kc.get_shell_msg, kc.get_iopub_msg):
4747
while True:
4848
try:
49-
msg = channel.get_msg(block=True, timeout=0.1)
49+
msg = get_msg(block=True, timeout=0.1)
5050
except Empty:
5151
break
5252
else:
@@ -149,12 +149,12 @@ def new_kernel(argv=None):
149149
kwargs['extra_arguments'] = argv
150150
return manager.run_kernel(**kwargs)
151151

152-
def assemble_output(iopub):
152+
def assemble_output(get_msg):
153153
"""assemble stdout/err from an execution"""
154154
stdout = ''
155155
stderr = ''
156156
while True:
157-
msg = iopub.get_msg(block=True, timeout=1)
157+
msg = get_msg(block=True, timeout=1)
158158
msg_type = msg['msg_type']
159159
content = msg['content']
160160
if msg_type == 'status' and content['execution_state'] == 'idle':
@@ -174,7 +174,7 @@ def assemble_output(iopub):
174174

175175
def wait_for_idle(kc):
176176
while True:
177-
msg = kc.iopub_channel.get_msg(block=True, timeout=1)
177+
msg = kc.get_iopub_msg(block=True, timeout=1)
178178
msg_type = msg['msg_type']
179179
content = msg['content']
180180
if msg_type == 'status' and content['execution_state'] == 'idle':

0 commit comments

Comments
 (0)