envisage.plugins.ipython_kernel.kernelapp module

This module contains an extended version of the upstream ipykernel IPKernelApp.

The main reason for extending is to support clean shutdown.

class envisage.plugins.ipython_kernel.kernelapp.IPKernelApp(**kwargs)[source]

Bases: ipykernel.kernelapp.IPKernelApp

Patched version of the IPKernelApp, mostly to support clean shutdown.


Clear SingletonConfigurable instances.


Undo the effects of the initialize method:

  • free resources allocated during initialization

  • undo changes to global state


Undo the global state change from init_crash_handler.

Restore the sys.excepthook attribute.


Stop the heartbeat thread, by terminating the corresponding zmq.Context.


Undo the effects of init_io.

Restores sys module attributes altered by init_io.


Close iopub-related resources.


Undo setup from init_kernel.


Undo changes made in init_profile_dir.


Clean up resources allocated by the shell.


Unbind, close and destroy sockets created by init_sockets.


Configure tornado logging.

Adds a NullHandler to the tornado root logger, if there are no handlers already present on that logger. If no tornado handler is present at the time the IO loop is started, tornado will call logging.basicConfig, which isn’t what we want to happen.

Overridden from the base class, which unconditionally adds a new StreamHandler every time.

See also: - https://github.com/tornadoweb/tornado/blob/v6.0.3/tornado/ioloop.py#L427-L445 # noqa: E501 - https://github.com/tornadoweb/tornado/pull/741


Set up a suitable exception hook.

Extended to keep track of the original sys.excepthook value, so that it can be restored later.


start the heart beating

Overridden from the base class in order to swap in our own Heartbeat class in place of the official one. Our Heartbeat class is modified to allow the heartbeat thread to be shut down cleanly.

This override can be removed once we’re on ipkernel 5.x.


Redirect input streams and set a display hook.

Extended to store the original sys attributes so that they can be restored later.


Create the kernel object itself.

Extended to store the original values of IPython.utils.io.stdout and IPython.utils.io.stderr, so that they can be restored later.


Display connection info, and store ports.

Overridden to not write information to __stdout__. We don’t usually want this in applications that embed an IPython kernel (as opposed to the case where IPython effectively is the application).


Patch important libraries that can’t handle sys.stdout forwarding.

Overridden from the base class to do nothing.

The base class method monkeypatches faulthandler so that calling faulthandler.enable() while streams are redirected doesn’t fail.

This override bypasses that patching. Users of the Envisage plugin are advised to enable faulthandler (if desired) as part of application setup, before the plugin is started.

Related: https://github.com/ipython/ipykernel/issues/91