Show More
@@ -1,418 +1,427 b'' | |||||
1 | """A ZMQ-based subclass of InteractiveShell. |
|
1 | """A ZMQ-based subclass of InteractiveShell. | |
2 |
|
2 | |||
3 | This code is meant to ease the refactoring of the base InteractiveShell into |
|
3 | This code is meant to ease the refactoring of the base InteractiveShell into | |
4 | something with a cleaner architecture for 2-process use, without actually |
|
4 | something with a cleaner architecture for 2-process use, without actually | |
5 | breaking InteractiveShell itself. So we're doing something a bit ugly, where |
|
5 | breaking InteractiveShell itself. So we're doing something a bit ugly, where | |
6 | we subclass and override what we want to fix. Once this is working well, we |
|
6 | we subclass and override what we want to fix. Once this is working well, we | |
7 | can go back to the base class and refactor the code for a cleaner inheritance |
|
7 | can go back to the base class and refactor the code for a cleaner inheritance | |
8 | implementation that doesn't rely on so much monkeypatching. |
|
8 | implementation that doesn't rely on so much monkeypatching. | |
9 |
|
9 | |||
10 | But this lets us maintain a fully working IPython as we develop the new |
|
10 | But this lets us maintain a fully working IPython as we develop the new | |
11 | machinery. This should thus be thought of as scaffolding. |
|
11 | machinery. This should thus be thought of as scaffolding. | |
12 | """ |
|
12 | """ | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | from __future__ import print_function | |||
|
17 | ||||
16 | # Stdlib |
|
18 | # Stdlib | |
17 | import inspect |
|
19 | import inspect | |
18 | import os |
|
20 | import os | |
19 | import re |
|
21 | import re | |
20 | import sys |
|
22 | import sys | |
21 |
|
23 | |||
22 | from subprocess import Popen, PIPE |
|
24 | from subprocess import Popen, PIPE | |
23 |
|
25 | |||
24 | # Our own |
|
26 | # Our own | |
25 | from IPython.core.interactiveshell import ( |
|
27 | from IPython.core.interactiveshell import ( | |
26 | InteractiveShell, InteractiveShellABC |
|
28 | InteractiveShell, InteractiveShellABC | |
27 | ) |
|
29 | ) | |
28 | from IPython.core.displayhook import DisplayHook |
|
30 | from IPython.core.displayhook import DisplayHook | |
29 | from IPython.core.macro import Macro |
|
31 | from IPython.core.macro import Macro | |
30 | from IPython.utils.path import get_py_filename |
|
32 | from IPython.utils.path import get_py_filename | |
31 | from IPython.utils.text import StringTypes |
|
33 | from IPython.utils.text import StringTypes | |
32 | from IPython.utils.traitlets import Instance, Type, Dict |
|
34 | from IPython.utils.traitlets import Instance, Type, Dict | |
33 | from IPython.utils.warn import warn |
|
35 | from IPython.utils.warn import warn | |
34 | from IPython.zmq.session import extract_header |
|
36 | from IPython.zmq.session import extract_header | |
35 | from IPython.core.payloadpage import install_payload_page |
|
37 | from IPython.core.payloadpage import install_payload_page | |
36 | from session import Session |
|
38 | from session import Session | |
37 |
|
39 | |||
38 | #----------------------------------------------------------------------------- |
|
40 | #----------------------------------------------------------------------------- | |
39 | # Globals and side-effects |
|
41 | # Globals and side-effects | |
40 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
41 |
|
43 | |||
42 | # Install the payload version of page. |
|
44 | # Install the payload version of page. | |
43 | install_payload_page() |
|
45 | install_payload_page() | |
44 |
|
46 | |||
45 | #----------------------------------------------------------------------------- |
|
47 | #----------------------------------------------------------------------------- | |
46 | # Functions and classes |
|
48 | # Functions and classes | |
47 | #----------------------------------------------------------------------------- |
|
49 | #----------------------------------------------------------------------------- | |
48 |
|
50 | |||
49 | class ZMQDisplayHook(DisplayHook): |
|
51 | class ZMQDisplayHook(DisplayHook): | |
50 |
|
52 | |||
51 | session = Instance(Session) |
|
53 | session = Instance(Session) | |
52 | pub_socket = Instance('zmq.Socket') |
|
54 | pub_socket = Instance('zmq.Socket') | |
53 | parent_header = Dict({}) |
|
55 | parent_header = Dict({}) | |
54 |
|
56 | |||
55 | def set_parent(self, parent): |
|
57 | def set_parent(self, parent): | |
56 | """Set the parent for outbound messages.""" |
|
58 | """Set the parent for outbound messages.""" | |
57 | self.parent_header = extract_header(parent) |
|
59 | self.parent_header = extract_header(parent) | |
58 |
|
60 | |||
59 | def start_displayhook(self): |
|
61 | def start_displayhook(self): | |
60 | self.msg = self.session.msg(u'pyout', {}, parent=self.parent_header) |
|
62 | self.msg = self.session.msg(u'pyout', {}, parent=self.parent_header) | |
61 |
|
63 | |||
62 | def write_output_prompt(self): |
|
64 | def write_output_prompt(self): | |
63 | """Write the output prompt.""" |
|
65 | """Write the output prompt.""" | |
64 | if self.do_full_cache: |
|
66 | if self.do_full_cache: | |
65 | self.msg['content']['output_sep'] = self.output_sep |
|
67 | self.msg['content']['output_sep'] = self.output_sep | |
66 | self.msg['content']['prompt_string'] = str(self.prompt_out) |
|
68 | self.msg['content']['prompt_string'] = str(self.prompt_out) | |
67 | self.msg['content']['prompt_number'] = self.prompt_count |
|
69 | self.msg['content']['prompt_number'] = self.prompt_count | |
68 | self.msg['content']['output_sep2'] = self.output_sep2 |
|
70 | self.msg['content']['output_sep2'] = self.output_sep2 | |
69 |
|
71 | |||
70 | def write_result_repr(self, result_repr): |
|
72 | def write_result_repr(self, result_repr): | |
71 | self.msg['content']['data'] = result_repr |
|
73 | self.msg['content']['data'] = result_repr | |
72 |
|
74 | |||
73 | def finish_displayhook(self): |
|
75 | def finish_displayhook(self): | |
74 | """Finish up all displayhook activities.""" |
|
76 | """Finish up all displayhook activities.""" | |
75 | self.pub_socket.send_json(self.msg) |
|
77 | self.pub_socket.send_json(self.msg) | |
76 | self.msg = None |
|
78 | self.msg = None | |
77 |
|
79 | |||
78 |
|
80 | |||
79 | class ZMQInteractiveShell(InteractiveShell): |
|
81 | class ZMQInteractiveShell(InteractiveShell): | |
80 | """A subclass of InteractiveShell for ZMQ.""" |
|
82 | """A subclass of InteractiveShell for ZMQ.""" | |
81 |
|
83 | |||
82 | displayhook_class = Type(ZMQDisplayHook) |
|
84 | displayhook_class = Type(ZMQDisplayHook) | |
83 |
|
85 | |||
84 | def system(self, cmd): |
|
86 | def system(self, cmd): | |
85 | cmd = self.var_expand(cmd, depth=2) |
|
87 | cmd = self.var_expand(cmd, depth=2).strip() | |
|
88 | ||||
|
89 | # Runnning a bacgkrounded process from within the gui isn't supported | |||
|
90 | # because we do p.wait() at the end. So instead of silently blocking | |||
|
91 | # we simply refuse to run in this mode, to avoid surprising the user. | |||
|
92 | if cmd.endswith('&'): | |||
|
93 | raise OSError("Background processes not supported.") | |||
|
94 | ||||
86 | sys.stdout.flush() |
|
95 | sys.stdout.flush() | |
87 | sys.stderr.flush() |
|
96 | sys.stderr.flush() | |
88 | p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) |
|
97 | p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) | |
89 | for line in p.stdout.read().split('\n'): |
|
98 | for line in p.stdout.read().split('\n'): | |
90 | if len(line) > 0: |
|
99 | if len(line) > 0: | |
91 |
print |
|
100 | print(line) | |
92 | for line in p.stderr.read().split('\n'): |
|
101 | for line in p.stderr.read().split('\n'): | |
93 | if len(line) > 0: |
|
102 | if len(line) > 0: | |
94 |
print |
|
103 | print(line, file=sys.stderr) | |
95 | p.wait() |
|
104 | p.wait() | |
96 |
|
105 | |||
97 | def init_io(self): |
|
106 | def init_io(self): | |
98 | # This will just use sys.stdout and sys.stderr. If you want to |
|
107 | # This will just use sys.stdout and sys.stderr. If you want to | |
99 | # override sys.stdout and sys.stderr themselves, you need to do that |
|
108 | # override sys.stdout and sys.stderr themselves, you need to do that | |
100 | # *before* instantiating this class, because Term holds onto |
|
109 | # *before* instantiating this class, because Term holds onto | |
101 | # references to the underlying streams. |
|
110 | # references to the underlying streams. | |
102 | import IPython.utils.io |
|
111 | import IPython.utils.io | |
103 | Term = IPython.utils.io.IOTerm() |
|
112 | Term = IPython.utils.io.IOTerm() | |
104 | IPython.utils.io.Term = Term |
|
113 | IPython.utils.io.Term = Term | |
105 |
|
114 | |||
106 | def magic_edit(self,parameter_s='',last_call=['','']): |
|
115 | def magic_edit(self,parameter_s='',last_call=['','']): | |
107 | """Bring up an editor and execute the resulting code. |
|
116 | """Bring up an editor and execute the resulting code. | |
108 |
|
117 | |||
109 | Usage: |
|
118 | Usage: | |
110 | %edit [options] [args] |
|
119 | %edit [options] [args] | |
111 |
|
120 | |||
112 | %edit runs IPython's editor hook. The default version of this hook is |
|
121 | %edit runs IPython's editor hook. The default version of this hook is | |
113 | set to call the __IPYTHON__.rc.editor command. This is read from your |
|
122 | set to call the __IPYTHON__.rc.editor command. This is read from your | |
114 | environment variable $EDITOR. If this isn't found, it will default to |
|
123 | environment variable $EDITOR. If this isn't found, it will default to | |
115 | vi under Linux/Unix and to notepad under Windows. See the end of this |
|
124 | vi under Linux/Unix and to notepad under Windows. See the end of this | |
116 | docstring for how to change the editor hook. |
|
125 | docstring for how to change the editor hook. | |
117 |
|
126 | |||
118 | You can also set the value of this editor via the command line option |
|
127 | You can also set the value of this editor via the command line option | |
119 | '-editor' or in your ipythonrc file. This is useful if you wish to use |
|
128 | '-editor' or in your ipythonrc file. This is useful if you wish to use | |
120 | specifically for IPython an editor different from your typical default |
|
129 | specifically for IPython an editor different from your typical default | |
121 | (and for Windows users who typically don't set environment variables). |
|
130 | (and for Windows users who typically don't set environment variables). | |
122 |
|
131 | |||
123 | This command allows you to conveniently edit multi-line code right in |
|
132 | This command allows you to conveniently edit multi-line code right in | |
124 | your IPython session. |
|
133 | your IPython session. | |
125 |
|
134 | |||
126 | If called without arguments, %edit opens up an empty editor with a |
|
135 | If called without arguments, %edit opens up an empty editor with a | |
127 | temporary file and will execute the contents of this file when you |
|
136 | temporary file and will execute the contents of this file when you | |
128 | close it (don't forget to save it!). |
|
137 | close it (don't forget to save it!). | |
129 |
|
138 | |||
130 |
|
139 | |||
131 | Options: |
|
140 | Options: | |
132 |
|
141 | |||
133 | -n <number>: open the editor at a specified line number. By default, |
|
142 | -n <number>: open the editor at a specified line number. By default, | |
134 | the IPython editor hook uses the unix syntax 'editor +N filename', but |
|
143 | the IPython editor hook uses the unix syntax 'editor +N filename', but | |
135 | you can configure this by providing your own modified hook if your |
|
144 | you can configure this by providing your own modified hook if your | |
136 | favorite editor supports line-number specifications with a different |
|
145 | favorite editor supports line-number specifications with a different | |
137 | syntax. |
|
146 | syntax. | |
138 |
|
147 | |||
139 | -p: this will call the editor with the same data as the previous time |
|
148 | -p: this will call the editor with the same data as the previous time | |
140 | it was used, regardless of how long ago (in your current session) it |
|
149 | it was used, regardless of how long ago (in your current session) it | |
141 | was. |
|
150 | was. | |
142 |
|
151 | |||
143 | -r: use 'raw' input. This option only applies to input taken from the |
|
152 | -r: use 'raw' input. This option only applies to input taken from the | |
144 | user's history. By default, the 'processed' history is used, so that |
|
153 | user's history. By default, the 'processed' history is used, so that | |
145 | magics are loaded in their transformed version to valid Python. If |
|
154 | magics are loaded in their transformed version to valid Python. If | |
146 | this option is given, the raw input as typed as the command line is |
|
155 | this option is given, the raw input as typed as the command line is | |
147 | used instead. When you exit the editor, it will be executed by |
|
156 | used instead. When you exit the editor, it will be executed by | |
148 | IPython's own processor. |
|
157 | IPython's own processor. | |
149 |
|
158 | |||
150 | -x: do not execute the edited code immediately upon exit. This is |
|
159 | -x: do not execute the edited code immediately upon exit. This is | |
151 | mainly useful if you are editing programs which need to be called with |
|
160 | mainly useful if you are editing programs which need to be called with | |
152 | command line arguments, which you can then do using %run. |
|
161 | command line arguments, which you can then do using %run. | |
153 |
|
162 | |||
154 |
|
163 | |||
155 | Arguments: |
|
164 | Arguments: | |
156 |
|
165 | |||
157 | If arguments are given, the following possibilites exist: |
|
166 | If arguments are given, the following possibilites exist: | |
158 |
|
167 | |||
159 | - The arguments are numbers or pairs of colon-separated numbers (like |
|
168 | - The arguments are numbers or pairs of colon-separated numbers (like | |
160 | 1 4:8 9). These are interpreted as lines of previous input to be |
|
169 | 1 4:8 9). These are interpreted as lines of previous input to be | |
161 | loaded into the editor. The syntax is the same of the %macro command. |
|
170 | loaded into the editor. The syntax is the same of the %macro command. | |
162 |
|
171 | |||
163 | - If the argument doesn't start with a number, it is evaluated as a |
|
172 | - If the argument doesn't start with a number, it is evaluated as a | |
164 | variable and its contents loaded into the editor. You can thus edit |
|
173 | variable and its contents loaded into the editor. You can thus edit | |
165 | any string which contains python code (including the result of |
|
174 | any string which contains python code (including the result of | |
166 | previous edits). |
|
175 | previous edits). | |
167 |
|
176 | |||
168 | - If the argument is the name of an object (other than a string), |
|
177 | - If the argument is the name of an object (other than a string), | |
169 | IPython will try to locate the file where it was defined and open the |
|
178 | IPython will try to locate the file where it was defined and open the | |
170 | editor at the point where it is defined. You can use `%edit function` |
|
179 | editor at the point where it is defined. You can use `%edit function` | |
171 | to load an editor exactly at the point where 'function' is defined, |
|
180 | to load an editor exactly at the point where 'function' is defined, | |
172 | edit it and have the file be executed automatically. |
|
181 | edit it and have the file be executed automatically. | |
173 |
|
182 | |||
174 | If the object is a macro (see %macro for details), this opens up your |
|
183 | If the object is a macro (see %macro for details), this opens up your | |
175 | specified editor with a temporary file containing the macro's data. |
|
184 | specified editor with a temporary file containing the macro's data. | |
176 | Upon exit, the macro is reloaded with the contents of the file. |
|
185 | Upon exit, the macro is reloaded with the contents of the file. | |
177 |
|
186 | |||
178 | Note: opening at an exact line is only supported under Unix, and some |
|
187 | Note: opening at an exact line is only supported under Unix, and some | |
179 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
188 | editors (like kedit and gedit up to Gnome 2.8) do not understand the | |
180 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
189 | '+NUMBER' parameter necessary for this feature. Good editors like | |
181 | (X)Emacs, vi, jed, pico and joe all do. |
|
190 | (X)Emacs, vi, jed, pico and joe all do. | |
182 |
|
191 | |||
183 | - If the argument is not found as a variable, IPython will look for a |
|
192 | - If the argument is not found as a variable, IPython will look for a | |
184 | file with that name (adding .py if necessary) and load it into the |
|
193 | file with that name (adding .py if necessary) and load it into the | |
185 | editor. It will execute its contents with execfile() when you exit, |
|
194 | editor. It will execute its contents with execfile() when you exit, | |
186 | loading any code in the file into your interactive namespace. |
|
195 | loading any code in the file into your interactive namespace. | |
187 |
|
196 | |||
188 | After executing your code, %edit will return as output the code you |
|
197 | After executing your code, %edit will return as output the code you | |
189 | typed in the editor (except when it was an existing file). This way |
|
198 | typed in the editor (except when it was an existing file). This way | |
190 | you can reload the code in further invocations of %edit as a variable, |
|
199 | you can reload the code in further invocations of %edit as a variable, | |
191 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of |
|
200 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of | |
192 | the output. |
|
201 | the output. | |
193 |
|
202 | |||
194 | Note that %edit is also available through the alias %ed. |
|
203 | Note that %edit is also available through the alias %ed. | |
195 |
|
204 | |||
196 | This is an example of creating a simple function inside the editor and |
|
205 | This is an example of creating a simple function inside the editor and | |
197 | then modifying it. First, start up the editor: |
|
206 | then modifying it. First, start up the editor: | |
198 |
|
207 | |||
199 | In [1]: ed |
|
208 | In [1]: ed | |
200 | Editing... done. Executing edited code... |
|
209 | Editing... done. Executing edited code... | |
201 | Out[1]: 'def foo():n print "foo() was defined in an editing session"n' |
|
210 | Out[1]: 'def foo():n print "foo() was defined in an editing session"n' | |
202 |
|
211 | |||
203 | We can then call the function foo(): |
|
212 | We can then call the function foo(): | |
204 |
|
213 | |||
205 | In [2]: foo() |
|
214 | In [2]: foo() | |
206 | foo() was defined in an editing session |
|
215 | foo() was defined in an editing session | |
207 |
|
216 | |||
208 | Now we edit foo. IPython automatically loads the editor with the |
|
217 | Now we edit foo. IPython automatically loads the editor with the | |
209 | (temporary) file where foo() was previously defined: |
|
218 | (temporary) file where foo() was previously defined: | |
210 |
|
219 | |||
211 | In [3]: ed foo |
|
220 | In [3]: ed foo | |
212 | Editing... done. Executing edited code... |
|
221 | Editing... done. Executing edited code... | |
213 |
|
222 | |||
214 | And if we call foo() again we get the modified version: |
|
223 | And if we call foo() again we get the modified version: | |
215 |
|
224 | |||
216 | In [4]: foo() |
|
225 | In [4]: foo() | |
217 | foo() has now been changed! |
|
226 | foo() has now been changed! | |
218 |
|
227 | |||
219 | Here is an example of how to edit a code snippet successive |
|
228 | Here is an example of how to edit a code snippet successive | |
220 | times. First we call the editor: |
|
229 | times. First we call the editor: | |
221 |
|
230 | |||
222 | In [5]: ed |
|
231 | In [5]: ed | |
223 | Editing... done. Executing edited code... |
|
232 | Editing... done. Executing edited code... | |
224 | hello |
|
233 | hello | |
225 | Out[5]: "print 'hello'n" |
|
234 | Out[5]: "print 'hello'n" | |
226 |
|
235 | |||
227 | Now we call it again with the previous output (stored in _): |
|
236 | Now we call it again with the previous output (stored in _): | |
228 |
|
237 | |||
229 | In [6]: ed _ |
|
238 | In [6]: ed _ | |
230 | Editing... done. Executing edited code... |
|
239 | Editing... done. Executing edited code... | |
231 | hello world |
|
240 | hello world | |
232 | Out[6]: "print 'hello world'n" |
|
241 | Out[6]: "print 'hello world'n" | |
233 |
|
242 | |||
234 | Now we call it with the output #8 (stored in _8, also as Out[8]): |
|
243 | Now we call it with the output #8 (stored in _8, also as Out[8]): | |
235 |
|
244 | |||
236 | In [7]: ed _8 |
|
245 | In [7]: ed _8 | |
237 | Editing... done. Executing edited code... |
|
246 | Editing... done. Executing edited code... | |
238 | hello again |
|
247 | hello again | |
239 | Out[7]: "print 'hello again'n" |
|
248 | Out[7]: "print 'hello again'n" | |
240 |
|
249 | |||
241 |
|
250 | |||
242 | Changing the default editor hook: |
|
251 | Changing the default editor hook: | |
243 |
|
252 | |||
244 | If you wish to write your own editor hook, you can put it in a |
|
253 | If you wish to write your own editor hook, you can put it in a | |
245 | configuration file which you load at startup time. The default hook |
|
254 | configuration file which you load at startup time. The default hook | |
246 | is defined in the IPython.core.hooks module, and you can use that as a |
|
255 | is defined in the IPython.core.hooks module, and you can use that as a | |
247 | starting example for further modifications. That file also has |
|
256 | starting example for further modifications. That file also has | |
248 | general instructions on how to set a new hook for use once you've |
|
257 | general instructions on how to set a new hook for use once you've | |
249 | defined it.""" |
|
258 | defined it.""" | |
250 |
|
259 | |||
251 | # FIXME: This function has become a convoluted mess. It needs a |
|
260 | # FIXME: This function has become a convoluted mess. It needs a | |
252 | # ground-up rewrite with clean, simple logic. |
|
261 | # ground-up rewrite with clean, simple logic. | |
253 |
|
262 | |||
254 | def make_filename(arg): |
|
263 | def make_filename(arg): | |
255 | "Make a filename from the given args" |
|
264 | "Make a filename from the given args" | |
256 | try: |
|
265 | try: | |
257 | filename = get_py_filename(arg) |
|
266 | filename = get_py_filename(arg) | |
258 | except IOError: |
|
267 | except IOError: | |
259 | if args.endswith('.py'): |
|
268 | if args.endswith('.py'): | |
260 | filename = arg |
|
269 | filename = arg | |
261 | else: |
|
270 | else: | |
262 | filename = None |
|
271 | filename = None | |
263 | return filename |
|
272 | return filename | |
264 |
|
273 | |||
265 | # custom exceptions |
|
274 | # custom exceptions | |
266 | class DataIsObject(Exception): pass |
|
275 | class DataIsObject(Exception): pass | |
267 |
|
276 | |||
268 | opts,args = self.parse_options(parameter_s,'prn:') |
|
277 | opts,args = self.parse_options(parameter_s,'prn:') | |
269 | # Set a few locals from the options for convenience: |
|
278 | # Set a few locals from the options for convenience: | |
270 | opts_p = opts.has_key('p') |
|
279 | opts_p = opts.has_key('p') | |
271 | opts_r = opts.has_key('r') |
|
280 | opts_r = opts.has_key('r') | |
272 |
|
281 | |||
273 | # Default line number value |
|
282 | # Default line number value | |
274 | lineno = opts.get('n',None) |
|
283 | lineno = opts.get('n',None) | |
275 | if lineno is not None: |
|
284 | if lineno is not None: | |
276 | try: |
|
285 | try: | |
277 | lineno = int(lineno) |
|
286 | lineno = int(lineno) | |
278 | except: |
|
287 | except: | |
279 | warn("The -n argument must be an integer.") |
|
288 | warn("The -n argument must be an integer.") | |
280 | return |
|
289 | return | |
281 |
|
290 | |||
282 | if opts_p: |
|
291 | if opts_p: | |
283 | args = '_%s' % last_call[0] |
|
292 | args = '_%s' % last_call[0] | |
284 | if not self.shell.user_ns.has_key(args): |
|
293 | if not self.shell.user_ns.has_key(args): | |
285 | args = last_call[1] |
|
294 | args = last_call[1] | |
286 |
|
295 | |||
287 | # use last_call to remember the state of the previous call, but don't |
|
296 | # use last_call to remember the state of the previous call, but don't | |
288 | # let it be clobbered by successive '-p' calls. |
|
297 | # let it be clobbered by successive '-p' calls. | |
289 | try: |
|
298 | try: | |
290 | last_call[0] = self.shell.displayhook.prompt_count |
|
299 | last_call[0] = self.shell.displayhook.prompt_count | |
291 | if not opts_p: |
|
300 | if not opts_p: | |
292 | last_call[1] = parameter_s |
|
301 | last_call[1] = parameter_s | |
293 | except: |
|
302 | except: | |
294 | pass |
|
303 | pass | |
295 |
|
304 | |||
296 | # by default this is done with temp files, except when the given |
|
305 | # by default this is done with temp files, except when the given | |
297 | # arg is a filename |
|
306 | # arg is a filename | |
298 | use_temp = 1 |
|
307 | use_temp = 1 | |
299 |
|
308 | |||
300 | if re.match(r'\d',args): |
|
309 | if re.match(r'\d',args): | |
301 | # Mode where user specifies ranges of lines, like in %macro. |
|
310 | # Mode where user specifies ranges of lines, like in %macro. | |
302 | # This means that you can't edit files whose names begin with |
|
311 | # This means that you can't edit files whose names begin with | |
303 | # numbers this way. Tough. |
|
312 | # numbers this way. Tough. | |
304 | ranges = args.split() |
|
313 | ranges = args.split() | |
305 | data = ''.join(self.extract_input_slices(ranges,opts_r)) |
|
314 | data = ''.join(self.extract_input_slices(ranges,opts_r)) | |
306 | elif args.endswith('.py'): |
|
315 | elif args.endswith('.py'): | |
307 | filename = make_filename(args) |
|
316 | filename = make_filename(args) | |
308 | data = '' |
|
317 | data = '' | |
309 | use_temp = 0 |
|
318 | use_temp = 0 | |
310 | elif args: |
|
319 | elif args: | |
311 | try: |
|
320 | try: | |
312 | # Load the parameter given as a variable. If not a string, |
|
321 | # Load the parameter given as a variable. If not a string, | |
313 | # process it as an object instead (below) |
|
322 | # process it as an object instead (below) | |
314 |
|
323 | |||
315 | #print '*** args',args,'type',type(args) # dbg |
|
324 | #print '*** args',args,'type',type(args) # dbg | |
316 | data = eval(args,self.shell.user_ns) |
|
325 | data = eval(args,self.shell.user_ns) | |
317 | if not type(data) in StringTypes: |
|
326 | if not type(data) in StringTypes: | |
318 | raise DataIsObject |
|
327 | raise DataIsObject | |
319 |
|
328 | |||
320 | except (NameError,SyntaxError): |
|
329 | except (NameError,SyntaxError): | |
321 | # given argument is not a variable, try as a filename |
|
330 | # given argument is not a variable, try as a filename | |
322 | filename = make_filename(args) |
|
331 | filename = make_filename(args) | |
323 | if filename is None: |
|
332 | if filename is None: | |
324 | warn("Argument given (%s) can't be found as a variable " |
|
333 | warn("Argument given (%s) can't be found as a variable " | |
325 | "or as a filename." % args) |
|
334 | "or as a filename." % args) | |
326 | return |
|
335 | return | |
327 |
|
336 | |||
328 | data = '' |
|
337 | data = '' | |
329 | use_temp = 0 |
|
338 | use_temp = 0 | |
330 | except DataIsObject: |
|
339 | except DataIsObject: | |
331 |
|
340 | |||
332 | # macros have a special edit function |
|
341 | # macros have a special edit function | |
333 | if isinstance(data,Macro): |
|
342 | if isinstance(data,Macro): | |
334 | self._edit_macro(args,data) |
|
343 | self._edit_macro(args,data) | |
335 | return |
|
344 | return | |
336 |
|
345 | |||
337 | # For objects, try to edit the file where they are defined |
|
346 | # For objects, try to edit the file where they are defined | |
338 | try: |
|
347 | try: | |
339 | filename = inspect.getabsfile(data) |
|
348 | filename = inspect.getabsfile(data) | |
340 | if 'fakemodule' in filename.lower() and inspect.isclass(data): |
|
349 | if 'fakemodule' in filename.lower() and inspect.isclass(data): | |
341 | # class created by %edit? Try to find source |
|
350 | # class created by %edit? Try to find source | |
342 | # by looking for method definitions instead, the |
|
351 | # by looking for method definitions instead, the | |
343 | # __module__ in those classes is FakeModule. |
|
352 | # __module__ in those classes is FakeModule. | |
344 | attrs = [getattr(data, aname) for aname in dir(data)] |
|
353 | attrs = [getattr(data, aname) for aname in dir(data)] | |
345 | for attr in attrs: |
|
354 | for attr in attrs: | |
346 | if not inspect.ismethod(attr): |
|
355 | if not inspect.ismethod(attr): | |
347 | continue |
|
356 | continue | |
348 | filename = inspect.getabsfile(attr) |
|
357 | filename = inspect.getabsfile(attr) | |
349 | if filename and 'fakemodule' not in filename.lower(): |
|
358 | if filename and 'fakemodule' not in filename.lower(): | |
350 | # change the attribute to be the edit target instead |
|
359 | # change the attribute to be the edit target instead | |
351 | data = attr |
|
360 | data = attr | |
352 | break |
|
361 | break | |
353 |
|
362 | |||
354 | datafile = 1 |
|
363 | datafile = 1 | |
355 | except TypeError: |
|
364 | except TypeError: | |
356 | filename = make_filename(args) |
|
365 | filename = make_filename(args) | |
357 | datafile = 1 |
|
366 | datafile = 1 | |
358 | warn('Could not find file where `%s` is defined.\n' |
|
367 | warn('Could not find file where `%s` is defined.\n' | |
359 | 'Opening a file named `%s`' % (args,filename)) |
|
368 | 'Opening a file named `%s`' % (args,filename)) | |
360 | # Now, make sure we can actually read the source (if it was in |
|
369 | # Now, make sure we can actually read the source (if it was in | |
361 | # a temp file it's gone by now). |
|
370 | # a temp file it's gone by now). | |
362 | if datafile: |
|
371 | if datafile: | |
363 | try: |
|
372 | try: | |
364 | if lineno is None: |
|
373 | if lineno is None: | |
365 | lineno = inspect.getsourcelines(data)[1] |
|
374 | lineno = inspect.getsourcelines(data)[1] | |
366 | except IOError: |
|
375 | except IOError: | |
367 | filename = make_filename(args) |
|
376 | filename = make_filename(args) | |
368 | if filename is None: |
|
377 | if filename is None: | |
369 | warn('The file `%s` where `%s` was defined cannot ' |
|
378 | warn('The file `%s` where `%s` was defined cannot ' | |
370 | 'be read.' % (filename,data)) |
|
379 | 'be read.' % (filename,data)) | |
371 | return |
|
380 | return | |
372 | use_temp = 0 |
|
381 | use_temp = 0 | |
373 | else: |
|
382 | else: | |
374 | data = '' |
|
383 | data = '' | |
375 |
|
384 | |||
376 | if use_temp: |
|
385 | if use_temp: | |
377 | filename = self.shell.mktempfile(data) |
|
386 | filename = self.shell.mktempfile(data) | |
378 |
print |
|
387 | print('IPython will make a temporary file named:', filename) | |
379 |
|
388 | |||
380 | # Make sure we send to the client an absolute path, in case the working |
|
389 | # Make sure we send to the client an absolute path, in case the working | |
381 | # directory of client and kernel don't match |
|
390 | # directory of client and kernel don't match | |
382 | filename = os.path.abspath(filename) |
|
391 | filename = os.path.abspath(filename) | |
383 |
|
392 | |||
384 | payload = { |
|
393 | payload = { | |
385 | 'source' : 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic', |
|
394 | 'source' : 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic', | |
386 | 'filename' : filename, |
|
395 | 'filename' : filename, | |
387 | 'line_number' : lineno |
|
396 | 'line_number' : lineno | |
388 | } |
|
397 | } | |
389 | self.payload_manager.write_payload(payload) |
|
398 | self.payload_manager.write_payload(payload) | |
390 |
|
399 | |||
391 |
|
400 | |||
392 | def _showtraceback(self, etype, evalue, stb): |
|
401 | def _showtraceback(self, etype, evalue, stb): | |
393 |
|
402 | |||
394 | exc_content = { |
|
403 | exc_content = { | |
395 | u'status' : u'error', |
|
404 | u'status' : u'error', | |
396 | u'traceback' : stb, |
|
405 | u'traceback' : stb, | |
397 | u'ename' : unicode(etype.__name__), |
|
406 | u'ename' : unicode(etype.__name__), | |
398 | u'evalue' : unicode(evalue) |
|
407 | u'evalue' : unicode(evalue) | |
399 | } |
|
408 | } | |
400 |
|
409 | |||
401 | dh = self.displayhook |
|
410 | dh = self.displayhook | |
402 | exc_msg = dh.session.msg(u'pyerr', exc_content, dh.parent_header) |
|
411 | exc_msg = dh.session.msg(u'pyerr', exc_content, dh.parent_header) | |
403 | # Send exception info over pub socket for other clients than the caller |
|
412 | # Send exception info over pub socket for other clients than the caller | |
404 | # to pick up |
|
413 | # to pick up | |
405 | dh.pub_socket.send_json(exc_msg) |
|
414 | dh.pub_socket.send_json(exc_msg) | |
406 |
|
415 | |||
407 | # FIXME - Hack: store exception info in shell object. Right now, the |
|
416 | # FIXME - Hack: store exception info in shell object. Right now, the | |
408 | # caller is reading this info after the fact, we need to fix this logic |
|
417 | # caller is reading this info after the fact, we need to fix this logic | |
409 | # to remove this hack. |
|
418 | # to remove this hack. | |
410 | self._reply_content = exc_content |
|
419 | self._reply_content = exc_content | |
411 | # /FIXME |
|
420 | # /FIXME | |
412 |
|
421 | |||
413 | return exc_content |
|
422 | return exc_content | |
414 |
|
423 | |||
415 | def runlines(self, lines, clean=False): |
|
424 | def runlines(self, lines, clean=False): | |
416 | return InteractiveShell.runlines(self, lines, clean) |
|
425 | return InteractiveShell.runlines(self, lines, clean) | |
417 |
|
426 | |||
418 | InteractiveShellABC.register(ZMQInteractiveShell) |
|
427 | InteractiveShellABC.register(ZMQInteractiveShell) |
General Comments 0
You need to be logged in to leave comments.
Login now