Show More
@@ -1,178 +1,182 b'' | |||
|
1 | 1 | """An in-process kernel""" |
|
2 | 2 | |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (C) 2012 The IPython Development Team |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | 7 | # the file COPYING, distributed as part of this software. |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | # Imports |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | # Standard library imports |
|
15 | 15 | from contextlib import contextmanager |
|
16 | 16 | import logging |
|
17 | 17 | import sys |
|
18 | 18 | |
|
19 | 19 | # Local imports |
|
20 | 20 | from IPython.core.interactiveshell import InteractiveShellABC |
|
21 | 21 | from IPython.utils.jsonutil import json_clean |
|
22 | 22 | from IPython.utils.traitlets import Any, Enum, Instance, List, Type |
|
23 | 23 | from IPython.kernel.zmq.ipkernel import Kernel |
|
24 | 24 | from IPython.kernel.zmq.zmqshell import ZMQInteractiveShell |
|
25 | 25 | |
|
26 | 26 | from .socket import DummySocket |
|
27 | 27 | |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | # Main kernel class |
|
30 | 30 | #----------------------------------------------------------------------------- |
|
31 | 31 | |
|
32 | 32 | class InProcessKernel(Kernel): |
|
33 | 33 | |
|
34 | 34 | #------------------------------------------------------------------------- |
|
35 | 35 | # InProcessKernel interface |
|
36 | 36 | #------------------------------------------------------------------------- |
|
37 | 37 | |
|
38 | 38 | # The frontends connected to this kernel. |
|
39 | 39 | frontends = List( |
|
40 | 40 | Instance('IPython.kernel.inprocess.client.InProcessKernelClient') |
|
41 | 41 | ) |
|
42 | 42 | |
|
43 | 43 | # The GUI environment that the kernel is running under. This need not be |
|
44 | 44 | # specified for the normal operation for the kernel, but is required for |
|
45 | 45 | # IPython's GUI support (including pylab). The default is 'inline' because |
|
46 | 46 | # it is safe under all GUI toolkits. |
|
47 | 47 | gui = Enum(('tk', 'gtk', 'wx', 'qt', 'qt4', 'inline'), |
|
48 | 48 | default_value='inline') |
|
49 | 49 | |
|
50 | 50 | raw_input_str = Any() |
|
51 | 51 | stdout = Any() |
|
52 | 52 | stderr = Any() |
|
53 | 53 | |
|
54 | 54 | #------------------------------------------------------------------------- |
|
55 | 55 | # Kernel interface |
|
56 | 56 | #------------------------------------------------------------------------- |
|
57 | 57 | |
|
58 | 58 | shell_class = Type() |
|
59 | 59 | shell_streams = List() |
|
60 | 60 | control_stream = Any() |
|
61 | 61 | iopub_socket = Instance(DummySocket, ()) |
|
62 | 62 | stdin_socket = Instance(DummySocket, ()) |
|
63 | 63 | |
|
64 | 64 | def __init__(self, **traits): |
|
65 | 65 | # When an InteractiveShell is instantiated by our base class, it binds |
|
66 | 66 | # the current values of sys.stdout and sys.stderr. |
|
67 | 67 | with self._redirected_io(): |
|
68 | 68 | super(InProcessKernel, self).__init__(**traits) |
|
69 | 69 | |
|
70 | 70 | self.iopub_socket.on_trait_change(self._io_dispatch, 'message_sent') |
|
71 | 71 | self.shell.kernel = self |
|
72 | 72 | |
|
73 | 73 | def execute_request(self, stream, ident, parent): |
|
74 | 74 | """ Override for temporary IO redirection. """ |
|
75 | 75 | with self._redirected_io(): |
|
76 | 76 | super(InProcessKernel, self).execute_request(stream, ident, parent) |
|
77 | 77 | |
|
78 | 78 | def start(self): |
|
79 | 79 | """ Override registration of dispatchers for streams. """ |
|
80 | 80 | self.shell.exit_now = False |
|
81 | 81 | |
|
82 | 82 | def _abort_queue(self, stream): |
|
83 | 83 | """ The in-process kernel doesn't abort requests. """ |
|
84 | 84 | pass |
|
85 | 85 | |
|
86 | 86 | def _raw_input(self, prompt, ident, parent): |
|
87 | 87 | # Flush output before making the request. |
|
88 | 88 | self.raw_input_str = None |
|
89 | 89 | sys.stderr.flush() |
|
90 | 90 | sys.stdout.flush() |
|
91 | 91 | |
|
92 | 92 | # Send the input request. |
|
93 | 93 | content = json_clean(dict(prompt=prompt)) |
|
94 | 94 | msg = self.session.msg(u'input_request', content, parent) |
|
95 | 95 | for frontend in self.frontends: |
|
96 | 96 | if frontend.session.session == parent['header']['session']: |
|
97 | 97 | frontend.stdin_channel.call_handlers(msg) |
|
98 | 98 | break |
|
99 | 99 | else: |
|
100 | 100 | logging.error('No frontend found for raw_input request') |
|
101 | 101 | return str() |
|
102 | 102 | |
|
103 | 103 | # Await a response. |
|
104 | 104 | while self.raw_input_str is None: |
|
105 | 105 | frontend.stdin_channel.process_events() |
|
106 | 106 | return self.raw_input_str |
|
107 | 107 | |
|
108 | 108 | #------------------------------------------------------------------------- |
|
109 | 109 | # Protected interface |
|
110 | 110 | #------------------------------------------------------------------------- |
|
111 | 111 | |
|
112 | 112 | @contextmanager |
|
113 | 113 | def _redirected_io(self): |
|
114 | 114 | """ Temporarily redirect IO to the kernel. |
|
115 | 115 | """ |
|
116 | 116 | sys_stdout, sys_stderr = sys.stdout, sys.stderr |
|
117 | 117 | sys.stdout, sys.stderr = self.stdout, self.stderr |
|
118 | 118 | yield |
|
119 | 119 | sys.stdout, sys.stderr = sys_stdout, sys_stderr |
|
120 | 120 | |
|
121 | 121 | #------ Trait change handlers -------------------------------------------- |
|
122 | 122 | |
|
123 | 123 | def _io_dispatch(self): |
|
124 | 124 | """ Called when a message is sent to the IO socket. |
|
125 | 125 | """ |
|
126 | 126 | ident, msg = self.session.recv(self.iopub_socket, copy=False) |
|
127 | 127 | for frontend in self.frontends: |
|
128 | 128 | frontend.iopub_channel.call_handlers(msg) |
|
129 | 129 | |
|
130 | 130 | #------ Trait initializers ----------------------------------------------- |
|
131 | 131 | |
|
132 | 132 | def _log_default(self): |
|
133 | 133 | return logging.getLogger(__name__) |
|
134 | 134 | |
|
135 | 135 | def _session_default(self): |
|
136 | 136 | from IPython.kernel.zmq.session import Session |
|
137 | 137 | return Session(parent=self) |
|
138 | 138 | |
|
139 | 139 | def _shell_class_default(self): |
|
140 | 140 | return InProcessInteractiveShell |
|
141 | 141 | |
|
142 | 142 | def _stdout_default(self): |
|
143 | 143 | from IPython.kernel.zmq.iostream import OutStream |
|
144 | 144 | return OutStream(self.session, self.iopub_socket, u'stdout', pipe=False) |
|
145 | 145 | |
|
146 | 146 | def _stderr_default(self): |
|
147 | 147 | from IPython.kernel.zmq.iostream import OutStream |
|
148 | 148 | return OutStream(self.session, self.iopub_socket, u'stderr', pipe=False) |
|
149 | 149 | |
|
150 | 150 | #----------------------------------------------------------------------------- |
|
151 | 151 | # Interactive shell subclass |
|
152 | 152 | #----------------------------------------------------------------------------- |
|
153 | 153 | |
|
154 | 154 | class InProcessInteractiveShell(ZMQInteractiveShell): |
|
155 | 155 | |
|
156 | 156 | kernel = Instance('IPython.kernel.inprocess.ipkernel.InProcessKernel') |
|
157 | 157 | |
|
158 | 158 | #------------------------------------------------------------------------- |
|
159 | 159 | # InteractiveShell interface |
|
160 | 160 | #------------------------------------------------------------------------- |
|
161 | 161 | |
|
162 | 162 | def enable_gui(self, gui=None): |
|
163 |
""" |
|
|
164 | """ | |
|
163 | """Enable GUI integration for the kernel.""" | |
|
165 | 164 | from IPython.kernel.zmq.eventloops import enable_gui |
|
166 | 165 | if not gui: |
|
167 | 166 | gui = self.kernel.gui |
|
168 | enable_gui(gui, kernel=self.kernel) | |
|
167 | return enable_gui(gui, kernel=self.kernel) | |
|
168 | ||
|
169 | def enable_matplotlib(self, gui=None): | |
|
170 | """Enable matplotlib integration for the kernel.""" | |
|
171 | if not gui: | |
|
172 | gui = self.kernel.gui | |
|
173 | return super(InProcessInteractiveShell, self).enable_matplotlib(self, gui) | |
|
169 | 174 | |
|
170 | 175 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): |
|
171 |
""" |
|
|
172 | """ | |
|
176 | """Activate pylab support at runtime.""" | |
|
173 | 177 | if not gui: |
|
174 | 178 | gui = self.kernel.gui |
|
175 | super(InProcessInteractiveShell, self).enable_pylab(gui, import_all, | |
|
179 | return super(InProcessInteractiveShell, self).enable_pylab(gui, import_all, | |
|
176 | 180 | welcome_message) |
|
177 | 181 | |
|
178 | 182 | InteractiveShellABC.register(InProcessInteractiveShell) |
General Comments 0
You need to be logged in to leave comments.
Login now