##// END OF EJS Templates
Merge pull request #2775 from ellisonbg/kernelid...
Merge pull request #2775 from ellisonbg/kernelid General cleanup of kernel manager code. This does some general cleanup of MultiKernelManager to better reflect how it is actually being used. Sometimes there is a need to create kernel_id's elsewhere in code. This minor change allows a kernel_id to be created outside of the MultiKernelManager and passed in as a kwarg. An exception is raised if the id is already used. Tasks: - [x] Fix cleanup of ipc files. - [x] Allow kernel_id to be passed to MultiKernelManager.start_kernel. - [x] Add ipc support to MultiKernelManager. - [x] Add more tests for MultiKernelManager. - [x] Rename sub channel to iopub channel everywhere. - [x] Use consistent naming for all channel classes in zmq, inprocess and qt. - [x] Move BlockingChannelMixin to zmq.blockingkernelmanager. - [x] Create ABC for KernelManager. - [x] Make the InProcessKernelManager a Configurable. - [x] Cleanup docstrings in ABCs. - [x] Add tests for KernelManager. - [x] Check over MultiKernelManager. - [x] Make KernelManager,kill_kernel private in ABC and implementations. - [x] Find bug that is causing the kernel manager tests to hang unless the shell channel linger is set to 0. - [x] Decide about critical logging in ipkernel. - [x] Debug lack of stderr redirect in tests.

File last commit:

r9120:beaf07d3
r9155:ef8974d5 merge
Show More
test_kernel.py
89 lines | 3.0 KiB | text/x-python | PythonLexer
#-------------------------------------------------------------------------------
# Copyright (C) 2012 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-------------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import print_function
# Standard library imports
from StringIO import StringIO
import sys
import unittest
# Local imports
from IPython.inprocess.blockingkernelmanager import \
BlockingInProcessKernelManager
from IPython.inprocess.ipkernel import InProcessKernel
from IPython.testing.decorators import skipif_not_matplotlib
from IPython.utils.io import capture_output
from IPython.utils import py3compat
#-----------------------------------------------------------------------------
# Test case
#-----------------------------------------------------------------------------
class InProcessKernelTestCase(unittest.TestCase):
@skipif_not_matplotlib
def test_pylab(self):
""" Does pylab work in the in-process kernel?
"""
km = BlockingInProcessKernelManager()
km.start_kernel()
km.shell_channel.execute('%pylab')
msg = get_stream_message(km)
self.assert_('Welcome to pylab' in msg['content']['data'])
def test_raw_input(self):
""" Does the in-process kernel handle raw_input correctly?
"""
km = BlockingInProcessKernelManager()
km.start_kernel()
io = StringIO('foobar\n')
sys_stdin = sys.stdin
sys.stdin = io
try:
if py3compat.PY3:
km.shell_channel.execute('x = input()')
else:
km.shell_channel.execute('x = raw_input()')
finally:
sys.stdin = sys_stdin
self.assertEqual(km.kernel.shell.user_ns.get('x'), 'foobar')
def test_stdout(self):
""" Does the in-process kernel correctly capture IO?
"""
kernel = InProcessKernel()
with capture_output() as io:
kernel.shell.run_cell('print("foo")')
self.assertEqual(io.stdout, 'foo\n')
km = BlockingInProcessKernelManager(kernel=kernel)
kernel.frontends.append(km)
km.shell_channel.execute('print("bar")')
msg = get_stream_message(km)
self.assertEqual(msg['content']['data'], 'bar\n')
#-----------------------------------------------------------------------------
# Utility functions
#-----------------------------------------------------------------------------
def get_stream_message(kernel_manager, timeout=5):
""" Gets a single stream message synchronously from the sub channel.
"""
while True:
msg = kernel_manager.iopub_channel.get_msg(timeout=timeout)
if msg['header']['msg_type'] == 'stream':
return msg
if __name__ == '__main__':
unittest.main()