##// END OF EJS Templates
Complete reorganization of InteractiveShell....
Brian Granger -
Show More
@@ -0,0 +1,14 b''
1 class InputList(list):
2 """Class to store user input.
3
4 It's basically a list, but slices return a string instead of a list, thus
5 allowing things like (assuming 'In' is an instance):
6
7 exec In[4:7]
8
9 or
10
11 exec In[5:9] + In[14] + In[21:25]"""
12
13 def __getslice__(self,i,j):
14 return ''.join(list.__getslice__(self,i,j))
This diff has been collapsed as it changes many lines, (541 lines changed) Show them Hide them
@@ -0,0 +1,541 b''
1 # -*- coding: utf-8 -*-
2 """Subclass of InteractiveShell for terminal based frontends."""
3
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2008-2010 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 import __builtin__
18 import bdb
19 from contextlib import nested
20 import os
21 import re
22 import sys
23
24 from IPython.core.error import TryNext
25 from IPython.core.usage import interactive_usage, default_banner
26 from IPython.core.inputlist import InputList
27 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
28 from IPython.lib.inputhook import enable_gui
29 from IPython.lib.pylabtools import pylab_activate
30 from IPython.utils.terminal import toggle_set_term_title, set_term_title
31 from IPython.utils.process import abbrev_cwd
32 from IPython.utils.warn import warn
33 from IPython.utils.text import num_ini_spaces
34 from IPython.utils.traitlets import Int, Str, CBool
35
36
37 #-----------------------------------------------------------------------------
38 # Utilities
39 #-----------------------------------------------------------------------------
40
41
42 def get_default_editor():
43 try:
44 ed = os.environ['EDITOR']
45 except KeyError:
46 if os.name == 'posix':
47 ed = 'vi' # the only one guaranteed to be there!
48 else:
49 ed = 'notepad' # same in Windows!
50 return ed
51
52
53 # store the builtin raw_input globally, and use this always, in case user code
54 # overwrites it (like wx.py.PyShell does)
55 raw_input_original = raw_input
56
57
58 class SeparateStr(Str):
59 """A Str subclass to validate separate_in, separate_out, etc.
60
61 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
62 """
63
64 def validate(self, obj, value):
65 if value == '0': value = ''
66 value = value.replace('\\n','\n')
67 return super(SeparateStr, self).validate(obj, value)
68
69
70 #-----------------------------------------------------------------------------
71 # Main class
72 #-----------------------------------------------------------------------------
73
74
75 class TerminalInteractiveShell(InteractiveShell):
76
77 autoedit_syntax = CBool(False, config=True)
78 autoindent = CBool(True, config=True)
79 banner = Str('')
80 banner1 = Str(default_banner, config=True)
81 banner2 = Str('', config=True)
82 confirm_exit = CBool(True, config=True)
83 # This display_banner only controls whether or not self.show_banner()
84 # is called when mainloop/interact are called. The default is False
85 # because for the terminal based application, the banner behavior
86 # is controlled by Global.display_banner, which IPythonApp looks at
87 # to determine if *it* should call show_banner() by hand or not.
88 display_banner = CBool(False) # This isn't configurable!
89 embedded = CBool(False)
90 embedded_active = CBool(False)
91 editor = Str(get_default_editor(), config=True)
92 exit_now = CBool(False)
93 pager = Str('less', config=True)
94
95 screen_length = Int(0, config=True)
96
97 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
98 separate_in = SeparateStr('\n', config=True)
99 separate_out = SeparateStr('', config=True)
100 separate_out2 = SeparateStr('', config=True)
101 term_title = CBool(False, config=True)
102
103 def __init__(self, config=None, ipython_dir=None, user_ns=None,
104 user_global_ns=None, custom_exceptions=((),None),
105 usage=None, banner1=None, banner2=None,
106 display_banner=None):
107
108 super(TerminalInteractiveShell, self).__init__(
109 config=config, ipython_dir=ipython_dir, user_ns=user_ns,
110 user_global_ns=user_global_ns, custom_exceptions=custom_exceptions
111 )
112 self.init_term_title()
113 self.init_usage(usage)
114 self.init_banner(banner1, banner2, display_banner)
115
116 #-------------------------------------------------------------------------
117 # Things related to the terminal
118 #-------------------------------------------------------------------------
119
120 @property
121 def usable_screen_length(self):
122 if self.screen_length == 0:
123 return 0
124 else:
125 num_lines_bot = self.separate_in.count('\n')+1
126 return self.screen_length - num_lines_bot
127
128 def init_term_title(self):
129 # Enable or disable the terminal title.
130 if self.term_title:
131 toggle_set_term_title(True)
132 set_term_title('IPython: ' + abbrev_cwd())
133 else:
134 toggle_set_term_title(False)
135
136 #-------------------------------------------------------------------------
137 # Things related to the banner and usage
138 #-------------------------------------------------------------------------
139
140 def _banner1_changed(self):
141 self.compute_banner()
142
143 def _banner2_changed(self):
144 self.compute_banner()
145
146 def _term_title_changed(self, name, new_value):
147 self.init_term_title()
148
149 def init_banner(self, banner1, banner2, display_banner):
150 if banner1 is not None:
151 self.banner1 = banner1
152 if banner2 is not None:
153 self.banner2 = banner2
154 if display_banner is not None:
155 self.display_banner = display_banner
156 self.compute_banner()
157
158 def show_banner(self, banner=None):
159 if banner is None:
160 banner = self.banner
161 self.write(banner)
162
163 def compute_banner(self):
164 self.banner = self.banner1 + '\n'
165 if self.profile:
166 self.banner += '\nIPython profile: %s\n' % self.profile
167 if self.banner2:
168 self.banner += '\n' + self.banner2 + '\n'
169
170 def init_usage(self, usage=None):
171 if usage is None:
172 self.usage = interactive_usage
173 else:
174 self.usage = usage
175
176 #-------------------------------------------------------------------------
177 # Mainloop and code execution logic
178 #-------------------------------------------------------------------------
179
180 def mainloop(self, display_banner=None):
181 """Start the mainloop.
182
183 If an optional banner argument is given, it will override the
184 internally created default banner.
185 """
186
187 with nested(self.builtin_trap, self.display_trap):
188
189 # if you run stuff with -c <cmd>, raw hist is not updated
190 # ensure that it's in sync
191 if len(self.input_hist) != len (self.input_hist_raw):
192 self.input_hist_raw = InputList(self.input_hist)
193
194 while 1:
195 try:
196 self.interact(display_banner=display_banner)
197 #self.interact_with_readline()
198 # XXX for testing of a readline-decoupled repl loop, call
199 # interact_with_readline above
200 break
201 except KeyboardInterrupt:
202 # this should not be necessary, but KeyboardInterrupt
203 # handling seems rather unpredictable...
204 self.write("\nKeyboardInterrupt in interact()\n")
205
206 def interact(self, display_banner=None):
207 """Closely emulate the interactive Python console."""
208
209 # batch run -> do not interact
210 if self.exit_now:
211 return
212
213 if display_banner is None:
214 display_banner = self.display_banner
215 if display_banner:
216 self.show_banner()
217
218 more = 0
219
220 # Mark activity in the builtins
221 __builtin__.__dict__['__IPYTHON__active'] += 1
222
223 if self.has_readline:
224 self.readline_startup_hook(self.pre_readline)
225 # exit_now is set by a call to %Exit or %Quit, through the
226 # ask_exit callback.
227
228 while not self.exit_now:
229 self.hooks.pre_prompt_hook()
230 if more:
231 try:
232 prompt = self.hooks.generate_prompt(True)
233 except:
234 self.showtraceback()
235 if self.autoindent:
236 self.rl_do_indent = True
237
238 else:
239 try:
240 prompt = self.hooks.generate_prompt(False)
241 except:
242 self.showtraceback()
243 try:
244 line = self.raw_input(prompt, more)
245 if self.exit_now:
246 # quick exit on sys.std[in|out] close
247 break
248 if self.autoindent:
249 self.rl_do_indent = False
250
251 except KeyboardInterrupt:
252 #double-guard against keyboardinterrupts during kbdint handling
253 try:
254 self.write('\nKeyboardInterrupt\n')
255 self.resetbuffer()
256 # keep cache in sync with the prompt counter:
257 self.outputcache.prompt_count -= 1
258
259 if self.autoindent:
260 self.indent_current_nsp = 0
261 more = 0
262 except KeyboardInterrupt:
263 pass
264 except EOFError:
265 if self.autoindent:
266 self.rl_do_indent = False
267 if self.has_readline:
268 self.readline_startup_hook(None)
269 self.write('\n')
270 self.exit()
271 except bdb.BdbQuit:
272 warn('The Python debugger has exited with a BdbQuit exception.\n'
273 'Because of how pdb handles the stack, it is impossible\n'
274 'for IPython to properly format this particular exception.\n'
275 'IPython will resume normal operation.')
276 except:
277 # exceptions here are VERY RARE, but they can be triggered
278 # asynchronously by signal handlers, for example.
279 self.showtraceback()
280 else:
281 more = self.push_line(line)
282 if (self.SyntaxTB.last_syntax_error and
283 self.autoedit_syntax):
284 self.edit_syntax_error()
285
286 # We are off again...
287 __builtin__.__dict__['__IPYTHON__active'] -= 1
288
289 # Turn off the exit flag, so the mainloop can be restarted if desired
290 self.exit_now = False
291
292 def raw_input(self,prompt='',continue_prompt=False):
293 """Write a prompt and read a line.
294
295 The returned line does not include the trailing newline.
296 When the user enters the EOF key sequence, EOFError is raised.
297
298 Optional inputs:
299
300 - prompt(''): a string to be printed to prompt the user.
301
302 - continue_prompt(False): whether this line is the first one or a
303 continuation in a sequence of inputs.
304 """
305 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
306
307 # Code run by the user may have modified the readline completer state.
308 # We must ensure that our completer is back in place.
309
310 if self.has_readline:
311 self.set_completer()
312
313 try:
314 line = raw_input_original(prompt).decode(self.stdin_encoding)
315 except ValueError:
316 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
317 " or sys.stdout.close()!\nExiting IPython!")
318 self.ask_exit()
319 return ""
320
321 # Try to be reasonably smart about not re-indenting pasted input more
322 # than necessary. We do this by trimming out the auto-indent initial
323 # spaces, if the user's actual input started itself with whitespace.
324 #debugx('self.buffer[-1]')
325
326 if self.autoindent:
327 if num_ini_spaces(line) > self.indent_current_nsp:
328 line = line[self.indent_current_nsp:]
329 self.indent_current_nsp = 0
330
331 # store the unfiltered input before the user has any chance to modify
332 # it.
333 if line.strip():
334 if continue_prompt:
335 self.input_hist_raw[-1] += '%s\n' % line
336 if self.has_readline and self.readline_use:
337 try:
338 histlen = self.readline.get_current_history_length()
339 if histlen > 1:
340 newhist = self.input_hist_raw[-1].rstrip()
341 self.readline.remove_history_item(histlen-1)
342 self.readline.replace_history_item(histlen-2,
343 newhist.encode(self.stdin_encoding))
344 except AttributeError:
345 pass # re{move,place}_history_item are new in 2.4.
346 else:
347 self.input_hist_raw.append('%s\n' % line)
348 # only entries starting at first column go to shadow history
349 if line.lstrip() == line:
350 self.shadowhist.add(line.strip())
351 elif not continue_prompt:
352 self.input_hist_raw.append('\n')
353 try:
354 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
355 except:
356 # blanket except, in case a user-defined prefilter crashes, so it
357 # can't take all of ipython with it.
358 self.showtraceback()
359 return ''
360 else:
361 return lineout
362
363 # TODO: The following three methods are an early attempt to refactor
364 # the main code execution logic. We don't use them, but they may be
365 # helpful when we refactor the code execution logic further.
366 # def interact_prompt(self):
367 # """ Print the prompt (in read-eval-print loop)
368 #
369 # Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
370 # used in standard IPython flow.
371 # """
372 # if self.more:
373 # try:
374 # prompt = self.hooks.generate_prompt(True)
375 # except:
376 # self.showtraceback()
377 # if self.autoindent:
378 # self.rl_do_indent = True
379 #
380 # else:
381 # try:
382 # prompt = self.hooks.generate_prompt(False)
383 # except:
384 # self.showtraceback()
385 # self.write(prompt)
386 #
387 # def interact_handle_input(self,line):
388 # """ Handle the input line (in read-eval-print loop)
389 #
390 # Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
391 # used in standard IPython flow.
392 # """
393 # if line.lstrip() == line:
394 # self.shadowhist.add(line.strip())
395 # lineout = self.prefilter_manager.prefilter_lines(line,self.more)
396 #
397 # if line.strip():
398 # if self.more:
399 # self.input_hist_raw[-1] += '%s\n' % line
400 # else:
401 # self.input_hist_raw.append('%s\n' % line)
402 #
403 #
404 # self.more = self.push_line(lineout)
405 # if (self.SyntaxTB.last_syntax_error and
406 # self.autoedit_syntax):
407 # self.edit_syntax_error()
408 #
409 # def interact_with_readline(self):
410 # """ Demo of using interact_handle_input, interact_prompt
411 #
412 # This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
413 # it should work like this.
414 # """
415 # self.readline_startup_hook(self.pre_readline)
416 # while not self.exit_now:
417 # self.interact_prompt()
418 # if self.more:
419 # self.rl_do_indent = True
420 # else:
421 # self.rl_do_indent = False
422 # line = raw_input_original().decode(self.stdin_encoding)
423 # self.interact_handle_input(line)
424
425 #-------------------------------------------------------------------------
426 # Methods to support auto-editing of SyntaxErrors.
427 #-------------------------------------------------------------------------
428
429 def edit_syntax_error(self):
430 """The bottom half of the syntax error handler called in the main loop.
431
432 Loop until syntax error is fixed or user cancels.
433 """
434
435 while self.SyntaxTB.last_syntax_error:
436 # copy and clear last_syntax_error
437 err = self.SyntaxTB.clear_err_state()
438 if not self._should_recompile(err):
439 return
440 try:
441 # may set last_syntax_error again if a SyntaxError is raised
442 self.safe_execfile(err.filename,self.user_ns)
443 except:
444 self.showtraceback()
445 else:
446 try:
447 f = file(err.filename)
448 try:
449 # This should be inside a display_trap block and I
450 # think it is.
451 sys.displayhook(f.read())
452 finally:
453 f.close()
454 except:
455 self.showtraceback()
456
457 def _should_recompile(self,e):
458 """Utility routine for edit_syntax_error"""
459
460 if e.filename in ('<ipython console>','<input>','<string>',
461 '<console>','<BackgroundJob compilation>',
462 None):
463
464 return False
465 try:
466 if (self.autoedit_syntax and
467 not self.ask_yes_no('Return to editor to correct syntax error? '
468 '[Y/n] ','y')):
469 return False
470 except EOFError:
471 return False
472
473 def int0(x):
474 try:
475 return int(x)
476 except TypeError:
477 return 0
478 # always pass integer line and offset values to editor hook
479 try:
480 self.hooks.fix_error_editor(e.filename,
481 int0(e.lineno),int0(e.offset),e.msg)
482 except TryNext:
483 warn('Could not open editor')
484 return False
485 return True
486
487 #-------------------------------------------------------------------------
488 # Things related to GUI support and pylab
489 #-------------------------------------------------------------------------
490
491 def enable_pylab(self, gui=None):
492 """Activate pylab support at runtime.
493
494 This turns on support for matplotlib, preloads into the interactive
495 namespace all of numpy and pylab, and configures IPython to correcdtly
496 interact with the GUI event loop. The GUI backend to be used can be
497 optionally selected with the optional :param:`gui` argument.
498
499 Parameters
500 ----------
501 gui : optional, string
502
503 If given, dictates the choice of matplotlib GUI backend to use
504 (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or
505 'gtk'), otherwise we use the default chosen by matplotlib (as
506 dictated by the matplotlib build-time options plus the user's
507 matplotlibrc configuration file).
508 """
509 # We want to prevent the loading of pylab to pollute the user's
510 # namespace as shown by the %who* magics, so we execute the activation
511 # code in an empty namespace, and we update *both* user_ns and
512 # user_ns_hidden with this information.
513 ns = {}
514 gui = pylab_activate(ns, gui)
515 self.user_ns.update(ns)
516 self.user_ns_hidden.update(ns)
517 # Now we must activate the gui pylab wants to use, and fix %run to take
518 # plot updates into account
519 enable_gui(gui)
520 self.magic_run = self._pylab_magic_run
521
522 #-------------------------------------------------------------------------
523 # Things related to exiting
524 #-------------------------------------------------------------------------
525
526 def ask_exit(self):
527 """ Ask the shell to exit. Can be overiden and used as a callback. """
528 self.exit_now = True
529
530 def exit(self):
531 """Handle interactive exit.
532
533 This method calls the ask_exit callback."""
534 if self.confirm_exit:
535 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
536 self.ask_exit()
537 else:
538 self.ask_exit()
539
540
541 InteractiveShellABC.register(TerminalInteractiveShell)
@@ -1,148 +1,148 b''
1 # Get the config being loaded so we can set attributes on it
1 # Get the config being loaded so we can set attributes on it
2 c = get_config()
2 c = get_config()
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Global options
5 # Global options
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7
7
8 # c.Global.display_banner = True
8 # c.Global.display_banner = True
9
9
10 # c.Global.classic = False
10 # c.Global.classic = False
11
11
12 # c.Global.nosep = True
12 # c.Global.nosep = True
13
13
14 # Set this to determine the detail of what is logged at startup.
14 # Set this to determine the detail of what is logged at startup.
15 # The default is 30 and possible values are 0,10,20,30,40,50.
15 # The default is 30 and possible values are 0,10,20,30,40,50.
16 # c.Global.log_level = 20
16 # c.Global.log_level = 20
17
17
18 # This should be a list of importable Python modules that have an
18 # This should be a list of importable Python modules that have an
19 # load_in_ipython(ip) method. This method gets called when the extension
19 # load_in_ipython(ip) method. This method gets called when the extension
20 # is loaded. You can put your extensions anywhere they can be imported
20 # is loaded. You can put your extensions anywhere they can be imported
21 # but we add the extensions subdir of the ipython directory to sys.path
21 # but we add the extensions subdir of the ipython directory to sys.path
22 # during extension loading, so you can put them there as well.
22 # during extension loading, so you can put them there as well.
23 # c.Global.extensions = [
23 # c.Global.extensions = [
24 # 'myextension'
24 # 'myextension'
25 # ]
25 # ]
26
26
27 # These lines are run in IPython in the user's namespace after extensions
27 # These lines are run in IPython in the user's namespace after extensions
28 # are loaded. They can contain full IPython syntax with magics etc.
28 # are loaded. They can contain full IPython syntax with magics etc.
29 # c.Global.exec_lines = [
29 # c.Global.exec_lines = [
30 # 'import numpy',
30 # 'import numpy',
31 # 'a = 10; b = 20',
31 # 'a = 10; b = 20',
32 # '1/0'
32 # '1/0'
33 # ]
33 # ]
34
34
35 # These files are run in IPython in the user's namespace. Files with a .py
35 # These files are run in IPython in the user's namespace. Files with a .py
36 # extension need to be pure Python. Files with a .ipy extension can have
36 # extension need to be pure Python. Files with a .ipy extension can have
37 # custom IPython syntax (like magics, etc.).
37 # custom IPython syntax (like magics, etc.).
38 # These files need to be in the cwd, the ipython_dir or be absolute paths.
38 # These files need to be in the cwd, the ipython_dir or be absolute paths.
39 # c.Global.exec_files = [
39 # c.Global.exec_files = [
40 # 'mycode.py',
40 # 'mycode.py',
41 # 'fancy.ipy'
41 # 'fancy.ipy'
42 # ]
42 # ]
43
43
44 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
45 # InteractiveShell options
45 # InteractiveShell options
46 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
47
47
48 # c.InteractiveShell.autocall = 1
48 # c.InteractiveShell.autocall = 1
49
49
50 # c.InteractiveShell.autoedit_syntax = False
50 # c.TerminalInteractiveShell.autoedit_syntax = False
51
51
52 # c.InteractiveShell.autoindent = True
52 # c.TerminalInteractiveShell.autoindent = True
53
53
54 # c.InteractiveShell.automagic = False
54 # c.InteractiveShell.automagic = False
55
55
56 # c.InteractiveShell.banner1 = 'This if for overriding the default IPython banner'
56 # c.TerminalTerminalInteractiveShell.banner1 = 'This if for overriding the default IPython banner'
57
57
58 # c.InteractiveShell.banner2 = "This is for extra banner text"
58 # c.TerminalTerminalInteractiveShell.banner2 = "This is for extra banner text"
59
59
60 # c.InteractiveShell.cache_size = 1000
60 # c.InteractiveShell.cache_size = 1000
61
61
62 # c.InteractiveShell.colors = 'LightBG'
62 # c.InteractiveShell.colors = 'LightBG'
63
63
64 # c.InteractiveShell.color_info = True
64 # c.InteractiveShell.color_info = True
65
65
66 # c.InteractiveShell.confirm_exit = True
66 # c.TerminalInteractiveShell.confirm_exit = True
67
67
68 # c.InteractiveShell.deep_reload = False
68 # c.InteractiveShell.deep_reload = False
69
69
70 # c.InteractiveShell.editor = 'nano'
70 # c.TerminalInteractiveShell.editor = 'nano'
71
71
72 # c.InteractiveShell.logstart = True
72 # c.InteractiveShell.logstart = True
73
73
74 # c.InteractiveShell.logfile = u'ipython_log.py'
74 # c.InteractiveShell.logfile = u'ipython_log.py'
75
75
76 # c.InteractiveShell.logappend = u'mylog.py'
76 # c.InteractiveShell.logappend = u'mylog.py'
77
77
78 # c.InteractiveShell.object_info_string_level = 0
78 # c.InteractiveShell.object_info_string_level = 0
79
79
80 # c.InteractiveShell.pager = 'less'
80 # c.TerminalInteractiveShell.pager = 'less'
81
81
82 # c.InteractiveShell.pdb = False
82 # c.InteractiveShell.pdb = False
83
83
84 # c.InteractiveShell.pprint = True
84 # c.InteractiveShell.pprint = True
85
85
86 # c.InteractiveShell.prompt_in1 = 'In [\#]: '
86 # c.InteractiveShell.prompt_in1 = 'In [\#]: '
87 # c.InteractiveShell.prompt_in2 = ' .\D.: '
87 # c.InteractiveShell.prompt_in2 = ' .\D.: '
88 # c.InteractiveShell.prompt_out = 'Out[\#]: '
88 # c.InteractiveShell.prompt_out = 'Out[\#]: '
89 # c.InteractiveShell.prompts_pad_left = True
89 # c.InteractiveShell.prompts_pad_left = True
90
90
91 # c.InteractiveShell.quiet = False
91 # c.InteractiveShell.quiet = False
92
92
93 # Readline
93 # Readline
94 # c.InteractiveShell.readline_use = True
94 # c.InteractiveShell.readline_use = True
95
95
96 # c.InteractiveShell.readline_parse_and_bind = [
96 # c.InteractiveShell.readline_parse_and_bind = [
97 # 'tab: complete',
97 # 'tab: complete',
98 # '"\C-l": possible-completions',
98 # '"\C-l": possible-completions',
99 # 'set show-all-if-ambiguous on',
99 # 'set show-all-if-ambiguous on',
100 # '"\C-o": tab-insert',
100 # '"\C-o": tab-insert',
101 # '"\M-i": " "',
101 # '"\M-i": " "',
102 # '"\M-o": "\d\d\d\d"',
102 # '"\M-o": "\d\d\d\d"',
103 # '"\M-I": "\d\d\d\d"',
103 # '"\M-I": "\d\d\d\d"',
104 # '"\C-r": reverse-search-history',
104 # '"\C-r": reverse-search-history',
105 # '"\C-s": forward-search-history',
105 # '"\C-s": forward-search-history',
106 # '"\C-p": history-search-backward',
106 # '"\C-p": history-search-backward',
107 # '"\C-n": history-search-forward',
107 # '"\C-n": history-search-forward',
108 # '"\e[A": history-search-backward',
108 # '"\e[A": history-search-backward',
109 # '"\e[B": history-search-forward',
109 # '"\e[B": history-search-forward',
110 # '"\C-k": kill-line',
110 # '"\C-k": kill-line',
111 # '"\C-u": unix-line-discard',
111 # '"\C-u": unix-line-discard',
112 # ]
112 # ]
113 # c.InteractiveShell.readline_remove_delims = '-/~'
113 # c.InteractiveShell.readline_remove_delims = '-/~'
114 # c.InteractiveShell.readline_merge_completions = True
114 # c.InteractiveShell.readline_merge_completions = True
115 # c.InteractiveShell.readline_omit__names = 0
115 # c.InteractiveShell.readline_omit__names = 0
116
116
117 # c.InteractiveShell.screen_length = 0
117 # c.TerminalInteractiveShell.screen_length = 0
118
118
119 # c.InteractiveShell.separate_in = '\n'
119 # c.TerminalInteractiveShell.separate_in = '\n'
120 # c.InteractiveShell.separate_out = ''
120 # c.TerminalInteractiveShell.separate_out = ''
121 # c.InteractiveShell.separate_out2 = ''
121 # c.TerminalInteractiveShell.separate_out2 = ''
122
122
123 # c.InteractiveShell.system_header = "IPython system call: "
123 # c.InteractiveShell.system_header = "IPython system call: "
124
124
125 # c.InteractiveShell.system_verbose = True
125 # c.InteractiveShell.system_verbose = True
126
126
127 # c.InteractiveShell.term_title = False
127 # c.TerminalInteractiveShell.term_title = False
128
128
129 # c.InteractiveShell.wildcards_case_sensitive = True
129 # c.InteractiveShell.wildcards_case_sensitive = True
130
130
131 # c.InteractiveShell.xmode = 'Context'
131 # c.InteractiveShell.xmode = 'Context'
132
132
133 #-----------------------------------------------------------------------------
133 #-----------------------------------------------------------------------------
134 # PrefilterManager options
134 # PrefilterManager options
135 #-----------------------------------------------------------------------------
135 #-----------------------------------------------------------------------------
136
136
137 # c.PrefilterManager.multi_line_specials = True
137 # c.PrefilterManager.multi_line_specials = True
138
138
139 #-----------------------------------------------------------------------------
139 #-----------------------------------------------------------------------------
140 # AliasManager options
140 # AliasManager options
141 #-----------------------------------------------------------------------------
141 #-----------------------------------------------------------------------------
142
142
143 # Do this to disable all defaults
143 # Do this to disable all defaults
144 # c.AliasManager.default_aliases = []
144 # c.AliasManager.default_aliases = []
145
145
146 # c.AliasManager.user_aliases = [
146 # c.AliasManager.user_aliases = [
147 # ('foo', 'echo Hi')
147 # ('foo', 'echo Hi')
148 # ]
148 # ]
@@ -1,29 +1,29 b''
1 c = get_config()
1 c = get_config()
2
2
3 # This can be used at any point in a config file to load a sub config
3 # This can be used at any point in a config file to load a sub config
4 # and merge it into the current one.
4 # and merge it into the current one.
5 load_subconfig('ipython_config.py')
5 load_subconfig('ipython_config.py')
6
6
7 c.InteractiveShell.prompt_in1 = '\C_LightGreen\u@\h\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> '
7 c.InteractiveShell.prompt_in1 = '\C_LightGreen\u@\h\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> '
8 c.InteractiveShell.prompt_in2 = '\C_Green|\C_LightGreen\D\C_Green> '
8 c.InteractiveShell.prompt_in2 = '\C_Green|\C_LightGreen\D\C_Green> '
9 c.InteractiveShell.prompt_out = '<\#> '
9 c.InteractiveShell.prompt_out = '<\#> '
10
10
11 c.InteractiveShell.prompts_pad_left = True
11 c.InteractiveShell.prompts_pad_left = True
12
12
13 c.InteractiveShell.separate_in = ''
13 c.TerminalInteractiveShell.separate_in = ''
14 c.InteractiveShell.separate_out = ''
14 c.TerminalInteractiveShell.separate_out = ''
15 c.InteractiveShell.separate_out2 = ''
15 c.TerminalInteractiveShell.separate_out2 = ''
16
16
17 c.PrefilterManager.multi_line_specials = True
17 c.PrefilterManager.multi_line_specials = True
18
18
19 lines = """
19 lines = """
20 %rehashx
20 %rehashx
21 """
21 """
22
22
23 # You have to make sure that attributes that are containers already
23 # You have to make sure that attributes that are containers already
24 # exist before using them. Simple assigning a new list will override
24 # exist before using them. Simple assigning a new list will override
25 # all previous values.
25 # all previous values.
26 if hasattr(c.Global, 'exec_lines'):
26 if hasattr(c.Global, 'exec_lines'):
27 c.Global.exec_lines.append(lines)
27 c.Global.exec_lines.append(lines)
28 else:
28 else:
29 c.Global.exec_lines = [lines] No newline at end of file
29 c.Global.exec_lines = [lines]
This diff has been collapsed as it changes many lines, (637 lines changed) Show them Hide them
@@ -1,2523 +1,2024 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Main IPython class."""
2 """Main IPython class."""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2008-2010 The IPython Development Team
7 # Copyright (C) 2008-2010 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from __future__ import with_statement
17 from __future__ import with_statement
18 from __future__ import absolute_import
18 from __future__ import absolute_import
19
19
20 import __builtin__
20 import __builtin__
21 import abc
21 import abc
22 import bdb
23 import codeop
22 import codeop
24 import exceptions
23 import exceptions
25 import new
24 import new
26 import os
25 import os
27 import re
26 import re
28 import string
27 import string
29 import sys
28 import sys
30 import tempfile
29 import tempfile
31 from contextlib import nested
30 from contextlib import nested
32
31
33 from IPython.core import debugger, oinspect
32 from IPython.core import debugger, oinspect
34 from IPython.core import history as ipcorehist
33 from IPython.core import history as ipcorehist
35 from IPython.core import prefilter
34 from IPython.core import prefilter
36 from IPython.core import shadowns
35 from IPython.core import shadowns
37 from IPython.core import ultratb
36 from IPython.core import ultratb
38 from IPython.core.alias import AliasManager
37 from IPython.core.alias import AliasManager
39 from IPython.core.builtin_trap import BuiltinTrap
38 from IPython.core.builtin_trap import BuiltinTrap
40 from IPython.config.configurable import Configurable
39 from IPython.config.configurable import Configurable
41 from IPython.core.display_trap import DisplayTrap
40 from IPython.core.display_trap import DisplayTrap
42 from IPython.core.error import TryNext, UsageError
41 from IPython.core.error import UsageError
43 from IPython.core.extensions import ExtensionManager
42 from IPython.core.extensions import ExtensionManager
44 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
43 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
44 from IPython.core.inputlist import InputList
45 from IPython.core.logger import Logger
45 from IPython.core.logger import Logger
46 from IPython.core.magic import Magic
46 from IPython.core.magic import Magic
47 from IPython.core.plugin import PluginManager
47 from IPython.core.plugin import PluginManager
48 from IPython.core.prefilter import PrefilterManager
48 from IPython.core.prefilter import PrefilterManager
49 from IPython.core.prompts import CachedOutput
49 from IPython.core.prompts import CachedOutput
50 from IPython.core.usage import interactive_usage, default_banner
51 import IPython.core.hooks
50 import IPython.core.hooks
52 from IPython.external.Itpl import ItplNS
51 from IPython.external.Itpl import ItplNS
53 from IPython.lib.inputhook import enable_gui
54 from IPython.lib.backgroundjobs import BackgroundJobManager
55 from IPython.lib.pylabtools import pylab_activate
56 from IPython.utils import PyColorize
52 from IPython.utils import PyColorize
57 from IPython.utils import pickleshare
53 from IPython.utils import pickleshare
58 from IPython.utils.doctestreload import doctest_reload
54 from IPython.utils.doctestreload import doctest_reload
59 from IPython.utils.ipstruct import Struct
55 from IPython.utils.ipstruct import Struct
60 from IPython.utils.io import Term, ask_yes_no
56 from IPython.utils.io import Term, ask_yes_no
61 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
57 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
62 from IPython.utils.process import (
58 from IPython.utils.process import getoutput, getoutputerror
63 abbrev_cwd,
64 getoutput,
65 getoutputerror
66 )
67 # import IPython.utils.rlineimpl as readline
68 from IPython.utils.strdispatch import StrDispatch
59 from IPython.utils.strdispatch import StrDispatch
69 from IPython.utils.syspathcontext import prepended_to_syspath
60 from IPython.utils.syspathcontext import prepended_to_syspath
70 from IPython.utils.terminal import toggle_set_term_title, set_term_title
61 from IPython.utils.text import num_ini_spaces
71 from IPython.utils.warn import warn, error, fatal
62 from IPython.utils.warn import warn, error, fatal
72 from IPython.utils.traitlets import (
63 from IPython.utils.traitlets import (
73 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode, Instance
64 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode, Instance
74 )
65 )
75
66
76 # from IPython.utils import growl
67 # from IPython.utils import growl
77 # growl.start("IPython")
68 # growl.start("IPython")
78
69
79 #-----------------------------------------------------------------------------
70 #-----------------------------------------------------------------------------
80 # Globals
71 # Globals
81 #-----------------------------------------------------------------------------
72 #-----------------------------------------------------------------------------
82
73
83 # store the builtin raw_input globally, and use this always, in case user code
84 # overwrites it (like wx.py.PyShell does)
85 raw_input_original = raw_input
86
87 # compiled regexps for autoindent management
74 # compiled regexps for autoindent management
88 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
75 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
89
76
90 #-----------------------------------------------------------------------------
77 #-----------------------------------------------------------------------------
91 # Utilities
78 # Utilities
92 #-----------------------------------------------------------------------------
79 #-----------------------------------------------------------------------------
93
80
94 ini_spaces_re = re.compile(r'^(\s+)')
81 # store the builtin raw_input globally, and use this always, in case user code
95
82 # overwrites it (like wx.py.PyShell does)
96
83 raw_input_original = raw_input
97 def num_ini_spaces(strng):
98 """Return the number of initial spaces in a string"""
99
100 ini_spaces = ini_spaces_re.match(strng)
101 if ini_spaces:
102 return ini_spaces.end()
103 else:
104 return 0
105
106
84
107 def softspace(file, newvalue):
85 def softspace(file, newvalue):
108 """Copied from code.py, to remove the dependency"""
86 """Copied from code.py, to remove the dependency"""
109
87
110 oldvalue = 0
88 oldvalue = 0
111 try:
89 try:
112 oldvalue = file.softspace
90 oldvalue = file.softspace
113 except AttributeError:
91 except AttributeError:
114 pass
92 pass
115 try:
93 try:
116 file.softspace = newvalue
94 file.softspace = newvalue
117 except (AttributeError, TypeError):
95 except (AttributeError, TypeError):
118 # "attribute-less object" or "read-only attributes"
96 # "attribute-less object" or "read-only attributes"
119 pass
97 pass
120 return oldvalue
98 return oldvalue
121
99
122
100
123 def no_op(*a, **kw): pass
101 def no_op(*a, **kw): pass
124
102
125 class SpaceInInput(exceptions.Exception): pass
103 class SpaceInInput(exceptions.Exception): pass
126
104
127 class Bunch: pass
105 class Bunch: pass
128
106
129 class InputList(list):
130 """Class to store user input.
131
132 It's basically a list, but slices return a string instead of a list, thus
133 allowing things like (assuming 'In' is an instance):
134
135 exec In[4:7]
136
137 or
138
139 exec In[5:9] + In[14] + In[21:25]"""
140
141 def __getslice__(self,i,j):
142 return ''.join(list.__getslice__(self,i,j))
143
144
145 class SyntaxTB(ultratb.ListTB):
107 class SyntaxTB(ultratb.ListTB):
146 """Extension which holds some state: the last exception value"""
108 """Extension which holds some state: the last exception value"""
147
109
148 def __init__(self,color_scheme = 'NoColor'):
110 def __init__(self,color_scheme = 'NoColor'):
149 ultratb.ListTB.__init__(self,color_scheme)
111 ultratb.ListTB.__init__(self,color_scheme)
150 self.last_syntax_error = None
112 self.last_syntax_error = None
151
113
152 def __call__(self, etype, value, elist):
114 def __call__(self, etype, value, elist):
153 self.last_syntax_error = value
115 self.last_syntax_error = value
154 ultratb.ListTB.__call__(self,etype,value,elist)
116 ultratb.ListTB.__call__(self,etype,value,elist)
155
117
156 def clear_err_state(self):
118 def clear_err_state(self):
157 """Return the current error state and clear it"""
119 """Return the current error state and clear it"""
158 e = self.last_syntax_error
120 e = self.last_syntax_error
159 self.last_syntax_error = None
121 self.last_syntax_error = None
160 return e
122 return e
161
123
162
124
163 def get_default_editor():
164 try:
165 ed = os.environ['EDITOR']
166 except KeyError:
167 if os.name == 'posix':
168 ed = 'vi' # the only one guaranteed to be there!
169 else:
170 ed = 'notepad' # same in Windows!
171 return ed
172
173
174 def get_default_colors():
125 def get_default_colors():
175 if sys.platform=='darwin':
126 if sys.platform=='darwin':
176 return "LightBG"
127 return "LightBG"
177 elif os.name=='nt':
128 elif os.name=='nt':
178 return 'Linux'
129 return 'Linux'
179 else:
130 else:
180 return 'Linux'
131 return 'Linux'
181
132
182
133
183 class SeparateStr(Str):
184 """A Str subclass to validate separate_in, separate_out, etc.
185
186 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
187 """
188
189 def validate(self, obj, value):
190 if value == '0': value = ''
191 value = value.replace('\\n','\n')
192 return super(SeparateStr, self).validate(obj, value)
193
194
195 #-----------------------------------------------------------------------------
134 #-----------------------------------------------------------------------------
196 # Main IPython class
135 # Main IPython class
197 #-----------------------------------------------------------------------------
136 #-----------------------------------------------------------------------------
198
137
199
138
200 class InteractiveShell(Configurable, Magic):
139 class InteractiveShell(Configurable, Magic):
201 """An enhanced, interactive shell for Python."""
140 """An enhanced, interactive shell for Python."""
202
141
203 autocall = Enum((0,1,2), default_value=1, config=True)
142 autocall = Enum((0,1,2), default_value=1, config=True)
204 autoedit_syntax = CBool(False, config=True)
205 autoindent = CBool(True, config=True)
206 automagic = CBool(True, config=True)
143 automagic = CBool(True, config=True)
207 banner = Str('')
208 banner1 = Str(default_banner, config=True)
209 banner2 = Str('', config=True)
210 cache_size = Int(1000, config=True)
144 cache_size = Int(1000, config=True)
211 color_info = CBool(True, config=True)
145 color_info = CBool(True, config=True)
212 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
146 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
213 default_value=get_default_colors(), config=True)
147 default_value=get_default_colors(), config=True)
214 confirm_exit = CBool(True, config=True)
215 debug = CBool(False, config=True)
148 debug = CBool(False, config=True)
216 deep_reload = CBool(False, config=True)
149 deep_reload = CBool(False, config=True)
217 # This display_banner only controls whether or not self.show_banner()
218 # is called when mainloop/interact are called. The default is False
219 # because for the terminal based application, the banner behavior
220 # is controlled by Global.display_banner, which IPythonApp looks at
221 # to determine if *it* should call show_banner() by hand or not.
222 display_banner = CBool(False) # This isn't configurable!
223 embedded = CBool(False)
224 embedded_active = CBool(False)
225 editor = Str(get_default_editor(), config=True)
226 filename = Str("<ipython console>")
150 filename = Str("<ipython console>")
227 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
151 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
228 logstart = CBool(False, config=True)
152 logstart = CBool(False, config=True)
229 logfile = Str('', config=True)
153 logfile = Str('', config=True)
230 logappend = Str('', config=True)
154 logappend = Str('', config=True)
231 object_info_string_level = Enum((0,1,2), default_value=0,
155 object_info_string_level = Enum((0,1,2), default_value=0,
232 config=True)
156 config=True)
233 pager = Str('less', config=True)
234 pdb = CBool(False, config=True)
157 pdb = CBool(False, config=True)
235 pprint = CBool(True, config=True)
158 pprint = CBool(True, config=True)
236 profile = Str('', config=True)
159 profile = Str('', config=True)
237 prompt_in1 = Str('In [\\#]: ', config=True)
160 prompt_in1 = Str('In [\\#]: ', config=True)
238 prompt_in2 = Str(' .\\D.: ', config=True)
161 prompt_in2 = Str(' .\\D.: ', config=True)
239 prompt_out = Str('Out[\\#]: ', config=True)
162 prompt_out = Str('Out[\\#]: ', config=True)
240 prompts_pad_left = CBool(True, config=True)
163 prompts_pad_left = CBool(True, config=True)
241 quiet = CBool(False, config=True)
164 quiet = CBool(False, config=True)
242
165
166 # The readline stuff will eventually be moved to the terminal subclass
167 # but for now, we can't do that as readline is welded in everywhere.
243 readline_use = CBool(True, config=True)
168 readline_use = CBool(True, config=True)
244 readline_merge_completions = CBool(True, config=True)
169 readline_merge_completions = CBool(True, config=True)
245 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
170 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
246 readline_remove_delims = Str('-/~', config=True)
171 readline_remove_delims = Str('-/~', config=True)
247 readline_parse_and_bind = List([
172 readline_parse_and_bind = List([
248 'tab: complete',
173 'tab: complete',
249 '"\C-l": clear-screen',
174 '"\C-l": clear-screen',
250 'set show-all-if-ambiguous on',
175 'set show-all-if-ambiguous on',
251 '"\C-o": tab-insert',
176 '"\C-o": tab-insert',
252 '"\M-i": " "',
177 '"\M-i": " "',
253 '"\M-o": "\d\d\d\d"',
178 '"\M-o": "\d\d\d\d"',
254 '"\M-I": "\d\d\d\d"',
179 '"\M-I": "\d\d\d\d"',
255 '"\C-r": reverse-search-history',
180 '"\C-r": reverse-search-history',
256 '"\C-s": forward-search-history',
181 '"\C-s": forward-search-history',
257 '"\C-p": history-search-backward',
182 '"\C-p": history-search-backward',
258 '"\C-n": history-search-forward',
183 '"\C-n": history-search-forward',
259 '"\e[A": history-search-backward',
184 '"\e[A": history-search-backward',
260 '"\e[B": history-search-forward',
185 '"\e[B": history-search-forward',
261 '"\C-k": kill-line',
186 '"\C-k": kill-line',
262 '"\C-u": unix-line-discard',
187 '"\C-u": unix-line-discard',
263 ], allow_none=False, config=True)
188 ], allow_none=False, config=True)
264
189
265 screen_length = Int(0, config=True)
266
267 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
268 separate_in = SeparateStr('\n', config=True)
269 separate_out = SeparateStr('', config=True)
270 separate_out2 = SeparateStr('', config=True)
271
272 system_header = Str('IPython system call: ', config=True)
190 system_header = Str('IPython system call: ', config=True)
273 system_verbose = CBool(False, config=True)
191 system_verbose = CBool(False, config=True)
274 term_title = CBool(False, config=True)
275 wildcards_case_sensitive = CBool(True, config=True)
192 wildcards_case_sensitive = CBool(True, config=True)
276 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
193 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
277 default_value='Context', config=True)
194 default_value='Context', config=True)
278
195
279 autoexec = List(allow_none=False)
280
281 # class attribute to indicate whether the class supports threads or not.
282 # Subclasses with thread support should override this as needed.
283 isthreaded = False
284
285 # Subcomponents of InteractiveShell
196 # Subcomponents of InteractiveShell
286 alias_manager = Instance('IPython.core.alias.AliasManager')
197 alias_manager = Instance('IPython.core.alias.AliasManager')
287 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
198 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
288 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
199 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
289 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
200 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
290 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
201 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
291 plugin_manager = Instance('IPython.core.plugin.PluginManager')
202 plugin_manager = Instance('IPython.core.plugin.PluginManager')
292
203
293 def __init__(self, config=None, ipython_dir=None, usage=None,
204 def __init__(self, config=None, ipython_dir=None,
294 user_ns=None, user_global_ns=None,
205 user_ns=None, user_global_ns=None,
295 banner1=None, banner2=None, display_banner=None,
296 custom_exceptions=((),None)):
206 custom_exceptions=((),None)):
297
207
298 # This is where traits with a config_key argument are updated
208 # This is where traits with a config_key argument are updated
299 # from the values on config.
209 # from the values on config.
300 super(InteractiveShell, self).__init__(config=config)
210 super(InteractiveShell, self).__init__(config=config)
301
211
302 # These are relatively independent and stateless
212 # These are relatively independent and stateless
303 self.init_ipython_dir(ipython_dir)
213 self.init_ipython_dir(ipython_dir)
304 self.init_instance_attrs()
214 self.init_instance_attrs()
305 self.init_term_title()
306 self.init_usage(usage)
307 self.init_banner(banner1, banner2, display_banner)
308
215
309 # Create namespaces (user_ns, user_global_ns, etc.)
216 # Create namespaces (user_ns, user_global_ns, etc.)
310 self.init_create_namespaces(user_ns, user_global_ns)
217 self.init_create_namespaces(user_ns, user_global_ns)
311 # This has to be done after init_create_namespaces because it uses
218 # This has to be done after init_create_namespaces because it uses
312 # something in self.user_ns, but before init_sys_modules, which
219 # something in self.user_ns, but before init_sys_modules, which
313 # is the first thing to modify sys.
220 # is the first thing to modify sys.
314 self.save_sys_module_state()
221 self.save_sys_module_state()
315 self.init_sys_modules()
222 self.init_sys_modules()
316
223
317 self.init_history()
224 self.init_history()
318 self.init_encoding()
225 self.init_encoding()
319 self.init_prefilter()
226 self.init_prefilter()
320
227
321 Magic.__init__(self, self)
228 Magic.__init__(self, self)
322
229
323 self.init_syntax_highlighting()
230 self.init_syntax_highlighting()
324 self.init_hooks()
231 self.init_hooks()
325 self.init_pushd_popd_magic()
232 self.init_pushd_popd_magic()
326 self.init_traceback_handlers(custom_exceptions)
233 self.init_traceback_handlers(custom_exceptions)
327 self.init_user_ns()
234 self.init_user_ns()
328 self.init_logger()
235 self.init_logger()
329 self.init_alias()
236 self.init_alias()
330 self.init_builtins()
237 self.init_builtins()
331
238
332 # pre_config_initialization
239 # pre_config_initialization
333 self.init_shadow_hist()
240 self.init_shadow_hist()
334
241
335 # The next section should contain averything that was in ipmaker.
242 # The next section should contain averything that was in ipmaker.
336 self.init_logstart()
243 self.init_logstart()
337
244
338 # The following was in post_config_initialization
245 # The following was in post_config_initialization
339 self.init_inspector()
246 self.init_inspector()
340 self.init_readline()
247 self.init_readline()
341 self.init_prompts()
248 self.init_prompts()
342 self.init_displayhook()
249 self.init_displayhook()
343 self.init_reload_doctest()
250 self.init_reload_doctest()
344 self.init_magics()
251 self.init_magics()
345 self.init_pdb()
252 self.init_pdb()
346 self.init_extension_manager()
253 self.init_extension_manager()
347 self.init_plugin_manager()
254 self.init_plugin_manager()
348 self.hooks.late_startup_hook()
255 self.hooks.late_startup_hook()
349
256
350 @classmethod
257 @classmethod
351 def instance(cls, *args, **kwargs):
258 def instance(cls, *args, **kwargs):
352 """Returns a global InteractiveShell instance."""
259 """Returns a global InteractiveShell instance."""
353 if not hasattr(cls, "_instance"):
260 if not hasattr(cls, "_instance"):
354 cls._instance = cls(*args, **kwargs)
261 cls._instance = cls(*args, **kwargs)
355 return cls._instance
262 return cls._instance
356
263
357 @classmethod
264 @classmethod
358 def initialized(cls):
265 def initialized(cls):
359 return hasattr(cls, "_instance")
266 return hasattr(cls, "_instance")
360
267
361 def get_ipython(self):
268 def get_ipython(self):
362 """Return the currently running IPython instance."""
269 """Return the currently running IPython instance."""
363 return self
270 return self
364
271
365 #-------------------------------------------------------------------------
272 #-------------------------------------------------------------------------
366 # Trait changed handlers
273 # Trait changed handlers
367 #-------------------------------------------------------------------------
274 #-------------------------------------------------------------------------
368
275
369 def _banner1_changed(self):
370 self.compute_banner()
371
372 def _banner2_changed(self):
373 self.compute_banner()
374
375 def _ipython_dir_changed(self, name, new):
276 def _ipython_dir_changed(self, name, new):
376 if not os.path.isdir(new):
277 if not os.path.isdir(new):
377 os.makedirs(new, mode = 0777)
278 os.makedirs(new, mode = 0777)
378
279
379 @property
380 def usable_screen_length(self):
381 if self.screen_length == 0:
382 return 0
383 else:
384 num_lines_bot = self.separate_in.count('\n')+1
385 return self.screen_length - num_lines_bot
386
387 def _term_title_changed(self, name, new_value):
388 self.init_term_title()
389
390 def set_autoindent(self,value=None):
280 def set_autoindent(self,value=None):
391 """Set the autoindent flag, checking for readline support.
281 """Set the autoindent flag, checking for readline support.
392
282
393 If called with no arguments, it acts as a toggle."""
283 If called with no arguments, it acts as a toggle."""
394
284
395 if not self.has_readline:
285 if not self.has_readline:
396 if os.name == 'posix':
286 if os.name == 'posix':
397 warn("The auto-indent feature requires the readline library")
287 warn("The auto-indent feature requires the readline library")
398 self.autoindent = 0
288 self.autoindent = 0
399 return
289 return
400 if value is None:
290 if value is None:
401 self.autoindent = not self.autoindent
291 self.autoindent = not self.autoindent
402 else:
292 else:
403 self.autoindent = value
293 self.autoindent = value
404
294
405 #-------------------------------------------------------------------------
295 #-------------------------------------------------------------------------
406 # init_* methods called by __init__
296 # init_* methods called by __init__
407 #-------------------------------------------------------------------------
297 #-------------------------------------------------------------------------
408
298
409 def init_ipython_dir(self, ipython_dir):
299 def init_ipython_dir(self, ipython_dir):
410 if ipython_dir is not None:
300 if ipython_dir is not None:
411 self.ipython_dir = ipython_dir
301 self.ipython_dir = ipython_dir
412 self.config.Global.ipython_dir = self.ipython_dir
302 self.config.Global.ipython_dir = self.ipython_dir
413 return
303 return
414
304
415 if hasattr(self.config.Global, 'ipython_dir'):
305 if hasattr(self.config.Global, 'ipython_dir'):
416 self.ipython_dir = self.config.Global.ipython_dir
306 self.ipython_dir = self.config.Global.ipython_dir
417 else:
307 else:
418 self.ipython_dir = get_ipython_dir()
308 self.ipython_dir = get_ipython_dir()
419
309
420 # All children can just read this
310 # All children can just read this
421 self.config.Global.ipython_dir = self.ipython_dir
311 self.config.Global.ipython_dir = self.ipython_dir
422
312
423 def init_instance_attrs(self):
313 def init_instance_attrs(self):
424 self.jobs = BackgroundJobManager()
425 self.more = False
314 self.more = False
426
315
427 # command compiler
316 # command compiler
428 self.compile = codeop.CommandCompiler()
317 self.compile = codeop.CommandCompiler()
429
318
430 # User input buffer
319 # User input buffer
431 self.buffer = []
320 self.buffer = []
432
321
433 # Make an empty namespace, which extension writers can rely on both
322 # Make an empty namespace, which extension writers can rely on both
434 # existing and NEVER being used by ipython itself. This gives them a
323 # existing and NEVER being used by ipython itself. This gives them a
435 # convenient location for storing additional information and state
324 # convenient location for storing additional information and state
436 # their extensions may require, without fear of collisions with other
325 # their extensions may require, without fear of collisions with other
437 # ipython names that may develop later.
326 # ipython names that may develop later.
438 self.meta = Struct()
327 self.meta = Struct()
439
328
440 # Object variable to store code object waiting execution. This is
329 # Object variable to store code object waiting execution. This is
441 # used mainly by the multithreaded shells, but it can come in handy in
330 # used mainly by the multithreaded shells, but it can come in handy in
442 # other situations. No need to use a Queue here, since it's a single
331 # other situations. No need to use a Queue here, since it's a single
443 # item which gets cleared once run.
332 # item which gets cleared once run.
444 self.code_to_run = None
333 self.code_to_run = None
445
334
446 # Flag to mark unconditional exit
447 self.exit_now = False
448
449 # Temporary files used for various purposes. Deleted at exit.
335 # Temporary files used for various purposes. Deleted at exit.
450 self.tempfiles = []
336 self.tempfiles = []
451
337
452 # Keep track of readline usage (later set by init_readline)
338 # Keep track of readline usage (later set by init_readline)
453 self.has_readline = False
339 self.has_readline = False
454
340
455 # keep track of where we started running (mainly for crash post-mortem)
341 # keep track of where we started running (mainly for crash post-mortem)
456 # This is not being used anywhere currently.
342 # This is not being used anywhere currently.
457 self.starting_dir = os.getcwd()
343 self.starting_dir = os.getcwd()
458
344
459 # Indentation management
345 # Indentation management
460 self.indent_current_nsp = 0
346 self.indent_current_nsp = 0
461
347
462 def init_term_title(self):
463 # Enable or disable the terminal title.
464 if self.term_title:
465 toggle_set_term_title(True)
466 set_term_title('IPython: ' + abbrev_cwd())
467 else:
468 toggle_set_term_title(False)
469
470 def init_usage(self, usage=None):
471 if usage is None:
472 self.usage = interactive_usage
473 else:
474 self.usage = usage
475
476 def init_encoding(self):
348 def init_encoding(self):
477 # Get system encoding at startup time. Certain terminals (like Emacs
349 # Get system encoding at startup time. Certain terminals (like Emacs
478 # under Win32 have it set to None, and we need to have a known valid
350 # under Win32 have it set to None, and we need to have a known valid
479 # encoding to use in the raw_input() method
351 # encoding to use in the raw_input() method
480 try:
352 try:
481 self.stdin_encoding = sys.stdin.encoding or 'ascii'
353 self.stdin_encoding = sys.stdin.encoding or 'ascii'
482 except AttributeError:
354 except AttributeError:
483 self.stdin_encoding = 'ascii'
355 self.stdin_encoding = 'ascii'
484
356
485 def init_syntax_highlighting(self):
357 def init_syntax_highlighting(self):
486 # Python source parser/formatter for syntax highlighting
358 # Python source parser/formatter for syntax highlighting
487 pyformat = PyColorize.Parser().format
359 pyformat = PyColorize.Parser().format
488 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
360 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
489
361
490 def init_pushd_popd_magic(self):
362 def init_pushd_popd_magic(self):
491 # for pushd/popd management
363 # for pushd/popd management
492 try:
364 try:
493 self.home_dir = get_home_dir()
365 self.home_dir = get_home_dir()
494 except HomeDirError, msg:
366 except HomeDirError, msg:
495 fatal(msg)
367 fatal(msg)
496
368
497 self.dir_stack = []
369 self.dir_stack = []
498
370
499 def init_logger(self):
371 def init_logger(self):
500 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
372 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
501 # local shortcut, this is used a LOT
373 # local shortcut, this is used a LOT
502 self.log = self.logger.log
374 self.log = self.logger.log
503
375
504 def init_logstart(self):
376 def init_logstart(self):
505 if self.logappend:
377 if self.logappend:
506 self.magic_logstart(self.logappend + ' append')
378 self.magic_logstart(self.logappend + ' append')
507 elif self.logfile:
379 elif self.logfile:
508 self.magic_logstart(self.logfile)
380 self.magic_logstart(self.logfile)
509 elif self.logstart:
381 elif self.logstart:
510 self.magic_logstart()
382 self.magic_logstart()
511
383
512 def init_builtins(self):
384 def init_builtins(self):
513 self.builtin_trap = BuiltinTrap(shell=self)
385 self.builtin_trap = BuiltinTrap(shell=self)
514
386
515 def init_inspector(self):
387 def init_inspector(self):
516 # Object inspector
388 # Object inspector
517 self.inspector = oinspect.Inspector(oinspect.InspectColors,
389 self.inspector = oinspect.Inspector(oinspect.InspectColors,
518 PyColorize.ANSICodeColors,
390 PyColorize.ANSICodeColors,
519 'NoColor',
391 'NoColor',
520 self.object_info_string_level)
392 self.object_info_string_level)
521
393
522 def init_prompts(self):
394 def init_prompts(self):
523 # Initialize cache, set in/out prompts and printing system
395 # Initialize cache, set in/out prompts and printing system
524 self.outputcache = CachedOutput(self,
396 self.outputcache = CachedOutput(self,
525 self.cache_size,
397 self.cache_size,
526 self.pprint,
398 self.pprint,
527 input_sep = self.separate_in,
399 input_sep = self.separate_in,
528 output_sep = self.separate_out,
400 output_sep = self.separate_out,
529 output_sep2 = self.separate_out2,
401 output_sep2 = self.separate_out2,
530 ps1 = self.prompt_in1,
402 ps1 = self.prompt_in1,
531 ps2 = self.prompt_in2,
403 ps2 = self.prompt_in2,
532 ps_out = self.prompt_out,
404 ps_out = self.prompt_out,
533 pad_left = self.prompts_pad_left)
405 pad_left = self.prompts_pad_left)
534
406
535 # user may have over-ridden the default print hook:
407 # user may have over-ridden the default print hook:
536 try:
408 try:
537 self.outputcache.__class__.display = self.hooks.display
409 self.outputcache.__class__.display = self.hooks.display
538 except AttributeError:
410 except AttributeError:
539 pass
411 pass
540
412
541 def init_displayhook(self):
413 def init_displayhook(self):
542 self.display_trap = DisplayTrap(hook=self.outputcache)
414 self.display_trap = DisplayTrap(hook=self.outputcache)
543
415
544 def init_reload_doctest(self):
416 def init_reload_doctest(self):
545 # Do a proper resetting of doctest, including the necessary displayhook
417 # Do a proper resetting of doctest, including the necessary displayhook
546 # monkeypatching
418 # monkeypatching
547 try:
419 try:
548 doctest_reload()
420 doctest_reload()
549 except ImportError:
421 except ImportError:
550 warn("doctest module does not exist.")
422 warn("doctest module does not exist.")
551
423
552 #-------------------------------------------------------------------------
424 #-------------------------------------------------------------------------
553 # Things related to the banner
554 #-------------------------------------------------------------------------
555
556 def init_banner(self, banner1, banner2, display_banner):
557 if banner1 is not None:
558 self.banner1 = banner1
559 if banner2 is not None:
560 self.banner2 = banner2
561 if display_banner is not None:
562 self.display_banner = display_banner
563 self.compute_banner()
564
565 def show_banner(self, banner=None):
566 if banner is None:
567 banner = self.banner
568 self.write(banner)
569
570 def compute_banner(self):
571 self.banner = self.banner1 + '\n'
572 if self.profile:
573 self.banner += '\nIPython profile: %s\n' % self.profile
574 if self.banner2:
575 self.banner += '\n' + self.banner2 + '\n'
576
577 #-------------------------------------------------------------------------
578 # Things related to injections into the sys module
425 # Things related to injections into the sys module
579 #-------------------------------------------------------------------------
426 #-------------------------------------------------------------------------
580
427
581 def save_sys_module_state(self):
428 def save_sys_module_state(self):
582 """Save the state of hooks in the sys module.
429 """Save the state of hooks in the sys module.
583
430
584 This has to be called after self.user_ns is created.
431 This has to be called after self.user_ns is created.
585 """
432 """
586 self._orig_sys_module_state = {}
433 self._orig_sys_module_state = {}
587 self._orig_sys_module_state['stdin'] = sys.stdin
434 self._orig_sys_module_state['stdin'] = sys.stdin
588 self._orig_sys_module_state['stdout'] = sys.stdout
435 self._orig_sys_module_state['stdout'] = sys.stdout
589 self._orig_sys_module_state['stderr'] = sys.stderr
436 self._orig_sys_module_state['stderr'] = sys.stderr
590 self._orig_sys_module_state['excepthook'] = sys.excepthook
437 self._orig_sys_module_state['excepthook'] = sys.excepthook
591 try:
438 try:
592 self._orig_sys_modules_main_name = self.user_ns['__name__']
439 self._orig_sys_modules_main_name = self.user_ns['__name__']
593 except KeyError:
440 except KeyError:
594 pass
441 pass
595
442
596 def restore_sys_module_state(self):
443 def restore_sys_module_state(self):
597 """Restore the state of the sys module."""
444 """Restore the state of the sys module."""
598 try:
445 try:
599 for k, v in self._orig_sys_module_state.items():
446 for k, v in self._orig_sys_module_state.items():
600 setattr(sys, k, v)
447 setattr(sys, k, v)
601 except AttributeError:
448 except AttributeError:
602 pass
449 pass
603 try:
450 try:
604 delattr(sys, 'ipcompleter')
451 delattr(sys, 'ipcompleter')
605 except AttributeError:
452 except AttributeError:
606 pass
453 pass
607 # Reset what what done in self.init_sys_modules
454 # Reset what what done in self.init_sys_modules
608 try:
455 try:
609 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
456 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
610 except (AttributeError, KeyError):
457 except (AttributeError, KeyError):
611 pass
458 pass
612
459
613 #-------------------------------------------------------------------------
460 #-------------------------------------------------------------------------
614 # Things related to hooks
461 # Things related to hooks
615 #-------------------------------------------------------------------------
462 #-------------------------------------------------------------------------
616
463
617 def init_hooks(self):
464 def init_hooks(self):
618 # hooks holds pointers used for user-side customizations
465 # hooks holds pointers used for user-side customizations
619 self.hooks = Struct()
466 self.hooks = Struct()
620
467
621 self.strdispatchers = {}
468 self.strdispatchers = {}
622
469
623 # Set all default hooks, defined in the IPython.hooks module.
470 # Set all default hooks, defined in the IPython.hooks module.
624 hooks = IPython.core.hooks
471 hooks = IPython.core.hooks
625 for hook_name in hooks.__all__:
472 for hook_name in hooks.__all__:
626 # default hooks have priority 100, i.e. low; user hooks should have
473 # default hooks have priority 100, i.e. low; user hooks should have
627 # 0-100 priority
474 # 0-100 priority
628 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
475 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
629
476
630 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
477 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
631 """set_hook(name,hook) -> sets an internal IPython hook.
478 """set_hook(name,hook) -> sets an internal IPython hook.
632
479
633 IPython exposes some of its internal API as user-modifiable hooks. By
480 IPython exposes some of its internal API as user-modifiable hooks. By
634 adding your function to one of these hooks, you can modify IPython's
481 adding your function to one of these hooks, you can modify IPython's
635 behavior to call at runtime your own routines."""
482 behavior to call at runtime your own routines."""
636
483
637 # At some point in the future, this should validate the hook before it
484 # At some point in the future, this should validate the hook before it
638 # accepts it. Probably at least check that the hook takes the number
485 # accepts it. Probably at least check that the hook takes the number
639 # of args it's supposed to.
486 # of args it's supposed to.
640
487
641 f = new.instancemethod(hook,self,self.__class__)
488 f = new.instancemethod(hook,self,self.__class__)
642
489
643 # check if the hook is for strdispatcher first
490 # check if the hook is for strdispatcher first
644 if str_key is not None:
491 if str_key is not None:
645 sdp = self.strdispatchers.get(name, StrDispatch())
492 sdp = self.strdispatchers.get(name, StrDispatch())
646 sdp.add_s(str_key, f, priority )
493 sdp.add_s(str_key, f, priority )
647 self.strdispatchers[name] = sdp
494 self.strdispatchers[name] = sdp
648 return
495 return
649 if re_key is not None:
496 if re_key is not None:
650 sdp = self.strdispatchers.get(name, StrDispatch())
497 sdp = self.strdispatchers.get(name, StrDispatch())
651 sdp.add_re(re.compile(re_key), f, priority )
498 sdp.add_re(re.compile(re_key), f, priority )
652 self.strdispatchers[name] = sdp
499 self.strdispatchers[name] = sdp
653 return
500 return
654
501
655 dp = getattr(self.hooks, name, None)
502 dp = getattr(self.hooks, name, None)
656 if name not in IPython.core.hooks.__all__:
503 if name not in IPython.core.hooks.__all__:
657 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
504 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
658 if not dp:
505 if not dp:
659 dp = IPython.core.hooks.CommandChainDispatcher()
506 dp = IPython.core.hooks.CommandChainDispatcher()
660
507
661 try:
508 try:
662 dp.add(f,priority)
509 dp.add(f,priority)
663 except AttributeError:
510 except AttributeError:
664 # it was not commandchain, plain old func - replace
511 # it was not commandchain, plain old func - replace
665 dp = f
512 dp = f
666
513
667 setattr(self.hooks,name, dp)
514 setattr(self.hooks,name, dp)
668
515
669 #-------------------------------------------------------------------------
516 #-------------------------------------------------------------------------
670 # Things related to the "main" module
517 # Things related to the "main" module
671 #-------------------------------------------------------------------------
518 #-------------------------------------------------------------------------
672
519
673 def new_main_mod(self,ns=None):
520 def new_main_mod(self,ns=None):
674 """Return a new 'main' module object for user code execution.
521 """Return a new 'main' module object for user code execution.
675 """
522 """
676 main_mod = self._user_main_module
523 main_mod = self._user_main_module
677 init_fakemod_dict(main_mod,ns)
524 init_fakemod_dict(main_mod,ns)
678 return main_mod
525 return main_mod
679
526
680 def cache_main_mod(self,ns,fname):
527 def cache_main_mod(self,ns,fname):
681 """Cache a main module's namespace.
528 """Cache a main module's namespace.
682
529
683 When scripts are executed via %run, we must keep a reference to the
530 When scripts are executed via %run, we must keep a reference to the
684 namespace of their __main__ module (a FakeModule instance) around so
531 namespace of their __main__ module (a FakeModule instance) around so
685 that Python doesn't clear it, rendering objects defined therein
532 that Python doesn't clear it, rendering objects defined therein
686 useless.
533 useless.
687
534
688 This method keeps said reference in a private dict, keyed by the
535 This method keeps said reference in a private dict, keyed by the
689 absolute path of the module object (which corresponds to the script
536 absolute path of the module object (which corresponds to the script
690 path). This way, for multiple executions of the same script we only
537 path). This way, for multiple executions of the same script we only
691 keep one copy of the namespace (the last one), thus preventing memory
538 keep one copy of the namespace (the last one), thus preventing memory
692 leaks from old references while allowing the objects from the last
539 leaks from old references while allowing the objects from the last
693 execution to be accessible.
540 execution to be accessible.
694
541
695 Note: we can not allow the actual FakeModule instances to be deleted,
542 Note: we can not allow the actual FakeModule instances to be deleted,
696 because of how Python tears down modules (it hard-sets all their
543 because of how Python tears down modules (it hard-sets all their
697 references to None without regard for reference counts). This method
544 references to None without regard for reference counts). This method
698 must therefore make a *copy* of the given namespace, to allow the
545 must therefore make a *copy* of the given namespace, to allow the
699 original module's __dict__ to be cleared and reused.
546 original module's __dict__ to be cleared and reused.
700
547
701
548
702 Parameters
549 Parameters
703 ----------
550 ----------
704 ns : a namespace (a dict, typically)
551 ns : a namespace (a dict, typically)
705
552
706 fname : str
553 fname : str
707 Filename associated with the namespace.
554 Filename associated with the namespace.
708
555
709 Examples
556 Examples
710 --------
557 --------
711
558
712 In [10]: import IPython
559 In [10]: import IPython
713
560
714 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
561 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
715
562
716 In [12]: IPython.__file__ in _ip._main_ns_cache
563 In [12]: IPython.__file__ in _ip._main_ns_cache
717 Out[12]: True
564 Out[12]: True
718 """
565 """
719 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
566 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
720
567
721 def clear_main_mod_cache(self):
568 def clear_main_mod_cache(self):
722 """Clear the cache of main modules.
569 """Clear the cache of main modules.
723
570
724 Mainly for use by utilities like %reset.
571 Mainly for use by utilities like %reset.
725
572
726 Examples
573 Examples
727 --------
574 --------
728
575
729 In [15]: import IPython
576 In [15]: import IPython
730
577
731 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
578 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
732
579
733 In [17]: len(_ip._main_ns_cache) > 0
580 In [17]: len(_ip._main_ns_cache) > 0
734 Out[17]: True
581 Out[17]: True
735
582
736 In [18]: _ip.clear_main_mod_cache()
583 In [18]: _ip.clear_main_mod_cache()
737
584
738 In [19]: len(_ip._main_ns_cache) == 0
585 In [19]: len(_ip._main_ns_cache) == 0
739 Out[19]: True
586 Out[19]: True
740 """
587 """
741 self._main_ns_cache.clear()
588 self._main_ns_cache.clear()
742
589
743 #-------------------------------------------------------------------------
590 #-------------------------------------------------------------------------
744 # Things related to debugging
591 # Things related to debugging
745 #-------------------------------------------------------------------------
592 #-------------------------------------------------------------------------
746
593
747 def init_pdb(self):
594 def init_pdb(self):
748 # Set calling of pdb on exceptions
595 # Set calling of pdb on exceptions
749 # self.call_pdb is a property
596 # self.call_pdb is a property
750 self.call_pdb = self.pdb
597 self.call_pdb = self.pdb
751
598
752 def _get_call_pdb(self):
599 def _get_call_pdb(self):
753 return self._call_pdb
600 return self._call_pdb
754
601
755 def _set_call_pdb(self,val):
602 def _set_call_pdb(self,val):
756
603
757 if val not in (0,1,False,True):
604 if val not in (0,1,False,True):
758 raise ValueError,'new call_pdb value must be boolean'
605 raise ValueError,'new call_pdb value must be boolean'
759
606
760 # store value in instance
607 # store value in instance
761 self._call_pdb = val
608 self._call_pdb = val
762
609
763 # notify the actual exception handlers
610 # notify the actual exception handlers
764 self.InteractiveTB.call_pdb = val
611 self.InteractiveTB.call_pdb = val
765 if self.isthreaded:
766 try:
767 self.sys_excepthook.call_pdb = val
768 except:
769 warn('Failed to activate pdb for threaded exception handler')
770
612
771 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
613 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
772 'Control auto-activation of pdb at exceptions')
614 'Control auto-activation of pdb at exceptions')
773
615
774 def debugger(self,force=False):
616 def debugger(self,force=False):
775 """Call the pydb/pdb debugger.
617 """Call the pydb/pdb debugger.
776
618
777 Keywords:
619 Keywords:
778
620
779 - force(False): by default, this routine checks the instance call_pdb
621 - force(False): by default, this routine checks the instance call_pdb
780 flag and does not actually invoke the debugger if the flag is false.
622 flag and does not actually invoke the debugger if the flag is false.
781 The 'force' option forces the debugger to activate even if the flag
623 The 'force' option forces the debugger to activate even if the flag
782 is false.
624 is false.
783 """
625 """
784
626
785 if not (force or self.call_pdb):
627 if not (force or self.call_pdb):
786 return
628 return
787
629
788 if not hasattr(sys,'last_traceback'):
630 if not hasattr(sys,'last_traceback'):
789 error('No traceback has been produced, nothing to debug.')
631 error('No traceback has been produced, nothing to debug.')
790 return
632 return
791
633
792 # use pydb if available
634 # use pydb if available
793 if debugger.has_pydb:
635 if debugger.has_pydb:
794 from pydb import pm
636 from pydb import pm
795 else:
637 else:
796 # fallback to our internal debugger
638 # fallback to our internal debugger
797 pm = lambda : self.InteractiveTB.debugger(force=True)
639 pm = lambda : self.InteractiveTB.debugger(force=True)
798 self.history_saving_wrapper(pm)()
640 self.history_saving_wrapper(pm)()
799
641
800 #-------------------------------------------------------------------------
642 #-------------------------------------------------------------------------
801 # Things related to IPython's various namespaces
643 # Things related to IPython's various namespaces
802 #-------------------------------------------------------------------------
644 #-------------------------------------------------------------------------
803
645
804 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
646 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
805 # Create the namespace where the user will operate. user_ns is
647 # Create the namespace where the user will operate. user_ns is
806 # normally the only one used, and it is passed to the exec calls as
648 # normally the only one used, and it is passed to the exec calls as
807 # the locals argument. But we do carry a user_global_ns namespace
649 # the locals argument. But we do carry a user_global_ns namespace
808 # given as the exec 'globals' argument, This is useful in embedding
650 # given as the exec 'globals' argument, This is useful in embedding
809 # situations where the ipython shell opens in a context where the
651 # situations where the ipython shell opens in a context where the
810 # distinction between locals and globals is meaningful. For
652 # distinction between locals and globals is meaningful. For
811 # non-embedded contexts, it is just the same object as the user_ns dict.
653 # non-embedded contexts, it is just the same object as the user_ns dict.
812
654
813 # FIXME. For some strange reason, __builtins__ is showing up at user
655 # FIXME. For some strange reason, __builtins__ is showing up at user
814 # level as a dict instead of a module. This is a manual fix, but I
656 # level as a dict instead of a module. This is a manual fix, but I
815 # should really track down where the problem is coming from. Alex
657 # should really track down where the problem is coming from. Alex
816 # Schmolck reported this problem first.
658 # Schmolck reported this problem first.
817
659
818 # A useful post by Alex Martelli on this topic:
660 # A useful post by Alex Martelli on this topic:
819 # Re: inconsistent value from __builtins__
661 # Re: inconsistent value from __builtins__
820 # Von: Alex Martelli <aleaxit@yahoo.com>
662 # Von: Alex Martelli <aleaxit@yahoo.com>
821 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
663 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
822 # Gruppen: comp.lang.python
664 # Gruppen: comp.lang.python
823
665
824 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
666 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
825 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
667 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
826 # > <type 'dict'>
668 # > <type 'dict'>
827 # > >>> print type(__builtins__)
669 # > >>> print type(__builtins__)
828 # > <type 'module'>
670 # > <type 'module'>
829 # > Is this difference in return value intentional?
671 # > Is this difference in return value intentional?
830
672
831 # Well, it's documented that '__builtins__' can be either a dictionary
673 # Well, it's documented that '__builtins__' can be either a dictionary
832 # or a module, and it's been that way for a long time. Whether it's
674 # or a module, and it's been that way for a long time. Whether it's
833 # intentional (or sensible), I don't know. In any case, the idea is
675 # intentional (or sensible), I don't know. In any case, the idea is
834 # that if you need to access the built-in namespace directly, you
676 # that if you need to access the built-in namespace directly, you
835 # should start with "import __builtin__" (note, no 's') which will
677 # should start with "import __builtin__" (note, no 's') which will
836 # definitely give you a module. Yeah, it's somewhat confusing:-(.
678 # definitely give you a module. Yeah, it's somewhat confusing:-(.
837
679
838 # These routines return properly built dicts as needed by the rest of
680 # These routines return properly built dicts as needed by the rest of
839 # the code, and can also be used by extension writers to generate
681 # the code, and can also be used by extension writers to generate
840 # properly initialized namespaces.
682 # properly initialized namespaces.
841 user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns)
683 user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns)
842
684
843 # Assign namespaces
685 # Assign namespaces
844 # This is the namespace where all normal user variables live
686 # This is the namespace where all normal user variables live
845 self.user_ns = user_ns
687 self.user_ns = user_ns
846 self.user_global_ns = user_global_ns
688 self.user_global_ns = user_global_ns
847
689
848 # An auxiliary namespace that checks what parts of the user_ns were
690 # An auxiliary namespace that checks what parts of the user_ns were
849 # loaded at startup, so we can list later only variables defined in
691 # loaded at startup, so we can list later only variables defined in
850 # actual interactive use. Since it is always a subset of user_ns, it
692 # actual interactive use. Since it is always a subset of user_ns, it
851 # doesn't need to be separately tracked in the ns_table.
693 # doesn't need to be separately tracked in the ns_table.
852 self.user_ns_hidden = {}
694 self.user_ns_hidden = {}
853
695
854 # A namespace to keep track of internal data structures to prevent
696 # A namespace to keep track of internal data structures to prevent
855 # them from cluttering user-visible stuff. Will be updated later
697 # them from cluttering user-visible stuff. Will be updated later
856 self.internal_ns = {}
698 self.internal_ns = {}
857
699
858 # Now that FakeModule produces a real module, we've run into a nasty
700 # Now that FakeModule produces a real module, we've run into a nasty
859 # problem: after script execution (via %run), the module where the user
701 # problem: after script execution (via %run), the module where the user
860 # code ran is deleted. Now that this object is a true module (needed
702 # code ran is deleted. Now that this object is a true module (needed
861 # so docetst and other tools work correctly), the Python module
703 # so docetst and other tools work correctly), the Python module
862 # teardown mechanism runs over it, and sets to None every variable
704 # teardown mechanism runs over it, and sets to None every variable
863 # present in that module. Top-level references to objects from the
705 # present in that module. Top-level references to objects from the
864 # script survive, because the user_ns is updated with them. However,
706 # script survive, because the user_ns is updated with them. However,
865 # calling functions defined in the script that use other things from
707 # calling functions defined in the script that use other things from
866 # the script will fail, because the function's closure had references
708 # the script will fail, because the function's closure had references
867 # to the original objects, which are now all None. So we must protect
709 # to the original objects, which are now all None. So we must protect
868 # these modules from deletion by keeping a cache.
710 # these modules from deletion by keeping a cache.
869 #
711 #
870 # To avoid keeping stale modules around (we only need the one from the
712 # To avoid keeping stale modules around (we only need the one from the
871 # last run), we use a dict keyed with the full path to the script, so
713 # last run), we use a dict keyed with the full path to the script, so
872 # only the last version of the module is held in the cache. Note,
714 # only the last version of the module is held in the cache. Note,
873 # however, that we must cache the module *namespace contents* (their
715 # however, that we must cache the module *namespace contents* (their
874 # __dict__). Because if we try to cache the actual modules, old ones
716 # __dict__). Because if we try to cache the actual modules, old ones
875 # (uncached) could be destroyed while still holding references (such as
717 # (uncached) could be destroyed while still holding references (such as
876 # those held by GUI objects that tend to be long-lived)>
718 # those held by GUI objects that tend to be long-lived)>
877 #
719 #
878 # The %reset command will flush this cache. See the cache_main_mod()
720 # The %reset command will flush this cache. See the cache_main_mod()
879 # and clear_main_mod_cache() methods for details on use.
721 # and clear_main_mod_cache() methods for details on use.
880
722
881 # This is the cache used for 'main' namespaces
723 # This is the cache used for 'main' namespaces
882 self._main_ns_cache = {}
724 self._main_ns_cache = {}
883 # And this is the single instance of FakeModule whose __dict__ we keep
725 # And this is the single instance of FakeModule whose __dict__ we keep
884 # copying and clearing for reuse on each %run
726 # copying and clearing for reuse on each %run
885 self._user_main_module = FakeModule()
727 self._user_main_module = FakeModule()
886
728
887 # A table holding all the namespaces IPython deals with, so that
729 # A table holding all the namespaces IPython deals with, so that
888 # introspection facilities can search easily.
730 # introspection facilities can search easily.
889 self.ns_table = {'user':user_ns,
731 self.ns_table = {'user':user_ns,
890 'user_global':user_global_ns,
732 'user_global':user_global_ns,
891 'internal':self.internal_ns,
733 'internal':self.internal_ns,
892 'builtin':__builtin__.__dict__
734 'builtin':__builtin__.__dict__
893 }
735 }
894
736
895 # Similarly, track all namespaces where references can be held and that
737 # Similarly, track all namespaces where references can be held and that
896 # we can safely clear (so it can NOT include builtin). This one can be
738 # we can safely clear (so it can NOT include builtin). This one can be
897 # a simple list.
739 # a simple list.
898 self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
740 self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
899 self.internal_ns, self._main_ns_cache ]
741 self.internal_ns, self._main_ns_cache ]
900
742
901 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
743 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
902 """Return a valid local and global user interactive namespaces.
744 """Return a valid local and global user interactive namespaces.
903
745
904 This builds a dict with the minimal information needed to operate as a
746 This builds a dict with the minimal information needed to operate as a
905 valid IPython user namespace, which you can pass to the various
747 valid IPython user namespace, which you can pass to the various
906 embedding classes in ipython. The default implementation returns the
748 embedding classes in ipython. The default implementation returns the
907 same dict for both the locals and the globals to allow functions to
749 same dict for both the locals and the globals to allow functions to
908 refer to variables in the namespace. Customized implementations can
750 refer to variables in the namespace. Customized implementations can
909 return different dicts. The locals dictionary can actually be anything
751 return different dicts. The locals dictionary can actually be anything
910 following the basic mapping protocol of a dict, but the globals dict
752 following the basic mapping protocol of a dict, but the globals dict
911 must be a true dict, not even a subclass. It is recommended that any
753 must be a true dict, not even a subclass. It is recommended that any
912 custom object for the locals namespace synchronize with the globals
754 custom object for the locals namespace synchronize with the globals
913 dict somehow.
755 dict somehow.
914
756
915 Raises TypeError if the provided globals namespace is not a true dict.
757 Raises TypeError if the provided globals namespace is not a true dict.
916
758
917 Parameters
759 Parameters
918 ----------
760 ----------
919 user_ns : dict-like, optional
761 user_ns : dict-like, optional
920 The current user namespace. The items in this namespace should
762 The current user namespace. The items in this namespace should
921 be included in the output. If None, an appropriate blank
763 be included in the output. If None, an appropriate blank
922 namespace should be created.
764 namespace should be created.
923 user_global_ns : dict, optional
765 user_global_ns : dict, optional
924 The current user global namespace. The items in this namespace
766 The current user global namespace. The items in this namespace
925 should be included in the output. If None, an appropriate
767 should be included in the output. If None, an appropriate
926 blank namespace should be created.
768 blank namespace should be created.
927
769
928 Returns
770 Returns
929 -------
771 -------
930 A pair of dictionary-like object to be used as the local namespace
772 A pair of dictionary-like object to be used as the local namespace
931 of the interpreter and a dict to be used as the global namespace.
773 of the interpreter and a dict to be used as the global namespace.
932 """
774 """
933
775
934
776
935 # We must ensure that __builtin__ (without the final 's') is always
777 # We must ensure that __builtin__ (without the final 's') is always
936 # available and pointing to the __builtin__ *module*. For more details:
778 # available and pointing to the __builtin__ *module*. For more details:
937 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
779 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
938
780
939 if user_ns is None:
781 if user_ns is None:
940 # Set __name__ to __main__ to better match the behavior of the
782 # Set __name__ to __main__ to better match the behavior of the
941 # normal interpreter.
783 # normal interpreter.
942 user_ns = {'__name__' :'__main__',
784 user_ns = {'__name__' :'__main__',
943 '__builtin__' : __builtin__,
785 '__builtin__' : __builtin__,
944 '__builtins__' : __builtin__,
786 '__builtins__' : __builtin__,
945 }
787 }
946 else:
788 else:
947 user_ns.setdefault('__name__','__main__')
789 user_ns.setdefault('__name__','__main__')
948 user_ns.setdefault('__builtin__',__builtin__)
790 user_ns.setdefault('__builtin__',__builtin__)
949 user_ns.setdefault('__builtins__',__builtin__)
791 user_ns.setdefault('__builtins__',__builtin__)
950
792
951 if user_global_ns is None:
793 if user_global_ns is None:
952 user_global_ns = user_ns
794 user_global_ns = user_ns
953 if type(user_global_ns) is not dict:
795 if type(user_global_ns) is not dict:
954 raise TypeError("user_global_ns must be a true dict; got %r"
796 raise TypeError("user_global_ns must be a true dict; got %r"
955 % type(user_global_ns))
797 % type(user_global_ns))
956
798
957 return user_ns, user_global_ns
799 return user_ns, user_global_ns
958
800
959 def init_sys_modules(self):
801 def init_sys_modules(self):
960 # We need to insert into sys.modules something that looks like a
802 # We need to insert into sys.modules something that looks like a
961 # module but which accesses the IPython namespace, for shelve and
803 # module but which accesses the IPython namespace, for shelve and
962 # pickle to work interactively. Normally they rely on getting
804 # pickle to work interactively. Normally they rely on getting
963 # everything out of __main__, but for embedding purposes each IPython
805 # everything out of __main__, but for embedding purposes each IPython
964 # instance has its own private namespace, so we can't go shoving
806 # instance has its own private namespace, so we can't go shoving
965 # everything into __main__.
807 # everything into __main__.
966
808
967 # note, however, that we should only do this for non-embedded
809 # note, however, that we should only do this for non-embedded
968 # ipythons, which really mimic the __main__.__dict__ with their own
810 # ipythons, which really mimic the __main__.__dict__ with their own
969 # namespace. Embedded instances, on the other hand, should not do
811 # namespace. Embedded instances, on the other hand, should not do
970 # this because they need to manage the user local/global namespaces
812 # this because they need to manage the user local/global namespaces
971 # only, but they live within a 'normal' __main__ (meaning, they
813 # only, but they live within a 'normal' __main__ (meaning, they
972 # shouldn't overtake the execution environment of the script they're
814 # shouldn't overtake the execution environment of the script they're
973 # embedded in).
815 # embedded in).
974
816
975 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
817 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
976
818
977 try:
819 try:
978 main_name = self.user_ns['__name__']
820 main_name = self.user_ns['__name__']
979 except KeyError:
821 except KeyError:
980 raise KeyError('user_ns dictionary MUST have a "__name__" key')
822 raise KeyError('user_ns dictionary MUST have a "__name__" key')
981 else:
823 else:
982 sys.modules[main_name] = FakeModule(self.user_ns)
824 sys.modules[main_name] = FakeModule(self.user_ns)
983
825
984 def init_user_ns(self):
826 def init_user_ns(self):
985 """Initialize all user-visible namespaces to their minimum defaults.
827 """Initialize all user-visible namespaces to their minimum defaults.
986
828
987 Certain history lists are also initialized here, as they effectively
829 Certain history lists are also initialized here, as they effectively
988 act as user namespaces.
830 act as user namespaces.
989
831
990 Notes
832 Notes
991 -----
833 -----
992 All data structures here are only filled in, they are NOT reset by this
834 All data structures here are only filled in, they are NOT reset by this
993 method. If they were not empty before, data will simply be added to
835 method. If they were not empty before, data will simply be added to
994 therm.
836 therm.
995 """
837 """
996 # This function works in two parts: first we put a few things in
838 # This function works in two parts: first we put a few things in
997 # user_ns, and we sync that contents into user_ns_hidden so that these
839 # user_ns, and we sync that contents into user_ns_hidden so that these
998 # initial variables aren't shown by %who. After the sync, we add the
840 # initial variables aren't shown by %who. After the sync, we add the
999 # rest of what we *do* want the user to see with %who even on a new
841 # rest of what we *do* want the user to see with %who even on a new
1000 # session (probably nothing, so theye really only see their own stuff)
842 # session (probably nothing, so theye really only see their own stuff)
1001
843
1002 # The user dict must *always* have a __builtin__ reference to the
844 # The user dict must *always* have a __builtin__ reference to the
1003 # Python standard __builtin__ namespace, which must be imported.
845 # Python standard __builtin__ namespace, which must be imported.
1004 # This is so that certain operations in prompt evaluation can be
846 # This is so that certain operations in prompt evaluation can be
1005 # reliably executed with builtins. Note that we can NOT use
847 # reliably executed with builtins. Note that we can NOT use
1006 # __builtins__ (note the 's'), because that can either be a dict or a
848 # __builtins__ (note the 's'), because that can either be a dict or a
1007 # module, and can even mutate at runtime, depending on the context
849 # module, and can even mutate at runtime, depending on the context
1008 # (Python makes no guarantees on it). In contrast, __builtin__ is
850 # (Python makes no guarantees on it). In contrast, __builtin__ is
1009 # always a module object, though it must be explicitly imported.
851 # always a module object, though it must be explicitly imported.
1010
852
1011 # For more details:
853 # For more details:
1012 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
854 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1013 ns = dict(__builtin__ = __builtin__)
855 ns = dict(__builtin__ = __builtin__)
1014
856
1015 # Put 'help' in the user namespace
857 # Put 'help' in the user namespace
1016 try:
858 try:
1017 from site import _Helper
859 from site import _Helper
1018 ns['help'] = _Helper()
860 ns['help'] = _Helper()
1019 except ImportError:
861 except ImportError:
1020 warn('help() not available - check site.py')
862 warn('help() not available - check site.py')
1021
863
1022 # make global variables for user access to the histories
864 # make global variables for user access to the histories
1023 ns['_ih'] = self.input_hist
865 ns['_ih'] = self.input_hist
1024 ns['_oh'] = self.output_hist
866 ns['_oh'] = self.output_hist
1025 ns['_dh'] = self.dir_hist
867 ns['_dh'] = self.dir_hist
1026
868
1027 ns['_sh'] = shadowns
869 ns['_sh'] = shadowns
1028
870
1029 # user aliases to input and output histories. These shouldn't show up
871 # user aliases to input and output histories. These shouldn't show up
1030 # in %who, as they can have very large reprs.
872 # in %who, as they can have very large reprs.
1031 ns['In'] = self.input_hist
873 ns['In'] = self.input_hist
1032 ns['Out'] = self.output_hist
874 ns['Out'] = self.output_hist
1033
875
1034 # Store myself as the public api!!!
876 # Store myself as the public api!!!
1035 ns['get_ipython'] = self.get_ipython
877 ns['get_ipython'] = self.get_ipython
1036
878
1037 # Sync what we've added so far to user_ns_hidden so these aren't seen
879 # Sync what we've added so far to user_ns_hidden so these aren't seen
1038 # by %who
880 # by %who
1039 self.user_ns_hidden.update(ns)
881 self.user_ns_hidden.update(ns)
1040
882
1041 # Anything put into ns now would show up in %who. Think twice before
883 # Anything put into ns now would show up in %who. Think twice before
1042 # putting anything here, as we really want %who to show the user their
884 # putting anything here, as we really want %who to show the user their
1043 # stuff, not our variables.
885 # stuff, not our variables.
1044
886
1045 # Finally, update the real user's namespace
887 # Finally, update the real user's namespace
1046 self.user_ns.update(ns)
888 self.user_ns.update(ns)
1047
889
1048
890
1049 def reset(self):
891 def reset(self):
1050 """Clear all internal namespaces.
892 """Clear all internal namespaces.
1051
893
1052 Note that this is much more aggressive than %reset, since it clears
894 Note that this is much more aggressive than %reset, since it clears
1053 fully all namespaces, as well as all input/output lists.
895 fully all namespaces, as well as all input/output lists.
1054 """
896 """
1055 for ns in self.ns_refs_table:
897 for ns in self.ns_refs_table:
1056 ns.clear()
898 ns.clear()
1057
899
1058 self.alias_manager.clear_aliases()
900 self.alias_manager.clear_aliases()
1059
901
1060 # Clear input and output histories
902 # Clear input and output histories
1061 self.input_hist[:] = []
903 self.input_hist[:] = []
1062 self.input_hist_raw[:] = []
904 self.input_hist_raw[:] = []
1063 self.output_hist.clear()
905 self.output_hist.clear()
1064
906
1065 # Restore the user namespaces to minimal usability
907 # Restore the user namespaces to minimal usability
1066 self.init_user_ns()
908 self.init_user_ns()
1067
909
1068 # Restore the default and user aliases
910 # Restore the default and user aliases
1069 self.alias_manager.init_aliases()
911 self.alias_manager.init_aliases()
1070
912
1071 def reset_selective(self, regex=None):
913 def reset_selective(self, regex=None):
1072 """Clear selective variables from internal namespaces based on a specified regular expression.
914 """Clear selective variables from internal namespaces based on a specified regular expression.
1073
915
1074 Parameters
916 Parameters
1075 ----------
917 ----------
1076 regex : string or compiled pattern, optional
918 regex : string or compiled pattern, optional
1077 A regular expression pattern that will be used in searching variable names in the users
919 A regular expression pattern that will be used in searching variable names in the users
1078 namespaces.
920 namespaces.
1079 """
921 """
1080 if regex is not None:
922 if regex is not None:
1081 try:
923 try:
1082 m = re.compile(regex)
924 m = re.compile(regex)
1083 except TypeError:
925 except TypeError:
1084 raise TypeError('regex must be a string or compiled pattern')
926 raise TypeError('regex must be a string or compiled pattern')
1085 # Search for keys in each namespace that match the given regex
927 # Search for keys in each namespace that match the given regex
1086 # If a match is found, delete the key/value pair.
928 # If a match is found, delete the key/value pair.
1087 for ns in self.ns_refs_table:
929 for ns in self.ns_refs_table:
1088 for var in ns:
930 for var in ns:
1089 if m.search(var):
931 if m.search(var):
1090 del ns[var]
932 del ns[var]
1091
933
1092 def push(self, variables, interactive=True):
934 def push(self, variables, interactive=True):
1093 """Inject a group of variables into the IPython user namespace.
935 """Inject a group of variables into the IPython user namespace.
1094
936
1095 Parameters
937 Parameters
1096 ----------
938 ----------
1097 variables : dict, str or list/tuple of str
939 variables : dict, str or list/tuple of str
1098 The variables to inject into the user's namespace. If a dict,
940 The variables to inject into the user's namespace. If a dict,
1099 a simple update is done. If a str, the string is assumed to
941 a simple update is done. If a str, the string is assumed to
1100 have variable names separated by spaces. A list/tuple of str
942 have variable names separated by spaces. A list/tuple of str
1101 can also be used to give the variable names. If just the variable
943 can also be used to give the variable names. If just the variable
1102 names are give (list/tuple/str) then the variable values looked
944 names are give (list/tuple/str) then the variable values looked
1103 up in the callers frame.
945 up in the callers frame.
1104 interactive : bool
946 interactive : bool
1105 If True (default), the variables will be listed with the ``who``
947 If True (default), the variables will be listed with the ``who``
1106 magic.
948 magic.
1107 """
949 """
1108 vdict = None
950 vdict = None
1109
951
1110 # We need a dict of name/value pairs to do namespace updates.
952 # We need a dict of name/value pairs to do namespace updates.
1111 if isinstance(variables, dict):
953 if isinstance(variables, dict):
1112 vdict = variables
954 vdict = variables
1113 elif isinstance(variables, (basestring, list, tuple)):
955 elif isinstance(variables, (basestring, list, tuple)):
1114 if isinstance(variables, basestring):
956 if isinstance(variables, basestring):
1115 vlist = variables.split()
957 vlist = variables.split()
1116 else:
958 else:
1117 vlist = variables
959 vlist = variables
1118 vdict = {}
960 vdict = {}
1119 cf = sys._getframe(1)
961 cf = sys._getframe(1)
1120 for name in vlist:
962 for name in vlist:
1121 try:
963 try:
1122 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
964 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1123 except:
965 except:
1124 print ('Could not get variable %s from %s' %
966 print ('Could not get variable %s from %s' %
1125 (name,cf.f_code.co_name))
967 (name,cf.f_code.co_name))
1126 else:
968 else:
1127 raise ValueError('variables must be a dict/str/list/tuple')
969 raise ValueError('variables must be a dict/str/list/tuple')
1128
970
1129 # Propagate variables to user namespace
971 # Propagate variables to user namespace
1130 self.user_ns.update(vdict)
972 self.user_ns.update(vdict)
1131
973
1132 # And configure interactive visibility
974 # And configure interactive visibility
1133 config_ns = self.user_ns_hidden
975 config_ns = self.user_ns_hidden
1134 if interactive:
976 if interactive:
1135 for name, val in vdict.iteritems():
977 for name, val in vdict.iteritems():
1136 config_ns.pop(name, None)
978 config_ns.pop(name, None)
1137 else:
979 else:
1138 for name,val in vdict.iteritems():
980 for name,val in vdict.iteritems():
1139 config_ns[name] = val
981 config_ns[name] = val
1140
982
1141 #-------------------------------------------------------------------------
983 #-------------------------------------------------------------------------
1142 # Things related to history management
984 # Things related to history management
1143 #-------------------------------------------------------------------------
985 #-------------------------------------------------------------------------
1144
986
1145 def init_history(self):
987 def init_history(self):
1146 # List of input with multi-line handling.
988 # List of input with multi-line handling.
1147 self.input_hist = InputList()
989 self.input_hist = InputList()
1148 # This one will hold the 'raw' input history, without any
990 # This one will hold the 'raw' input history, without any
1149 # pre-processing. This will allow users to retrieve the input just as
991 # pre-processing. This will allow users to retrieve the input just as
1150 # it was exactly typed in by the user, with %hist -r.
992 # it was exactly typed in by the user, with %hist -r.
1151 self.input_hist_raw = InputList()
993 self.input_hist_raw = InputList()
1152
994
1153 # list of visited directories
995 # list of visited directories
1154 try:
996 try:
1155 self.dir_hist = [os.getcwd()]
997 self.dir_hist = [os.getcwd()]
1156 except OSError:
998 except OSError:
1157 self.dir_hist = []
999 self.dir_hist = []
1158
1000
1159 # dict of output history
1001 # dict of output history
1160 self.output_hist = {}
1002 self.output_hist = {}
1161
1003
1162 # Now the history file
1004 # Now the history file
1163 if self.profile:
1005 if self.profile:
1164 histfname = 'history-%s' % self.profile
1006 histfname = 'history-%s' % self.profile
1165 else:
1007 else:
1166 histfname = 'history'
1008 histfname = 'history'
1167 self.histfile = os.path.join(self.ipython_dir, histfname)
1009 self.histfile = os.path.join(self.ipython_dir, histfname)
1168
1010
1169 # Fill the history zero entry, user counter starts at 1
1011 # Fill the history zero entry, user counter starts at 1
1170 self.input_hist.append('\n')
1012 self.input_hist.append('\n')
1171 self.input_hist_raw.append('\n')
1013 self.input_hist_raw.append('\n')
1172
1014
1173 def init_shadow_hist(self):
1015 def init_shadow_hist(self):
1174 try:
1016 try:
1175 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1017 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1176 except exceptions.UnicodeDecodeError:
1018 except exceptions.UnicodeDecodeError:
1177 print "Your ipython_dir can't be decoded to unicode!"
1019 print "Your ipython_dir can't be decoded to unicode!"
1178 print "Please set HOME environment variable to something that"
1020 print "Please set HOME environment variable to something that"
1179 print r"only has ASCII characters, e.g. c:\home"
1021 print r"only has ASCII characters, e.g. c:\home"
1180 print "Now it is", self.ipython_dir
1022 print "Now it is", self.ipython_dir
1181 sys.exit()
1023 sys.exit()
1182 self.shadowhist = ipcorehist.ShadowHist(self.db)
1024 self.shadowhist = ipcorehist.ShadowHist(self.db)
1183
1025
1184 def savehist(self):
1026 def savehist(self):
1185 """Save input history to a file (via readline library)."""
1027 """Save input history to a file (via readline library)."""
1186
1028
1187 try:
1029 try:
1188 self.readline.write_history_file(self.histfile)
1030 self.readline.write_history_file(self.histfile)
1189 except:
1031 except:
1190 print 'Unable to save IPython command history to file: ' + \
1032 print 'Unable to save IPython command history to file: ' + \
1191 `self.histfile`
1033 `self.histfile`
1192
1034
1193 def reloadhist(self):
1035 def reloadhist(self):
1194 """Reload the input history from disk file."""
1036 """Reload the input history from disk file."""
1195
1037
1196 try:
1038 try:
1197 self.readline.clear_history()
1039 self.readline.clear_history()
1198 self.readline.read_history_file(self.shell.histfile)
1040 self.readline.read_history_file(self.shell.histfile)
1199 except AttributeError:
1041 except AttributeError:
1200 pass
1042 pass
1201
1043
1202 def history_saving_wrapper(self, func):
1044 def history_saving_wrapper(self, func):
1203 """ Wrap func for readline history saving
1045 """ Wrap func for readline history saving
1204
1046
1205 Convert func into callable that saves & restores
1047 Convert func into callable that saves & restores
1206 history around the call """
1048 history around the call """
1207
1049
1208 if self.has_readline:
1050 if self.has_readline:
1209 from IPython.utils import rlineimpl as readline
1051 from IPython.utils import rlineimpl as readline
1210 else:
1052 else:
1211 return func
1053 return func
1212
1054
1213 def wrapper():
1055 def wrapper():
1214 self.savehist()
1056 self.savehist()
1215 try:
1057 try:
1216 func()
1058 func()
1217 finally:
1059 finally:
1218 readline.read_history_file(self.histfile)
1060 readline.read_history_file(self.histfile)
1219 return wrapper
1061 return wrapper
1220
1062
1221 #-------------------------------------------------------------------------
1063 #-------------------------------------------------------------------------
1222 # Things related to exception handling and tracebacks (not debugging)
1064 # Things related to exception handling and tracebacks (not debugging)
1223 #-------------------------------------------------------------------------
1065 #-------------------------------------------------------------------------
1224
1066
1225 def init_traceback_handlers(self, custom_exceptions):
1067 def init_traceback_handlers(self, custom_exceptions):
1226 # Syntax error handler.
1068 # Syntax error handler.
1227 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1069 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1228
1070
1229 # The interactive one is initialized with an offset, meaning we always
1071 # The interactive one is initialized with an offset, meaning we always
1230 # want to remove the topmost item in the traceback, which is our own
1072 # want to remove the topmost item in the traceback, which is our own
1231 # internal code. Valid modes: ['Plain','Context','Verbose']
1073 # internal code. Valid modes: ['Plain','Context','Verbose']
1232 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1074 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1233 color_scheme='NoColor',
1075 color_scheme='NoColor',
1234 tb_offset = 1)
1076 tb_offset = 1)
1235
1077
1236 # The instance will store a pointer to the system-wide exception hook,
1078 # The instance will store a pointer to the system-wide exception hook,
1237 # so that runtime code (such as magics) can access it. This is because
1079 # so that runtime code (such as magics) can access it. This is because
1238 # during the read-eval loop, it may get temporarily overwritten.
1080 # during the read-eval loop, it may get temporarily overwritten.
1239 self.sys_excepthook = sys.excepthook
1081 self.sys_excepthook = sys.excepthook
1240
1082
1241 # and add any custom exception handlers the user may have specified
1083 # and add any custom exception handlers the user may have specified
1242 self.set_custom_exc(*custom_exceptions)
1084 self.set_custom_exc(*custom_exceptions)
1243
1085
1244 # Set the exception mode
1086 # Set the exception mode
1245 self.InteractiveTB.set_mode(mode=self.xmode)
1087 self.InteractiveTB.set_mode(mode=self.xmode)
1246
1088
1247 def set_custom_exc(self,exc_tuple,handler):
1089 def set_custom_exc(self,exc_tuple,handler):
1248 """set_custom_exc(exc_tuple,handler)
1090 """set_custom_exc(exc_tuple,handler)
1249
1091
1250 Set a custom exception handler, which will be called if any of the
1092 Set a custom exception handler, which will be called if any of the
1251 exceptions in exc_tuple occur in the mainloop (specifically, in the
1093 exceptions in exc_tuple occur in the mainloop (specifically, in the
1252 runcode() method.
1094 runcode() method.
1253
1095
1254 Inputs:
1096 Inputs:
1255
1097
1256 - exc_tuple: a *tuple* of valid exceptions to call the defined
1098 - exc_tuple: a *tuple* of valid exceptions to call the defined
1257 handler for. It is very important that you use a tuple, and NOT A
1099 handler for. It is very important that you use a tuple, and NOT A
1258 LIST here, because of the way Python's except statement works. If
1100 LIST here, because of the way Python's except statement works. If
1259 you only want to trap a single exception, use a singleton tuple:
1101 you only want to trap a single exception, use a singleton tuple:
1260
1102
1261 exc_tuple == (MyCustomException,)
1103 exc_tuple == (MyCustomException,)
1262
1104
1263 - handler: this must be defined as a function with the following
1105 - handler: this must be defined as a function with the following
1264 basic interface: def my_handler(self,etype,value,tb).
1106 basic interface: def my_handler(self,etype,value,tb).
1265
1107
1266 This will be made into an instance method (via new.instancemethod)
1108 This will be made into an instance method (via new.instancemethod)
1267 of IPython itself, and it will be called if any of the exceptions
1109 of IPython itself, and it will be called if any of the exceptions
1268 listed in the exc_tuple are caught. If the handler is None, an
1110 listed in the exc_tuple are caught. If the handler is None, an
1269 internal basic one is used, which just prints basic info.
1111 internal basic one is used, which just prints basic info.
1270
1112
1271 WARNING: by putting in your own exception handler into IPython's main
1113 WARNING: by putting in your own exception handler into IPython's main
1272 execution loop, you run a very good chance of nasty crashes. This
1114 execution loop, you run a very good chance of nasty crashes. This
1273 facility should only be used if you really know what you are doing."""
1115 facility should only be used if you really know what you are doing."""
1274
1116
1275 assert type(exc_tuple)==type(()) , \
1117 assert type(exc_tuple)==type(()) , \
1276 "The custom exceptions must be given AS A TUPLE."
1118 "The custom exceptions must be given AS A TUPLE."
1277
1119
1278 def dummy_handler(self,etype,value,tb):
1120 def dummy_handler(self,etype,value,tb):
1279 print '*** Simple custom exception handler ***'
1121 print '*** Simple custom exception handler ***'
1280 print 'Exception type :',etype
1122 print 'Exception type :',etype
1281 print 'Exception value:',value
1123 print 'Exception value:',value
1282 print 'Traceback :',tb
1124 print 'Traceback :',tb
1283 print 'Source code :','\n'.join(self.buffer)
1125 print 'Source code :','\n'.join(self.buffer)
1284
1126
1285 if handler is None: handler = dummy_handler
1127 if handler is None: handler = dummy_handler
1286
1128
1287 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1129 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1288 self.custom_exceptions = exc_tuple
1130 self.custom_exceptions = exc_tuple
1289
1131
1290 def excepthook(self, etype, value, tb):
1132 def excepthook(self, etype, value, tb):
1291 """One more defense for GUI apps that call sys.excepthook.
1133 """One more defense for GUI apps that call sys.excepthook.
1292
1134
1293 GUI frameworks like wxPython trap exceptions and call
1135 GUI frameworks like wxPython trap exceptions and call
1294 sys.excepthook themselves. I guess this is a feature that
1136 sys.excepthook themselves. I guess this is a feature that
1295 enables them to keep running after exceptions that would
1137 enables them to keep running after exceptions that would
1296 otherwise kill their mainloop. This is a bother for IPython
1138 otherwise kill their mainloop. This is a bother for IPython
1297 which excepts to catch all of the program exceptions with a try:
1139 which excepts to catch all of the program exceptions with a try:
1298 except: statement.
1140 except: statement.
1299
1141
1300 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1142 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1301 any app directly invokes sys.excepthook, it will look to the user like
1143 any app directly invokes sys.excepthook, it will look to the user like
1302 IPython crashed. In order to work around this, we can disable the
1144 IPython crashed. In order to work around this, we can disable the
1303 CrashHandler and replace it with this excepthook instead, which prints a
1145 CrashHandler and replace it with this excepthook instead, which prints a
1304 regular traceback using our InteractiveTB. In this fashion, apps which
1146 regular traceback using our InteractiveTB. In this fashion, apps which
1305 call sys.excepthook will generate a regular-looking exception from
1147 call sys.excepthook will generate a regular-looking exception from
1306 IPython, and the CrashHandler will only be triggered by real IPython
1148 IPython, and the CrashHandler will only be triggered by real IPython
1307 crashes.
1149 crashes.
1308
1150
1309 This hook should be used sparingly, only in places which are not likely
1151 This hook should be used sparingly, only in places which are not likely
1310 to be true IPython errors.
1152 to be true IPython errors.
1311 """
1153 """
1312 self.showtraceback((etype,value,tb),tb_offset=0)
1154 self.showtraceback((etype,value,tb),tb_offset=0)
1313
1155
1314 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1156 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1315 exception_only=False):
1157 exception_only=False):
1316 """Display the exception that just occurred.
1158 """Display the exception that just occurred.
1317
1159
1318 If nothing is known about the exception, this is the method which
1160 If nothing is known about the exception, this is the method which
1319 should be used throughout the code for presenting user tracebacks,
1161 should be used throughout the code for presenting user tracebacks,
1320 rather than directly invoking the InteractiveTB object.
1162 rather than directly invoking the InteractiveTB object.
1321
1163
1322 A specific showsyntaxerror() also exists, but this method can take
1164 A specific showsyntaxerror() also exists, but this method can take
1323 care of calling it if needed, so unless you are explicitly catching a
1165 care of calling it if needed, so unless you are explicitly catching a
1324 SyntaxError exception, don't try to analyze the stack manually and
1166 SyntaxError exception, don't try to analyze the stack manually and
1325 simply call this method."""
1167 simply call this method."""
1326
1168
1327 try:
1169 try:
1328 if exc_tuple is None:
1170 if exc_tuple is None:
1329 etype, value, tb = sys.exc_info()
1171 etype, value, tb = sys.exc_info()
1330 else:
1172 else:
1331 etype, value, tb = exc_tuple
1173 etype, value, tb = exc_tuple
1332
1174
1333 if etype is None:
1175 if etype is None:
1334 if hasattr(sys, 'last_type'):
1176 if hasattr(sys, 'last_type'):
1335 etype, value, tb = sys.last_type, sys.last_value, \
1177 etype, value, tb = sys.last_type, sys.last_value, \
1336 sys.last_traceback
1178 sys.last_traceback
1337 else:
1179 else:
1338 self.write('No traceback available to show.\n')
1180 self.write('No traceback available to show.\n')
1339 return
1181 return
1340
1182
1341 if etype is SyntaxError:
1183 if etype is SyntaxError:
1342 # Though this won't be called by syntax errors in the input
1184 # Though this won't be called by syntax errors in the input
1343 # line, there may be SyntaxError cases whith imported code.
1185 # line, there may be SyntaxError cases whith imported code.
1344 self.showsyntaxerror(filename)
1186 self.showsyntaxerror(filename)
1345 elif etype is UsageError:
1187 elif etype is UsageError:
1346 print "UsageError:", value
1188 print "UsageError:", value
1347 else:
1189 else:
1348 # WARNING: these variables are somewhat deprecated and not
1190 # WARNING: these variables are somewhat deprecated and not
1349 # necessarily safe to use in a threaded environment, but tools
1191 # necessarily safe to use in a threaded environment, but tools
1350 # like pdb depend on their existence, so let's set them. If we
1192 # like pdb depend on their existence, so let's set them. If we
1351 # find problems in the field, we'll need to revisit their use.
1193 # find problems in the field, we'll need to revisit their use.
1352 sys.last_type = etype
1194 sys.last_type = etype
1353 sys.last_value = value
1195 sys.last_value = value
1354 sys.last_traceback = tb
1196 sys.last_traceback = tb
1355
1197
1356 if etype in self.custom_exceptions:
1198 if etype in self.custom_exceptions:
1357 self.CustomTB(etype,value,tb)
1199 self.CustomTB(etype,value,tb)
1358 else:
1200 else:
1359 if exception_only:
1201 if exception_only:
1360 m = ('An exception has occurred, use %tb to see the '
1202 m = ('An exception has occurred, use %tb to see the '
1361 'full traceback.')
1203 'full traceback.')
1362 print m
1204 print m
1363 self.InteractiveTB.show_exception_only(etype, value)
1205 self.InteractiveTB.show_exception_only(etype, value)
1364 else:
1206 else:
1365 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1207 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1366 if self.InteractiveTB.call_pdb:
1208 if self.InteractiveTB.call_pdb:
1367 # pdb mucks up readline, fix it back
1209 # pdb mucks up readline, fix it back
1368 self.set_completer()
1210 self.set_completer()
1369
1211
1370 except KeyboardInterrupt:
1212 except KeyboardInterrupt:
1371 self.write("\nKeyboardInterrupt\n")
1213 self.write("\nKeyboardInterrupt\n")
1372
1214
1373
1215
1374 def showsyntaxerror(self, filename=None):
1216 def showsyntaxerror(self, filename=None):
1375 """Display the syntax error that just occurred.
1217 """Display the syntax error that just occurred.
1376
1218
1377 This doesn't display a stack trace because there isn't one.
1219 This doesn't display a stack trace because there isn't one.
1378
1220
1379 If a filename is given, it is stuffed in the exception instead
1221 If a filename is given, it is stuffed in the exception instead
1380 of what was there before (because Python's parser always uses
1222 of what was there before (because Python's parser always uses
1381 "<string>" when reading from a string).
1223 "<string>" when reading from a string).
1382 """
1224 """
1383 etype, value, last_traceback = sys.exc_info()
1225 etype, value, last_traceback = sys.exc_info()
1384
1226
1385 # See note about these variables in showtraceback() above
1227 # See note about these variables in showtraceback() above
1386 sys.last_type = etype
1228 sys.last_type = etype
1387 sys.last_value = value
1229 sys.last_value = value
1388 sys.last_traceback = last_traceback
1230 sys.last_traceback = last_traceback
1389
1231
1390 if filename and etype is SyntaxError:
1232 if filename and etype is SyntaxError:
1391 # Work hard to stuff the correct filename in the exception
1233 # Work hard to stuff the correct filename in the exception
1392 try:
1234 try:
1393 msg, (dummy_filename, lineno, offset, line) = value
1235 msg, (dummy_filename, lineno, offset, line) = value
1394 except:
1236 except:
1395 # Not the format we expect; leave it alone
1237 # Not the format we expect; leave it alone
1396 pass
1238 pass
1397 else:
1239 else:
1398 # Stuff in the right filename
1240 # Stuff in the right filename
1399 try:
1241 try:
1400 # Assume SyntaxError is a class exception
1242 # Assume SyntaxError is a class exception
1401 value = SyntaxError(msg, (filename, lineno, offset, line))
1243 value = SyntaxError(msg, (filename, lineno, offset, line))
1402 except:
1244 except:
1403 # If that failed, assume SyntaxError is a string
1245 # If that failed, assume SyntaxError is a string
1404 value = msg, (filename, lineno, offset, line)
1246 value = msg, (filename, lineno, offset, line)
1405 self.SyntaxTB(etype,value,[])
1247 self.SyntaxTB(etype,value,[])
1406
1248
1407 def edit_syntax_error(self):
1408 """The bottom half of the syntax error handler called in the main loop.
1409
1410 Loop until syntax error is fixed or user cancels.
1411 """
1412
1413 while self.SyntaxTB.last_syntax_error:
1414 # copy and clear last_syntax_error
1415 err = self.SyntaxTB.clear_err_state()
1416 if not self._should_recompile(err):
1417 return
1418 try:
1419 # may set last_syntax_error again if a SyntaxError is raised
1420 self.safe_execfile(err.filename,self.user_ns)
1421 except:
1422 self.showtraceback()
1423 else:
1424 try:
1425 f = file(err.filename)
1426 try:
1427 # This should be inside a display_trap block and I
1428 # think it is.
1429 sys.displayhook(f.read())
1430 finally:
1431 f.close()
1432 except:
1433 self.showtraceback()
1434
1435 def _should_recompile(self,e):
1436 """Utility routine for edit_syntax_error"""
1437
1438 if e.filename in ('<ipython console>','<input>','<string>',
1439 '<console>','<BackgroundJob compilation>',
1440 None):
1441
1442 return False
1443 try:
1444 if (self.autoedit_syntax and
1445 not self.ask_yes_no('Return to editor to correct syntax error? '
1446 '[Y/n] ','y')):
1447 return False
1448 except EOFError:
1449 return False
1450
1451 def int0(x):
1452 try:
1453 return int(x)
1454 except TypeError:
1455 return 0
1456 # always pass integer line and offset values to editor hook
1457 try:
1458 self.hooks.fix_error_editor(e.filename,
1459 int0(e.lineno),int0(e.offset),e.msg)
1460 except TryNext:
1461 warn('Could not open editor')
1462 return False
1463 return True
1464
1465 #-------------------------------------------------------------------------
1249 #-------------------------------------------------------------------------
1466 # Things related to tab completion
1250 # Things related to tab completion
1467 #-------------------------------------------------------------------------
1251 #-------------------------------------------------------------------------
1468
1252
1469 def complete(self, text):
1253 def complete(self, text):
1470 """Return a sorted list of all possible completions on text.
1254 """Return a sorted list of all possible completions on text.
1471
1255
1472 Inputs:
1256 Inputs:
1473
1257
1474 - text: a string of text to be completed on.
1258 - text: a string of text to be completed on.
1475
1259
1476 This is a wrapper around the completion mechanism, similar to what
1260 This is a wrapper around the completion mechanism, similar to what
1477 readline does at the command line when the TAB key is hit. By
1261 readline does at the command line when the TAB key is hit. By
1478 exposing it as a method, it can be used by other non-readline
1262 exposing it as a method, it can be used by other non-readline
1479 environments (such as GUIs) for text completion.
1263 environments (such as GUIs) for text completion.
1480
1264
1481 Simple usage example:
1265 Simple usage example:
1482
1266
1483 In [7]: x = 'hello'
1267 In [7]: x = 'hello'
1484
1268
1485 In [8]: x
1269 In [8]: x
1486 Out[8]: 'hello'
1270 Out[8]: 'hello'
1487
1271
1488 In [9]: print x
1272 In [9]: print x
1489 hello
1273 hello
1490
1274
1491 In [10]: _ip.complete('x.l')
1275 In [10]: _ip.complete('x.l')
1492 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1276 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1493 """
1277 """
1494
1278
1495 # Inject names into __builtin__ so we can complete on the added names.
1279 # Inject names into __builtin__ so we can complete on the added names.
1496 with self.builtin_trap:
1280 with self.builtin_trap:
1497 complete = self.Completer.complete
1281 complete = self.Completer.complete
1498 state = 0
1282 state = 0
1499 # use a dict so we get unique keys, since ipyhton's multiple
1283 # use a dict so we get unique keys, since ipyhton's multiple
1500 # completers can return duplicates. When we make 2.4 a requirement,
1284 # completers can return duplicates. When we make 2.4 a requirement,
1501 # start using sets instead, which are faster.
1285 # start using sets instead, which are faster.
1502 comps = {}
1286 comps = {}
1503 while True:
1287 while True:
1504 newcomp = complete(text,state,line_buffer=text)
1288 newcomp = complete(text,state,line_buffer=text)
1505 if newcomp is None:
1289 if newcomp is None:
1506 break
1290 break
1507 comps[newcomp] = 1
1291 comps[newcomp] = 1
1508 state += 1
1292 state += 1
1509 outcomps = comps.keys()
1293 outcomps = comps.keys()
1510 outcomps.sort()
1294 outcomps.sort()
1511 #print "T:",text,"OC:",outcomps # dbg
1295 #print "T:",text,"OC:",outcomps # dbg
1512 #print "vars:",self.user_ns.keys()
1296 #print "vars:",self.user_ns.keys()
1513 return outcomps
1297 return outcomps
1514
1298
1515 def set_custom_completer(self,completer,pos=0):
1299 def set_custom_completer(self,completer,pos=0):
1516 """Adds a new custom completer function.
1300 """Adds a new custom completer function.
1517
1301
1518 The position argument (defaults to 0) is the index in the completers
1302 The position argument (defaults to 0) is the index in the completers
1519 list where you want the completer to be inserted."""
1303 list where you want the completer to be inserted."""
1520
1304
1521 newcomp = new.instancemethod(completer,self.Completer,
1305 newcomp = new.instancemethod(completer,self.Completer,
1522 self.Completer.__class__)
1306 self.Completer.__class__)
1523 self.Completer.matchers.insert(pos,newcomp)
1307 self.Completer.matchers.insert(pos,newcomp)
1524
1308
1525 def set_completer(self):
1309 def set_completer(self):
1526 """Reset readline's completer to be our own."""
1310 """Reset readline's completer to be our own."""
1527 self.readline.set_completer(self.Completer.complete)
1311 self.readline.set_completer(self.Completer.complete)
1528
1312
1529 def set_completer_frame(self, frame=None):
1313 def set_completer_frame(self, frame=None):
1530 """Set the frame of the completer."""
1314 """Set the frame of the completer."""
1531 if frame:
1315 if frame:
1532 self.Completer.namespace = frame.f_locals
1316 self.Completer.namespace = frame.f_locals
1533 self.Completer.global_namespace = frame.f_globals
1317 self.Completer.global_namespace = frame.f_globals
1534 else:
1318 else:
1535 self.Completer.namespace = self.user_ns
1319 self.Completer.namespace = self.user_ns
1536 self.Completer.global_namespace = self.user_global_ns
1320 self.Completer.global_namespace = self.user_global_ns
1537
1321
1538 #-------------------------------------------------------------------------
1322 #-------------------------------------------------------------------------
1539 # Things related to readline
1323 # Things related to readline
1540 #-------------------------------------------------------------------------
1324 #-------------------------------------------------------------------------
1541
1325
1542 def init_readline(self):
1326 def init_readline(self):
1543 """Command history completion/saving/reloading."""
1327 """Command history completion/saving/reloading."""
1544
1328
1545 if self.readline_use:
1329 if self.readline_use:
1546 import IPython.utils.rlineimpl as readline
1330 import IPython.utils.rlineimpl as readline
1547
1331
1548 self.rl_next_input = None
1332 self.rl_next_input = None
1549 self.rl_do_indent = False
1333 self.rl_do_indent = False
1550
1334
1551 if not self.readline_use or not readline.have_readline:
1335 if not self.readline_use or not readline.have_readline:
1552 self.has_readline = False
1336 self.has_readline = False
1553 self.readline = None
1337 self.readline = None
1554 # Set a number of methods that depend on readline to be no-op
1338 # Set a number of methods that depend on readline to be no-op
1555 self.savehist = no_op
1339 self.savehist = no_op
1556 self.reloadhist = no_op
1340 self.reloadhist = no_op
1557 self.set_completer = no_op
1341 self.set_completer = no_op
1558 self.set_custom_completer = no_op
1342 self.set_custom_completer = no_op
1559 self.set_completer_frame = no_op
1343 self.set_completer_frame = no_op
1560 warn('Readline services not available or not loaded.')
1344 warn('Readline services not available or not loaded.')
1561 else:
1345 else:
1562 self.has_readline = True
1346 self.has_readline = True
1563 self.readline = readline
1347 self.readline = readline
1564 sys.modules['readline'] = readline
1348 sys.modules['readline'] = readline
1565 import atexit
1349 import atexit
1566 from IPython.core.completer import IPCompleter
1350 from IPython.core.completer import IPCompleter
1567 self.Completer = IPCompleter(self,
1351 self.Completer = IPCompleter(self,
1568 self.user_ns,
1352 self.user_ns,
1569 self.user_global_ns,
1353 self.user_global_ns,
1570 self.readline_omit__names,
1354 self.readline_omit__names,
1571 self.alias_manager.alias_table)
1355 self.alias_manager.alias_table)
1572 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1356 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1573 self.strdispatchers['complete_command'] = sdisp
1357 self.strdispatchers['complete_command'] = sdisp
1574 self.Completer.custom_completers = sdisp
1358 self.Completer.custom_completers = sdisp
1575 # Platform-specific configuration
1359 # Platform-specific configuration
1576 if os.name == 'nt':
1360 if os.name == 'nt':
1577 self.readline_startup_hook = readline.set_pre_input_hook
1361 self.readline_startup_hook = readline.set_pre_input_hook
1578 else:
1362 else:
1579 self.readline_startup_hook = readline.set_startup_hook
1363 self.readline_startup_hook = readline.set_startup_hook
1580
1364
1581 # Load user's initrc file (readline config)
1365 # Load user's initrc file (readline config)
1582 # Or if libedit is used, load editrc.
1366 # Or if libedit is used, load editrc.
1583 inputrc_name = os.environ.get('INPUTRC')
1367 inputrc_name = os.environ.get('INPUTRC')
1584 if inputrc_name is None:
1368 if inputrc_name is None:
1585 home_dir = get_home_dir()
1369 home_dir = get_home_dir()
1586 if home_dir is not None:
1370 if home_dir is not None:
1587 inputrc_name = '.inputrc'
1371 inputrc_name = '.inputrc'
1588 if readline.uses_libedit:
1372 if readline.uses_libedit:
1589 inputrc_name = '.editrc'
1373 inputrc_name = '.editrc'
1590 inputrc_name = os.path.join(home_dir, inputrc_name)
1374 inputrc_name = os.path.join(home_dir, inputrc_name)
1591 if os.path.isfile(inputrc_name):
1375 if os.path.isfile(inputrc_name):
1592 try:
1376 try:
1593 readline.read_init_file(inputrc_name)
1377 readline.read_init_file(inputrc_name)
1594 except:
1378 except:
1595 warn('Problems reading readline initialization file <%s>'
1379 warn('Problems reading readline initialization file <%s>'
1596 % inputrc_name)
1380 % inputrc_name)
1597
1381
1598 # save this in sys so embedded copies can restore it properly
1382 # save this in sys so embedded copies can restore it properly
1599 sys.ipcompleter = self.Completer.complete
1383 sys.ipcompleter = self.Completer.complete
1600 self.set_completer()
1384 self.set_completer()
1601
1385
1602 # Configure readline according to user's prefs
1386 # Configure readline according to user's prefs
1603 # This is only done if GNU readline is being used. If libedit
1387 # This is only done if GNU readline is being used. If libedit
1604 # is being used (as on Leopard) the readline config is
1388 # is being used (as on Leopard) the readline config is
1605 # not run as the syntax for libedit is different.
1389 # not run as the syntax for libedit is different.
1606 if not readline.uses_libedit:
1390 if not readline.uses_libedit:
1607 for rlcommand in self.readline_parse_and_bind:
1391 for rlcommand in self.readline_parse_and_bind:
1608 #print "loading rl:",rlcommand # dbg
1392 #print "loading rl:",rlcommand # dbg
1609 readline.parse_and_bind(rlcommand)
1393 readline.parse_and_bind(rlcommand)
1610
1394
1611 # Remove some chars from the delimiters list. If we encounter
1395 # Remove some chars from the delimiters list. If we encounter
1612 # unicode chars, discard them.
1396 # unicode chars, discard them.
1613 delims = readline.get_completer_delims().encode("ascii", "ignore")
1397 delims = readline.get_completer_delims().encode("ascii", "ignore")
1614 delims = delims.translate(string._idmap,
1398 delims = delims.translate(string._idmap,
1615 self.readline_remove_delims)
1399 self.readline_remove_delims)
1616 readline.set_completer_delims(delims)
1400 readline.set_completer_delims(delims)
1617 # otherwise we end up with a monster history after a while:
1401 # otherwise we end up with a monster history after a while:
1618 readline.set_history_length(1000)
1402 readline.set_history_length(1000)
1619 try:
1403 try:
1620 #print '*** Reading readline history' # dbg
1404 #print '*** Reading readline history' # dbg
1621 readline.read_history_file(self.histfile)
1405 readline.read_history_file(self.histfile)
1622 except IOError:
1406 except IOError:
1623 pass # It doesn't exist yet.
1407 pass # It doesn't exist yet.
1624
1408
1625 atexit.register(self.atexit_operations)
1409 atexit.register(self.atexit_operations)
1626 del atexit
1410 del atexit
1627
1411
1628 # Configure auto-indent for all platforms
1412 # Configure auto-indent for all platforms
1629 self.set_autoindent(self.autoindent)
1413 self.set_autoindent(self.autoindent)
1630
1414
1631 def set_next_input(self, s):
1415 def set_next_input(self, s):
1632 """ Sets the 'default' input string for the next command line.
1416 """ Sets the 'default' input string for the next command line.
1633
1417
1634 Requires readline.
1418 Requires readline.
1635
1419
1636 Example:
1420 Example:
1637
1421
1638 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1422 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1639 [D:\ipython]|2> Hello Word_ # cursor is here
1423 [D:\ipython]|2> Hello Word_ # cursor is here
1640 """
1424 """
1641
1425
1642 self.rl_next_input = s
1426 self.rl_next_input = s
1643
1427
1428 # Maybe move this to the terminal subclass?
1644 def pre_readline(self):
1429 def pre_readline(self):
1645 """readline hook to be used at the start of each line.
1430 """readline hook to be used at the start of each line.
1646
1431
1647 Currently it handles auto-indent only."""
1432 Currently it handles auto-indent only."""
1648
1433
1649 #debugx('self.indent_current_nsp','pre_readline:')
1650
1651 if self.rl_do_indent:
1434 if self.rl_do_indent:
1652 self.readline.insert_text(self._indent_current_str())
1435 self.readline.insert_text(self._indent_current_str())
1653 if self.rl_next_input is not None:
1436 if self.rl_next_input is not None:
1654 self.readline.insert_text(self.rl_next_input)
1437 self.readline.insert_text(self.rl_next_input)
1655 self.rl_next_input = None
1438 self.rl_next_input = None
1656
1439
1657 def _indent_current_str(self):
1440 def _indent_current_str(self):
1658 """return the current level of indentation as a string"""
1441 """return the current level of indentation as a string"""
1659 return self.indent_current_nsp * ' '
1442 return self.indent_current_nsp * ' '
1660
1443
1661 #-------------------------------------------------------------------------
1444 #-------------------------------------------------------------------------
1662 # Things related to magics
1445 # Things related to magics
1663 #-------------------------------------------------------------------------
1446 #-------------------------------------------------------------------------
1664
1447
1665 def init_magics(self):
1448 def init_magics(self):
1666 # Set user colors (don't do it in the constructor above so that it
1449 # Set user colors (don't do it in the constructor above so that it
1667 # doesn't crash if colors option is invalid)
1450 # doesn't crash if colors option is invalid)
1668 self.magic_colors(self.colors)
1451 self.magic_colors(self.colors)
1669 # History was moved to a separate module
1452 # History was moved to a separate module
1670 from . import history
1453 from . import history
1671 history.init_ipython(self)
1454 history.init_ipython(self)
1672
1455
1673 def magic(self,arg_s):
1456 def magic(self,arg_s):
1674 """Call a magic function by name.
1457 """Call a magic function by name.
1675
1458
1676 Input: a string containing the name of the magic function to call and any
1459 Input: a string containing the name of the magic function to call and any
1677 additional arguments to be passed to the magic.
1460 additional arguments to be passed to the magic.
1678
1461
1679 magic('name -opt foo bar') is equivalent to typing at the ipython
1462 magic('name -opt foo bar') is equivalent to typing at the ipython
1680 prompt:
1463 prompt:
1681
1464
1682 In[1]: %name -opt foo bar
1465 In[1]: %name -opt foo bar
1683
1466
1684 To call a magic without arguments, simply use magic('name').
1467 To call a magic without arguments, simply use magic('name').
1685
1468
1686 This provides a proper Python function to call IPython's magics in any
1469 This provides a proper Python function to call IPython's magics in any
1687 valid Python code you can type at the interpreter, including loops and
1470 valid Python code you can type at the interpreter, including loops and
1688 compound statements.
1471 compound statements.
1689 """
1472 """
1690 args = arg_s.split(' ',1)
1473 args = arg_s.split(' ',1)
1691 magic_name = args[0]
1474 magic_name = args[0]
1692 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1475 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1693
1476
1694 try:
1477 try:
1695 magic_args = args[1]
1478 magic_args = args[1]
1696 except IndexError:
1479 except IndexError:
1697 magic_args = ''
1480 magic_args = ''
1698 fn = getattr(self,'magic_'+magic_name,None)
1481 fn = getattr(self,'magic_'+magic_name,None)
1699 if fn is None:
1482 if fn is None:
1700 error("Magic function `%s` not found." % magic_name)
1483 error("Magic function `%s` not found." % magic_name)
1701 else:
1484 else:
1702 magic_args = self.var_expand(magic_args,1)
1485 magic_args = self.var_expand(magic_args,1)
1703 with nested(self.builtin_trap,):
1486 with nested(self.builtin_trap,):
1704 result = fn(magic_args)
1487 result = fn(magic_args)
1705 return result
1488 return result
1706
1489
1707 def define_magic(self, magicname, func):
1490 def define_magic(self, magicname, func):
1708 """Expose own function as magic function for ipython
1491 """Expose own function as magic function for ipython
1709
1492
1710 def foo_impl(self,parameter_s=''):
1493 def foo_impl(self,parameter_s=''):
1711 'My very own magic!. (Use docstrings, IPython reads them).'
1494 'My very own magic!. (Use docstrings, IPython reads them).'
1712 print 'Magic function. Passed parameter is between < >:'
1495 print 'Magic function. Passed parameter is between < >:'
1713 print '<%s>' % parameter_s
1496 print '<%s>' % parameter_s
1714 print 'The self object is:',self
1497 print 'The self object is:',self
1715
1498
1716 self.define_magic('foo',foo_impl)
1499 self.define_magic('foo',foo_impl)
1717 """
1500 """
1718
1501
1719 import new
1502 import new
1720 im = new.instancemethod(func,self, self.__class__)
1503 im = new.instancemethod(func,self, self.__class__)
1721 old = getattr(self, "magic_" + magicname, None)
1504 old = getattr(self, "magic_" + magicname, None)
1722 setattr(self, "magic_" + magicname, im)
1505 setattr(self, "magic_" + magicname, im)
1723 return old
1506 return old
1724
1507
1725 #-------------------------------------------------------------------------
1508 #-------------------------------------------------------------------------
1726 # Things related to macros
1509 # Things related to macros
1727 #-------------------------------------------------------------------------
1510 #-------------------------------------------------------------------------
1728
1511
1729 def define_macro(self, name, themacro):
1512 def define_macro(self, name, themacro):
1730 """Define a new macro
1513 """Define a new macro
1731
1514
1732 Parameters
1515 Parameters
1733 ----------
1516 ----------
1734 name : str
1517 name : str
1735 The name of the macro.
1518 The name of the macro.
1736 themacro : str or Macro
1519 themacro : str or Macro
1737 The action to do upon invoking the macro. If a string, a new
1520 The action to do upon invoking the macro. If a string, a new
1738 Macro object is created by passing the string to it.
1521 Macro object is created by passing the string to it.
1739 """
1522 """
1740
1523
1741 from IPython.core import macro
1524 from IPython.core import macro
1742
1525
1743 if isinstance(themacro, basestring):
1526 if isinstance(themacro, basestring):
1744 themacro = macro.Macro(themacro)
1527 themacro = macro.Macro(themacro)
1745 if not isinstance(themacro, macro.Macro):
1528 if not isinstance(themacro, macro.Macro):
1746 raise ValueError('A macro must be a string or a Macro instance.')
1529 raise ValueError('A macro must be a string or a Macro instance.')
1747 self.user_ns[name] = themacro
1530 self.user_ns[name] = themacro
1748
1531
1749 #-------------------------------------------------------------------------
1532 #-------------------------------------------------------------------------
1750 # Things related to the running of system commands
1533 # Things related to the running of system commands
1751 #-------------------------------------------------------------------------
1534 #-------------------------------------------------------------------------
1752
1535
1753 def system(self, cmd):
1536 def system(self, cmd):
1754 """Make a system call, using IPython."""
1537 """Make a system call, using IPython."""
1755 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1538 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1756
1539
1757 #-------------------------------------------------------------------------
1540 #-------------------------------------------------------------------------
1758 # Things related to aliases
1541 # Things related to aliases
1759 #-------------------------------------------------------------------------
1542 #-------------------------------------------------------------------------
1760
1543
1761 def init_alias(self):
1544 def init_alias(self):
1762 self.alias_manager = AliasManager(shell=self, config=self.config)
1545 self.alias_manager = AliasManager(shell=self, config=self.config)
1763 self.ns_table['alias'] = self.alias_manager.alias_table,
1546 self.ns_table['alias'] = self.alias_manager.alias_table,
1764
1547
1765 #-------------------------------------------------------------------------
1548 #-------------------------------------------------------------------------
1766 # Things related to extensions and plugins
1549 # Things related to extensions and plugins
1767 #-------------------------------------------------------------------------
1550 #-------------------------------------------------------------------------
1768
1551
1769 def init_extension_manager(self):
1552 def init_extension_manager(self):
1770 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1553 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1771
1554
1772 def init_plugin_manager(self):
1555 def init_plugin_manager(self):
1773 self.plugin_manager = PluginManager(config=self.config)
1556 self.plugin_manager = PluginManager(config=self.config)
1774
1557
1775 #-------------------------------------------------------------------------
1558 #-------------------------------------------------------------------------
1559 # Things related to the prefilter
1560 #-------------------------------------------------------------------------
1561
1562 def init_prefilter(self):
1563 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1564 # Ultimately this will be refactored in the new interpreter code, but
1565 # for now, we should expose the main prefilter method (there's legacy
1566 # code out there that may rely on this).
1567 self.prefilter = self.prefilter_manager.prefilter_lines
1568
1569 #-------------------------------------------------------------------------
1776 # Things related to the running of code
1570 # Things related to the running of code
1777 #-------------------------------------------------------------------------
1571 #-------------------------------------------------------------------------
1778
1572
1779 def ex(self, cmd):
1573 def ex(self, cmd):
1780 """Execute a normal python statement in user namespace."""
1574 """Execute a normal python statement in user namespace."""
1781 with nested(self.builtin_trap,):
1575 with nested(self.builtin_trap,):
1782 exec cmd in self.user_global_ns, self.user_ns
1576 exec cmd in self.user_global_ns, self.user_ns
1783
1577
1784 def ev(self, expr):
1578 def ev(self, expr):
1785 """Evaluate python expression expr in user namespace.
1579 """Evaluate python expression expr in user namespace.
1786
1580
1787 Returns the result of evaluation
1581 Returns the result of evaluation
1788 """
1582 """
1789 with nested(self.builtin_trap,):
1583 with nested(self.builtin_trap,):
1790 return eval(expr, self.user_global_ns, self.user_ns)
1584 return eval(expr, self.user_global_ns, self.user_ns)
1791
1585
1792 def mainloop(self, display_banner=None):
1793 """Start the mainloop.
1794
1795 If an optional banner argument is given, it will override the
1796 internally created default banner.
1797 """
1798
1799 with nested(self.builtin_trap, self.display_trap):
1800
1801 # if you run stuff with -c <cmd>, raw hist is not updated
1802 # ensure that it's in sync
1803 if len(self.input_hist) != len (self.input_hist_raw):
1804 self.input_hist_raw = InputList(self.input_hist)
1805
1806 while 1:
1807 try:
1808 self.interact(display_banner=display_banner)
1809 #self.interact_with_readline()
1810 # XXX for testing of a readline-decoupled repl loop, call
1811 # interact_with_readline above
1812 break
1813 except KeyboardInterrupt:
1814 # this should not be necessary, but KeyboardInterrupt
1815 # handling seems rather unpredictable...
1816 self.write("\nKeyboardInterrupt in interact()\n")
1817
1818 def interact_prompt(self):
1819 """ Print the prompt (in read-eval-print loop)
1820
1821 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1822 used in standard IPython flow.
1823 """
1824 if self.more:
1825 try:
1826 prompt = self.hooks.generate_prompt(True)
1827 except:
1828 self.showtraceback()
1829 if self.autoindent:
1830 self.rl_do_indent = True
1831
1832 else:
1833 try:
1834 prompt = self.hooks.generate_prompt(False)
1835 except:
1836 self.showtraceback()
1837 self.write(prompt)
1838
1839 def interact_handle_input(self,line):
1840 """ Handle the input line (in read-eval-print loop)
1841
1842 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1843 used in standard IPython flow.
1844 """
1845 if line.lstrip() == line:
1846 self.shadowhist.add(line.strip())
1847 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1848
1849 if line.strip():
1850 if self.more:
1851 self.input_hist_raw[-1] += '%s\n' % line
1852 else:
1853 self.input_hist_raw.append('%s\n' % line)
1854
1855
1856 self.more = self.push_line(lineout)
1857 if (self.SyntaxTB.last_syntax_error and
1858 self.autoedit_syntax):
1859 self.edit_syntax_error()
1860
1861 def interact_with_readline(self):
1862 """ Demo of using interact_handle_input, interact_prompt
1863
1864 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1865 it should work like this.
1866 """
1867 self.readline_startup_hook(self.pre_readline)
1868 while not self.exit_now:
1869 self.interact_prompt()
1870 if self.more:
1871 self.rl_do_indent = True
1872 else:
1873 self.rl_do_indent = False
1874 line = raw_input_original().decode(self.stdin_encoding)
1875 self.interact_handle_input(line)
1876
1877 def interact(self, display_banner=None):
1878 """Closely emulate the interactive Python console."""
1879
1880 # batch run -> do not interact
1881 if self.exit_now:
1882 return
1883
1884 if display_banner is None:
1885 display_banner = self.display_banner
1886 if display_banner:
1887 self.show_banner()
1888
1889 more = 0
1890
1891 # Mark activity in the builtins
1892 __builtin__.__dict__['__IPYTHON__active'] += 1
1893
1894 if self.has_readline:
1895 self.readline_startup_hook(self.pre_readline)
1896 # exit_now is set by a call to %Exit or %Quit, through the
1897 # ask_exit callback.
1898
1899 while not self.exit_now:
1900 self.hooks.pre_prompt_hook()
1901 if more:
1902 try:
1903 prompt = self.hooks.generate_prompt(True)
1904 except:
1905 self.showtraceback()
1906 if self.autoindent:
1907 self.rl_do_indent = True
1908
1909 else:
1910 try:
1911 prompt = self.hooks.generate_prompt(False)
1912 except:
1913 self.showtraceback()
1914 try:
1915 line = self.raw_input(prompt, more)
1916 if self.exit_now:
1917 # quick exit on sys.std[in|out] close
1918 break
1919 if self.autoindent:
1920 self.rl_do_indent = False
1921
1922 except KeyboardInterrupt:
1923 #double-guard against keyboardinterrupts during kbdint handling
1924 try:
1925 self.write('\nKeyboardInterrupt\n')
1926 self.resetbuffer()
1927 # keep cache in sync with the prompt counter:
1928 self.outputcache.prompt_count -= 1
1929
1930 if self.autoindent:
1931 self.indent_current_nsp = 0
1932 more = 0
1933 except KeyboardInterrupt:
1934 pass
1935 except EOFError:
1936 if self.autoindent:
1937 self.rl_do_indent = False
1938 if self.has_readline:
1939 self.readline_startup_hook(None)
1940 self.write('\n')
1941 self.exit()
1942 except bdb.BdbQuit:
1943 warn('The Python debugger has exited with a BdbQuit exception.\n'
1944 'Because of how pdb handles the stack, it is impossible\n'
1945 'for IPython to properly format this particular exception.\n'
1946 'IPython will resume normal operation.')
1947 except:
1948 # exceptions here are VERY RARE, but they can be triggered
1949 # asynchronously by signal handlers, for example.
1950 self.showtraceback()
1951 else:
1952 more = self.push_line(line)
1953 if (self.SyntaxTB.last_syntax_error and
1954 self.autoedit_syntax):
1955 self.edit_syntax_error()
1956
1957 # We are off again...
1958 __builtin__.__dict__['__IPYTHON__active'] -= 1
1959
1960 # Turn off the exit flag, so the mainloop can be restarted if desired
1961 self.exit_now = False
1962
1963 def safe_execfile(self, fname, *where, **kw):
1586 def safe_execfile(self, fname, *where, **kw):
1964 """A safe version of the builtin execfile().
1587 """A safe version of the builtin execfile().
1965
1588
1966 This version will never throw an exception, but instead print
1589 This version will never throw an exception, but instead print
1967 helpful error messages to the screen. This only works on pure
1590 helpful error messages to the screen. This only works on pure
1968 Python files with the .py extension.
1591 Python files with the .py extension.
1969
1592
1970 Parameters
1593 Parameters
1971 ----------
1594 ----------
1972 fname : string
1595 fname : string
1973 The name of the file to be executed.
1596 The name of the file to be executed.
1974 where : tuple
1597 where : tuple
1975 One or two namespaces, passed to execfile() as (globals,locals).
1598 One or two namespaces, passed to execfile() as (globals,locals).
1976 If only one is given, it is passed as both.
1599 If only one is given, it is passed as both.
1977 exit_ignore : bool (False)
1600 exit_ignore : bool (False)
1978 If True, then silence SystemExit for non-zero status (it is always
1601 If True, then silence SystemExit for non-zero status (it is always
1979 silenced for zero status, as it is so common).
1602 silenced for zero status, as it is so common).
1980 """
1603 """
1981 kw.setdefault('exit_ignore', False)
1604 kw.setdefault('exit_ignore', False)
1982
1605
1983 fname = os.path.abspath(os.path.expanduser(fname))
1606 fname = os.path.abspath(os.path.expanduser(fname))
1984
1607
1985 # Make sure we have a .py file
1608 # Make sure we have a .py file
1986 if not fname.endswith('.py'):
1609 if not fname.endswith('.py'):
1987 warn('File must end with .py to be run using execfile: <%s>' % fname)
1610 warn('File must end with .py to be run using execfile: <%s>' % fname)
1988
1611
1989 # Make sure we can open the file
1612 # Make sure we can open the file
1990 try:
1613 try:
1991 with open(fname) as thefile:
1614 with open(fname) as thefile:
1992 pass
1615 pass
1993 except:
1616 except:
1994 warn('Could not open file <%s> for safe execution.' % fname)
1617 warn('Could not open file <%s> for safe execution.' % fname)
1995 return
1618 return
1996
1619
1997 # Find things also in current directory. This is needed to mimic the
1620 # Find things also in current directory. This is needed to mimic the
1998 # behavior of running a script from the system command line, where
1621 # behavior of running a script from the system command line, where
1999 # Python inserts the script's directory into sys.path
1622 # Python inserts the script's directory into sys.path
2000 dname = os.path.dirname(fname)
1623 dname = os.path.dirname(fname)
2001
1624
2002 with prepended_to_syspath(dname):
1625 with prepended_to_syspath(dname):
2003 try:
1626 try:
2004 execfile(fname,*where)
1627 execfile(fname,*where)
2005 except SystemExit, status:
1628 except SystemExit, status:
2006 # If the call was made with 0 or None exit status (sys.exit(0)
1629 # If the call was made with 0 or None exit status (sys.exit(0)
2007 # or sys.exit() ), don't bother showing a traceback, as both of
1630 # or sys.exit() ), don't bother showing a traceback, as both of
2008 # these are considered normal by the OS:
1631 # these are considered normal by the OS:
2009 # > python -c'import sys;sys.exit(0)'; echo $?
1632 # > python -c'import sys;sys.exit(0)'; echo $?
2010 # 0
1633 # 0
2011 # > python -c'import sys;sys.exit()'; echo $?
1634 # > python -c'import sys;sys.exit()'; echo $?
2012 # 0
1635 # 0
2013 # For other exit status, we show the exception unless
1636 # For other exit status, we show the exception unless
2014 # explicitly silenced, but only in short form.
1637 # explicitly silenced, but only in short form.
2015 if status.code not in (0, None) and not kw['exit_ignore']:
1638 if status.code not in (0, None) and not kw['exit_ignore']:
2016 self.showtraceback(exception_only=True)
1639 self.showtraceback(exception_only=True)
2017 except:
1640 except:
2018 self.showtraceback()
1641 self.showtraceback()
2019
1642
2020 def safe_execfile_ipy(self, fname):
1643 def safe_execfile_ipy(self, fname):
2021 """Like safe_execfile, but for .ipy files with IPython syntax.
1644 """Like safe_execfile, but for .ipy files with IPython syntax.
2022
1645
2023 Parameters
1646 Parameters
2024 ----------
1647 ----------
2025 fname : str
1648 fname : str
2026 The name of the file to execute. The filename must have a
1649 The name of the file to execute. The filename must have a
2027 .ipy extension.
1650 .ipy extension.
2028 """
1651 """
2029 fname = os.path.abspath(os.path.expanduser(fname))
1652 fname = os.path.abspath(os.path.expanduser(fname))
2030
1653
2031 # Make sure we have a .py file
1654 # Make sure we have a .py file
2032 if not fname.endswith('.ipy'):
1655 if not fname.endswith('.ipy'):
2033 warn('File must end with .py to be run using execfile: <%s>' % fname)
1656 warn('File must end with .py to be run using execfile: <%s>' % fname)
2034
1657
2035 # Make sure we can open the file
1658 # Make sure we can open the file
2036 try:
1659 try:
2037 with open(fname) as thefile:
1660 with open(fname) as thefile:
2038 pass
1661 pass
2039 except:
1662 except:
2040 warn('Could not open file <%s> for safe execution.' % fname)
1663 warn('Could not open file <%s> for safe execution.' % fname)
2041 return
1664 return
2042
1665
2043 # Find things also in current directory. This is needed to mimic the
1666 # Find things also in current directory. This is needed to mimic the
2044 # behavior of running a script from the system command line, where
1667 # behavior of running a script from the system command line, where
2045 # Python inserts the script's directory into sys.path
1668 # Python inserts the script's directory into sys.path
2046 dname = os.path.dirname(fname)
1669 dname = os.path.dirname(fname)
2047
1670
2048 with prepended_to_syspath(dname):
1671 with prepended_to_syspath(dname):
2049 try:
1672 try:
2050 with open(fname) as thefile:
1673 with open(fname) as thefile:
2051 script = thefile.read()
1674 script = thefile.read()
2052 # self.runlines currently captures all exceptions
1675 # self.runlines currently captures all exceptions
2053 # raise in user code. It would be nice if there were
1676 # raise in user code. It would be nice if there were
2054 # versions of runlines, execfile that did raise, so
1677 # versions of runlines, execfile that did raise, so
2055 # we could catch the errors.
1678 # we could catch the errors.
2056 self.runlines(script, clean=True)
1679 self.runlines(script, clean=True)
2057 except:
1680 except:
2058 self.showtraceback()
1681 self.showtraceback()
2059 warn('Unknown failure executing file: <%s>' % fname)
1682 warn('Unknown failure executing file: <%s>' % fname)
2060
2061 def _is_secondary_block_start(self, s):
2062 if not s.endswith(':'):
2063 return False
2064 if (s.startswith('elif') or
2065 s.startswith('else') or
2066 s.startswith('except') or
2067 s.startswith('finally')):
2068 return True
2069
2070 def cleanup_ipy_script(self, script):
2071 """Make a script safe for self.runlines()
2072
2073 Currently, IPython is lines based, with blocks being detected by
2074 empty lines. This is a problem for block based scripts that may
2075 not have empty lines after blocks. This script adds those empty
2076 lines to make scripts safe for running in the current line based
2077 IPython.
2078 """
2079 res = []
2080 lines = script.splitlines()
2081 level = 0
2082
2083 for l in lines:
2084 lstripped = l.lstrip()
2085 stripped = l.strip()
2086 if not stripped:
2087 continue
2088 newlevel = len(l) - len(lstripped)
2089 if level > 0 and newlevel == 0 and \
2090 not self._is_secondary_block_start(stripped):
2091 # add empty line
2092 res.append('')
2093 res.append(l)
2094 level = newlevel
2095
2096 return '\n'.join(res) + '\n'
2097
1683
2098 def runlines(self, lines, clean=False):
1684 def runlines(self, lines, clean=False):
2099 """Run a string of one or more lines of source.
1685 """Run a string of one or more lines of source.
2100
1686
2101 This method is capable of running a string containing multiple source
1687 This method is capable of running a string containing multiple source
2102 lines, as if they had been entered at the IPython prompt. Since it
1688 lines, as if they had been entered at the IPython prompt. Since it
2103 exposes IPython's processing machinery, the given strings can contain
1689 exposes IPython's processing machinery, the given strings can contain
2104 magic calls (%magic), special shell access (!cmd), etc.
1690 magic calls (%magic), special shell access (!cmd), etc.
2105 """
1691 """
2106
1692
2107 if isinstance(lines, (list, tuple)):
1693 if isinstance(lines, (list, tuple)):
2108 lines = '\n'.join(lines)
1694 lines = '\n'.join(lines)
2109
1695
2110 if clean:
1696 if clean:
2111 lines = self.cleanup_ipy_script(lines)
1697 lines = self._cleanup_ipy_script(lines)
2112
1698
2113 # We must start with a clean buffer, in case this is run from an
1699 # We must start with a clean buffer, in case this is run from an
2114 # interactive IPython session (via a magic, for example).
1700 # interactive IPython session (via a magic, for example).
2115 self.resetbuffer()
1701 self.resetbuffer()
2116 lines = lines.splitlines()
1702 lines = lines.splitlines()
2117 more = 0
1703 more = 0
2118
1704
2119 with nested(self.builtin_trap, self.display_trap):
1705 with nested(self.builtin_trap, self.display_trap):
2120 for line in lines:
1706 for line in lines:
2121 # skip blank lines so we don't mess up the prompt counter, but do
1707 # skip blank lines so we don't mess up the prompt counter, but do
2122 # NOT skip even a blank line if we are in a code block (more is
1708 # NOT skip even a blank line if we are in a code block (more is
2123 # true)
1709 # true)
2124
1710
2125 if line or more:
1711 if line or more:
2126 # push to raw history, so hist line numbers stay in sync
1712 # push to raw history, so hist line numbers stay in sync
2127 self.input_hist_raw.append("# " + line + "\n")
1713 self.input_hist_raw.append("# " + line + "\n")
2128 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
1714 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2129 more = self.push_line(prefiltered)
1715 more = self.push_line(prefiltered)
2130 # IPython's runsource returns None if there was an error
1716 # IPython's runsource returns None if there was an error
2131 # compiling the code. This allows us to stop processing right
1717 # compiling the code. This allows us to stop processing right
2132 # away, so the user gets the error message at the right place.
1718 # away, so the user gets the error message at the right place.
2133 if more is None:
1719 if more is None:
2134 break
1720 break
2135 else:
1721 else:
2136 self.input_hist_raw.append("\n")
1722 self.input_hist_raw.append("\n")
2137 # final newline in case the input didn't have it, so that the code
1723 # final newline in case the input didn't have it, so that the code
2138 # actually does get executed
1724 # actually does get executed
2139 if more:
1725 if more:
2140 self.push_line('\n')
1726 self.push_line('\n')
2141
1727
2142 def runsource(self, source, filename='<input>', symbol='single'):
1728 def runsource(self, source, filename='<input>', symbol='single'):
2143 """Compile and run some source in the interpreter.
1729 """Compile and run some source in the interpreter.
2144
1730
2145 Arguments are as for compile_command().
1731 Arguments are as for compile_command().
2146
1732
2147 One several things can happen:
1733 One several things can happen:
2148
1734
2149 1) The input is incorrect; compile_command() raised an
1735 1) The input is incorrect; compile_command() raised an
2150 exception (SyntaxError or OverflowError). A syntax traceback
1736 exception (SyntaxError or OverflowError). A syntax traceback
2151 will be printed by calling the showsyntaxerror() method.
1737 will be printed by calling the showsyntaxerror() method.
2152
1738
2153 2) The input is incomplete, and more input is required;
1739 2) The input is incomplete, and more input is required;
2154 compile_command() returned None. Nothing happens.
1740 compile_command() returned None. Nothing happens.
2155
1741
2156 3) The input is complete; compile_command() returned a code
1742 3) The input is complete; compile_command() returned a code
2157 object. The code is executed by calling self.runcode() (which
1743 object. The code is executed by calling self.runcode() (which
2158 also handles run-time exceptions, except for SystemExit).
1744 also handles run-time exceptions, except for SystemExit).
2159
1745
2160 The return value is:
1746 The return value is:
2161
1747
2162 - True in case 2
1748 - True in case 2
2163
1749
2164 - False in the other cases, unless an exception is raised, where
1750 - False in the other cases, unless an exception is raised, where
2165 None is returned instead. This can be used by external callers to
1751 None is returned instead. This can be used by external callers to
2166 know whether to continue feeding input or not.
1752 know whether to continue feeding input or not.
2167
1753
2168 The return value can be used to decide whether to use sys.ps1 or
1754 The return value can be used to decide whether to use sys.ps1 or
2169 sys.ps2 to prompt the next line."""
1755 sys.ps2 to prompt the next line."""
2170
1756
2171 # if the source code has leading blanks, add 'if 1:\n' to it
1757 # if the source code has leading blanks, add 'if 1:\n' to it
2172 # this allows execution of indented pasted code. It is tempting
1758 # this allows execution of indented pasted code. It is tempting
2173 # to add '\n' at the end of source to run commands like ' a=1'
1759 # to add '\n' at the end of source to run commands like ' a=1'
2174 # directly, but this fails for more complicated scenarios
1760 # directly, but this fails for more complicated scenarios
2175 source=source.encode(self.stdin_encoding)
1761 source=source.encode(self.stdin_encoding)
2176 if source[:1] in [' ', '\t']:
1762 if source[:1] in [' ', '\t']:
2177 source = 'if 1:\n%s' % source
1763 source = 'if 1:\n%s' % source
2178
1764
2179 try:
1765 try:
2180 code = self.compile(source,filename,symbol)
1766 code = self.compile(source,filename,symbol)
2181 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
1767 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2182 # Case 1
1768 # Case 1
2183 self.showsyntaxerror(filename)
1769 self.showsyntaxerror(filename)
2184 return None
1770 return None
2185
1771
2186 if code is None:
1772 if code is None:
2187 # Case 2
1773 # Case 2
2188 return True
1774 return True
2189
1775
2190 # Case 3
1776 # Case 3
2191 # We store the code object so that threaded shells and
1777 # We store the code object so that threaded shells and
2192 # custom exception handlers can access all this info if needed.
1778 # custom exception handlers can access all this info if needed.
2193 # The source corresponding to this can be obtained from the
1779 # The source corresponding to this can be obtained from the
2194 # buffer attribute as '\n'.join(self.buffer).
1780 # buffer attribute as '\n'.join(self.buffer).
2195 self.code_to_run = code
1781 self.code_to_run = code
2196 # now actually execute the code object
1782 # now actually execute the code object
2197 if self.runcode(code) == 0:
1783 if self.runcode(code) == 0:
2198 return False
1784 return False
2199 else:
1785 else:
2200 return None
1786 return None
2201
1787
2202 def runcode(self,code_obj):
1788 def runcode(self,code_obj):
2203 """Execute a code object.
1789 """Execute a code object.
2204
1790
2205 When an exception occurs, self.showtraceback() is called to display a
1791 When an exception occurs, self.showtraceback() is called to display a
2206 traceback.
1792 traceback.
2207
1793
2208 Return value: a flag indicating whether the code to be run completed
1794 Return value: a flag indicating whether the code to be run completed
2209 successfully:
1795 successfully:
2210
1796
2211 - 0: successful execution.
1797 - 0: successful execution.
2212 - 1: an error occurred.
1798 - 1: an error occurred.
2213 """
1799 """
2214
1800
2215 # Set our own excepthook in case the user code tries to call it
1801 # Set our own excepthook in case the user code tries to call it
2216 # directly, so that the IPython crash handler doesn't get triggered
1802 # directly, so that the IPython crash handler doesn't get triggered
2217 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1803 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2218
1804
2219 # we save the original sys.excepthook in the instance, in case config
1805 # we save the original sys.excepthook in the instance, in case config
2220 # code (such as magics) needs access to it.
1806 # code (such as magics) needs access to it.
2221 self.sys_excepthook = old_excepthook
1807 self.sys_excepthook = old_excepthook
2222 outflag = 1 # happens in more places, so it's easier as default
1808 outflag = 1 # happens in more places, so it's easier as default
2223 try:
1809 try:
2224 try:
1810 try:
2225 self.hooks.pre_runcode_hook()
1811 self.hooks.pre_runcode_hook()
2226 exec code_obj in self.user_global_ns, self.user_ns
1812 exec code_obj in self.user_global_ns, self.user_ns
2227 finally:
1813 finally:
2228 # Reset our crash handler in place
1814 # Reset our crash handler in place
2229 sys.excepthook = old_excepthook
1815 sys.excepthook = old_excepthook
2230 except SystemExit:
1816 except SystemExit:
2231 self.resetbuffer()
1817 self.resetbuffer()
2232 self.showtraceback(exception_only=True)
1818 self.showtraceback(exception_only=True)
2233 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
1819 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2234 except self.custom_exceptions:
1820 except self.custom_exceptions:
2235 etype,value,tb = sys.exc_info()
1821 etype,value,tb = sys.exc_info()
2236 self.CustomTB(etype,value,tb)
1822 self.CustomTB(etype,value,tb)
2237 except:
1823 except:
2238 self.showtraceback()
1824 self.showtraceback()
2239 else:
1825 else:
2240 outflag = 0
1826 outflag = 0
2241 if softspace(sys.stdout, 0):
1827 if softspace(sys.stdout, 0):
2242 print
1828 print
2243 # Flush out code object which has been run (and source)
1829 # Flush out code object which has been run (and source)
2244 self.code_to_run = None
1830 self.code_to_run = None
2245 return outflag
1831 return outflag
2246
1832
2247 def push_line(self, line):
1833 def push_line(self, line):
2248 """Push a line to the interpreter.
1834 """Push a line to the interpreter.
2249
1835
2250 The line should not have a trailing newline; it may have
1836 The line should not have a trailing newline; it may have
2251 internal newlines. The line is appended to a buffer and the
1837 internal newlines. The line is appended to a buffer and the
2252 interpreter's runsource() method is called with the
1838 interpreter's runsource() method is called with the
2253 concatenated contents of the buffer as source. If this
1839 concatenated contents of the buffer as source. If this
2254 indicates that the command was executed or invalid, the buffer
1840 indicates that the command was executed or invalid, the buffer
2255 is reset; otherwise, the command is incomplete, and the buffer
1841 is reset; otherwise, the command is incomplete, and the buffer
2256 is left as it was after the line was appended. The return
1842 is left as it was after the line was appended. The return
2257 value is 1 if more input is required, 0 if the line was dealt
1843 value is 1 if more input is required, 0 if the line was dealt
2258 with in some way (this is the same as runsource()).
1844 with in some way (this is the same as runsource()).
2259 """
1845 """
2260
1846
2261 # autoindent management should be done here, and not in the
1847 # autoindent management should be done here, and not in the
2262 # interactive loop, since that one is only seen by keyboard input. We
1848 # interactive loop, since that one is only seen by keyboard input. We
2263 # need this done correctly even for code run via runlines (which uses
1849 # need this done correctly even for code run via runlines (which uses
2264 # push).
1850 # push).
2265
1851
2266 #print 'push line: <%s>' % line # dbg
1852 #print 'push line: <%s>' % line # dbg
2267 for subline in line.splitlines():
1853 for subline in line.splitlines():
2268 self._autoindent_update(subline)
1854 self._autoindent_update(subline)
2269 self.buffer.append(line)
1855 self.buffer.append(line)
2270 more = self.runsource('\n'.join(self.buffer), self.filename)
1856 more = self.runsource('\n'.join(self.buffer), self.filename)
2271 if not more:
1857 if not more:
2272 self.resetbuffer()
1858 self.resetbuffer()
2273 return more
1859 return more
2274
1860
1861 def resetbuffer(self):
1862 """Reset the input buffer."""
1863 self.buffer[:] = []
1864
1865 def _is_secondary_block_start(self, s):
1866 if not s.endswith(':'):
1867 return False
1868 if (s.startswith('elif') or
1869 s.startswith('else') or
1870 s.startswith('except') or
1871 s.startswith('finally')):
1872 return True
1873
1874 def _cleanup_ipy_script(self, script):
1875 """Make a script safe for self.runlines()
1876
1877 Currently, IPython is lines based, with blocks being detected by
1878 empty lines. This is a problem for block based scripts that may
1879 not have empty lines after blocks. This script adds those empty
1880 lines to make scripts safe for running in the current line based
1881 IPython.
1882 """
1883 res = []
1884 lines = script.splitlines()
1885 level = 0
1886
1887 for l in lines:
1888 lstripped = l.lstrip()
1889 stripped = l.strip()
1890 if not stripped:
1891 continue
1892 newlevel = len(l) - len(lstripped)
1893 if level > 0 and newlevel == 0 and \
1894 not self._is_secondary_block_start(stripped):
1895 # add empty line
1896 res.append('')
1897 res.append(l)
1898 level = newlevel
1899
1900 return '\n'.join(res) + '\n'
1901
2275 def _autoindent_update(self,line):
1902 def _autoindent_update(self,line):
2276 """Keep track of the indent level."""
1903 """Keep track of the indent level."""
2277
1904
2278 #debugx('line')
1905 #debugx('line')
2279 #debugx('self.indent_current_nsp')
1906 #debugx('self.indent_current_nsp')
2280 if self.autoindent:
1907 if self.autoindent:
2281 if line:
1908 if line:
2282 inisp = num_ini_spaces(line)
1909 inisp = num_ini_spaces(line)
2283 if inisp < self.indent_current_nsp:
1910 if inisp < self.indent_current_nsp:
2284 self.indent_current_nsp = inisp
1911 self.indent_current_nsp = inisp
2285
1912
2286 if line[-1] == ':':
1913 if line[-1] == ':':
2287 self.indent_current_nsp += 4
1914 self.indent_current_nsp += 4
2288 elif dedent_re.match(line):
1915 elif dedent_re.match(line):
2289 self.indent_current_nsp -= 4
1916 self.indent_current_nsp -= 4
2290 else:
1917 else:
2291 self.indent_current_nsp = 0
1918 self.indent_current_nsp = 0
2292
1919
2293 def resetbuffer(self):
2294 """Reset the input buffer."""
2295 self.buffer[:] = []
2296
2297 def raw_input(self,prompt='',continue_prompt=False):
2298 """Write a prompt and read a line.
2299
2300 The returned line does not include the trailing newline.
2301 When the user enters the EOF key sequence, EOFError is raised.
2302
2303 Optional inputs:
2304
2305 - prompt(''): a string to be printed to prompt the user.
2306
2307 - continue_prompt(False): whether this line is the first one or a
2308 continuation in a sequence of inputs.
2309 """
2310 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2311
2312 # Code run by the user may have modified the readline completer state.
2313 # We must ensure that our completer is back in place.
2314
2315 if self.has_readline:
2316 self.set_completer()
2317
2318 try:
2319 line = raw_input_original(prompt).decode(self.stdin_encoding)
2320 except ValueError:
2321 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2322 " or sys.stdout.close()!\nExiting IPython!")
2323 self.ask_exit()
2324 return ""
2325
2326 # Try to be reasonably smart about not re-indenting pasted input more
2327 # than necessary. We do this by trimming out the auto-indent initial
2328 # spaces, if the user's actual input started itself with whitespace.
2329 #debugx('self.buffer[-1]')
2330
2331 if self.autoindent:
2332 if num_ini_spaces(line) > self.indent_current_nsp:
2333 line = line[self.indent_current_nsp:]
2334 self.indent_current_nsp = 0
2335
2336 # store the unfiltered input before the user has any chance to modify
2337 # it.
2338 if line.strip():
2339 if continue_prompt:
2340 self.input_hist_raw[-1] += '%s\n' % line
2341 if self.has_readline and self.readline_use:
2342 try:
2343 histlen = self.readline.get_current_history_length()
2344 if histlen > 1:
2345 newhist = self.input_hist_raw[-1].rstrip()
2346 self.readline.remove_history_item(histlen-1)
2347 self.readline.replace_history_item(histlen-2,
2348 newhist.encode(self.stdin_encoding))
2349 except AttributeError:
2350 pass # re{move,place}_history_item are new in 2.4.
2351 else:
2352 self.input_hist_raw.append('%s\n' % line)
2353 # only entries starting at first column go to shadow history
2354 if line.lstrip() == line:
2355 self.shadowhist.add(line.strip())
2356 elif not continue_prompt:
2357 self.input_hist_raw.append('\n')
2358 try:
2359 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2360 except:
2361 # blanket except, in case a user-defined prefilter crashes, so it
2362 # can't take all of ipython with it.
2363 self.showtraceback()
2364 return ''
2365 else:
2366 return lineout
2367
2368 #-------------------------------------------------------------------------
1920 #-------------------------------------------------------------------------
2369 # Things related to the prefilter
1921 # Things related to GUI support and pylab
2370 #-------------------------------------------------------------------------
1922 #-------------------------------------------------------------------------
2371
1923
2372 def init_prefilter(self):
1924 def enable_pylab(self, gui=None):
2373 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1925 raise NotImplementedError('Implement enable_pylab in a subclass')
2374 # Ultimately this will be refactored in the new interpreter code, but
2375 # for now, we should expose the main prefilter method (there's legacy
2376 # code out there that may rely on this).
2377 self.prefilter = self.prefilter_manager.prefilter_lines
2378
1926
2379 #-------------------------------------------------------------------------
1927 #-------------------------------------------------------------------------
2380 # Utilities
1928 # Utilities
2381 #-------------------------------------------------------------------------
1929 #-------------------------------------------------------------------------
2382
1930
2383 def getoutput(self, cmd):
1931 def getoutput(self, cmd):
2384 return getoutput(self.var_expand(cmd,depth=2),
1932 return getoutput(self.var_expand(cmd,depth=2),
2385 header=self.system_header,
1933 header=self.system_header,
2386 verbose=self.system_verbose)
1934 verbose=self.system_verbose)
2387
1935
2388 def getoutputerror(self, cmd):
1936 def getoutputerror(self, cmd):
2389 return getoutputerror(self.var_expand(cmd,depth=2),
1937 return getoutputerror(self.var_expand(cmd,depth=2),
2390 header=self.system_header,
1938 header=self.system_header,
2391 verbose=self.system_verbose)
1939 verbose=self.system_verbose)
2392
1940
2393 def var_expand(self,cmd,depth=0):
1941 def var_expand(self,cmd,depth=0):
2394 """Expand python variables in a string.
1942 """Expand python variables in a string.
2395
1943
2396 The depth argument indicates how many frames above the caller should
1944 The depth argument indicates how many frames above the caller should
2397 be walked to look for the local namespace where to expand variables.
1945 be walked to look for the local namespace where to expand variables.
2398
1946
2399 The global namespace for expansion is always the user's interactive
1947 The global namespace for expansion is always the user's interactive
2400 namespace.
1948 namespace.
2401 """
1949 """
2402
1950
2403 return str(ItplNS(cmd,
1951 return str(ItplNS(cmd,
2404 self.user_ns, # globals
1952 self.user_ns, # globals
2405 # Skip our own frame in searching for locals:
1953 # Skip our own frame in searching for locals:
2406 sys._getframe(depth+1).f_locals # locals
1954 sys._getframe(depth+1).f_locals # locals
2407 ))
1955 ))
2408
1956
2409 def mktempfile(self,data=None):
1957 def mktempfile(self,data=None):
2410 """Make a new tempfile and return its filename.
1958 """Make a new tempfile and return its filename.
2411
1959
2412 This makes a call to tempfile.mktemp, but it registers the created
1960 This makes a call to tempfile.mktemp, but it registers the created
2413 filename internally so ipython cleans it up at exit time.
1961 filename internally so ipython cleans it up at exit time.
2414
1962
2415 Optional inputs:
1963 Optional inputs:
2416
1964
2417 - data(None): if data is given, it gets written out to the temp file
1965 - data(None): if data is given, it gets written out to the temp file
2418 immediately, and the file is closed again."""
1966 immediately, and the file is closed again."""
2419
1967
2420 filename = tempfile.mktemp('.py','ipython_edit_')
1968 filename = tempfile.mktemp('.py','ipython_edit_')
2421 self.tempfiles.append(filename)
1969 self.tempfiles.append(filename)
2422
1970
2423 if data:
1971 if data:
2424 tmp_file = open(filename,'w')
1972 tmp_file = open(filename,'w')
2425 tmp_file.write(data)
1973 tmp_file.write(data)
2426 tmp_file.close()
1974 tmp_file.close()
2427 return filename
1975 return filename
2428
1976
1977 # TODO: This should be removed when Term is refactored.
2429 def write(self,data):
1978 def write(self,data):
2430 """Write a string to the default output"""
1979 """Write a string to the default output"""
2431 Term.cout.write(data)
1980 Term.cout.write(data)
2432
1981
1982 # TODO: This should be removed when Term is refactored.
2433 def write_err(self,data):
1983 def write_err(self,data):
2434 """Write a string to the default error output"""
1984 """Write a string to the default error output"""
2435 Term.cerr.write(data)
1985 Term.cerr.write(data)
2436
1986
2437 def ask_yes_no(self,prompt,default=True):
1987 def ask_yes_no(self,prompt,default=True):
2438 if self.quiet:
1988 if self.quiet:
2439 return True
1989 return True
2440 return ask_yes_no(prompt,default)
1990 return ask_yes_no(prompt,default)
2441
1991
2442 #-------------------------------------------------------------------------
1992 #-------------------------------------------------------------------------
2443 # Things related to GUI support and pylab
2444 #-------------------------------------------------------------------------
2445
2446 def enable_pylab(self, gui=None):
2447 """Activate pylab support at runtime.
2448
2449 This turns on support for matplotlib, preloads into the interactive
2450 namespace all of numpy and pylab, and configures IPython to correcdtly
2451 interact with the GUI event loop. The GUI backend to be used can be
2452 optionally selected with the optional :param:`gui` argument.
2453
2454 Parameters
2455 ----------
2456 gui : optional, string
2457
2458 If given, dictates the choice of matplotlib GUI backend to use
2459 (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or
2460 'gtk'), otherwise we use the default chosen by matplotlib (as
2461 dictated by the matplotlib build-time options plus the user's
2462 matplotlibrc configuration file).
2463 """
2464 # We want to prevent the loading of pylab to pollute the user's
2465 # namespace as shown by the %who* magics, so we execute the activation
2466 # code in an empty namespace, and we update *both* user_ns and
2467 # user_ns_hidden with this information.
2468 ns = {}
2469 gui = pylab_activate(ns, gui)
2470 self.user_ns.update(ns)
2471 self.user_ns_hidden.update(ns)
2472 # Now we must activate the gui pylab wants to use, and fix %run to take
2473 # plot updates into account
2474 enable_gui(gui)
2475 self.magic_run = self._pylab_magic_run
2476
2477 #-------------------------------------------------------------------------
2478 # Things related to IPython exiting
1993 # Things related to IPython exiting
2479 #-------------------------------------------------------------------------
1994 #-------------------------------------------------------------------------
2480
1995
2481 def ask_exit(self):
2482 """ Ask the shell to exit. Can be overiden and used as a callback. """
2483 self.exit_now = True
2484
2485 def exit(self):
2486 """Handle interactive exit.
2487
2488 This method calls the ask_exit callback."""
2489 if self.confirm_exit:
2490 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2491 self.ask_exit()
2492 else:
2493 self.ask_exit()
2494
2495 def atexit_operations(self):
1996 def atexit_operations(self):
2496 """This will be executed at the time of exit.
1997 """This will be executed at the time of exit.
2497
1998
2498 Saving of persistent data should be performed here.
1999 Saving of persistent data should be performed here.
2499 """
2000 """
2500 self.savehist()
2001 self.savehist()
2501
2002
2502 # Cleanup all tempfiles left around
2003 # Cleanup all tempfiles left around
2503 for tfile in self.tempfiles:
2004 for tfile in self.tempfiles:
2504 try:
2005 try:
2505 os.unlink(tfile)
2006 os.unlink(tfile)
2506 except OSError:
2007 except OSError:
2507 pass
2008 pass
2508
2009
2509 # Clear all user namespaces to release all references cleanly.
2010 # Clear all user namespaces to release all references cleanly.
2510 self.reset()
2011 self.reset()
2511
2012
2512 # Run user hooks
2013 # Run user hooks
2513 self.hooks.shutdown_hook()
2014 self.hooks.shutdown_hook()
2514
2015
2515 def cleanup(self):
2016 def cleanup(self):
2516 self.restore_sys_module_state()
2017 self.restore_sys_module_state()
2517
2018
2518
2019
2519 class InteractiveShellABC(object):
2020 class InteractiveShellABC(object):
2520 """An abstract base class for InteractiveShell."""
2021 """An abstract base class for InteractiveShell."""
2521 __metaclass__ = abc.ABCMeta
2022 __metaclass__ = abc.ABCMeta
2522
2023
2523 InteractiveShellABC.register(InteractiveShell)
2024 InteractiveShellABC.register(InteractiveShell)
@@ -1,30 +1,30 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 This module is *completely* deprecated and should no longer be used for
4 This module is *completely* deprecated and should no longer be used for
5 any purpose. Currently, we have a few parts of the core that have
5 any purpose. Currently, we have a few parts of the core that have
6 not been componentized and thus, still rely on this module. When everything
6 not been componentized and thus, still rely on this module. When everything
7 has been made into a component, this module will be sent to deathrow.
7 has been made into a component, this module will be sent to deathrow.
8 """
8 """
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2008-2009 The IPython Development Team
11 # Copyright (C) 2008-2009 The IPython Development Team
12 #
12 #
13 # Distributed under the terms of the BSD License. The full license is in
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Imports
18 # Imports
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Classes and functions
22 # Classes and functions
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25
25
26 def get():
26 def get():
27 """Get the global InteractiveShell instance."""
27 """Get the global InteractiveShell instance."""
28 from IPython.core.interactiveshell import InteractiveShell
28 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
29 return InteractiveShell.instance()
29 return TerminalInteractiveShell.instance()
30
30
@@ -1,3708 +1,3651 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
7 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2008-2009 The IPython Development Team
8 # Copyright (C) 2008-2009 The IPython Development Team
9
9
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 import __builtin__
18 import __builtin__
19 import __future__
19 import __future__
20 import bdb
20 import bdb
21 import inspect
21 import inspect
22 import os
22 import os
23 import sys
23 import sys
24 import shutil
24 import shutil
25 import re
25 import re
26 import time
26 import time
27 import textwrap
27 import textwrap
28 import types
28 import types
29 from cStringIO import StringIO
29 from cStringIO import StringIO
30 from getopt import getopt,GetoptError
30 from getopt import getopt,GetoptError
31 from pprint import pformat
31 from pprint import pformat
32
32
33 # cProfile was added in Python2.5
33 # cProfile was added in Python2.5
34 try:
34 try:
35 import cProfile as profile
35 import cProfile as profile
36 import pstats
36 import pstats
37 except ImportError:
37 except ImportError:
38 # profile isn't bundled by default in Debian for license reasons
38 # profile isn't bundled by default in Debian for license reasons
39 try:
39 try:
40 import profile,pstats
40 import profile,pstats
41 except ImportError:
41 except ImportError:
42 profile = pstats = None
42 profile = pstats = None
43
43
44 # print_function was added to __future__ in Python2.6, remove this when we drop
44 # print_function was added to __future__ in Python2.6, remove this when we drop
45 # 2.5 compatibility
45 # 2.5 compatibility
46 if not hasattr(__future__,'CO_FUTURE_PRINT_FUNCTION'):
46 if not hasattr(__future__,'CO_FUTURE_PRINT_FUNCTION'):
47 __future__.CO_FUTURE_PRINT_FUNCTION = 65536
47 __future__.CO_FUTURE_PRINT_FUNCTION = 65536
48
48
49 import IPython
49 import IPython
50 from IPython.core import debugger, oinspect
50 from IPython.core import debugger, oinspect
51 from IPython.core.error import TryNext
51 from IPython.core.error import TryNext
52 from IPython.core.error import UsageError
52 from IPython.core.error import UsageError
53 from IPython.core.fakemodule import FakeModule
53 from IPython.core.fakemodule import FakeModule
54 from IPython.core.macro import Macro
54 from IPython.core.macro import Macro
55 from IPython.core.page import page
55 from IPython.core.page import page
56 from IPython.core.prefilter import ESC_MAGIC
56 from IPython.core.prefilter import ESC_MAGIC
57 from IPython.lib.pylabtools import mpl_runner
57 from IPython.lib.pylabtools import mpl_runner
58 from IPython.lib.inputhook import enable_gui
58 from IPython.lib.inputhook import enable_gui
59 from IPython.external.Itpl import itpl, printpl
59 from IPython.external.Itpl import itpl, printpl
60 from IPython.testing import decorators as testdec
60 from IPython.testing import decorators as testdec
61 from IPython.utils.io import Term, file_read, nlprint
61 from IPython.utils.io import Term, file_read, nlprint
62 from IPython.utils.path import get_py_filename
62 from IPython.utils.path import get_py_filename
63 from IPython.utils.process import arg_split, abbrev_cwd
63 from IPython.utils.process import arg_split, abbrev_cwd
64 from IPython.utils.terminal import set_term_title
64 from IPython.utils.terminal import set_term_title
65 from IPython.utils.text import LSString, SList, StringTypes
65 from IPython.utils.text import LSString, SList, StringTypes
66 from IPython.utils.timing import clock, clock2
66 from IPython.utils.timing import clock, clock2
67 from IPython.utils.warn import warn, error
67 from IPython.utils.warn import warn, error
68 from IPython.utils.ipstruct import Struct
68 from IPython.utils.ipstruct import Struct
69 import IPython.utils.generics
69 import IPython.utils.generics
70
70
71 #-----------------------------------------------------------------------------
71 #-----------------------------------------------------------------------------
72 # Utility functions
72 # Utility functions
73 #-----------------------------------------------------------------------------
73 #-----------------------------------------------------------------------------
74
74
75 def on_off(tag):
75 def on_off(tag):
76 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
76 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
77 return ['OFF','ON'][tag]
77 return ['OFF','ON'][tag]
78
78
79 class Bunch: pass
79 class Bunch: pass
80
80
81 def compress_dhist(dh):
81 def compress_dhist(dh):
82 head, tail = dh[:-10], dh[-10:]
82 head, tail = dh[:-10], dh[-10:]
83
83
84 newhead = []
84 newhead = []
85 done = set()
85 done = set()
86 for h in head:
86 for h in head:
87 if h in done:
87 if h in done:
88 continue
88 continue
89 newhead.append(h)
89 newhead.append(h)
90 done.add(h)
90 done.add(h)
91
91
92 return newhead + tail
92 return newhead + tail
93
93
94
94
95 #***************************************************************************
95 #***************************************************************************
96 # Main class implementing Magic functionality
96 # Main class implementing Magic functionality
97
97
98 # XXX - for some odd reason, if Magic is made a new-style class, we get errors
98 # XXX - for some odd reason, if Magic is made a new-style class, we get errors
99 # on construction of the main InteractiveShell object. Something odd is going
99 # on construction of the main InteractiveShell object. Something odd is going
100 # on with super() calls, Configurable and the MRO... For now leave it as-is, but
100 # on with super() calls, Configurable and the MRO... For now leave it as-is, but
101 # eventually this needs to be clarified.
101 # eventually this needs to be clarified.
102 # BG: This is because InteractiveShell inherits from this, but is itself a
102 # BG: This is because InteractiveShell inherits from this, but is itself a
103 # Configurable. This messes up the MRO in some way. The fix is that we need to
103 # Configurable. This messes up the MRO in some way. The fix is that we need to
104 # make Magic a configurable that InteractiveShell does not subclass.
104 # make Magic a configurable that InteractiveShell does not subclass.
105
105
106 class Magic:
106 class Magic:
107 """Magic functions for InteractiveShell.
107 """Magic functions for InteractiveShell.
108
108
109 Shell functions which can be reached as %function_name. All magic
109 Shell functions which can be reached as %function_name. All magic
110 functions should accept a string, which they can parse for their own
110 functions should accept a string, which they can parse for their own
111 needs. This can make some functions easier to type, eg `%cd ../`
111 needs. This can make some functions easier to type, eg `%cd ../`
112 vs. `%cd("../")`
112 vs. `%cd("../")`
113
113
114 ALL definitions MUST begin with the prefix magic_. The user won't need it
114 ALL definitions MUST begin with the prefix magic_. The user won't need it
115 at the command line, but it is is needed in the definition. """
115 at the command line, but it is is needed in the definition. """
116
116
117 # class globals
117 # class globals
118 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
118 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
119 'Automagic is ON, % prefix NOT needed for magic functions.']
119 'Automagic is ON, % prefix NOT needed for magic functions.']
120
120
121 #......................................................................
121 #......................................................................
122 # some utility functions
122 # some utility functions
123
123
124 def __init__(self,shell):
124 def __init__(self,shell):
125
125
126 self.options_table = {}
126 self.options_table = {}
127 if profile is None:
127 if profile is None:
128 self.magic_prun = self.profile_missing_notice
128 self.magic_prun = self.profile_missing_notice
129 self.shell = shell
129 self.shell = shell
130
130
131 # namespace for holding state we may need
131 # namespace for holding state we may need
132 self._magic_state = Bunch()
132 self._magic_state = Bunch()
133
133
134 def profile_missing_notice(self, *args, **kwargs):
134 def profile_missing_notice(self, *args, **kwargs):
135 error("""\
135 error("""\
136 The profile module could not be found. It has been removed from the standard
136 The profile module could not be found. It has been removed from the standard
137 python packages because of its non-free license. To use profiling, install the
137 python packages because of its non-free license. To use profiling, install the
138 python-profiler package from non-free.""")
138 python-profiler package from non-free.""")
139
139
140 def default_option(self,fn,optstr):
140 def default_option(self,fn,optstr):
141 """Make an entry in the options_table for fn, with value optstr"""
141 """Make an entry in the options_table for fn, with value optstr"""
142
142
143 if fn not in self.lsmagic():
143 if fn not in self.lsmagic():
144 error("%s is not a magic function" % fn)
144 error("%s is not a magic function" % fn)
145 self.options_table[fn] = optstr
145 self.options_table[fn] = optstr
146
146
147 def lsmagic(self):
147 def lsmagic(self):
148 """Return a list of currently available magic functions.
148 """Return a list of currently available magic functions.
149
149
150 Gives a list of the bare names after mangling (['ls','cd', ...], not
150 Gives a list of the bare names after mangling (['ls','cd', ...], not
151 ['magic_ls','magic_cd',...]"""
151 ['magic_ls','magic_cd',...]"""
152
152
153 # FIXME. This needs a cleanup, in the way the magics list is built.
153 # FIXME. This needs a cleanup, in the way the magics list is built.
154
154
155 # magics in class definition
155 # magics in class definition
156 class_magic = lambda fn: fn.startswith('magic_') and \
156 class_magic = lambda fn: fn.startswith('magic_') and \
157 callable(Magic.__dict__[fn])
157 callable(Magic.__dict__[fn])
158 # in instance namespace (run-time user additions)
158 # in instance namespace (run-time user additions)
159 inst_magic = lambda fn: fn.startswith('magic_') and \
159 inst_magic = lambda fn: fn.startswith('magic_') and \
160 callable(self.__dict__[fn])
160 callable(self.__dict__[fn])
161 # and bound magics by user (so they can access self):
161 # and bound magics by user (so they can access self):
162 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
162 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
163 callable(self.__class__.__dict__[fn])
163 callable(self.__class__.__dict__[fn])
164 magics = filter(class_magic,Magic.__dict__.keys()) + \
164 magics = filter(class_magic,Magic.__dict__.keys()) + \
165 filter(inst_magic,self.__dict__.keys()) + \
165 filter(inst_magic,self.__dict__.keys()) + \
166 filter(inst_bound_magic,self.__class__.__dict__.keys())
166 filter(inst_bound_magic,self.__class__.__dict__.keys())
167 out = []
167 out = []
168 for fn in set(magics):
168 for fn in set(magics):
169 out.append(fn.replace('magic_','',1))
169 out.append(fn.replace('magic_','',1))
170 out.sort()
170 out.sort()
171 return out
171 return out
172
172
173 def extract_input_slices(self,slices,raw=False):
173 def extract_input_slices(self,slices,raw=False):
174 """Return as a string a set of input history slices.
174 """Return as a string a set of input history slices.
175
175
176 Inputs:
176 Inputs:
177
177
178 - slices: the set of slices is given as a list of strings (like
178 - slices: the set of slices is given as a list of strings (like
179 ['1','4:8','9'], since this function is for use by magic functions
179 ['1','4:8','9'], since this function is for use by magic functions
180 which get their arguments as strings.
180 which get their arguments as strings.
181
181
182 Optional inputs:
182 Optional inputs:
183
183
184 - raw(False): by default, the processed input is used. If this is
184 - raw(False): by default, the processed input is used. If this is
185 true, the raw input history is used instead.
185 true, the raw input history is used instead.
186
186
187 Note that slices can be called with two notations:
187 Note that slices can be called with two notations:
188
188
189 N:M -> standard python form, means including items N...(M-1).
189 N:M -> standard python form, means including items N...(M-1).
190
190
191 N-M -> include items N..M (closed endpoint)."""
191 N-M -> include items N..M (closed endpoint)."""
192
192
193 if raw:
193 if raw:
194 hist = self.shell.input_hist_raw
194 hist = self.shell.input_hist_raw
195 else:
195 else:
196 hist = self.shell.input_hist
196 hist = self.shell.input_hist
197
197
198 cmds = []
198 cmds = []
199 for chunk in slices:
199 for chunk in slices:
200 if ':' in chunk:
200 if ':' in chunk:
201 ini,fin = map(int,chunk.split(':'))
201 ini,fin = map(int,chunk.split(':'))
202 elif '-' in chunk:
202 elif '-' in chunk:
203 ini,fin = map(int,chunk.split('-'))
203 ini,fin = map(int,chunk.split('-'))
204 fin += 1
204 fin += 1
205 else:
205 else:
206 ini = int(chunk)
206 ini = int(chunk)
207 fin = ini+1
207 fin = ini+1
208 cmds.append(hist[ini:fin])
208 cmds.append(hist[ini:fin])
209 return cmds
209 return cmds
210
210
211 def _ofind(self, oname, namespaces=None):
211 def _ofind(self, oname, namespaces=None):
212 """Find an object in the available namespaces.
212 """Find an object in the available namespaces.
213
213
214 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
214 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
215
215
216 Has special code to detect magic functions.
216 Has special code to detect magic functions.
217 """
217 """
218 oname = oname.strip()
218 oname = oname.strip()
219 alias_ns = None
219 alias_ns = None
220 if namespaces is None:
220 if namespaces is None:
221 # Namespaces to search in:
221 # Namespaces to search in:
222 # Put them in a list. The order is important so that we
222 # Put them in a list. The order is important so that we
223 # find things in the same order that Python finds them.
223 # find things in the same order that Python finds them.
224 namespaces = [ ('Interactive', self.shell.user_ns),
224 namespaces = [ ('Interactive', self.shell.user_ns),
225 ('IPython internal', self.shell.internal_ns),
225 ('IPython internal', self.shell.internal_ns),
226 ('Python builtin', __builtin__.__dict__),
226 ('Python builtin', __builtin__.__dict__),
227 ('Alias', self.shell.alias_manager.alias_table),
227 ('Alias', self.shell.alias_manager.alias_table),
228 ]
228 ]
229 alias_ns = self.shell.alias_manager.alias_table
229 alias_ns = self.shell.alias_manager.alias_table
230
230
231 # initialize results to 'null'
231 # initialize results to 'null'
232 found = False; obj = None; ospace = None; ds = None;
232 found = False; obj = None; ospace = None; ds = None;
233 ismagic = False; isalias = False; parent = None
233 ismagic = False; isalias = False; parent = None
234
234
235 # We need to special-case 'print', which as of python2.6 registers as a
235 # We need to special-case 'print', which as of python2.6 registers as a
236 # function but should only be treated as one if print_function was
236 # function but should only be treated as one if print_function was
237 # loaded with a future import. In this case, just bail.
237 # loaded with a future import. In this case, just bail.
238 if (oname == 'print' and not (self.shell.compile.compiler.flags &
238 if (oname == 'print' and not (self.shell.compile.compiler.flags &
239 __future__.CO_FUTURE_PRINT_FUNCTION)):
239 __future__.CO_FUTURE_PRINT_FUNCTION)):
240 return {'found':found, 'obj':obj, 'namespace':ospace,
240 return {'found':found, 'obj':obj, 'namespace':ospace,
241 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
241 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
242
242
243 # Look for the given name by splitting it in parts. If the head is
243 # Look for the given name by splitting it in parts. If the head is
244 # found, then we look for all the remaining parts as members, and only
244 # found, then we look for all the remaining parts as members, and only
245 # declare success if we can find them all.
245 # declare success if we can find them all.
246 oname_parts = oname.split('.')
246 oname_parts = oname.split('.')
247 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
247 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
248 for nsname,ns in namespaces:
248 for nsname,ns in namespaces:
249 try:
249 try:
250 obj = ns[oname_head]
250 obj = ns[oname_head]
251 except KeyError:
251 except KeyError:
252 continue
252 continue
253 else:
253 else:
254 #print 'oname_rest:', oname_rest # dbg
254 #print 'oname_rest:', oname_rest # dbg
255 for part in oname_rest:
255 for part in oname_rest:
256 try:
256 try:
257 parent = obj
257 parent = obj
258 obj = getattr(obj,part)
258 obj = getattr(obj,part)
259 except:
259 except:
260 # Blanket except b/c some badly implemented objects
260 # Blanket except b/c some badly implemented objects
261 # allow __getattr__ to raise exceptions other than
261 # allow __getattr__ to raise exceptions other than
262 # AttributeError, which then crashes IPython.
262 # AttributeError, which then crashes IPython.
263 break
263 break
264 else:
264 else:
265 # If we finish the for loop (no break), we got all members
265 # If we finish the for loop (no break), we got all members
266 found = True
266 found = True
267 ospace = nsname
267 ospace = nsname
268 if ns == alias_ns:
268 if ns == alias_ns:
269 isalias = True
269 isalias = True
270 break # namespace loop
270 break # namespace loop
271
271
272 # Try to see if it's magic
272 # Try to see if it's magic
273 if not found:
273 if not found:
274 if oname.startswith(ESC_MAGIC):
274 if oname.startswith(ESC_MAGIC):
275 oname = oname[1:]
275 oname = oname[1:]
276 obj = getattr(self,'magic_'+oname,None)
276 obj = getattr(self,'magic_'+oname,None)
277 if obj is not None:
277 if obj is not None:
278 found = True
278 found = True
279 ospace = 'IPython internal'
279 ospace = 'IPython internal'
280 ismagic = True
280 ismagic = True
281
281
282 # Last try: special-case some literals like '', [], {}, etc:
282 # Last try: special-case some literals like '', [], {}, etc:
283 if not found and oname_head in ["''",'""','[]','{}','()']:
283 if not found and oname_head in ["''",'""','[]','{}','()']:
284 obj = eval(oname_head)
284 obj = eval(oname_head)
285 found = True
285 found = True
286 ospace = 'Interactive'
286 ospace = 'Interactive'
287
287
288 return {'found':found, 'obj':obj, 'namespace':ospace,
288 return {'found':found, 'obj':obj, 'namespace':ospace,
289 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
289 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
290
290
291 def arg_err(self,func):
291 def arg_err(self,func):
292 """Print docstring if incorrect arguments were passed"""
292 """Print docstring if incorrect arguments were passed"""
293 print 'Error in arguments:'
293 print 'Error in arguments:'
294 print oinspect.getdoc(func)
294 print oinspect.getdoc(func)
295
295
296 def format_latex(self,strng):
296 def format_latex(self,strng):
297 """Format a string for latex inclusion."""
297 """Format a string for latex inclusion."""
298
298
299 # Characters that need to be escaped for latex:
299 # Characters that need to be escaped for latex:
300 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
300 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
301 # Magic command names as headers:
301 # Magic command names as headers:
302 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
302 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
303 re.MULTILINE)
303 re.MULTILINE)
304 # Magic commands
304 # Magic commands
305 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
305 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
306 re.MULTILINE)
306 re.MULTILINE)
307 # Paragraph continue
307 # Paragraph continue
308 par_re = re.compile(r'\\$',re.MULTILINE)
308 par_re = re.compile(r'\\$',re.MULTILINE)
309
309
310 # The "\n" symbol
310 # The "\n" symbol
311 newline_re = re.compile(r'\\n')
311 newline_re = re.compile(r'\\n')
312
312
313 # Now build the string for output:
313 # Now build the string for output:
314 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
314 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
315 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
315 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
316 strng)
316 strng)
317 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
317 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
318 strng = par_re.sub(r'\\\\',strng)
318 strng = par_re.sub(r'\\\\',strng)
319 strng = escape_re.sub(r'\\\1',strng)
319 strng = escape_re.sub(r'\\\1',strng)
320 strng = newline_re.sub(r'\\textbackslash{}n',strng)
320 strng = newline_re.sub(r'\\textbackslash{}n',strng)
321 return strng
321 return strng
322
322
323 def format_screen(self,strng):
323 def format_screen(self,strng):
324 """Format a string for screen printing.
324 """Format a string for screen printing.
325
325
326 This removes some latex-type format codes."""
326 This removes some latex-type format codes."""
327 # Paragraph continue
327 # Paragraph continue
328 par_re = re.compile(r'\\$',re.MULTILINE)
328 par_re = re.compile(r'\\$',re.MULTILINE)
329 strng = par_re.sub('',strng)
329 strng = par_re.sub('',strng)
330 return strng
330 return strng
331
331
332 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
332 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
333 """Parse options passed to an argument string.
333 """Parse options passed to an argument string.
334
334
335 The interface is similar to that of getopt(), but it returns back a
335 The interface is similar to that of getopt(), but it returns back a
336 Struct with the options as keys and the stripped argument string still
336 Struct with the options as keys and the stripped argument string still
337 as a string.
337 as a string.
338
338
339 arg_str is quoted as a true sys.argv vector by using shlex.split.
339 arg_str is quoted as a true sys.argv vector by using shlex.split.
340 This allows us to easily expand variables, glob files, quote
340 This allows us to easily expand variables, glob files, quote
341 arguments, etc.
341 arguments, etc.
342
342
343 Options:
343 Options:
344 -mode: default 'string'. If given as 'list', the argument string is
344 -mode: default 'string'. If given as 'list', the argument string is
345 returned as a list (split on whitespace) instead of a string.
345 returned as a list (split on whitespace) instead of a string.
346
346
347 -list_all: put all option values in lists. Normally only options
347 -list_all: put all option values in lists. Normally only options
348 appearing more than once are put in a list.
348 appearing more than once are put in a list.
349
349
350 -posix (True): whether to split the input line in POSIX mode or not,
350 -posix (True): whether to split the input line in POSIX mode or not,
351 as per the conventions outlined in the shlex module from the
351 as per the conventions outlined in the shlex module from the
352 standard library."""
352 standard library."""
353
353
354 # inject default options at the beginning of the input line
354 # inject default options at the beginning of the input line
355 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
355 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
356 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
356 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
357
357
358 mode = kw.get('mode','string')
358 mode = kw.get('mode','string')
359 if mode not in ['string','list']:
359 if mode not in ['string','list']:
360 raise ValueError,'incorrect mode given: %s' % mode
360 raise ValueError,'incorrect mode given: %s' % mode
361 # Get options
361 # Get options
362 list_all = kw.get('list_all',0)
362 list_all = kw.get('list_all',0)
363 posix = kw.get('posix', os.name == 'posix')
363 posix = kw.get('posix', os.name == 'posix')
364
364
365 # Check if we have more than one argument to warrant extra processing:
365 # Check if we have more than one argument to warrant extra processing:
366 odict = {} # Dictionary with options
366 odict = {} # Dictionary with options
367 args = arg_str.split()
367 args = arg_str.split()
368 if len(args) >= 1:
368 if len(args) >= 1:
369 # If the list of inputs only has 0 or 1 thing in it, there's no
369 # If the list of inputs only has 0 or 1 thing in it, there's no
370 # need to look for options
370 # need to look for options
371 argv = arg_split(arg_str,posix)
371 argv = arg_split(arg_str,posix)
372 # Do regular option processing
372 # Do regular option processing
373 try:
373 try:
374 opts,args = getopt(argv,opt_str,*long_opts)
374 opts,args = getopt(argv,opt_str,*long_opts)
375 except GetoptError,e:
375 except GetoptError,e:
376 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
376 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
377 " ".join(long_opts)))
377 " ".join(long_opts)))
378 for o,a in opts:
378 for o,a in opts:
379 if o.startswith('--'):
379 if o.startswith('--'):
380 o = o[2:]
380 o = o[2:]
381 else:
381 else:
382 o = o[1:]
382 o = o[1:]
383 try:
383 try:
384 odict[o].append(a)
384 odict[o].append(a)
385 except AttributeError:
385 except AttributeError:
386 odict[o] = [odict[o],a]
386 odict[o] = [odict[o],a]
387 except KeyError:
387 except KeyError:
388 if list_all:
388 if list_all:
389 odict[o] = [a]
389 odict[o] = [a]
390 else:
390 else:
391 odict[o] = a
391 odict[o] = a
392
392
393 # Prepare opts,args for return
393 # Prepare opts,args for return
394 opts = Struct(odict)
394 opts = Struct(odict)
395 if mode == 'string':
395 if mode == 'string':
396 args = ' '.join(args)
396 args = ' '.join(args)
397
397
398 return opts,args
398 return opts,args
399
399
400 #......................................................................
400 #......................................................................
401 # And now the actual magic functions
401 # And now the actual magic functions
402
402
403 # Functions for IPython shell work (vars,funcs, config, etc)
403 # Functions for IPython shell work (vars,funcs, config, etc)
404 def magic_lsmagic(self, parameter_s = ''):
404 def magic_lsmagic(self, parameter_s = ''):
405 """List currently available magic functions."""
405 """List currently available magic functions."""
406 mesc = ESC_MAGIC
406 mesc = ESC_MAGIC
407 print 'Available magic functions:\n'+mesc+\
407 print 'Available magic functions:\n'+mesc+\
408 (' '+mesc).join(self.lsmagic())
408 (' '+mesc).join(self.lsmagic())
409 print '\n' + Magic.auto_status[self.shell.automagic]
409 print '\n' + Magic.auto_status[self.shell.automagic]
410 return None
410 return None
411
411
412 def magic_magic(self, parameter_s = ''):
412 def magic_magic(self, parameter_s = ''):
413 """Print information about the magic function system.
413 """Print information about the magic function system.
414
414
415 Supported formats: -latex, -brief, -rest
415 Supported formats: -latex, -brief, -rest
416 """
416 """
417
417
418 mode = ''
418 mode = ''
419 try:
419 try:
420 if parameter_s.split()[0] == '-latex':
420 if parameter_s.split()[0] == '-latex':
421 mode = 'latex'
421 mode = 'latex'
422 if parameter_s.split()[0] == '-brief':
422 if parameter_s.split()[0] == '-brief':
423 mode = 'brief'
423 mode = 'brief'
424 if parameter_s.split()[0] == '-rest':
424 if parameter_s.split()[0] == '-rest':
425 mode = 'rest'
425 mode = 'rest'
426 rest_docs = []
426 rest_docs = []
427 except:
427 except:
428 pass
428 pass
429
429
430 magic_docs = []
430 magic_docs = []
431 for fname in self.lsmagic():
431 for fname in self.lsmagic():
432 mname = 'magic_' + fname
432 mname = 'magic_' + fname
433 for space in (Magic,self,self.__class__):
433 for space in (Magic,self,self.__class__):
434 try:
434 try:
435 fn = space.__dict__[mname]
435 fn = space.__dict__[mname]
436 except KeyError:
436 except KeyError:
437 pass
437 pass
438 else:
438 else:
439 break
439 break
440 if mode == 'brief':
440 if mode == 'brief':
441 # only first line
441 # only first line
442 if fn.__doc__:
442 if fn.__doc__:
443 fndoc = fn.__doc__.split('\n',1)[0]
443 fndoc = fn.__doc__.split('\n',1)[0]
444 else:
444 else:
445 fndoc = 'No documentation'
445 fndoc = 'No documentation'
446 else:
446 else:
447 if fn.__doc__:
447 if fn.__doc__:
448 fndoc = fn.__doc__.rstrip()
448 fndoc = fn.__doc__.rstrip()
449 else:
449 else:
450 fndoc = 'No documentation'
450 fndoc = 'No documentation'
451
451
452
452
453 if mode == 'rest':
453 if mode == 'rest':
454 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(ESC_MAGIC,
454 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(ESC_MAGIC,
455 fname,fndoc))
455 fname,fndoc))
456
456
457 else:
457 else:
458 magic_docs.append('%s%s:\n\t%s\n' %(ESC_MAGIC,
458 magic_docs.append('%s%s:\n\t%s\n' %(ESC_MAGIC,
459 fname,fndoc))
459 fname,fndoc))
460
460
461 magic_docs = ''.join(magic_docs)
461 magic_docs = ''.join(magic_docs)
462
462
463 if mode == 'rest':
463 if mode == 'rest':
464 return "".join(rest_docs)
464 return "".join(rest_docs)
465
465
466 if mode == 'latex':
466 if mode == 'latex':
467 print self.format_latex(magic_docs)
467 print self.format_latex(magic_docs)
468 return
468 return
469 else:
469 else:
470 magic_docs = self.format_screen(magic_docs)
470 magic_docs = self.format_screen(magic_docs)
471 if mode == 'brief':
471 if mode == 'brief':
472 return magic_docs
472 return magic_docs
473
473
474 outmsg = """
474 outmsg = """
475 IPython's 'magic' functions
475 IPython's 'magic' functions
476 ===========================
476 ===========================
477
477
478 The magic function system provides a series of functions which allow you to
478 The magic function system provides a series of functions which allow you to
479 control the behavior of IPython itself, plus a lot of system-type
479 control the behavior of IPython itself, plus a lot of system-type
480 features. All these functions are prefixed with a % character, but parameters
480 features. All these functions are prefixed with a % character, but parameters
481 are given without parentheses or quotes.
481 are given without parentheses or quotes.
482
482
483 NOTE: If you have 'automagic' enabled (via the command line option or with the
483 NOTE: If you have 'automagic' enabled (via the command line option or with the
484 %automagic function), you don't need to type in the % explicitly. By default,
484 %automagic function), you don't need to type in the % explicitly. By default,
485 IPython ships with automagic on, so you should only rarely need the % escape.
485 IPython ships with automagic on, so you should only rarely need the % escape.
486
486
487 Example: typing '%cd mydir' (without the quotes) changes you working directory
487 Example: typing '%cd mydir' (without the quotes) changes you working directory
488 to 'mydir', if it exists.
488 to 'mydir', if it exists.
489
489
490 You can define your own magic functions to extend the system. See the supplied
490 You can define your own magic functions to extend the system. See the supplied
491 ipythonrc and example-magic.py files for details (in your ipython
491 ipythonrc and example-magic.py files for details (in your ipython
492 configuration directory, typically $HOME/.ipython/).
492 configuration directory, typically $HOME/.ipython/).
493
493
494 You can also define your own aliased names for magic functions. In your
494 You can also define your own aliased names for magic functions. In your
495 ipythonrc file, placing a line like:
495 ipythonrc file, placing a line like:
496
496
497 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
497 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
498
498
499 will define %pf as a new name for %profile.
499 will define %pf as a new name for %profile.
500
500
501 You can also call magics in code using the magic() function, which IPython
501 You can also call magics in code using the magic() function, which IPython
502 automatically adds to the builtin namespace. Type 'magic?' for details.
502 automatically adds to the builtin namespace. Type 'magic?' for details.
503
503
504 For a list of the available magic functions, use %lsmagic. For a description
504 For a list of the available magic functions, use %lsmagic. For a description
505 of any of them, type %magic_name?, e.g. '%cd?'.
505 of any of them, type %magic_name?, e.g. '%cd?'.
506
506
507 Currently the magic system has the following functions:\n"""
507 Currently the magic system has the following functions:\n"""
508
508
509 mesc = ESC_MAGIC
509 mesc = ESC_MAGIC
510 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
510 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
511 "\n\n%s%s\n\n%s" % (outmsg,
511 "\n\n%s%s\n\n%s" % (outmsg,
512 magic_docs,mesc,mesc,
512 magic_docs,mesc,mesc,
513 (' '+mesc).join(self.lsmagic()),
513 (' '+mesc).join(self.lsmagic()),
514 Magic.auto_status[self.shell.automagic] ) )
514 Magic.auto_status[self.shell.automagic] ) )
515
515
516 page(outmsg,screen_lines=self.shell.usable_screen_length)
516 page(outmsg,screen_lines=self.shell.usable_screen_length)
517
517
518
518
519 def magic_autoindent(self, parameter_s = ''):
519 def magic_autoindent(self, parameter_s = ''):
520 """Toggle autoindent on/off (if available)."""
520 """Toggle autoindent on/off (if available)."""
521
521
522 self.shell.set_autoindent()
522 self.shell.set_autoindent()
523 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
523 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
524
524
525
525
526 def magic_automagic(self, parameter_s = ''):
526 def magic_automagic(self, parameter_s = ''):
527 """Make magic functions callable without having to type the initial %.
527 """Make magic functions callable without having to type the initial %.
528
528
529 Without argumentsl toggles on/off (when off, you must call it as
529 Without argumentsl toggles on/off (when off, you must call it as
530 %automagic, of course). With arguments it sets the value, and you can
530 %automagic, of course). With arguments it sets the value, and you can
531 use any of (case insensitive):
531 use any of (case insensitive):
532
532
533 - on,1,True: to activate
533 - on,1,True: to activate
534
534
535 - off,0,False: to deactivate.
535 - off,0,False: to deactivate.
536
536
537 Note that magic functions have lowest priority, so if there's a
537 Note that magic functions have lowest priority, so if there's a
538 variable whose name collides with that of a magic fn, automagic won't
538 variable whose name collides with that of a magic fn, automagic won't
539 work for that function (you get the variable instead). However, if you
539 work for that function (you get the variable instead). However, if you
540 delete the variable (del var), the previously shadowed magic function
540 delete the variable (del var), the previously shadowed magic function
541 becomes visible to automagic again."""
541 becomes visible to automagic again."""
542
542
543 arg = parameter_s.lower()
543 arg = parameter_s.lower()
544 if parameter_s in ('on','1','true'):
544 if parameter_s in ('on','1','true'):
545 self.shell.automagic = True
545 self.shell.automagic = True
546 elif parameter_s in ('off','0','false'):
546 elif parameter_s in ('off','0','false'):
547 self.shell.automagic = False
547 self.shell.automagic = False
548 else:
548 else:
549 self.shell.automagic = not self.shell.automagic
549 self.shell.automagic = not self.shell.automagic
550 print '\n' + Magic.auto_status[self.shell.automagic]
550 print '\n' + Magic.auto_status[self.shell.automagic]
551
551
552 @testdec.skip_doctest
552 @testdec.skip_doctest
553 def magic_autocall(self, parameter_s = ''):
553 def magic_autocall(self, parameter_s = ''):
554 """Make functions callable without having to type parentheses.
554 """Make functions callable without having to type parentheses.
555
555
556 Usage:
556 Usage:
557
557
558 %autocall [mode]
558 %autocall [mode]
559
559
560 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
560 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
561 value is toggled on and off (remembering the previous state).
561 value is toggled on and off (remembering the previous state).
562
562
563 In more detail, these values mean:
563 In more detail, these values mean:
564
564
565 0 -> fully disabled
565 0 -> fully disabled
566
566
567 1 -> active, but do not apply if there are no arguments on the line.
567 1 -> active, but do not apply if there are no arguments on the line.
568
568
569 In this mode, you get:
569 In this mode, you get:
570
570
571 In [1]: callable
571 In [1]: callable
572 Out[1]: <built-in function callable>
572 Out[1]: <built-in function callable>
573
573
574 In [2]: callable 'hello'
574 In [2]: callable 'hello'
575 ------> callable('hello')
575 ------> callable('hello')
576 Out[2]: False
576 Out[2]: False
577
577
578 2 -> Active always. Even if no arguments are present, the callable
578 2 -> Active always. Even if no arguments are present, the callable
579 object is called:
579 object is called:
580
580
581 In [2]: float
581 In [2]: float
582 ------> float()
582 ------> float()
583 Out[2]: 0.0
583 Out[2]: 0.0
584
584
585 Note that even with autocall off, you can still use '/' at the start of
585 Note that even with autocall off, you can still use '/' at the start of
586 a line to treat the first argument on the command line as a function
586 a line to treat the first argument on the command line as a function
587 and add parentheses to it:
587 and add parentheses to it:
588
588
589 In [8]: /str 43
589 In [8]: /str 43
590 ------> str(43)
590 ------> str(43)
591 Out[8]: '43'
591 Out[8]: '43'
592
592
593 # all-random (note for auto-testing)
593 # all-random (note for auto-testing)
594 """
594 """
595
595
596 if parameter_s:
596 if parameter_s:
597 arg = int(parameter_s)
597 arg = int(parameter_s)
598 else:
598 else:
599 arg = 'toggle'
599 arg = 'toggle'
600
600
601 if not arg in (0,1,2,'toggle'):
601 if not arg in (0,1,2,'toggle'):
602 error('Valid modes: (0->Off, 1->Smart, 2->Full')
602 error('Valid modes: (0->Off, 1->Smart, 2->Full')
603 return
603 return
604
604
605 if arg in (0,1,2):
605 if arg in (0,1,2):
606 self.shell.autocall = arg
606 self.shell.autocall = arg
607 else: # toggle
607 else: # toggle
608 if self.shell.autocall:
608 if self.shell.autocall:
609 self._magic_state.autocall_save = self.shell.autocall
609 self._magic_state.autocall_save = self.shell.autocall
610 self.shell.autocall = 0
610 self.shell.autocall = 0
611 else:
611 else:
612 try:
612 try:
613 self.shell.autocall = self._magic_state.autocall_save
613 self.shell.autocall = self._magic_state.autocall_save
614 except AttributeError:
614 except AttributeError:
615 self.shell.autocall = self._magic_state.autocall_save = 1
615 self.shell.autocall = self._magic_state.autocall_save = 1
616
616
617 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
617 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
618
618
619 def magic_system_verbose(self, parameter_s = ''):
619 def magic_system_verbose(self, parameter_s = ''):
620 """Set verbose printing of system calls.
620 """Set verbose printing of system calls.
621
621
622 If called without an argument, act as a toggle"""
622 If called without an argument, act as a toggle"""
623
623
624 if parameter_s:
624 if parameter_s:
625 val = bool(eval(parameter_s))
625 val = bool(eval(parameter_s))
626 else:
626 else:
627 val = None
627 val = None
628
628
629 if self.shell.system_verbose:
629 if self.shell.system_verbose:
630 self.shell.system_verbose = False
630 self.shell.system_verbose = False
631 else:
631 else:
632 self.shell.system_verbose = True
632 self.shell.system_verbose = True
633 print "System verbose printing is:",\
633 print "System verbose printing is:",\
634 ['OFF','ON'][self.shell.system_verbose]
634 ['OFF','ON'][self.shell.system_verbose]
635
635
636
636
637 def magic_page(self, parameter_s=''):
637 def magic_page(self, parameter_s=''):
638 """Pretty print the object and display it through a pager.
638 """Pretty print the object and display it through a pager.
639
639
640 %page [options] OBJECT
640 %page [options] OBJECT
641
641
642 If no object is given, use _ (last output).
642 If no object is given, use _ (last output).
643
643
644 Options:
644 Options:
645
645
646 -r: page str(object), don't pretty-print it."""
646 -r: page str(object), don't pretty-print it."""
647
647
648 # After a function contributed by Olivier Aubert, slightly modified.
648 # After a function contributed by Olivier Aubert, slightly modified.
649
649
650 # Process options/args
650 # Process options/args
651 opts,args = self.parse_options(parameter_s,'r')
651 opts,args = self.parse_options(parameter_s,'r')
652 raw = 'r' in opts
652 raw = 'r' in opts
653
653
654 oname = args and args or '_'
654 oname = args and args or '_'
655 info = self._ofind(oname)
655 info = self._ofind(oname)
656 if info['found']:
656 if info['found']:
657 txt = (raw and str or pformat)( info['obj'] )
657 txt = (raw and str or pformat)( info['obj'] )
658 page(txt)
658 page(txt)
659 else:
659 else:
660 print 'Object `%s` not found' % oname
660 print 'Object `%s` not found' % oname
661
661
662 def magic_profile(self, parameter_s=''):
662 def magic_profile(self, parameter_s=''):
663 """Print your currently active IPython profile."""
663 """Print your currently active IPython profile."""
664 if self.shell.profile:
664 if self.shell.profile:
665 printpl('Current IPython profile: $self.shell.profile.')
665 printpl('Current IPython profile: $self.shell.profile.')
666 else:
666 else:
667 print 'No profile active.'
667 print 'No profile active.'
668
668
669 def magic_pinfo(self, parameter_s='', namespaces=None):
669 def magic_pinfo(self, parameter_s='', namespaces=None):
670 """Provide detailed information about an object.
670 """Provide detailed information about an object.
671
671
672 '%pinfo object' is just a synonym for object? or ?object."""
672 '%pinfo object' is just a synonym for object? or ?object."""
673
673
674 #print 'pinfo par: <%s>' % parameter_s # dbg
674 #print 'pinfo par: <%s>' % parameter_s # dbg
675
675
676
676
677 # detail_level: 0 -> obj? , 1 -> obj??
677 # detail_level: 0 -> obj? , 1 -> obj??
678 detail_level = 0
678 detail_level = 0
679 # We need to detect if we got called as 'pinfo pinfo foo', which can
679 # We need to detect if we got called as 'pinfo pinfo foo', which can
680 # happen if the user types 'pinfo foo?' at the cmd line.
680 # happen if the user types 'pinfo foo?' at the cmd line.
681 pinfo,qmark1,oname,qmark2 = \
681 pinfo,qmark1,oname,qmark2 = \
682 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
682 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
683 if pinfo or qmark1 or qmark2:
683 if pinfo or qmark1 or qmark2:
684 detail_level = 1
684 detail_level = 1
685 if "*" in oname:
685 if "*" in oname:
686 self.magic_psearch(oname)
686 self.magic_psearch(oname)
687 else:
687 else:
688 self._inspect('pinfo', oname, detail_level=detail_level,
688 self._inspect('pinfo', oname, detail_level=detail_level,
689 namespaces=namespaces)
689 namespaces=namespaces)
690
690
691 def magic_pdef(self, parameter_s='', namespaces=None):
691 def magic_pdef(self, parameter_s='', namespaces=None):
692 """Print the definition header for any callable object.
692 """Print the definition header for any callable object.
693
693
694 If the object is a class, print the constructor information."""
694 If the object is a class, print the constructor information."""
695 self._inspect('pdef',parameter_s, namespaces)
695 self._inspect('pdef',parameter_s, namespaces)
696
696
697 def magic_pdoc(self, parameter_s='', namespaces=None):
697 def magic_pdoc(self, parameter_s='', namespaces=None):
698 """Print the docstring for an object.
698 """Print the docstring for an object.
699
699
700 If the given object is a class, it will print both the class and the
700 If the given object is a class, it will print both the class and the
701 constructor docstrings."""
701 constructor docstrings."""
702 self._inspect('pdoc',parameter_s, namespaces)
702 self._inspect('pdoc',parameter_s, namespaces)
703
703
704 def magic_psource(self, parameter_s='', namespaces=None):
704 def magic_psource(self, parameter_s='', namespaces=None):
705 """Print (or run through pager) the source code for an object."""
705 """Print (or run through pager) the source code for an object."""
706 self._inspect('psource',parameter_s, namespaces)
706 self._inspect('psource',parameter_s, namespaces)
707
707
708 def magic_pfile(self, parameter_s=''):
708 def magic_pfile(self, parameter_s=''):
709 """Print (or run through pager) the file where an object is defined.
709 """Print (or run through pager) the file where an object is defined.
710
710
711 The file opens at the line where the object definition begins. IPython
711 The file opens at the line where the object definition begins. IPython
712 will honor the environment variable PAGER if set, and otherwise will
712 will honor the environment variable PAGER if set, and otherwise will
713 do its best to print the file in a convenient form.
713 do its best to print the file in a convenient form.
714
714
715 If the given argument is not an object currently defined, IPython will
715 If the given argument is not an object currently defined, IPython will
716 try to interpret it as a filename (automatically adding a .py extension
716 try to interpret it as a filename (automatically adding a .py extension
717 if needed). You can thus use %pfile as a syntax highlighting code
717 if needed). You can thus use %pfile as a syntax highlighting code
718 viewer."""
718 viewer."""
719
719
720 # first interpret argument as an object name
720 # first interpret argument as an object name
721 out = self._inspect('pfile',parameter_s)
721 out = self._inspect('pfile',parameter_s)
722 # if not, try the input as a filename
722 # if not, try the input as a filename
723 if out == 'not found':
723 if out == 'not found':
724 try:
724 try:
725 filename = get_py_filename(parameter_s)
725 filename = get_py_filename(parameter_s)
726 except IOError,msg:
726 except IOError,msg:
727 print msg
727 print msg
728 return
728 return
729 page(self.shell.inspector.format(file(filename).read()))
729 page(self.shell.inspector.format(file(filename).read()))
730
730
731 def _inspect(self,meth,oname,namespaces=None,**kw):
731 def _inspect(self,meth,oname,namespaces=None,**kw):
732 """Generic interface to the inspector system.
732 """Generic interface to the inspector system.
733
733
734 This function is meant to be called by pdef, pdoc & friends."""
734 This function is meant to be called by pdef, pdoc & friends."""
735
735
736 #oname = oname.strip()
736 #oname = oname.strip()
737 #print '1- oname: <%r>' % oname # dbg
737 #print '1- oname: <%r>' % oname # dbg
738 try:
738 try:
739 oname = oname.strip().encode('ascii')
739 oname = oname.strip().encode('ascii')
740 #print '2- oname: <%r>' % oname # dbg
740 #print '2- oname: <%r>' % oname # dbg
741 except UnicodeEncodeError:
741 except UnicodeEncodeError:
742 print 'Python identifiers can only contain ascii characters.'
742 print 'Python identifiers can only contain ascii characters.'
743 return 'not found'
743 return 'not found'
744
744
745 info = Struct(self._ofind(oname, namespaces))
745 info = Struct(self._ofind(oname, namespaces))
746
746
747 if info.found:
747 if info.found:
748 try:
748 try:
749 IPython.utils.generics.inspect_object(info.obj)
749 IPython.utils.generics.inspect_object(info.obj)
750 return
750 return
751 except TryNext:
751 except TryNext:
752 pass
752 pass
753 # Get the docstring of the class property if it exists.
753 # Get the docstring of the class property if it exists.
754 path = oname.split('.')
754 path = oname.split('.')
755 root = '.'.join(path[:-1])
755 root = '.'.join(path[:-1])
756 if info.parent is not None:
756 if info.parent is not None:
757 try:
757 try:
758 target = getattr(info.parent, '__class__')
758 target = getattr(info.parent, '__class__')
759 # The object belongs to a class instance.
759 # The object belongs to a class instance.
760 try:
760 try:
761 target = getattr(target, path[-1])
761 target = getattr(target, path[-1])
762 # The class defines the object.
762 # The class defines the object.
763 if isinstance(target, property):
763 if isinstance(target, property):
764 oname = root + '.__class__.' + path[-1]
764 oname = root + '.__class__.' + path[-1]
765 info = Struct(self._ofind(oname))
765 info = Struct(self._ofind(oname))
766 except AttributeError: pass
766 except AttributeError: pass
767 except AttributeError: pass
767 except AttributeError: pass
768
768
769 pmethod = getattr(self.shell.inspector,meth)
769 pmethod = getattr(self.shell.inspector,meth)
770 formatter = info.ismagic and self.format_screen or None
770 formatter = info.ismagic and self.format_screen or None
771 if meth == 'pdoc':
771 if meth == 'pdoc':
772 pmethod(info.obj,oname,formatter)
772 pmethod(info.obj,oname,formatter)
773 elif meth == 'pinfo':
773 elif meth == 'pinfo':
774 pmethod(info.obj,oname,formatter,info,**kw)
774 pmethod(info.obj,oname,formatter,info,**kw)
775 else:
775 else:
776 pmethod(info.obj,oname)
776 pmethod(info.obj,oname)
777 else:
777 else:
778 print 'Object `%s` not found.' % oname
778 print 'Object `%s` not found.' % oname
779 return 'not found' # so callers can take other action
779 return 'not found' # so callers can take other action
780
780
781 def magic_psearch(self, parameter_s=''):
781 def magic_psearch(self, parameter_s=''):
782 """Search for object in namespaces by wildcard.
782 """Search for object in namespaces by wildcard.
783
783
784 %psearch [options] PATTERN [OBJECT TYPE]
784 %psearch [options] PATTERN [OBJECT TYPE]
785
785
786 Note: ? can be used as a synonym for %psearch, at the beginning or at
786 Note: ? can be used as a synonym for %psearch, at the beginning or at
787 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
787 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
788 rest of the command line must be unchanged (options come first), so
788 rest of the command line must be unchanged (options come first), so
789 for example the following forms are equivalent
789 for example the following forms are equivalent
790
790
791 %psearch -i a* function
791 %psearch -i a* function
792 -i a* function?
792 -i a* function?
793 ?-i a* function
793 ?-i a* function
794
794
795 Arguments:
795 Arguments:
796
796
797 PATTERN
797 PATTERN
798
798
799 where PATTERN is a string containing * as a wildcard similar to its
799 where PATTERN is a string containing * as a wildcard similar to its
800 use in a shell. The pattern is matched in all namespaces on the
800 use in a shell. The pattern is matched in all namespaces on the
801 search path. By default objects starting with a single _ are not
801 search path. By default objects starting with a single _ are not
802 matched, many IPython generated objects have a single
802 matched, many IPython generated objects have a single
803 underscore. The default is case insensitive matching. Matching is
803 underscore. The default is case insensitive matching. Matching is
804 also done on the attributes of objects and not only on the objects
804 also done on the attributes of objects and not only on the objects
805 in a module.
805 in a module.
806
806
807 [OBJECT TYPE]
807 [OBJECT TYPE]
808
808
809 Is the name of a python type from the types module. The name is
809 Is the name of a python type from the types module. The name is
810 given in lowercase without the ending type, ex. StringType is
810 given in lowercase without the ending type, ex. StringType is
811 written string. By adding a type here only objects matching the
811 written string. By adding a type here only objects matching the
812 given type are matched. Using all here makes the pattern match all
812 given type are matched. Using all here makes the pattern match all
813 types (this is the default).
813 types (this is the default).
814
814
815 Options:
815 Options:
816
816
817 -a: makes the pattern match even objects whose names start with a
817 -a: makes the pattern match even objects whose names start with a
818 single underscore. These names are normally ommitted from the
818 single underscore. These names are normally ommitted from the
819 search.
819 search.
820
820
821 -i/-c: make the pattern case insensitive/sensitive. If neither of
821 -i/-c: make the pattern case insensitive/sensitive. If neither of
822 these options is given, the default is read from your ipythonrc
822 these options is given, the default is read from your ipythonrc
823 file. The option name which sets this value is
823 file. The option name which sets this value is
824 'wildcards_case_sensitive'. If this option is not specified in your
824 'wildcards_case_sensitive'. If this option is not specified in your
825 ipythonrc file, IPython's internal default is to do a case sensitive
825 ipythonrc file, IPython's internal default is to do a case sensitive
826 search.
826 search.
827
827
828 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
828 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
829 specifiy can be searched in any of the following namespaces:
829 specifiy can be searched in any of the following namespaces:
830 'builtin', 'user', 'user_global','internal', 'alias', where
830 'builtin', 'user', 'user_global','internal', 'alias', where
831 'builtin' and 'user' are the search defaults. Note that you should
831 'builtin' and 'user' are the search defaults. Note that you should
832 not use quotes when specifying namespaces.
832 not use quotes when specifying namespaces.
833
833
834 'Builtin' contains the python module builtin, 'user' contains all
834 'Builtin' contains the python module builtin, 'user' contains all
835 user data, 'alias' only contain the shell aliases and no python
835 user data, 'alias' only contain the shell aliases and no python
836 objects, 'internal' contains objects used by IPython. The
836 objects, 'internal' contains objects used by IPython. The
837 'user_global' namespace is only used by embedded IPython instances,
837 'user_global' namespace is only used by embedded IPython instances,
838 and it contains module-level globals. You can add namespaces to the
838 and it contains module-level globals. You can add namespaces to the
839 search with -s or exclude them with -e (these options can be given
839 search with -s or exclude them with -e (these options can be given
840 more than once).
840 more than once).
841
841
842 Examples:
842 Examples:
843
843
844 %psearch a* -> objects beginning with an a
844 %psearch a* -> objects beginning with an a
845 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
845 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
846 %psearch a* function -> all functions beginning with an a
846 %psearch a* function -> all functions beginning with an a
847 %psearch re.e* -> objects beginning with an e in module re
847 %psearch re.e* -> objects beginning with an e in module re
848 %psearch r*.e* -> objects that start with e in modules starting in r
848 %psearch r*.e* -> objects that start with e in modules starting in r
849 %psearch r*.* string -> all strings in modules beginning with r
849 %psearch r*.* string -> all strings in modules beginning with r
850
850
851 Case sensitve search:
851 Case sensitve search:
852
852
853 %psearch -c a* list all object beginning with lower case a
853 %psearch -c a* list all object beginning with lower case a
854
854
855 Show objects beginning with a single _:
855 Show objects beginning with a single _:
856
856
857 %psearch -a _* list objects beginning with a single underscore"""
857 %psearch -a _* list objects beginning with a single underscore"""
858 try:
858 try:
859 parameter_s = parameter_s.encode('ascii')
859 parameter_s = parameter_s.encode('ascii')
860 except UnicodeEncodeError:
860 except UnicodeEncodeError:
861 print 'Python identifiers can only contain ascii characters.'
861 print 'Python identifiers can only contain ascii characters.'
862 return
862 return
863
863
864 # default namespaces to be searched
864 # default namespaces to be searched
865 def_search = ['user','builtin']
865 def_search = ['user','builtin']
866
866
867 # Process options/args
867 # Process options/args
868 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
868 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
869 opt = opts.get
869 opt = opts.get
870 shell = self.shell
870 shell = self.shell
871 psearch = shell.inspector.psearch
871 psearch = shell.inspector.psearch
872
872
873 # select case options
873 # select case options
874 if opts.has_key('i'):
874 if opts.has_key('i'):
875 ignore_case = True
875 ignore_case = True
876 elif opts.has_key('c'):
876 elif opts.has_key('c'):
877 ignore_case = False
877 ignore_case = False
878 else:
878 else:
879 ignore_case = not shell.wildcards_case_sensitive
879 ignore_case = not shell.wildcards_case_sensitive
880
880
881 # Build list of namespaces to search from user options
881 # Build list of namespaces to search from user options
882 def_search.extend(opt('s',[]))
882 def_search.extend(opt('s',[]))
883 ns_exclude = ns_exclude=opt('e',[])
883 ns_exclude = ns_exclude=opt('e',[])
884 ns_search = [nm for nm in def_search if nm not in ns_exclude]
884 ns_search = [nm for nm in def_search if nm not in ns_exclude]
885
885
886 # Call the actual search
886 # Call the actual search
887 try:
887 try:
888 psearch(args,shell.ns_table,ns_search,
888 psearch(args,shell.ns_table,ns_search,
889 show_all=opt('a'),ignore_case=ignore_case)
889 show_all=opt('a'),ignore_case=ignore_case)
890 except:
890 except:
891 shell.showtraceback()
891 shell.showtraceback()
892
892
893 def magic_who_ls(self, parameter_s=''):
893 def magic_who_ls(self, parameter_s=''):
894 """Return a sorted list of all interactive variables.
894 """Return a sorted list of all interactive variables.
895
895
896 If arguments are given, only variables of types matching these
896 If arguments are given, only variables of types matching these
897 arguments are returned."""
897 arguments are returned."""
898
898
899 user_ns = self.shell.user_ns
899 user_ns = self.shell.user_ns
900 internal_ns = self.shell.internal_ns
900 internal_ns = self.shell.internal_ns
901 user_ns_hidden = self.shell.user_ns_hidden
901 user_ns_hidden = self.shell.user_ns_hidden
902 out = [ i for i in user_ns
902 out = [ i for i in user_ns
903 if not i.startswith('_') \
903 if not i.startswith('_') \
904 and not (i in internal_ns or i in user_ns_hidden) ]
904 and not (i in internal_ns or i in user_ns_hidden) ]
905
905
906 typelist = parameter_s.split()
906 typelist = parameter_s.split()
907 if typelist:
907 if typelist:
908 typeset = set(typelist)
908 typeset = set(typelist)
909 out = [i for i in out if type(i).__name__ in typeset]
909 out = [i for i in out if type(i).__name__ in typeset]
910
910
911 out.sort()
911 out.sort()
912 return out
912 return out
913
913
914 def magic_who(self, parameter_s=''):
914 def magic_who(self, parameter_s=''):
915 """Print all interactive variables, with some minimal formatting.
915 """Print all interactive variables, with some minimal formatting.
916
916
917 If any arguments are given, only variables whose type matches one of
917 If any arguments are given, only variables whose type matches one of
918 these are printed. For example:
918 these are printed. For example:
919
919
920 %who function str
920 %who function str
921
921
922 will only list functions and strings, excluding all other types of
922 will only list functions and strings, excluding all other types of
923 variables. To find the proper type names, simply use type(var) at a
923 variables. To find the proper type names, simply use type(var) at a
924 command line to see how python prints type names. For example:
924 command line to see how python prints type names. For example:
925
925
926 In [1]: type('hello')\\
926 In [1]: type('hello')\\
927 Out[1]: <type 'str'>
927 Out[1]: <type 'str'>
928
928
929 indicates that the type name for strings is 'str'.
929 indicates that the type name for strings is 'str'.
930
930
931 %who always excludes executed names loaded through your configuration
931 %who always excludes executed names loaded through your configuration
932 file and things which are internal to IPython.
932 file and things which are internal to IPython.
933
933
934 This is deliberate, as typically you may load many modules and the
934 This is deliberate, as typically you may load many modules and the
935 purpose of %who is to show you only what you've manually defined."""
935 purpose of %who is to show you only what you've manually defined."""
936
936
937 varlist = self.magic_who_ls(parameter_s)
937 varlist = self.magic_who_ls(parameter_s)
938 if not varlist:
938 if not varlist:
939 if parameter_s:
939 if parameter_s:
940 print 'No variables match your requested type.'
940 print 'No variables match your requested type.'
941 else:
941 else:
942 print 'Interactive namespace is empty.'
942 print 'Interactive namespace is empty.'
943 return
943 return
944
944
945 # if we have variables, move on...
945 # if we have variables, move on...
946 count = 0
946 count = 0
947 for i in varlist:
947 for i in varlist:
948 print i+'\t',
948 print i+'\t',
949 count += 1
949 count += 1
950 if count > 8:
950 if count > 8:
951 count = 0
951 count = 0
952 print
952 print
953 print
953 print
954
954
955 def magic_whos(self, parameter_s=''):
955 def magic_whos(self, parameter_s=''):
956 """Like %who, but gives some extra information about each variable.
956 """Like %who, but gives some extra information about each variable.
957
957
958 The same type filtering of %who can be applied here.
958 The same type filtering of %who can be applied here.
959
959
960 For all variables, the type is printed. Additionally it prints:
960 For all variables, the type is printed. Additionally it prints:
961
961
962 - For {},[],(): their length.
962 - For {},[],(): their length.
963
963
964 - For numpy and Numeric arrays, a summary with shape, number of
964 - For numpy and Numeric arrays, a summary with shape, number of
965 elements, typecode and size in memory.
965 elements, typecode and size in memory.
966
966
967 - Everything else: a string representation, snipping their middle if
967 - Everything else: a string representation, snipping their middle if
968 too long."""
968 too long."""
969
969
970 varnames = self.magic_who_ls(parameter_s)
970 varnames = self.magic_who_ls(parameter_s)
971 if not varnames:
971 if not varnames:
972 if parameter_s:
972 if parameter_s:
973 print 'No variables match your requested type.'
973 print 'No variables match your requested type.'
974 else:
974 else:
975 print 'Interactive namespace is empty.'
975 print 'Interactive namespace is empty.'
976 return
976 return
977
977
978 # if we have variables, move on...
978 # if we have variables, move on...
979
979
980 # for these types, show len() instead of data:
980 # for these types, show len() instead of data:
981 seq_types = [types.DictType,types.ListType,types.TupleType]
981 seq_types = [types.DictType,types.ListType,types.TupleType]
982
982
983 # for numpy/Numeric arrays, display summary info
983 # for numpy/Numeric arrays, display summary info
984 try:
984 try:
985 import numpy
985 import numpy
986 except ImportError:
986 except ImportError:
987 ndarray_type = None
987 ndarray_type = None
988 else:
988 else:
989 ndarray_type = numpy.ndarray.__name__
989 ndarray_type = numpy.ndarray.__name__
990 try:
990 try:
991 import Numeric
991 import Numeric
992 except ImportError:
992 except ImportError:
993 array_type = None
993 array_type = None
994 else:
994 else:
995 array_type = Numeric.ArrayType.__name__
995 array_type = Numeric.ArrayType.__name__
996
996
997 # Find all variable names and types so we can figure out column sizes
997 # Find all variable names and types so we can figure out column sizes
998 def get_vars(i):
998 def get_vars(i):
999 return self.shell.user_ns[i]
999 return self.shell.user_ns[i]
1000
1000
1001 # some types are well known and can be shorter
1001 # some types are well known and can be shorter
1002 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
1002 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
1003 def type_name(v):
1003 def type_name(v):
1004 tn = type(v).__name__
1004 tn = type(v).__name__
1005 return abbrevs.get(tn,tn)
1005 return abbrevs.get(tn,tn)
1006
1006
1007 varlist = map(get_vars,varnames)
1007 varlist = map(get_vars,varnames)
1008
1008
1009 typelist = []
1009 typelist = []
1010 for vv in varlist:
1010 for vv in varlist:
1011 tt = type_name(vv)
1011 tt = type_name(vv)
1012
1012
1013 if tt=='instance':
1013 if tt=='instance':
1014 typelist.append( abbrevs.get(str(vv.__class__),
1014 typelist.append( abbrevs.get(str(vv.__class__),
1015 str(vv.__class__)))
1015 str(vv.__class__)))
1016 else:
1016 else:
1017 typelist.append(tt)
1017 typelist.append(tt)
1018
1018
1019 # column labels and # of spaces as separator
1019 # column labels and # of spaces as separator
1020 varlabel = 'Variable'
1020 varlabel = 'Variable'
1021 typelabel = 'Type'
1021 typelabel = 'Type'
1022 datalabel = 'Data/Info'
1022 datalabel = 'Data/Info'
1023 colsep = 3
1023 colsep = 3
1024 # variable format strings
1024 # variable format strings
1025 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
1025 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
1026 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
1026 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
1027 aformat = "%s: %s elems, type `%s`, %s bytes"
1027 aformat = "%s: %s elems, type `%s`, %s bytes"
1028 # find the size of the columns to format the output nicely
1028 # find the size of the columns to format the output nicely
1029 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
1029 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
1030 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
1030 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
1031 # table header
1031 # table header
1032 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
1032 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
1033 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
1033 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
1034 # and the table itself
1034 # and the table itself
1035 kb = 1024
1035 kb = 1024
1036 Mb = 1048576 # kb**2
1036 Mb = 1048576 # kb**2
1037 for vname,var,vtype in zip(varnames,varlist,typelist):
1037 for vname,var,vtype in zip(varnames,varlist,typelist):
1038 print itpl(vformat),
1038 print itpl(vformat),
1039 if vtype in seq_types:
1039 if vtype in seq_types:
1040 print len(var)
1040 print len(var)
1041 elif vtype in [array_type,ndarray_type]:
1041 elif vtype in [array_type,ndarray_type]:
1042 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
1042 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
1043 if vtype==ndarray_type:
1043 if vtype==ndarray_type:
1044 # numpy
1044 # numpy
1045 vsize = var.size
1045 vsize = var.size
1046 vbytes = vsize*var.itemsize
1046 vbytes = vsize*var.itemsize
1047 vdtype = var.dtype
1047 vdtype = var.dtype
1048 else:
1048 else:
1049 # Numeric
1049 # Numeric
1050 vsize = Numeric.size(var)
1050 vsize = Numeric.size(var)
1051 vbytes = vsize*var.itemsize()
1051 vbytes = vsize*var.itemsize()
1052 vdtype = var.typecode()
1052 vdtype = var.typecode()
1053
1053
1054 if vbytes < 100000:
1054 if vbytes < 100000:
1055 print aformat % (vshape,vsize,vdtype,vbytes)
1055 print aformat % (vshape,vsize,vdtype,vbytes)
1056 else:
1056 else:
1057 print aformat % (vshape,vsize,vdtype,vbytes),
1057 print aformat % (vshape,vsize,vdtype,vbytes),
1058 if vbytes < Mb:
1058 if vbytes < Mb:
1059 print '(%s kb)' % (vbytes/kb,)
1059 print '(%s kb)' % (vbytes/kb,)
1060 else:
1060 else:
1061 print '(%s Mb)' % (vbytes/Mb,)
1061 print '(%s Mb)' % (vbytes/Mb,)
1062 else:
1062 else:
1063 try:
1063 try:
1064 vstr = str(var)
1064 vstr = str(var)
1065 except UnicodeEncodeError:
1065 except UnicodeEncodeError:
1066 vstr = unicode(var).encode(sys.getdefaultencoding(),
1066 vstr = unicode(var).encode(sys.getdefaultencoding(),
1067 'backslashreplace')
1067 'backslashreplace')
1068 vstr = vstr.replace('\n','\\n')
1068 vstr = vstr.replace('\n','\\n')
1069 if len(vstr) < 50:
1069 if len(vstr) < 50:
1070 print vstr
1070 print vstr
1071 else:
1071 else:
1072 printpl(vfmt_short)
1072 printpl(vfmt_short)
1073
1073
1074 def magic_reset(self, parameter_s=''):
1074 def magic_reset(self, parameter_s=''):
1075 """Resets the namespace by removing all names defined by the user.
1075 """Resets the namespace by removing all names defined by the user.
1076
1076
1077 Input/Output history are left around in case you need them.
1077 Input/Output history are left around in case you need them.
1078
1078
1079 Parameters
1079 Parameters
1080 ----------
1080 ----------
1081 -y : force reset without asking for confirmation.
1081 -y : force reset without asking for confirmation.
1082
1082
1083 Examples
1083 Examples
1084 --------
1084 --------
1085 In [6]: a = 1
1085 In [6]: a = 1
1086
1086
1087 In [7]: a
1087 In [7]: a
1088 Out[7]: 1
1088 Out[7]: 1
1089
1089
1090 In [8]: 'a' in _ip.user_ns
1090 In [8]: 'a' in _ip.user_ns
1091 Out[8]: True
1091 Out[8]: True
1092
1092
1093 In [9]: %reset -f
1093 In [9]: %reset -f
1094
1094
1095 In [10]: 'a' in _ip.user_ns
1095 In [10]: 'a' in _ip.user_ns
1096 Out[10]: False
1096 Out[10]: False
1097 """
1097 """
1098
1098
1099 if parameter_s == '-f':
1099 if parameter_s == '-f':
1100 ans = True
1100 ans = True
1101 else:
1101 else:
1102 ans = self.shell.ask_yes_no(
1102 ans = self.shell.ask_yes_no(
1103 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1103 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1104 if not ans:
1104 if not ans:
1105 print 'Nothing done.'
1105 print 'Nothing done.'
1106 return
1106 return
1107 user_ns = self.shell.user_ns
1107 user_ns = self.shell.user_ns
1108 for i in self.magic_who_ls():
1108 for i in self.magic_who_ls():
1109 del(user_ns[i])
1109 del(user_ns[i])
1110
1110
1111 # Also flush the private list of module references kept for script
1111 # Also flush the private list of module references kept for script
1112 # execution protection
1112 # execution protection
1113 self.shell.clear_main_mod_cache()
1113 self.shell.clear_main_mod_cache()
1114
1114
1115 def magic_reset_selective(self, parameter_s=''):
1115 def magic_reset_selective(self, parameter_s=''):
1116 """Resets the namespace by removing names defined by the user.
1116 """Resets the namespace by removing names defined by the user.
1117
1117
1118 Input/Output history are left around in case you need them.
1118 Input/Output history are left around in case you need them.
1119
1119
1120 %reset_selective [-f] regex
1120 %reset_selective [-f] regex
1121
1121
1122 No action is taken if regex is not included
1122 No action is taken if regex is not included
1123
1123
1124 Options
1124 Options
1125 -f : force reset without asking for confirmation.
1125 -f : force reset without asking for confirmation.
1126
1126
1127 Examples
1127 Examples
1128 --------
1128 --------
1129
1129
1130 We first fully reset the namespace so your output looks identical to
1130 We first fully reset the namespace so your output looks identical to
1131 this example for pedagogical reasons; in practice you do not need a
1131 this example for pedagogical reasons; in practice you do not need a
1132 full reset.
1132 full reset.
1133
1133
1134 In [1]: %reset -f
1134 In [1]: %reset -f
1135
1135
1136 Now, with a clean namespace we can make a few variables and use
1136 Now, with a clean namespace we can make a few variables and use
1137 %reset_selective to only delete names that match our regexp:
1137 %reset_selective to only delete names that match our regexp:
1138
1138
1139 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
1139 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
1140
1140
1141 In [3]: who_ls
1141 In [3]: who_ls
1142 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
1142 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
1143
1143
1144 In [4]: %reset_selective -f b[2-3]m
1144 In [4]: %reset_selective -f b[2-3]m
1145
1145
1146 In [5]: who_ls
1146 In [5]: who_ls
1147 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1147 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1148
1148
1149 In [6]: %reset_selective -f d
1149 In [6]: %reset_selective -f d
1150
1150
1151 In [7]: who_ls
1151 In [7]: who_ls
1152 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1152 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1153
1153
1154 In [8]: %reset_selective -f c
1154 In [8]: %reset_selective -f c
1155
1155
1156 In [9]: who_ls
1156 In [9]: who_ls
1157 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
1157 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
1158
1158
1159 In [10]: %reset_selective -f b
1159 In [10]: %reset_selective -f b
1160
1160
1161 In [11]: who_ls
1161 In [11]: who_ls
1162 Out[11]: ['a']
1162 Out[11]: ['a']
1163 """
1163 """
1164
1164
1165 opts, regex = self.parse_options(parameter_s,'f')
1165 opts, regex = self.parse_options(parameter_s,'f')
1166
1166
1167 if opts.has_key('f'):
1167 if opts.has_key('f'):
1168 ans = True
1168 ans = True
1169 else:
1169 else:
1170 ans = self.shell.ask_yes_no(
1170 ans = self.shell.ask_yes_no(
1171 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1171 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1172 if not ans:
1172 if not ans:
1173 print 'Nothing done.'
1173 print 'Nothing done.'
1174 return
1174 return
1175 user_ns = self.shell.user_ns
1175 user_ns = self.shell.user_ns
1176 if not regex:
1176 if not regex:
1177 print 'No regex pattern specified. Nothing done.'
1177 print 'No regex pattern specified. Nothing done.'
1178 return
1178 return
1179 else:
1179 else:
1180 try:
1180 try:
1181 m = re.compile(regex)
1181 m = re.compile(regex)
1182 except TypeError:
1182 except TypeError:
1183 raise TypeError('regex must be a string or compiled pattern')
1183 raise TypeError('regex must be a string or compiled pattern')
1184 for i in self.magic_who_ls():
1184 for i in self.magic_who_ls():
1185 if m.search(i):
1185 if m.search(i):
1186 del(user_ns[i])
1186 del(user_ns[i])
1187
1187
1188 def magic_logstart(self,parameter_s=''):
1188 def magic_logstart(self,parameter_s=''):
1189 """Start logging anywhere in a session.
1189 """Start logging anywhere in a session.
1190
1190
1191 %logstart [-o|-r|-t] [log_name [log_mode]]
1191 %logstart [-o|-r|-t] [log_name [log_mode]]
1192
1192
1193 If no name is given, it defaults to a file named 'ipython_log.py' in your
1193 If no name is given, it defaults to a file named 'ipython_log.py' in your
1194 current directory, in 'rotate' mode (see below).
1194 current directory, in 'rotate' mode (see below).
1195
1195
1196 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1196 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1197 history up to that point and then continues logging.
1197 history up to that point and then continues logging.
1198
1198
1199 %logstart takes a second optional parameter: logging mode. This can be one
1199 %logstart takes a second optional parameter: logging mode. This can be one
1200 of (note that the modes are given unquoted):\\
1200 of (note that the modes are given unquoted):\\
1201 append: well, that says it.\\
1201 append: well, that says it.\\
1202 backup: rename (if exists) to name~ and start name.\\
1202 backup: rename (if exists) to name~ and start name.\\
1203 global: single logfile in your home dir, appended to.\\
1203 global: single logfile in your home dir, appended to.\\
1204 over : overwrite existing log.\\
1204 over : overwrite existing log.\\
1205 rotate: create rotating logs name.1~, name.2~, etc.
1205 rotate: create rotating logs name.1~, name.2~, etc.
1206
1206
1207 Options:
1207 Options:
1208
1208
1209 -o: log also IPython's output. In this mode, all commands which
1209 -o: log also IPython's output. In this mode, all commands which
1210 generate an Out[NN] prompt are recorded to the logfile, right after
1210 generate an Out[NN] prompt are recorded to the logfile, right after
1211 their corresponding input line. The output lines are always
1211 their corresponding input line. The output lines are always
1212 prepended with a '#[Out]# ' marker, so that the log remains valid
1212 prepended with a '#[Out]# ' marker, so that the log remains valid
1213 Python code.
1213 Python code.
1214
1214
1215 Since this marker is always the same, filtering only the output from
1215 Since this marker is always the same, filtering only the output from
1216 a log is very easy, using for example a simple awk call:
1216 a log is very easy, using for example a simple awk call:
1217
1217
1218 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1218 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1219
1219
1220 -r: log 'raw' input. Normally, IPython's logs contain the processed
1220 -r: log 'raw' input. Normally, IPython's logs contain the processed
1221 input, so that user lines are logged in their final form, converted
1221 input, so that user lines are logged in their final form, converted
1222 into valid Python. For example, %Exit is logged as
1222 into valid Python. For example, %Exit is logged as
1223 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1223 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1224 exactly as typed, with no transformations applied.
1224 exactly as typed, with no transformations applied.
1225
1225
1226 -t: put timestamps before each input line logged (these are put in
1226 -t: put timestamps before each input line logged (these are put in
1227 comments)."""
1227 comments)."""
1228
1228
1229 opts,par = self.parse_options(parameter_s,'ort')
1229 opts,par = self.parse_options(parameter_s,'ort')
1230 log_output = 'o' in opts
1230 log_output = 'o' in opts
1231 log_raw_input = 'r' in opts
1231 log_raw_input = 'r' in opts
1232 timestamp = 't' in opts
1232 timestamp = 't' in opts
1233
1233
1234 logger = self.shell.logger
1234 logger = self.shell.logger
1235
1235
1236 # if no args are given, the defaults set in the logger constructor by
1236 # if no args are given, the defaults set in the logger constructor by
1237 # ipytohn remain valid
1237 # ipytohn remain valid
1238 if par:
1238 if par:
1239 try:
1239 try:
1240 logfname,logmode = par.split()
1240 logfname,logmode = par.split()
1241 except:
1241 except:
1242 logfname = par
1242 logfname = par
1243 logmode = 'backup'
1243 logmode = 'backup'
1244 else:
1244 else:
1245 logfname = logger.logfname
1245 logfname = logger.logfname
1246 logmode = logger.logmode
1246 logmode = logger.logmode
1247 # put logfname into rc struct as if it had been called on the command
1247 # put logfname into rc struct as if it had been called on the command
1248 # line, so it ends up saved in the log header Save it in case we need
1248 # line, so it ends up saved in the log header Save it in case we need
1249 # to restore it...
1249 # to restore it...
1250 old_logfile = self.shell.logfile
1250 old_logfile = self.shell.logfile
1251 if logfname:
1251 if logfname:
1252 logfname = os.path.expanduser(logfname)
1252 logfname = os.path.expanduser(logfname)
1253 self.shell.logfile = logfname
1253 self.shell.logfile = logfname
1254
1254
1255 loghead = '# IPython log file\n\n'
1255 loghead = '# IPython log file\n\n'
1256 try:
1256 try:
1257 started = logger.logstart(logfname,loghead,logmode,
1257 started = logger.logstart(logfname,loghead,logmode,
1258 log_output,timestamp,log_raw_input)
1258 log_output,timestamp,log_raw_input)
1259 except:
1259 except:
1260 self.shell.logfile = old_logfile
1260 self.shell.logfile = old_logfile
1261 warn("Couldn't start log: %s" % sys.exc_info()[1])
1261 warn("Couldn't start log: %s" % sys.exc_info()[1])
1262 else:
1262 else:
1263 # log input history up to this point, optionally interleaving
1263 # log input history up to this point, optionally interleaving
1264 # output if requested
1264 # output if requested
1265
1265
1266 if timestamp:
1266 if timestamp:
1267 # disable timestamping for the previous history, since we've
1267 # disable timestamping for the previous history, since we've
1268 # lost those already (no time machine here).
1268 # lost those already (no time machine here).
1269 logger.timestamp = False
1269 logger.timestamp = False
1270
1270
1271 if log_raw_input:
1271 if log_raw_input:
1272 input_hist = self.shell.input_hist_raw
1272 input_hist = self.shell.input_hist_raw
1273 else:
1273 else:
1274 input_hist = self.shell.input_hist
1274 input_hist = self.shell.input_hist
1275
1275
1276 if log_output:
1276 if log_output:
1277 log_write = logger.log_write
1277 log_write = logger.log_write
1278 output_hist = self.shell.output_hist
1278 output_hist = self.shell.output_hist
1279 for n in range(1,len(input_hist)-1):
1279 for n in range(1,len(input_hist)-1):
1280 log_write(input_hist[n].rstrip())
1280 log_write(input_hist[n].rstrip())
1281 if n in output_hist:
1281 if n in output_hist:
1282 log_write(repr(output_hist[n]),'output')
1282 log_write(repr(output_hist[n]),'output')
1283 else:
1283 else:
1284 logger.log_write(input_hist[1:])
1284 logger.log_write(input_hist[1:])
1285 if timestamp:
1285 if timestamp:
1286 # re-enable timestamping
1286 # re-enable timestamping
1287 logger.timestamp = True
1287 logger.timestamp = True
1288
1288
1289 print ('Activating auto-logging. '
1289 print ('Activating auto-logging. '
1290 'Current session state plus future input saved.')
1290 'Current session state plus future input saved.')
1291 logger.logstate()
1291 logger.logstate()
1292
1292
1293 def magic_logstop(self,parameter_s=''):
1293 def magic_logstop(self,parameter_s=''):
1294 """Fully stop logging and close log file.
1294 """Fully stop logging and close log file.
1295
1295
1296 In order to start logging again, a new %logstart call needs to be made,
1296 In order to start logging again, a new %logstart call needs to be made,
1297 possibly (though not necessarily) with a new filename, mode and other
1297 possibly (though not necessarily) with a new filename, mode and other
1298 options."""
1298 options."""
1299 self.logger.logstop()
1299 self.logger.logstop()
1300
1300
1301 def magic_logoff(self,parameter_s=''):
1301 def magic_logoff(self,parameter_s=''):
1302 """Temporarily stop logging.
1302 """Temporarily stop logging.
1303
1303
1304 You must have previously started logging."""
1304 You must have previously started logging."""
1305 self.shell.logger.switch_log(0)
1305 self.shell.logger.switch_log(0)
1306
1306
1307 def magic_logon(self,parameter_s=''):
1307 def magic_logon(self,parameter_s=''):
1308 """Restart logging.
1308 """Restart logging.
1309
1309
1310 This function is for restarting logging which you've temporarily
1310 This function is for restarting logging which you've temporarily
1311 stopped with %logoff. For starting logging for the first time, you
1311 stopped with %logoff. For starting logging for the first time, you
1312 must use the %logstart function, which allows you to specify an
1312 must use the %logstart function, which allows you to specify an
1313 optional log filename."""
1313 optional log filename."""
1314
1314
1315 self.shell.logger.switch_log(1)
1315 self.shell.logger.switch_log(1)
1316
1316
1317 def magic_logstate(self,parameter_s=''):
1317 def magic_logstate(self,parameter_s=''):
1318 """Print the status of the logging system."""
1318 """Print the status of the logging system."""
1319
1319
1320 self.shell.logger.logstate()
1320 self.shell.logger.logstate()
1321
1321
1322 def magic_pdb(self, parameter_s=''):
1322 def magic_pdb(self, parameter_s=''):
1323 """Control the automatic calling of the pdb interactive debugger.
1323 """Control the automatic calling of the pdb interactive debugger.
1324
1324
1325 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1325 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1326 argument it works as a toggle.
1326 argument it works as a toggle.
1327
1327
1328 When an exception is triggered, IPython can optionally call the
1328 When an exception is triggered, IPython can optionally call the
1329 interactive pdb debugger after the traceback printout. %pdb toggles
1329 interactive pdb debugger after the traceback printout. %pdb toggles
1330 this feature on and off.
1330 this feature on and off.
1331
1331
1332 The initial state of this feature is set in your ipythonrc
1332 The initial state of this feature is set in your ipythonrc
1333 configuration file (the variable is called 'pdb').
1333 configuration file (the variable is called 'pdb').
1334
1334
1335 If you want to just activate the debugger AFTER an exception has fired,
1335 If you want to just activate the debugger AFTER an exception has fired,
1336 without having to type '%pdb on' and rerunning your code, you can use
1336 without having to type '%pdb on' and rerunning your code, you can use
1337 the %debug magic."""
1337 the %debug magic."""
1338
1338
1339 par = parameter_s.strip().lower()
1339 par = parameter_s.strip().lower()
1340
1340
1341 if par:
1341 if par:
1342 try:
1342 try:
1343 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1343 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1344 except KeyError:
1344 except KeyError:
1345 print ('Incorrect argument. Use on/1, off/0, '
1345 print ('Incorrect argument. Use on/1, off/0, '
1346 'or nothing for a toggle.')
1346 'or nothing for a toggle.')
1347 return
1347 return
1348 else:
1348 else:
1349 # toggle
1349 # toggle
1350 new_pdb = not self.shell.call_pdb
1350 new_pdb = not self.shell.call_pdb
1351
1351
1352 # set on the shell
1352 # set on the shell
1353 self.shell.call_pdb = new_pdb
1353 self.shell.call_pdb = new_pdb
1354 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1354 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1355
1355
1356 def magic_debug(self, parameter_s=''):
1356 def magic_debug(self, parameter_s=''):
1357 """Activate the interactive debugger in post-mortem mode.
1357 """Activate the interactive debugger in post-mortem mode.
1358
1358
1359 If an exception has just occurred, this lets you inspect its stack
1359 If an exception has just occurred, this lets you inspect its stack
1360 frames interactively. Note that this will always work only on the last
1360 frames interactively. Note that this will always work only on the last
1361 traceback that occurred, so you must call this quickly after an
1361 traceback that occurred, so you must call this quickly after an
1362 exception that you wish to inspect has fired, because if another one
1362 exception that you wish to inspect has fired, because if another one
1363 occurs, it clobbers the previous one.
1363 occurs, it clobbers the previous one.
1364
1364
1365 If you want IPython to automatically do this on every exception, see
1365 If you want IPython to automatically do this on every exception, see
1366 the %pdb magic for more details.
1366 the %pdb magic for more details.
1367 """
1367 """
1368 self.shell.debugger(force=True)
1368 self.shell.debugger(force=True)
1369
1369
1370 @testdec.skip_doctest
1370 @testdec.skip_doctest
1371 def magic_prun(self, parameter_s ='',user_mode=1,
1371 def magic_prun(self, parameter_s ='',user_mode=1,
1372 opts=None,arg_lst=None,prog_ns=None):
1372 opts=None,arg_lst=None,prog_ns=None):
1373
1373
1374 """Run a statement through the python code profiler.
1374 """Run a statement through the python code profiler.
1375
1375
1376 Usage:
1376 Usage:
1377 %prun [options] statement
1377 %prun [options] statement
1378
1378
1379 The given statement (which doesn't require quote marks) is run via the
1379 The given statement (which doesn't require quote marks) is run via the
1380 python profiler in a manner similar to the profile.run() function.
1380 python profiler in a manner similar to the profile.run() function.
1381 Namespaces are internally managed to work correctly; profile.run
1381 Namespaces are internally managed to work correctly; profile.run
1382 cannot be used in IPython because it makes certain assumptions about
1382 cannot be used in IPython because it makes certain assumptions about
1383 namespaces which do not hold under IPython.
1383 namespaces which do not hold under IPython.
1384
1384
1385 Options:
1385 Options:
1386
1386
1387 -l <limit>: you can place restrictions on what or how much of the
1387 -l <limit>: you can place restrictions on what or how much of the
1388 profile gets printed. The limit value can be:
1388 profile gets printed. The limit value can be:
1389
1389
1390 * A string: only information for function names containing this string
1390 * A string: only information for function names containing this string
1391 is printed.
1391 is printed.
1392
1392
1393 * An integer: only these many lines are printed.
1393 * An integer: only these many lines are printed.
1394
1394
1395 * A float (between 0 and 1): this fraction of the report is printed
1395 * A float (between 0 and 1): this fraction of the report is printed
1396 (for example, use a limit of 0.4 to see the topmost 40% only).
1396 (for example, use a limit of 0.4 to see the topmost 40% only).
1397
1397
1398 You can combine several limits with repeated use of the option. For
1398 You can combine several limits with repeated use of the option. For
1399 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1399 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1400 information about class constructors.
1400 information about class constructors.
1401
1401
1402 -r: return the pstats.Stats object generated by the profiling. This
1402 -r: return the pstats.Stats object generated by the profiling. This
1403 object has all the information about the profile in it, and you can
1403 object has all the information about the profile in it, and you can
1404 later use it for further analysis or in other functions.
1404 later use it for further analysis or in other functions.
1405
1405
1406 -s <key>: sort profile by given key. You can provide more than one key
1406 -s <key>: sort profile by given key. You can provide more than one key
1407 by using the option several times: '-s key1 -s key2 -s key3...'. The
1407 by using the option several times: '-s key1 -s key2 -s key3...'. The
1408 default sorting key is 'time'.
1408 default sorting key is 'time'.
1409
1409
1410 The following is copied verbatim from the profile documentation
1410 The following is copied verbatim from the profile documentation
1411 referenced below:
1411 referenced below:
1412
1412
1413 When more than one key is provided, additional keys are used as
1413 When more than one key is provided, additional keys are used as
1414 secondary criteria when the there is equality in all keys selected
1414 secondary criteria when the there is equality in all keys selected
1415 before them.
1415 before them.
1416
1416
1417 Abbreviations can be used for any key names, as long as the
1417 Abbreviations can be used for any key names, as long as the
1418 abbreviation is unambiguous. The following are the keys currently
1418 abbreviation is unambiguous. The following are the keys currently
1419 defined:
1419 defined:
1420
1420
1421 Valid Arg Meaning
1421 Valid Arg Meaning
1422 "calls" call count
1422 "calls" call count
1423 "cumulative" cumulative time
1423 "cumulative" cumulative time
1424 "file" file name
1424 "file" file name
1425 "module" file name
1425 "module" file name
1426 "pcalls" primitive call count
1426 "pcalls" primitive call count
1427 "line" line number
1427 "line" line number
1428 "name" function name
1428 "name" function name
1429 "nfl" name/file/line
1429 "nfl" name/file/line
1430 "stdname" standard name
1430 "stdname" standard name
1431 "time" internal time
1431 "time" internal time
1432
1432
1433 Note that all sorts on statistics are in descending order (placing
1433 Note that all sorts on statistics are in descending order (placing
1434 most time consuming items first), where as name, file, and line number
1434 most time consuming items first), where as name, file, and line number
1435 searches are in ascending order (i.e., alphabetical). The subtle
1435 searches are in ascending order (i.e., alphabetical). The subtle
1436 distinction between "nfl" and "stdname" is that the standard name is a
1436 distinction between "nfl" and "stdname" is that the standard name is a
1437 sort of the name as printed, which means that the embedded line
1437 sort of the name as printed, which means that the embedded line
1438 numbers get compared in an odd way. For example, lines 3, 20, and 40
1438 numbers get compared in an odd way. For example, lines 3, 20, and 40
1439 would (if the file names were the same) appear in the string order
1439 would (if the file names were the same) appear in the string order
1440 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1440 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1441 line numbers. In fact, sort_stats("nfl") is the same as
1441 line numbers. In fact, sort_stats("nfl") is the same as
1442 sort_stats("name", "file", "line").
1442 sort_stats("name", "file", "line").
1443
1443
1444 -T <filename>: save profile results as shown on screen to a text
1444 -T <filename>: save profile results as shown on screen to a text
1445 file. The profile is still shown on screen.
1445 file. The profile is still shown on screen.
1446
1446
1447 -D <filename>: save (via dump_stats) profile statistics to given
1447 -D <filename>: save (via dump_stats) profile statistics to given
1448 filename. This data is in a format understod by the pstats module, and
1448 filename. This data is in a format understod by the pstats module, and
1449 is generated by a call to the dump_stats() method of profile
1449 is generated by a call to the dump_stats() method of profile
1450 objects. The profile is still shown on screen.
1450 objects. The profile is still shown on screen.
1451
1451
1452 If you want to run complete programs under the profiler's control, use
1452 If you want to run complete programs under the profiler's control, use
1453 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1453 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1454 contains profiler specific options as described here.
1454 contains profiler specific options as described here.
1455
1455
1456 You can read the complete documentation for the profile module with::
1456 You can read the complete documentation for the profile module with::
1457
1457
1458 In [1]: import profile; profile.help()
1458 In [1]: import profile; profile.help()
1459 """
1459 """
1460
1460
1461 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1461 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1462 # protect user quote marks
1462 # protect user quote marks
1463 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1463 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1464
1464
1465 if user_mode: # regular user call
1465 if user_mode: # regular user call
1466 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1466 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1467 list_all=1)
1467 list_all=1)
1468 namespace = self.shell.user_ns
1468 namespace = self.shell.user_ns
1469 else: # called to run a program by %run -p
1469 else: # called to run a program by %run -p
1470 try:
1470 try:
1471 filename = get_py_filename(arg_lst[0])
1471 filename = get_py_filename(arg_lst[0])
1472 except IOError,msg:
1472 except IOError,msg:
1473 error(msg)
1473 error(msg)
1474 return
1474 return
1475
1475
1476 arg_str = 'execfile(filename,prog_ns)'
1476 arg_str = 'execfile(filename,prog_ns)'
1477 namespace = locals()
1477 namespace = locals()
1478
1478
1479 opts.merge(opts_def)
1479 opts.merge(opts_def)
1480
1480
1481 prof = profile.Profile()
1481 prof = profile.Profile()
1482 try:
1482 try:
1483 prof = prof.runctx(arg_str,namespace,namespace)
1483 prof = prof.runctx(arg_str,namespace,namespace)
1484 sys_exit = ''
1484 sys_exit = ''
1485 except SystemExit:
1485 except SystemExit:
1486 sys_exit = """*** SystemExit exception caught in code being profiled."""
1486 sys_exit = """*** SystemExit exception caught in code being profiled."""
1487
1487
1488 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1488 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1489
1489
1490 lims = opts.l
1490 lims = opts.l
1491 if lims:
1491 if lims:
1492 lims = [] # rebuild lims with ints/floats/strings
1492 lims = [] # rebuild lims with ints/floats/strings
1493 for lim in opts.l:
1493 for lim in opts.l:
1494 try:
1494 try:
1495 lims.append(int(lim))
1495 lims.append(int(lim))
1496 except ValueError:
1496 except ValueError:
1497 try:
1497 try:
1498 lims.append(float(lim))
1498 lims.append(float(lim))
1499 except ValueError:
1499 except ValueError:
1500 lims.append(lim)
1500 lims.append(lim)
1501
1501
1502 # Trap output.
1502 # Trap output.
1503 stdout_trap = StringIO()
1503 stdout_trap = StringIO()
1504
1504
1505 if hasattr(stats,'stream'):
1505 if hasattr(stats,'stream'):
1506 # In newer versions of python, the stats object has a 'stream'
1506 # In newer versions of python, the stats object has a 'stream'
1507 # attribute to write into.
1507 # attribute to write into.
1508 stats.stream = stdout_trap
1508 stats.stream = stdout_trap
1509 stats.print_stats(*lims)
1509 stats.print_stats(*lims)
1510 else:
1510 else:
1511 # For older versions, we manually redirect stdout during printing
1511 # For older versions, we manually redirect stdout during printing
1512 sys_stdout = sys.stdout
1512 sys_stdout = sys.stdout
1513 try:
1513 try:
1514 sys.stdout = stdout_trap
1514 sys.stdout = stdout_trap
1515 stats.print_stats(*lims)
1515 stats.print_stats(*lims)
1516 finally:
1516 finally:
1517 sys.stdout = sys_stdout
1517 sys.stdout = sys_stdout
1518
1518
1519 output = stdout_trap.getvalue()
1519 output = stdout_trap.getvalue()
1520 output = output.rstrip()
1520 output = output.rstrip()
1521
1521
1522 page(output,screen_lines=self.shell.usable_screen_length)
1522 page(output,screen_lines=self.shell.usable_screen_length)
1523 print sys_exit,
1523 print sys_exit,
1524
1524
1525 dump_file = opts.D[0]
1525 dump_file = opts.D[0]
1526 text_file = opts.T[0]
1526 text_file = opts.T[0]
1527 if dump_file:
1527 if dump_file:
1528 prof.dump_stats(dump_file)
1528 prof.dump_stats(dump_file)
1529 print '\n*** Profile stats marshalled to file',\
1529 print '\n*** Profile stats marshalled to file',\
1530 `dump_file`+'.',sys_exit
1530 `dump_file`+'.',sys_exit
1531 if text_file:
1531 if text_file:
1532 pfile = file(text_file,'w')
1532 pfile = file(text_file,'w')
1533 pfile.write(output)
1533 pfile.write(output)
1534 pfile.close()
1534 pfile.close()
1535 print '\n*** Profile printout saved to text file',\
1535 print '\n*** Profile printout saved to text file',\
1536 `text_file`+'.',sys_exit
1536 `text_file`+'.',sys_exit
1537
1537
1538 if opts.has_key('r'):
1538 if opts.has_key('r'):
1539 return stats
1539 return stats
1540 else:
1540 else:
1541 return None
1541 return None
1542
1542
1543 @testdec.skip_doctest
1543 @testdec.skip_doctest
1544 def magic_run(self, parameter_s ='',runner=None,
1544 def magic_run(self, parameter_s ='',runner=None,
1545 file_finder=get_py_filename):
1545 file_finder=get_py_filename):
1546 """Run the named file inside IPython as a program.
1546 """Run the named file inside IPython as a program.
1547
1547
1548 Usage:\\
1548 Usage:\\
1549 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1549 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1550
1550
1551 Parameters after the filename are passed as command-line arguments to
1551 Parameters after the filename are passed as command-line arguments to
1552 the program (put in sys.argv). Then, control returns to IPython's
1552 the program (put in sys.argv). Then, control returns to IPython's
1553 prompt.
1553 prompt.
1554
1554
1555 This is similar to running at a system prompt:\\
1555 This is similar to running at a system prompt:\\
1556 $ python file args\\
1556 $ python file args\\
1557 but with the advantage of giving you IPython's tracebacks, and of
1557 but with the advantage of giving you IPython's tracebacks, and of
1558 loading all variables into your interactive namespace for further use
1558 loading all variables into your interactive namespace for further use
1559 (unless -p is used, see below).
1559 (unless -p is used, see below).
1560
1560
1561 The file is executed in a namespace initially consisting only of
1561 The file is executed in a namespace initially consisting only of
1562 __name__=='__main__' and sys.argv constructed as indicated. It thus
1562 __name__=='__main__' and sys.argv constructed as indicated. It thus
1563 sees its environment as if it were being run as a stand-alone program
1563 sees its environment as if it were being run as a stand-alone program
1564 (except for sharing global objects such as previously imported
1564 (except for sharing global objects such as previously imported
1565 modules). But after execution, the IPython interactive namespace gets
1565 modules). But after execution, the IPython interactive namespace gets
1566 updated with all variables defined in the program (except for __name__
1566 updated with all variables defined in the program (except for __name__
1567 and sys.argv). This allows for very convenient loading of code for
1567 and sys.argv). This allows for very convenient loading of code for
1568 interactive work, while giving each program a 'clean sheet' to run in.
1568 interactive work, while giving each program a 'clean sheet' to run in.
1569
1569
1570 Options:
1570 Options:
1571
1571
1572 -n: __name__ is NOT set to '__main__', but to the running file's name
1572 -n: __name__ is NOT set to '__main__', but to the running file's name
1573 without extension (as python does under import). This allows running
1573 without extension (as python does under import). This allows running
1574 scripts and reloading the definitions in them without calling code
1574 scripts and reloading the definitions in them without calling code
1575 protected by an ' if __name__ == "__main__" ' clause.
1575 protected by an ' if __name__ == "__main__" ' clause.
1576
1576
1577 -i: run the file in IPython's namespace instead of an empty one. This
1577 -i: run the file in IPython's namespace instead of an empty one. This
1578 is useful if you are experimenting with code written in a text editor
1578 is useful if you are experimenting with code written in a text editor
1579 which depends on variables defined interactively.
1579 which depends on variables defined interactively.
1580
1580
1581 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1581 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1582 being run. This is particularly useful if IPython is being used to
1582 being run. This is particularly useful if IPython is being used to
1583 run unittests, which always exit with a sys.exit() call. In such
1583 run unittests, which always exit with a sys.exit() call. In such
1584 cases you are interested in the output of the test results, not in
1584 cases you are interested in the output of the test results, not in
1585 seeing a traceback of the unittest module.
1585 seeing a traceback of the unittest module.
1586
1586
1587 -t: print timing information at the end of the run. IPython will give
1587 -t: print timing information at the end of the run. IPython will give
1588 you an estimated CPU time consumption for your script, which under
1588 you an estimated CPU time consumption for your script, which under
1589 Unix uses the resource module to avoid the wraparound problems of
1589 Unix uses the resource module to avoid the wraparound problems of
1590 time.clock(). Under Unix, an estimate of time spent on system tasks
1590 time.clock(). Under Unix, an estimate of time spent on system tasks
1591 is also given (for Windows platforms this is reported as 0.0).
1591 is also given (for Windows platforms this is reported as 0.0).
1592
1592
1593 If -t is given, an additional -N<N> option can be given, where <N>
1593 If -t is given, an additional -N<N> option can be given, where <N>
1594 must be an integer indicating how many times you want the script to
1594 must be an integer indicating how many times you want the script to
1595 run. The final timing report will include total and per run results.
1595 run. The final timing report will include total and per run results.
1596
1596
1597 For example (testing the script uniq_stable.py):
1597 For example (testing the script uniq_stable.py):
1598
1598
1599 In [1]: run -t uniq_stable
1599 In [1]: run -t uniq_stable
1600
1600
1601 IPython CPU timings (estimated):\\
1601 IPython CPU timings (estimated):\\
1602 User : 0.19597 s.\\
1602 User : 0.19597 s.\\
1603 System: 0.0 s.\\
1603 System: 0.0 s.\\
1604
1604
1605 In [2]: run -t -N5 uniq_stable
1605 In [2]: run -t -N5 uniq_stable
1606
1606
1607 IPython CPU timings (estimated):\\
1607 IPython CPU timings (estimated):\\
1608 Total runs performed: 5\\
1608 Total runs performed: 5\\
1609 Times : Total Per run\\
1609 Times : Total Per run\\
1610 User : 0.910862 s, 0.1821724 s.\\
1610 User : 0.910862 s, 0.1821724 s.\\
1611 System: 0.0 s, 0.0 s.
1611 System: 0.0 s, 0.0 s.
1612
1612
1613 -d: run your program under the control of pdb, the Python debugger.
1613 -d: run your program under the control of pdb, the Python debugger.
1614 This allows you to execute your program step by step, watch variables,
1614 This allows you to execute your program step by step, watch variables,
1615 etc. Internally, what IPython does is similar to calling:
1615 etc. Internally, what IPython does is similar to calling:
1616
1616
1617 pdb.run('execfile("YOURFILENAME")')
1617 pdb.run('execfile("YOURFILENAME")')
1618
1618
1619 with a breakpoint set on line 1 of your file. You can change the line
1619 with a breakpoint set on line 1 of your file. You can change the line
1620 number for this automatic breakpoint to be <N> by using the -bN option
1620 number for this automatic breakpoint to be <N> by using the -bN option
1621 (where N must be an integer). For example:
1621 (where N must be an integer). For example:
1622
1622
1623 %run -d -b40 myscript
1623 %run -d -b40 myscript
1624
1624
1625 will set the first breakpoint at line 40 in myscript.py. Note that
1625 will set the first breakpoint at line 40 in myscript.py. Note that
1626 the first breakpoint must be set on a line which actually does
1626 the first breakpoint must be set on a line which actually does
1627 something (not a comment or docstring) for it to stop execution.
1627 something (not a comment or docstring) for it to stop execution.
1628
1628
1629 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1629 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1630 first enter 'c' (without qoutes) to start execution up to the first
1630 first enter 'c' (without qoutes) to start execution up to the first
1631 breakpoint.
1631 breakpoint.
1632
1632
1633 Entering 'help' gives information about the use of the debugger. You
1633 Entering 'help' gives information about the use of the debugger. You
1634 can easily see pdb's full documentation with "import pdb;pdb.help()"
1634 can easily see pdb's full documentation with "import pdb;pdb.help()"
1635 at a prompt.
1635 at a prompt.
1636
1636
1637 -p: run program under the control of the Python profiler module (which
1637 -p: run program under the control of the Python profiler module (which
1638 prints a detailed report of execution times, function calls, etc).
1638 prints a detailed report of execution times, function calls, etc).
1639
1639
1640 You can pass other options after -p which affect the behavior of the
1640 You can pass other options after -p which affect the behavior of the
1641 profiler itself. See the docs for %prun for details.
1641 profiler itself. See the docs for %prun for details.
1642
1642
1643 In this mode, the program's variables do NOT propagate back to the
1643 In this mode, the program's variables do NOT propagate back to the
1644 IPython interactive namespace (because they remain in the namespace
1644 IPython interactive namespace (because they remain in the namespace
1645 where the profiler executes them).
1645 where the profiler executes them).
1646
1646
1647 Internally this triggers a call to %prun, see its documentation for
1647 Internally this triggers a call to %prun, see its documentation for
1648 details on the options available specifically for profiling.
1648 details on the options available specifically for profiling.
1649
1649
1650 There is one special usage for which the text above doesn't apply:
1650 There is one special usage for which the text above doesn't apply:
1651 if the filename ends with .ipy, the file is run as ipython script,
1651 if the filename ends with .ipy, the file is run as ipython script,
1652 just as if the commands were written on IPython prompt.
1652 just as if the commands were written on IPython prompt.
1653 """
1653 """
1654
1654
1655 # get arguments and set sys.argv for program to be run.
1655 # get arguments and set sys.argv for program to be run.
1656 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1656 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1657 mode='list',list_all=1)
1657 mode='list',list_all=1)
1658
1658
1659 try:
1659 try:
1660 filename = file_finder(arg_lst[0])
1660 filename = file_finder(arg_lst[0])
1661 except IndexError:
1661 except IndexError:
1662 warn('you must provide at least a filename.')
1662 warn('you must provide at least a filename.')
1663 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1663 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1664 return
1664 return
1665 except IOError,msg:
1665 except IOError,msg:
1666 error(msg)
1666 error(msg)
1667 return
1667 return
1668
1668
1669 if filename.lower().endswith('.ipy'):
1669 if filename.lower().endswith('.ipy'):
1670 self.shell.safe_execfile_ipy(filename)
1670 self.shell.safe_execfile_ipy(filename)
1671 return
1671 return
1672
1672
1673 # Control the response to exit() calls made by the script being run
1673 # Control the response to exit() calls made by the script being run
1674 exit_ignore = opts.has_key('e')
1674 exit_ignore = opts.has_key('e')
1675
1675
1676 # Make sure that the running script gets a proper sys.argv as if it
1676 # Make sure that the running script gets a proper sys.argv as if it
1677 # were run from a system shell.
1677 # were run from a system shell.
1678 save_argv = sys.argv # save it for later restoring
1678 save_argv = sys.argv # save it for later restoring
1679 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1679 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1680
1680
1681 if opts.has_key('i'):
1681 if opts.has_key('i'):
1682 # Run in user's interactive namespace
1682 # Run in user's interactive namespace
1683 prog_ns = self.shell.user_ns
1683 prog_ns = self.shell.user_ns
1684 __name__save = self.shell.user_ns['__name__']
1684 __name__save = self.shell.user_ns['__name__']
1685 prog_ns['__name__'] = '__main__'
1685 prog_ns['__name__'] = '__main__'
1686 main_mod = self.shell.new_main_mod(prog_ns)
1686 main_mod = self.shell.new_main_mod(prog_ns)
1687 else:
1687 else:
1688 # Run in a fresh, empty namespace
1688 # Run in a fresh, empty namespace
1689 if opts.has_key('n'):
1689 if opts.has_key('n'):
1690 name = os.path.splitext(os.path.basename(filename))[0]
1690 name = os.path.splitext(os.path.basename(filename))[0]
1691 else:
1691 else:
1692 name = '__main__'
1692 name = '__main__'
1693
1693
1694 main_mod = self.shell.new_main_mod()
1694 main_mod = self.shell.new_main_mod()
1695 prog_ns = main_mod.__dict__
1695 prog_ns = main_mod.__dict__
1696 prog_ns['__name__'] = name
1696 prog_ns['__name__'] = name
1697
1697
1698 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1698 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1699 # set the __file__ global in the script's namespace
1699 # set the __file__ global in the script's namespace
1700 prog_ns['__file__'] = filename
1700 prog_ns['__file__'] = filename
1701
1701
1702 # pickle fix. See interactiveshell for an explanation. But we need to make sure
1702 # pickle fix. See interactiveshell for an explanation. But we need to make sure
1703 # that, if we overwrite __main__, we replace it at the end
1703 # that, if we overwrite __main__, we replace it at the end
1704 main_mod_name = prog_ns['__name__']
1704 main_mod_name = prog_ns['__name__']
1705
1705
1706 if main_mod_name == '__main__':
1706 if main_mod_name == '__main__':
1707 restore_main = sys.modules['__main__']
1707 restore_main = sys.modules['__main__']
1708 else:
1708 else:
1709 restore_main = False
1709 restore_main = False
1710
1710
1711 # This needs to be undone at the end to prevent holding references to
1711 # This needs to be undone at the end to prevent holding references to
1712 # every single object ever created.
1712 # every single object ever created.
1713 sys.modules[main_mod_name] = main_mod
1713 sys.modules[main_mod_name] = main_mod
1714
1714
1715 stats = None
1715 stats = None
1716 try:
1716 try:
1717 self.shell.savehist()
1717 self.shell.savehist()
1718
1718
1719 if opts.has_key('p'):
1719 if opts.has_key('p'):
1720 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1720 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1721 else:
1721 else:
1722 if opts.has_key('d'):
1722 if opts.has_key('d'):
1723 deb = debugger.Pdb(self.shell.colors)
1723 deb = debugger.Pdb(self.shell.colors)
1724 # reset Breakpoint state, which is moronically kept
1724 # reset Breakpoint state, which is moronically kept
1725 # in a class
1725 # in a class
1726 bdb.Breakpoint.next = 1
1726 bdb.Breakpoint.next = 1
1727 bdb.Breakpoint.bplist = {}
1727 bdb.Breakpoint.bplist = {}
1728 bdb.Breakpoint.bpbynumber = [None]
1728 bdb.Breakpoint.bpbynumber = [None]
1729 # Set an initial breakpoint to stop execution
1729 # Set an initial breakpoint to stop execution
1730 maxtries = 10
1730 maxtries = 10
1731 bp = int(opts.get('b',[1])[0])
1731 bp = int(opts.get('b',[1])[0])
1732 checkline = deb.checkline(filename,bp)
1732 checkline = deb.checkline(filename,bp)
1733 if not checkline:
1733 if not checkline:
1734 for bp in range(bp+1,bp+maxtries+1):
1734 for bp in range(bp+1,bp+maxtries+1):
1735 if deb.checkline(filename,bp):
1735 if deb.checkline(filename,bp):
1736 break
1736 break
1737 else:
1737 else:
1738 msg = ("\nI failed to find a valid line to set "
1738 msg = ("\nI failed to find a valid line to set "
1739 "a breakpoint\n"
1739 "a breakpoint\n"
1740 "after trying up to line: %s.\n"
1740 "after trying up to line: %s.\n"
1741 "Please set a valid breakpoint manually "
1741 "Please set a valid breakpoint manually "
1742 "with the -b option." % bp)
1742 "with the -b option." % bp)
1743 error(msg)
1743 error(msg)
1744 return
1744 return
1745 # if we find a good linenumber, set the breakpoint
1745 # if we find a good linenumber, set the breakpoint
1746 deb.do_break('%s:%s' % (filename,bp))
1746 deb.do_break('%s:%s' % (filename,bp))
1747 # Start file run
1747 # Start file run
1748 print "NOTE: Enter 'c' at the",
1748 print "NOTE: Enter 'c' at the",
1749 print "%s prompt to start your script." % deb.prompt
1749 print "%s prompt to start your script." % deb.prompt
1750 try:
1750 try:
1751 deb.run('execfile("%s")' % filename,prog_ns)
1751 deb.run('execfile("%s")' % filename,prog_ns)
1752
1752
1753 except:
1753 except:
1754 etype, value, tb = sys.exc_info()
1754 etype, value, tb = sys.exc_info()
1755 # Skip three frames in the traceback: the %run one,
1755 # Skip three frames in the traceback: the %run one,
1756 # one inside bdb.py, and the command-line typed by the
1756 # one inside bdb.py, and the command-line typed by the
1757 # user (run by exec in pdb itself).
1757 # user (run by exec in pdb itself).
1758 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1758 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1759 else:
1759 else:
1760 if runner is None:
1760 if runner is None:
1761 runner = self.shell.safe_execfile
1761 runner = self.shell.safe_execfile
1762 if opts.has_key('t'):
1762 if opts.has_key('t'):
1763 # timed execution
1763 # timed execution
1764 try:
1764 try:
1765 nruns = int(opts['N'][0])
1765 nruns = int(opts['N'][0])
1766 if nruns < 1:
1766 if nruns < 1:
1767 error('Number of runs must be >=1')
1767 error('Number of runs must be >=1')
1768 return
1768 return
1769 except (KeyError):
1769 except (KeyError):
1770 nruns = 1
1770 nruns = 1
1771 if nruns == 1:
1771 if nruns == 1:
1772 t0 = clock2()
1772 t0 = clock2()
1773 runner(filename,prog_ns,prog_ns,
1773 runner(filename,prog_ns,prog_ns,
1774 exit_ignore=exit_ignore)
1774 exit_ignore=exit_ignore)
1775 t1 = clock2()
1775 t1 = clock2()
1776 t_usr = t1[0]-t0[0]
1776 t_usr = t1[0]-t0[0]
1777 t_sys = t1[1]-t0[1]
1777 t_sys = t1[1]-t0[1]
1778 print "\nIPython CPU timings (estimated):"
1778 print "\nIPython CPU timings (estimated):"
1779 print " User : %10s s." % t_usr
1779 print " User : %10s s." % t_usr
1780 print " System: %10s s." % t_sys
1780 print " System: %10s s." % t_sys
1781 else:
1781 else:
1782 runs = range(nruns)
1782 runs = range(nruns)
1783 t0 = clock2()
1783 t0 = clock2()
1784 for nr in runs:
1784 for nr in runs:
1785 runner(filename,prog_ns,prog_ns,
1785 runner(filename,prog_ns,prog_ns,
1786 exit_ignore=exit_ignore)
1786 exit_ignore=exit_ignore)
1787 t1 = clock2()
1787 t1 = clock2()
1788 t_usr = t1[0]-t0[0]
1788 t_usr = t1[0]-t0[0]
1789 t_sys = t1[1]-t0[1]
1789 t_sys = t1[1]-t0[1]
1790 print "\nIPython CPU timings (estimated):"
1790 print "\nIPython CPU timings (estimated):"
1791 print "Total runs performed:",nruns
1791 print "Total runs performed:",nruns
1792 print " Times : %10s %10s" % ('Total','Per run')
1792 print " Times : %10s %10s" % ('Total','Per run')
1793 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1793 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1794 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1794 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1795
1795
1796 else:
1796 else:
1797 # regular execution
1797 # regular execution
1798 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1798 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1799
1799
1800 if opts.has_key('i'):
1800 if opts.has_key('i'):
1801 self.shell.user_ns['__name__'] = __name__save
1801 self.shell.user_ns['__name__'] = __name__save
1802 else:
1802 else:
1803 # The shell MUST hold a reference to prog_ns so after %run
1803 # The shell MUST hold a reference to prog_ns so after %run
1804 # exits, the python deletion mechanism doesn't zero it out
1804 # exits, the python deletion mechanism doesn't zero it out
1805 # (leaving dangling references).
1805 # (leaving dangling references).
1806 self.shell.cache_main_mod(prog_ns,filename)
1806 self.shell.cache_main_mod(prog_ns,filename)
1807 # update IPython interactive namespace
1807 # update IPython interactive namespace
1808
1808
1809 # Some forms of read errors on the file may mean the
1809 # Some forms of read errors on the file may mean the
1810 # __name__ key was never set; using pop we don't have to
1810 # __name__ key was never set; using pop we don't have to
1811 # worry about a possible KeyError.
1811 # worry about a possible KeyError.
1812 prog_ns.pop('__name__', None)
1812 prog_ns.pop('__name__', None)
1813
1813
1814 self.shell.user_ns.update(prog_ns)
1814 self.shell.user_ns.update(prog_ns)
1815 finally:
1815 finally:
1816 # It's a bit of a mystery why, but __builtins__ can change from
1816 # It's a bit of a mystery why, but __builtins__ can change from
1817 # being a module to becoming a dict missing some key data after
1817 # being a module to becoming a dict missing some key data after
1818 # %run. As best I can see, this is NOT something IPython is doing
1818 # %run. As best I can see, this is NOT something IPython is doing
1819 # at all, and similar problems have been reported before:
1819 # at all, and similar problems have been reported before:
1820 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
1820 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
1821 # Since this seems to be done by the interpreter itself, the best
1821 # Since this seems to be done by the interpreter itself, the best
1822 # we can do is to at least restore __builtins__ for the user on
1822 # we can do is to at least restore __builtins__ for the user on
1823 # exit.
1823 # exit.
1824 self.shell.user_ns['__builtins__'] = __builtin__
1824 self.shell.user_ns['__builtins__'] = __builtin__
1825
1825
1826 # Ensure key global structures are restored
1826 # Ensure key global structures are restored
1827 sys.argv = save_argv
1827 sys.argv = save_argv
1828 if restore_main:
1828 if restore_main:
1829 sys.modules['__main__'] = restore_main
1829 sys.modules['__main__'] = restore_main
1830 else:
1830 else:
1831 # Remove from sys.modules the reference to main_mod we'd
1831 # Remove from sys.modules the reference to main_mod we'd
1832 # added. Otherwise it will trap references to objects
1832 # added. Otherwise it will trap references to objects
1833 # contained therein.
1833 # contained therein.
1834 del sys.modules[main_mod_name]
1834 del sys.modules[main_mod_name]
1835
1835
1836 self.shell.reloadhist()
1836 self.shell.reloadhist()
1837
1837
1838 return stats
1838 return stats
1839
1839
1840 @testdec.skip_doctest
1840 @testdec.skip_doctest
1841 def magic_timeit(self, parameter_s =''):
1841 def magic_timeit(self, parameter_s =''):
1842 """Time execution of a Python statement or expression
1842 """Time execution of a Python statement or expression
1843
1843
1844 Usage:\\
1844 Usage:\\
1845 %timeit [-n<N> -r<R> [-t|-c]] statement
1845 %timeit [-n<N> -r<R> [-t|-c]] statement
1846
1846
1847 Time execution of a Python statement or expression using the timeit
1847 Time execution of a Python statement or expression using the timeit
1848 module.
1848 module.
1849
1849
1850 Options:
1850 Options:
1851 -n<N>: execute the given statement <N> times in a loop. If this value
1851 -n<N>: execute the given statement <N> times in a loop. If this value
1852 is not given, a fitting value is chosen.
1852 is not given, a fitting value is chosen.
1853
1853
1854 -r<R>: repeat the loop iteration <R> times and take the best result.
1854 -r<R>: repeat the loop iteration <R> times and take the best result.
1855 Default: 3
1855 Default: 3
1856
1856
1857 -t: use time.time to measure the time, which is the default on Unix.
1857 -t: use time.time to measure the time, which is the default on Unix.
1858 This function measures wall time.
1858 This function measures wall time.
1859
1859
1860 -c: use time.clock to measure the time, which is the default on
1860 -c: use time.clock to measure the time, which is the default on
1861 Windows and measures wall time. On Unix, resource.getrusage is used
1861 Windows and measures wall time. On Unix, resource.getrusage is used
1862 instead and returns the CPU user time.
1862 instead and returns the CPU user time.
1863
1863
1864 -p<P>: use a precision of <P> digits to display the timing result.
1864 -p<P>: use a precision of <P> digits to display the timing result.
1865 Default: 3
1865 Default: 3
1866
1866
1867
1867
1868 Examples:
1868 Examples:
1869
1869
1870 In [1]: %timeit pass
1870 In [1]: %timeit pass
1871 10000000 loops, best of 3: 53.3 ns per loop
1871 10000000 loops, best of 3: 53.3 ns per loop
1872
1872
1873 In [2]: u = None
1873 In [2]: u = None
1874
1874
1875 In [3]: %timeit u is None
1875 In [3]: %timeit u is None
1876 10000000 loops, best of 3: 184 ns per loop
1876 10000000 loops, best of 3: 184 ns per loop
1877
1877
1878 In [4]: %timeit -r 4 u == None
1878 In [4]: %timeit -r 4 u == None
1879 1000000 loops, best of 4: 242 ns per loop
1879 1000000 loops, best of 4: 242 ns per loop
1880
1880
1881 In [5]: import time
1881 In [5]: import time
1882
1882
1883 In [6]: %timeit -n1 time.sleep(2)
1883 In [6]: %timeit -n1 time.sleep(2)
1884 1 loops, best of 3: 2 s per loop
1884 1 loops, best of 3: 2 s per loop
1885
1885
1886
1886
1887 The times reported by %timeit will be slightly higher than those
1887 The times reported by %timeit will be slightly higher than those
1888 reported by the timeit.py script when variables are accessed. This is
1888 reported by the timeit.py script when variables are accessed. This is
1889 due to the fact that %timeit executes the statement in the namespace
1889 due to the fact that %timeit executes the statement in the namespace
1890 of the shell, compared with timeit.py, which uses a single setup
1890 of the shell, compared with timeit.py, which uses a single setup
1891 statement to import function or create variables. Generally, the bias
1891 statement to import function or create variables. Generally, the bias
1892 does not matter as long as results from timeit.py are not mixed with
1892 does not matter as long as results from timeit.py are not mixed with
1893 those from %timeit."""
1893 those from %timeit."""
1894
1894
1895 import timeit
1895 import timeit
1896 import math
1896 import math
1897
1897
1898 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1898 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1899 # certain terminals. Until we figure out a robust way of
1899 # certain terminals. Until we figure out a robust way of
1900 # auto-detecting if the terminal can deal with it, use plain 'us' for
1900 # auto-detecting if the terminal can deal with it, use plain 'us' for
1901 # microseconds. I am really NOT happy about disabling the proper
1901 # microseconds. I am really NOT happy about disabling the proper
1902 # 'micro' prefix, but crashing is worse... If anyone knows what the
1902 # 'micro' prefix, but crashing is worse... If anyone knows what the
1903 # right solution for this is, I'm all ears...
1903 # right solution for this is, I'm all ears...
1904 #
1904 #
1905 # Note: using
1905 # Note: using
1906 #
1906 #
1907 # s = u'\xb5'
1907 # s = u'\xb5'
1908 # s.encode(sys.getdefaultencoding())
1908 # s.encode(sys.getdefaultencoding())
1909 #
1909 #
1910 # is not sufficient, as I've seen terminals where that fails but
1910 # is not sufficient, as I've seen terminals where that fails but
1911 # print s
1911 # print s
1912 #
1912 #
1913 # succeeds
1913 # succeeds
1914 #
1914 #
1915 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1915 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1916
1916
1917 #units = [u"s", u"ms",u'\xb5',"ns"]
1917 #units = [u"s", u"ms",u'\xb5',"ns"]
1918 units = [u"s", u"ms",u'us',"ns"]
1918 units = [u"s", u"ms",u'us',"ns"]
1919
1919
1920 scaling = [1, 1e3, 1e6, 1e9]
1920 scaling = [1, 1e3, 1e6, 1e9]
1921
1921
1922 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1922 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1923 posix=False)
1923 posix=False)
1924 if stmt == "":
1924 if stmt == "":
1925 return
1925 return
1926 timefunc = timeit.default_timer
1926 timefunc = timeit.default_timer
1927 number = int(getattr(opts, "n", 0))
1927 number = int(getattr(opts, "n", 0))
1928 repeat = int(getattr(opts, "r", timeit.default_repeat))
1928 repeat = int(getattr(opts, "r", timeit.default_repeat))
1929 precision = int(getattr(opts, "p", 3))
1929 precision = int(getattr(opts, "p", 3))
1930 if hasattr(opts, "t"):
1930 if hasattr(opts, "t"):
1931 timefunc = time.time
1931 timefunc = time.time
1932 if hasattr(opts, "c"):
1932 if hasattr(opts, "c"):
1933 timefunc = clock
1933 timefunc = clock
1934
1934
1935 timer = timeit.Timer(timer=timefunc)
1935 timer = timeit.Timer(timer=timefunc)
1936 # this code has tight coupling to the inner workings of timeit.Timer,
1936 # this code has tight coupling to the inner workings of timeit.Timer,
1937 # but is there a better way to achieve that the code stmt has access
1937 # but is there a better way to achieve that the code stmt has access
1938 # to the shell namespace?
1938 # to the shell namespace?
1939
1939
1940 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1940 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1941 'setup': "pass"}
1941 'setup': "pass"}
1942 # Track compilation time so it can be reported if too long
1942 # Track compilation time so it can be reported if too long
1943 # Minimum time above which compilation time will be reported
1943 # Minimum time above which compilation time will be reported
1944 tc_min = 0.1
1944 tc_min = 0.1
1945
1945
1946 t0 = clock()
1946 t0 = clock()
1947 code = compile(src, "<magic-timeit>", "exec")
1947 code = compile(src, "<magic-timeit>", "exec")
1948 tc = clock()-t0
1948 tc = clock()-t0
1949
1949
1950 ns = {}
1950 ns = {}
1951 exec code in self.shell.user_ns, ns
1951 exec code in self.shell.user_ns, ns
1952 timer.inner = ns["inner"]
1952 timer.inner = ns["inner"]
1953
1953
1954 if number == 0:
1954 if number == 0:
1955 # determine number so that 0.2 <= total time < 2.0
1955 # determine number so that 0.2 <= total time < 2.0
1956 number = 1
1956 number = 1
1957 for i in range(1, 10):
1957 for i in range(1, 10):
1958 if timer.timeit(number) >= 0.2:
1958 if timer.timeit(number) >= 0.2:
1959 break
1959 break
1960 number *= 10
1960 number *= 10
1961
1961
1962 best = min(timer.repeat(repeat, number)) / number
1962 best = min(timer.repeat(repeat, number)) / number
1963
1963
1964 if best > 0.0 and best < 1000.0:
1964 if best > 0.0 and best < 1000.0:
1965 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1965 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1966 elif best >= 1000.0:
1966 elif best >= 1000.0:
1967 order = 0
1967 order = 0
1968 else:
1968 else:
1969 order = 3
1969 order = 3
1970 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1970 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1971 precision,
1971 precision,
1972 best * scaling[order],
1972 best * scaling[order],
1973 units[order])
1973 units[order])
1974 if tc > tc_min:
1974 if tc > tc_min:
1975 print "Compiler time: %.2f s" % tc
1975 print "Compiler time: %.2f s" % tc
1976
1976
1977 @testdec.skip_doctest
1977 @testdec.skip_doctest
1978 def magic_time(self,parameter_s = ''):
1978 def magic_time(self,parameter_s = ''):
1979 """Time execution of a Python statement or expression.
1979 """Time execution of a Python statement or expression.
1980
1980
1981 The CPU and wall clock times are printed, and the value of the
1981 The CPU and wall clock times are printed, and the value of the
1982 expression (if any) is returned. Note that under Win32, system time
1982 expression (if any) is returned. Note that under Win32, system time
1983 is always reported as 0, since it can not be measured.
1983 is always reported as 0, since it can not be measured.
1984
1984
1985 This function provides very basic timing functionality. In Python
1985 This function provides very basic timing functionality. In Python
1986 2.3, the timeit module offers more control and sophistication, so this
1986 2.3, the timeit module offers more control and sophistication, so this
1987 could be rewritten to use it (patches welcome).
1987 could be rewritten to use it (patches welcome).
1988
1988
1989 Some examples:
1989 Some examples:
1990
1990
1991 In [1]: time 2**128
1991 In [1]: time 2**128
1992 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1992 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1993 Wall time: 0.00
1993 Wall time: 0.00
1994 Out[1]: 340282366920938463463374607431768211456L
1994 Out[1]: 340282366920938463463374607431768211456L
1995
1995
1996 In [2]: n = 1000000
1996 In [2]: n = 1000000
1997
1997
1998 In [3]: time sum(range(n))
1998 In [3]: time sum(range(n))
1999 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1999 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
2000 Wall time: 1.37
2000 Wall time: 1.37
2001 Out[3]: 499999500000L
2001 Out[3]: 499999500000L
2002
2002
2003 In [4]: time print 'hello world'
2003 In [4]: time print 'hello world'
2004 hello world
2004 hello world
2005 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2005 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2006 Wall time: 0.00
2006 Wall time: 0.00
2007
2007
2008 Note that the time needed by Python to compile the given expression
2008 Note that the time needed by Python to compile the given expression
2009 will be reported if it is more than 0.1s. In this example, the
2009 will be reported if it is more than 0.1s. In this example, the
2010 actual exponentiation is done by Python at compilation time, so while
2010 actual exponentiation is done by Python at compilation time, so while
2011 the expression can take a noticeable amount of time to compute, that
2011 the expression can take a noticeable amount of time to compute, that
2012 time is purely due to the compilation:
2012 time is purely due to the compilation:
2013
2013
2014 In [5]: time 3**9999;
2014 In [5]: time 3**9999;
2015 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2015 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2016 Wall time: 0.00 s
2016 Wall time: 0.00 s
2017
2017
2018 In [6]: time 3**999999;
2018 In [6]: time 3**999999;
2019 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2019 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2020 Wall time: 0.00 s
2020 Wall time: 0.00 s
2021 Compiler : 0.78 s
2021 Compiler : 0.78 s
2022 """
2022 """
2023
2023
2024 # fail immediately if the given expression can't be compiled
2024 # fail immediately if the given expression can't be compiled
2025
2025
2026 expr = self.shell.prefilter(parameter_s,False)
2026 expr = self.shell.prefilter(parameter_s,False)
2027
2027
2028 # Minimum time above which compilation time will be reported
2028 # Minimum time above which compilation time will be reported
2029 tc_min = 0.1
2029 tc_min = 0.1
2030
2030
2031 try:
2031 try:
2032 mode = 'eval'
2032 mode = 'eval'
2033 t0 = clock()
2033 t0 = clock()
2034 code = compile(expr,'<timed eval>',mode)
2034 code = compile(expr,'<timed eval>',mode)
2035 tc = clock()-t0
2035 tc = clock()-t0
2036 except SyntaxError:
2036 except SyntaxError:
2037 mode = 'exec'
2037 mode = 'exec'
2038 t0 = clock()
2038 t0 = clock()
2039 code = compile(expr,'<timed exec>',mode)
2039 code = compile(expr,'<timed exec>',mode)
2040 tc = clock()-t0
2040 tc = clock()-t0
2041 # skew measurement as little as possible
2041 # skew measurement as little as possible
2042 glob = self.shell.user_ns
2042 glob = self.shell.user_ns
2043 clk = clock2
2043 clk = clock2
2044 wtime = time.time
2044 wtime = time.time
2045 # time execution
2045 # time execution
2046 wall_st = wtime()
2046 wall_st = wtime()
2047 if mode=='eval':
2047 if mode=='eval':
2048 st = clk()
2048 st = clk()
2049 out = eval(code,glob)
2049 out = eval(code,glob)
2050 end = clk()
2050 end = clk()
2051 else:
2051 else:
2052 st = clk()
2052 st = clk()
2053 exec code in glob
2053 exec code in glob
2054 end = clk()
2054 end = clk()
2055 out = None
2055 out = None
2056 wall_end = wtime()
2056 wall_end = wtime()
2057 # Compute actual times and report
2057 # Compute actual times and report
2058 wall_time = wall_end-wall_st
2058 wall_time = wall_end-wall_st
2059 cpu_user = end[0]-st[0]
2059 cpu_user = end[0]-st[0]
2060 cpu_sys = end[1]-st[1]
2060 cpu_sys = end[1]-st[1]
2061 cpu_tot = cpu_user+cpu_sys
2061 cpu_tot = cpu_user+cpu_sys
2062 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
2062 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
2063 (cpu_user,cpu_sys,cpu_tot)
2063 (cpu_user,cpu_sys,cpu_tot)
2064 print "Wall time: %.2f s" % wall_time
2064 print "Wall time: %.2f s" % wall_time
2065 if tc > tc_min:
2065 if tc > tc_min:
2066 print "Compiler : %.2f s" % tc
2066 print "Compiler : %.2f s" % tc
2067 return out
2067 return out
2068
2068
2069 @testdec.skip_doctest
2069 @testdec.skip_doctest
2070 def magic_macro(self,parameter_s = ''):
2070 def magic_macro(self,parameter_s = ''):
2071 """Define a set of input lines as a macro for future re-execution.
2071 """Define a set of input lines as a macro for future re-execution.
2072
2072
2073 Usage:\\
2073 Usage:\\
2074 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
2074 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
2075
2075
2076 Options:
2076 Options:
2077
2077
2078 -r: use 'raw' input. By default, the 'processed' history is used,
2078 -r: use 'raw' input. By default, the 'processed' history is used,
2079 so that magics are loaded in their transformed version to valid
2079 so that magics are loaded in their transformed version to valid
2080 Python. If this option is given, the raw input as typed as the
2080 Python. If this option is given, the raw input as typed as the
2081 command line is used instead.
2081 command line is used instead.
2082
2082
2083 This will define a global variable called `name` which is a string
2083 This will define a global variable called `name` which is a string
2084 made of joining the slices and lines you specify (n1,n2,... numbers
2084 made of joining the slices and lines you specify (n1,n2,... numbers
2085 above) from your input history into a single string. This variable
2085 above) from your input history into a single string. This variable
2086 acts like an automatic function which re-executes those lines as if
2086 acts like an automatic function which re-executes those lines as if
2087 you had typed them. You just type 'name' at the prompt and the code
2087 you had typed them. You just type 'name' at the prompt and the code
2088 executes.
2088 executes.
2089
2089
2090 The notation for indicating number ranges is: n1-n2 means 'use line
2090 The notation for indicating number ranges is: n1-n2 means 'use line
2091 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
2091 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
2092 using the lines numbered 5,6 and 7.
2092 using the lines numbered 5,6 and 7.
2093
2093
2094 Note: as a 'hidden' feature, you can also use traditional python slice
2094 Note: as a 'hidden' feature, you can also use traditional python slice
2095 notation, where N:M means numbers N through M-1.
2095 notation, where N:M means numbers N through M-1.
2096
2096
2097 For example, if your history contains (%hist prints it):
2097 For example, if your history contains (%hist prints it):
2098
2098
2099 44: x=1
2099 44: x=1
2100 45: y=3
2100 45: y=3
2101 46: z=x+y
2101 46: z=x+y
2102 47: print x
2102 47: print x
2103 48: a=5
2103 48: a=5
2104 49: print 'x',x,'y',y
2104 49: print 'x',x,'y',y
2105
2105
2106 you can create a macro with lines 44 through 47 (included) and line 49
2106 you can create a macro with lines 44 through 47 (included) and line 49
2107 called my_macro with:
2107 called my_macro with:
2108
2108
2109 In [55]: %macro my_macro 44-47 49
2109 In [55]: %macro my_macro 44-47 49
2110
2110
2111 Now, typing `my_macro` (without quotes) will re-execute all this code
2111 Now, typing `my_macro` (without quotes) will re-execute all this code
2112 in one pass.
2112 in one pass.
2113
2113
2114 You don't need to give the line-numbers in order, and any given line
2114 You don't need to give the line-numbers in order, and any given line
2115 number can appear multiple times. You can assemble macros with any
2115 number can appear multiple times. You can assemble macros with any
2116 lines from your input history in any order.
2116 lines from your input history in any order.
2117
2117
2118 The macro is a simple object which holds its value in an attribute,
2118 The macro is a simple object which holds its value in an attribute,
2119 but IPython's display system checks for macros and executes them as
2119 but IPython's display system checks for macros and executes them as
2120 code instead of printing them when you type their name.
2120 code instead of printing them when you type their name.
2121
2121
2122 You can view a macro's contents by explicitly printing it with:
2122 You can view a macro's contents by explicitly printing it with:
2123
2123
2124 'print macro_name'.
2124 'print macro_name'.
2125
2125
2126 For one-off cases which DON'T contain magic function calls in them you
2126 For one-off cases which DON'T contain magic function calls in them you
2127 can obtain similar results by explicitly executing slices from your
2127 can obtain similar results by explicitly executing slices from your
2128 input history with:
2128 input history with:
2129
2129
2130 In [60]: exec In[44:48]+In[49]"""
2130 In [60]: exec In[44:48]+In[49]"""
2131
2131
2132 opts,args = self.parse_options(parameter_s,'r',mode='list')
2132 opts,args = self.parse_options(parameter_s,'r',mode='list')
2133 if not args:
2133 if not args:
2134 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
2134 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
2135 macs.sort()
2135 macs.sort()
2136 return macs
2136 return macs
2137 if len(args) == 1:
2137 if len(args) == 1:
2138 raise UsageError(
2138 raise UsageError(
2139 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2139 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2140 name,ranges = args[0], args[1:]
2140 name,ranges = args[0], args[1:]
2141
2141
2142 #print 'rng',ranges # dbg
2142 #print 'rng',ranges # dbg
2143 lines = self.extract_input_slices(ranges,opts.has_key('r'))
2143 lines = self.extract_input_slices(ranges,opts.has_key('r'))
2144 macro = Macro(lines)
2144 macro = Macro(lines)
2145 self.shell.define_macro(name, macro)
2145 self.shell.define_macro(name, macro)
2146 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2146 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2147 print 'Macro contents:'
2147 print 'Macro contents:'
2148 print macro,
2148 print macro,
2149
2149
2150 def magic_save(self,parameter_s = ''):
2150 def magic_save(self,parameter_s = ''):
2151 """Save a set of lines to a given filename.
2151 """Save a set of lines to a given filename.
2152
2152
2153 Usage:\\
2153 Usage:\\
2154 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
2154 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
2155
2155
2156 Options:
2156 Options:
2157
2157
2158 -r: use 'raw' input. By default, the 'processed' history is used,
2158 -r: use 'raw' input. By default, the 'processed' history is used,
2159 so that magics are loaded in their transformed version to valid
2159 so that magics are loaded in their transformed version to valid
2160 Python. If this option is given, the raw input as typed as the
2160 Python. If this option is given, the raw input as typed as the
2161 command line is used instead.
2161 command line is used instead.
2162
2162
2163 This function uses the same syntax as %macro for line extraction, but
2163 This function uses the same syntax as %macro for line extraction, but
2164 instead of creating a macro it saves the resulting string to the
2164 instead of creating a macro it saves the resulting string to the
2165 filename you specify.
2165 filename you specify.
2166
2166
2167 It adds a '.py' extension to the file if you don't do so yourself, and
2167 It adds a '.py' extension to the file if you don't do so yourself, and
2168 it asks for confirmation before overwriting existing files."""
2168 it asks for confirmation before overwriting existing files."""
2169
2169
2170 opts,args = self.parse_options(parameter_s,'r',mode='list')
2170 opts,args = self.parse_options(parameter_s,'r',mode='list')
2171 fname,ranges = args[0], args[1:]
2171 fname,ranges = args[0], args[1:]
2172 if not fname.endswith('.py'):
2172 if not fname.endswith('.py'):
2173 fname += '.py'
2173 fname += '.py'
2174 if os.path.isfile(fname):
2174 if os.path.isfile(fname):
2175 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2175 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2176 if ans.lower() not in ['y','yes']:
2176 if ans.lower() not in ['y','yes']:
2177 print 'Operation cancelled.'
2177 print 'Operation cancelled.'
2178 return
2178 return
2179 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
2179 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
2180 f = file(fname,'w')
2180 f = file(fname,'w')
2181 f.write(cmds)
2181 f.write(cmds)
2182 f.close()
2182 f.close()
2183 print 'The following commands were written to file `%s`:' % fname
2183 print 'The following commands were written to file `%s`:' % fname
2184 print cmds
2184 print cmds
2185
2185
2186 def _edit_macro(self,mname,macro):
2186 def _edit_macro(self,mname,macro):
2187 """open an editor with the macro data in a file"""
2187 """open an editor with the macro data in a file"""
2188 filename = self.shell.mktempfile(macro.value)
2188 filename = self.shell.mktempfile(macro.value)
2189 self.shell.hooks.editor(filename)
2189 self.shell.hooks.editor(filename)
2190
2190
2191 # and make a new macro object, to replace the old one
2191 # and make a new macro object, to replace the old one
2192 mfile = open(filename)
2192 mfile = open(filename)
2193 mvalue = mfile.read()
2193 mvalue = mfile.read()
2194 mfile.close()
2194 mfile.close()
2195 self.shell.user_ns[mname] = Macro(mvalue)
2195 self.shell.user_ns[mname] = Macro(mvalue)
2196
2196
2197 def magic_ed(self,parameter_s=''):
2197 def magic_ed(self,parameter_s=''):
2198 """Alias to %edit."""
2198 """Alias to %edit."""
2199 return self.magic_edit(parameter_s)
2199 return self.magic_edit(parameter_s)
2200
2200
2201 @testdec.skip_doctest
2201 @testdec.skip_doctest
2202 def magic_edit(self,parameter_s='',last_call=['','']):
2202 def magic_edit(self,parameter_s='',last_call=['','']):
2203 """Bring up an editor and execute the resulting code.
2203 """Bring up an editor and execute the resulting code.
2204
2204
2205 Usage:
2205 Usage:
2206 %edit [options] [args]
2206 %edit [options] [args]
2207
2207
2208 %edit runs IPython's editor hook. The default version of this hook is
2208 %edit runs IPython's editor hook. The default version of this hook is
2209 set to call the __IPYTHON__.rc.editor command. This is read from your
2209 set to call the __IPYTHON__.rc.editor command. This is read from your
2210 environment variable $EDITOR. If this isn't found, it will default to
2210 environment variable $EDITOR. If this isn't found, it will default to
2211 vi under Linux/Unix and to notepad under Windows. See the end of this
2211 vi under Linux/Unix and to notepad under Windows. See the end of this
2212 docstring for how to change the editor hook.
2212 docstring for how to change the editor hook.
2213
2213
2214 You can also set the value of this editor via the command line option
2214 You can also set the value of this editor via the command line option
2215 '-editor' or in your ipythonrc file. This is useful if you wish to use
2215 '-editor' or in your ipythonrc file. This is useful if you wish to use
2216 specifically for IPython an editor different from your typical default
2216 specifically for IPython an editor different from your typical default
2217 (and for Windows users who typically don't set environment variables).
2217 (and for Windows users who typically don't set environment variables).
2218
2218
2219 This command allows you to conveniently edit multi-line code right in
2219 This command allows you to conveniently edit multi-line code right in
2220 your IPython session.
2220 your IPython session.
2221
2221
2222 If called without arguments, %edit opens up an empty editor with a
2222 If called without arguments, %edit opens up an empty editor with a
2223 temporary file and will execute the contents of this file when you
2223 temporary file and will execute the contents of this file when you
2224 close it (don't forget to save it!).
2224 close it (don't forget to save it!).
2225
2225
2226
2226
2227 Options:
2227 Options:
2228
2228
2229 -n <number>: open the editor at a specified line number. By default,
2229 -n <number>: open the editor at a specified line number. By default,
2230 the IPython editor hook uses the unix syntax 'editor +N filename', but
2230 the IPython editor hook uses the unix syntax 'editor +N filename', but
2231 you can configure this by providing your own modified hook if your
2231 you can configure this by providing your own modified hook if your
2232 favorite editor supports line-number specifications with a different
2232 favorite editor supports line-number specifications with a different
2233 syntax.
2233 syntax.
2234
2234
2235 -p: this will call the editor with the same data as the previous time
2235 -p: this will call the editor with the same data as the previous time
2236 it was used, regardless of how long ago (in your current session) it
2236 it was used, regardless of how long ago (in your current session) it
2237 was.
2237 was.
2238
2238
2239 -r: use 'raw' input. This option only applies to input taken from the
2239 -r: use 'raw' input. This option only applies to input taken from the
2240 user's history. By default, the 'processed' history is used, so that
2240 user's history. By default, the 'processed' history is used, so that
2241 magics are loaded in their transformed version to valid Python. If
2241 magics are loaded in their transformed version to valid Python. If
2242 this option is given, the raw input as typed as the command line is
2242 this option is given, the raw input as typed as the command line is
2243 used instead. When you exit the editor, it will be executed by
2243 used instead. When you exit the editor, it will be executed by
2244 IPython's own processor.
2244 IPython's own processor.
2245
2245
2246 -x: do not execute the edited code immediately upon exit. This is
2246 -x: do not execute the edited code immediately upon exit. This is
2247 mainly useful if you are editing programs which need to be called with
2247 mainly useful if you are editing programs which need to be called with
2248 command line arguments, which you can then do using %run.
2248 command line arguments, which you can then do using %run.
2249
2249
2250
2250
2251 Arguments:
2251 Arguments:
2252
2252
2253 If arguments are given, the following possibilites exist:
2253 If arguments are given, the following possibilites exist:
2254
2254
2255 - The arguments are numbers or pairs of colon-separated numbers (like
2255 - The arguments are numbers or pairs of colon-separated numbers (like
2256 1 4:8 9). These are interpreted as lines of previous input to be
2256 1 4:8 9). These are interpreted as lines of previous input to be
2257 loaded into the editor. The syntax is the same of the %macro command.
2257 loaded into the editor. The syntax is the same of the %macro command.
2258
2258
2259 - If the argument doesn't start with a number, it is evaluated as a
2259 - If the argument doesn't start with a number, it is evaluated as a
2260 variable and its contents loaded into the editor. You can thus edit
2260 variable and its contents loaded into the editor. You can thus edit
2261 any string which contains python code (including the result of
2261 any string which contains python code (including the result of
2262 previous edits).
2262 previous edits).
2263
2263
2264 - If the argument is the name of an object (other than a string),
2264 - If the argument is the name of an object (other than a string),
2265 IPython will try to locate the file where it was defined and open the
2265 IPython will try to locate the file where it was defined and open the
2266 editor at the point where it is defined. You can use `%edit function`
2266 editor at the point where it is defined. You can use `%edit function`
2267 to load an editor exactly at the point where 'function' is defined,
2267 to load an editor exactly at the point where 'function' is defined,
2268 edit it and have the file be executed automatically.
2268 edit it and have the file be executed automatically.
2269
2269
2270 If the object is a macro (see %macro for details), this opens up your
2270 If the object is a macro (see %macro for details), this opens up your
2271 specified editor with a temporary file containing the macro's data.
2271 specified editor with a temporary file containing the macro's data.
2272 Upon exit, the macro is reloaded with the contents of the file.
2272 Upon exit, the macro is reloaded with the contents of the file.
2273
2273
2274 Note: opening at an exact line is only supported under Unix, and some
2274 Note: opening at an exact line is only supported under Unix, and some
2275 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2275 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2276 '+NUMBER' parameter necessary for this feature. Good editors like
2276 '+NUMBER' parameter necessary for this feature. Good editors like
2277 (X)Emacs, vi, jed, pico and joe all do.
2277 (X)Emacs, vi, jed, pico and joe all do.
2278
2278
2279 - If the argument is not found as a variable, IPython will look for a
2279 - If the argument is not found as a variable, IPython will look for a
2280 file with that name (adding .py if necessary) and load it into the
2280 file with that name (adding .py if necessary) and load it into the
2281 editor. It will execute its contents with execfile() when you exit,
2281 editor. It will execute its contents with execfile() when you exit,
2282 loading any code in the file into your interactive namespace.
2282 loading any code in the file into your interactive namespace.
2283
2283
2284 After executing your code, %edit will return as output the code you
2284 After executing your code, %edit will return as output the code you
2285 typed in the editor (except when it was an existing file). This way
2285 typed in the editor (except when it was an existing file). This way
2286 you can reload the code in further invocations of %edit as a variable,
2286 you can reload the code in further invocations of %edit as a variable,
2287 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2287 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2288 the output.
2288 the output.
2289
2289
2290 Note that %edit is also available through the alias %ed.
2290 Note that %edit is also available through the alias %ed.
2291
2291
2292 This is an example of creating a simple function inside the editor and
2292 This is an example of creating a simple function inside the editor and
2293 then modifying it. First, start up the editor:
2293 then modifying it. First, start up the editor:
2294
2294
2295 In [1]: ed
2295 In [1]: ed
2296 Editing... done. Executing edited code...
2296 Editing... done. Executing edited code...
2297 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2297 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2298
2298
2299 We can then call the function foo():
2299 We can then call the function foo():
2300
2300
2301 In [2]: foo()
2301 In [2]: foo()
2302 foo() was defined in an editing session
2302 foo() was defined in an editing session
2303
2303
2304 Now we edit foo. IPython automatically loads the editor with the
2304 Now we edit foo. IPython automatically loads the editor with the
2305 (temporary) file where foo() was previously defined:
2305 (temporary) file where foo() was previously defined:
2306
2306
2307 In [3]: ed foo
2307 In [3]: ed foo
2308 Editing... done. Executing edited code...
2308 Editing... done. Executing edited code...
2309
2309
2310 And if we call foo() again we get the modified version:
2310 And if we call foo() again we get the modified version:
2311
2311
2312 In [4]: foo()
2312 In [4]: foo()
2313 foo() has now been changed!
2313 foo() has now been changed!
2314
2314
2315 Here is an example of how to edit a code snippet successive
2315 Here is an example of how to edit a code snippet successive
2316 times. First we call the editor:
2316 times. First we call the editor:
2317
2317
2318 In [5]: ed
2318 In [5]: ed
2319 Editing... done. Executing edited code...
2319 Editing... done. Executing edited code...
2320 hello
2320 hello
2321 Out[5]: "print 'hello'n"
2321 Out[5]: "print 'hello'n"
2322
2322
2323 Now we call it again with the previous output (stored in _):
2323 Now we call it again with the previous output (stored in _):
2324
2324
2325 In [6]: ed _
2325 In [6]: ed _
2326 Editing... done. Executing edited code...
2326 Editing... done. Executing edited code...
2327 hello world
2327 hello world
2328 Out[6]: "print 'hello world'n"
2328 Out[6]: "print 'hello world'n"
2329
2329
2330 Now we call it with the output #8 (stored in _8, also as Out[8]):
2330 Now we call it with the output #8 (stored in _8, also as Out[8]):
2331
2331
2332 In [7]: ed _8
2332 In [7]: ed _8
2333 Editing... done. Executing edited code...
2333 Editing... done. Executing edited code...
2334 hello again
2334 hello again
2335 Out[7]: "print 'hello again'n"
2335 Out[7]: "print 'hello again'n"
2336
2336
2337
2337
2338 Changing the default editor hook:
2338 Changing the default editor hook:
2339
2339
2340 If you wish to write your own editor hook, you can put it in a
2340 If you wish to write your own editor hook, you can put it in a
2341 configuration file which you load at startup time. The default hook
2341 configuration file which you load at startup time. The default hook
2342 is defined in the IPython.core.hooks module, and you can use that as a
2342 is defined in the IPython.core.hooks module, and you can use that as a
2343 starting example for further modifications. That file also has
2343 starting example for further modifications. That file also has
2344 general instructions on how to set a new hook for use once you've
2344 general instructions on how to set a new hook for use once you've
2345 defined it."""
2345 defined it."""
2346
2346
2347 # FIXME: This function has become a convoluted mess. It needs a
2347 # FIXME: This function has become a convoluted mess. It needs a
2348 # ground-up rewrite with clean, simple logic.
2348 # ground-up rewrite with clean, simple logic.
2349
2349
2350 def make_filename(arg):
2350 def make_filename(arg):
2351 "Make a filename from the given args"
2351 "Make a filename from the given args"
2352 try:
2352 try:
2353 filename = get_py_filename(arg)
2353 filename = get_py_filename(arg)
2354 except IOError:
2354 except IOError:
2355 if args.endswith('.py'):
2355 if args.endswith('.py'):
2356 filename = arg
2356 filename = arg
2357 else:
2357 else:
2358 filename = None
2358 filename = None
2359 return filename
2359 return filename
2360
2360
2361 # custom exceptions
2361 # custom exceptions
2362 class DataIsObject(Exception): pass
2362 class DataIsObject(Exception): pass
2363
2363
2364 opts,args = self.parse_options(parameter_s,'prxn:')
2364 opts,args = self.parse_options(parameter_s,'prxn:')
2365 # Set a few locals from the options for convenience:
2365 # Set a few locals from the options for convenience:
2366 opts_p = opts.has_key('p')
2366 opts_p = opts.has_key('p')
2367 opts_r = opts.has_key('r')
2367 opts_r = opts.has_key('r')
2368
2368
2369 # Default line number value
2369 # Default line number value
2370 lineno = opts.get('n',None)
2370 lineno = opts.get('n',None)
2371
2371
2372 if opts_p:
2372 if opts_p:
2373 args = '_%s' % last_call[0]
2373 args = '_%s' % last_call[0]
2374 if not self.shell.user_ns.has_key(args):
2374 if not self.shell.user_ns.has_key(args):
2375 args = last_call[1]
2375 args = last_call[1]
2376
2376
2377 # use last_call to remember the state of the previous call, but don't
2377 # use last_call to remember the state of the previous call, but don't
2378 # let it be clobbered by successive '-p' calls.
2378 # let it be clobbered by successive '-p' calls.
2379 try:
2379 try:
2380 last_call[0] = self.shell.outputcache.prompt_count
2380 last_call[0] = self.shell.outputcache.prompt_count
2381 if not opts_p:
2381 if not opts_p:
2382 last_call[1] = parameter_s
2382 last_call[1] = parameter_s
2383 except:
2383 except:
2384 pass
2384 pass
2385
2385
2386 # by default this is done with temp files, except when the given
2386 # by default this is done with temp files, except when the given
2387 # arg is a filename
2387 # arg is a filename
2388 use_temp = 1
2388 use_temp = 1
2389
2389
2390 if re.match(r'\d',args):
2390 if re.match(r'\d',args):
2391 # Mode where user specifies ranges of lines, like in %macro.
2391 # Mode where user specifies ranges of lines, like in %macro.
2392 # This means that you can't edit files whose names begin with
2392 # This means that you can't edit files whose names begin with
2393 # numbers this way. Tough.
2393 # numbers this way. Tough.
2394 ranges = args.split()
2394 ranges = args.split()
2395 data = ''.join(self.extract_input_slices(ranges,opts_r))
2395 data = ''.join(self.extract_input_slices(ranges,opts_r))
2396 elif args.endswith('.py'):
2396 elif args.endswith('.py'):
2397 filename = make_filename(args)
2397 filename = make_filename(args)
2398 data = ''
2398 data = ''
2399 use_temp = 0
2399 use_temp = 0
2400 elif args:
2400 elif args:
2401 try:
2401 try:
2402 # Load the parameter given as a variable. If not a string,
2402 # Load the parameter given as a variable. If not a string,
2403 # process it as an object instead (below)
2403 # process it as an object instead (below)
2404
2404
2405 #print '*** args',args,'type',type(args) # dbg
2405 #print '*** args',args,'type',type(args) # dbg
2406 data = eval(args,self.shell.user_ns)
2406 data = eval(args,self.shell.user_ns)
2407 if not type(data) in StringTypes:
2407 if not type(data) in StringTypes:
2408 raise DataIsObject
2408 raise DataIsObject
2409
2409
2410 except (NameError,SyntaxError):
2410 except (NameError,SyntaxError):
2411 # given argument is not a variable, try as a filename
2411 # given argument is not a variable, try as a filename
2412 filename = make_filename(args)
2412 filename = make_filename(args)
2413 if filename is None:
2413 if filename is None:
2414 warn("Argument given (%s) can't be found as a variable "
2414 warn("Argument given (%s) can't be found as a variable "
2415 "or as a filename." % args)
2415 "or as a filename." % args)
2416 return
2416 return
2417
2417
2418 data = ''
2418 data = ''
2419 use_temp = 0
2419 use_temp = 0
2420 except DataIsObject:
2420 except DataIsObject:
2421
2421
2422 # macros have a special edit function
2422 # macros have a special edit function
2423 if isinstance(data,Macro):
2423 if isinstance(data,Macro):
2424 self._edit_macro(args,data)
2424 self._edit_macro(args,data)
2425 return
2425 return
2426
2426
2427 # For objects, try to edit the file where they are defined
2427 # For objects, try to edit the file where they are defined
2428 try:
2428 try:
2429 filename = inspect.getabsfile(data)
2429 filename = inspect.getabsfile(data)
2430 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2430 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2431 # class created by %edit? Try to find source
2431 # class created by %edit? Try to find source
2432 # by looking for method definitions instead, the
2432 # by looking for method definitions instead, the
2433 # __module__ in those classes is FakeModule.
2433 # __module__ in those classes is FakeModule.
2434 attrs = [getattr(data, aname) for aname in dir(data)]
2434 attrs = [getattr(data, aname) for aname in dir(data)]
2435 for attr in attrs:
2435 for attr in attrs:
2436 if not inspect.ismethod(attr):
2436 if not inspect.ismethod(attr):
2437 continue
2437 continue
2438 filename = inspect.getabsfile(attr)
2438 filename = inspect.getabsfile(attr)
2439 if filename and 'fakemodule' not in filename.lower():
2439 if filename and 'fakemodule' not in filename.lower():
2440 # change the attribute to be the edit target instead
2440 # change the attribute to be the edit target instead
2441 data = attr
2441 data = attr
2442 break
2442 break
2443
2443
2444 datafile = 1
2444 datafile = 1
2445 except TypeError:
2445 except TypeError:
2446 filename = make_filename(args)
2446 filename = make_filename(args)
2447 datafile = 1
2447 datafile = 1
2448 warn('Could not find file where `%s` is defined.\n'
2448 warn('Could not find file where `%s` is defined.\n'
2449 'Opening a file named `%s`' % (args,filename))
2449 'Opening a file named `%s`' % (args,filename))
2450 # Now, make sure we can actually read the source (if it was in
2450 # Now, make sure we can actually read the source (if it was in
2451 # a temp file it's gone by now).
2451 # a temp file it's gone by now).
2452 if datafile:
2452 if datafile:
2453 try:
2453 try:
2454 if lineno is None:
2454 if lineno is None:
2455 lineno = inspect.getsourcelines(data)[1]
2455 lineno = inspect.getsourcelines(data)[1]
2456 except IOError:
2456 except IOError:
2457 filename = make_filename(args)
2457 filename = make_filename(args)
2458 if filename is None:
2458 if filename is None:
2459 warn('The file `%s` where `%s` was defined cannot '
2459 warn('The file `%s` where `%s` was defined cannot '
2460 'be read.' % (filename,data))
2460 'be read.' % (filename,data))
2461 return
2461 return
2462 use_temp = 0
2462 use_temp = 0
2463 else:
2463 else:
2464 data = ''
2464 data = ''
2465
2465
2466 if use_temp:
2466 if use_temp:
2467 filename = self.shell.mktempfile(data)
2467 filename = self.shell.mktempfile(data)
2468 print 'IPython will make a temporary file named:',filename
2468 print 'IPython will make a temporary file named:',filename
2469
2469
2470 # do actual editing here
2470 # do actual editing here
2471 print 'Editing...',
2471 print 'Editing...',
2472 sys.stdout.flush()
2472 sys.stdout.flush()
2473 try:
2473 try:
2474 # Quote filenames that may have spaces in them
2474 # Quote filenames that may have spaces in them
2475 if ' ' in filename:
2475 if ' ' in filename:
2476 filename = "%s" % filename
2476 filename = "%s" % filename
2477 self.shell.hooks.editor(filename,lineno)
2477 self.shell.hooks.editor(filename,lineno)
2478 except TryNext:
2478 except TryNext:
2479 warn('Could not open editor')
2479 warn('Could not open editor')
2480 return
2480 return
2481
2481
2482 # XXX TODO: should this be generalized for all string vars?
2482 # XXX TODO: should this be generalized for all string vars?
2483 # For now, this is special-cased to blocks created by cpaste
2483 # For now, this is special-cased to blocks created by cpaste
2484 if args.strip() == 'pasted_block':
2484 if args.strip() == 'pasted_block':
2485 self.shell.user_ns['pasted_block'] = file_read(filename)
2485 self.shell.user_ns['pasted_block'] = file_read(filename)
2486
2486
2487 if opts.has_key('x'): # -x prevents actual execution
2487 if opts.has_key('x'): # -x prevents actual execution
2488 print
2488 print
2489 else:
2489 else:
2490 print 'done. Executing edited code...'
2490 print 'done. Executing edited code...'
2491 if opts_r:
2491 if opts_r:
2492 self.shell.runlines(file_read(filename))
2492 self.shell.runlines(file_read(filename))
2493 else:
2493 else:
2494 self.shell.safe_execfile(filename,self.shell.user_ns,
2494 self.shell.safe_execfile(filename,self.shell.user_ns,
2495 self.shell.user_ns)
2495 self.shell.user_ns)
2496
2496
2497
2497
2498 if use_temp:
2498 if use_temp:
2499 try:
2499 try:
2500 return open(filename).read()
2500 return open(filename).read()
2501 except IOError,msg:
2501 except IOError,msg:
2502 if msg.filename == filename:
2502 if msg.filename == filename:
2503 warn('File not found. Did you forget to save?')
2503 warn('File not found. Did you forget to save?')
2504 return
2504 return
2505 else:
2505 else:
2506 self.shell.showtraceback()
2506 self.shell.showtraceback()
2507
2507
2508 def magic_xmode(self,parameter_s = ''):
2508 def magic_xmode(self,parameter_s = ''):
2509 """Switch modes for the exception handlers.
2509 """Switch modes for the exception handlers.
2510
2510
2511 Valid modes: Plain, Context and Verbose.
2511 Valid modes: Plain, Context and Verbose.
2512
2512
2513 If called without arguments, acts as a toggle."""
2513 If called without arguments, acts as a toggle."""
2514
2514
2515 def xmode_switch_err(name):
2515 def xmode_switch_err(name):
2516 warn('Error changing %s exception modes.\n%s' %
2516 warn('Error changing %s exception modes.\n%s' %
2517 (name,sys.exc_info()[1]))
2517 (name,sys.exc_info()[1]))
2518
2518
2519 shell = self.shell
2519 shell = self.shell
2520 new_mode = parameter_s.strip().capitalize()
2520 new_mode = parameter_s.strip().capitalize()
2521 try:
2521 try:
2522 shell.InteractiveTB.set_mode(mode=new_mode)
2522 shell.InteractiveTB.set_mode(mode=new_mode)
2523 print 'Exception reporting mode:',shell.InteractiveTB.mode
2523 print 'Exception reporting mode:',shell.InteractiveTB.mode
2524 except:
2524 except:
2525 xmode_switch_err('user')
2525 xmode_switch_err('user')
2526
2526
2527 # threaded shells use a special handler in sys.excepthook
2528 if shell.isthreaded:
2529 try:
2530 shell.sys_excepthook.set_mode(mode=new_mode)
2531 except:
2532 xmode_switch_err('threaded')
2533
2534 def magic_colors(self,parameter_s = ''):
2527 def magic_colors(self,parameter_s = ''):
2535 """Switch color scheme for prompts, info system and exception handlers.
2528 """Switch color scheme for prompts, info system and exception handlers.
2536
2529
2537 Currently implemented schemes: NoColor, Linux, LightBG.
2530 Currently implemented schemes: NoColor, Linux, LightBG.
2538
2531
2539 Color scheme names are not case-sensitive."""
2532 Color scheme names are not case-sensitive."""
2540
2533
2541 def color_switch_err(name):
2534 def color_switch_err(name):
2542 warn('Error changing %s color schemes.\n%s' %
2535 warn('Error changing %s color schemes.\n%s' %
2543 (name,sys.exc_info()[1]))
2536 (name,sys.exc_info()[1]))
2544
2537
2545
2538
2546 new_scheme = parameter_s.strip()
2539 new_scheme = parameter_s.strip()
2547 if not new_scheme:
2540 if not new_scheme:
2548 raise UsageError(
2541 raise UsageError(
2549 "%colors: you must specify a color scheme. See '%colors?'")
2542 "%colors: you must specify a color scheme. See '%colors?'")
2550 return
2543 return
2551 # local shortcut
2544 # local shortcut
2552 shell = self.shell
2545 shell = self.shell
2553
2546
2554 import IPython.utils.rlineimpl as readline
2547 import IPython.utils.rlineimpl as readline
2555
2548
2556 if not readline.have_readline and sys.platform == "win32":
2549 if not readline.have_readline and sys.platform == "win32":
2557 msg = """\
2550 msg = """\
2558 Proper color support under MS Windows requires the pyreadline library.
2551 Proper color support under MS Windows requires the pyreadline library.
2559 You can find it at:
2552 You can find it at:
2560 http://ipython.scipy.org/moin/PyReadline/Intro
2553 http://ipython.scipy.org/moin/PyReadline/Intro
2561 Gary's readline needs the ctypes module, from:
2554 Gary's readline needs the ctypes module, from:
2562 http://starship.python.net/crew/theller/ctypes
2555 http://starship.python.net/crew/theller/ctypes
2563 (Note that ctypes is already part of Python versions 2.5 and newer).
2556 (Note that ctypes is already part of Python versions 2.5 and newer).
2564
2557
2565 Defaulting color scheme to 'NoColor'"""
2558 Defaulting color scheme to 'NoColor'"""
2566 new_scheme = 'NoColor'
2559 new_scheme = 'NoColor'
2567 warn(msg)
2560 warn(msg)
2568
2561
2569 # readline option is 0
2562 # readline option is 0
2570 if not shell.has_readline:
2563 if not shell.has_readline:
2571 new_scheme = 'NoColor'
2564 new_scheme = 'NoColor'
2572
2565
2573 # Set prompt colors
2566 # Set prompt colors
2574 try:
2567 try:
2575 shell.outputcache.set_colors(new_scheme)
2568 shell.outputcache.set_colors(new_scheme)
2576 except:
2569 except:
2577 color_switch_err('prompt')
2570 color_switch_err('prompt')
2578 else:
2571 else:
2579 shell.colors = \
2572 shell.colors = \
2580 shell.outputcache.color_table.active_scheme_name
2573 shell.outputcache.color_table.active_scheme_name
2581 # Set exception colors
2574 # Set exception colors
2582 try:
2575 try:
2583 shell.InteractiveTB.set_colors(scheme = new_scheme)
2576 shell.InteractiveTB.set_colors(scheme = new_scheme)
2584 shell.SyntaxTB.set_colors(scheme = new_scheme)
2577 shell.SyntaxTB.set_colors(scheme = new_scheme)
2585 except:
2578 except:
2586 color_switch_err('exception')
2579 color_switch_err('exception')
2587
2580
2588 # threaded shells use a verbose traceback in sys.excepthook
2589 if shell.isthreaded:
2590 try:
2591 shell.sys_excepthook.set_colors(scheme=new_scheme)
2592 except:
2593 color_switch_err('system exception handler')
2594
2595 # Set info (for 'object?') colors
2581 # Set info (for 'object?') colors
2596 if shell.color_info:
2582 if shell.color_info:
2597 try:
2583 try:
2598 shell.inspector.set_active_scheme(new_scheme)
2584 shell.inspector.set_active_scheme(new_scheme)
2599 except:
2585 except:
2600 color_switch_err('object inspector')
2586 color_switch_err('object inspector')
2601 else:
2587 else:
2602 shell.inspector.set_active_scheme('NoColor')
2588 shell.inspector.set_active_scheme('NoColor')
2603
2589
2604 def magic_color_info(self,parameter_s = ''):
2590 def magic_color_info(self,parameter_s = ''):
2605 """Toggle color_info.
2591 """Toggle color_info.
2606
2592
2607 The color_info configuration parameter controls whether colors are
2593 The color_info configuration parameter controls whether colors are
2608 used for displaying object details (by things like %psource, %pfile or
2594 used for displaying object details (by things like %psource, %pfile or
2609 the '?' system). This function toggles this value with each call.
2595 the '?' system). This function toggles this value with each call.
2610
2596
2611 Note that unless you have a fairly recent pager (less works better
2597 Note that unless you have a fairly recent pager (less works better
2612 than more) in your system, using colored object information displays
2598 than more) in your system, using colored object information displays
2613 will not work properly. Test it and see."""
2599 will not work properly. Test it and see."""
2614
2600
2615 self.shell.color_info = not self.shell.color_info
2601 self.shell.color_info = not self.shell.color_info
2616 self.magic_colors(self.shell.colors)
2602 self.magic_colors(self.shell.colors)
2617 print 'Object introspection functions have now coloring:',
2603 print 'Object introspection functions have now coloring:',
2618 print ['OFF','ON'][int(self.shell.color_info)]
2604 print ['OFF','ON'][int(self.shell.color_info)]
2619
2605
2620 def magic_Pprint(self, parameter_s=''):
2606 def magic_Pprint(self, parameter_s=''):
2621 """Toggle pretty printing on/off."""
2607 """Toggle pretty printing on/off."""
2622
2608
2623 self.shell.pprint = 1 - self.shell.pprint
2609 self.shell.pprint = 1 - self.shell.pprint
2624 print 'Pretty printing has been turned', \
2610 print 'Pretty printing has been turned', \
2625 ['OFF','ON'][self.shell.pprint]
2611 ['OFF','ON'][self.shell.pprint]
2626
2612
2627 def magic_Exit(self, parameter_s=''):
2613 def magic_Exit(self, parameter_s=''):
2628 """Exit IPython without confirmation."""
2614 """Exit IPython without confirmation."""
2629
2615
2630 self.shell.ask_exit()
2616 self.shell.ask_exit()
2631
2617
2632 # Add aliases as magics so all common forms work: exit, quit, Exit, Quit.
2618 # Add aliases as magics so all common forms work: exit, quit, Exit, Quit.
2633 magic_exit = magic_quit = magic_Quit = magic_Exit
2619 magic_exit = magic_quit = magic_Quit = magic_Exit
2634
2620
2635 #......................................................................
2621 #......................................................................
2636 # Functions to implement unix shell-type things
2622 # Functions to implement unix shell-type things
2637
2623
2638 @testdec.skip_doctest
2624 @testdec.skip_doctest
2639 def magic_alias(self, parameter_s = ''):
2625 def magic_alias(self, parameter_s = ''):
2640 """Define an alias for a system command.
2626 """Define an alias for a system command.
2641
2627
2642 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2628 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2643
2629
2644 Then, typing 'alias_name params' will execute the system command 'cmd
2630 Then, typing 'alias_name params' will execute the system command 'cmd
2645 params' (from your underlying operating system).
2631 params' (from your underlying operating system).
2646
2632
2647 Aliases have lower precedence than magic functions and Python normal
2633 Aliases have lower precedence than magic functions and Python normal
2648 variables, so if 'foo' is both a Python variable and an alias, the
2634 variables, so if 'foo' is both a Python variable and an alias, the
2649 alias can not be executed until 'del foo' removes the Python variable.
2635 alias can not be executed until 'del foo' removes the Python variable.
2650
2636
2651 You can use the %l specifier in an alias definition to represent the
2637 You can use the %l specifier in an alias definition to represent the
2652 whole line when the alias is called. For example:
2638 whole line when the alias is called. For example:
2653
2639
2654 In [2]: alias all echo "Input in brackets: <%l>"
2640 In [2]: alias all echo "Input in brackets: <%l>"
2655 In [3]: all hello world
2641 In [3]: all hello world
2656 Input in brackets: <hello world>
2642 Input in brackets: <hello world>
2657
2643
2658 You can also define aliases with parameters using %s specifiers (one
2644 You can also define aliases with parameters using %s specifiers (one
2659 per parameter):
2645 per parameter):
2660
2646
2661 In [1]: alias parts echo first %s second %s
2647 In [1]: alias parts echo first %s second %s
2662 In [2]: %parts A B
2648 In [2]: %parts A B
2663 first A second B
2649 first A second B
2664 In [3]: %parts A
2650 In [3]: %parts A
2665 Incorrect number of arguments: 2 expected.
2651 Incorrect number of arguments: 2 expected.
2666 parts is an alias to: 'echo first %s second %s'
2652 parts is an alias to: 'echo first %s second %s'
2667
2653
2668 Note that %l and %s are mutually exclusive. You can only use one or
2654 Note that %l and %s are mutually exclusive. You can only use one or
2669 the other in your aliases.
2655 the other in your aliases.
2670
2656
2671 Aliases expand Python variables just like system calls using ! or !!
2657 Aliases expand Python variables just like system calls using ! or !!
2672 do: all expressions prefixed with '$' get expanded. For details of
2658 do: all expressions prefixed with '$' get expanded. For details of
2673 the semantic rules, see PEP-215:
2659 the semantic rules, see PEP-215:
2674 http://www.python.org/peps/pep-0215.html. This is the library used by
2660 http://www.python.org/peps/pep-0215.html. This is the library used by
2675 IPython for variable expansion. If you want to access a true shell
2661 IPython for variable expansion. If you want to access a true shell
2676 variable, an extra $ is necessary to prevent its expansion by IPython:
2662 variable, an extra $ is necessary to prevent its expansion by IPython:
2677
2663
2678 In [6]: alias show echo
2664 In [6]: alias show echo
2679 In [7]: PATH='A Python string'
2665 In [7]: PATH='A Python string'
2680 In [8]: show $PATH
2666 In [8]: show $PATH
2681 A Python string
2667 A Python string
2682 In [9]: show $$PATH
2668 In [9]: show $$PATH
2683 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2669 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2684
2670
2685 You can use the alias facility to acess all of $PATH. See the %rehash
2671 You can use the alias facility to acess all of $PATH. See the %rehash
2686 and %rehashx functions, which automatically create aliases for the
2672 and %rehashx functions, which automatically create aliases for the
2687 contents of your $PATH.
2673 contents of your $PATH.
2688
2674
2689 If called with no parameters, %alias prints the current alias table."""
2675 If called with no parameters, %alias prints the current alias table."""
2690
2676
2691 par = parameter_s.strip()
2677 par = parameter_s.strip()
2692 if not par:
2678 if not par:
2693 stored = self.db.get('stored_aliases', {} )
2679 stored = self.db.get('stored_aliases', {} )
2694 aliases = sorted(self.shell.alias_manager.aliases)
2680 aliases = sorted(self.shell.alias_manager.aliases)
2695 # for k, v in stored:
2681 # for k, v in stored:
2696 # atab.append(k, v[0])
2682 # atab.append(k, v[0])
2697
2683
2698 print "Total number of aliases:", len(aliases)
2684 print "Total number of aliases:", len(aliases)
2699 return aliases
2685 return aliases
2700
2686
2701 # Now try to define a new one
2687 # Now try to define a new one
2702 try:
2688 try:
2703 alias,cmd = par.split(None, 1)
2689 alias,cmd = par.split(None, 1)
2704 except:
2690 except:
2705 print oinspect.getdoc(self.magic_alias)
2691 print oinspect.getdoc(self.magic_alias)
2706 else:
2692 else:
2707 self.shell.alias_manager.soft_define_alias(alias, cmd)
2693 self.shell.alias_manager.soft_define_alias(alias, cmd)
2708 # end magic_alias
2694 # end magic_alias
2709
2695
2710 def magic_unalias(self, parameter_s = ''):
2696 def magic_unalias(self, parameter_s = ''):
2711 """Remove an alias"""
2697 """Remove an alias"""
2712
2698
2713 aname = parameter_s.strip()
2699 aname = parameter_s.strip()
2714 self.shell.alias_manager.undefine_alias(aname)
2700 self.shell.alias_manager.undefine_alias(aname)
2715 stored = self.db.get('stored_aliases', {} )
2701 stored = self.db.get('stored_aliases', {} )
2716 if aname in stored:
2702 if aname in stored:
2717 print "Removing %stored alias",aname
2703 print "Removing %stored alias",aname
2718 del stored[aname]
2704 del stored[aname]
2719 self.db['stored_aliases'] = stored
2705 self.db['stored_aliases'] = stored
2720
2706
2721
2707
2722 def magic_rehashx(self, parameter_s = ''):
2708 def magic_rehashx(self, parameter_s = ''):
2723 """Update the alias table with all executable files in $PATH.
2709 """Update the alias table with all executable files in $PATH.
2724
2710
2725 This version explicitly checks that every entry in $PATH is a file
2711 This version explicitly checks that every entry in $PATH is a file
2726 with execute access (os.X_OK), so it is much slower than %rehash.
2712 with execute access (os.X_OK), so it is much slower than %rehash.
2727
2713
2728 Under Windows, it checks executability as a match agains a
2714 Under Windows, it checks executability as a match agains a
2729 '|'-separated string of extensions, stored in the IPython config
2715 '|'-separated string of extensions, stored in the IPython config
2730 variable win_exec_ext. This defaults to 'exe|com|bat'.
2716 variable win_exec_ext. This defaults to 'exe|com|bat'.
2731
2717
2732 This function also resets the root module cache of module completer,
2718 This function also resets the root module cache of module completer,
2733 used on slow filesystems.
2719 used on slow filesystems.
2734 """
2720 """
2735 from IPython.core.alias import InvalidAliasError
2721 from IPython.core.alias import InvalidAliasError
2736
2722
2737 # for the benefit of module completer in ipy_completers.py
2723 # for the benefit of module completer in ipy_completers.py
2738 del self.db['rootmodules']
2724 del self.db['rootmodules']
2739
2725
2740 path = [os.path.abspath(os.path.expanduser(p)) for p in
2726 path = [os.path.abspath(os.path.expanduser(p)) for p in
2741 os.environ.get('PATH','').split(os.pathsep)]
2727 os.environ.get('PATH','').split(os.pathsep)]
2742 path = filter(os.path.isdir,path)
2728 path = filter(os.path.isdir,path)
2743
2729
2744 syscmdlist = []
2730 syscmdlist = []
2745 # Now define isexec in a cross platform manner.
2731 # Now define isexec in a cross platform manner.
2746 if os.name == 'posix':
2732 if os.name == 'posix':
2747 isexec = lambda fname:os.path.isfile(fname) and \
2733 isexec = lambda fname:os.path.isfile(fname) and \
2748 os.access(fname,os.X_OK)
2734 os.access(fname,os.X_OK)
2749 else:
2735 else:
2750 try:
2736 try:
2751 winext = os.environ['pathext'].replace(';','|').replace('.','')
2737 winext = os.environ['pathext'].replace(';','|').replace('.','')
2752 except KeyError:
2738 except KeyError:
2753 winext = 'exe|com|bat|py'
2739 winext = 'exe|com|bat|py'
2754 if 'py' not in winext:
2740 if 'py' not in winext:
2755 winext += '|py'
2741 winext += '|py'
2756 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2742 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2757 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2743 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2758 savedir = os.getcwd()
2744 savedir = os.getcwd()
2759
2745
2760 # Now walk the paths looking for executables to alias.
2746 # Now walk the paths looking for executables to alias.
2761 try:
2747 try:
2762 # write the whole loop for posix/Windows so we don't have an if in
2748 # write the whole loop for posix/Windows so we don't have an if in
2763 # the innermost part
2749 # the innermost part
2764 if os.name == 'posix':
2750 if os.name == 'posix':
2765 for pdir in path:
2751 for pdir in path:
2766 os.chdir(pdir)
2752 os.chdir(pdir)
2767 for ff in os.listdir(pdir):
2753 for ff in os.listdir(pdir):
2768 if isexec(ff):
2754 if isexec(ff):
2769 try:
2755 try:
2770 # Removes dots from the name since ipython
2756 # Removes dots from the name since ipython
2771 # will assume names with dots to be python.
2757 # will assume names with dots to be python.
2772 self.shell.alias_manager.define_alias(
2758 self.shell.alias_manager.define_alias(
2773 ff.replace('.',''), ff)
2759 ff.replace('.',''), ff)
2774 except InvalidAliasError:
2760 except InvalidAliasError:
2775 pass
2761 pass
2776 else:
2762 else:
2777 syscmdlist.append(ff)
2763 syscmdlist.append(ff)
2778 else:
2764 else:
2779 no_alias = self.shell.alias_manager.no_alias
2765 no_alias = self.shell.alias_manager.no_alias
2780 for pdir in path:
2766 for pdir in path:
2781 os.chdir(pdir)
2767 os.chdir(pdir)
2782 for ff in os.listdir(pdir):
2768 for ff in os.listdir(pdir):
2783 base, ext = os.path.splitext(ff)
2769 base, ext = os.path.splitext(ff)
2784 if isexec(ff) and base.lower() not in no_alias:
2770 if isexec(ff) and base.lower() not in no_alias:
2785 if ext.lower() == '.exe':
2771 if ext.lower() == '.exe':
2786 ff = base
2772 ff = base
2787 try:
2773 try:
2788 # Removes dots from the name since ipython
2774 # Removes dots from the name since ipython
2789 # will assume names with dots to be python.
2775 # will assume names with dots to be python.
2790 self.shell.alias_manager.define_alias(
2776 self.shell.alias_manager.define_alias(
2791 base.lower().replace('.',''), ff)
2777 base.lower().replace('.',''), ff)
2792 except InvalidAliasError:
2778 except InvalidAliasError:
2793 pass
2779 pass
2794 syscmdlist.append(ff)
2780 syscmdlist.append(ff)
2795 db = self.db
2781 db = self.db
2796 db['syscmdlist'] = syscmdlist
2782 db['syscmdlist'] = syscmdlist
2797 finally:
2783 finally:
2798 os.chdir(savedir)
2784 os.chdir(savedir)
2799
2785
2800 def magic_pwd(self, parameter_s = ''):
2786 def magic_pwd(self, parameter_s = ''):
2801 """Return the current working directory path."""
2787 """Return the current working directory path."""
2802 return os.getcwd()
2788 return os.getcwd()
2803
2789
2804 def magic_cd(self, parameter_s=''):
2790 def magic_cd(self, parameter_s=''):
2805 """Change the current working directory.
2791 """Change the current working directory.
2806
2792
2807 This command automatically maintains an internal list of directories
2793 This command automatically maintains an internal list of directories
2808 you visit during your IPython session, in the variable _dh. The
2794 you visit during your IPython session, in the variable _dh. The
2809 command %dhist shows this history nicely formatted. You can also
2795 command %dhist shows this history nicely formatted. You can also
2810 do 'cd -<tab>' to see directory history conveniently.
2796 do 'cd -<tab>' to see directory history conveniently.
2811
2797
2812 Usage:
2798 Usage:
2813
2799
2814 cd 'dir': changes to directory 'dir'.
2800 cd 'dir': changes to directory 'dir'.
2815
2801
2816 cd -: changes to the last visited directory.
2802 cd -: changes to the last visited directory.
2817
2803
2818 cd -<n>: changes to the n-th directory in the directory history.
2804 cd -<n>: changes to the n-th directory in the directory history.
2819
2805
2820 cd --foo: change to directory that matches 'foo' in history
2806 cd --foo: change to directory that matches 'foo' in history
2821
2807
2822 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2808 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2823 (note: cd <bookmark_name> is enough if there is no
2809 (note: cd <bookmark_name> is enough if there is no
2824 directory <bookmark_name>, but a bookmark with the name exists.)
2810 directory <bookmark_name>, but a bookmark with the name exists.)
2825 'cd -b <tab>' allows you to tab-complete bookmark names.
2811 'cd -b <tab>' allows you to tab-complete bookmark names.
2826
2812
2827 Options:
2813 Options:
2828
2814
2829 -q: quiet. Do not print the working directory after the cd command is
2815 -q: quiet. Do not print the working directory after the cd command is
2830 executed. By default IPython's cd command does print this directory,
2816 executed. By default IPython's cd command does print this directory,
2831 since the default prompts do not display path information.
2817 since the default prompts do not display path information.
2832
2818
2833 Note that !cd doesn't work for this purpose because the shell where
2819 Note that !cd doesn't work for this purpose because the shell where
2834 !command runs is immediately discarded after executing 'command'."""
2820 !command runs is immediately discarded after executing 'command'."""
2835
2821
2836 parameter_s = parameter_s.strip()
2822 parameter_s = parameter_s.strip()
2837 #bkms = self.shell.persist.get("bookmarks",{})
2823 #bkms = self.shell.persist.get("bookmarks",{})
2838
2824
2839 oldcwd = os.getcwd()
2825 oldcwd = os.getcwd()
2840 numcd = re.match(r'(-)(\d+)$',parameter_s)
2826 numcd = re.match(r'(-)(\d+)$',parameter_s)
2841 # jump in directory history by number
2827 # jump in directory history by number
2842 if numcd:
2828 if numcd:
2843 nn = int(numcd.group(2))
2829 nn = int(numcd.group(2))
2844 try:
2830 try:
2845 ps = self.shell.user_ns['_dh'][nn]
2831 ps = self.shell.user_ns['_dh'][nn]
2846 except IndexError:
2832 except IndexError:
2847 print 'The requested directory does not exist in history.'
2833 print 'The requested directory does not exist in history.'
2848 return
2834 return
2849 else:
2835 else:
2850 opts = {}
2836 opts = {}
2851 elif parameter_s.startswith('--'):
2837 elif parameter_s.startswith('--'):
2852 ps = None
2838 ps = None
2853 fallback = None
2839 fallback = None
2854 pat = parameter_s[2:]
2840 pat = parameter_s[2:]
2855 dh = self.shell.user_ns['_dh']
2841 dh = self.shell.user_ns['_dh']
2856 # first search only by basename (last component)
2842 # first search only by basename (last component)
2857 for ent in reversed(dh):
2843 for ent in reversed(dh):
2858 if pat in os.path.basename(ent) and os.path.isdir(ent):
2844 if pat in os.path.basename(ent) and os.path.isdir(ent):
2859 ps = ent
2845 ps = ent
2860 break
2846 break
2861
2847
2862 if fallback is None and pat in ent and os.path.isdir(ent):
2848 if fallback is None and pat in ent and os.path.isdir(ent):
2863 fallback = ent
2849 fallback = ent
2864
2850
2865 # if we have no last part match, pick the first full path match
2851 # if we have no last part match, pick the first full path match
2866 if ps is None:
2852 if ps is None:
2867 ps = fallback
2853 ps = fallback
2868
2854
2869 if ps is None:
2855 if ps is None:
2870 print "No matching entry in directory history"
2856 print "No matching entry in directory history"
2871 return
2857 return
2872 else:
2858 else:
2873 opts = {}
2859 opts = {}
2874
2860
2875
2861
2876 else:
2862 else:
2877 #turn all non-space-escaping backslashes to slashes,
2863 #turn all non-space-escaping backslashes to slashes,
2878 # for c:\windows\directory\names\
2864 # for c:\windows\directory\names\
2879 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2865 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2880 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2866 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2881 # jump to previous
2867 # jump to previous
2882 if ps == '-':
2868 if ps == '-':
2883 try:
2869 try:
2884 ps = self.shell.user_ns['_dh'][-2]
2870 ps = self.shell.user_ns['_dh'][-2]
2885 except IndexError:
2871 except IndexError:
2886 raise UsageError('%cd -: No previous directory to change to.')
2872 raise UsageError('%cd -: No previous directory to change to.')
2887 # jump to bookmark if needed
2873 # jump to bookmark if needed
2888 else:
2874 else:
2889 if not os.path.isdir(ps) or opts.has_key('b'):
2875 if not os.path.isdir(ps) or opts.has_key('b'):
2890 bkms = self.db.get('bookmarks', {})
2876 bkms = self.db.get('bookmarks', {})
2891
2877
2892 if bkms.has_key(ps):
2878 if bkms.has_key(ps):
2893 target = bkms[ps]
2879 target = bkms[ps]
2894 print '(bookmark:%s) -> %s' % (ps,target)
2880 print '(bookmark:%s) -> %s' % (ps,target)
2895 ps = target
2881 ps = target
2896 else:
2882 else:
2897 if opts.has_key('b'):
2883 if opts.has_key('b'):
2898 raise UsageError("Bookmark '%s' not found. "
2884 raise UsageError("Bookmark '%s' not found. "
2899 "Use '%%bookmark -l' to see your bookmarks." % ps)
2885 "Use '%%bookmark -l' to see your bookmarks." % ps)
2900
2886
2901 # at this point ps should point to the target dir
2887 # at this point ps should point to the target dir
2902 if ps:
2888 if ps:
2903 try:
2889 try:
2904 os.chdir(os.path.expanduser(ps))
2890 os.chdir(os.path.expanduser(ps))
2905 if self.shell.term_title:
2891 if self.shell.term_title:
2906 set_term_title('IPython: ' + abbrev_cwd())
2892 set_term_title('IPython: ' + abbrev_cwd())
2907 except OSError:
2893 except OSError:
2908 print sys.exc_info()[1]
2894 print sys.exc_info()[1]
2909 else:
2895 else:
2910 cwd = os.getcwd()
2896 cwd = os.getcwd()
2911 dhist = self.shell.user_ns['_dh']
2897 dhist = self.shell.user_ns['_dh']
2912 if oldcwd != cwd:
2898 if oldcwd != cwd:
2913 dhist.append(cwd)
2899 dhist.append(cwd)
2914 self.db['dhist'] = compress_dhist(dhist)[-100:]
2900 self.db['dhist'] = compress_dhist(dhist)[-100:]
2915
2901
2916 else:
2902 else:
2917 os.chdir(self.shell.home_dir)
2903 os.chdir(self.shell.home_dir)
2918 if self.shell.term_title:
2904 if self.shell.term_title:
2919 set_term_title('IPython: ' + '~')
2905 set_term_title('IPython: ' + '~')
2920 cwd = os.getcwd()
2906 cwd = os.getcwd()
2921 dhist = self.shell.user_ns['_dh']
2907 dhist = self.shell.user_ns['_dh']
2922
2908
2923 if oldcwd != cwd:
2909 if oldcwd != cwd:
2924 dhist.append(cwd)
2910 dhist.append(cwd)
2925 self.db['dhist'] = compress_dhist(dhist)[-100:]
2911 self.db['dhist'] = compress_dhist(dhist)[-100:]
2926 if not 'q' in opts and self.shell.user_ns['_dh']:
2912 if not 'q' in opts and self.shell.user_ns['_dh']:
2927 print self.shell.user_ns['_dh'][-1]
2913 print self.shell.user_ns['_dh'][-1]
2928
2914
2929
2915
2930 def magic_env(self, parameter_s=''):
2916 def magic_env(self, parameter_s=''):
2931 """List environment variables."""
2917 """List environment variables."""
2932
2918
2933 return os.environ.data
2919 return os.environ.data
2934
2920
2935 def magic_pushd(self, parameter_s=''):
2921 def magic_pushd(self, parameter_s=''):
2936 """Place the current dir on stack and change directory.
2922 """Place the current dir on stack and change directory.
2937
2923
2938 Usage:\\
2924 Usage:\\
2939 %pushd ['dirname']
2925 %pushd ['dirname']
2940 """
2926 """
2941
2927
2942 dir_s = self.shell.dir_stack
2928 dir_s = self.shell.dir_stack
2943 tgt = os.path.expanduser(parameter_s)
2929 tgt = os.path.expanduser(parameter_s)
2944 cwd = os.getcwd().replace(self.home_dir,'~')
2930 cwd = os.getcwd().replace(self.home_dir,'~')
2945 if tgt:
2931 if tgt:
2946 self.magic_cd(parameter_s)
2932 self.magic_cd(parameter_s)
2947 dir_s.insert(0,cwd)
2933 dir_s.insert(0,cwd)
2948 return self.magic_dirs()
2934 return self.magic_dirs()
2949
2935
2950 def magic_popd(self, parameter_s=''):
2936 def magic_popd(self, parameter_s=''):
2951 """Change to directory popped off the top of the stack.
2937 """Change to directory popped off the top of the stack.
2952 """
2938 """
2953 if not self.shell.dir_stack:
2939 if not self.shell.dir_stack:
2954 raise UsageError("%popd on empty stack")
2940 raise UsageError("%popd on empty stack")
2955 top = self.shell.dir_stack.pop(0)
2941 top = self.shell.dir_stack.pop(0)
2956 self.magic_cd(top)
2942 self.magic_cd(top)
2957 print "popd ->",top
2943 print "popd ->",top
2958
2944
2959 def magic_dirs(self, parameter_s=''):
2945 def magic_dirs(self, parameter_s=''):
2960 """Return the current directory stack."""
2946 """Return the current directory stack."""
2961
2947
2962 return self.shell.dir_stack
2948 return self.shell.dir_stack
2963
2949
2964 def magic_dhist(self, parameter_s=''):
2950 def magic_dhist(self, parameter_s=''):
2965 """Print your history of visited directories.
2951 """Print your history of visited directories.
2966
2952
2967 %dhist -> print full history\\
2953 %dhist -> print full history\\
2968 %dhist n -> print last n entries only\\
2954 %dhist n -> print last n entries only\\
2969 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2955 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2970
2956
2971 This history is automatically maintained by the %cd command, and
2957 This history is automatically maintained by the %cd command, and
2972 always available as the global list variable _dh. You can use %cd -<n>
2958 always available as the global list variable _dh. You can use %cd -<n>
2973 to go to directory number <n>.
2959 to go to directory number <n>.
2974
2960
2975 Note that most of time, you should view directory history by entering
2961 Note that most of time, you should view directory history by entering
2976 cd -<TAB>.
2962 cd -<TAB>.
2977
2963
2978 """
2964 """
2979
2965
2980 dh = self.shell.user_ns['_dh']
2966 dh = self.shell.user_ns['_dh']
2981 if parameter_s:
2967 if parameter_s:
2982 try:
2968 try:
2983 args = map(int,parameter_s.split())
2969 args = map(int,parameter_s.split())
2984 except:
2970 except:
2985 self.arg_err(Magic.magic_dhist)
2971 self.arg_err(Magic.magic_dhist)
2986 return
2972 return
2987 if len(args) == 1:
2973 if len(args) == 1:
2988 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2974 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2989 elif len(args) == 2:
2975 elif len(args) == 2:
2990 ini,fin = args
2976 ini,fin = args
2991 else:
2977 else:
2992 self.arg_err(Magic.magic_dhist)
2978 self.arg_err(Magic.magic_dhist)
2993 return
2979 return
2994 else:
2980 else:
2995 ini,fin = 0,len(dh)
2981 ini,fin = 0,len(dh)
2996 nlprint(dh,
2982 nlprint(dh,
2997 header = 'Directory history (kept in _dh)',
2983 header = 'Directory history (kept in _dh)',
2998 start=ini,stop=fin)
2984 start=ini,stop=fin)
2999
2985
3000 @testdec.skip_doctest
2986 @testdec.skip_doctest
3001 def magic_sc(self, parameter_s=''):
2987 def magic_sc(self, parameter_s=''):
3002 """Shell capture - execute a shell command and capture its output.
2988 """Shell capture - execute a shell command and capture its output.
3003
2989
3004 DEPRECATED. Suboptimal, retained for backwards compatibility.
2990 DEPRECATED. Suboptimal, retained for backwards compatibility.
3005
2991
3006 You should use the form 'var = !command' instead. Example:
2992 You should use the form 'var = !command' instead. Example:
3007
2993
3008 "%sc -l myfiles = ls ~" should now be written as
2994 "%sc -l myfiles = ls ~" should now be written as
3009
2995
3010 "myfiles = !ls ~"
2996 "myfiles = !ls ~"
3011
2997
3012 myfiles.s, myfiles.l and myfiles.n still apply as documented
2998 myfiles.s, myfiles.l and myfiles.n still apply as documented
3013 below.
2999 below.
3014
3000
3015 --
3001 --
3016 %sc [options] varname=command
3002 %sc [options] varname=command
3017
3003
3018 IPython will run the given command using commands.getoutput(), and
3004 IPython will run the given command using commands.getoutput(), and
3019 will then update the user's interactive namespace with a variable
3005 will then update the user's interactive namespace with a variable
3020 called varname, containing the value of the call. Your command can
3006 called varname, containing the value of the call. Your command can
3021 contain shell wildcards, pipes, etc.
3007 contain shell wildcards, pipes, etc.
3022
3008
3023 The '=' sign in the syntax is mandatory, and the variable name you
3009 The '=' sign in the syntax is mandatory, and the variable name you
3024 supply must follow Python's standard conventions for valid names.
3010 supply must follow Python's standard conventions for valid names.
3025
3011
3026 (A special format without variable name exists for internal use)
3012 (A special format without variable name exists for internal use)
3027
3013
3028 Options:
3014 Options:
3029
3015
3030 -l: list output. Split the output on newlines into a list before
3016 -l: list output. Split the output on newlines into a list before
3031 assigning it to the given variable. By default the output is stored
3017 assigning it to the given variable. By default the output is stored
3032 as a single string.
3018 as a single string.
3033
3019
3034 -v: verbose. Print the contents of the variable.
3020 -v: verbose. Print the contents of the variable.
3035
3021
3036 In most cases you should not need to split as a list, because the
3022 In most cases you should not need to split as a list, because the
3037 returned value is a special type of string which can automatically
3023 returned value is a special type of string which can automatically
3038 provide its contents either as a list (split on newlines) or as a
3024 provide its contents either as a list (split on newlines) or as a
3039 space-separated string. These are convenient, respectively, either
3025 space-separated string. These are convenient, respectively, either
3040 for sequential processing or to be passed to a shell command.
3026 for sequential processing or to be passed to a shell command.
3041
3027
3042 For example:
3028 For example:
3043
3029
3044 # all-random
3030 # all-random
3045
3031
3046 # Capture into variable a
3032 # Capture into variable a
3047 In [1]: sc a=ls *py
3033 In [1]: sc a=ls *py
3048
3034
3049 # a is a string with embedded newlines
3035 # a is a string with embedded newlines
3050 In [2]: a
3036 In [2]: a
3051 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
3037 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
3052
3038
3053 # which can be seen as a list:
3039 # which can be seen as a list:
3054 In [3]: a.l
3040 In [3]: a.l
3055 Out[3]: ['setup.py', 'win32_manual_post_install.py']
3041 Out[3]: ['setup.py', 'win32_manual_post_install.py']
3056
3042
3057 # or as a whitespace-separated string:
3043 # or as a whitespace-separated string:
3058 In [4]: a.s
3044 In [4]: a.s
3059 Out[4]: 'setup.py win32_manual_post_install.py'
3045 Out[4]: 'setup.py win32_manual_post_install.py'
3060
3046
3061 # a.s is useful to pass as a single command line:
3047 # a.s is useful to pass as a single command line:
3062 In [5]: !wc -l $a.s
3048 In [5]: !wc -l $a.s
3063 146 setup.py
3049 146 setup.py
3064 130 win32_manual_post_install.py
3050 130 win32_manual_post_install.py
3065 276 total
3051 276 total
3066
3052
3067 # while the list form is useful to loop over:
3053 # while the list form is useful to loop over:
3068 In [6]: for f in a.l:
3054 In [6]: for f in a.l:
3069 ...: !wc -l $f
3055 ...: !wc -l $f
3070 ...:
3056 ...:
3071 146 setup.py
3057 146 setup.py
3072 130 win32_manual_post_install.py
3058 130 win32_manual_post_install.py
3073
3059
3074 Similiarly, the lists returned by the -l option are also special, in
3060 Similiarly, the lists returned by the -l option are also special, in
3075 the sense that you can equally invoke the .s attribute on them to
3061 the sense that you can equally invoke the .s attribute on them to
3076 automatically get a whitespace-separated string from their contents:
3062 automatically get a whitespace-separated string from their contents:
3077
3063
3078 In [7]: sc -l b=ls *py
3064 In [7]: sc -l b=ls *py
3079
3065
3080 In [8]: b
3066 In [8]: b
3081 Out[8]: ['setup.py', 'win32_manual_post_install.py']
3067 Out[8]: ['setup.py', 'win32_manual_post_install.py']
3082
3068
3083 In [9]: b.s
3069 In [9]: b.s
3084 Out[9]: 'setup.py win32_manual_post_install.py'
3070 Out[9]: 'setup.py win32_manual_post_install.py'
3085
3071
3086 In summary, both the lists and strings used for ouptut capture have
3072 In summary, both the lists and strings used for ouptut capture have
3087 the following special attributes:
3073 the following special attributes:
3088
3074
3089 .l (or .list) : value as list.
3075 .l (or .list) : value as list.
3090 .n (or .nlstr): value as newline-separated string.
3076 .n (or .nlstr): value as newline-separated string.
3091 .s (or .spstr): value as space-separated string.
3077 .s (or .spstr): value as space-separated string.
3092 """
3078 """
3093
3079
3094 opts,args = self.parse_options(parameter_s,'lv')
3080 opts,args = self.parse_options(parameter_s,'lv')
3095 # Try to get a variable name and command to run
3081 # Try to get a variable name and command to run
3096 try:
3082 try:
3097 # the variable name must be obtained from the parse_options
3083 # the variable name must be obtained from the parse_options
3098 # output, which uses shlex.split to strip options out.
3084 # output, which uses shlex.split to strip options out.
3099 var,_ = args.split('=',1)
3085 var,_ = args.split('=',1)
3100 var = var.strip()
3086 var = var.strip()
3101 # But the the command has to be extracted from the original input
3087 # But the the command has to be extracted from the original input
3102 # parameter_s, not on what parse_options returns, to avoid the
3088 # parameter_s, not on what parse_options returns, to avoid the
3103 # quote stripping which shlex.split performs on it.
3089 # quote stripping which shlex.split performs on it.
3104 _,cmd = parameter_s.split('=',1)
3090 _,cmd = parameter_s.split('=',1)
3105 except ValueError:
3091 except ValueError:
3106 var,cmd = '',''
3092 var,cmd = '',''
3107 # If all looks ok, proceed
3093 # If all looks ok, proceed
3108 out,err = self.shell.getoutputerror(cmd)
3094 out,err = self.shell.getoutputerror(cmd)
3109 if err:
3095 if err:
3110 print >> Term.cerr,err
3096 print >> Term.cerr,err
3111 if opts.has_key('l'):
3097 if opts.has_key('l'):
3112 out = SList(out.split('\n'))
3098 out = SList(out.split('\n'))
3113 else:
3099 else:
3114 out = LSString(out)
3100 out = LSString(out)
3115 if opts.has_key('v'):
3101 if opts.has_key('v'):
3116 print '%s ==\n%s' % (var,pformat(out))
3102 print '%s ==\n%s' % (var,pformat(out))
3117 if var:
3103 if var:
3118 self.shell.user_ns.update({var:out})
3104 self.shell.user_ns.update({var:out})
3119 else:
3105 else:
3120 return out
3106 return out
3121
3107
3122 def magic_sx(self, parameter_s=''):
3108 def magic_sx(self, parameter_s=''):
3123 """Shell execute - run a shell command and capture its output.
3109 """Shell execute - run a shell command and capture its output.
3124
3110
3125 %sx command
3111 %sx command
3126
3112
3127 IPython will run the given command using commands.getoutput(), and
3113 IPython will run the given command using commands.getoutput(), and
3128 return the result formatted as a list (split on '\\n'). Since the
3114 return the result formatted as a list (split on '\\n'). Since the
3129 output is _returned_, it will be stored in ipython's regular output
3115 output is _returned_, it will be stored in ipython's regular output
3130 cache Out[N] and in the '_N' automatic variables.
3116 cache Out[N] and in the '_N' automatic variables.
3131
3117
3132 Notes:
3118 Notes:
3133
3119
3134 1) If an input line begins with '!!', then %sx is automatically
3120 1) If an input line begins with '!!', then %sx is automatically
3135 invoked. That is, while:
3121 invoked. That is, while:
3136 !ls
3122 !ls
3137 causes ipython to simply issue system('ls'), typing
3123 causes ipython to simply issue system('ls'), typing
3138 !!ls
3124 !!ls
3139 is a shorthand equivalent to:
3125 is a shorthand equivalent to:
3140 %sx ls
3126 %sx ls
3141
3127
3142 2) %sx differs from %sc in that %sx automatically splits into a list,
3128 2) %sx differs from %sc in that %sx automatically splits into a list,
3143 like '%sc -l'. The reason for this is to make it as easy as possible
3129 like '%sc -l'. The reason for this is to make it as easy as possible
3144 to process line-oriented shell output via further python commands.
3130 to process line-oriented shell output via further python commands.
3145 %sc is meant to provide much finer control, but requires more
3131 %sc is meant to provide much finer control, but requires more
3146 typing.
3132 typing.
3147
3133
3148 3) Just like %sc -l, this is a list with special attributes:
3134 3) Just like %sc -l, this is a list with special attributes:
3149
3135
3150 .l (or .list) : value as list.
3136 .l (or .list) : value as list.
3151 .n (or .nlstr): value as newline-separated string.
3137 .n (or .nlstr): value as newline-separated string.
3152 .s (or .spstr): value as whitespace-separated string.
3138 .s (or .spstr): value as whitespace-separated string.
3153
3139
3154 This is very useful when trying to use such lists as arguments to
3140 This is very useful when trying to use such lists as arguments to
3155 system commands."""
3141 system commands."""
3156
3142
3157 if parameter_s:
3143 if parameter_s:
3158 out,err = self.shell.getoutputerror(parameter_s)
3144 out,err = self.shell.getoutputerror(parameter_s)
3159 if err:
3145 if err:
3160 print >> Term.cerr,err
3146 print >> Term.cerr,err
3161 return SList(out.split('\n'))
3147 return SList(out.split('\n'))
3162
3148
3163 def magic_bg(self, parameter_s=''):
3164 """Run a job in the background, in a separate thread.
3165
3166 For example,
3167
3168 %bg myfunc(x,y,z=1)
3169
3170 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
3171 execution starts, a message will be printed indicating the job
3172 number. If your job number is 5, you can use
3173
3174 myvar = jobs.result(5) or myvar = jobs[5].result
3175
3176 to assign this result to variable 'myvar'.
3177
3178 IPython has a job manager, accessible via the 'jobs' object. You can
3179 type jobs? to get more information about it, and use jobs.<TAB> to see
3180 its attributes. All attributes not starting with an underscore are
3181 meant for public use.
3182
3183 In particular, look at the jobs.new() method, which is used to create
3184 new jobs. This magic %bg function is just a convenience wrapper
3185 around jobs.new(), for expression-based jobs. If you want to create a
3186 new job with an explicit function object and arguments, you must call
3187 jobs.new() directly.
3188
3189 The jobs.new docstring also describes in detail several important
3190 caveats associated with a thread-based model for background job
3191 execution. Type jobs.new? for details.
3192
3193 You can check the status of all jobs with jobs.status().
3194
3195 The jobs variable is set by IPython into the Python builtin namespace.
3196 If you ever declare a variable named 'jobs', you will shadow this
3197 name. You can either delete your global jobs variable to regain
3198 access to the job manager, or make a new name and assign it manually
3199 to the manager (stored in IPython's namespace). For example, to
3200 assign the job manager to the Jobs name, use:
3201
3202 Jobs = __builtins__.jobs"""
3203
3204 self.shell.jobs.new(parameter_s,self.shell.user_ns)
3205
3206 def magic_r(self, parameter_s=''):
3149 def magic_r(self, parameter_s=''):
3207 """Repeat previous input.
3150 """Repeat previous input.
3208
3151
3209 Note: Consider using the more powerfull %rep instead!
3152 Note: Consider using the more powerfull %rep instead!
3210
3153
3211 If given an argument, repeats the previous command which starts with
3154 If given an argument, repeats the previous command which starts with
3212 the same string, otherwise it just repeats the previous input.
3155 the same string, otherwise it just repeats the previous input.
3213
3156
3214 Shell escaped commands (with ! as first character) are not recognized
3157 Shell escaped commands (with ! as first character) are not recognized
3215 by this system, only pure python code and magic commands.
3158 by this system, only pure python code and magic commands.
3216 """
3159 """
3217
3160
3218 start = parameter_s.strip()
3161 start = parameter_s.strip()
3219 esc_magic = ESC_MAGIC
3162 esc_magic = ESC_MAGIC
3220 # Identify magic commands even if automagic is on (which means
3163 # Identify magic commands even if automagic is on (which means
3221 # the in-memory version is different from that typed by the user).
3164 # the in-memory version is different from that typed by the user).
3222 if self.shell.automagic:
3165 if self.shell.automagic:
3223 start_magic = esc_magic+start
3166 start_magic = esc_magic+start
3224 else:
3167 else:
3225 start_magic = start
3168 start_magic = start
3226 # Look through the input history in reverse
3169 # Look through the input history in reverse
3227 for n in range(len(self.shell.input_hist)-2,0,-1):
3170 for n in range(len(self.shell.input_hist)-2,0,-1):
3228 input = self.shell.input_hist[n]
3171 input = self.shell.input_hist[n]
3229 # skip plain 'r' lines so we don't recurse to infinity
3172 # skip plain 'r' lines so we don't recurse to infinity
3230 if input != '_ip.magic("r")\n' and \
3173 if input != '_ip.magic("r")\n' and \
3231 (input.startswith(start) or input.startswith(start_magic)):
3174 (input.startswith(start) or input.startswith(start_magic)):
3232 #print 'match',`input` # dbg
3175 #print 'match',`input` # dbg
3233 print 'Executing:',input,
3176 print 'Executing:',input,
3234 self.shell.runlines(input)
3177 self.shell.runlines(input)
3235 return
3178 return
3236 print 'No previous input matching `%s` found.' % start
3179 print 'No previous input matching `%s` found.' % start
3237
3180
3238
3181
3239 def magic_bookmark(self, parameter_s=''):
3182 def magic_bookmark(self, parameter_s=''):
3240 """Manage IPython's bookmark system.
3183 """Manage IPython's bookmark system.
3241
3184
3242 %bookmark <name> - set bookmark to current dir
3185 %bookmark <name> - set bookmark to current dir
3243 %bookmark <name> <dir> - set bookmark to <dir>
3186 %bookmark <name> <dir> - set bookmark to <dir>
3244 %bookmark -l - list all bookmarks
3187 %bookmark -l - list all bookmarks
3245 %bookmark -d <name> - remove bookmark
3188 %bookmark -d <name> - remove bookmark
3246 %bookmark -r - remove all bookmarks
3189 %bookmark -r - remove all bookmarks
3247
3190
3248 You can later on access a bookmarked folder with:
3191 You can later on access a bookmarked folder with:
3249 %cd -b <name>
3192 %cd -b <name>
3250 or simply '%cd <name>' if there is no directory called <name> AND
3193 or simply '%cd <name>' if there is no directory called <name> AND
3251 there is such a bookmark defined.
3194 there is such a bookmark defined.
3252
3195
3253 Your bookmarks persist through IPython sessions, but they are
3196 Your bookmarks persist through IPython sessions, but they are
3254 associated with each profile."""
3197 associated with each profile."""
3255
3198
3256 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3199 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3257 if len(args) > 2:
3200 if len(args) > 2:
3258 raise UsageError("%bookmark: too many arguments")
3201 raise UsageError("%bookmark: too many arguments")
3259
3202
3260 bkms = self.db.get('bookmarks',{})
3203 bkms = self.db.get('bookmarks',{})
3261
3204
3262 if opts.has_key('d'):
3205 if opts.has_key('d'):
3263 try:
3206 try:
3264 todel = args[0]
3207 todel = args[0]
3265 except IndexError:
3208 except IndexError:
3266 raise UsageError(
3209 raise UsageError(
3267 "%bookmark -d: must provide a bookmark to delete")
3210 "%bookmark -d: must provide a bookmark to delete")
3268 else:
3211 else:
3269 try:
3212 try:
3270 del bkms[todel]
3213 del bkms[todel]
3271 except KeyError:
3214 except KeyError:
3272 raise UsageError(
3215 raise UsageError(
3273 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3216 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3274
3217
3275 elif opts.has_key('r'):
3218 elif opts.has_key('r'):
3276 bkms = {}
3219 bkms = {}
3277 elif opts.has_key('l'):
3220 elif opts.has_key('l'):
3278 bks = bkms.keys()
3221 bks = bkms.keys()
3279 bks.sort()
3222 bks.sort()
3280 if bks:
3223 if bks:
3281 size = max(map(len,bks))
3224 size = max(map(len,bks))
3282 else:
3225 else:
3283 size = 0
3226 size = 0
3284 fmt = '%-'+str(size)+'s -> %s'
3227 fmt = '%-'+str(size)+'s -> %s'
3285 print 'Current bookmarks:'
3228 print 'Current bookmarks:'
3286 for bk in bks:
3229 for bk in bks:
3287 print fmt % (bk,bkms[bk])
3230 print fmt % (bk,bkms[bk])
3288 else:
3231 else:
3289 if not args:
3232 if not args:
3290 raise UsageError("%bookmark: You must specify the bookmark name")
3233 raise UsageError("%bookmark: You must specify the bookmark name")
3291 elif len(args)==1:
3234 elif len(args)==1:
3292 bkms[args[0]] = os.getcwd()
3235 bkms[args[0]] = os.getcwd()
3293 elif len(args)==2:
3236 elif len(args)==2:
3294 bkms[args[0]] = args[1]
3237 bkms[args[0]] = args[1]
3295 self.db['bookmarks'] = bkms
3238 self.db['bookmarks'] = bkms
3296
3239
3297 def magic_pycat(self, parameter_s=''):
3240 def magic_pycat(self, parameter_s=''):
3298 """Show a syntax-highlighted file through a pager.
3241 """Show a syntax-highlighted file through a pager.
3299
3242
3300 This magic is similar to the cat utility, but it will assume the file
3243 This magic is similar to the cat utility, but it will assume the file
3301 to be Python source and will show it with syntax highlighting. """
3244 to be Python source and will show it with syntax highlighting. """
3302
3245
3303 try:
3246 try:
3304 filename = get_py_filename(parameter_s)
3247 filename = get_py_filename(parameter_s)
3305 cont = file_read(filename)
3248 cont = file_read(filename)
3306 except IOError:
3249 except IOError:
3307 try:
3250 try:
3308 cont = eval(parameter_s,self.user_ns)
3251 cont = eval(parameter_s,self.user_ns)
3309 except NameError:
3252 except NameError:
3310 cont = None
3253 cont = None
3311 if cont is None:
3254 if cont is None:
3312 print "Error: no such file or variable"
3255 print "Error: no such file or variable"
3313 return
3256 return
3314
3257
3315 page(self.shell.pycolorize(cont),
3258 page(self.shell.pycolorize(cont),
3316 screen_lines=self.shell.usable_screen_length)
3259 screen_lines=self.shell.usable_screen_length)
3317
3260
3318 def _rerun_pasted(self):
3261 def _rerun_pasted(self):
3319 """ Rerun a previously pasted command.
3262 """ Rerun a previously pasted command.
3320 """
3263 """
3321 b = self.user_ns.get('pasted_block', None)
3264 b = self.user_ns.get('pasted_block', None)
3322 if b is None:
3265 if b is None:
3323 raise UsageError('No previous pasted block available')
3266 raise UsageError('No previous pasted block available')
3324 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
3267 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
3325 exec b in self.user_ns
3268 exec b in self.user_ns
3326
3269
3327 def _get_pasted_lines(self, sentinel):
3270 def _get_pasted_lines(self, sentinel):
3328 """ Yield pasted lines until the user enters the given sentinel value.
3271 """ Yield pasted lines until the user enters the given sentinel value.
3329 """
3272 """
3330 from IPython.core import interactiveshell
3273 from IPython.core import interactiveshell
3331 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3274 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3332 while True:
3275 while True:
3333 l = interactiveshell.raw_input_original(':')
3276 l = interactiveshell.raw_input_original(':')
3334 if l == sentinel:
3277 if l == sentinel:
3335 return
3278 return
3336 else:
3279 else:
3337 yield l
3280 yield l
3338
3281
3339 def _strip_pasted_lines_for_code(self, raw_lines):
3282 def _strip_pasted_lines_for_code(self, raw_lines):
3340 """ Strip non-code parts of a sequence of lines to return a block of
3283 """ Strip non-code parts of a sequence of lines to return a block of
3341 code.
3284 code.
3342 """
3285 """
3343 # Regular expressions that declare text we strip from the input:
3286 # Regular expressions that declare text we strip from the input:
3344 strip_re = [r'^\s*In \[\d+\]:', # IPython input prompt
3287 strip_re = [r'^\s*In \[\d+\]:', # IPython input prompt
3345 r'^\s*(\s?>)+', # Python input prompt
3288 r'^\s*(\s?>)+', # Python input prompt
3346 r'^\s*\.{3,}', # Continuation prompts
3289 r'^\s*\.{3,}', # Continuation prompts
3347 r'^\++',
3290 r'^\++',
3348 ]
3291 ]
3349
3292
3350 strip_from_start = map(re.compile,strip_re)
3293 strip_from_start = map(re.compile,strip_re)
3351
3294
3352 lines = []
3295 lines = []
3353 for l in raw_lines:
3296 for l in raw_lines:
3354 for pat in strip_from_start:
3297 for pat in strip_from_start:
3355 l = pat.sub('',l)
3298 l = pat.sub('',l)
3356 lines.append(l)
3299 lines.append(l)
3357
3300
3358 block = "\n".join(lines) + '\n'
3301 block = "\n".join(lines) + '\n'
3359 #print "block:\n",block
3302 #print "block:\n",block
3360 return block
3303 return block
3361
3304
3362 def _execute_block(self, block, par):
3305 def _execute_block(self, block, par):
3363 """ Execute a block, or store it in a variable, per the user's request.
3306 """ Execute a block, or store it in a variable, per the user's request.
3364 """
3307 """
3365 if not par:
3308 if not par:
3366 b = textwrap.dedent(block)
3309 b = textwrap.dedent(block)
3367 self.user_ns['pasted_block'] = b
3310 self.user_ns['pasted_block'] = b
3368 exec b in self.user_ns
3311 exec b in self.user_ns
3369 else:
3312 else:
3370 self.user_ns[par] = SList(block.splitlines())
3313 self.user_ns[par] = SList(block.splitlines())
3371 print "Block assigned to '%s'" % par
3314 print "Block assigned to '%s'" % par
3372
3315
3373 def magic_cpaste(self, parameter_s=''):
3316 def magic_cpaste(self, parameter_s=''):
3374 """Allows you to paste & execute a pre-formatted code block from clipboard.
3317 """Allows you to paste & execute a pre-formatted code block from clipboard.
3375
3318
3376 You must terminate the block with '--' (two minus-signs) alone on the
3319 You must terminate the block with '--' (two minus-signs) alone on the
3377 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
3320 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
3378 is the new sentinel for this operation)
3321 is the new sentinel for this operation)
3379
3322
3380 The block is dedented prior to execution to enable execution of method
3323 The block is dedented prior to execution to enable execution of method
3381 definitions. '>' and '+' characters at the beginning of a line are
3324 definitions. '>' and '+' characters at the beginning of a line are
3382 ignored, to allow pasting directly from e-mails, diff files and
3325 ignored, to allow pasting directly from e-mails, diff files and
3383 doctests (the '...' continuation prompt is also stripped). The
3326 doctests (the '...' continuation prompt is also stripped). The
3384 executed block is also assigned to variable named 'pasted_block' for
3327 executed block is also assigned to variable named 'pasted_block' for
3385 later editing with '%edit pasted_block'.
3328 later editing with '%edit pasted_block'.
3386
3329
3387 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
3330 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
3388 This assigns the pasted block to variable 'foo' as string, without
3331 This assigns the pasted block to variable 'foo' as string, without
3389 dedenting or executing it (preceding >>> and + is still stripped)
3332 dedenting or executing it (preceding >>> and + is still stripped)
3390
3333
3391 '%cpaste -r' re-executes the block previously entered by cpaste.
3334 '%cpaste -r' re-executes the block previously entered by cpaste.
3392
3335
3393 Do not be alarmed by garbled output on Windows (it's a readline bug).
3336 Do not be alarmed by garbled output on Windows (it's a readline bug).
3394 Just press enter and type -- (and press enter again) and the block
3337 Just press enter and type -- (and press enter again) and the block
3395 will be what was just pasted.
3338 will be what was just pasted.
3396
3339
3397 IPython statements (magics, shell escapes) are not supported (yet).
3340 IPython statements (magics, shell escapes) are not supported (yet).
3398
3341
3399 See also
3342 See also
3400 --------
3343 --------
3401 paste: automatically pull code from clipboard.
3344 paste: automatically pull code from clipboard.
3402 """
3345 """
3403
3346
3404 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
3347 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
3405 par = args.strip()
3348 par = args.strip()
3406 if opts.has_key('r'):
3349 if opts.has_key('r'):
3407 self._rerun_pasted()
3350 self._rerun_pasted()
3408 return
3351 return
3409
3352
3410 sentinel = opts.get('s','--')
3353 sentinel = opts.get('s','--')
3411
3354
3412 block = self._strip_pasted_lines_for_code(
3355 block = self._strip_pasted_lines_for_code(
3413 self._get_pasted_lines(sentinel))
3356 self._get_pasted_lines(sentinel))
3414
3357
3415 self._execute_block(block, par)
3358 self._execute_block(block, par)
3416
3359
3417 def magic_paste(self, parameter_s=''):
3360 def magic_paste(self, parameter_s=''):
3418 """Allows you to paste & execute a pre-formatted code block from clipboard.
3361 """Allows you to paste & execute a pre-formatted code block from clipboard.
3419
3362
3420 The text is pulled directly from the clipboard without user
3363 The text is pulled directly from the clipboard without user
3421 intervention and printed back on the screen before execution (unless
3364 intervention and printed back on the screen before execution (unless
3422 the -q flag is given to force quiet mode).
3365 the -q flag is given to force quiet mode).
3423
3366
3424 The block is dedented prior to execution to enable execution of method
3367 The block is dedented prior to execution to enable execution of method
3425 definitions. '>' and '+' characters at the beginning of a line are
3368 definitions. '>' and '+' characters at the beginning of a line are
3426 ignored, to allow pasting directly from e-mails, diff files and
3369 ignored, to allow pasting directly from e-mails, diff files and
3427 doctests (the '...' continuation prompt is also stripped). The
3370 doctests (the '...' continuation prompt is also stripped). The
3428 executed block is also assigned to variable named 'pasted_block' for
3371 executed block is also assigned to variable named 'pasted_block' for
3429 later editing with '%edit pasted_block'.
3372 later editing with '%edit pasted_block'.
3430
3373
3431 You can also pass a variable name as an argument, e.g. '%paste foo'.
3374 You can also pass a variable name as an argument, e.g. '%paste foo'.
3432 This assigns the pasted block to variable 'foo' as string, without
3375 This assigns the pasted block to variable 'foo' as string, without
3433 dedenting or executing it (preceding >>> and + is still stripped)
3376 dedenting or executing it (preceding >>> and + is still stripped)
3434
3377
3435 Options
3378 Options
3436 -------
3379 -------
3437
3380
3438 -r: re-executes the block previously entered by cpaste.
3381 -r: re-executes the block previously entered by cpaste.
3439
3382
3440 -q: quiet mode: do not echo the pasted text back to the terminal.
3383 -q: quiet mode: do not echo the pasted text back to the terminal.
3441
3384
3442 IPython statements (magics, shell escapes) are not supported (yet).
3385 IPython statements (magics, shell escapes) are not supported (yet).
3443
3386
3444 See also
3387 See also
3445 --------
3388 --------
3446 cpaste: manually paste code into terminal until you mark its end.
3389 cpaste: manually paste code into terminal until you mark its end.
3447 """
3390 """
3448 opts,args = self.parse_options(parameter_s,'rq',mode='string')
3391 opts,args = self.parse_options(parameter_s,'rq',mode='string')
3449 par = args.strip()
3392 par = args.strip()
3450 if opts.has_key('r'):
3393 if opts.has_key('r'):
3451 self._rerun_pasted()
3394 self._rerun_pasted()
3452 return
3395 return
3453
3396
3454 text = self.shell.hooks.clipboard_get()
3397 text = self.shell.hooks.clipboard_get()
3455 block = self._strip_pasted_lines_for_code(text.splitlines())
3398 block = self._strip_pasted_lines_for_code(text.splitlines())
3456
3399
3457 # By default, echo back to terminal unless quiet mode is requested
3400 # By default, echo back to terminal unless quiet mode is requested
3458 if not opts.has_key('q'):
3401 if not opts.has_key('q'):
3459 write = self.shell.write
3402 write = self.shell.write
3460 write(self.shell.pycolorize(block))
3403 write(self.shell.pycolorize(block))
3461 if not block.endswith('\n'):
3404 if not block.endswith('\n'):
3462 write('\n')
3405 write('\n')
3463 write("## -- End pasted text --\n")
3406 write("## -- End pasted text --\n")
3464
3407
3465 self._execute_block(block, par)
3408 self._execute_block(block, par)
3466
3409
3467 def magic_quickref(self,arg):
3410 def magic_quickref(self,arg):
3468 """ Show a quick reference sheet """
3411 """ Show a quick reference sheet """
3469 import IPython.core.usage
3412 import IPython.core.usage
3470 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3413 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3471
3414
3472 page(qr)
3415 page(qr)
3473
3416
3474 def magic_doctest_mode(self,parameter_s=''):
3417 def magic_doctest_mode(self,parameter_s=''):
3475 """Toggle doctest mode on and off.
3418 """Toggle doctest mode on and off.
3476
3419
3477 This mode allows you to toggle the prompt behavior between normal
3420 This mode allows you to toggle the prompt behavior between normal
3478 IPython prompts and ones that are as similar to the default IPython
3421 IPython prompts and ones that are as similar to the default IPython
3479 interpreter as possible.
3422 interpreter as possible.
3480
3423
3481 It also supports the pasting of code snippets that have leading '>>>'
3424 It also supports the pasting of code snippets that have leading '>>>'
3482 and '...' prompts in them. This means that you can paste doctests from
3425 and '...' prompts in them. This means that you can paste doctests from
3483 files or docstrings (even if they have leading whitespace), and the
3426 files or docstrings (even if they have leading whitespace), and the
3484 code will execute correctly. You can then use '%history -tn' to see
3427 code will execute correctly. You can then use '%history -tn' to see
3485 the translated history without line numbers; this will give you the
3428 the translated history without line numbers; this will give you the
3486 input after removal of all the leading prompts and whitespace, which
3429 input after removal of all the leading prompts and whitespace, which
3487 can be pasted back into an editor.
3430 can be pasted back into an editor.
3488
3431
3489 With these features, you can switch into this mode easily whenever you
3432 With these features, you can switch into this mode easily whenever you
3490 need to do testing and changes to doctests, without having to leave
3433 need to do testing and changes to doctests, without having to leave
3491 your existing IPython session.
3434 your existing IPython session.
3492 """
3435 """
3493
3436
3494 from IPython.utils.ipstruct import Struct
3437 from IPython.utils.ipstruct import Struct
3495
3438
3496 # Shorthands
3439 # Shorthands
3497 shell = self.shell
3440 shell = self.shell
3498 oc = shell.outputcache
3441 oc = shell.outputcache
3499 meta = shell.meta
3442 meta = shell.meta
3500 # dstore is a data store kept in the instance metadata bag to track any
3443 # dstore is a data store kept in the instance metadata bag to track any
3501 # changes we make, so we can undo them later.
3444 # changes we make, so we can undo them later.
3502 dstore = meta.setdefault('doctest_mode',Struct())
3445 dstore = meta.setdefault('doctest_mode',Struct())
3503 save_dstore = dstore.setdefault
3446 save_dstore = dstore.setdefault
3504
3447
3505 # save a few values we'll need to recover later
3448 # save a few values we'll need to recover later
3506 mode = save_dstore('mode',False)
3449 mode = save_dstore('mode',False)
3507 save_dstore('rc_pprint',shell.pprint)
3450 save_dstore('rc_pprint',shell.pprint)
3508 save_dstore('xmode',shell.InteractiveTB.mode)
3451 save_dstore('xmode',shell.InteractiveTB.mode)
3509 save_dstore('rc_separate_out',shell.separate_out)
3452 save_dstore('rc_separate_out',shell.separate_out)
3510 save_dstore('rc_separate_out2',shell.separate_out2)
3453 save_dstore('rc_separate_out2',shell.separate_out2)
3511 save_dstore('rc_prompts_pad_left',shell.prompts_pad_left)
3454 save_dstore('rc_prompts_pad_left',shell.prompts_pad_left)
3512 save_dstore('rc_separate_in',shell.separate_in)
3455 save_dstore('rc_separate_in',shell.separate_in)
3513
3456
3514 if mode == False:
3457 if mode == False:
3515 # turn on
3458 # turn on
3516 oc.prompt1.p_template = '>>> '
3459 oc.prompt1.p_template = '>>> '
3517 oc.prompt2.p_template = '... '
3460 oc.prompt2.p_template = '... '
3518 oc.prompt_out.p_template = ''
3461 oc.prompt_out.p_template = ''
3519
3462
3520 # Prompt separators like plain python
3463 # Prompt separators like plain python
3521 oc.input_sep = oc.prompt1.sep = ''
3464 oc.input_sep = oc.prompt1.sep = ''
3522 oc.output_sep = ''
3465 oc.output_sep = ''
3523 oc.output_sep2 = ''
3466 oc.output_sep2 = ''
3524
3467
3525 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3468 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3526 oc.prompt_out.pad_left = False
3469 oc.prompt_out.pad_left = False
3527
3470
3528 shell.pprint = False
3471 shell.pprint = False
3529
3472
3530 shell.magic_xmode('Plain')
3473 shell.magic_xmode('Plain')
3531
3474
3532 else:
3475 else:
3533 # turn off
3476 # turn off
3534 oc.prompt1.p_template = shell.prompt_in1
3477 oc.prompt1.p_template = shell.prompt_in1
3535 oc.prompt2.p_template = shell.prompt_in2
3478 oc.prompt2.p_template = shell.prompt_in2
3536 oc.prompt_out.p_template = shell.prompt_out
3479 oc.prompt_out.p_template = shell.prompt_out
3537
3480
3538 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3481 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3539
3482
3540 oc.output_sep = dstore.rc_separate_out
3483 oc.output_sep = dstore.rc_separate_out
3541 oc.output_sep2 = dstore.rc_separate_out2
3484 oc.output_sep2 = dstore.rc_separate_out2
3542
3485
3543 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3486 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3544 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3487 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3545
3488
3546 shell.pprint = dstore.rc_pprint
3489 shell.pprint = dstore.rc_pprint
3547
3490
3548 shell.magic_xmode(dstore.xmode)
3491 shell.magic_xmode(dstore.xmode)
3549
3492
3550 # Store new mode and inform
3493 # Store new mode and inform
3551 dstore.mode = bool(1-int(mode))
3494 dstore.mode = bool(1-int(mode))
3552 print 'Doctest mode is:',
3495 print 'Doctest mode is:',
3553 print ['OFF','ON'][dstore.mode]
3496 print ['OFF','ON'][dstore.mode]
3554
3497
3555 def magic_gui(self, parameter_s=''):
3498 def magic_gui(self, parameter_s=''):
3556 """Enable or disable IPython GUI event loop integration.
3499 """Enable or disable IPython GUI event loop integration.
3557
3500
3558 %gui [-a] [GUINAME]
3501 %gui [-a] [GUINAME]
3559
3502
3560 This magic replaces IPython's threaded shells that were activated
3503 This magic replaces IPython's threaded shells that were activated
3561 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3504 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3562 can now be enabled, disabled and swtiched at runtime and keyboard
3505 can now be enabled, disabled and swtiched at runtime and keyboard
3563 interrupts should work without any problems. The following toolkits
3506 interrupts should work without any problems. The following toolkits
3564 are supported: wxPython, PyQt4, PyGTK, and Tk::
3507 are supported: wxPython, PyQt4, PyGTK, and Tk::
3565
3508
3566 %gui wx # enable wxPython event loop integration
3509 %gui wx # enable wxPython event loop integration
3567 %gui qt4|qt # enable PyQt4 event loop integration
3510 %gui qt4|qt # enable PyQt4 event loop integration
3568 %gui gtk # enable PyGTK event loop integration
3511 %gui gtk # enable PyGTK event loop integration
3569 %gui tk # enable Tk event loop integration
3512 %gui tk # enable Tk event loop integration
3570 %gui # disable all event loop integration
3513 %gui # disable all event loop integration
3571
3514
3572 WARNING: after any of these has been called you can simply create
3515 WARNING: after any of these has been called you can simply create
3573 an application object, but DO NOT start the event loop yourself, as
3516 an application object, but DO NOT start the event loop yourself, as
3574 we have already handled that.
3517 we have already handled that.
3575
3518
3576 If you want us to create an appropriate application object add the
3519 If you want us to create an appropriate application object add the
3577 "-a" flag to your command::
3520 "-a" flag to your command::
3578
3521
3579 %gui -a wx
3522 %gui -a wx
3580
3523
3581 This is highly recommended for most users.
3524 This is highly recommended for most users.
3582 """
3525 """
3583 opts, arg = self.parse_options(parameter_s,'a')
3526 opts, arg = self.parse_options(parameter_s,'a')
3584 if arg=='': arg = None
3527 if arg=='': arg = None
3585 return enable_gui(arg, 'a' in opts)
3528 return enable_gui(arg, 'a' in opts)
3586
3529
3587 def magic_load_ext(self, module_str):
3530 def magic_load_ext(self, module_str):
3588 """Load an IPython extension by its module name."""
3531 """Load an IPython extension by its module name."""
3589 return self.extension_manager.load_extension(module_str)
3532 return self.extension_manager.load_extension(module_str)
3590
3533
3591 def magic_unload_ext(self, module_str):
3534 def magic_unload_ext(self, module_str):
3592 """Unload an IPython extension by its module name."""
3535 """Unload an IPython extension by its module name."""
3593 self.extension_manager.unload_extension(module_str)
3536 self.extension_manager.unload_extension(module_str)
3594
3537
3595 def magic_reload_ext(self, module_str):
3538 def magic_reload_ext(self, module_str):
3596 """Reload an IPython extension by its module name."""
3539 """Reload an IPython extension by its module name."""
3597 self.extension_manager.reload_extension(module_str)
3540 self.extension_manager.reload_extension(module_str)
3598
3541
3599 @testdec.skip_doctest
3542 @testdec.skip_doctest
3600 def magic_install_profiles(self, s):
3543 def magic_install_profiles(self, s):
3601 """Install the default IPython profiles into the .ipython dir.
3544 """Install the default IPython profiles into the .ipython dir.
3602
3545
3603 If the default profiles have already been installed, they will not
3546 If the default profiles have already been installed, they will not
3604 be overwritten. You can force overwriting them by using the ``-o``
3547 be overwritten. You can force overwriting them by using the ``-o``
3605 option::
3548 option::
3606
3549
3607 In [1]: %install_profiles -o
3550 In [1]: %install_profiles -o
3608 """
3551 """
3609 if '-o' in s:
3552 if '-o' in s:
3610 overwrite = True
3553 overwrite = True
3611 else:
3554 else:
3612 overwrite = False
3555 overwrite = False
3613 from IPython.config import profile
3556 from IPython.config import profile
3614 profile_dir = os.path.split(profile.__file__)[0]
3557 profile_dir = os.path.split(profile.__file__)[0]
3615 ipython_dir = self.ipython_dir
3558 ipython_dir = self.ipython_dir
3616 files = os.listdir(profile_dir)
3559 files = os.listdir(profile_dir)
3617
3560
3618 to_install = []
3561 to_install = []
3619 for f in files:
3562 for f in files:
3620 if f.startswith('ipython_config'):
3563 if f.startswith('ipython_config'):
3621 src = os.path.join(profile_dir, f)
3564 src = os.path.join(profile_dir, f)
3622 dst = os.path.join(ipython_dir, f)
3565 dst = os.path.join(ipython_dir, f)
3623 if (not os.path.isfile(dst)) or overwrite:
3566 if (not os.path.isfile(dst)) or overwrite:
3624 to_install.append((f, src, dst))
3567 to_install.append((f, src, dst))
3625 if len(to_install)>0:
3568 if len(to_install)>0:
3626 print "Installing profiles to: ", ipython_dir
3569 print "Installing profiles to: ", ipython_dir
3627 for (f, src, dst) in to_install:
3570 for (f, src, dst) in to_install:
3628 shutil.copy(src, dst)
3571 shutil.copy(src, dst)
3629 print " %s" % f
3572 print " %s" % f
3630
3573
3631 def magic_install_default_config(self, s):
3574 def magic_install_default_config(self, s):
3632 """Install IPython's default config file into the .ipython dir.
3575 """Install IPython's default config file into the .ipython dir.
3633
3576
3634 If the default config file (:file:`ipython_config.py`) is already
3577 If the default config file (:file:`ipython_config.py`) is already
3635 installed, it will not be overwritten. You can force overwriting
3578 installed, it will not be overwritten. You can force overwriting
3636 by using the ``-o`` option::
3579 by using the ``-o`` option::
3637
3580
3638 In [1]: %install_default_config
3581 In [1]: %install_default_config
3639 """
3582 """
3640 if '-o' in s:
3583 if '-o' in s:
3641 overwrite = True
3584 overwrite = True
3642 else:
3585 else:
3643 overwrite = False
3586 overwrite = False
3644 from IPython.config import default
3587 from IPython.config import default
3645 config_dir = os.path.split(default.__file__)[0]
3588 config_dir = os.path.split(default.__file__)[0]
3646 ipython_dir = self.ipython_dir
3589 ipython_dir = self.ipython_dir
3647 default_config_file_name = 'ipython_config.py'
3590 default_config_file_name = 'ipython_config.py'
3648 src = os.path.join(config_dir, default_config_file_name)
3591 src = os.path.join(config_dir, default_config_file_name)
3649 dst = os.path.join(ipython_dir, default_config_file_name)
3592 dst = os.path.join(ipython_dir, default_config_file_name)
3650 if (not os.path.isfile(dst)) or overwrite:
3593 if (not os.path.isfile(dst)) or overwrite:
3651 shutil.copy(src, dst)
3594 shutil.copy(src, dst)
3652 print "Installing default config file: %s" % dst
3595 print "Installing default config file: %s" % dst
3653
3596
3654 # Pylab support: simple wrappers that activate pylab, load gui input
3597 # Pylab support: simple wrappers that activate pylab, load gui input
3655 # handling and modify slightly %run
3598 # handling and modify slightly %run
3656
3599
3657 @testdec.skip_doctest
3600 @testdec.skip_doctest
3658 def _pylab_magic_run(self, parameter_s=''):
3601 def _pylab_magic_run(self, parameter_s=''):
3659 Magic.magic_run(self, parameter_s,
3602 Magic.magic_run(self, parameter_s,
3660 runner=mpl_runner(self.shell.safe_execfile))
3603 runner=mpl_runner(self.shell.safe_execfile))
3661
3604
3662 _pylab_magic_run.__doc__ = magic_run.__doc__
3605 _pylab_magic_run.__doc__ = magic_run.__doc__
3663
3606
3664 @testdec.skip_doctest
3607 @testdec.skip_doctest
3665 def magic_pylab(self, s):
3608 def magic_pylab(self, s):
3666 """Load numpy and matplotlib to work interactively.
3609 """Load numpy and matplotlib to work interactively.
3667
3610
3668 %pylab [GUINAME]
3611 %pylab [GUINAME]
3669
3612
3670 This function lets you activate pylab (matplotlib, numpy and
3613 This function lets you activate pylab (matplotlib, numpy and
3671 interactive support) at any point during an IPython session.
3614 interactive support) at any point during an IPython session.
3672
3615
3673 It will import at the top level numpy as np, pyplot as plt, matplotlib,
3616 It will import at the top level numpy as np, pyplot as plt, matplotlib,
3674 pylab and mlab, as well as all names from numpy and pylab.
3617 pylab and mlab, as well as all names from numpy and pylab.
3675
3618
3676 Parameters
3619 Parameters
3677 ----------
3620 ----------
3678 guiname : optional
3621 guiname : optional
3679 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk' or
3622 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk' or
3680 'tk'). If given, the corresponding Matplotlib backend is used,
3623 'tk'). If given, the corresponding Matplotlib backend is used,
3681 otherwise matplotlib's default (which you can override in your
3624 otherwise matplotlib's default (which you can override in your
3682 matplotlib config file) is used.
3625 matplotlib config file) is used.
3683
3626
3684 Examples
3627 Examples
3685 --------
3628 --------
3686 In this case, where the MPL default is TkAgg:
3629 In this case, where the MPL default is TkAgg:
3687 In [2]: %pylab
3630 In [2]: %pylab
3688
3631
3689 Welcome to pylab, a matplotlib-based Python environment.
3632 Welcome to pylab, a matplotlib-based Python environment.
3690 Backend in use: TkAgg
3633 Backend in use: TkAgg
3691 For more information, type 'help(pylab)'.
3634 For more information, type 'help(pylab)'.
3692
3635
3693 But you can explicitly request a different backend:
3636 But you can explicitly request a different backend:
3694 In [3]: %pylab qt
3637 In [3]: %pylab qt
3695
3638
3696 Welcome to pylab, a matplotlib-based Python environment.
3639 Welcome to pylab, a matplotlib-based Python environment.
3697 Backend in use: Qt4Agg
3640 Backend in use: Qt4Agg
3698 For more information, type 'help(pylab)'.
3641 For more information, type 'help(pylab)'.
3699 """
3642 """
3700 self.shell.enable_pylab(s)
3643 self.shell.enable_pylab(s)
3701
3644
3702 def magic_tb(self, s):
3645 def magic_tb(self, s):
3703 """Print the last traceback with the currently active exception mode.
3646 """Print the last traceback with the currently active exception mode.
3704
3647
3705 See %xmode for changing exception reporting modes."""
3648 See %xmode for changing exception reporting modes."""
3706 self.shell.showtraceback()
3649 self.shell.showtraceback()
3707
3650
3708 # end Magic
3651 # end Magic
@@ -1,62 +1,59 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3
3
4 def test_import_completer():
4 def test_import_completer():
5 from IPython.core import completer
5 from IPython.core import completer
6
6
7 def test_import_crashhandler():
7 def test_import_crashhandler():
8 from IPython.core import crashhandler
8 from IPython.core import crashhandler
9
9
10 def test_import_debugger():
10 def test_import_debugger():
11 from IPython.core import debugger
11 from IPython.core import debugger
12
12
13 def test_import_fakemodule():
13 def test_import_fakemodule():
14 from IPython.core import fakemodule
14 from IPython.core import fakemodule
15
15
16 def test_import_excolors():
16 def test_import_excolors():
17 from IPython.core import excolors
17 from IPython.core import excolors
18
18
19 def test_import_history():
19 def test_import_history():
20 from IPython.core import history
20 from IPython.core import history
21
21
22 def test_import_hooks():
22 def test_import_hooks():
23 from IPython.core import hooks
23 from IPython.core import hooks
24
24
25 def test_import_ipapi():
25 def test_import_ipapi():
26 from IPython.core import ipapi
26 from IPython.core import ipapi
27
27
28 def test_import_interactiveshell():
28 def test_import_interactiveshell():
29 from IPython.core import interactiveshell
29 from IPython.core import interactiveshell
30
30
31 def test_import_logger():
31 def test_import_logger():
32 from IPython.core import logger
32 from IPython.core import logger
33
33
34 def test_import_macro():
34 def test_import_macro():
35 from IPython.core import macro
35 from IPython.core import macro
36
36
37 def test_import_magic():
37 def test_import_magic():
38 from IPython.core import magic
38 from IPython.core import magic
39
39
40 def test_import_oinspect():
40 def test_import_oinspect():
41 from IPython.core import oinspect
41 from IPython.core import oinspect
42
42
43 def test_import_outputtrap():
44 from IPython.core import outputtrap
45
46 def test_import_prefilter():
43 def test_import_prefilter():
47 from IPython.core import prefilter
44 from IPython.core import prefilter
48
45
49 def test_import_prompts():
46 def test_import_prompts():
50 from IPython.core import prompts
47 from IPython.core import prompts
51
48
52 def test_import_release():
49 def test_import_release():
53 from IPython.core import release
50 from IPython.core import release
54
51
55 def test_import_shadowns():
52 def test_import_shadowns():
56 from IPython.core import shadowns
53 from IPython.core import shadowns
57
54
58 def test_import_ultratb():
55 def test_import_ultratb():
59 from IPython.core import ultratb
56 from IPython.core import ultratb
60
57
61 def test_import_usage():
58 def test_import_usage():
62 from IPython.core import usage
59 from IPython.core import usage
1 NO CONTENT: file renamed from IPython/core/outputtrap.py to IPython/deathrow/outputtrap.py
NO CONTENT: file renamed from IPython/core/outputtrap.py to IPython/deathrow/outputtrap.py
@@ -1,275 +1,272 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 An embedded IPython shell.
4 An embedded IPython shell.
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 * Fernando Perez
9 * Fernando Perez
10
10
11 Notes
11 Notes
12 -----
12 -----
13 """
13 """
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Copyright (C) 2008-2009 The IPython Development Team
16 # Copyright (C) 2008-2009 The IPython Development Team
17 #
17 #
18 # Distributed under the terms of the BSD License. The full license is in
18 # Distributed under the terms of the BSD License. The full license is in
19 # the file COPYING, distributed as part of this software.
19 # the file COPYING, distributed as part of this software.
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Imports
23 # Imports
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 from __future__ import with_statement
26 from __future__ import with_statement
27 import __main__
27 import __main__
28
28
29 import sys
29 import sys
30 from contextlib import nested
30 from contextlib import nested
31
31
32 from IPython.core import ultratb
32 from IPython.core import ultratb
33 from IPython.core.interactiveshell import InteractiveShell
33 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
34 from IPython.frontend.terminal.ipapp import load_default_config
34 from IPython.frontend.terminal.ipapp import load_default_config
35
35
36 from IPython.utils.traitlets import Bool, Str, CBool
36 from IPython.utils.traitlets import Bool, Str, CBool
37 from IPython.utils.io import ask_yes_no
37 from IPython.utils.io import ask_yes_no
38
38
39
39
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # Classes and functions
41 # Classes and functions
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43
43
44 # This is an additional magic that is exposed in embedded shells.
44 # This is an additional magic that is exposed in embedded shells.
45 def kill_embedded(self,parameter_s=''):
45 def kill_embedded(self,parameter_s=''):
46 """%kill_embedded : deactivate for good the current embedded IPython.
46 """%kill_embedded : deactivate for good the current embedded IPython.
47
47
48 This function (after asking for confirmation) sets an internal flag so that
48 This function (after asking for confirmation) sets an internal flag so that
49 an embedded IPython will never activate again. This is useful to
49 an embedded IPython will never activate again. This is useful to
50 permanently disable a shell that is being called inside a loop: once you've
50 permanently disable a shell that is being called inside a loop: once you've
51 figured out what you needed from it, you may then kill it and the program
51 figured out what you needed from it, you may then kill it and the program
52 will then continue to run without the interactive shell interfering again.
52 will then continue to run without the interactive shell interfering again.
53 """
53 """
54
54
55 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
55 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
56 "(y/n)? [y/N] ",'n')
56 "(y/n)? [y/N] ",'n')
57 if kill:
57 if kill:
58 self.embedded_active = False
58 self.embedded_active = False
59 print "This embedded IPython will not reactivate anymore once you exit."
59 print "This embedded IPython will not reactivate anymore once you exit."
60
60
61
61
62 class InteractiveShellEmbed(InteractiveShell):
62 class InteractiveShellEmbed(TerminalInteractiveShell):
63
63
64 dummy_mode = Bool(False)
64 dummy_mode = Bool(False)
65 exit_msg = Str('')
65 exit_msg = Str('')
66 embedded = CBool(True)
66 embedded = CBool(True)
67 embedded_active = CBool(True)
67 embedded_active = CBool(True)
68 # Like the base class display_banner is not configurable, but here it
68 # Like the base class display_banner is not configurable, but here it
69 # is True by default.
69 # is True by default.
70 display_banner = CBool(True)
70 display_banner = CBool(True)
71
71
72 def __init__(self, parent=None, config=None, ipython_dir=None, usage=None,
72 def __init__(self, config=None, ipython_dir=None, user_ns=None,
73 user_ns=None, user_global_ns=None,
73 user_global_ns=None, custom_exceptions=((),None),
74 banner1=None, banner2=None, display_banner=None,
74 usage=None, banner1=None, banner2=None,
75 custom_exceptions=((),None), exit_msg=''):
75 display_banner=None, exit_msg=u''):
76
76
77 self.save_sys_ipcompleter()
77 self.save_sys_ipcompleter()
78
78
79 super(InteractiveShellEmbed,self).__init__(
79 super(InteractiveShellEmbed,self).__init__(
80 parent=parent, config=config, ipython_dir=ipython_dir, usage=usage,
80 config=config, ipython_dir=ipython_dir, user_ns=user_ns,
81 user_ns=user_ns, user_global_ns=user_global_ns,
81 user_global_ns=user_global_ns, custom_exceptions=custom_exceptions,
82 banner1=banner1, banner2=banner2, display_banner=display_banner,
82 usage=usage, banner1=banner1, banner2=banner2,
83 custom_exceptions=custom_exceptions)
83 display_banner=display_banner
84 )
84
85
85 self.exit_msg = exit_msg
86 self.exit_msg = exit_msg
86 self.define_magic("kill_embedded", kill_embedded)
87 self.define_magic("kill_embedded", kill_embedded)
87
88
88 # don't use the ipython crash handler so that user exceptions aren't
89 # don't use the ipython crash handler so that user exceptions aren't
89 # trapped
90 # trapped
90 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
91 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
91 mode=self.xmode,
92 mode=self.xmode,
92 call_pdb=self.pdb)
93 call_pdb=self.pdb)
93
94
94 self.restore_sys_ipcompleter()
95 self.restore_sys_ipcompleter()
95
96
96 def init_sys_modules(self):
97 def init_sys_modules(self):
97 pass
98 pass
98
99
99 def save_sys_ipcompleter(self):
100 def save_sys_ipcompleter(self):
100 """Save readline completer status."""
101 """Save readline completer status."""
101 try:
102 try:
102 #print 'Save completer',sys.ipcompleter # dbg
103 #print 'Save completer',sys.ipcompleter # dbg
103 self.sys_ipcompleter_orig = sys.ipcompleter
104 self.sys_ipcompleter_orig = sys.ipcompleter
104 except:
105 except:
105 pass # not nested with IPython
106 pass # not nested with IPython
106
107
107 def restore_sys_ipcompleter(self):
108 def restore_sys_ipcompleter(self):
108 """Restores the readline completer which was in place.
109 """Restores the readline completer which was in place.
109
110
110 This allows embedded IPython within IPython not to disrupt the
111 This allows embedded IPython within IPython not to disrupt the
111 parent's completion.
112 parent's completion.
112 """
113 """
113 try:
114 try:
114 self.readline.set_completer(self.sys_ipcompleter_orig)
115 self.readline.set_completer(self.sys_ipcompleter_orig)
115 sys.ipcompleter = self.sys_ipcompleter_orig
116 sys.ipcompleter = self.sys_ipcompleter_orig
116 except:
117 except:
117 pass
118 pass
118
119
119 def __call__(self, header='', local_ns=None, global_ns=None, dummy=None,
120 def __call__(self, header='', local_ns=None, global_ns=None, dummy=None,
120 stack_depth=1):
121 stack_depth=1):
121 """Activate the interactive interpreter.
122 """Activate the interactive interpreter.
122
123
123 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
124 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
124 the interpreter shell with the given local and global namespaces, and
125 the interpreter shell with the given local and global namespaces, and
125 optionally print a header string at startup.
126 optionally print a header string at startup.
126
127
127 The shell can be globally activated/deactivated using the
128 The shell can be globally activated/deactivated using the
128 set/get_dummy_mode methods. This allows you to turn off a shell used
129 set/get_dummy_mode methods. This allows you to turn off a shell used
129 for debugging globally.
130 for debugging globally.
130
131
131 However, *each* time you call the shell you can override the current
132 However, *each* time you call the shell you can override the current
132 state of dummy_mode with the optional keyword parameter 'dummy'. For
133 state of dummy_mode with the optional keyword parameter 'dummy'. For
133 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
134 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
134 can still have a specific call work by making it as IPShell(dummy=0).
135 can still have a specific call work by making it as IPShell(dummy=0).
135
136
136 The optional keyword parameter dummy controls whether the call
137 The optional keyword parameter dummy controls whether the call
137 actually does anything.
138 actually does anything.
138 """
139 """
139
140
140 # If the user has turned it off, go away
141 # If the user has turned it off, go away
141 if not self.embedded_active:
142 if not self.embedded_active:
142 return
143 return
143
144
144 # Normal exits from interactive mode set this flag, so the shell can't
145 # Normal exits from interactive mode set this flag, so the shell can't
145 # re-enter (it checks this variable at the start of interactive mode).
146 # re-enter (it checks this variable at the start of interactive mode).
146 self.exit_now = False
147 self.exit_now = False
147
148
148 # Allow the dummy parameter to override the global __dummy_mode
149 # Allow the dummy parameter to override the global __dummy_mode
149 if dummy or (dummy != 0 and self.dummy_mode):
150 if dummy or (dummy != 0 and self.dummy_mode):
150 return
151 return
151
152
152 if self.has_readline:
153 if self.has_readline:
153 self.set_completer()
154 self.set_completer()
154
155
155 # self.banner is auto computed
156 # self.banner is auto computed
156 if header:
157 if header:
157 self.old_banner2 = self.banner2
158 self.old_banner2 = self.banner2
158 self.banner2 = self.banner2 + '\n' + header + '\n'
159 self.banner2 = self.banner2 + '\n' + header + '\n'
159 else:
160 else:
160 self.old_banner2 = ''
161 self.old_banner2 = ''
161
162
162 # Call the embedding code with a stack depth of 1 so it can skip over
163 # Call the embedding code with a stack depth of 1 so it can skip over
163 # our call and get the original caller's namespaces.
164 # our call and get the original caller's namespaces.
164 self.mainloop(local_ns, global_ns, stack_depth=stack_depth)
165 self.mainloop(local_ns, global_ns, stack_depth=stack_depth)
165
166
166 self.banner2 = self.old_banner2
167 self.banner2 = self.old_banner2
167
168
168 if self.exit_msg is not None:
169 if self.exit_msg is not None:
169 print self.exit_msg
170 print self.exit_msg
170
171
171 self.restore_sys_ipcompleter()
172 self.restore_sys_ipcompleter()
172
173
173 def mainloop(self, local_ns=None, global_ns=None, stack_depth=0,
174 def mainloop(self, local_ns=None, global_ns=None, stack_depth=0,
174 display_banner=None):
175 display_banner=None):
175 """Embeds IPython into a running python program.
176 """Embeds IPython into a running python program.
176
177
177 Input:
178 Input:
178
179
179 - header: An optional header message can be specified.
180 - header: An optional header message can be specified.
180
181
181 - local_ns, global_ns: working namespaces. If given as None, the
182 - local_ns, global_ns: working namespaces. If given as None, the
182 IPython-initialized one is updated with __main__.__dict__, so that
183 IPython-initialized one is updated with __main__.__dict__, so that
183 program variables become visible but user-specific configuration
184 program variables become visible but user-specific configuration
184 remains possible.
185 remains possible.
185
186
186 - stack_depth: specifies how many levels in the stack to go to
187 - stack_depth: specifies how many levels in the stack to go to
187 looking for namespaces (when local_ns and global_ns are None). This
188 looking for namespaces (when local_ns and global_ns are None). This
188 allows an intermediate caller to make sure that this function gets
189 allows an intermediate caller to make sure that this function gets
189 the namespace from the intended level in the stack. By default (0)
190 the namespace from the intended level in the stack. By default (0)
190 it will get its locals and globals from the immediate caller.
191 it will get its locals and globals from the immediate caller.
191
192
192 Warning: it's possible to use this in a program which is being run by
193 Warning: it's possible to use this in a program which is being run by
193 IPython itself (via %run), but some funny things will happen (a few
194 IPython itself (via %run), but some funny things will happen (a few
194 globals get overwritten). In the future this will be cleaned up, as
195 globals get overwritten). In the future this will be cleaned up, as
195 there is no fundamental reason why it can't work perfectly."""
196 there is no fundamental reason why it can't work perfectly."""
196
197
197 # Get locals and globals from caller
198 # Get locals and globals from caller
198 if local_ns is None or global_ns is None:
199 if local_ns is None or global_ns is None:
199 call_frame = sys._getframe(stack_depth).f_back
200 call_frame = sys._getframe(stack_depth).f_back
200
201
201 if local_ns is None:
202 if local_ns is None:
202 local_ns = call_frame.f_locals
203 local_ns = call_frame.f_locals
203 if global_ns is None:
204 if global_ns is None:
204 global_ns = call_frame.f_globals
205 global_ns = call_frame.f_globals
205
206
206 # Update namespaces and fire up interpreter
207 # Update namespaces and fire up interpreter
207
208
208 # The global one is easy, we can just throw it in
209 # The global one is easy, we can just throw it in
209 self.user_global_ns = global_ns
210 self.user_global_ns = global_ns
210
211
211 # but the user/local one is tricky: ipython needs it to store internal
212 # but the user/local one is tricky: ipython needs it to store internal
212 # data, but we also need the locals. We'll copy locals in the user
213 # data, but we also need the locals. We'll copy locals in the user
213 # one, but will track what got copied so we can delete them at exit.
214 # one, but will track what got copied so we can delete them at exit.
214 # This is so that a later embedded call doesn't see locals from a
215 # This is so that a later embedded call doesn't see locals from a
215 # previous call (which most likely existed in a separate scope).
216 # previous call (which most likely existed in a separate scope).
216 local_varnames = local_ns.keys()
217 local_varnames = local_ns.keys()
217 self.user_ns.update(local_ns)
218 self.user_ns.update(local_ns)
218 #self.user_ns['local_ns'] = local_ns # dbg
219 #self.user_ns['local_ns'] = local_ns # dbg
219
220
220 # Patch for global embedding to make sure that things don't overwrite
221 # Patch for global embedding to make sure that things don't overwrite
221 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
222 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
222 # FIXME. Test this a bit more carefully (the if.. is new)
223 # FIXME. Test this a bit more carefully (the if.. is new)
223 if local_ns is None and global_ns is None:
224 if local_ns is None and global_ns is None:
224 self.user_global_ns.update(__main__.__dict__)
225 self.user_global_ns.update(__main__.__dict__)
225
226
226 # make sure the tab-completer has the correct frame information, so it
227 # make sure the tab-completer has the correct frame information, so it
227 # actually completes using the frame's locals/globals
228 # actually completes using the frame's locals/globals
228 self.set_completer_frame()
229 self.set_completer_frame()
229
230
230 with nested(self.builtin_trap, self.display_trap):
231 with nested(self.builtin_trap, self.display_trap):
231 self.interact(display_banner=display_banner)
232 self.interact(display_banner=display_banner)
232
233
233 # now, purge out the user namespace from anything we might have added
234 # now, purge out the user namespace from anything we might have added
234 # from the caller's local namespace
235 # from the caller's local namespace
235 delvar = self.user_ns.pop
236 delvar = self.user_ns.pop
236 for var in local_varnames:
237 for var in local_varnames:
237 delvar(var,None)
238 delvar(var,None)
238
239
239
240
240 _embedded_shell = None
241 _embedded_shell = None
241
242
242
243
243 def embed(header='', config=None, usage=None, banner1=None, banner2=None,
244 def embed(**kwargs):
244 display_banner=True, exit_msg=''):
245 """Call this to embed IPython at the current point in your program.
245 """Call this to embed IPython at the current point in your program.
246
246
247 The first invocation of this will create an :class:`InteractiveShellEmbed`
247 The first invocation of this will create an :class:`InteractiveShellEmbed`
248 instance and then call it. Consecutive calls just call the already
248 instance and then call it. Consecutive calls just call the already
249 created instance.
249 created instance.
250
250
251 Here is a simple example::
251 Here is a simple example::
252
252
253 from IPython import embed
253 from IPython import embed
254 a = 10
254 a = 10
255 b = 20
255 b = 20
256 embed('First time')
256 embed('First time')
257 c = 30
257 c = 30
258 d = 40
258 d = 40
259 embed
259 embed
260
260
261 Full customization can be done by passing a :class:`Struct` in as the
261 Full customization can be done by passing a :class:`Struct` in as the
262 config argument.
262 config argument.
263 """
263 """
264 config = kwargs.get('config')
265 header = kwargs.pop('header', u'')
264 if config is None:
266 if config is None:
265 config = load_default_config()
267 config = load_default_config()
266 config.InteractiveShellEmbed = config.InteractiveShell
268 config.InteractiveShellEmbed = config.TerminalInteractiveShell
267 global _embedded_shell
269 global _embedded_shell
268 if _embedded_shell is None:
270 if _embedded_shell is None:
269 _embedded_shell = InteractiveShellEmbed(
271 _embedded_shell = InteractiveShellEmbed(**kwargs)
270 config=config, usage=usage,
271 banner1=banner1, banner2=banner2,
272 display_banner=display_banner, exit_msg=exit_msg
273 )
274 _embedded_shell(header=header, stack_depth=2)
272 _embedded_shell(header=header, stack_depth=2)
275
@@ -1,665 +1,665 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 The :class:`~IPython.core.application.Application` object for the command
4 The :class:`~IPython.core.application.Application` object for the command
5 line :command:`ipython` program.
5 line :command:`ipython` program.
6
6
7 Authors
7 Authors
8 -------
8 -------
9
9
10 * Brian Granger
10 * Brian Granger
11 * Fernando Perez
11 * Fernando Perez
12 """
12 """
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Copyright (C) 2008-2010 The IPython Development Team
15 # Copyright (C) 2008-2010 The IPython Development Team
16 #
16 #
17 # Distributed under the terms of the BSD License. The full license is in
17 # Distributed under the terms of the BSD License. The full license is in
18 # the file COPYING, distributed as part of this software.
18 # the file COPYING, distributed as part of this software.
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Imports
22 # Imports
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 from __future__ import absolute_import
25 from __future__ import absolute_import
26
26
27 import logging
27 import logging
28 import os
28 import os
29 import sys
29 import sys
30
30
31 from IPython.core import release
31 from IPython.core import release
32 from IPython.core.crashhandler import CrashHandler
32 from IPython.core.crashhandler import CrashHandler
33 from IPython.core.application import Application, BaseAppConfigLoader
33 from IPython.core.application import Application, BaseAppConfigLoader
34 from IPython.core.interactiveshell import InteractiveShell
34 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
35 from IPython.config.loader import (
35 from IPython.config.loader import (
36 Config,
36 Config,
37 PyFileConfigLoader
37 PyFileConfigLoader
38 )
38 )
39 from IPython.lib import inputhook
39 from IPython.lib import inputhook
40 from IPython.utils.path import filefind, get_ipython_dir
40 from IPython.utils.path import filefind, get_ipython_dir
41 from IPython.core import usage
41 from IPython.core import usage
42
42
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44 # Globals, utilities and helpers
44 # Globals, utilities and helpers
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46
46
47 #: The default config file name for this application.
47 #: The default config file name for this application.
48 default_config_file_name = u'ipython_config.py'
48 default_config_file_name = u'ipython_config.py'
49
49
50
50
51 class IPAppConfigLoader(BaseAppConfigLoader):
51 class IPAppConfigLoader(BaseAppConfigLoader):
52
52
53 def _add_arguments(self):
53 def _add_arguments(self):
54 super(IPAppConfigLoader, self)._add_arguments()
54 super(IPAppConfigLoader, self)._add_arguments()
55 paa = self.parser.add_argument
55 paa = self.parser.add_argument
56 paa('-p',
56 paa('-p',
57 '--profile', dest='Global.profile', type=unicode,
57 '--profile', dest='Global.profile', type=unicode,
58 help=
58 help=
59 """The string name of the ipython profile to be used. Assume that your
59 """The string name of the ipython profile to be used. Assume that your
60 config file is ipython_config-<name>.py (looks in current dir first,
60 config file is ipython_config-<name>.py (looks in current dir first,
61 then in IPYTHON_DIR). This is a quick way to keep and load multiple
61 then in IPYTHON_DIR). This is a quick way to keep and load multiple
62 config files for different tasks, especially if include your basic one
62 config files for different tasks, especially if include your basic one
63 in your more specialized ones. You can keep a basic
63 in your more specialized ones. You can keep a basic
64 IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which
64 IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which
65 include this one and load extra things for particular tasks.""",
65 include this one and load extra things for particular tasks.""",
66 metavar='Global.profile')
66 metavar='Global.profile')
67 paa('--config-file',
67 paa('--config-file',
68 dest='Global.config_file', type=unicode,
68 dest='Global.config_file', type=unicode,
69 help=
69 help=
70 """Set the config file name to override default. Normally IPython
70 """Set the config file name to override default. Normally IPython
71 loads ipython_config.py (from current directory) or
71 loads ipython_config.py (from current directory) or
72 IPYTHON_DIR/ipython_config.py. If the loading of your config file
72 IPYTHON_DIR/ipython_config.py. If the loading of your config file
73 fails, IPython starts with a bare bones configuration (no modules
73 fails, IPython starts with a bare bones configuration (no modules
74 loaded at all).""",
74 loaded at all).""",
75 metavar='Global.config_file')
75 metavar='Global.config_file')
76 paa('--autocall',
76 paa('--autocall',
77 dest='InteractiveShell.autocall', type=int,
77 dest='InteractiveShell.autocall', type=int,
78 help=
78 help=
79 """Make IPython automatically call any callable object even if you
79 """Make IPython automatically call any callable object even if you
80 didn't type explicit parentheses. For example, 'str 43' becomes
80 didn't type explicit parentheses. For example, 'str 43' becomes
81 'str(43)' automatically. The value can be '0' to disable the feature,
81 'str(43)' automatically. The value can be '0' to disable the feature,
82 '1' for 'smart' autocall, where it is not applied if there are no more
82 '1' for 'smart' autocall, where it is not applied if there are no more
83 arguments on the line, and '2' for 'full' autocall, where all callable
83 arguments on the line, and '2' for 'full' autocall, where all callable
84 objects are automatically called (even if no arguments are present).
84 objects are automatically called (even if no arguments are present).
85 The default is '1'.""",
85 The default is '1'.""",
86 metavar='InteractiveShell.autocall')
86 metavar='InteractiveShell.autocall')
87 paa('--autoindent',
87 paa('--autoindent',
88 action='store_true', dest='InteractiveShell.autoindent',
88 action='store_true', dest='TerminalInteractiveShell.autoindent',
89 help='Turn on autoindenting.')
89 help='Turn on autoindenting.')
90 paa('--no-autoindent',
90 paa('--no-autoindent',
91 action='store_false', dest='InteractiveShell.autoindent',
91 action='store_false', dest='TerminalInteractiveShell.autoindent',
92 help='Turn off autoindenting.')
92 help='Turn off autoindenting.')
93 paa('--automagic',
93 paa('--automagic',
94 action='store_true', dest='InteractiveShell.automagic',
94 action='store_true', dest='InteractiveShell.automagic',
95 help=
95 help=
96 """Turn on the auto calling of magic commands. Type %%magic at the
96 """Turn on the auto calling of magic commands. Type %%magic at the
97 IPython prompt for more information.""")
97 IPython prompt for more information.""")
98 paa('--no-automagic',
98 paa('--no-automagic',
99 action='store_false', dest='InteractiveShell.automagic',
99 action='store_false', dest='InteractiveShell.automagic',
100 help='Turn off the auto calling of magic commands.')
100 help='Turn off the auto calling of magic commands.')
101 paa('--autoedit-syntax',
101 paa('--autoedit-syntax',
102 action='store_true', dest='InteractiveShell.autoedit_syntax',
102 action='store_true', dest='TerminalInteractiveShell.autoedit_syntax',
103 help='Turn on auto editing of files with syntax errors.')
103 help='Turn on auto editing of files with syntax errors.')
104 paa('--no-autoedit-syntax',
104 paa('--no-autoedit-syntax',
105 action='store_false', dest='InteractiveShell.autoedit_syntax',
105 action='store_false', dest='TerminalInteractiveShell.autoedit_syntax',
106 help='Turn off auto editing of files with syntax errors.')
106 help='Turn off auto editing of files with syntax errors.')
107 paa('--banner',
107 paa('--banner',
108 action='store_true', dest='Global.display_banner',
108 action='store_true', dest='Global.display_banner',
109 help='Display a banner upon starting IPython.')
109 help='Display a banner upon starting IPython.')
110 paa('--no-banner',
110 paa('--no-banner',
111 action='store_false', dest='Global.display_banner',
111 action='store_false', dest='Global.display_banner',
112 help="Don't display a banner upon starting IPython.")
112 help="Don't display a banner upon starting IPython.")
113 paa('--cache-size',
113 paa('--cache-size',
114 type=int, dest='InteractiveShell.cache_size',
114 type=int, dest='InteractiveShell.cache_size',
115 help=
115 help=
116 """Set the size of the output cache. The default is 1000, you can
116 """Set the size of the output cache. The default is 1000, you can
117 change it permanently in your config file. Setting it to 0 completely
117 change it permanently in your config file. Setting it to 0 completely
118 disables the caching system, and the minimum value accepted is 20 (if
118 disables the caching system, and the minimum value accepted is 20 (if
119 you provide a value less than 20, it is reset to 0 and a warning is
119 you provide a value less than 20, it is reset to 0 and a warning is
120 issued). This limit is defined because otherwise you'll spend more
120 issued). This limit is defined because otherwise you'll spend more
121 time re-flushing a too small cache than working""",
121 time re-flushing a too small cache than working""",
122 metavar='InteractiveShell.cache_size')
122 metavar='InteractiveShell.cache_size')
123 paa('--classic',
123 paa('--classic',
124 action='store_true', dest='Global.classic',
124 action='store_true', dest='Global.classic',
125 help="Gives IPython a similar feel to the classic Python prompt.")
125 help="Gives IPython a similar feel to the classic Python prompt.")
126 paa('--colors',
126 paa('--colors',
127 type=str, dest='InteractiveShell.colors',
127 type=str, dest='InteractiveShell.colors',
128 help="Set the color scheme (NoColor, Linux, and LightBG).",
128 help="Set the color scheme (NoColor, Linux, and LightBG).",
129 metavar='InteractiveShell.colors')
129 metavar='InteractiveShell.colors')
130 paa('--color-info',
130 paa('--color-info',
131 action='store_true', dest='InteractiveShell.color_info',
131 action='store_true', dest='InteractiveShell.color_info',
132 help=
132 help=
133 """IPython can display information about objects via a set of func-
133 """IPython can display information about objects via a set of func-
134 tions, and optionally can use colors for this, syntax highlighting
134 tions, and optionally can use colors for this, syntax highlighting
135 source code and various other elements. However, because this
135 source code and various other elements. However, because this
136 information is passed through a pager (like 'less') and many pagers get
136 information is passed through a pager (like 'less') and many pagers get
137 confused with color codes, this option is off by default. You can test
137 confused with color codes, this option is off by default. You can test
138 it and turn it on permanently in your ipython_config.py file if it
138 it and turn it on permanently in your ipython_config.py file if it
139 works for you. Test it and turn it on permanently if it works with
139 works for you. Test it and turn it on permanently if it works with
140 your system. The magic function %%color_info allows you to toggle this
140 your system. The magic function %%color_info allows you to toggle this
141 inter- actively for testing.""")
141 inter- actively for testing.""")
142 paa('--no-color-info',
142 paa('--no-color-info',
143 action='store_false', dest='InteractiveShell.color_info',
143 action='store_false', dest='InteractiveShell.color_info',
144 help="Disable using colors for info related things.")
144 help="Disable using colors for info related things.")
145 paa('--confirm-exit',
145 paa('--confirm-exit',
146 action='store_true', dest='InteractiveShell.confirm_exit',
146 action='store_true', dest='TerminalInteractiveShell.confirm_exit',
147 help=
147 help=
148 """Set to confirm when you try to exit IPython with an EOF (Control-D
148 """Set to confirm when you try to exit IPython with an EOF (Control-D
149 in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or
149 in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or
150 '%%Exit', you can force a direct exit without any confirmation.""")
150 '%%Exit', you can force a direct exit without any confirmation.""")
151 paa('--no-confirm-exit',
151 paa('--no-confirm-exit',
152 action='store_false', dest='InteractiveShell.confirm_exit',
152 action='store_false', dest='TerminalInteractiveShell.confirm_exit',
153 help="Don't prompt the user when exiting.")
153 help="Don't prompt the user when exiting.")
154 paa('--deep-reload',
154 paa('--deep-reload',
155 action='store_true', dest='InteractiveShell.deep_reload',
155 action='store_true', dest='InteractiveShell.deep_reload',
156 help=
156 help=
157 """Enable deep (recursive) reloading by default. IPython can use the
157 """Enable deep (recursive) reloading by default. IPython can use the
158 deep_reload module which reloads changes in modules recursively (it
158 deep_reload module which reloads changes in modules recursively (it
159 replaces the reload() function, so you don't need to change anything to
159 replaces the reload() function, so you don't need to change anything to
160 use it). deep_reload() forces a full reload of modules whose code may
160 use it). deep_reload() forces a full reload of modules whose code may
161 have changed, which the default reload() function does not. When
161 have changed, which the default reload() function does not. When
162 deep_reload is off, IPython will use the normal reload(), but
162 deep_reload is off, IPython will use the normal reload(), but
163 deep_reload will still be available as dreload(). This fea- ture is off
163 deep_reload will still be available as dreload(). This fea- ture is off
164 by default [which means that you have both normal reload() and
164 by default [which means that you have both normal reload() and
165 dreload()].""")
165 dreload()].""")
166 paa('--no-deep-reload',
166 paa('--no-deep-reload',
167 action='store_false', dest='InteractiveShell.deep_reload',
167 action='store_false', dest='InteractiveShell.deep_reload',
168 help="Disable deep (recursive) reloading by default.")
168 help="Disable deep (recursive) reloading by default.")
169 paa('--editor',
169 paa('--editor',
170 type=str, dest='InteractiveShell.editor',
170 type=str, dest='TerminalInteractiveShell.editor',
171 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
171 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
172 metavar='InteractiveShell.editor')
172 metavar='TerminalInteractiveShell.editor')
173 paa('--log','-l',
173 paa('--log','-l',
174 action='store_true', dest='InteractiveShell.logstart',
174 action='store_true', dest='InteractiveShell.logstart',
175 help="Start logging to the default log file (./ipython_log.py).")
175 help="Start logging to the default log file (./ipython_log.py).")
176 paa('--logfile','-lf',
176 paa('--logfile','-lf',
177 type=unicode, dest='InteractiveShell.logfile',
177 type=unicode, dest='InteractiveShell.logfile',
178 help="Start logging to logfile with this name.",
178 help="Start logging to logfile with this name.",
179 metavar='InteractiveShell.logfile')
179 metavar='InteractiveShell.logfile')
180 paa('--log-append','-la',
180 paa('--log-append','-la',
181 type=unicode, dest='InteractiveShell.logappend',
181 type=unicode, dest='InteractiveShell.logappend',
182 help="Start logging to the given file in append mode.",
182 help="Start logging to the given file in append mode.",
183 metavar='InteractiveShell.logfile')
183 metavar='InteractiveShell.logfile')
184 paa('--pdb',
184 paa('--pdb',
185 action='store_true', dest='InteractiveShell.pdb',
185 action='store_true', dest='InteractiveShell.pdb',
186 help="Enable auto calling the pdb debugger after every exception.")
186 help="Enable auto calling the pdb debugger after every exception.")
187 paa('--no-pdb',
187 paa('--no-pdb',
188 action='store_false', dest='InteractiveShell.pdb',
188 action='store_false', dest='InteractiveShell.pdb',
189 help="Disable auto calling the pdb debugger after every exception.")
189 help="Disable auto calling the pdb debugger after every exception.")
190 paa('--pprint',
190 paa('--pprint',
191 action='store_true', dest='InteractiveShell.pprint',
191 action='store_true', dest='InteractiveShell.pprint',
192 help="Enable auto pretty printing of results.")
192 help="Enable auto pretty printing of results.")
193 paa('--no-pprint',
193 paa('--no-pprint',
194 action='store_false', dest='InteractiveShell.pprint',
194 action='store_false', dest='InteractiveShell.pprint',
195 help="Disable auto auto pretty printing of results.")
195 help="Disable auto auto pretty printing of results.")
196 paa('--prompt-in1','-pi1',
196 paa('--prompt-in1','-pi1',
197 type=str, dest='InteractiveShell.prompt_in1',
197 type=str, dest='InteractiveShell.prompt_in1',
198 help=
198 help=
199 """Set the main input prompt ('In [\#]: '). Note that if you are using
199 """Set the main input prompt ('In [\#]: '). Note that if you are using
200 numbered prompts, the number is represented with a '\#' in the string.
200 numbered prompts, the number is represented with a '\#' in the string.
201 Don't forget to quote strings with spaces embedded in them. Most
201 Don't forget to quote strings with spaces embedded in them. Most
202 bash-like escapes can be used to customize IPython's prompts, as well
202 bash-like escapes can be used to customize IPython's prompts, as well
203 as a few additional ones which are IPython-spe- cific. All valid
203 as a few additional ones which are IPython-spe- cific. All valid
204 prompt escapes are described in detail in the Customization section of
204 prompt escapes are described in detail in the Customization section of
205 the IPython manual.""",
205 the IPython manual.""",
206 metavar='InteractiveShell.prompt_in1')
206 metavar='InteractiveShell.prompt_in1')
207 paa('--prompt-in2','-pi2',
207 paa('--prompt-in2','-pi2',
208 type=str, dest='InteractiveShell.prompt_in2',
208 type=str, dest='InteractiveShell.prompt_in2',
209 help=
209 help=
210 """Set the secondary input prompt (' .\D.: '). Similar to the previous
210 """Set the secondary input prompt (' .\D.: '). Similar to the previous
211 option, but used for the continuation prompts. The special sequence
211 option, but used for the continuation prompts. The special sequence
212 '\D' is similar to '\#', but with all digits replaced by dots (so you
212 '\D' is similar to '\#', but with all digits replaced by dots (so you
213 can have your continuation prompt aligned with your input prompt).
213 can have your continuation prompt aligned with your input prompt).
214 Default: ' .\D.: ' (note three spaces at the start for alignment with
214 Default: ' .\D.: ' (note three spaces at the start for alignment with
215 'In [\#]')""",
215 'In [\#]')""",
216 metavar='InteractiveShell.prompt_in2')
216 metavar='InteractiveShell.prompt_in2')
217 paa('--prompt-out','-po',
217 paa('--prompt-out','-po',
218 type=str, dest='InteractiveShell.prompt_out',
218 type=str, dest='InteractiveShell.prompt_out',
219 help="Set the output prompt ('Out[\#]:')",
219 help="Set the output prompt ('Out[\#]:')",
220 metavar='InteractiveShell.prompt_out')
220 metavar='InteractiveShell.prompt_out')
221 paa('--quick',
221 paa('--quick',
222 action='store_true', dest='Global.quick',
222 action='store_true', dest='Global.quick',
223 help="Enable quick startup with no config files.")
223 help="Enable quick startup with no config files.")
224 paa('--readline',
224 paa('--readline',
225 action='store_true', dest='InteractiveShell.readline_use',
225 action='store_true', dest='InteractiveShell.readline_use',
226 help="Enable readline for command line usage.")
226 help="Enable readline for command line usage.")
227 paa('--no-readline',
227 paa('--no-readline',
228 action='store_false', dest='InteractiveShell.readline_use',
228 action='store_false', dest='InteractiveShell.readline_use',
229 help="Disable readline for command line usage.")
229 help="Disable readline for command line usage.")
230 paa('--screen-length','-sl',
230 paa('--screen-length','-sl',
231 type=int, dest='InteractiveShell.screen_length',
231 type=int, dest='TerminalInteractiveShell.screen_length',
232 help=
232 help=
233 """Number of lines of your screen, used to control printing of very
233 """Number of lines of your screen, used to control printing of very
234 long strings. Strings longer than this number of lines will be sent
234 long strings. Strings longer than this number of lines will be sent
235 through a pager instead of directly printed. The default value for
235 through a pager instead of directly printed. The default value for
236 this is 0, which means IPython will auto-detect your screen size every
236 this is 0, which means IPython will auto-detect your screen size every
237 time it needs to print certain potentially long strings (this doesn't
237 time it needs to print certain potentially long strings (this doesn't
238 change the behavior of the 'print' keyword, it's only triggered
238 change the behavior of the 'print' keyword, it's only triggered
239 internally). If for some reason this isn't working well (it needs
239 internally). If for some reason this isn't working well (it needs
240 curses support), specify it yourself. Otherwise don't change the
240 curses support), specify it yourself. Otherwise don't change the
241 default.""",
241 default.""",
242 metavar='InteractiveShell.screen_length')
242 metavar='TerminalInteractiveShell.screen_length')
243 paa('--separate-in','-si',
243 paa('--separate-in','-si',
244 type=str, dest='InteractiveShell.separate_in',
244 type=str, dest='TerminalInteractiveShell.separate_in',
245 help="Separator before input prompts. Default '\\n'.",
245 help="Separator before input prompts. Default '\\n'.",
246 metavar='InteractiveShell.separate_in')
246 metavar='TerminalInteractiveShell.separate_in')
247 paa('--separate-out','-so',
247 paa('--separate-out','-so',
248 type=str, dest='InteractiveShell.separate_out',
248 type=str, dest='TerminalInteractiveShell.separate_out',
249 help="Separator before output prompts. Default 0 (nothing).",
249 help="Separator before output prompts. Default 0 (nothing).",
250 metavar='InteractiveShell.separate_out')
250 metavar='TerminalInteractiveShell.separate_out')
251 paa('--separate-out2','-so2',
251 paa('--separate-out2','-so2',
252 type=str, dest='InteractiveShell.separate_out2',
252 type=str, dest='TerminalInteractiveShell.separate_out2',
253 help="Separator after output prompts. Default 0 (nonight).",
253 help="Separator after output prompts. Default 0 (nonight).",
254 metavar='InteractiveShell.separate_out2')
254 metavar='TerminalInteractiveShell.separate_out2')
255 paa('--no-sep',
255 paa('--no-sep',
256 action='store_true', dest='Global.nosep',
256 action='store_true', dest='Global.nosep',
257 help="Eliminate all spacing between prompts.")
257 help="Eliminate all spacing between prompts.")
258 paa('--term-title',
258 paa('--term-title',
259 action='store_true', dest='InteractiveShell.term_title',
259 action='store_true', dest='TerminalInteractiveShell.term_title',
260 help="Enable auto setting the terminal title.")
260 help="Enable auto setting the terminal title.")
261 paa('--no-term-title',
261 paa('--no-term-title',
262 action='store_false', dest='InteractiveShell.term_title',
262 action='store_false', dest='TerminalInteractiveShell.term_title',
263 help="Disable auto setting the terminal title.")
263 help="Disable auto setting the terminal title.")
264 paa('--xmode',
264 paa('--xmode',
265 type=str, dest='InteractiveShell.xmode',
265 type=str, dest='InteractiveShell.xmode',
266 help=
266 help=
267 """Exception reporting mode ('Plain','Context','Verbose'). Plain:
267 """Exception reporting mode ('Plain','Context','Verbose'). Plain:
268 similar to python's normal traceback printing. Context: prints 5 lines
268 similar to python's normal traceback printing. Context: prints 5 lines
269 of context source code around each line in the traceback. Verbose:
269 of context source code around each line in the traceback. Verbose:
270 similar to Context, but additionally prints the variables currently
270 similar to Context, but additionally prints the variables currently
271 visible where the exception happened (shortening their strings if too
271 visible where the exception happened (shortening their strings if too
272 long). This can potentially be very slow, if you happen to have a huge
272 long). This can potentially be very slow, if you happen to have a huge
273 data structure whose string representation is complex to compute.
273 data structure whose string representation is complex to compute.
274 Your computer may appear to freeze for a while with cpu usage at 100%%.
274 Your computer may appear to freeze for a while with cpu usage at 100%%.
275 If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting
275 If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting
276 it more than once).
276 it more than once).
277 """,
277 """,
278 metavar='InteractiveShell.xmode')
278 metavar='InteractiveShell.xmode')
279 paa('--ext',
279 paa('--ext',
280 type=str, dest='Global.extra_extension',
280 type=str, dest='Global.extra_extension',
281 help="The dotted module name of an IPython extension to load.",
281 help="The dotted module name of an IPython extension to load.",
282 metavar='Global.extra_extension')
282 metavar='Global.extra_extension')
283 paa('-c',
283 paa('-c',
284 type=str, dest='Global.code_to_run',
284 type=str, dest='Global.code_to_run',
285 help="Execute the given command string.",
285 help="Execute the given command string.",
286 metavar='Global.code_to_run')
286 metavar='Global.code_to_run')
287 paa('-i',
287 paa('-i',
288 action='store_true', dest='Global.force_interact',
288 action='store_true', dest='Global.force_interact',
289 help=
289 help=
290 "If running code from the command line, become interactive afterwards.")
290 "If running code from the command line, become interactive afterwards.")
291
291
292 # Options to start with GUI control enabled from the beginning
292 # Options to start with GUI control enabled from the beginning
293 paa('--gui',
293 paa('--gui',
294 type=str, dest='Global.gui',
294 type=str, dest='Global.gui',
295 help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
295 help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
296 metavar='gui-mode')
296 metavar='gui-mode')
297 paa('--pylab','-pylab',
297 paa('--pylab','-pylab',
298 type=str, dest='Global.pylab',
298 type=str, dest='Global.pylab',
299 nargs='?', const='auto', metavar='gui-mode',
299 nargs='?', const='auto', metavar='gui-mode',
300 help="Pre-load matplotlib and numpy for interactive use. "+
300 help="Pre-load matplotlib and numpy for interactive use. "+
301 "If no value is given, the gui backend is matplotlib's, else use "+
301 "If no value is given, the gui backend is matplotlib's, else use "+
302 "one of: ['tk', 'qt', 'wx', 'gtk'].")
302 "one of: ['tk', 'qt', 'wx', 'gtk'].")
303
303
304 # Legacy GUI options. Leave them in for backwards compatibility, but the
304 # Legacy GUI options. Leave them in for backwards compatibility, but the
305 # 'thread' names are really a misnomer now.
305 # 'thread' names are really a misnomer now.
306 paa('--wthread', '-wthread',
306 paa('--wthread', '-wthread',
307 action='store_true', dest='Global.wthread',
307 action='store_true', dest='Global.wthread',
308 help=
308 help=
309 """Enable wxPython event loop integration. (DEPRECATED, use --gui wx)""")
309 """Enable wxPython event loop integration. (DEPRECATED, use --gui wx)""")
310 paa('--q4thread', '--qthread', '-q4thread', '-qthread',
310 paa('--q4thread', '--qthread', '-q4thread', '-qthread',
311 action='store_true', dest='Global.q4thread',
311 action='store_true', dest='Global.q4thread',
312 help=
312 help=
313 """Enable Qt4 event loop integration. Qt3 is no longer supported.
313 """Enable Qt4 event loop integration. Qt3 is no longer supported.
314 (DEPRECATED, use --gui qt)""")
314 (DEPRECATED, use --gui qt)""")
315 paa('--gthread', '-gthread',
315 paa('--gthread', '-gthread',
316 action='store_true', dest='Global.gthread',
316 action='store_true', dest='Global.gthread',
317 help=
317 help=
318 """Enable GTK event loop integration. (DEPRECATED, use --gui gtk)""")
318 """Enable GTK event loop integration. (DEPRECATED, use --gui gtk)""")
319
319
320
320
321 #-----------------------------------------------------------------------------
321 #-----------------------------------------------------------------------------
322 # Crash handler for this application
322 # Crash handler for this application
323 #-----------------------------------------------------------------------------
323 #-----------------------------------------------------------------------------
324
324
325
325
326 _message_template = """\
326 _message_template = """\
327 Oops, $self.app_name crashed. We do our best to make it stable, but...
327 Oops, $self.app_name crashed. We do our best to make it stable, but...
328
328
329 A crash report was automatically generated with the following information:
329 A crash report was automatically generated with the following information:
330 - A verbatim copy of the crash traceback.
330 - A verbatim copy of the crash traceback.
331 - A copy of your input history during this session.
331 - A copy of your input history during this session.
332 - Data on your current $self.app_name configuration.
332 - Data on your current $self.app_name configuration.
333
333
334 It was left in the file named:
334 It was left in the file named:
335 \t'$self.crash_report_fname'
335 \t'$self.crash_report_fname'
336 If you can email this file to the developers, the information in it will help
336 If you can email this file to the developers, the information in it will help
337 them in understanding and correcting the problem.
337 them in understanding and correcting the problem.
338
338
339 You can mail it to: $self.contact_name at $self.contact_email
339 You can mail it to: $self.contact_name at $self.contact_email
340 with the subject '$self.app_name Crash Report'.
340 with the subject '$self.app_name Crash Report'.
341
341
342 If you want to do it now, the following command will work (under Unix):
342 If you want to do it now, the following command will work (under Unix):
343 mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
343 mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
344
344
345 To ensure accurate tracking of this issue, please file a report about it at:
345 To ensure accurate tracking of this issue, please file a report about it at:
346 $self.bug_tracker
346 $self.bug_tracker
347 """
347 """
348
348
349 class IPAppCrashHandler(CrashHandler):
349 class IPAppCrashHandler(CrashHandler):
350 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
350 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
351
351
352 message_template = _message_template
352 message_template = _message_template
353
353
354 def __init__(self, app):
354 def __init__(self, app):
355 contact_name = release.authors['Fernando'][0]
355 contact_name = release.authors['Fernando'][0]
356 contact_email = release.authors['Fernando'][1]
356 contact_email = release.authors['Fernando'][1]
357 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
357 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
358 super(IPAppCrashHandler,self).__init__(
358 super(IPAppCrashHandler,self).__init__(
359 app, contact_name, contact_email, bug_tracker
359 app, contact_name, contact_email, bug_tracker
360 )
360 )
361
361
362 def make_report(self,traceback):
362 def make_report(self,traceback):
363 """Return a string containing a crash report."""
363 """Return a string containing a crash report."""
364
364
365 sec_sep = self.section_sep
365 sec_sep = self.section_sep
366 # Start with parent report
366 # Start with parent report
367 report = [super(IPAppCrashHandler, self).make_report(traceback)]
367 report = [super(IPAppCrashHandler, self).make_report(traceback)]
368 # Add interactive-specific info we may have
368 # Add interactive-specific info we may have
369 rpt_add = report.append
369 rpt_add = report.append
370 try:
370 try:
371 rpt_add(sec_sep+"History of session input:")
371 rpt_add(sec_sep+"History of session input:")
372 for line in self.app.shell.user_ns['_ih']:
372 for line in self.app.shell.user_ns['_ih']:
373 rpt_add(line)
373 rpt_add(line)
374 rpt_add('\n*** Last line of input (may not be in above history):\n')
374 rpt_add('\n*** Last line of input (may not be in above history):\n')
375 rpt_add(self.app.shell._last_input_line+'\n')
375 rpt_add(self.app.shell._last_input_line+'\n')
376 except:
376 except:
377 pass
377 pass
378
378
379 return ''.join(report)
379 return ''.join(report)
380
380
381
381
382 #-----------------------------------------------------------------------------
382 #-----------------------------------------------------------------------------
383 # Main classes and functions
383 # Main classes and functions
384 #-----------------------------------------------------------------------------
384 #-----------------------------------------------------------------------------
385
385
386 class IPythonApp(Application):
386 class IPythonApp(Application):
387 name = u'ipython'
387 name = u'ipython'
388 #: argparse formats better the 'usage' than the 'description' field
388 #: argparse formats better the 'usage' than the 'description' field
389 description = None
389 description = None
390 usage = usage.cl_usage
390 usage = usage.cl_usage
391 command_line_loader = IPAppConfigLoader
391 command_line_loader = IPAppConfigLoader
392 default_config_file_name = default_config_file_name
392 default_config_file_name = default_config_file_name
393 crash_handler_class = IPAppCrashHandler
393 crash_handler_class = IPAppCrashHandler
394
394
395 def create_default_config(self):
395 def create_default_config(self):
396 super(IPythonApp, self).create_default_config()
396 super(IPythonApp, self).create_default_config()
397 # Eliminate multiple lookups
397 # Eliminate multiple lookups
398 Global = self.default_config.Global
398 Global = self.default_config.Global
399
399
400 # Set all default values
400 # Set all default values
401 Global.display_banner = True
401 Global.display_banner = True
402
402
403 # If the -c flag is given or a file is given to run at the cmd line
403 # If the -c flag is given or a file is given to run at the cmd line
404 # like "ipython foo.py", normally we exit without starting the main
404 # like "ipython foo.py", normally we exit without starting the main
405 # loop. The force_interact config variable allows a user to override
405 # loop. The force_interact config variable allows a user to override
406 # this and interact. It is also set by the -i cmd line flag, just
406 # this and interact. It is also set by the -i cmd line flag, just
407 # like Python.
407 # like Python.
408 Global.force_interact = False
408 Global.force_interact = False
409
409
410 # By default always interact by starting the IPython mainloop.
410 # By default always interact by starting the IPython mainloop.
411 Global.interact = True
411 Global.interact = True
412
412
413 # No GUI integration by default
413 # No GUI integration by default
414 Global.gui = False
414 Global.gui = False
415 # Pylab off by default
415 # Pylab off by default
416 Global.pylab = False
416 Global.pylab = False
417
417
418 # Deprecated versions of gui support that used threading, we support
418 # Deprecated versions of gui support that used threading, we support
419 # them just for bacwards compatibility as an alternate spelling for
419 # them just for bacwards compatibility as an alternate spelling for
420 # '--gui X'
420 # '--gui X'
421 Global.qthread = False
421 Global.qthread = False
422 Global.q4thread = False
422 Global.q4thread = False
423 Global.wthread = False
423 Global.wthread = False
424 Global.gthread = False
424 Global.gthread = False
425
425
426 def load_file_config(self):
426 def load_file_config(self):
427 if hasattr(self.command_line_config.Global, 'quick'):
427 if hasattr(self.command_line_config.Global, 'quick'):
428 if self.command_line_config.Global.quick:
428 if self.command_line_config.Global.quick:
429 self.file_config = Config()
429 self.file_config = Config()
430 return
430 return
431 super(IPythonApp, self).load_file_config()
431 super(IPythonApp, self).load_file_config()
432
432
433 def post_load_file_config(self):
433 def post_load_file_config(self):
434 if hasattr(self.command_line_config.Global, 'extra_extension'):
434 if hasattr(self.command_line_config.Global, 'extra_extension'):
435 if not hasattr(self.file_config.Global, 'extensions'):
435 if not hasattr(self.file_config.Global, 'extensions'):
436 self.file_config.Global.extensions = []
436 self.file_config.Global.extensions = []
437 self.file_config.Global.extensions.append(
437 self.file_config.Global.extensions.append(
438 self.command_line_config.Global.extra_extension)
438 self.command_line_config.Global.extra_extension)
439 del self.command_line_config.Global.extra_extension
439 del self.command_line_config.Global.extra_extension
440
440
441 def pre_construct(self):
441 def pre_construct(self):
442 config = self.master_config
442 config = self.master_config
443
443
444 if hasattr(config.Global, 'classic'):
444 if hasattr(config.Global, 'classic'):
445 if config.Global.classic:
445 if config.Global.classic:
446 config.InteractiveShell.cache_size = 0
446 config.InteractiveShell.cache_size = 0
447 config.InteractiveShell.pprint = 0
447 config.InteractiveShell.pprint = 0
448 config.InteractiveShell.prompt_in1 = '>>> '
448 config.InteractiveShell.prompt_in1 = '>>> '
449 config.InteractiveShell.prompt_in2 = '... '
449 config.InteractiveShell.prompt_in2 = '... '
450 config.InteractiveShell.prompt_out = ''
450 config.InteractiveShell.prompt_out = ''
451 config.InteractiveShell.separate_in = \
451 config.TerminalInteractiveShell.separate_in = \
452 config.InteractiveShell.separate_out = \
452 config.TerminalInteractiveShell.separate_out = \
453 config.InteractiveShell.separate_out2 = ''
453 config.TerminalInteractiveShell.separate_out2 = ''
454 config.InteractiveShell.colors = 'NoColor'
454 config.InteractiveShell.colors = 'NoColor'
455 config.InteractiveShell.xmode = 'Plain'
455 config.InteractiveShell.xmode = 'Plain'
456
456
457 if hasattr(config.Global, 'nosep'):
457 if hasattr(config.Global, 'nosep'):
458 if config.Global.nosep:
458 if config.Global.nosep:
459 config.InteractiveShell.separate_in = \
459 config.TerminalInteractiveShell.separate_in = \
460 config.InteractiveShell.separate_out = \
460 config.TerminalInteractiveShell.separate_out = \
461 config.InteractiveShell.separate_out2 = ''
461 config.TerminalInteractiveShell.separate_out2 = ''
462
462
463 # if there is code of files to run from the cmd line, don't interact
463 # if there is code of files to run from the cmd line, don't interact
464 # unless the -i flag (Global.force_interact) is true.
464 # unless the -i flag (Global.force_interact) is true.
465 code_to_run = config.Global.get('code_to_run','')
465 code_to_run = config.Global.get('code_to_run','')
466 file_to_run = False
466 file_to_run = False
467 if self.extra_args and self.extra_args[0]:
467 if self.extra_args and self.extra_args[0]:
468 file_to_run = True
468 file_to_run = True
469 if file_to_run or code_to_run:
469 if file_to_run or code_to_run:
470 if not config.Global.force_interact:
470 if not config.Global.force_interact:
471 config.Global.interact = False
471 config.Global.interact = False
472
472
473 def construct(self):
473 def construct(self):
474 # I am a little hesitant to put these into InteractiveShell itself.
474 # I am a little hesitant to put these into InteractiveShell itself.
475 # But that might be the place for them
475 # But that might be the place for them
476 sys.path.insert(0, '')
476 sys.path.insert(0, '')
477
477
478 # Create an InteractiveShell instance.
478 # Create an InteractiveShell instance.
479 self.shell = InteractiveShell.instance(config=self.master_config)
479 self.shell = TerminalInteractiveShell.instance(config=self.master_config)
480
480
481 def post_construct(self):
481 def post_construct(self):
482 """Do actions after construct, but before starting the app."""
482 """Do actions after construct, but before starting the app."""
483 config = self.master_config
483 config = self.master_config
484
484
485 # shell.display_banner should always be False for the terminal
485 # shell.display_banner should always be False for the terminal
486 # based app, because we call shell.show_banner() by hand below
486 # based app, because we call shell.show_banner() by hand below
487 # so the banner shows *before* all extension loading stuff.
487 # so the banner shows *before* all extension loading stuff.
488 self.shell.display_banner = False
488 self.shell.display_banner = False
489 if config.Global.display_banner and \
489 if config.Global.display_banner and \
490 config.Global.interact:
490 config.Global.interact:
491 self.shell.show_banner()
491 self.shell.show_banner()
492
492
493 # Make sure there is a space below the banner.
493 # Make sure there is a space below the banner.
494 if self.log_level <= logging.INFO: print
494 if self.log_level <= logging.INFO: print
495
495
496 # Now a variety of things that happen after the banner is printed.
496 # Now a variety of things that happen after the banner is printed.
497 self._enable_gui_pylab()
497 self._enable_gui_pylab()
498 self._load_extensions()
498 self._load_extensions()
499 self._run_exec_lines()
499 self._run_exec_lines()
500 self._run_exec_files()
500 self._run_exec_files()
501 self._run_cmd_line_code()
501 self._run_cmd_line_code()
502
502
503 def _enable_gui_pylab(self):
503 def _enable_gui_pylab(self):
504 """Enable GUI event loop integration, taking pylab into account."""
504 """Enable GUI event loop integration, taking pylab into account."""
505 Global = self.master_config.Global
505 Global = self.master_config.Global
506
506
507 # Select which gui to use
507 # Select which gui to use
508 if Global.gui:
508 if Global.gui:
509 gui = Global.gui
509 gui = Global.gui
510 # The following are deprecated, but there's likely to be a lot of use
510 # The following are deprecated, but there's likely to be a lot of use
511 # of this form out there, so we might as well support it for now. But
511 # of this form out there, so we might as well support it for now. But
512 # the --gui option above takes precedence.
512 # the --gui option above takes precedence.
513 elif Global.wthread:
513 elif Global.wthread:
514 gui = inputhook.GUI_WX
514 gui = inputhook.GUI_WX
515 elif Global.qthread:
515 elif Global.qthread:
516 gui = inputhook.GUI_QT
516 gui = inputhook.GUI_QT
517 elif Global.gthread:
517 elif Global.gthread:
518 gui = inputhook.GUI_GTK
518 gui = inputhook.GUI_GTK
519 else:
519 else:
520 gui = None
520 gui = None
521
521
522 # Using --pylab will also require gui activation, though which toolkit
522 # Using --pylab will also require gui activation, though which toolkit
523 # to use may be chosen automatically based on mpl configuration.
523 # to use may be chosen automatically based on mpl configuration.
524 if Global.pylab:
524 if Global.pylab:
525 activate = self.shell.enable_pylab
525 activate = self.shell.enable_pylab
526 if Global.pylab == 'auto':
526 if Global.pylab == 'auto':
527 gui = None
527 gui = None
528 else:
528 else:
529 gui = Global.pylab
529 gui = Global.pylab
530 else:
530 else:
531 # Enable only GUI integration, no pylab
531 # Enable only GUI integration, no pylab
532 activate = inputhook.enable_gui
532 activate = inputhook.enable_gui
533
533
534 if gui or Global.pylab:
534 if gui or Global.pylab:
535 try:
535 try:
536 self.log.info("Enabling GUI event loop integration, "
536 self.log.info("Enabling GUI event loop integration, "
537 "toolkit=%s, pylab=%s" % (gui, Global.pylab) )
537 "toolkit=%s, pylab=%s" % (gui, Global.pylab) )
538 activate(gui)
538 activate(gui)
539 except:
539 except:
540 self.log.warn("Error in enabling GUI event loop integration:")
540 self.log.warn("Error in enabling GUI event loop integration:")
541 self.shell.showtraceback()
541 self.shell.showtraceback()
542
542
543 def _load_extensions(self):
543 def _load_extensions(self):
544 """Load all IPython extensions in Global.extensions.
544 """Load all IPython extensions in Global.extensions.
545
545
546 This uses the :meth:`ExtensionManager.load_extensions` to load all
546 This uses the :meth:`ExtensionManager.load_extensions` to load all
547 the extensions listed in ``self.master_config.Global.extensions``.
547 the extensions listed in ``self.master_config.Global.extensions``.
548 """
548 """
549 try:
549 try:
550 if hasattr(self.master_config.Global, 'extensions'):
550 if hasattr(self.master_config.Global, 'extensions'):
551 self.log.debug("Loading IPython extensions...")
551 self.log.debug("Loading IPython extensions...")
552 extensions = self.master_config.Global.extensions
552 extensions = self.master_config.Global.extensions
553 for ext in extensions:
553 for ext in extensions:
554 try:
554 try:
555 self.log.info("Loading IPython extension: %s" % ext)
555 self.log.info("Loading IPython extension: %s" % ext)
556 self.shell.extension_manager.load_extension(ext)
556 self.shell.extension_manager.load_extension(ext)
557 except:
557 except:
558 self.log.warn("Error in loading extension: %s" % ext)
558 self.log.warn("Error in loading extension: %s" % ext)
559 self.shell.showtraceback()
559 self.shell.showtraceback()
560 except:
560 except:
561 self.log.warn("Unknown error in loading extensions:")
561 self.log.warn("Unknown error in loading extensions:")
562 self.shell.showtraceback()
562 self.shell.showtraceback()
563
563
564 def _run_exec_lines(self):
564 def _run_exec_lines(self):
565 """Run lines of code in Global.exec_lines in the user's namespace."""
565 """Run lines of code in Global.exec_lines in the user's namespace."""
566 try:
566 try:
567 if hasattr(self.master_config.Global, 'exec_lines'):
567 if hasattr(self.master_config.Global, 'exec_lines'):
568 self.log.debug("Running code from Global.exec_lines...")
568 self.log.debug("Running code from Global.exec_lines...")
569 exec_lines = self.master_config.Global.exec_lines
569 exec_lines = self.master_config.Global.exec_lines
570 for line in exec_lines:
570 for line in exec_lines:
571 try:
571 try:
572 self.log.info("Running code in user namespace: %s" %
572 self.log.info("Running code in user namespace: %s" %
573 line)
573 line)
574 self.shell.runlines(line)
574 self.shell.runlines(line)
575 except:
575 except:
576 self.log.warn("Error in executing line in user "
576 self.log.warn("Error in executing line in user "
577 "namespace: %s" % line)
577 "namespace: %s" % line)
578 self.shell.showtraceback()
578 self.shell.showtraceback()
579 except:
579 except:
580 self.log.warn("Unknown error in handling Global.exec_lines:")
580 self.log.warn("Unknown error in handling Global.exec_lines:")
581 self.shell.showtraceback()
581 self.shell.showtraceback()
582
582
583 def _exec_file(self, fname):
583 def _exec_file(self, fname):
584 full_filename = filefind(fname, [u'.', self.ipython_dir])
584 full_filename = filefind(fname, [u'.', self.ipython_dir])
585 if os.path.isfile(full_filename):
585 if os.path.isfile(full_filename):
586 if full_filename.endswith(u'.py'):
586 if full_filename.endswith(u'.py'):
587 self.log.info("Running file in user namespace: %s" %
587 self.log.info("Running file in user namespace: %s" %
588 full_filename)
588 full_filename)
589 # Ensure that __file__ is always defined to match Python behavior
589 # Ensure that __file__ is always defined to match Python behavior
590 self.shell.user_ns['__file__'] = fname
590 self.shell.user_ns['__file__'] = fname
591 try:
591 try:
592 self.shell.safe_execfile(full_filename, self.shell.user_ns)
592 self.shell.safe_execfile(full_filename, self.shell.user_ns)
593 finally:
593 finally:
594 del self.shell.user_ns['__file__']
594 del self.shell.user_ns['__file__']
595 elif full_filename.endswith('.ipy'):
595 elif full_filename.endswith('.ipy'):
596 self.log.info("Running file in user namespace: %s" %
596 self.log.info("Running file in user namespace: %s" %
597 full_filename)
597 full_filename)
598 self.shell.safe_execfile_ipy(full_filename)
598 self.shell.safe_execfile_ipy(full_filename)
599 else:
599 else:
600 self.log.warn("File does not have a .py or .ipy extension: <%s>"
600 self.log.warn("File does not have a .py or .ipy extension: <%s>"
601 % full_filename)
601 % full_filename)
602 def _run_exec_files(self):
602 def _run_exec_files(self):
603 try:
603 try:
604 if hasattr(self.master_config.Global, 'exec_files'):
604 if hasattr(self.master_config.Global, 'exec_files'):
605 self.log.debug("Running files in Global.exec_files...")
605 self.log.debug("Running files in Global.exec_files...")
606 exec_files = self.master_config.Global.exec_files
606 exec_files = self.master_config.Global.exec_files
607 for fname in exec_files:
607 for fname in exec_files:
608 self._exec_file(fname)
608 self._exec_file(fname)
609 except:
609 except:
610 self.log.warn("Unknown error in handling Global.exec_files:")
610 self.log.warn("Unknown error in handling Global.exec_files:")
611 self.shell.showtraceback()
611 self.shell.showtraceback()
612
612
613 def _run_cmd_line_code(self):
613 def _run_cmd_line_code(self):
614 if hasattr(self.master_config.Global, 'code_to_run'):
614 if hasattr(self.master_config.Global, 'code_to_run'):
615 line = self.master_config.Global.code_to_run
615 line = self.master_config.Global.code_to_run
616 try:
616 try:
617 self.log.info("Running code given at command line (-c): %s" %
617 self.log.info("Running code given at command line (-c): %s" %
618 line)
618 line)
619 self.shell.runlines(line)
619 self.shell.runlines(line)
620 except:
620 except:
621 self.log.warn("Error in executing line in user namespace: %s" %
621 self.log.warn("Error in executing line in user namespace: %s" %
622 line)
622 line)
623 self.shell.showtraceback()
623 self.shell.showtraceback()
624 return
624 return
625 # Like Python itself, ignore the second if the first of these is present
625 # Like Python itself, ignore the second if the first of these is present
626 try:
626 try:
627 fname = self.extra_args[0]
627 fname = self.extra_args[0]
628 except:
628 except:
629 pass
629 pass
630 else:
630 else:
631 try:
631 try:
632 self._exec_file(fname)
632 self._exec_file(fname)
633 except:
633 except:
634 self.log.warn("Error in executing file in user namespace: %s" %
634 self.log.warn("Error in executing file in user namespace: %s" %
635 fname)
635 fname)
636 self.shell.showtraceback()
636 self.shell.showtraceback()
637
637
638 def start_app(self):
638 def start_app(self):
639 if self.master_config.Global.interact:
639 if self.master_config.Global.interact:
640 self.log.debug("Starting IPython's mainloop...")
640 self.log.debug("Starting IPython's mainloop...")
641 self.shell.mainloop()
641 self.shell.mainloop()
642 else:
642 else:
643 self.log.debug("IPython not interactive, start_app is no-op...")
643 self.log.debug("IPython not interactive, start_app is no-op...")
644
644
645
645
646 def load_default_config(ipython_dir=None):
646 def load_default_config(ipython_dir=None):
647 """Load the default config file from the default ipython_dir.
647 """Load the default config file from the default ipython_dir.
648
648
649 This is useful for embedded shells.
649 This is useful for embedded shells.
650 """
650 """
651 if ipython_dir is None:
651 if ipython_dir is None:
652 ipython_dir = get_ipython_dir()
652 ipython_dir = get_ipython_dir()
653 cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
653 cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
654 config = cl.load_config()
654 config = cl.load_config()
655 return config
655 return config
656
656
657
657
658 def launch_new_instance():
658 def launch_new_instance():
659 """Create and run a full blown IPython instance"""
659 """Create and run a full blown IPython instance"""
660 app = IPythonApp()
660 app = IPythonApp()
661 app.start()
661 app.start()
662
662
663
663
664 if __name__ == '__main__':
664 if __name__ == '__main__':
665 launch_new_instance()
665 launch_new_instance()
@@ -1,174 +1,173 b''
1 """Global IPython app to support test running.
1 """Global IPython app to support test running.
2
2
3 We must start our own ipython object and heavily muck with it so that all the
3 We must start our own ipython object and heavily muck with it so that all the
4 modifications IPython makes to system behavior don't send the doctest machinery
4 modifications IPython makes to system behavior don't send the doctest machinery
5 into a fit. This code should be considered a gross hack, but it gets the job
5 into a fit. This code should be considered a gross hack, but it gets the job
6 done.
6 done.
7 """
7 """
8
8
9 from __future__ import absolute_import
9 from __future__ import absolute_import
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2009 The IPython Development Team
12 # Copyright (C) 2009 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 import __builtin__
22 import __builtin__
23 import commands
23 import commands
24 import os
24 import os
25 import sys
25 import sys
26
26
27 from . import tools
27 from . import tools
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Functions
30 # Functions
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 # Hack to modify the %run command so we can sync the user's namespace with the
33 # Hack to modify the %run command so we can sync the user's namespace with the
34 # test globals. Once we move over to a clean magic system, this will be done
34 # test globals. Once we move over to a clean magic system, this will be done
35 # with much less ugliness.
35 # with much less ugliness.
36
36
37 class py_file_finder(object):
37 class py_file_finder(object):
38 def __init__(self,test_filename):
38 def __init__(self,test_filename):
39 self.test_filename = test_filename
39 self.test_filename = test_filename
40
40
41 def __call__(self,name):
41 def __call__(self,name):
42 from IPython.utils.path import get_py_filename
42 from IPython.utils.path import get_py_filename
43 try:
43 try:
44 return get_py_filename(name)
44 return get_py_filename(name)
45 except IOError:
45 except IOError:
46 test_dir = os.path.dirname(self.test_filename)
46 test_dir = os.path.dirname(self.test_filename)
47 new_path = os.path.join(test_dir,name)
47 new_path = os.path.join(test_dir,name)
48 return get_py_filename(new_path)
48 return get_py_filename(new_path)
49
49
50
50
51 def _run_ns_sync(self,arg_s,runner=None):
51 def _run_ns_sync(self,arg_s,runner=None):
52 """Modified version of %run that syncs testing namespaces.
52 """Modified version of %run that syncs testing namespaces.
53
53
54 This is strictly needed for running doctests that call %run.
54 This is strictly needed for running doctests that call %run.
55 """
55 """
56 #print >> sys.stderr, 'in run_ns_sync', arg_s # dbg
56 #print >> sys.stderr, 'in run_ns_sync', arg_s # dbg
57
57
58 _ip = get_ipython()
58 _ip = get_ipython()
59 finder = py_file_finder(arg_s)
59 finder = py_file_finder(arg_s)
60 out = _ip.magic_run_ori(arg_s,runner,finder)
60 out = _ip.magic_run_ori(arg_s,runner,finder)
61 return out
61 return out
62
62
63
63
64 class ipnsdict(dict):
64 class ipnsdict(dict):
65 """A special subclass of dict for use as an IPython namespace in doctests.
65 """A special subclass of dict for use as an IPython namespace in doctests.
66
66
67 This subclass adds a simple checkpointing capability so that when testing
67 This subclass adds a simple checkpointing capability so that when testing
68 machinery clears it (we use it as the test execution context), it doesn't
68 machinery clears it (we use it as the test execution context), it doesn't
69 get completely destroyed.
69 get completely destroyed.
70 """
70 """
71
71
72 def __init__(self,*a):
72 def __init__(self,*a):
73 dict.__init__(self,*a)
73 dict.__init__(self,*a)
74 self._savedict = {}
74 self._savedict = {}
75
75
76 def clear(self):
76 def clear(self):
77 dict.clear(self)
77 dict.clear(self)
78 self.update(self._savedict)
78 self.update(self._savedict)
79
79
80 def _checkpoint(self):
80 def _checkpoint(self):
81 self._savedict.clear()
81 self._savedict.clear()
82 self._savedict.update(self)
82 self._savedict.update(self)
83
83
84 def update(self,other):
84 def update(self,other):
85 self._checkpoint()
85 self._checkpoint()
86 dict.update(self,other)
86 dict.update(self,other)
87
87
88 # If '_' is in the namespace, python won't set it when executing code,
88 # If '_' is in the namespace, python won't set it when executing code,
89 # and we have examples that test it. So we ensure that the namespace
89 # and we have examples that test it. So we ensure that the namespace
90 # is always 'clean' of it before it's used for test code execution.
90 # is always 'clean' of it before it's used for test code execution.
91 self.pop('_',None)
91 self.pop('_',None)
92
92
93 # The builtins namespace must *always* be the real __builtin__ module,
93 # The builtins namespace must *always* be the real __builtin__ module,
94 # else weird stuff happens. The main ipython code does have provisions
94 # else weird stuff happens. The main ipython code does have provisions
95 # to ensure this after %run, but since in this class we do some
95 # to ensure this after %run, but since in this class we do some
96 # aggressive low-level cleaning of the execution namespace, we need to
96 # aggressive low-level cleaning of the execution namespace, we need to
97 # correct for that ourselves, to ensure consitency with the 'real'
97 # correct for that ourselves, to ensure consitency with the 'real'
98 # ipython.
98 # ipython.
99 self['__builtins__'] = __builtin__
99 self['__builtins__'] = __builtin__
100
100
101
101
102 def get_ipython():
102 def get_ipython():
103 # This will get replaced by the real thing once we start IPython below
103 # This will get replaced by the real thing once we start IPython below
104 return start_ipython()
104 return start_ipython()
105
105
106
106
107 def start_ipython():
107 def start_ipython():
108 """Start a global IPython shell, which we need for IPython-specific syntax.
108 """Start a global IPython shell, which we need for IPython-specific syntax.
109 """
109 """
110 global get_ipython
110 global get_ipython
111
111
112 # This function should only ever run once!
112 # This function should only ever run once!
113 if hasattr(start_ipython, 'already_called'):
113 if hasattr(start_ipython, 'already_called'):
114 return
114 return
115 start_ipython.already_called = True
115 start_ipython.already_called = True
116
116
117 # Ok, first time we're called, go ahead
117 from IPython.frontend.terminal import interactiveshell
118 from IPython.core import interactiveshell
119
118
120 def xsys(cmd):
119 def xsys(cmd):
121 """Execute a command and print its output.
120 """Execute a command and print its output.
122
121
123 This is just a convenience function to replace the IPython system call
122 This is just a convenience function to replace the IPython system call
124 with one that is more doctest-friendly.
123 with one that is more doctest-friendly.
125 """
124 """
126 cmd = _ip.var_expand(cmd,depth=1)
125 cmd = _ip.var_expand(cmd,depth=1)
127 sys.stdout.write(commands.getoutput(cmd))
126 sys.stdout.write(commands.getoutput(cmd))
128 sys.stdout.flush()
127 sys.stdout.flush()
129
128
130 # Store certain global objects that IPython modifies
129 # Store certain global objects that IPython modifies
131 _displayhook = sys.displayhook
130 _displayhook = sys.displayhook
132 _excepthook = sys.excepthook
131 _excepthook = sys.excepthook
133 _main = sys.modules.get('__main__')
132 _main = sys.modules.get('__main__')
134
133
135 # Create custom argv and namespaces for our IPython to be test-friendly
134 # Create custom argv and namespaces for our IPython to be test-friendly
136 config = tools.default_config()
135 config = tools.default_config()
137
136
138 # Create and initialize our test-friendly IPython instance.
137 # Create and initialize our test-friendly IPython instance.
139 shell = interactiveshell.InteractiveShell.instance(
138 shell = interactiveshell.TerminalInteractiveShell.instance(
140 config=config,
139 config=config,
141 user_ns=ipnsdict(), user_global_ns={}
140 user_ns=ipnsdict(), user_global_ns={}
142 )
141 )
143
142
144 # A few more tweaks needed for playing nicely with doctests...
143 # A few more tweaks needed for playing nicely with doctests...
145
144
146 # These traps are normally only active for interactive use, set them
145 # These traps are normally only active for interactive use, set them
147 # permanently since we'll be mocking interactive sessions.
146 # permanently since we'll be mocking interactive sessions.
148 shell.builtin_trap.set()
147 shell.builtin_trap.set()
149
148
150 # Set error printing to stdout so nose can doctest exceptions
149 # Set error printing to stdout so nose can doctest exceptions
151 shell.InteractiveTB.out_stream = 'stdout'
150 shell.InteractiveTB.out_stream = 'stdout'
152
151
153 # Modify the IPython system call with one that uses getoutput, so that we
152 # Modify the IPython system call with one that uses getoutput, so that we
154 # can capture subcommands and print them to Python's stdout, otherwise the
153 # can capture subcommands and print them to Python's stdout, otherwise the
155 # doctest machinery would miss them.
154 # doctest machinery would miss them.
156 shell.system = xsys
155 shell.system = xsys
157
156
158 # IPython is ready, now clean up some global state...
157 # IPython is ready, now clean up some global state...
159
158
160 # Deactivate the various python system hooks added by ipython for
159 # Deactivate the various python system hooks added by ipython for
161 # interactive convenience so we don't confuse the doctest system
160 # interactive convenience so we don't confuse the doctest system
162 sys.modules['__main__'] = _main
161 sys.modules['__main__'] = _main
163 sys.displayhook = _displayhook
162 sys.displayhook = _displayhook
164 sys.excepthook = _excepthook
163 sys.excepthook = _excepthook
165
164
166 # So that ipython magics and aliases can be doctested (they work by making
165 # So that ipython magics and aliases can be doctested (they work by making
167 # a call into a global _ip object). Also make the top-level get_ipython
166 # a call into a global _ip object). Also make the top-level get_ipython
168 # now return this without recursively calling here again.
167 # now return this without recursively calling here again.
169 _ip = shell
168 _ip = shell
170 get_ipython = _ip.get_ipython
169 get_ipython = _ip.get_ipython
171 __builtin__._ip = _ip
170 __builtin__._ip = _ip
172 __builtin__.get_ipython = get_ipython
171 __builtin__.get_ipython = get_ipython
173
172
174 return _ip
173 return _ip
@@ -1,277 +1,277 b''
1 """Generic testing tools that do NOT depend on Twisted.
1 """Generic testing tools that do NOT depend on Twisted.
2
2
3 In particular, this module exposes a set of top-level assert* functions that
3 In particular, this module exposes a set of top-level assert* functions that
4 can be used in place of nose.tools.assert* in method generators (the ones in
4 can be used in place of nose.tools.assert* in method generators (the ones in
5 nose can not, at least as of nose 0.10.4).
5 nose can not, at least as of nose 0.10.4).
6
6
7 Note: our testing package contains testing.util, which does depend on Twisted
7 Note: our testing package contains testing.util, which does depend on Twisted
8 and provides utilities for tests that manage Deferreds. All testing support
8 and provides utilities for tests that manage Deferreds. All testing support
9 tools that only depend on nose, IPython or the standard library should go here
9 tools that only depend on nose, IPython or the standard library should go here
10 instead.
10 instead.
11
11
12
12
13 Authors
13 Authors
14 -------
14 -------
15 - Fernando Perez <Fernando.Perez@berkeley.edu>
15 - Fernando Perez <Fernando.Perez@berkeley.edu>
16 """
16 """
17
17
18 from __future__ import absolute_import
18 from __future__ import absolute_import
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Copyright (C) 2009 The IPython Development Team
21 # Copyright (C) 2009 The IPython Development Team
22 #
22 #
23 # Distributed under the terms of the BSD License. The full license is in
23 # Distributed under the terms of the BSD License. The full license is in
24 # the file COPYING, distributed as part of this software.
24 # the file COPYING, distributed as part of this software.
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Imports
28 # Imports
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
30
31 import os
31 import os
32 import re
32 import re
33 import sys
33 import sys
34
34
35 try:
35 try:
36 # These tools are used by parts of the runtime, so we make the nose
36 # These tools are used by parts of the runtime, so we make the nose
37 # dependency optional at this point. Nose is a hard dependency to run the
37 # dependency optional at this point. Nose is a hard dependency to run the
38 # test suite, but NOT to use ipython itself.
38 # test suite, but NOT to use ipython itself.
39 import nose.tools as nt
39 import nose.tools as nt
40 has_nose = True
40 has_nose = True
41 except ImportError:
41 except ImportError:
42 has_nose = False
42 has_nose = False
43
43
44 from IPython.config.loader import Config
44 from IPython.config.loader import Config
45 from IPython.utils.process import find_cmd, getoutputerror
45 from IPython.utils.process import find_cmd, getoutputerror
46 from IPython.utils.text import list_strings
46 from IPython.utils.text import list_strings
47 from IPython.utils.io import temp_pyfile
47 from IPython.utils.io import temp_pyfile
48
48
49 from . import decorators as dec
49 from . import decorators as dec
50
50
51 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
52 # Globals
52 # Globals
53 #-----------------------------------------------------------------------------
53 #-----------------------------------------------------------------------------
54
54
55 # Make a bunch of nose.tools assert wrappers that can be used in test
55 # Make a bunch of nose.tools assert wrappers that can be used in test
56 # generators. This will expose an assert* function for each one in nose.tools.
56 # generators. This will expose an assert* function for each one in nose.tools.
57
57
58 _tpl = """
58 _tpl = """
59 def %(name)s(*a,**kw):
59 def %(name)s(*a,**kw):
60 return nt.%(name)s(*a,**kw)
60 return nt.%(name)s(*a,**kw)
61 """
61 """
62
62
63 if has_nose:
63 if has_nose:
64 for _x in [a for a in dir(nt) if a.startswith('assert')]:
64 for _x in [a for a in dir(nt) if a.startswith('assert')]:
65 exec _tpl % dict(name=_x)
65 exec _tpl % dict(name=_x)
66
66
67 #-----------------------------------------------------------------------------
67 #-----------------------------------------------------------------------------
68 # Functions and classes
68 # Functions and classes
69 #-----------------------------------------------------------------------------
69 #-----------------------------------------------------------------------------
70
70
71 # The docstring for full_path doctests differently on win32 (different path
71 # The docstring for full_path doctests differently on win32 (different path
72 # separator) so just skip the doctest there. The example remains informative.
72 # separator) so just skip the doctest there. The example remains informative.
73 doctest_deco = dec.skip_doctest if sys.platform == 'win32' else dec.null_deco
73 doctest_deco = dec.skip_doctest if sys.platform == 'win32' else dec.null_deco
74
74
75 @doctest_deco
75 @doctest_deco
76 def full_path(startPath,files):
76 def full_path(startPath,files):
77 """Make full paths for all the listed files, based on startPath.
77 """Make full paths for all the listed files, based on startPath.
78
78
79 Only the base part of startPath is kept, since this routine is typically
79 Only the base part of startPath is kept, since this routine is typically
80 used with a script's __file__ variable as startPath. The base of startPath
80 used with a script's __file__ variable as startPath. The base of startPath
81 is then prepended to all the listed files, forming the output list.
81 is then prepended to all the listed files, forming the output list.
82
82
83 Parameters
83 Parameters
84 ----------
84 ----------
85 startPath : string
85 startPath : string
86 Initial path to use as the base for the results. This path is split
86 Initial path to use as the base for the results. This path is split
87 using os.path.split() and only its first component is kept.
87 using os.path.split() and only its first component is kept.
88
88
89 files : string or list
89 files : string or list
90 One or more files.
90 One or more files.
91
91
92 Examples
92 Examples
93 --------
93 --------
94
94
95 >>> full_path('/foo/bar.py',['a.txt','b.txt'])
95 >>> full_path('/foo/bar.py',['a.txt','b.txt'])
96 ['/foo/a.txt', '/foo/b.txt']
96 ['/foo/a.txt', '/foo/b.txt']
97
97
98 >>> full_path('/foo',['a.txt','b.txt'])
98 >>> full_path('/foo',['a.txt','b.txt'])
99 ['/a.txt', '/b.txt']
99 ['/a.txt', '/b.txt']
100
100
101 If a single file is given, the output is still a list:
101 If a single file is given, the output is still a list:
102 >>> full_path('/foo','a.txt')
102 >>> full_path('/foo','a.txt')
103 ['/a.txt']
103 ['/a.txt']
104 """
104 """
105
105
106 files = list_strings(files)
106 files = list_strings(files)
107 base = os.path.split(startPath)[0]
107 base = os.path.split(startPath)[0]
108 return [ os.path.join(base,f) for f in files ]
108 return [ os.path.join(base,f) for f in files ]
109
109
110
110
111 def parse_test_output(txt):
111 def parse_test_output(txt):
112 """Parse the output of a test run and return errors, failures.
112 """Parse the output of a test run and return errors, failures.
113
113
114 Parameters
114 Parameters
115 ----------
115 ----------
116 txt : str
116 txt : str
117 Text output of a test run, assumed to contain a line of one of the
117 Text output of a test run, assumed to contain a line of one of the
118 following forms::
118 following forms::
119 'FAILED (errors=1)'
119 'FAILED (errors=1)'
120 'FAILED (failures=1)'
120 'FAILED (failures=1)'
121 'FAILED (errors=1, failures=1)'
121 'FAILED (errors=1, failures=1)'
122
122
123 Returns
123 Returns
124 -------
124 -------
125 nerr, nfail: number of errors and failures.
125 nerr, nfail: number of errors and failures.
126 """
126 """
127
127
128 err_m = re.search(r'^FAILED \(errors=(\d+)\)', txt, re.MULTILINE)
128 err_m = re.search(r'^FAILED \(errors=(\d+)\)', txt, re.MULTILINE)
129 if err_m:
129 if err_m:
130 nerr = int(err_m.group(1))
130 nerr = int(err_m.group(1))
131 nfail = 0
131 nfail = 0
132 return nerr, nfail
132 return nerr, nfail
133
133
134 fail_m = re.search(r'^FAILED \(failures=(\d+)\)', txt, re.MULTILINE)
134 fail_m = re.search(r'^FAILED \(failures=(\d+)\)', txt, re.MULTILINE)
135 if fail_m:
135 if fail_m:
136 nerr = 0
136 nerr = 0
137 nfail = int(fail_m.group(1))
137 nfail = int(fail_m.group(1))
138 return nerr, nfail
138 return nerr, nfail
139
139
140 both_m = re.search(r'^FAILED \(errors=(\d+), failures=(\d+)\)', txt,
140 both_m = re.search(r'^FAILED \(errors=(\d+), failures=(\d+)\)', txt,
141 re.MULTILINE)
141 re.MULTILINE)
142 if both_m:
142 if both_m:
143 nerr = int(both_m.group(1))
143 nerr = int(both_m.group(1))
144 nfail = int(both_m.group(2))
144 nfail = int(both_m.group(2))
145 return nerr, nfail
145 return nerr, nfail
146
146
147 # If the input didn't match any of these forms, assume no error/failures
147 # If the input didn't match any of these forms, assume no error/failures
148 return 0, 0
148 return 0, 0
149
149
150
150
151 # So nose doesn't think this is a test
151 # So nose doesn't think this is a test
152 parse_test_output.__test__ = False
152 parse_test_output.__test__ = False
153
153
154
154
155 def default_argv():
155 def default_argv():
156 """Return a valid default argv for creating testing instances of ipython"""
156 """Return a valid default argv for creating testing instances of ipython"""
157
157
158 return ['--quick', # so no config file is loaded
158 return ['--quick', # so no config file is loaded
159 # Other defaults to minimize side effects on stdout
159 # Other defaults to minimize side effects on stdout
160 '--colors=NoColor', '--no-term-title','--no-banner',
160 '--colors=NoColor', '--no-term-title','--no-banner',
161 '--autocall=0']
161 '--autocall=0']
162
162
163
163
164 def default_config():
164 def default_config():
165 """Return a config object with good defaults for testing."""
165 """Return a config object with good defaults for testing."""
166 config = Config()
166 config = Config()
167 config.InteractiveShell.colors = 'NoColor'
167 config.TerminalInteractiveShell.colors = 'NoColor'
168 config.InteractiveShell.term_title = False,
168 config.TerminalTerminalInteractiveShell.term_title = False,
169 config.InteractiveShell.autocall = 0
169 config.TerminalInteractiveShell.autocall = 0
170 return config
170 return config
171
171
172
172
173 def ipexec(fname, options=None):
173 def ipexec(fname, options=None):
174 """Utility to call 'ipython filename'.
174 """Utility to call 'ipython filename'.
175
175
176 Starts IPython witha minimal and safe configuration to make startup as fast
176 Starts IPython witha minimal and safe configuration to make startup as fast
177 as possible.
177 as possible.
178
178
179 Note that this starts IPython in a subprocess!
179 Note that this starts IPython in a subprocess!
180
180
181 Parameters
181 Parameters
182 ----------
182 ----------
183 fname : str
183 fname : str
184 Name of file to be executed (should have .py or .ipy extension).
184 Name of file to be executed (should have .py or .ipy extension).
185
185
186 options : optional, list
186 options : optional, list
187 Extra command-line flags to be passed to IPython.
187 Extra command-line flags to be passed to IPython.
188
188
189 Returns
189 Returns
190 -------
190 -------
191 (stdout, stderr) of ipython subprocess.
191 (stdout, stderr) of ipython subprocess.
192 """
192 """
193 if options is None: options = []
193 if options is None: options = []
194
194
195 # For these subprocess calls, eliminate all prompt printing so we only see
195 # For these subprocess calls, eliminate all prompt printing so we only see
196 # output from script execution
196 # output from script execution
197 prompt_opts = ['--prompt-in1=""', '--prompt-in2=""', '--prompt-out=""']
197 prompt_opts = ['--prompt-in1=""', '--prompt-in2=""', '--prompt-out=""']
198 cmdargs = ' '.join(default_argv() + prompt_opts + options)
198 cmdargs = ' '.join(default_argv() + prompt_opts + options)
199
199
200 _ip = get_ipython()
200 _ip = get_ipython()
201 test_dir = os.path.dirname(__file__)
201 test_dir = os.path.dirname(__file__)
202
202
203 ipython_cmd = find_cmd('ipython')
203 ipython_cmd = find_cmd('ipython')
204 # Absolute path for filename
204 # Absolute path for filename
205 full_fname = os.path.join(test_dir, fname)
205 full_fname = os.path.join(test_dir, fname)
206 full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname)
206 full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname)
207 #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg
207 #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg
208 return getoutputerror(full_cmd)
208 return getoutputerror(full_cmd)
209
209
210
210
211 def ipexec_validate(fname, expected_out, expected_err='',
211 def ipexec_validate(fname, expected_out, expected_err='',
212 options=None):
212 options=None):
213 """Utility to call 'ipython filename' and validate output/error.
213 """Utility to call 'ipython filename' and validate output/error.
214
214
215 This function raises an AssertionError if the validation fails.
215 This function raises an AssertionError if the validation fails.
216
216
217 Note that this starts IPython in a subprocess!
217 Note that this starts IPython in a subprocess!
218
218
219 Parameters
219 Parameters
220 ----------
220 ----------
221 fname : str
221 fname : str
222 Name of the file to be executed (should have .py or .ipy extension).
222 Name of the file to be executed (should have .py or .ipy extension).
223
223
224 expected_out : str
224 expected_out : str
225 Expected stdout of the process.
225 Expected stdout of the process.
226
226
227 expected_err : optional, str
227 expected_err : optional, str
228 Expected stderr of the process.
228 Expected stderr of the process.
229
229
230 options : optional, list
230 options : optional, list
231 Extra command-line flags to be passed to IPython.
231 Extra command-line flags to be passed to IPython.
232
232
233 Returns
233 Returns
234 -------
234 -------
235 None
235 None
236 """
236 """
237
237
238 import nose.tools as nt
238 import nose.tools as nt
239
239
240 out, err = ipexec(fname)
240 out, err = ipexec(fname)
241 #print 'OUT', out # dbg
241 #print 'OUT', out # dbg
242 #print 'ERR', err # dbg
242 #print 'ERR', err # dbg
243 # If there are any errors, we must check those befor stdout, as they may be
243 # If there are any errors, we must check those befor stdout, as they may be
244 # more informative than simply having an empty stdout.
244 # more informative than simply having an empty stdout.
245 if err:
245 if err:
246 if expected_err:
246 if expected_err:
247 nt.assert_equals(err.strip(), expected_err.strip())
247 nt.assert_equals(err.strip(), expected_err.strip())
248 else:
248 else:
249 raise ValueError('Running file %r produced error: %r' %
249 raise ValueError('Running file %r produced error: %r' %
250 (fname, err))
250 (fname, err))
251 # If no errors or output on stderr was expected, match stdout
251 # If no errors or output on stderr was expected, match stdout
252 nt.assert_equals(out.strip(), expected_out.strip())
252 nt.assert_equals(out.strip(), expected_out.strip())
253
253
254
254
255 class TempFileMixin(object):
255 class TempFileMixin(object):
256 """Utility class to create temporary Python/IPython files.
256 """Utility class to create temporary Python/IPython files.
257
257
258 Meant as a mixin class for test cases."""
258 Meant as a mixin class for test cases."""
259
259
260 def mktmp(self, src, ext='.py'):
260 def mktmp(self, src, ext='.py'):
261 """Make a valid python temp file."""
261 """Make a valid python temp file."""
262 fname, f = temp_pyfile(src, ext)
262 fname, f = temp_pyfile(src, ext)
263 self.tmpfile = f
263 self.tmpfile = f
264 self.fname = fname
264 self.fname = fname
265
265
266 def teardown(self):
266 def teardown(self):
267 if hasattr(self, 'tmpfile'):
267 if hasattr(self, 'tmpfile'):
268 # If the tmpfile wasn't made because of skipped tests, like in
268 # If the tmpfile wasn't made because of skipped tests, like in
269 # win32, there's nothing to cleanup.
269 # win32, there's nothing to cleanup.
270 self.tmpfile.close()
270 self.tmpfile.close()
271 try:
271 try:
272 os.unlink(self.fname)
272 os.unlink(self.fname)
273 except:
273 except:
274 # On Windows, even though we close the file, we still can't
274 # On Windows, even though we close the file, we still can't
275 # delete it. I have no clue why
275 # delete it. I have no clue why
276 pass
276 pass
277
277
@@ -1,473 +1,484 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Utilities for working with strings and text.
3 Utilities for working with strings and text.
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import __main__
17 import __main__
18
18
19 import os
19 import os
20 import re
20 import re
21 import shutil
21 import shutil
22 import types
22 import types
23
23
24 from IPython.external.path import path
24 from IPython.external.path import path
25
25
26 from IPython.utils.generics import result_display
26 from IPython.utils.generics import result_display
27 from IPython.utils.io import nlprint
27 from IPython.utils.io import nlprint
28 from IPython.utils.data import flatten
28 from IPython.utils.data import flatten
29
29
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31 # Code
31 # Code
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33
33
34 StringTypes = types.StringTypes
34 StringTypes = types.StringTypes
35
35
36
36
37 def unquote_ends(istr):
37 def unquote_ends(istr):
38 """Remove a single pair of quotes from the endpoints of a string."""
38 """Remove a single pair of quotes from the endpoints of a string."""
39
39
40 if not istr:
40 if not istr:
41 return istr
41 return istr
42 if (istr[0]=="'" and istr[-1]=="'") or \
42 if (istr[0]=="'" and istr[-1]=="'") or \
43 (istr[0]=='"' and istr[-1]=='"'):
43 (istr[0]=='"' and istr[-1]=='"'):
44 return istr[1:-1]
44 return istr[1:-1]
45 else:
45 else:
46 return istr
46 return istr
47
47
48
48
49 class LSString(str):
49 class LSString(str):
50 """String derivative with a special access attributes.
50 """String derivative with a special access attributes.
51
51
52 These are normal strings, but with the special attributes:
52 These are normal strings, but with the special attributes:
53
53
54 .l (or .list) : value as list (split on newlines).
54 .l (or .list) : value as list (split on newlines).
55 .n (or .nlstr): original value (the string itself).
55 .n (or .nlstr): original value (the string itself).
56 .s (or .spstr): value as whitespace-separated string.
56 .s (or .spstr): value as whitespace-separated string.
57 .p (or .paths): list of path objects
57 .p (or .paths): list of path objects
58
58
59 Any values which require transformations are computed only once and
59 Any values which require transformations are computed only once and
60 cached.
60 cached.
61
61
62 Such strings are very useful to efficiently interact with the shell, which
62 Such strings are very useful to efficiently interact with the shell, which
63 typically only understands whitespace-separated options for commands."""
63 typically only understands whitespace-separated options for commands."""
64
64
65 def get_list(self):
65 def get_list(self):
66 try:
66 try:
67 return self.__list
67 return self.__list
68 except AttributeError:
68 except AttributeError:
69 self.__list = self.split('\n')
69 self.__list = self.split('\n')
70 return self.__list
70 return self.__list
71
71
72 l = list = property(get_list)
72 l = list = property(get_list)
73
73
74 def get_spstr(self):
74 def get_spstr(self):
75 try:
75 try:
76 return self.__spstr
76 return self.__spstr
77 except AttributeError:
77 except AttributeError:
78 self.__spstr = self.replace('\n',' ')
78 self.__spstr = self.replace('\n',' ')
79 return self.__spstr
79 return self.__spstr
80
80
81 s = spstr = property(get_spstr)
81 s = spstr = property(get_spstr)
82
82
83 def get_nlstr(self):
83 def get_nlstr(self):
84 return self
84 return self
85
85
86 n = nlstr = property(get_nlstr)
86 n = nlstr = property(get_nlstr)
87
87
88 def get_paths(self):
88 def get_paths(self):
89 try:
89 try:
90 return self.__paths
90 return self.__paths
91 except AttributeError:
91 except AttributeError:
92 self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)]
92 self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)]
93 return self.__paths
93 return self.__paths
94
94
95 p = paths = property(get_paths)
95 p = paths = property(get_paths)
96
96
97
97
98 def print_lsstring(arg):
98 def print_lsstring(arg):
99 """ Prettier (non-repr-like) and more informative printer for LSString """
99 """ Prettier (non-repr-like) and more informative printer for LSString """
100 print "LSString (.p, .n, .l, .s available). Value:"
100 print "LSString (.p, .n, .l, .s available). Value:"
101 print arg
101 print arg
102
102
103
103
104 print_lsstring = result_display.when_type(LSString)(print_lsstring)
104 print_lsstring = result_display.when_type(LSString)(print_lsstring)
105
105
106
106
107 class SList(list):
107 class SList(list):
108 """List derivative with a special access attributes.
108 """List derivative with a special access attributes.
109
109
110 These are normal lists, but with the special attributes:
110 These are normal lists, but with the special attributes:
111
111
112 .l (or .list) : value as list (the list itself).
112 .l (or .list) : value as list (the list itself).
113 .n (or .nlstr): value as a string, joined on newlines.
113 .n (or .nlstr): value as a string, joined on newlines.
114 .s (or .spstr): value as a string, joined on spaces.
114 .s (or .spstr): value as a string, joined on spaces.
115 .p (or .paths): list of path objects
115 .p (or .paths): list of path objects
116
116
117 Any values which require transformations are computed only once and
117 Any values which require transformations are computed only once and
118 cached."""
118 cached."""
119
119
120 def get_list(self):
120 def get_list(self):
121 return self
121 return self
122
122
123 l = list = property(get_list)
123 l = list = property(get_list)
124
124
125 def get_spstr(self):
125 def get_spstr(self):
126 try:
126 try:
127 return self.__spstr
127 return self.__spstr
128 except AttributeError:
128 except AttributeError:
129 self.__spstr = ' '.join(self)
129 self.__spstr = ' '.join(self)
130 return self.__spstr
130 return self.__spstr
131
131
132 s = spstr = property(get_spstr)
132 s = spstr = property(get_spstr)
133
133
134 def get_nlstr(self):
134 def get_nlstr(self):
135 try:
135 try:
136 return self.__nlstr
136 return self.__nlstr
137 except AttributeError:
137 except AttributeError:
138 self.__nlstr = '\n'.join(self)
138 self.__nlstr = '\n'.join(self)
139 return self.__nlstr
139 return self.__nlstr
140
140
141 n = nlstr = property(get_nlstr)
141 n = nlstr = property(get_nlstr)
142
142
143 def get_paths(self):
143 def get_paths(self):
144 try:
144 try:
145 return self.__paths
145 return self.__paths
146 except AttributeError:
146 except AttributeError:
147 self.__paths = [path(p) for p in self if os.path.exists(p)]
147 self.__paths = [path(p) for p in self if os.path.exists(p)]
148 return self.__paths
148 return self.__paths
149
149
150 p = paths = property(get_paths)
150 p = paths = property(get_paths)
151
151
152 def grep(self, pattern, prune = False, field = None):
152 def grep(self, pattern, prune = False, field = None):
153 """ Return all strings matching 'pattern' (a regex or callable)
153 """ Return all strings matching 'pattern' (a regex or callable)
154
154
155 This is case-insensitive. If prune is true, return all items
155 This is case-insensitive. If prune is true, return all items
156 NOT matching the pattern.
156 NOT matching the pattern.
157
157
158 If field is specified, the match must occur in the specified
158 If field is specified, the match must occur in the specified
159 whitespace-separated field.
159 whitespace-separated field.
160
160
161 Examples::
161 Examples::
162
162
163 a.grep( lambda x: x.startswith('C') )
163 a.grep( lambda x: x.startswith('C') )
164 a.grep('Cha.*log', prune=1)
164 a.grep('Cha.*log', prune=1)
165 a.grep('chm', field=-1)
165 a.grep('chm', field=-1)
166 """
166 """
167
167
168 def match_target(s):
168 def match_target(s):
169 if field is None:
169 if field is None:
170 return s
170 return s
171 parts = s.split()
171 parts = s.split()
172 try:
172 try:
173 tgt = parts[field]
173 tgt = parts[field]
174 return tgt
174 return tgt
175 except IndexError:
175 except IndexError:
176 return ""
176 return ""
177
177
178 if isinstance(pattern, basestring):
178 if isinstance(pattern, basestring):
179 pred = lambda x : re.search(pattern, x, re.IGNORECASE)
179 pred = lambda x : re.search(pattern, x, re.IGNORECASE)
180 else:
180 else:
181 pred = pattern
181 pred = pattern
182 if not prune:
182 if not prune:
183 return SList([el for el in self if pred(match_target(el))])
183 return SList([el for el in self if pred(match_target(el))])
184 else:
184 else:
185 return SList([el for el in self if not pred(match_target(el))])
185 return SList([el for el in self if not pred(match_target(el))])
186
186
187 def fields(self, *fields):
187 def fields(self, *fields):
188 """ Collect whitespace-separated fields from string list
188 """ Collect whitespace-separated fields from string list
189
189
190 Allows quick awk-like usage of string lists.
190 Allows quick awk-like usage of string lists.
191
191
192 Example data (in var a, created by 'a = !ls -l')::
192 Example data (in var a, created by 'a = !ls -l')::
193 -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog
193 -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog
194 drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython
194 drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython
195
195
196 a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+']
196 a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+']
197 a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+']
197 a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+']
198 (note the joining by space).
198 (note the joining by space).
199 a.fields(-1) is ['ChangeLog', 'IPython']
199 a.fields(-1) is ['ChangeLog', 'IPython']
200
200
201 IndexErrors are ignored.
201 IndexErrors are ignored.
202
202
203 Without args, fields() just split()'s the strings.
203 Without args, fields() just split()'s the strings.
204 """
204 """
205 if len(fields) == 0:
205 if len(fields) == 0:
206 return [el.split() for el in self]
206 return [el.split() for el in self]
207
207
208 res = SList()
208 res = SList()
209 for el in [f.split() for f in self]:
209 for el in [f.split() for f in self]:
210 lineparts = []
210 lineparts = []
211
211
212 for fd in fields:
212 for fd in fields:
213 try:
213 try:
214 lineparts.append(el[fd])
214 lineparts.append(el[fd])
215 except IndexError:
215 except IndexError:
216 pass
216 pass
217 if lineparts:
217 if lineparts:
218 res.append(" ".join(lineparts))
218 res.append(" ".join(lineparts))
219
219
220 return res
220 return res
221
221
222 def sort(self,field= None, nums = False):
222 def sort(self,field= None, nums = False):
223 """ sort by specified fields (see fields())
223 """ sort by specified fields (see fields())
224
224
225 Example::
225 Example::
226 a.sort(1, nums = True)
226 a.sort(1, nums = True)
227
227
228 Sorts a by second field, in numerical order (so that 21 > 3)
228 Sorts a by second field, in numerical order (so that 21 > 3)
229
229
230 """
230 """
231
231
232 #decorate, sort, undecorate
232 #decorate, sort, undecorate
233 if field is not None:
233 if field is not None:
234 dsu = [[SList([line]).fields(field), line] for line in self]
234 dsu = [[SList([line]).fields(field), line] for line in self]
235 else:
235 else:
236 dsu = [[line, line] for line in self]
236 dsu = [[line, line] for line in self]
237 if nums:
237 if nums:
238 for i in range(len(dsu)):
238 for i in range(len(dsu)):
239 numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()])
239 numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()])
240 try:
240 try:
241 n = int(numstr)
241 n = int(numstr)
242 except ValueError:
242 except ValueError:
243 n = 0;
243 n = 0;
244 dsu[i][0] = n
244 dsu[i][0] = n
245
245
246
246
247 dsu.sort()
247 dsu.sort()
248 return SList([t[1] for t in dsu])
248 return SList([t[1] for t in dsu])
249
249
250
250
251 def print_slist(arg):
251 def print_slist(arg):
252 """ Prettier (non-repr-like) and more informative printer for SList """
252 """ Prettier (non-repr-like) and more informative printer for SList """
253 print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):"
253 print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):"
254 if hasattr(arg, 'hideonce') and arg.hideonce:
254 if hasattr(arg, 'hideonce') and arg.hideonce:
255 arg.hideonce = False
255 arg.hideonce = False
256 return
256 return
257
257
258 nlprint(arg)
258 nlprint(arg)
259
259
260
260
261 print_slist = result_display.when_type(SList)(print_slist)
261 print_slist = result_display.when_type(SList)(print_slist)
262
262
263
263
264 def esc_quotes(strng):
264 def esc_quotes(strng):
265 """Return the input string with single and double quotes escaped out"""
265 """Return the input string with single and double quotes escaped out"""
266
266
267 return strng.replace('"','\\"').replace("'","\\'")
267 return strng.replace('"','\\"').replace("'","\\'")
268
268
269
269
270 def make_quoted_expr(s):
270 def make_quoted_expr(s):
271 """Return string s in appropriate quotes, using raw string if possible.
271 """Return string s in appropriate quotes, using raw string if possible.
272
272
273 XXX - example removed because it caused encoding errors in documentation
273 XXX - example removed because it caused encoding errors in documentation
274 generation. We need a new example that doesn't contain invalid chars.
274 generation. We need a new example that doesn't contain invalid chars.
275
275
276 Note the use of raw string and padding at the end to allow trailing
276 Note the use of raw string and padding at the end to allow trailing
277 backslash.
277 backslash.
278 """
278 """
279
279
280 tail = ''
280 tail = ''
281 tailpadding = ''
281 tailpadding = ''
282 raw = ''
282 raw = ''
283 if "\\" in s:
283 if "\\" in s:
284 raw = 'r'
284 raw = 'r'
285 if s.endswith('\\'):
285 if s.endswith('\\'):
286 tail = '[:-1]'
286 tail = '[:-1]'
287 tailpadding = '_'
287 tailpadding = '_'
288 if '"' not in s:
288 if '"' not in s:
289 quote = '"'
289 quote = '"'
290 elif "'" not in s:
290 elif "'" not in s:
291 quote = "'"
291 quote = "'"
292 elif '"""' not in s and not s.endswith('"'):
292 elif '"""' not in s and not s.endswith('"'):
293 quote = '"""'
293 quote = '"""'
294 elif "'''" not in s and not s.endswith("'"):
294 elif "'''" not in s and not s.endswith("'"):
295 quote = "'''"
295 quote = "'''"
296 else:
296 else:
297 # give up, backslash-escaped string will do
297 # give up, backslash-escaped string will do
298 return '"%s"' % esc_quotes(s)
298 return '"%s"' % esc_quotes(s)
299 res = raw + quote + s + tailpadding + quote + tail
299 res = raw + quote + s + tailpadding + quote + tail
300 return res
300 return res
301
301
302
302
303 def qw(words,flat=0,sep=None,maxsplit=-1):
303 def qw(words,flat=0,sep=None,maxsplit=-1):
304 """Similar to Perl's qw() operator, but with some more options.
304 """Similar to Perl's qw() operator, but with some more options.
305
305
306 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
306 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
307
307
308 words can also be a list itself, and with flat=1, the output will be
308 words can also be a list itself, and with flat=1, the output will be
309 recursively flattened.
309 recursively flattened.
310
310
311 Examples:
311 Examples:
312
312
313 >>> qw('1 2')
313 >>> qw('1 2')
314 ['1', '2']
314 ['1', '2']
315
315
316 >>> qw(['a b','1 2',['m n','p q']])
316 >>> qw(['a b','1 2',['m n','p q']])
317 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
317 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
318
318
319 >>> qw(['a b','1 2',['m n','p q']],flat=1)
319 >>> qw(['a b','1 2',['m n','p q']],flat=1)
320 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q']
320 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q']
321 """
321 """
322
322
323 if type(words) in StringTypes:
323 if type(words) in StringTypes:
324 return [word.strip() for word in words.split(sep,maxsplit)
324 return [word.strip() for word in words.split(sep,maxsplit)
325 if word and not word.isspace() ]
325 if word and not word.isspace() ]
326 if flat:
326 if flat:
327 return flatten(map(qw,words,[1]*len(words)))
327 return flatten(map(qw,words,[1]*len(words)))
328 return map(qw,words)
328 return map(qw,words)
329
329
330
330
331 def qwflat(words,sep=None,maxsplit=-1):
331 def qwflat(words,sep=None,maxsplit=-1):
332 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
332 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
333 return qw(words,1,sep,maxsplit)
333 return qw(words,1,sep,maxsplit)
334
334
335
335
336 def qw_lol(indata):
336 def qw_lol(indata):
337 """qw_lol('a b') -> [['a','b']],
337 """qw_lol('a b') -> [['a','b']],
338 otherwise it's just a call to qw().
338 otherwise it's just a call to qw().
339
339
340 We need this to make sure the modules_some keys *always* end up as a
340 We need this to make sure the modules_some keys *always* end up as a
341 list of lists."""
341 list of lists."""
342
342
343 if type(indata) in StringTypes:
343 if type(indata) in StringTypes:
344 return [qw(indata)]
344 return [qw(indata)]
345 else:
345 else:
346 return qw(indata)
346 return qw(indata)
347
347
348
348
349 def grep(pat,list,case=1):
349 def grep(pat,list,case=1):
350 """Simple minded grep-like function.
350 """Simple minded grep-like function.
351 grep(pat,list) returns occurrences of pat in list, None on failure.
351 grep(pat,list) returns occurrences of pat in list, None on failure.
352
352
353 It only does simple string matching, with no support for regexps. Use the
353 It only does simple string matching, with no support for regexps. Use the
354 option case=0 for case-insensitive matching."""
354 option case=0 for case-insensitive matching."""
355
355
356 # This is pretty crude. At least it should implement copying only references
356 # This is pretty crude. At least it should implement copying only references
357 # to the original data in case it's big. Now it copies the data for output.
357 # to the original data in case it's big. Now it copies the data for output.
358 out=[]
358 out=[]
359 if case:
359 if case:
360 for term in list:
360 for term in list:
361 if term.find(pat)>-1: out.append(term)
361 if term.find(pat)>-1: out.append(term)
362 else:
362 else:
363 lpat=pat.lower()
363 lpat=pat.lower()
364 for term in list:
364 for term in list:
365 if term.lower().find(lpat)>-1: out.append(term)
365 if term.lower().find(lpat)>-1: out.append(term)
366
366
367 if len(out): return out
367 if len(out): return out
368 else: return None
368 else: return None
369
369
370
370
371 def dgrep(pat,*opts):
371 def dgrep(pat,*opts):
372 """Return grep() on dir()+dir(__builtins__).
372 """Return grep() on dir()+dir(__builtins__).
373
373
374 A very common use of grep() when working interactively."""
374 A very common use of grep() when working interactively."""
375
375
376 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
376 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
377
377
378
378
379 def idgrep(pat):
379 def idgrep(pat):
380 """Case-insensitive dgrep()"""
380 """Case-insensitive dgrep()"""
381
381
382 return dgrep(pat,0)
382 return dgrep(pat,0)
383
383
384
384
385 def igrep(pat,list):
385 def igrep(pat,list):
386 """Synonym for case-insensitive grep."""
386 """Synonym for case-insensitive grep."""
387
387
388 return grep(pat,list,case=0)
388 return grep(pat,list,case=0)
389
389
390
390
391 def indent(str,nspaces=4,ntabs=0):
391 def indent(str,nspaces=4,ntabs=0):
392 """Indent a string a given number of spaces or tabstops.
392 """Indent a string a given number of spaces or tabstops.
393
393
394 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
394 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
395 """
395 """
396 if str is None:
396 if str is None:
397 return
397 return
398 ind = '\t'*ntabs+' '*nspaces
398 ind = '\t'*ntabs+' '*nspaces
399 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
399 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
400 if outstr.endswith(os.linesep+ind):
400 if outstr.endswith(os.linesep+ind):
401 return outstr[:-len(ind)]
401 return outstr[:-len(ind)]
402 else:
402 else:
403 return outstr
403 return outstr
404
404
405 def native_line_ends(filename,backup=1):
405 def native_line_ends(filename,backup=1):
406 """Convert (in-place) a file to line-ends native to the current OS.
406 """Convert (in-place) a file to line-ends native to the current OS.
407
407
408 If the optional backup argument is given as false, no backup of the
408 If the optional backup argument is given as false, no backup of the
409 original file is left. """
409 original file is left. """
410
410
411 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
411 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
412
412
413 bak_filename = filename + backup_suffixes[os.name]
413 bak_filename = filename + backup_suffixes[os.name]
414
414
415 original = open(filename).read()
415 original = open(filename).read()
416 shutil.copy2(filename,bak_filename)
416 shutil.copy2(filename,bak_filename)
417 try:
417 try:
418 new = open(filename,'wb')
418 new = open(filename,'wb')
419 new.write(os.linesep.join(original.splitlines()))
419 new.write(os.linesep.join(original.splitlines()))
420 new.write(os.linesep) # ALWAYS put an eol at the end of the file
420 new.write(os.linesep) # ALWAYS put an eol at the end of the file
421 new.close()
421 new.close()
422 except:
422 except:
423 os.rename(bak_filename,filename)
423 os.rename(bak_filename,filename)
424 if not backup:
424 if not backup:
425 try:
425 try:
426 os.remove(bak_filename)
426 os.remove(bak_filename)
427 except:
427 except:
428 pass
428 pass
429
429
430
430
431 def list_strings(arg):
431 def list_strings(arg):
432 """Always return a list of strings, given a string or list of strings
432 """Always return a list of strings, given a string or list of strings
433 as input.
433 as input.
434
434
435 :Examples:
435 :Examples:
436
436
437 In [7]: list_strings('A single string')
437 In [7]: list_strings('A single string')
438 Out[7]: ['A single string']
438 Out[7]: ['A single string']
439
439
440 In [8]: list_strings(['A single string in a list'])
440 In [8]: list_strings(['A single string in a list'])
441 Out[8]: ['A single string in a list']
441 Out[8]: ['A single string in a list']
442
442
443 In [9]: list_strings(['A','list','of','strings'])
443 In [9]: list_strings(['A','list','of','strings'])
444 Out[9]: ['A', 'list', 'of', 'strings']
444 Out[9]: ['A', 'list', 'of', 'strings']
445 """
445 """
446
446
447 if isinstance(arg,basestring): return [arg]
447 if isinstance(arg,basestring): return [arg]
448 else: return arg
448 else: return arg
449
449
450
450
451 def marquee(txt='',width=78,mark='*'):
451 def marquee(txt='',width=78,mark='*'):
452 """Return the input string centered in a 'marquee'.
452 """Return the input string centered in a 'marquee'.
453
453
454 :Examples:
454 :Examples:
455
455
456 In [16]: marquee('A test',40)
456 In [16]: marquee('A test',40)
457 Out[16]: '**************** A test ****************'
457 Out[16]: '**************** A test ****************'
458
458
459 In [17]: marquee('A test',40,'-')
459 In [17]: marquee('A test',40,'-')
460 Out[17]: '---------------- A test ----------------'
460 Out[17]: '---------------- A test ----------------'
461
461
462 In [18]: marquee('A test',40,' ')
462 In [18]: marquee('A test',40,' ')
463 Out[18]: ' A test '
463 Out[18]: ' A test '
464
464
465 """
465 """
466 if not txt:
466 if not txt:
467 return (mark*width)[:width]
467 return (mark*width)[:width]
468 nmark = (width-len(txt)-2)/len(mark)/2
468 nmark = (width-len(txt)-2)/len(mark)/2
469 if nmark < 0: nmark =0
469 if nmark < 0: nmark =0
470 marks = mark*nmark
470 marks = mark*nmark
471 return '%s %s %s' % (marks,txt,marks)
471 return '%s %s %s' % (marks,txt,marks)
472
472
473
473
474 ini_spaces_re = re.compile(r'^(\s+)')
475
476 def num_ini_spaces(strng):
477 """Return the number of initial spaces in a string"""
478
479 ini_spaces = ini_spaces_re.match(strng)
480 if ini_spaces:
481 return ini_spaces.end()
482 else:
483 return 0
484
General Comments 0
You need to be logged in to leave comments. Login now