def create_and_destroy_kernel(self): """ Set up a new kernel with two associated consoles, then shut everything down. """ kernel = InternalIPKernel() kernel.init_ipkernel(gui_backend=None) kernel.new_qt_console() kernel.new_qt_console() kernel.shutdown()
def test_shutdown_closes_console_pipes(self): kernel = InternalIPKernel() kernel.init_ipkernel(gui_backend=None) console = kernel.new_qt_console() self.assertFalse(console.stdout.closed) self.assertFalse(console.stderr.closed) kernel.shutdown() self.assertTrue(console.stdout.closed) self.assertTrue(console.stderr.closed)
def test_lifecycle(self): kernel = InternalIPKernel() self.assertIsNone(kernel.ipkernel) kernel.init_ipkernel(gui_backend=None) self.assertIsNotNone(kernel.ipkernel) self.assertIsInstance(kernel.ipkernel, ipykernel.kernelapp.IPKernelApp) kernel.new_qt_console() kernel.new_qt_console() self.assertEqual(len(kernel.consoles), 2) kernel.shutdown() self.assertIsNone(kernel.ipkernel) self.assertEqual(len(kernel.consoles), 0)
def test_initialize_twice(self): # Trying to re-initialize an already initialized IPKernelApp can # happen right now as a result of refactoring, but eventually # it should be an error. For now, it's a warning. kernel = InternalIPKernel() self.assertIsNone(kernel.ipkernel) kernel.init_ipkernel(gui_backend=None) try: self.assertIsNotNone(kernel.ipkernel) ipkernel = kernel.ipkernel with warnings.catch_warnings(record=True) as warn_msgs: warnings.simplefilter("always", category=DeprecationWarning) kernel.init_ipkernel(gui_backend=None) # Check that the existing kernel has not been replaced. self.assertIs(ipkernel, kernel.ipkernel) finally: kernel.shutdown() # Check that we got the expected warning message. self.assertEqual(len(warn_msgs), 1) message = str(warn_msgs[0].message) self.assertIn("already been initialized", message)
def test_init_ipkernel_with_explicit_gui_backend(self): loop = tornado.ioloop.IOLoop.current() # Kernel initialization adds an "enter_eventloop" call to the # ioloop event loop queue. Mock to avoid modifying the actual event # loop. with mock.patch.object(loop, "add_callback") as mock_add_callback: with warnings.catch_warnings(record=True) as warn_msgs: warnings.simplefilter("always", category=DeprecationWarning) # Use of gui_backend is deprecated. kernel = InternalIPKernel() kernel.init_ipkernel(gui_backend="qt4") kernel.shutdown() mock_add_callback.reset_mock() # Check that we got the expected warning message. matching_messages = [ msg for msg in warn_msgs if "gui_backend argument is deprecated" in str(msg.message) ] self.assertEqual(len(matching_messages), 1)
def test_initial_namespace(self): kernel = InternalIPKernel(initial_namespace=[("x", 42.1)]) kernel.init_ipkernel(gui_backend=None) self.assertIn("x", kernel.namespace) self.assertEqual(kernel.namespace["x"], 42.1) kernel.shutdown()