def setUp(self):
     _init_asyncio_patch()
     self.km = InProcessKernelManager()
     self.km.start_kernel()
     self.kc = self.km.client()
     self.kc.start_channels()
     self.kc.wait_for_ready()
class InProcessKernelTestCase(unittest.TestCase):

    def setUp(self):
        _init_asyncio_patch()
        self.km = InProcessKernelManager()
        self.km.start_kernel()
        self.kc = self.km.client()
        self.kc.start_channels()
        self.kc.wait_for_ready()

    @skipif_not_matplotlib
    def test_pylab(self):
        """Does %pylab work in the in-process kernel?"""
        kc = self.kc
        kc.execute('%pylab')
        out, err = assemble_output(kc.iopub_channel)
        self.assertIn('matplotlib', out)

    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:
            self.kc.execute('x = input()')
        finally:
            sys.stdin = sys_stdin
        assert self.km.kernel.shell.user_ns.get('x') == 'foobar'

    @pytest.mark.skipif(
        '__pypy__' in sys.builtin_module_names,
        reason="fails on pypy"
    )
    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")')
        assert io.stdout == 'foo\n'

        kc = BlockingInProcessKernelClient(kernel=kernel, session=kernel.session)
        kernel.frontends.append(kc)
        kc.execute('print("bar")')
        out, err = assemble_output(kc.iopub_channel)
        assert out == 'bar\n'

    def test_getpass_stream(self):
        "Tests that kernel getpass accept the stream parameter"
        kernel = InProcessKernel()
        kernel._allow_stdin = True
        kernel._input_request = lambda *args, **kwargs : None

        kernel.getpass(stream='non empty')
 def test_execute(self):
     """ Does executing code in an in-process kernel work?
     """
     km = InProcessKernelManager()
     km.start_kernel()
     kc = km.client()
     kc.start_channels()
     kc.wait_for_ready()
     kc.execute('foo = 1')
     self.assertEquals(km.kernel.shell.user_ns['foo'], 1)
    def test_interface(self):
        """ Does the in-process kernel manager implement the basic KM interface?
        """
        km = InProcessKernelManager()
        self.assert_(not km.has_kernel)

        km.start_kernel()
        self.assert_(km.has_kernel)
        self.assert_(km.kernel is not None)

        kc = km.client()
        self.assert_(not kc.channels_running)

        kc.start_channels()
        self.assert_(kc.channels_running)

        old_kernel = km.kernel
        km.restart_kernel()
        self.assertIsNotNone(km.kernel)
        self.assertNotEquals(km.kernel, old_kernel)

        km.shutdown_kernel()
        self.assert_(not km.has_kernel)

        self.assertRaises(NotImplementedError, km.interrupt_kernel)
        self.assertRaises(NotImplementedError, km.signal_kernel, 9)

        kc.stop_channels()
        self.assert_(not kc.channels_running)
Beispiel #5
0
class InProcessKernelTestCase(unittest.TestCase):

    def setUp(self):
        self.km = InProcessKernelManager()
        self.km.start_kernel()
        self.kc = self.km.client()
        self.kc.start_channels()
        self.kc.wait_for_ready()

    @skipif_not_matplotlib
    def test_pylab(self):
        """Does %pylab work in the in-process kernel?"""
        kc = self.kc
        kc.execute('%pylab')
        out, err = assemble_output(kc.iopub_channel)
        self.assertIn('matplotlib', out)

    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:
            if py3compat.PY3:
                self.kc.execute('x = input()')
            else:
                self.kc.execute('x = raw_input()')
        finally:
            sys.stdin = sys_stdin
        assert self.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")')
        assert io.stdout == 'foo\n'

        kc = BlockingInProcessKernelClient(kernel=kernel, session=kernel.session)
        kernel.frontends.append(kc)
        kc.execute('print("bar")')
        out, err = assemble_output(kc.iopub_channel)
        assert out == 'bar\n'

    def test_getpass_stream(self):
        "Tests that kernel getpass accept the stream parameter"
        kernel = InProcessKernel()
        kernel._allow_stdin = True
        kernel._input_request = lambda *args, **kwargs : None

        kernel.getpass(stream='non empty')
 def test_complete(self):
     """ Does requesting completion from an in-process kernel work?
     """
     km = InProcessKernelManager()
     km.start_kernel()
     kc = km.client()
     kc.start_channels()
     kc.wait_for_ready()
     km.kernel.shell.push({'my_bar': 0, 'my_baz': 1})
     kc.complete('my_ba', 5)
     msg = kc.get_shell_msg()
     self.assertEqual(msg['header']['msg_type'], 'complete_reply')
     self.assertEqual(sorted(msg['content']['matches']),
                       ['my_bar', 'my_baz'])
 def test_history(self):
     """ Does requesting history from an in-process kernel work?
     """
     km = InProcessKernelManager()
     km.start_kernel()
     kc = km.client()
     kc.start_channels()
     kc.wait_for_ready()
     kc.execute('%who')
     kc.history(hist_access_type='tail', n=1)
     msg = kc.shell_channel.get_msgs()[-1]
     self.assertEquals(msg['header']['msg_type'], 'history_reply')
     history = msg['content']['history']
     self.assertEquals(len(history), 1)
     self.assertEquals(history[0][2], '%who')
 def test_inspect(self):
     """ Does requesting object information from an in-process kernel work?
     """
     km = InProcessKernelManager()
     km.start_kernel()
     kc = km.client()
     kc.start_channels()
     kc.wait_for_ready()
     km.kernel.shell.user_ns['foo'] = 1
     kc.inspect('foo')
     msg = kc.get_shell_msg()
     self.assertEqual(msg['header']['msg_type'], 'inspect_reply')
     content = msg['content']
     assert content['found']
     text = content['data']['text/plain']
     self.assertIn('int', text)
Beispiel #9
0
def main():
    print_process_id()

    # Create an in-process kernel
    # >>> print_process_id()
    # will print the same process ID as the main process
    init_asyncio_patch()
    kernel_manager = InProcessKernelManager()
    kernel_manager.start_kernel()
    kernel = kernel_manager.kernel
    kernel.gui = "qt4"
    kernel.shell.push({"foo": 43, "print_process_id": print_process_id})
    client = kernel_manager.client()
    client.start_channels()

    shell = ZMQTerminalInteractiveShell(manager=kernel_manager, client=client)
    shell.mainloop()
Beispiel #10
0
class InProcessKernelTestCase(unittest.TestCase):

    def setUp(self):
        self.km = InProcessKernelManager()
        self.km.start_kernel()
        self.kc = self.km.client()
        self.kc.start_channels()
        self.kc.wait_for_ready()

    @skipif_not_matplotlib
    def test_pylab(self):
        """Does %pylab work in the in-process kernel?"""
        kc = self.kc
        kc.execute('%pylab')
        out, err = assemble_output(kc.iopub_channel)
        self.assertIn('matplotlib', out)

    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:
            if py3compat.PY3:
                self.kc.execute('x = input()')
            else:
                self.kc.execute('x = raw_input()')
        finally:
            sys.stdin = sys_stdin
        self.assertEqual(self.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')

        kc = BlockingInProcessKernelClient(kernel=kernel, session=kernel.session)
        kernel.frontends.append(kc)
        kc.execute('print("bar")')
        out, err = assemble_output(kc.iopub_channel)
        self.assertEqual(out, 'bar\n')
Beispiel #11
0
class InProcessKernelTestCase(unittest.TestCase):
    def setUp(self):
        self.km = InProcessKernelManager()
        self.km.start_kernel()
        self.kc = self.km.client()
        self.kc.start_channels()
        self.kc.wait_for_ready()

    @skipif_not_matplotlib
    def test_pylab(self):
        """Does %pylab work in the in-process kernel?"""
        kc = self.kc
        kc.execute('%pylab')
        out, err = assemble_output(kc.iopub_channel)
        self.assertIn('matplotlib', out)

    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:
            if py3compat.PY3:
                self.kc.execute('x = input()')
            else:
                self.kc.execute('x = raw_input()')
        finally:
            sys.stdin = sys_stdin
        self.assertEqual(self.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')

        kc = BlockingInProcessKernelClient(kernel=kernel,
                                           session=kernel.session)
        kernel.frontends.append(kc)
        kc.execute('print("bar")')
        out, err = assemble_output(kc.iopub_channel)
        self.assertEqual(out, 'bar\n')
Beispiel #12
0
def create_kernel():
    kernel_manager = InProcessKernelManager()
    kernel_manager.start_kernel()
    kernel = kernel_manager.kernel
    kernel.gui = 'qt4'
    return kernel
 def setUp(self):
     self.km = InProcessKernelManager()
class InProcessKernelManagerTestCase(unittest.TestCase):

    def setUp(self):
        self.km = InProcessKernelManager()

    def tearDown(self):
        if self.km.has_kernel:
            self.km.shutdown_kernel()

    def test_interface(self):
        """ Does the in-process kernel manager implement the basic KM interface?
        """
        km = self.km
        self.assert_(not km.has_kernel)

        km.start_kernel()
        self.assert_(km.has_kernel)
        self.assert_(km.kernel is not None)

        kc = km.client()
        self.assert_(not kc.channels_running)

        kc.start_channels()
        self.assert_(kc.channels_running)

        old_kernel = km.kernel
        km.restart_kernel()
        self.assertIsNotNone(km.kernel)
        self.assertNotEquals(km.kernel, old_kernel)

        km.shutdown_kernel()
        self.assert_(not km.has_kernel)

        self.assertRaises(NotImplementedError, km.interrupt_kernel)
        self.assertRaises(NotImplementedError, km.signal_kernel, 9)

        kc.stop_channels()
        self.assert_(not kc.channels_running)

    def test_execute(self):
        """ Does executing code in an in-process kernel work?
        """
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        kc.execute('foo = 1')
        self.assertEquals(km.kernel.shell.user_ns['foo'], 1)

    def test_complete(self):
        """ Does requesting completion from an in-process kernel work?
        """
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        km.kernel.shell.push({'my_bar': 0, 'my_baz': 1})
        kc.complete('my_ba', 5)
        msg = kc.get_shell_msg()
        self.assertEqual(msg['header']['msg_type'], 'complete_reply')
        self.assertEqual(sorted(msg['content']['matches']),
                          ['my_bar', 'my_baz'])

    def test_inspect(self):
        """ Does requesting object information from an in-process kernel work?
        """
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        km.kernel.shell.user_ns['foo'] = 1
        kc.inspect('foo')
        msg = kc.get_shell_msg()
        self.assertEqual(msg['header']['msg_type'], 'inspect_reply')
        content = msg['content']
        assert content['found']
        text = content['data']['text/plain']
        self.assertIn('int', text)

    def test_history(self):
        """ Does requesting history from an in-process kernel work?
        """
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        kc.execute('1')
        kc.history(hist_access_type='tail', n=1)
        msg = kc.shell_channel.get_msgs()[-1]
        self.assertEquals(msg['header']['msg_type'], 'history_reply')
        history = msg['content']['history']
        self.assertEquals(len(history), 1)
        self.assertEquals(history[0][2], '1')
class InProcessKernelManagerTestCase(unittest.TestCase):
    def setUp(self):
        self.km = InProcessKernelManager()

    def tearDown(self):
        if self.km.has_kernel:
            self.km.shutdown_kernel()

    def test_interface(self):
        """Does the in-process kernel manager implement the basic KM interface?"""
        km = self.km
        assert not km.has_kernel

        km.start_kernel()
        assert km.has_kernel
        assert km.kernel is not None

        kc = km.client()
        assert not kc.channels_running

        kc.start_channels()
        assert kc.channels_running

        old_kernel = km.kernel
        km.restart_kernel()
        self.assertIsNotNone(km.kernel)
        assert km.kernel != old_kernel

        km.shutdown_kernel()
        assert not km.has_kernel

        self.assertRaises(NotImplementedError, km.interrupt_kernel)
        self.assertRaises(NotImplementedError, km.signal_kernel, 9)

        kc.stop_channels()
        assert not kc.channels_running

    def test_execute(self):
        """Does executing code in an in-process kernel work?"""
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        kc.execute("foo = 1")
        assert km.kernel.shell.user_ns["foo"] == 1

    def test_complete(self):
        """Does requesting completion from an in-process kernel work?"""
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        km.kernel.shell.push({"my_bar": 0, "my_baz": 1})
        kc.complete("my_ba", 5)
        msg = kc.get_shell_msg()
        assert msg["header"]["msg_type"] == "complete_reply"
        self.assertEqual(sorted(msg["content"]["matches"]),
                         ["my_bar", "my_baz"])

    def test_inspect(self):
        """Does requesting object information from an in-process kernel work?"""
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        km.kernel.shell.user_ns["foo"] = 1
        kc.inspect("foo")
        msg = kc.get_shell_msg()
        assert msg["header"]["msg_type"] == "inspect_reply"
        content = msg["content"]
        assert content["found"]
        text = content["data"]["text/plain"]
        self.assertIn("int", text)

    def test_history(self):
        """Does requesting history from an in-process kernel work?"""
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        kc.execute("1")
        kc.history(hist_access_type="tail", n=1)
        msg = kc.shell_channel.get_msgs()[-1]
        assert msg["header"]["msg_type"] == "history_reply"
        history = msg["content"]["history"]
        assert len(history) == 1
        assert history[0][2] == "1"
class InProcessKernelManagerTestCase(unittest.TestCase):
    def setUp(self):
        self.km = InProcessKernelManager()

    def tearDown(self):
        if self.km.has_kernel:
            self.km.shutdown_kernel()

    def test_interface(self):
        """ Does the in-process kernel manager implement the basic KM interface?
        """
        km = self.km
        assert not km.has_kernel

        km.start_kernel()
        assert km.has_kernel
        assert km.kernel is not None

        kc = km.client()
        assert not kc.channels_running

        kc.start_channels()
        assert kc.channels_running

        old_kernel = km.kernel
        km.restart_kernel()
        self.assertIsNotNone(km.kernel)
        assert km.kernel != old_kernel

        km.shutdown_kernel()
        assert not km.has_kernel

        self.assertRaises(NotImplementedError, km.interrupt_kernel)
        self.assertRaises(NotImplementedError, km.signal_kernel, 9)

        kc.stop_channels()
        assert not kc.channels_running

    def test_execute(self):
        """ Does executing code in an in-process kernel work?
        """
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        kc.execute('foo = 1')
        assert km.kernel.shell.user_ns['foo'] == 1

    def test_complete(self):
        """ Does requesting completion from an in-process kernel work?
        """
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        km.kernel.shell.push({'my_bar': 0, 'my_baz': 1})
        kc.complete('my_ba', 5)
        msg = kc.get_shell_msg()
        assert msg['header']['msg_type'] == 'complete_reply'
        self.assertEqual(sorted(msg['content']['matches']),
                         ['my_bar', 'my_baz'])

    def test_inspect(self):
        """ Does requesting object information from an in-process kernel work?
        """
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        km.kernel.shell.user_ns['foo'] = 1
        kc.inspect('foo')
        msg = kc.get_shell_msg()
        assert msg['header']['msg_type'] == 'inspect_reply'
        content = msg['content']
        assert content['found']
        text = content['data']['text/plain']
        self.assertIn('int', text)

    def test_history(self):
        """ Does requesting history from an in-process kernel work?
        """
        km = self.km
        km.start_kernel()
        kc = km.client()
        kc.start_channels()
        kc.wait_for_ready()
        kc.execute('1')
        kc.history(hist_access_type='tail', n=1)
        msg = kc.shell_channel.get_msgs()[-1]
        assert msg['header']['msg_type'] == 'history_reply'
        history = msg['content']['history']
        assert len(history) == 1
        assert history[0][2] == '1'
Beispiel #17
0
 def setUp(self):
     self.km = InProcessKernelManager()
     self.km.start_kernel()
     self.kc = self.km.client()
     self.kc.start_channels()
     self.kc.wait_for_ready()
 def setUp(self):
     self.km = InProcessKernelManager()
class InProcessKernelTestCase(unittest.TestCase):
    def setUp(self):
        _init_asyncio_patch()
        self.km = InProcessKernelManager()
        self.km.start_kernel()
        self.kc = self.km.client()
        self.kc.start_channels()
        self.kc.wait_for_ready()

    def test_with_cell_id(self):

        with patch_cell_id():
            self.kc.execute("1+1")

    def test_pylab(self):
        """Does %pylab work in the in-process kernel?"""
        _ = pytest.importorskip("matplotlib",
                                reason="This test requires matplotlib")
        kc = self.kc
        kc.execute("%pylab")
        out, err = assemble_output(kc.get_iopub_msg)
        self.assertIn("matplotlib", out)

    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:
            self.kc.execute("x = input()")
        finally:
            sys.stdin = sys_stdin
        assert self.km.kernel.shell.user_ns.get("x") == "foobar"

    @pytest.mark.skipif("__pypy__" in sys.builtin_module_names,
                        reason="fails on pypy")
    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")')
        assert io.stdout == "foo\n"

        kc = BlockingInProcessKernelClient(kernel=kernel,
                                           session=kernel.session)
        kernel.frontends.append(kc)
        kc.execute('print("bar")')
        out, err = assemble_output(kc.get_iopub_msg)
        assert out == "bar\n"

    @pytest.mark.skip(
        reason=
        "Currently don't capture during test as pytest does its own capturing")
    def test_capfd(self):
        """Does correctly capture fd"""
        kernel = InProcessKernel()

        with capture_output() as io:
            kernel.shell.run_cell('print("foo")')
        assert io.stdout == "foo\n"

        kc = BlockingInProcessKernelClient(kernel=kernel,
                                           session=kernel.session)
        kernel.frontends.append(kc)
        kc.execute("import os")
        kc.execute('os.system("echo capfd")')
        out, err = assemble_output(kc.iopub_channel)
        assert out == "capfd\n"

    def test_getpass_stream(self):
        "Tests that kernel getpass accept the stream parameter"
        kernel = InProcessKernel()
        kernel._allow_stdin = True
        kernel._input_request = lambda *args, **kwargs: None

        kernel.getpass(stream="non empty")

    def test_do_execute(self):
        kernel = InProcessKernel()
        asyncio.run(kernel.do_execute("a=1", True))
        assert kernel.shell.user_ns["a"] == 1