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