##// END OF EJS Templates
Merge pull request #5464 from takluyver/i5460...
Merge pull request #5464 from takluyver/i5460 Fix starting notebook server with file/directory at command line.

File last commit:

r11483:48701c86
r16042:91db3b0a merge
Show More
ipkernel.py
182 lines | 6.7 KiB | text/x-python | PythonLexer
MinRK
move IPython.inprocess to IPython.kernel.inprocess
r9375 """An in-process kernel"""
epatters
Refactor kernel managers in preparation for the EmbeddedKernel.
r8408
#-----------------------------------------------------------------------------
# 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
#-----------------------------------------------------------------------------
epatters
Implement EmbeddedKernel.
r8411 # Standard library imports
from contextlib import contextmanager
import logging
import sys
MinRK
move IPython.inprocess to IPython.kernel.inprocess
r9375 # Local imports
epatters
BUG: GUI integration broken in the in-process kernel.
r8474 from IPython.core.interactiveshell import InteractiveShellABC
epatters
Implement EmbeddedKernel.
r8411 from IPython.utils.jsonutil import json_clean
epatters
BUG: GUI integration broken in the in-process kernel.
r8474 from IPython.utils.traitlets import Any, Enum, Instance, List, Type
MinRK
mv IPython.zmq to IPython.kernel.zmq
r9372 from IPython.kernel.zmq.ipkernel import Kernel
from IPython.kernel.zmq.zmqshell import ZMQInteractiveShell
epatters
Refactor kernel managers in preparation for the EmbeddedKernel.
r8408
MinRK
move IPython.inprocess to IPython.kernel.inprocess
r9375 from .socket import DummySocket
epatters
Refactor kernel managers in preparation for the EmbeddedKernel.
r8408 #-----------------------------------------------------------------------------
# Main kernel class
#-----------------------------------------------------------------------------
epatters
REFACTOR: Terminology change: 'embedded' -> 'in-process'.
r8471 class InProcessKernel(Kernel):
epatters
Implement EmbeddedKernel.
r8411
#-------------------------------------------------------------------------
epatters
REFACTOR: Terminology change: 'embedded' -> 'in-process'.
r8471 # InProcessKernel interface
epatters
Implement EmbeddedKernel.
r8411 #-------------------------------------------------------------------------
epatters
BUG: GUI integration broken in the in-process kernel.
r8474 # The frontends connected to this kernel.
epatters
Implement EmbeddedKernel.
r8411 frontends = List(
MinRK
update inprocess kernel to new layout...
r10298 Instance('IPython.kernel.inprocess.client.InProcessKernelClient')
)
epatters
Implement EmbeddedKernel.
r8411
epatters
BUG: GUI integration broken in the in-process kernel.
r8474 # The GUI environment that the kernel is running under. This need not be
# specified for the normal operation for the kernel, but is required for
# IPython's GUI support (including pylab). The default is 'inline' because
# it is safe under all GUI toolkits.
gui = Enum(('tk', 'gtk', 'wx', 'qt', 'qt4', 'inline'),
default_value='inline')
epatters
Implement EmbeddedKernel.
r8411 raw_input_str = Any()
stdout = Any()
stderr = Any()
#-------------------------------------------------------------------------
# Kernel interface
#-------------------------------------------------------------------------
epatters
BUG: GUI integration broken in the in-process kernel.
r8474 shell_class = Type()
epatters
Implement EmbeddedKernel.
r8411 shell_streams = List()
control_stream = Any()
iopub_socket = Instance(DummySocket, ())
stdin_socket = Instance(DummySocket, ())
def __init__(self, **traits):
# When an InteractiveShell is instantiated by our base class, it binds
# the current values of sys.stdout and sys.stderr.
with self._redirected_io():
epatters
REFACTOR: Terminology change: 'embedded' -> 'in-process'.
r8471 super(InProcessKernel, self).__init__(**traits)
epatters
Implement EmbeddedKernel.
r8411
self.iopub_socket.on_trait_change(self._io_dispatch, 'message_sent')
epatters
BUG: GUI integration broken in the in-process kernel.
r8474 self.shell.kernel = self
epatters
Implement EmbeddedKernel.
r8411
def execute_request(self, stream, ident, parent):
""" Override for temporary IO redirection. """
with self._redirected_io():
epatters
REFACTOR: Terminology change: 'embedded' -> 'in-process'.
r8471 super(InProcessKernel, self).execute_request(stream, ident, parent)
epatters
Implement EmbeddedKernel.
r8411
def start(self):
""" Override registration of dispatchers for streams. """
self.shell.exit_now = False
def _abort_queue(self, stream):
epatters
REFACTOR: Terminology change: 'embedded' -> 'in-process'.
r8471 """ The in-process kernel doesn't abort requests. """
epatters
Implement EmbeddedKernel.
r8411 pass
def _raw_input(self, prompt, ident, parent):
# Flush output before making the request.
self.raw_input_str = None
sys.stderr.flush()
sys.stdout.flush()
# Send the input request.
content = json_clean(dict(prompt=prompt))
msg = self.session.msg(u'input_request', content, parent)
for frontend in self.frontends:
if frontend.session.session == parent['header']['session']:
frontend.stdin_channel.call_handlers(msg)
break
else:
Pietro Berkes
BUG: Logging a rare error condition would have failed.
r8939 logging.error('No frontend found for raw_input request')
epatters
Implement EmbeddedKernel.
r8411 return str()
# Await a response.
while self.raw_input_str is None:
frontend.stdin_channel.process_events()
return self.raw_input_str
#-------------------------------------------------------------------------
# Protected interface
#-------------------------------------------------------------------------
@contextmanager
def _redirected_io(self):
""" Temporarily redirect IO to the kernel.
"""
sys_stdout, sys_stderr = sys.stdout, sys.stderr
sys.stdout, sys.stderr = self.stdout, self.stderr
yield
sys.stdout, sys.stderr = sys_stdout, sys_stderr
#------ Trait change handlers --------------------------------------------
def _io_dispatch(self):
""" Called when a message is sent to the IO socket.
"""
ident, msg = self.session.recv(self.iopub_socket, copy=False)
for frontend in self.frontends:
Brian Granger
Cleanup naming and organization of channels....
r9120 frontend.iopub_channel.call_handlers(msg)
epatters
Implement EmbeddedKernel.
r8411
#------ Trait initializers -----------------------------------------------
def _log_default(self):
return logging.getLogger(__name__)
def _session_default(self):
MinRK
mv IPython.zmq to IPython.kernel.zmq
r9372 from IPython.kernel.zmq.session import Session
MinRK
use `parent=self` throughout IPython...
r11064 return Session(parent=self)
epatters
Implement EmbeddedKernel.
r8411
epatters
BUG: GUI integration broken in the in-process kernel.
r8474 def _shell_class_default(self):
return InProcessInteractiveShell
epatters
Implement EmbeddedKernel.
r8411 def _stdout_default(self):
MinRK
mv IPython.zmq to IPython.kernel.zmq
r9372 from IPython.kernel.zmq.iostream import OutStream
MinRK
disable subprocess pipe in inprocess kernel...
r9448 return OutStream(self.session, self.iopub_socket, u'stdout', pipe=False)
epatters
Implement EmbeddedKernel.
r8411
def _stderr_default(self):
MinRK
mv IPython.zmq to IPython.kernel.zmq
r9372 from IPython.kernel.zmq.iostream import OutStream
MinRK
disable subprocess pipe in inprocess kernel...
r9448 return OutStream(self.session, self.iopub_socket, u'stderr', pipe=False)
epatters
BUG: GUI integration broken in the in-process kernel.
r8474
#-----------------------------------------------------------------------------
# Interactive shell subclass
#-----------------------------------------------------------------------------
class InProcessInteractiveShell(ZMQInteractiveShell):
MinRK
move IPython.inprocess to IPython.kernel.inprocess
r9375 kernel = Instance('IPython.kernel.inprocess.ipkernel.InProcessKernel')
epatters
BUG: GUI integration broken in the in-process kernel.
r8474
#-------------------------------------------------------------------------
# InteractiveShell interface
#-------------------------------------------------------------------------
def enable_gui(self, gui=None):
MinRK
update enable_pylab, etc. for inprocess kernel
r11455 """Enable GUI integration for the kernel."""
MinRK
mv IPython.zmq to IPython.kernel.zmq
r9372 from IPython.kernel.zmq.eventloops import enable_gui
epatters
BUG: GUI integration broken in the in-process kernel.
r8474 if not gui:
gui = self.kernel.gui
MinRK
update enable_pylab, etc. for inprocess kernel
r11455 return enable_gui(gui, kernel=self.kernel)
def enable_matplotlib(self, gui=None):
"""Enable matplotlib integration for the kernel."""
if not gui:
gui = self.kernel.gui
MinRK
remove extra self in InProcess.enable_matplotlib
r11483 return super(InProcessInteractiveShell, self).enable_matplotlib(gui)
epatters
BUG: GUI integration broken in the in-process kernel.
r8474
def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
MinRK
update enable_pylab, etc. for inprocess kernel
r11455 """Activate pylab support at runtime."""
epatters
BUG: GUI integration broken in the in-process kernel.
r8474 if not gui:
gui = self.kernel.gui
MinRK
update enable_pylab, etc. for inprocess kernel
r11455 return super(InProcessInteractiveShell, self).enable_pylab(gui, import_all,
epatters
BUG: GUI integration broken in the in-process kernel.
r8474 welcome_message)
InteractiveShellABC.register(InProcessInteractiveShell)