Skip to content

Commit 9aaf90a

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

File tree

4 files changed

+24
-35
lines changed

4 files changed

+24
-35
lines changed

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 & 21 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

@@ -44,9 +44,8 @@ def _check_status(content):
4444
def test_simple_print():
4545
"""simple print statement in kernel"""
4646
with kernel() as kc:
47-
iopub = kc.iopub_channel
4847
msg_id, content = execute(kc=kc, code="print ('hi')")
49-
stdout, stderr = assemble_output(iopub)
48+
stdout, stderr = assemble_output(kc.get_iopub_msg)
5049
assert stdout == 'hi\n'
5150
assert stderr == ''
5251
_check_master(kc, expected=True)
@@ -56,7 +55,7 @@ def test_sys_path():
5655
"""test that sys.path doesn't get messed up by default"""
5756
with kernel() as kc:
5857
msg_id, content = execute(kc=kc, code="import sys; print(repr(sys.path))")
59-
stdout, stderr = assemble_output(kc.iopub_channel)
58+
stdout, stderr = assemble_output(kc.get_iopub_msg)
6059
# for error-output on failure
6160
sys.stderr.write(stderr)
6261

@@ -69,7 +68,7 @@ def test_sys_path_profile_dir():
6968

7069
with new_kernel(['--profile-dir', locate_profile('default')]) as kc:
7170
msg_id, content = execute(kc=kc, code="import sys; print(repr(sys.path))")
72-
stdout, stderr = assemble_output(kc.iopub_channel)
71+
stdout, stderr = assemble_output(kc.get_iopub_msg)
7372
# for error-output on failure
7473
sys.stderr.write(stderr)
7574

@@ -85,8 +84,6 @@ def test_sys_path_profile_dir():
8584
def test_subprocess_print():
8685
"""printing from forked mp.Process"""
8786
with new_kernel() as kc:
88-
iopub = kc.iopub_channel
89-
9087
_check_master(kc, expected=True)
9188
flush_channels(kc)
9289
np = 5
@@ -100,7 +97,7 @@ def test_subprocess_print():
10097
])
10198

10299
msg_id, content = execute(kc=kc, code=code)
103-
stdout, stderr = assemble_output(iopub)
100+
stdout, stderr = assemble_output(kc.get_iopub_msg)
104101
nt.assert_equal(stdout.count("hello"), np, stdout)
105102
for n in range(np):
106103
nt.assert_equal(stdout.count(str(n)), 1, stdout)
@@ -113,8 +110,6 @@ def test_subprocess_print():
113110
def test_subprocess_noprint():
114111
"""mp.Process without print doesn't trigger iostream mp_mode"""
115112
with kernel() as kc:
116-
iopub = kc.iopub_channel
117-
118113
np = 5
119114
code = '\n'.join([
120115
"import multiprocessing as mp",
@@ -124,7 +119,7 @@ def test_subprocess_noprint():
124119
])
125120

126121
msg_id, content = execute(kc=kc, code=code)
127-
stdout, stderr = assemble_output(iopub)
122+
stdout, stderr = assemble_output(kc.get_iopub_msg)
128123
assert stdout == ''
129124
assert stderr == ''
130125

@@ -140,8 +135,6 @@ def test_subprocess_noprint():
140135
def test_subprocess_error():
141136
"""error in mp.Process doesn't crash"""
142137
with new_kernel() as kc:
143-
iopub = kc.iopub_channel
144-
145138
code = '\n'.join([
146139
"import multiprocessing as mp",
147140
"p = mp.Process(target=int, args=('hi',))",
@@ -150,7 +143,7 @@ def test_subprocess_error():
150143
])
151144

152145
msg_id, content = execute(kc=kc, code=code)
153-
stdout, stderr = assemble_output(iopub)
146+
stdout, stderr = assemble_output(kc.get_iopub_msg)
154147
assert stdout == ''
155148
assert "ValueError" in stderr
156149

@@ -162,8 +155,6 @@ def test_subprocess_error():
162155
def test_raw_input():
163156
"""test input"""
164157
with kernel() as kc:
165-
iopub = kc.iopub_channel
166-
167158
input_f = "input"
168159
theprompt = "prompt> "
169160
code = 'print({input_f}("{theprompt}"))'.format(**locals())
@@ -176,7 +167,7 @@ def test_raw_input():
176167
kc.input(text)
177168
reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
178169
assert reply['content']['status'] == 'ok'
179-
stdout, stderr = assemble_output(iopub)
170+
stdout, stderr = assemble_output(kc.get_iopub_msg)
180171
assert stdout == text + "\n"
181172

182173

@@ -313,19 +304,17 @@ def test_unc_paths():
313304
file_path = os.path.splitdrive(os.path.dirname(drive_file_path))[1]
314305
unc_file_path = os.path.join(unc_root, file_path[1:])
315306

316-
iopub = kc.iopub_channel
317-
318307
kc.execute("cd {0:s}".format(unc_file_path))
319308
reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
320309
assert reply['content']['status'] == 'ok'
321-
out, err = assemble_output(iopub)
310+
out, err = assemble_output(kc.get_iopub_msg)
322311
assert unc_file_path in out
323312

324313
flush_channels(kc)
325314
kc.execute(code="ls")
326315
reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
327316
assert reply['content']['status'] == 'ok'
328-
out, err = assemble_output(iopub)
317+
out, err = assemble_output(kc.get_iopub_msg)
329318
assert 'unc.txt' in out
330319

331320
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)