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