Show More
@@ -1,252 +1,252 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Frontend of ipython working with python-zmq |
|
3 | 3 | |
|
4 | 4 | Ipython's frontend, is a ipython interface that send request to kernel and proccess the kernel's outputs. |
|
5 | 5 | |
|
6 | 6 | For more details, see the ipython-zmq design |
|
7 | 7 | """ |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2011 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | from __future__ import print_function |
|
19 | 19 | |
|
20 | 20 | import bdb |
|
21 | 21 | import sys |
|
22 | 22 | |
|
23 | 23 | from Queue import Empty |
|
24 | 24 | |
|
25 | 25 | from IPython.core.alias import AliasManager, AliasError |
|
26 | 26 | from IPython.utils.warn import warn, error, fatal |
|
27 | 27 | from IPython.utils import io |
|
28 | 28 | |
|
29 | 29 | from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell |
|
30 | 30 | from IPython.frontend.zmqterminal.completer import ZMQCompleter |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | class ZMQTerminalInteractiveShell(TerminalInteractiveShell): |
|
34 | 34 | """A subclass of TerminalInteractiveShell that """ |
|
35 | 35 | |
|
36 | 36 | def __init__(self, *args, **kwargs): |
|
37 | 37 | self.km = kwargs.pop('kernel_manager') |
|
38 | 38 | self.session_id = self.km.session.session |
|
39 | 39 | super(ZMQTerminalInteractiveShell, self).__init__(*args, **kwargs) |
|
40 | 40 | |
|
41 | 41 | def init_completer(self): |
|
42 | 42 | """Initialize the completion machinery. |
|
43 | 43 | |
|
44 | 44 | This creates completion machinery that can be used by client code, |
|
45 | 45 | either interactively in-process (typically triggered by the readline |
|
46 | 46 | library), programatically (such as in test suites) or out-of-prcess |
|
47 | 47 | (typically over the network by remote frontends). |
|
48 | 48 | """ |
|
49 | 49 | from IPython.core.completerlib import (module_completer, |
|
50 | 50 | magic_run_completer, cd_completer) |
|
51 | 51 | |
|
52 | 52 | self.Completer = ZMQCompleter(self, self.km) |
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
56 | 56 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
57 | 57 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
58 | 58 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
59 | 59 | |
|
60 | 60 | # Only configure readline if we truly are using readline. IPython can |
|
61 | 61 | # do tab-completion over the network, in GUIs, etc, where readline |
|
62 | 62 | # itself may be absent |
|
63 | 63 | if self.has_readline: |
|
64 | 64 | self.set_readline_completer() |
|
65 | 65 | |
|
66 | 66 | def run_cell(self, cell, store_history=True): |
|
67 | 67 | """Run a complete IPython cell. |
|
68 | 68 | |
|
69 | 69 | Parameters |
|
70 | 70 | ---------- |
|
71 | 71 | cell : str |
|
72 | 72 | The code (including IPython code such as %magic functions) to run. |
|
73 | 73 | store_history : bool |
|
74 | 74 | If True, the raw and translated cell will be stored in IPython's |
|
75 | 75 | history. For user code calling back into IPython's machinery, this |
|
76 | 76 | should be set to False. |
|
77 | 77 | """ |
|
78 | 78 | if (not cell) or cell.isspace(): |
|
79 | 79 | return |
|
80 | 80 | |
|
81 | 81 | # shell_channel.execute takes 'hidden', which is the inverse of store_hist |
|
82 | 82 | msg_id = self.km.shell_channel.execute(cell, not store_history) |
|
83 | 83 | while not self.km.shell_channel.msg_ready(): |
|
84 | 84 | try: |
|
85 | 85 | self.handle_stdin_request(timeout=0.05) |
|
86 | 86 | except Empty: |
|
87 | 87 | pass |
|
88 | 88 | self.handle_execute_reply(msg_id) |
|
89 | 89 | |
|
90 | 90 | #----------------- |
|
91 | 91 | # message handlers |
|
92 | 92 | #----------------- |
|
93 | 93 | |
|
94 | 94 | def handle_execute_reply(self, msg_id): |
|
95 | 95 | msg = self.km.shell_channel.get_msg() |
|
96 | 96 | if msg["parent_header"]["msg_id"] == msg_id: |
|
97 | 97 | if msg["content"]["status"] == 'ok' : |
|
98 | 98 | self.handle_iopub() |
|
99 | 99 | |
|
100 | 100 | elif msg["content"]["status"] == 'error': |
|
101 | 101 | for frame in msg["content"]["traceback"]: |
|
102 | 102 | print(frame, file=io.stderr) |
|
103 | 103 | |
|
104 | self.execution_count = msg["content"]["execution_count"] + 1 | |
|
104 | self.execution_count = int(msg["content"]["execution_count"] + 1) | |
|
105 | 105 | |
|
106 | 106 | |
|
107 | 107 | def handle_iopub(self): |
|
108 | 108 | """ Method to procces subscribe channel's messages |
|
109 | 109 | |
|
110 | 110 | This method reads a message and processes the content in different |
|
111 | 111 | outputs like stdout, stderr, pyout and status |
|
112 | 112 | |
|
113 | 113 | Arguments: |
|
114 | 114 | sub_msg: message receive from kernel in the sub socket channel |
|
115 | 115 | capture by kernel manager. |
|
116 | 116 | """ |
|
117 | 117 | while self.km.sub_channel.msg_ready(): |
|
118 | 118 | sub_msg = self.km.sub_channel.get_msg() |
|
119 | 119 | msg_type = sub_msg['header']['msg_type'] |
|
120 | 120 | if self.session_id == sub_msg['parent_header']['session']: |
|
121 | 121 | if msg_type == 'status' : |
|
122 | 122 | if sub_msg["content"]["execution_state"] == "busy" : |
|
123 | 123 | pass |
|
124 | 124 | |
|
125 | 125 | elif msg_type == 'stream' : |
|
126 | 126 | if sub_msg["content"]["name"] == "stdout": |
|
127 | 127 | print(sub_msg["content"]["data"], file=io.stdout, end="") |
|
128 | 128 | io.stdout.flush() |
|
129 | 129 | elif sub_msg["content"]["name"] == "stderr" : |
|
130 | 130 | print(sub_msg["content"]["data"], file=io.stderr, end="") |
|
131 | 131 | io.stderr.flush() |
|
132 | 132 | |
|
133 | 133 | elif msg_type == 'pyout': |
|
134 | 134 | format_dict = sub_msg["content"]["data"] |
|
135 | 135 | # taken from DisplayHook.__call__: |
|
136 | 136 | hook = self.displayhook |
|
137 | 137 | hook.start_displayhook() |
|
138 | 138 | hook.write_output_prompt() |
|
139 | 139 | hook.write_format_data(format_dict) |
|
140 | 140 | hook.log_output(format_dict) |
|
141 | 141 | hook.finish_displayhook() |
|
142 | 142 | |
|
143 | 143 | def handle_stdin_request(self, timeout=0.1): |
|
144 | 144 | """ Method to capture raw_input |
|
145 | 145 | """ |
|
146 | 146 | msg_rep = self.km.stdin_channel.get_msg(timeout=timeout) |
|
147 | 147 | if self.session_id == msg_rep["parent_header"]["session"] : |
|
148 | 148 | raw_data = raw_input(msg_rep["content"]["prompt"]) |
|
149 | 149 | self.km.stdin_channel.input(raw_data) |
|
150 | 150 | |
|
151 | 151 | def mainloop(self, display_banner=False): |
|
152 | 152 | while True: |
|
153 | 153 | try: |
|
154 | 154 | self.interact(display_banner=display_banner) |
|
155 | 155 | #self.interact_with_readline() |
|
156 | 156 | # XXX for testing of a readline-decoupled repl loop, call |
|
157 | 157 | # interact_with_readline above |
|
158 | 158 | break |
|
159 | 159 | except KeyboardInterrupt: |
|
160 | 160 | # this should not be necessary, but KeyboardInterrupt |
|
161 | 161 | # handling seems rather unpredictable... |
|
162 | 162 | self.write("\nKeyboardInterrupt in interact()\n") |
|
163 | 163 | |
|
164 | 164 | def interact(self, display_banner=None): |
|
165 | 165 | """Closely emulate the interactive Python console.""" |
|
166 | 166 | |
|
167 | 167 | # batch run -> do not interact |
|
168 | 168 | if self.exit_now: |
|
169 | 169 | return |
|
170 | 170 | |
|
171 | 171 | if display_banner is None: |
|
172 | 172 | display_banner = self.display_banner |
|
173 | 173 | |
|
174 | 174 | if isinstance(display_banner, basestring): |
|
175 | 175 | self.show_banner(display_banner) |
|
176 | 176 | elif display_banner: |
|
177 | 177 | self.show_banner() |
|
178 | 178 | |
|
179 | 179 | more = False |
|
180 | 180 | |
|
181 | 181 | if self.has_readline: |
|
182 | 182 | self.readline_startup_hook(self.pre_readline) |
|
183 | 183 | # exit_now is set by a call to %Exit or %Quit, through the |
|
184 | 184 | # ask_exit callback. |
|
185 | 185 | |
|
186 | 186 | while not self.exit_now: |
|
187 | 187 | if not self.km.is_alive: |
|
188 | 188 | ans = self.raw_input("kernel died, restart ([y]/n)?") |
|
189 | 189 | if not ans.lower().startswith('n'): |
|
190 | 190 | self.km.restart_kernel(True) |
|
191 | 191 | else: |
|
192 | 192 | self.exit_now=True |
|
193 | 193 | continue |
|
194 | 194 | self.hooks.pre_prompt_hook() |
|
195 | 195 | if more: |
|
196 | 196 | try: |
|
197 | 197 | prompt = self.hooks.generate_prompt(True) |
|
198 | 198 | except: |
|
199 | 199 | self.showtraceback() |
|
200 | 200 | if self.autoindent: |
|
201 | 201 | self.rl_do_indent = True |
|
202 | 202 | |
|
203 | 203 | else: |
|
204 | 204 | try: |
|
205 | 205 | prompt = self.hooks.generate_prompt(False) |
|
206 | 206 | except: |
|
207 | 207 | self.showtraceback() |
|
208 | 208 | try: |
|
209 | 209 | line = self.raw_input(prompt) |
|
210 | 210 | if self.exit_now: |
|
211 | 211 | # quick exit on sys.std[in|out] close |
|
212 | 212 | break |
|
213 | 213 | if self.autoindent: |
|
214 | 214 | self.rl_do_indent = False |
|
215 | 215 | |
|
216 | 216 | except KeyboardInterrupt: |
|
217 | 217 | #double-guard against keyboardinterrupts during kbdint handling |
|
218 | 218 | try: |
|
219 | 219 | self.write('\nKeyboardInterrupt\n') |
|
220 | 220 | self.input_splitter.reset() |
|
221 | 221 | more = False |
|
222 | 222 | except KeyboardInterrupt: |
|
223 | 223 | pass |
|
224 | 224 | except EOFError: |
|
225 | 225 | if self.autoindent: |
|
226 | 226 | self.rl_do_indent = False |
|
227 | 227 | if self.has_readline: |
|
228 | 228 | self.readline_startup_hook(None) |
|
229 | 229 | self.write('\n') |
|
230 | 230 | self.exit() |
|
231 | 231 | except bdb.BdbQuit: |
|
232 | 232 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
233 | 233 | 'Because of how pdb handles the stack, it is impossible\n' |
|
234 | 234 | 'for IPython to properly format this particular exception.\n' |
|
235 | 235 | 'IPython will resume normal operation.') |
|
236 | 236 | except: |
|
237 | 237 | # exceptions here are VERY RARE, but they can be triggered |
|
238 | 238 | # asynchronously by signal handlers, for example. |
|
239 | 239 | self.showtraceback() |
|
240 | 240 | else: |
|
241 | 241 | self.input_splitter.push(line) |
|
242 | 242 | more = self.input_splitter.push_accepts_more() |
|
243 | 243 | if (self.SyntaxTB.last_syntax_error and |
|
244 | 244 | self.autoedit_syntax): |
|
245 | 245 | self.edit_syntax_error() |
|
246 | 246 | if not more: |
|
247 | 247 | source_raw = self.input_splitter.source_reset() |
|
248 | 248 | self.run_cell(source_raw) |
|
249 | 249 | |
|
250 | 250 | |
|
251 | 251 | # Turn off the exit flag, so the mainloop can be restarted if desired |
|
252 | 252 | self.exit_now = False |
General Comments 0
You need to be logged in to leave comments.
Login now