##// END OF EJS Templates
disable subprocess pipe in inprocess kernel...
MinRK -
Show More
@@ -1,177 +1,177 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.kernelmanager.InProcessKernelManager'))
40 Instance('IPython.kernel.inprocess.kernelmanager.InProcessKernelManager'))
41
41
42 # The GUI environment that the kernel is running under. This need not be
42 # The GUI environment that the kernel is running under. This need not be
43 # specified for the normal operation for the kernel, but is required for
43 # specified for the normal operation for the kernel, but is required for
44 # IPython's GUI support (including pylab). The default is 'inline' because
44 # IPython's GUI support (including pylab). The default is 'inline' because
45 # it is safe under all GUI toolkits.
45 # it is safe under all GUI toolkits.
46 gui = Enum(('tk', 'gtk', 'wx', 'qt', 'qt4', 'inline'),
46 gui = Enum(('tk', 'gtk', 'wx', 'qt', 'qt4', 'inline'),
47 default_value='inline')
47 default_value='inline')
48
48
49 raw_input_str = Any()
49 raw_input_str = Any()
50 stdout = Any()
50 stdout = Any()
51 stderr = Any()
51 stderr = Any()
52
52
53 #-------------------------------------------------------------------------
53 #-------------------------------------------------------------------------
54 # Kernel interface
54 # Kernel interface
55 #-------------------------------------------------------------------------
55 #-------------------------------------------------------------------------
56
56
57 shell_class = Type()
57 shell_class = Type()
58 shell_streams = List()
58 shell_streams = List()
59 control_stream = Any()
59 control_stream = Any()
60 iopub_socket = Instance(DummySocket, ())
60 iopub_socket = Instance(DummySocket, ())
61 stdin_socket = Instance(DummySocket, ())
61 stdin_socket = Instance(DummySocket, ())
62
62
63 def __init__(self, **traits):
63 def __init__(self, **traits):
64 # When an InteractiveShell is instantiated by our base class, it binds
64 # When an InteractiveShell is instantiated by our base class, it binds
65 # the current values of sys.stdout and sys.stderr.
65 # the current values of sys.stdout and sys.stderr.
66 with self._redirected_io():
66 with self._redirected_io():
67 super(InProcessKernel, self).__init__(**traits)
67 super(InProcessKernel, self).__init__(**traits)
68
68
69 self.iopub_socket.on_trait_change(self._io_dispatch, 'message_sent')
69 self.iopub_socket.on_trait_change(self._io_dispatch, 'message_sent')
70 self.shell.kernel = self
70 self.shell.kernel = self
71
71
72 def execute_request(self, stream, ident, parent):
72 def execute_request(self, stream, ident, parent):
73 """ Override for temporary IO redirection. """
73 """ Override for temporary IO redirection. """
74 with self._redirected_io():
74 with self._redirected_io():
75 super(InProcessKernel, self).execute_request(stream, ident, parent)
75 super(InProcessKernel, self).execute_request(stream, ident, parent)
76
76
77 def start(self):
77 def start(self):
78 """ Override registration of dispatchers for streams. """
78 """ Override registration of dispatchers for streams. """
79 self.shell.exit_now = False
79 self.shell.exit_now = False
80
80
81 def _abort_queue(self, stream):
81 def _abort_queue(self, stream):
82 """ The in-process kernel doesn't abort requests. """
82 """ The in-process kernel doesn't abort requests. """
83 pass
83 pass
84
84
85 def _raw_input(self, prompt, ident, parent):
85 def _raw_input(self, prompt, ident, parent):
86 # Flush output before making the request.
86 # Flush output before making the request.
87 self.raw_input_str = None
87 self.raw_input_str = None
88 sys.stderr.flush()
88 sys.stderr.flush()
89 sys.stdout.flush()
89 sys.stdout.flush()
90
90
91 # Send the input request.
91 # Send the input request.
92 content = json_clean(dict(prompt=prompt))
92 content = json_clean(dict(prompt=prompt))
93 msg = self.session.msg(u'input_request', content, parent)
93 msg = self.session.msg(u'input_request', content, parent)
94 for frontend in self.frontends:
94 for frontend in self.frontends:
95 if frontend.session.session == parent['header']['session']:
95 if frontend.session.session == parent['header']['session']:
96 frontend.stdin_channel.call_handlers(msg)
96 frontend.stdin_channel.call_handlers(msg)
97 break
97 break
98 else:
98 else:
99 logging.error('No frontend found for raw_input request')
99 logging.error('No frontend found for raw_input request')
100 return str()
100 return str()
101
101
102 # Await a response.
102 # Await a response.
103 while self.raw_input_str is None:
103 while self.raw_input_str is None:
104 frontend.stdin_channel.process_events()
104 frontend.stdin_channel.process_events()
105 return self.raw_input_str
105 return self.raw_input_str
106
106
107 #-------------------------------------------------------------------------
107 #-------------------------------------------------------------------------
108 # Protected interface
108 # Protected interface
109 #-------------------------------------------------------------------------
109 #-------------------------------------------------------------------------
110
110
111 @contextmanager
111 @contextmanager
112 def _redirected_io(self):
112 def _redirected_io(self):
113 """ Temporarily redirect IO to the kernel.
113 """ Temporarily redirect IO to the kernel.
114 """
114 """
115 sys_stdout, sys_stderr = sys.stdout, sys.stderr
115 sys_stdout, sys_stderr = sys.stdout, sys.stderr
116 sys.stdout, sys.stderr = self.stdout, self.stderr
116 sys.stdout, sys.stderr = self.stdout, self.stderr
117 yield
117 yield
118 sys.stdout, sys.stderr = sys_stdout, sys_stderr
118 sys.stdout, sys.stderr = sys_stdout, sys_stderr
119
119
120 #------ Trait change handlers --------------------------------------------
120 #------ Trait change handlers --------------------------------------------
121
121
122 def _io_dispatch(self):
122 def _io_dispatch(self):
123 """ Called when a message is sent to the IO socket.
123 """ Called when a message is sent to the IO socket.
124 """
124 """
125 ident, msg = self.session.recv(self.iopub_socket, copy=False)
125 ident, msg = self.session.recv(self.iopub_socket, copy=False)
126 for frontend in self.frontends:
126 for frontend in self.frontends:
127 frontend.iopub_channel.call_handlers(msg)
127 frontend.iopub_channel.call_handlers(msg)
128
128
129 #------ Trait initializers -----------------------------------------------
129 #------ Trait initializers -----------------------------------------------
130
130
131 def _log_default(self):
131 def _log_default(self):
132 return logging.getLogger(__name__)
132 return logging.getLogger(__name__)
133
133
134 def _session_default(self):
134 def _session_default(self):
135 from IPython.kernel.zmq.session import Session
135 from IPython.kernel.zmq.session import Session
136 return Session(config=self.config)
136 return Session(config=self.config)
137
137
138 def _shell_class_default(self):
138 def _shell_class_default(self):
139 return InProcessInteractiveShell
139 return InProcessInteractiveShell
140
140
141 def _stdout_default(self):
141 def _stdout_default(self):
142 from IPython.kernel.zmq.iostream import OutStream
142 from IPython.kernel.zmq.iostream import OutStream
143 return OutStream(self.session, self.iopub_socket, u'stdout')
143 return OutStream(self.session, self.iopub_socket, u'stdout', pipe=False)
144
144
145 def _stderr_default(self):
145 def _stderr_default(self):
146 from IPython.kernel.zmq.iostream import OutStream
146 from IPython.kernel.zmq.iostream import OutStream
147 return OutStream(self.session, self.iopub_socket, u'stderr')
147 return OutStream(self.session, self.iopub_socket, u'stderr', pipe=False)
148
148
149 #-----------------------------------------------------------------------------
149 #-----------------------------------------------------------------------------
150 # Interactive shell subclass
150 # Interactive shell subclass
151 #-----------------------------------------------------------------------------
151 #-----------------------------------------------------------------------------
152
152
153 class InProcessInteractiveShell(ZMQInteractiveShell):
153 class InProcessInteractiveShell(ZMQInteractiveShell):
154
154
155 kernel = Instance('IPython.kernel.inprocess.ipkernel.InProcessKernel')
155 kernel = Instance('IPython.kernel.inprocess.ipkernel.InProcessKernel')
156
156
157 #-------------------------------------------------------------------------
157 #-------------------------------------------------------------------------
158 # InteractiveShell interface
158 # InteractiveShell interface
159 #-------------------------------------------------------------------------
159 #-------------------------------------------------------------------------
160
160
161 def enable_gui(self, gui=None):
161 def enable_gui(self, gui=None):
162 """ Enable GUI integration for the kernel.
162 """ Enable GUI integration for the kernel.
163 """
163 """
164 from IPython.kernel.zmq.eventloops import enable_gui
164 from IPython.kernel.zmq.eventloops import enable_gui
165 if not gui:
165 if not gui:
166 gui = self.kernel.gui
166 gui = self.kernel.gui
167 enable_gui(gui, kernel=self.kernel)
167 enable_gui(gui, kernel=self.kernel)
168
168
169 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
169 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
170 """ Activate pylab support at runtime.
170 """ Activate pylab support at runtime.
171 """
171 """
172 if not gui:
172 if not gui:
173 gui = self.kernel.gui
173 gui = self.kernel.gui
174 super(InProcessInteractiveShell, self).enable_pylab(gui, import_all,
174 super(InProcessInteractiveShell, self).enable_pylab(gui, import_all,
175 welcome_message)
175 welcome_message)
176
176
177 InteractiveShellABC.register(InProcessInteractiveShell)
177 InteractiveShellABC.register(InProcessInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now