test_kernel.py
69 lines
| 2.1 KiB
| text/x-python
|
PythonLexer
MinRK
|
r18104 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
epatters
|
r8475 | |||
from __future__ import print_function | ||||
epatters
|
r8482 | import sys | ||
epatters
|
r8475 | import unittest | ||
MinRK
|
r10298 | from IPython.kernel.inprocess.blocking import BlockingInProcessKernelClient | ||
from IPython.kernel.inprocess.manager import InProcessKernelManager | ||||
MinRK
|
r9375 | from IPython.kernel.inprocess.ipkernel import InProcessKernel | ||
Min RK
|
r19881 | from IPython.kernel.tests.utils import assemble_output | ||
epatters
|
r8493 | from IPython.testing.decorators import skipif_not_matplotlib | ||
epatters
|
r8475 | from IPython.utils.io import capture_output | ||
Pietro Berkes
|
r8941 | from IPython.utils import py3compat | ||
epatters
|
r8475 | |||
Thomas Kluyver
|
r13366 | if py3compat.PY3: | ||
from io import StringIO | ||||
else: | ||||
from StringIO import StringIO | ||||
epatters
|
r8475 | |||
class InProcessKernelTestCase(unittest.TestCase): | ||||
MinRK
|
r10298 | def setUp(self): | ||
self.km = InProcessKernelManager() | ||||
self.km.start_kernel() | ||||
Min RK
|
r20572 | self.kc = self.km.client() | ||
MinRK
|
r10298 | self.kc.start_channels() | ||
Thomas Kluyver
|
r19223 | self.kc.wait_for_ready() | ||
MinRK
|
r10298 | |||
epatters
|
r8493 | @skipif_not_matplotlib | ||
epatters
|
r8475 | def test_pylab(self): | ||
MinRK
|
r18104 | """Does %pylab work in the in-process kernel?""" | ||
MinRK
|
r10298 | kc = self.kc | ||
kc.execute('%pylab') | ||||
Min RK
|
r19881 | out, err = assemble_output(kc.iopub_channel) | ||
self.assertIn('matplotlib', out) | ||||
epatters
|
r8475 | |||
epatters
|
r8482 | def test_raw_input(self): | ||
""" Does the in-process kernel handle raw_input correctly? | ||||
""" | ||||
io = StringIO('foobar\n') | ||||
sys_stdin = sys.stdin | ||||
sys.stdin = io | ||||
try: | ||||
Pietro Berkes
|
r8941 | if py3compat.PY3: | ||
MinRK
|
r10298 | self.kc.execute('x = input()') | ||
Pietro Berkes
|
r8941 | else: | ||
MinRK
|
r10298 | self.kc.execute('x = raw_input()') | ||
epatters
|
r8482 | finally: | ||
sys.stdin = sys_stdin | ||||
MinRK
|
r10298 | self.assertEqual(self.km.kernel.shell.user_ns.get('x'), 'foobar') | ||
epatters
|
r8482 | |||
epatters
|
r8475 | 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') | ||||
Min RK
|
r20572 | kc = BlockingInProcessKernelClient(kernel=kernel, session=kernel.session) | ||
MinRK
|
r10298 | kernel.frontends.append(kc) | ||
Thomas Kluyver
|
r19213 | kc.execute('print("bar")') | ||
Min RK
|
r19881 | out, err = assemble_output(kc.iopub_channel) | ||
self.assertEqual(out, 'bar\n') | ||||
epatters
|
r8475 | |||