/usr/share/pyshared/IPython/frontend/qt/kernelmanager.py is in ipython-qtconsole 0.12.1+dfsg-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | """ Defines a KernelManager that provides signals and slots.
"""
# System library imports.
from IPython.external.qt import QtCore
# IPython imports.
from IPython.utils.traitlets import Type
from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \
ShellSocketChannel, StdInSocketChannel, HBSocketChannel
from util import MetaQObjectHasTraits, SuperQObject
class SocketChannelQObject(SuperQObject):
# Emitted when the channel is started.
started = QtCore.Signal()
# Emitted when the channel is stopped.
stopped = QtCore.Signal()
#---------------------------------------------------------------------------
# 'ZMQSocketChannel' interface
#---------------------------------------------------------------------------
def start(self):
""" Reimplemented to emit signal.
"""
super(SocketChannelQObject, self).start()
self.started.emit()
def stop(self):
""" Reimplemented to emit signal.
"""
super(SocketChannelQObject, self).stop()
self.stopped.emit()
class QtShellSocketChannel(SocketChannelQObject, ShellSocketChannel):
# Emitted when any message is received.
message_received = QtCore.Signal(object)
# Emitted when a reply has been received for the corresponding request
# type.
execute_reply = QtCore.Signal(object)
complete_reply = QtCore.Signal(object)
object_info_reply = QtCore.Signal(object)
history_reply = QtCore.Signal(object)
# Emitted when the first reply comes back.
first_reply = QtCore.Signal()
# Used by the first_reply signal logic to determine if a reply is the
# first.
_handlers_called = False
#---------------------------------------------------------------------------
# 'ShellSocketChannel' interface
#---------------------------------------------------------------------------
def call_handlers(self, msg):
""" Reimplemented to emit signals instead of making callbacks.
"""
# Emit the generic signal.
self.message_received.emit(msg)
# Emit signals for specialized message types.
msg_type = msg['header']['msg_type']
signal = getattr(self, msg_type, None)
if signal:
signal.emit(msg)
if not self._handlers_called:
self.first_reply.emit()
self._handlers_called = True
#---------------------------------------------------------------------------
# 'QtShellSocketChannel' interface
#---------------------------------------------------------------------------
def reset_first_reply(self):
""" Reset the first_reply signal to fire again on the next reply.
"""
self._handlers_called = False
class QtSubSocketChannel(SocketChannelQObject, SubSocketChannel):
# Emitted when any message is received.
message_received = QtCore.Signal(object)
# Emitted when a message of type 'stream' is received.
stream_received = QtCore.Signal(object)
# Emitted when a message of type 'pyin' is received.
pyin_received = QtCore.Signal(object)
# Emitted when a message of type 'pyout' is received.
pyout_received = QtCore.Signal(object)
# Emitted when a message of type 'pyerr' is received.
pyerr_received = QtCore.Signal(object)
# Emitted when a message of type 'display_data' is received
display_data_received = QtCore.Signal(object)
# Emitted when a crash report message is received from the kernel's
# last-resort sys.excepthook.
crash_received = QtCore.Signal(object)
# Emitted when a shutdown is noticed.
shutdown_reply_received = QtCore.Signal(object)
#---------------------------------------------------------------------------
# 'SubSocketChannel' interface
#---------------------------------------------------------------------------
def call_handlers(self, msg):
""" Reimplemented to emit signals instead of making callbacks.
"""
# Emit the generic signal.
self.message_received.emit(msg)
# Emit signals for specialized message types.
msg_type = msg['header']['msg_type']
signal = getattr(self, msg_type + '_received', None)
if signal:
signal.emit(msg)
elif msg_type in ('stdout', 'stderr'):
self.stream_received.emit(msg)
def flush(self):
""" Reimplemented to ensure that signals are dispatched immediately.
"""
super(QtSubSocketChannel, self).flush()
QtCore.QCoreApplication.instance().processEvents()
class QtStdInSocketChannel(SocketChannelQObject, StdInSocketChannel):
# Emitted when any message is received.
message_received = QtCore.Signal(object)
# Emitted when an input request is received.
input_requested = QtCore.Signal(object)
#---------------------------------------------------------------------------
# 'StdInSocketChannel' interface
#---------------------------------------------------------------------------
def call_handlers(self, msg):
""" Reimplemented to emit signals instead of making callbacks.
"""
# Emit the generic signal.
self.message_received.emit(msg)
# Emit signals for specialized message types.
msg_type = msg['header']['msg_type']
if msg_type == 'input_request':
self.input_requested.emit(msg)
class QtHBSocketChannel(SocketChannelQObject, HBSocketChannel):
# Emitted when the kernel has died.
kernel_died = QtCore.Signal(object)
#---------------------------------------------------------------------------
# 'HBSocketChannel' interface
#---------------------------------------------------------------------------
def call_handlers(self, since_last_heartbeat):
""" Reimplemented to emit signals instead of making callbacks.
"""
# Emit the generic signal.
self.kernel_died.emit(since_last_heartbeat)
class QtKernelManager(KernelManager, SuperQObject):
""" A KernelManager that provides signals and slots.
"""
__metaclass__ = MetaQObjectHasTraits
# Emitted when the kernel manager has started listening.
started_channels = QtCore.Signal()
# Emitted when the kernel manager has stopped listening.
stopped_channels = QtCore.Signal()
# Use Qt-specific channel classes that emit signals.
sub_channel_class = Type(QtSubSocketChannel)
shell_channel_class = Type(QtShellSocketChannel)
stdin_channel_class = Type(QtStdInSocketChannel)
hb_channel_class = Type(QtHBSocketChannel)
#---------------------------------------------------------------------------
# 'KernelManager' interface
#---------------------------------------------------------------------------
#------ Kernel process management ------------------------------------------
def start_kernel(self, *args, **kw):
""" Reimplemented for proper heartbeat management.
"""
if self._shell_channel is not None:
self._shell_channel.reset_first_reply()
super(QtKernelManager, self).start_kernel(*args, **kw)
#------ Channel management -------------------------------------------------
def start_channels(self, *args, **kw):
""" Reimplemented to emit signal.
"""
super(QtKernelManager, self).start_channels(*args, **kw)
self.started_channels.emit()
def stop_channels(self):
""" Reimplemented to emit signal.
"""
super(QtKernelManager, self).stop_channels()
self.stopped_channels.emit()
@property
def shell_channel(self):
""" Reimplemented for proper heartbeat management.
"""
if self._shell_channel is None:
self._shell_channel = super(QtKernelManager, self).shell_channel
self._shell_channel.first_reply.connect(self._first_reply)
return self._shell_channel
#---------------------------------------------------------------------------
# Protected interface
#---------------------------------------------------------------------------
def _first_reply(self):
""" Unpauses the heartbeat channel when the first reply is received on
the execute channel. Note that this will *not* start the heartbeat
channel if it is not already running!
"""
if self._hb_channel is not None:
self._hb_channel.unpause()
|