##// END OF EJS Templates
Complete reorganization of InteractiveShell....
Brian Granger -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -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 1 # Get the config being loaded so we can set attributes on it
2 2 c = get_config()
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Global options
6 6 #-----------------------------------------------------------------------------
7 7
8 8 # c.Global.display_banner = True
9 9
10 10 # c.Global.classic = False
11 11
12 12 # c.Global.nosep = True
13 13
14 14 # Set this to determine the detail of what is logged at startup.
15 15 # The default is 30 and possible values are 0,10,20,30,40,50.
16 16 # c.Global.log_level = 20
17 17
18 18 # This should be a list of importable Python modules that have an
19 19 # load_in_ipython(ip) method. This method gets called when the extension
20 20 # is loaded. You can put your extensions anywhere they can be imported
21 21 # but we add the extensions subdir of the ipython directory to sys.path
22 22 # during extension loading, so you can put them there as well.
23 23 # c.Global.extensions = [
24 24 # 'myextension'
25 25 # ]
26 26
27 27 # These lines are run in IPython in the user's namespace after extensions
28 28 # are loaded. They can contain full IPython syntax with magics etc.
29 29 # c.Global.exec_lines = [
30 30 # 'import numpy',
31 31 # 'a = 10; b = 20',
32 32 # '1/0'
33 33 # ]
34 34
35 35 # These files are run in IPython in the user's namespace. Files with a .py
36 36 # extension need to be pure Python. Files with a .ipy extension can have
37 37 # custom IPython syntax (like magics, etc.).
38 38 # These files need to be in the cwd, the ipython_dir or be absolute paths.
39 39 # c.Global.exec_files = [
40 40 # 'mycode.py',
41 41 # 'fancy.ipy'
42 42 # ]
43 43
44 44 #-----------------------------------------------------------------------------
45 45 # InteractiveShell options
46 46 #-----------------------------------------------------------------------------
47 47
48 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 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 60 # c.InteractiveShell.cache_size = 1000
61 61
62 62 # c.InteractiveShell.colors = 'LightBG'
63 63
64 64 # c.InteractiveShell.color_info = True
65 65
66 # c.InteractiveShell.confirm_exit = True
66 # c.TerminalInteractiveShell.confirm_exit = True
67 67
68 68 # c.InteractiveShell.deep_reload = False
69 69
70 # c.InteractiveShell.editor = 'nano'
70 # c.TerminalInteractiveShell.editor = 'nano'
71 71
72 72 # c.InteractiveShell.logstart = True
73 73
74 74 # c.InteractiveShell.logfile = u'ipython_log.py'
75 75
76 76 # c.InteractiveShell.logappend = u'mylog.py'
77 77
78 78 # c.InteractiveShell.object_info_string_level = 0
79 79
80 # c.InteractiveShell.pager = 'less'
80 # c.TerminalInteractiveShell.pager = 'less'
81 81
82 82 # c.InteractiveShell.pdb = False
83 83
84 84 # c.InteractiveShell.pprint = True
85 85
86 86 # c.InteractiveShell.prompt_in1 = 'In [\#]: '
87 87 # c.InteractiveShell.prompt_in2 = ' .\D.: '
88 88 # c.InteractiveShell.prompt_out = 'Out[\#]: '
89 89 # c.InteractiveShell.prompts_pad_left = True
90 90
91 91 # c.InteractiveShell.quiet = False
92 92
93 93 # Readline
94 94 # c.InteractiveShell.readline_use = True
95 95
96 96 # c.InteractiveShell.readline_parse_and_bind = [
97 97 # 'tab: complete',
98 98 # '"\C-l": possible-completions',
99 99 # 'set show-all-if-ambiguous on',
100 100 # '"\C-o": tab-insert',
101 101 # '"\M-i": " "',
102 102 # '"\M-o": "\d\d\d\d"',
103 103 # '"\M-I": "\d\d\d\d"',
104 104 # '"\C-r": reverse-search-history',
105 105 # '"\C-s": forward-search-history',
106 106 # '"\C-p": history-search-backward',
107 107 # '"\C-n": history-search-forward',
108 108 # '"\e[A": history-search-backward',
109 109 # '"\e[B": history-search-forward',
110 110 # '"\C-k": kill-line',
111 111 # '"\C-u": unix-line-discard',
112 112 # ]
113 113 # c.InteractiveShell.readline_remove_delims = '-/~'
114 114 # c.InteractiveShell.readline_merge_completions = True
115 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'
120 # c.InteractiveShell.separate_out = ''
121 # c.InteractiveShell.separate_out2 = ''
119 # c.TerminalInteractiveShell.separate_in = '\n'
120 # c.TerminalInteractiveShell.separate_out = ''
121 # c.TerminalInteractiveShell.separate_out2 = ''
122 122
123 123 # c.InteractiveShell.system_header = "IPython system call: "
124 124
125 125 # c.InteractiveShell.system_verbose = True
126 126
127 # c.InteractiveShell.term_title = False
127 # c.TerminalInteractiveShell.term_title = False
128 128
129 129 # c.InteractiveShell.wildcards_case_sensitive = True
130 130
131 131 # c.InteractiveShell.xmode = 'Context'
132 132
133 133 #-----------------------------------------------------------------------------
134 134 # PrefilterManager options
135 135 #-----------------------------------------------------------------------------
136 136
137 137 # c.PrefilterManager.multi_line_specials = True
138 138
139 139 #-----------------------------------------------------------------------------
140 140 # AliasManager options
141 141 #-----------------------------------------------------------------------------
142 142
143 143 # Do this to disable all defaults
144 144 # c.AliasManager.default_aliases = []
145 145
146 146 # c.AliasManager.user_aliases = [
147 147 # ('foo', 'echo Hi')
148 148 # ]
@@ -1,29 +1,29 b''
1 1 c = get_config()
2 2
3 3 # This can be used at any point in a config file to load a sub config
4 4 # and merge it into the current one.
5 5 load_subconfig('ipython_config.py')
6 6
7 7 c.InteractiveShell.prompt_in1 = '\C_LightGreen\u@\h\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> '
8 8 c.InteractiveShell.prompt_in2 = '\C_Green|\C_LightGreen\D\C_Green> '
9 9 c.InteractiveShell.prompt_out = '<\#> '
10 10
11 11 c.InteractiveShell.prompts_pad_left = True
12 12
13 c.InteractiveShell.separate_in = ''
14 c.InteractiveShell.separate_out = ''
15 c.InteractiveShell.separate_out2 = ''
13 c.TerminalInteractiveShell.separate_in = ''
14 c.TerminalInteractiveShell.separate_out = ''
15 c.TerminalInteractiveShell.separate_out2 = ''
16 16
17 17 c.PrefilterManager.multi_line_specials = True
18 18
19 19 lines = """
20 20 %rehashx
21 21 """
22 22
23 23 # You have to make sure that attributes that are containers already
24 24 # exist before using them. Simple assigning a new list will override
25 25 # all previous values.
26 26 if hasattr(c.Global, 'exec_lines'):
27 27 c.Global.exec_lines.append(lines)
28 28 else:
29 29 c.Global.exec_lines = [lines] No newline at end of file
This diff has been collapsed as it changes many lines, (637 lines changed) Show them Hide them
@@ -1,2523 +1,2024 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2010 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 from __future__ import with_statement
18 18 from __future__ import absolute_import
19 19
20 20 import __builtin__
21 21 import abc
22 import bdb
23 22 import codeop
24 23 import exceptions
25 24 import new
26 25 import os
27 26 import re
28 27 import string
29 28 import sys
30 29 import tempfile
31 30 from contextlib import nested
32 31
33 32 from IPython.core import debugger, oinspect
34 33 from IPython.core import history as ipcorehist
35 34 from IPython.core import prefilter
36 35 from IPython.core import shadowns
37 36 from IPython.core import ultratb
38 37 from IPython.core.alias import AliasManager
39 38 from IPython.core.builtin_trap import BuiltinTrap
40 39 from IPython.config.configurable import Configurable
41 40 from IPython.core.display_trap import DisplayTrap
42 from IPython.core.error import TryNext, UsageError
41 from IPython.core.error import UsageError
43 42 from IPython.core.extensions import ExtensionManager
44 43 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
44 from IPython.core.inputlist import InputList
45 45 from IPython.core.logger import Logger
46 46 from IPython.core.magic import Magic
47 47 from IPython.core.plugin import PluginManager
48 48 from IPython.core.prefilter import PrefilterManager
49 49 from IPython.core.prompts import CachedOutput
50 from IPython.core.usage import interactive_usage, default_banner
51 50 import IPython.core.hooks
52 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 52 from IPython.utils import PyColorize
57 53 from IPython.utils import pickleshare
58 54 from IPython.utils.doctestreload import doctest_reload
59 55 from IPython.utils.ipstruct import Struct
60 56 from IPython.utils.io import Term, ask_yes_no
61 57 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
62 from IPython.utils.process import (
63 abbrev_cwd,
64 getoutput,
65 getoutputerror
66 )
67 # import IPython.utils.rlineimpl as readline
58 from IPython.utils.process import getoutput, getoutputerror
68 59 from IPython.utils.strdispatch import StrDispatch
69 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 62 from IPython.utils.warn import warn, error, fatal
72 63 from IPython.utils.traitlets import (
73 64 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode, Instance
74 65 )
75 66
76 67 # from IPython.utils import growl
77 68 # growl.start("IPython")
78 69
79 70 #-----------------------------------------------------------------------------
80 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 74 # compiled regexps for autoindent management
88 75 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
89 76
90 77 #-----------------------------------------------------------------------------
91 78 # Utilities
92 79 #-----------------------------------------------------------------------------
93 80
94 ini_spaces_re = re.compile(r'^(\s+)')
95
96
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
81 # store the builtin raw_input globally, and use this always, in case user code
82 # overwrites it (like wx.py.PyShell does)
83 raw_input_original = raw_input
106 84
107 85 def softspace(file, newvalue):
108 86 """Copied from code.py, to remove the dependency"""
109 87
110 88 oldvalue = 0
111 89 try:
112 90 oldvalue = file.softspace
113 91 except AttributeError:
114 92 pass
115 93 try:
116 94 file.softspace = newvalue
117 95 except (AttributeError, TypeError):
118 96 # "attribute-less object" or "read-only attributes"
119 97 pass
120 98 return oldvalue
121 99
122 100
123 101 def no_op(*a, **kw): pass
124 102
125 103 class SpaceInInput(exceptions.Exception): pass
126 104
127 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 107 class SyntaxTB(ultratb.ListTB):
146 108 """Extension which holds some state: the last exception value"""
147 109
148 110 def __init__(self,color_scheme = 'NoColor'):
149 111 ultratb.ListTB.__init__(self,color_scheme)
150 112 self.last_syntax_error = None
151 113
152 114 def __call__(self, etype, value, elist):
153 115 self.last_syntax_error = value
154 116 ultratb.ListTB.__call__(self,etype,value,elist)
155 117
156 118 def clear_err_state(self):
157 119 """Return the current error state and clear it"""
158 120 e = self.last_syntax_error
159 121 self.last_syntax_error = None
160 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 125 def get_default_colors():
175 126 if sys.platform=='darwin':
176 127 return "LightBG"
177 128 elif os.name=='nt':
178 129 return 'Linux'
179 130 else:
180 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 135 # Main IPython class
197 136 #-----------------------------------------------------------------------------
198 137
199 138
200 139 class InteractiveShell(Configurable, Magic):
201 140 """An enhanced, interactive shell for Python."""
202 141
203 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 143 automagic = CBool(True, config=True)
207 banner = Str('')
208 banner1 = Str(default_banner, config=True)
209 banner2 = Str('', config=True)
210 144 cache_size = Int(1000, config=True)
211 145 color_info = CBool(True, config=True)
212 146 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
213 147 default_value=get_default_colors(), config=True)
214 confirm_exit = CBool(True, config=True)
215 148 debug = CBool(False, config=True)
216 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 150 filename = Str("<ipython console>")
227 151 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
228 152 logstart = CBool(False, config=True)
229 153 logfile = Str('', config=True)
230 154 logappend = Str('', config=True)
231 155 object_info_string_level = Enum((0,1,2), default_value=0,
232 156 config=True)
233 pager = Str('less', config=True)
234 157 pdb = CBool(False, config=True)
235 158 pprint = CBool(True, config=True)
236 159 profile = Str('', config=True)
237 160 prompt_in1 = Str('In [\\#]: ', config=True)
238 161 prompt_in2 = Str(' .\\D.: ', config=True)
239 162 prompt_out = Str('Out[\\#]: ', config=True)
240 163 prompts_pad_left = CBool(True, config=True)
241 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 168 readline_use = CBool(True, config=True)
244 169 readline_merge_completions = CBool(True, config=True)
245 170 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
246 171 readline_remove_delims = Str('-/~', config=True)
247 172 readline_parse_and_bind = List([
248 173 'tab: complete',
249 174 '"\C-l": clear-screen',
250 175 'set show-all-if-ambiguous on',
251 176 '"\C-o": tab-insert',
252 177 '"\M-i": " "',
253 178 '"\M-o": "\d\d\d\d"',
254 179 '"\M-I": "\d\d\d\d"',
255 180 '"\C-r": reverse-search-history',
256 181 '"\C-s": forward-search-history',
257 182 '"\C-p": history-search-backward',
258 183 '"\C-n": history-search-forward',
259 184 '"\e[A": history-search-backward',
260 185 '"\e[B": history-search-forward',
261 186 '"\C-k": kill-line',
262 187 '"\C-u": unix-line-discard',
263 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 190 system_header = Str('IPython system call: ', config=True)
273 191 system_verbose = CBool(False, config=True)
274 term_title = CBool(False, config=True)
275 192 wildcards_case_sensitive = CBool(True, config=True)
276 193 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
277 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 196 # Subcomponents of InteractiveShell
286 197 alias_manager = Instance('IPython.core.alias.AliasManager')
287 198 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
288 199 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
289 200 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
290 201 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
291 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 205 user_ns=None, user_global_ns=None,
295 banner1=None, banner2=None, display_banner=None,
296 206 custom_exceptions=((),None)):
297 207
298 208 # This is where traits with a config_key argument are updated
299 209 # from the values on config.
300 210 super(InteractiveShell, self).__init__(config=config)
301 211
302 212 # These are relatively independent and stateless
303 213 self.init_ipython_dir(ipython_dir)
304 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 216 # Create namespaces (user_ns, user_global_ns, etc.)
310 217 self.init_create_namespaces(user_ns, user_global_ns)
311 218 # This has to be done after init_create_namespaces because it uses
312 219 # something in self.user_ns, but before init_sys_modules, which
313 220 # is the first thing to modify sys.
314 221 self.save_sys_module_state()
315 222 self.init_sys_modules()
316 223
317 224 self.init_history()
318 225 self.init_encoding()
319 226 self.init_prefilter()
320 227
321 228 Magic.__init__(self, self)
322 229
323 230 self.init_syntax_highlighting()
324 231 self.init_hooks()
325 232 self.init_pushd_popd_magic()
326 233 self.init_traceback_handlers(custom_exceptions)
327 234 self.init_user_ns()
328 235 self.init_logger()
329 236 self.init_alias()
330 237 self.init_builtins()
331 238
332 239 # pre_config_initialization
333 240 self.init_shadow_hist()
334 241
335 242 # The next section should contain averything that was in ipmaker.
336 243 self.init_logstart()
337 244
338 245 # The following was in post_config_initialization
339 246 self.init_inspector()
340 247 self.init_readline()
341 248 self.init_prompts()
342 249 self.init_displayhook()
343 250 self.init_reload_doctest()
344 251 self.init_magics()
345 252 self.init_pdb()
346 253 self.init_extension_manager()
347 254 self.init_plugin_manager()
348 255 self.hooks.late_startup_hook()
349 256
350 257 @classmethod
351 258 def instance(cls, *args, **kwargs):
352 259 """Returns a global InteractiveShell instance."""
353 260 if not hasattr(cls, "_instance"):
354 261 cls._instance = cls(*args, **kwargs)
355 262 return cls._instance
356 263
357 264 @classmethod
358 265 def initialized(cls):
359 266 return hasattr(cls, "_instance")
360 267
361 268 def get_ipython(self):
362 269 """Return the currently running IPython instance."""
363 270 return self
364 271
365 272 #-------------------------------------------------------------------------
366 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 276 def _ipython_dir_changed(self, name, new):
376 277 if not os.path.isdir(new):
377 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 280 def set_autoindent(self,value=None):
391 281 """Set the autoindent flag, checking for readline support.
392 282
393 283 If called with no arguments, it acts as a toggle."""
394 284
395 285 if not self.has_readline:
396 286 if os.name == 'posix':
397 287 warn("The auto-indent feature requires the readline library")
398 288 self.autoindent = 0
399 289 return
400 290 if value is None:
401 291 self.autoindent = not self.autoindent
402 292 else:
403 293 self.autoindent = value
404 294
405 295 #-------------------------------------------------------------------------
406 296 # init_* methods called by __init__
407 297 #-------------------------------------------------------------------------
408 298
409 299 def init_ipython_dir(self, ipython_dir):
410 300 if ipython_dir is not None:
411 301 self.ipython_dir = ipython_dir
412 302 self.config.Global.ipython_dir = self.ipython_dir
413 303 return
414 304
415 305 if hasattr(self.config.Global, 'ipython_dir'):
416 306 self.ipython_dir = self.config.Global.ipython_dir
417 307 else:
418 308 self.ipython_dir = get_ipython_dir()
419 309
420 310 # All children can just read this
421 311 self.config.Global.ipython_dir = self.ipython_dir
422 312
423 313 def init_instance_attrs(self):
424 self.jobs = BackgroundJobManager()
425 314 self.more = False
426 315
427 316 # command compiler
428 317 self.compile = codeop.CommandCompiler()
429 318
430 319 # User input buffer
431 320 self.buffer = []
432 321
433 322 # Make an empty namespace, which extension writers can rely on both
434 323 # existing and NEVER being used by ipython itself. This gives them a
435 324 # convenient location for storing additional information and state
436 325 # their extensions may require, without fear of collisions with other
437 326 # ipython names that may develop later.
438 327 self.meta = Struct()
439 328
440 329 # Object variable to store code object waiting execution. This is
441 330 # used mainly by the multithreaded shells, but it can come in handy in
442 331 # other situations. No need to use a Queue here, since it's a single
443 332 # item which gets cleared once run.
444 333 self.code_to_run = None
445 334
446 # Flag to mark unconditional exit
447 self.exit_now = False
448
449 335 # Temporary files used for various purposes. Deleted at exit.
450 336 self.tempfiles = []
451 337
452 338 # Keep track of readline usage (later set by init_readline)
453 339 self.has_readline = False
454 340
455 341 # keep track of where we started running (mainly for crash post-mortem)
456 342 # This is not being used anywhere currently.
457 343 self.starting_dir = os.getcwd()
458 344
459 345 # Indentation management
460 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 348 def init_encoding(self):
477 349 # Get system encoding at startup time. Certain terminals (like Emacs
478 350 # under Win32 have it set to None, and we need to have a known valid
479 351 # encoding to use in the raw_input() method
480 352 try:
481 353 self.stdin_encoding = sys.stdin.encoding or 'ascii'
482 354 except AttributeError:
483 355 self.stdin_encoding = 'ascii'
484 356
485 357 def init_syntax_highlighting(self):
486 358 # Python source parser/formatter for syntax highlighting
487 359 pyformat = PyColorize.Parser().format
488 360 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
489 361
490 362 def init_pushd_popd_magic(self):
491 363 # for pushd/popd management
492 364 try:
493 365 self.home_dir = get_home_dir()
494 366 except HomeDirError, msg:
495 367 fatal(msg)
496 368
497 369 self.dir_stack = []
498 370
499 371 def init_logger(self):
500 372 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
501 373 # local shortcut, this is used a LOT
502 374 self.log = self.logger.log
503 375
504 376 def init_logstart(self):
505 377 if self.logappend:
506 378 self.magic_logstart(self.logappend + ' append')
507 379 elif self.logfile:
508 380 self.magic_logstart(self.logfile)
509 381 elif self.logstart:
510 382 self.magic_logstart()
511 383
512 384 def init_builtins(self):
513 385 self.builtin_trap = BuiltinTrap(shell=self)
514 386
515 387 def init_inspector(self):
516 388 # Object inspector
517 389 self.inspector = oinspect.Inspector(oinspect.InspectColors,
518 390 PyColorize.ANSICodeColors,
519 391 'NoColor',
520 392 self.object_info_string_level)
521 393
522 394 def init_prompts(self):
523 395 # Initialize cache, set in/out prompts and printing system
524 396 self.outputcache = CachedOutput(self,
525 397 self.cache_size,
526 398 self.pprint,
527 399 input_sep = self.separate_in,
528 400 output_sep = self.separate_out,
529 401 output_sep2 = self.separate_out2,
530 402 ps1 = self.prompt_in1,
531 403 ps2 = self.prompt_in2,
532 404 ps_out = self.prompt_out,
533 405 pad_left = self.prompts_pad_left)
534 406
535 407 # user may have over-ridden the default print hook:
536 408 try:
537 409 self.outputcache.__class__.display = self.hooks.display
538 410 except AttributeError:
539 411 pass
540 412
541 413 def init_displayhook(self):
542 414 self.display_trap = DisplayTrap(hook=self.outputcache)
543 415
544 416 def init_reload_doctest(self):
545 417 # Do a proper resetting of doctest, including the necessary displayhook
546 418 # monkeypatching
547 419 try:
548 420 doctest_reload()
549 421 except ImportError:
550 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 425 # Things related to injections into the sys module
579 426 #-------------------------------------------------------------------------
580 427
581 428 def save_sys_module_state(self):
582 429 """Save the state of hooks in the sys module.
583 430
584 431 This has to be called after self.user_ns is created.
585 432 """
586 433 self._orig_sys_module_state = {}
587 434 self._orig_sys_module_state['stdin'] = sys.stdin
588 435 self._orig_sys_module_state['stdout'] = sys.stdout
589 436 self._orig_sys_module_state['stderr'] = sys.stderr
590 437 self._orig_sys_module_state['excepthook'] = sys.excepthook
591 438 try:
592 439 self._orig_sys_modules_main_name = self.user_ns['__name__']
593 440 except KeyError:
594 441 pass
595 442
596 443 def restore_sys_module_state(self):
597 444 """Restore the state of the sys module."""
598 445 try:
599 446 for k, v in self._orig_sys_module_state.items():
600 447 setattr(sys, k, v)
601 448 except AttributeError:
602 449 pass
603 450 try:
604 451 delattr(sys, 'ipcompleter')
605 452 except AttributeError:
606 453 pass
607 454 # Reset what what done in self.init_sys_modules
608 455 try:
609 456 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
610 457 except (AttributeError, KeyError):
611 458 pass
612 459
613 460 #-------------------------------------------------------------------------
614 461 # Things related to hooks
615 462 #-------------------------------------------------------------------------
616 463
617 464 def init_hooks(self):
618 465 # hooks holds pointers used for user-side customizations
619 466 self.hooks = Struct()
620 467
621 468 self.strdispatchers = {}
622 469
623 470 # Set all default hooks, defined in the IPython.hooks module.
624 471 hooks = IPython.core.hooks
625 472 for hook_name in hooks.__all__:
626 473 # default hooks have priority 100, i.e. low; user hooks should have
627 474 # 0-100 priority
628 475 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
629 476
630 477 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
631 478 """set_hook(name,hook) -> sets an internal IPython hook.
632 479
633 480 IPython exposes some of its internal API as user-modifiable hooks. By
634 481 adding your function to one of these hooks, you can modify IPython's
635 482 behavior to call at runtime your own routines."""
636 483
637 484 # At some point in the future, this should validate the hook before it
638 485 # accepts it. Probably at least check that the hook takes the number
639 486 # of args it's supposed to.
640 487
641 488 f = new.instancemethod(hook,self,self.__class__)
642 489
643 490 # check if the hook is for strdispatcher first
644 491 if str_key is not None:
645 492 sdp = self.strdispatchers.get(name, StrDispatch())
646 493 sdp.add_s(str_key, f, priority )
647 494 self.strdispatchers[name] = sdp
648 495 return
649 496 if re_key is not None:
650 497 sdp = self.strdispatchers.get(name, StrDispatch())
651 498 sdp.add_re(re.compile(re_key), f, priority )
652 499 self.strdispatchers[name] = sdp
653 500 return
654 501
655 502 dp = getattr(self.hooks, name, None)
656 503 if name not in IPython.core.hooks.__all__:
657 504 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
658 505 if not dp:
659 506 dp = IPython.core.hooks.CommandChainDispatcher()
660 507
661 508 try:
662 509 dp.add(f,priority)
663 510 except AttributeError:
664 511 # it was not commandchain, plain old func - replace
665 512 dp = f
666 513
667 514 setattr(self.hooks,name, dp)
668 515
669 516 #-------------------------------------------------------------------------
670 517 # Things related to the "main" module
671 518 #-------------------------------------------------------------------------
672 519
673 520 def new_main_mod(self,ns=None):
674 521 """Return a new 'main' module object for user code execution.
675 522 """
676 523 main_mod = self._user_main_module
677 524 init_fakemod_dict(main_mod,ns)
678 525 return main_mod
679 526
680 527 def cache_main_mod(self,ns,fname):
681 528 """Cache a main module's namespace.
682 529
683 530 When scripts are executed via %run, we must keep a reference to the
684 531 namespace of their __main__ module (a FakeModule instance) around so
685 532 that Python doesn't clear it, rendering objects defined therein
686 533 useless.
687 534
688 535 This method keeps said reference in a private dict, keyed by the
689 536 absolute path of the module object (which corresponds to the script
690 537 path). This way, for multiple executions of the same script we only
691 538 keep one copy of the namespace (the last one), thus preventing memory
692 539 leaks from old references while allowing the objects from the last
693 540 execution to be accessible.
694 541
695 542 Note: we can not allow the actual FakeModule instances to be deleted,
696 543 because of how Python tears down modules (it hard-sets all their
697 544 references to None without regard for reference counts). This method
698 545 must therefore make a *copy* of the given namespace, to allow the
699 546 original module's __dict__ to be cleared and reused.
700 547
701 548
702 549 Parameters
703 550 ----------
704 551 ns : a namespace (a dict, typically)
705 552
706 553 fname : str
707 554 Filename associated with the namespace.
708 555
709 556 Examples
710 557 --------
711 558
712 559 In [10]: import IPython
713 560
714 561 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
715 562
716 563 In [12]: IPython.__file__ in _ip._main_ns_cache
717 564 Out[12]: True
718 565 """
719 566 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
720 567
721 568 def clear_main_mod_cache(self):
722 569 """Clear the cache of main modules.
723 570
724 571 Mainly for use by utilities like %reset.
725 572
726 573 Examples
727 574 --------
728 575
729 576 In [15]: import IPython
730 577
731 578 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
732 579
733 580 In [17]: len(_ip._main_ns_cache) > 0
734 581 Out[17]: True
735 582
736 583 In [18]: _ip.clear_main_mod_cache()
737 584
738 585 In [19]: len(_ip._main_ns_cache) == 0
739 586 Out[19]: True
740 587 """
741 588 self._main_ns_cache.clear()
742 589
743 590 #-------------------------------------------------------------------------
744 591 # Things related to debugging
745 592 #-------------------------------------------------------------------------
746 593
747 594 def init_pdb(self):
748 595 # Set calling of pdb on exceptions
749 596 # self.call_pdb is a property
750 597 self.call_pdb = self.pdb
751 598
752 599 def _get_call_pdb(self):
753 600 return self._call_pdb
754 601
755 602 def _set_call_pdb(self,val):
756 603
757 604 if val not in (0,1,False,True):
758 605 raise ValueError,'new call_pdb value must be boolean'
759 606
760 607 # store value in instance
761 608 self._call_pdb = val
762 609
763 610 # notify the actual exception handlers
764 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 613 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
772 614 'Control auto-activation of pdb at exceptions')
773 615
774 616 def debugger(self,force=False):
775 617 """Call the pydb/pdb debugger.
776 618
777 619 Keywords:
778 620
779 621 - force(False): by default, this routine checks the instance call_pdb
780 622 flag and does not actually invoke the debugger if the flag is false.
781 623 The 'force' option forces the debugger to activate even if the flag
782 624 is false.
783 625 """
784 626
785 627 if not (force or self.call_pdb):
786 628 return
787 629
788 630 if not hasattr(sys,'last_traceback'):
789 631 error('No traceback has been produced, nothing to debug.')
790 632 return
791 633
792 634 # use pydb if available
793 635 if debugger.has_pydb:
794 636 from pydb import pm
795 637 else:
796 638 # fallback to our internal debugger
797 639 pm = lambda : self.InteractiveTB.debugger(force=True)
798 640 self.history_saving_wrapper(pm)()
799 641
800 642 #-------------------------------------------------------------------------
801 643 # Things related to IPython's various namespaces
802 644 #-------------------------------------------------------------------------
803 645
804 646 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
805 647 # Create the namespace where the user will operate. user_ns is
806 648 # normally the only one used, and it is passed to the exec calls as
807 649 # the locals argument. But we do carry a user_global_ns namespace
808 650 # given as the exec 'globals' argument, This is useful in embedding
809 651 # situations where the ipython shell opens in a context where the
810 652 # distinction between locals and globals is meaningful. For
811 653 # non-embedded contexts, it is just the same object as the user_ns dict.
812 654
813 655 # FIXME. For some strange reason, __builtins__ is showing up at user
814 656 # level as a dict instead of a module. This is a manual fix, but I
815 657 # should really track down where the problem is coming from. Alex
816 658 # Schmolck reported this problem first.
817 659
818 660 # A useful post by Alex Martelli on this topic:
819 661 # Re: inconsistent value from __builtins__
820 662 # Von: Alex Martelli <aleaxit@yahoo.com>
821 663 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
822 664 # Gruppen: comp.lang.python
823 665
824 666 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
825 667 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
826 668 # > <type 'dict'>
827 669 # > >>> print type(__builtins__)
828 670 # > <type 'module'>
829 671 # > Is this difference in return value intentional?
830 672
831 673 # Well, it's documented that '__builtins__' can be either a dictionary
832 674 # or a module, and it's been that way for a long time. Whether it's
833 675 # intentional (or sensible), I don't know. In any case, the idea is
834 676 # that if you need to access the built-in namespace directly, you
835 677 # should start with "import __builtin__" (note, no 's') which will
836 678 # definitely give you a module. Yeah, it's somewhat confusing:-(.
837 679
838 680 # These routines return properly built dicts as needed by the rest of
839 681 # the code, and can also be used by extension writers to generate
840 682 # properly initialized namespaces.
841 683 user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns)
842 684
843 685 # Assign namespaces
844 686 # This is the namespace where all normal user variables live
845 687 self.user_ns = user_ns
846 688 self.user_global_ns = user_global_ns
847 689
848 690 # An auxiliary namespace that checks what parts of the user_ns were
849 691 # loaded at startup, so we can list later only variables defined in
850 692 # actual interactive use. Since it is always a subset of user_ns, it
851 693 # doesn't need to be separately tracked in the ns_table.
852 694 self.user_ns_hidden = {}
853 695
854 696 # A namespace to keep track of internal data structures to prevent
855 697 # them from cluttering user-visible stuff. Will be updated later
856 698 self.internal_ns = {}
857 699
858 700 # Now that FakeModule produces a real module, we've run into a nasty
859 701 # problem: after script execution (via %run), the module where the user
860 702 # code ran is deleted. Now that this object is a true module (needed
861 703 # so docetst and other tools work correctly), the Python module
862 704 # teardown mechanism runs over it, and sets to None every variable
863 705 # present in that module. Top-level references to objects from the
864 706 # script survive, because the user_ns is updated with them. However,
865 707 # calling functions defined in the script that use other things from
866 708 # the script will fail, because the function's closure had references
867 709 # to the original objects, which are now all None. So we must protect
868 710 # these modules from deletion by keeping a cache.
869 711 #
870 712 # To avoid keeping stale modules around (we only need the one from the
871 713 # last run), we use a dict keyed with the full path to the script, so
872 714 # only the last version of the module is held in the cache. Note,
873 715 # however, that we must cache the module *namespace contents* (their
874 716 # __dict__). Because if we try to cache the actual modules, old ones
875 717 # (uncached) could be destroyed while still holding references (such as
876 718 # those held by GUI objects that tend to be long-lived)>
877 719 #
878 720 # The %reset command will flush this cache. See the cache_main_mod()
879 721 # and clear_main_mod_cache() methods for details on use.
880 722
881 723 # This is the cache used for 'main' namespaces
882 724 self._main_ns_cache = {}
883 725 # And this is the single instance of FakeModule whose __dict__ we keep
884 726 # copying and clearing for reuse on each %run
885 727 self._user_main_module = FakeModule()
886 728
887 729 # A table holding all the namespaces IPython deals with, so that
888 730 # introspection facilities can search easily.
889 731 self.ns_table = {'user':user_ns,
890 732 'user_global':user_global_ns,
891 733 'internal':self.internal_ns,
892 734 'builtin':__builtin__.__dict__
893 735 }
894 736
895 737 # Similarly, track all namespaces where references can be held and that
896 738 # we can safely clear (so it can NOT include builtin). This one can be
897 739 # a simple list.
898 740 self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
899 741 self.internal_ns, self._main_ns_cache ]
900 742
901 743 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
902 744 """Return a valid local and global user interactive namespaces.
903 745
904 746 This builds a dict with the minimal information needed to operate as a
905 747 valid IPython user namespace, which you can pass to the various
906 748 embedding classes in ipython. The default implementation returns the
907 749 same dict for both the locals and the globals to allow functions to
908 750 refer to variables in the namespace. Customized implementations can
909 751 return different dicts. The locals dictionary can actually be anything
910 752 following the basic mapping protocol of a dict, but the globals dict
911 753 must be a true dict, not even a subclass. It is recommended that any
912 754 custom object for the locals namespace synchronize with the globals
913 755 dict somehow.
914 756
915 757 Raises TypeError if the provided globals namespace is not a true dict.
916 758
917 759 Parameters
918 760 ----------
919 761 user_ns : dict-like, optional
920 762 The current user namespace. The items in this namespace should
921 763 be included in the output. If None, an appropriate blank
922 764 namespace should be created.
923 765 user_global_ns : dict, optional
924 766 The current user global namespace. The items in this namespace
925 767 should be included in the output. If None, an appropriate
926 768 blank namespace should be created.
927 769
928 770 Returns
929 771 -------
930 772 A pair of dictionary-like object to be used as the local namespace
931 773 of the interpreter and a dict to be used as the global namespace.
932 774 """
933 775
934 776
935 777 # We must ensure that __builtin__ (without the final 's') is always
936 778 # available and pointing to the __builtin__ *module*. For more details:
937 779 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
938 780
939 781 if user_ns is None:
940 782 # Set __name__ to __main__ to better match the behavior of the
941 783 # normal interpreter.
942 784 user_ns = {'__name__' :'__main__',
943 785 '__builtin__' : __builtin__,
944 786 '__builtins__' : __builtin__,
945 787 }
946 788 else:
947 789 user_ns.setdefault('__name__','__main__')
948 790 user_ns.setdefault('__builtin__',__builtin__)
949 791 user_ns.setdefault('__builtins__',__builtin__)
950 792
951 793 if user_global_ns is None:
952 794 user_global_ns = user_ns
953 795 if type(user_global_ns) is not dict:
954 796 raise TypeError("user_global_ns must be a true dict; got %r"
955 797 % type(user_global_ns))
956 798
957 799 return user_ns, user_global_ns
958 800
959 801 def init_sys_modules(self):
960 802 # We need to insert into sys.modules something that looks like a
961 803 # module but which accesses the IPython namespace, for shelve and
962 804 # pickle to work interactively. Normally they rely on getting
963 805 # everything out of __main__, but for embedding purposes each IPython
964 806 # instance has its own private namespace, so we can't go shoving
965 807 # everything into __main__.
966 808
967 809 # note, however, that we should only do this for non-embedded
968 810 # ipythons, which really mimic the __main__.__dict__ with their own
969 811 # namespace. Embedded instances, on the other hand, should not do
970 812 # this because they need to manage the user local/global namespaces
971 813 # only, but they live within a 'normal' __main__ (meaning, they
972 814 # shouldn't overtake the execution environment of the script they're
973 815 # embedded in).
974 816
975 817 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
976 818
977 819 try:
978 820 main_name = self.user_ns['__name__']
979 821 except KeyError:
980 822 raise KeyError('user_ns dictionary MUST have a "__name__" key')
981 823 else:
982 824 sys.modules[main_name] = FakeModule(self.user_ns)
983 825
984 826 def init_user_ns(self):
985 827 """Initialize all user-visible namespaces to their minimum defaults.
986 828
987 829 Certain history lists are also initialized here, as they effectively
988 830 act as user namespaces.
989 831
990 832 Notes
991 833 -----
992 834 All data structures here are only filled in, they are NOT reset by this
993 835 method. If they were not empty before, data will simply be added to
994 836 therm.
995 837 """
996 838 # This function works in two parts: first we put a few things in
997 839 # user_ns, and we sync that contents into user_ns_hidden so that these
998 840 # initial variables aren't shown by %who. After the sync, we add the
999 841 # rest of what we *do* want the user to see with %who even on a new
1000 842 # session (probably nothing, so theye really only see their own stuff)
1001 843
1002 844 # The user dict must *always* have a __builtin__ reference to the
1003 845 # Python standard __builtin__ namespace, which must be imported.
1004 846 # This is so that certain operations in prompt evaluation can be
1005 847 # reliably executed with builtins. Note that we can NOT use
1006 848 # __builtins__ (note the 's'), because that can either be a dict or a
1007 849 # module, and can even mutate at runtime, depending on the context
1008 850 # (Python makes no guarantees on it). In contrast, __builtin__ is
1009 851 # always a module object, though it must be explicitly imported.
1010 852
1011 853 # For more details:
1012 854 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1013 855 ns = dict(__builtin__ = __builtin__)
1014 856
1015 857 # Put 'help' in the user namespace
1016 858 try:
1017 859 from site import _Helper
1018 860 ns['help'] = _Helper()
1019 861 except ImportError:
1020 862 warn('help() not available - check site.py')
1021 863
1022 864 # make global variables for user access to the histories
1023 865 ns['_ih'] = self.input_hist
1024 866 ns['_oh'] = self.output_hist
1025 867 ns['_dh'] = self.dir_hist
1026 868
1027 869 ns['_sh'] = shadowns
1028 870
1029 871 # user aliases to input and output histories. These shouldn't show up
1030 872 # in %who, as they can have very large reprs.
1031 873 ns['In'] = self.input_hist
1032 874 ns['Out'] = self.output_hist
1033 875
1034 876 # Store myself as the public api!!!
1035 877 ns['get_ipython'] = self.get_ipython
1036 878
1037 879 # Sync what we've added so far to user_ns_hidden so these aren't seen
1038 880 # by %who
1039 881 self.user_ns_hidden.update(ns)
1040 882
1041 883 # Anything put into ns now would show up in %who. Think twice before
1042 884 # putting anything here, as we really want %who to show the user their
1043 885 # stuff, not our variables.
1044 886
1045 887 # Finally, update the real user's namespace
1046 888 self.user_ns.update(ns)
1047 889
1048 890
1049 891 def reset(self):
1050 892 """Clear all internal namespaces.
1051 893
1052 894 Note that this is much more aggressive than %reset, since it clears
1053 895 fully all namespaces, as well as all input/output lists.
1054 896 """
1055 897 for ns in self.ns_refs_table:
1056 898 ns.clear()
1057 899
1058 900 self.alias_manager.clear_aliases()
1059 901
1060 902 # Clear input and output histories
1061 903 self.input_hist[:] = []
1062 904 self.input_hist_raw[:] = []
1063 905 self.output_hist.clear()
1064 906
1065 907 # Restore the user namespaces to minimal usability
1066 908 self.init_user_ns()
1067 909
1068 910 # Restore the default and user aliases
1069 911 self.alias_manager.init_aliases()
1070 912
1071 913 def reset_selective(self, regex=None):
1072 914 """Clear selective variables from internal namespaces based on a specified regular expression.
1073 915
1074 916 Parameters
1075 917 ----------
1076 918 regex : string or compiled pattern, optional
1077 919 A regular expression pattern that will be used in searching variable names in the users
1078 920 namespaces.
1079 921 """
1080 922 if regex is not None:
1081 923 try:
1082 924 m = re.compile(regex)
1083 925 except TypeError:
1084 926 raise TypeError('regex must be a string or compiled pattern')
1085 927 # Search for keys in each namespace that match the given regex
1086 928 # If a match is found, delete the key/value pair.
1087 929 for ns in self.ns_refs_table:
1088 930 for var in ns:
1089 931 if m.search(var):
1090 932 del ns[var]
1091 933
1092 934 def push(self, variables, interactive=True):
1093 935 """Inject a group of variables into the IPython user namespace.
1094 936
1095 937 Parameters
1096 938 ----------
1097 939 variables : dict, str or list/tuple of str
1098 940 The variables to inject into the user's namespace. If a dict,
1099 941 a simple update is done. If a str, the string is assumed to
1100 942 have variable names separated by spaces. A list/tuple of str
1101 943 can also be used to give the variable names. If just the variable
1102 944 names are give (list/tuple/str) then the variable values looked
1103 945 up in the callers frame.
1104 946 interactive : bool
1105 947 If True (default), the variables will be listed with the ``who``
1106 948 magic.
1107 949 """
1108 950 vdict = None
1109 951
1110 952 # We need a dict of name/value pairs to do namespace updates.
1111 953 if isinstance(variables, dict):
1112 954 vdict = variables
1113 955 elif isinstance(variables, (basestring, list, tuple)):
1114 956 if isinstance(variables, basestring):
1115 957 vlist = variables.split()
1116 958 else:
1117 959 vlist = variables
1118 960 vdict = {}
1119 961 cf = sys._getframe(1)
1120 962 for name in vlist:
1121 963 try:
1122 964 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1123 965 except:
1124 966 print ('Could not get variable %s from %s' %
1125 967 (name,cf.f_code.co_name))
1126 968 else:
1127 969 raise ValueError('variables must be a dict/str/list/tuple')
1128 970
1129 971 # Propagate variables to user namespace
1130 972 self.user_ns.update(vdict)
1131 973
1132 974 # And configure interactive visibility
1133 975 config_ns = self.user_ns_hidden
1134 976 if interactive:
1135 977 for name, val in vdict.iteritems():
1136 978 config_ns.pop(name, None)
1137 979 else:
1138 980 for name,val in vdict.iteritems():
1139 981 config_ns[name] = val
1140 982
1141 983 #-------------------------------------------------------------------------
1142 984 # Things related to history management
1143 985 #-------------------------------------------------------------------------
1144 986
1145 987 def init_history(self):
1146 988 # List of input with multi-line handling.
1147 989 self.input_hist = InputList()
1148 990 # This one will hold the 'raw' input history, without any
1149 991 # pre-processing. This will allow users to retrieve the input just as
1150 992 # it was exactly typed in by the user, with %hist -r.
1151 993 self.input_hist_raw = InputList()
1152 994
1153 995 # list of visited directories
1154 996 try:
1155 997 self.dir_hist = [os.getcwd()]
1156 998 except OSError:
1157 999 self.dir_hist = []
1158 1000
1159 1001 # dict of output history
1160 1002 self.output_hist = {}
1161 1003
1162 1004 # Now the history file
1163 1005 if self.profile:
1164 1006 histfname = 'history-%s' % self.profile
1165 1007 else:
1166 1008 histfname = 'history'
1167 1009 self.histfile = os.path.join(self.ipython_dir, histfname)
1168 1010
1169 1011 # Fill the history zero entry, user counter starts at 1
1170 1012 self.input_hist.append('\n')
1171 1013 self.input_hist_raw.append('\n')
1172 1014
1173 1015 def init_shadow_hist(self):
1174 1016 try:
1175 1017 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1176 1018 except exceptions.UnicodeDecodeError:
1177 1019 print "Your ipython_dir can't be decoded to unicode!"
1178 1020 print "Please set HOME environment variable to something that"
1179 1021 print r"only has ASCII characters, e.g. c:\home"
1180 1022 print "Now it is", self.ipython_dir
1181 1023 sys.exit()
1182 1024 self.shadowhist = ipcorehist.ShadowHist(self.db)
1183 1025
1184 1026 def savehist(self):
1185 1027 """Save input history to a file (via readline library)."""
1186 1028
1187 1029 try:
1188 1030 self.readline.write_history_file(self.histfile)
1189 1031 except:
1190 1032 print 'Unable to save IPython command history to file: ' + \
1191 1033 `self.histfile`
1192 1034
1193 1035 def reloadhist(self):
1194 1036 """Reload the input history from disk file."""
1195 1037
1196 1038 try:
1197 1039 self.readline.clear_history()
1198 1040 self.readline.read_history_file(self.shell.histfile)
1199 1041 except AttributeError:
1200 1042 pass
1201 1043
1202 1044 def history_saving_wrapper(self, func):
1203 1045 """ Wrap func for readline history saving
1204 1046
1205 1047 Convert func into callable that saves & restores
1206 1048 history around the call """
1207 1049
1208 1050 if self.has_readline:
1209 1051 from IPython.utils import rlineimpl as readline
1210 1052 else:
1211 1053 return func
1212 1054
1213 1055 def wrapper():
1214 1056 self.savehist()
1215 1057 try:
1216 1058 func()
1217 1059 finally:
1218 1060 readline.read_history_file(self.histfile)
1219 1061 return wrapper
1220 1062
1221 1063 #-------------------------------------------------------------------------
1222 1064 # Things related to exception handling and tracebacks (not debugging)
1223 1065 #-------------------------------------------------------------------------
1224 1066
1225 1067 def init_traceback_handlers(self, custom_exceptions):
1226 1068 # Syntax error handler.
1227 1069 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1228 1070
1229 1071 # The interactive one is initialized with an offset, meaning we always
1230 1072 # want to remove the topmost item in the traceback, which is our own
1231 1073 # internal code. Valid modes: ['Plain','Context','Verbose']
1232 1074 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1233 1075 color_scheme='NoColor',
1234 1076 tb_offset = 1)
1235 1077
1236 1078 # The instance will store a pointer to the system-wide exception hook,
1237 1079 # so that runtime code (such as magics) can access it. This is because
1238 1080 # during the read-eval loop, it may get temporarily overwritten.
1239 1081 self.sys_excepthook = sys.excepthook
1240 1082
1241 1083 # and add any custom exception handlers the user may have specified
1242 1084 self.set_custom_exc(*custom_exceptions)
1243 1085
1244 1086 # Set the exception mode
1245 1087 self.InteractiveTB.set_mode(mode=self.xmode)
1246 1088
1247 1089 def set_custom_exc(self,exc_tuple,handler):
1248 1090 """set_custom_exc(exc_tuple,handler)
1249 1091
1250 1092 Set a custom exception handler, which will be called if any of the
1251 1093 exceptions in exc_tuple occur in the mainloop (specifically, in the
1252 1094 runcode() method.
1253 1095
1254 1096 Inputs:
1255 1097
1256 1098 - exc_tuple: a *tuple* of valid exceptions to call the defined
1257 1099 handler for. It is very important that you use a tuple, and NOT A
1258 1100 LIST here, because of the way Python's except statement works. If
1259 1101 you only want to trap a single exception, use a singleton tuple:
1260 1102
1261 1103 exc_tuple == (MyCustomException,)
1262 1104
1263 1105 - handler: this must be defined as a function with the following
1264 1106 basic interface: def my_handler(self,etype,value,tb).
1265 1107
1266 1108 This will be made into an instance method (via new.instancemethod)
1267 1109 of IPython itself, and it will be called if any of the exceptions
1268 1110 listed in the exc_tuple are caught. If the handler is None, an
1269 1111 internal basic one is used, which just prints basic info.
1270 1112
1271 1113 WARNING: by putting in your own exception handler into IPython's main
1272 1114 execution loop, you run a very good chance of nasty crashes. This
1273 1115 facility should only be used if you really know what you are doing."""
1274 1116
1275 1117 assert type(exc_tuple)==type(()) , \
1276 1118 "The custom exceptions must be given AS A TUPLE."
1277 1119
1278 1120 def dummy_handler(self,etype,value,tb):
1279 1121 print '*** Simple custom exception handler ***'
1280 1122 print 'Exception type :',etype
1281 1123 print 'Exception value:',value
1282 1124 print 'Traceback :',tb
1283 1125 print 'Source code :','\n'.join(self.buffer)
1284 1126
1285 1127 if handler is None: handler = dummy_handler
1286 1128
1287 1129 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1288 1130 self.custom_exceptions = exc_tuple
1289 1131
1290 1132 def excepthook(self, etype, value, tb):
1291 1133 """One more defense for GUI apps that call sys.excepthook.
1292 1134
1293 1135 GUI frameworks like wxPython trap exceptions and call
1294 1136 sys.excepthook themselves. I guess this is a feature that
1295 1137 enables them to keep running after exceptions that would
1296 1138 otherwise kill their mainloop. This is a bother for IPython
1297 1139 which excepts to catch all of the program exceptions with a try:
1298 1140 except: statement.
1299 1141
1300 1142 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1301 1143 any app directly invokes sys.excepthook, it will look to the user like
1302 1144 IPython crashed. In order to work around this, we can disable the
1303 1145 CrashHandler and replace it with this excepthook instead, which prints a
1304 1146 regular traceback using our InteractiveTB. In this fashion, apps which
1305 1147 call sys.excepthook will generate a regular-looking exception from
1306 1148 IPython, and the CrashHandler will only be triggered by real IPython
1307 1149 crashes.
1308 1150
1309 1151 This hook should be used sparingly, only in places which are not likely
1310 1152 to be true IPython errors.
1311 1153 """
1312 1154 self.showtraceback((etype,value,tb),tb_offset=0)
1313 1155
1314 1156 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1315 1157 exception_only=False):
1316 1158 """Display the exception that just occurred.
1317 1159
1318 1160 If nothing is known about the exception, this is the method which
1319 1161 should be used throughout the code for presenting user tracebacks,
1320 1162 rather than directly invoking the InteractiveTB object.
1321 1163
1322 1164 A specific showsyntaxerror() also exists, but this method can take
1323 1165 care of calling it if needed, so unless you are explicitly catching a
1324 1166 SyntaxError exception, don't try to analyze the stack manually and
1325 1167 simply call this method."""
1326 1168
1327 1169 try:
1328 1170 if exc_tuple is None:
1329 1171 etype, value, tb = sys.exc_info()
1330 1172 else:
1331 1173 etype, value, tb = exc_tuple
1332 1174
1333 1175 if etype is None:
1334 1176 if hasattr(sys, 'last_type'):
1335 1177 etype, value, tb = sys.last_type, sys.last_value, \
1336 1178 sys.last_traceback
1337 1179 else:
1338 1180 self.write('No traceback available to show.\n')
1339 1181 return
1340 1182
1341 1183 if etype is SyntaxError:
1342 1184 # Though this won't be called by syntax errors in the input
1343 1185 # line, there may be SyntaxError cases whith imported code.
1344 1186 self.showsyntaxerror(filename)
1345 1187 elif etype is UsageError:
1346 1188 print "UsageError:", value
1347 1189 else:
1348 1190 # WARNING: these variables are somewhat deprecated and not
1349 1191 # necessarily safe to use in a threaded environment, but tools
1350 1192 # like pdb depend on their existence, so let's set them. If we
1351 1193 # find problems in the field, we'll need to revisit their use.
1352 1194 sys.last_type = etype
1353 1195 sys.last_value = value
1354 1196 sys.last_traceback = tb
1355 1197
1356 1198 if etype in self.custom_exceptions:
1357 1199 self.CustomTB(etype,value,tb)
1358 1200 else:
1359 1201 if exception_only:
1360 1202 m = ('An exception has occurred, use %tb to see the '
1361 1203 'full traceback.')
1362 1204 print m
1363 1205 self.InteractiveTB.show_exception_only(etype, value)
1364 1206 else:
1365 1207 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1366 1208 if self.InteractiveTB.call_pdb:
1367 1209 # pdb mucks up readline, fix it back
1368 1210 self.set_completer()
1369 1211
1370 1212 except KeyboardInterrupt:
1371 1213 self.write("\nKeyboardInterrupt\n")
1372 1214
1373 1215
1374 1216 def showsyntaxerror(self, filename=None):
1375 1217 """Display the syntax error that just occurred.
1376 1218
1377 1219 This doesn't display a stack trace because there isn't one.
1378 1220
1379 1221 If a filename is given, it is stuffed in the exception instead
1380 1222 of what was there before (because Python's parser always uses
1381 1223 "<string>" when reading from a string).
1382 1224 """
1383 1225 etype, value, last_traceback = sys.exc_info()
1384 1226
1385 1227 # See note about these variables in showtraceback() above
1386 1228 sys.last_type = etype
1387 1229 sys.last_value = value
1388 1230 sys.last_traceback = last_traceback
1389 1231
1390 1232 if filename and etype is SyntaxError:
1391 1233 # Work hard to stuff the correct filename in the exception
1392 1234 try:
1393 1235 msg, (dummy_filename, lineno, offset, line) = value
1394 1236 except:
1395 1237 # Not the format we expect; leave it alone
1396 1238 pass
1397 1239 else:
1398 1240 # Stuff in the right filename
1399 1241 try:
1400 1242 # Assume SyntaxError is a class exception
1401 1243 value = SyntaxError(msg, (filename, lineno, offset, line))
1402 1244 except:
1403 1245 # If that failed, assume SyntaxError is a string
1404 1246 value = msg, (filename, lineno, offset, line)
1405 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 1250 # Things related to tab completion
1467 1251 #-------------------------------------------------------------------------
1468 1252
1469 1253 def complete(self, text):
1470 1254 """Return a sorted list of all possible completions on text.
1471 1255
1472 1256 Inputs:
1473 1257
1474 1258 - text: a string of text to be completed on.
1475 1259
1476 1260 This is a wrapper around the completion mechanism, similar to what
1477 1261 readline does at the command line when the TAB key is hit. By
1478 1262 exposing it as a method, it can be used by other non-readline
1479 1263 environments (such as GUIs) for text completion.
1480 1264
1481 1265 Simple usage example:
1482 1266
1483 1267 In [7]: x = 'hello'
1484 1268
1485 1269 In [8]: x
1486 1270 Out[8]: 'hello'
1487 1271
1488 1272 In [9]: print x
1489 1273 hello
1490 1274
1491 1275 In [10]: _ip.complete('x.l')
1492 1276 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1493 1277 """
1494 1278
1495 1279 # Inject names into __builtin__ so we can complete on the added names.
1496 1280 with self.builtin_trap:
1497 1281 complete = self.Completer.complete
1498 1282 state = 0
1499 1283 # use a dict so we get unique keys, since ipyhton's multiple
1500 1284 # completers can return duplicates. When we make 2.4 a requirement,
1501 1285 # start using sets instead, which are faster.
1502 1286 comps = {}
1503 1287 while True:
1504 1288 newcomp = complete(text,state,line_buffer=text)
1505 1289 if newcomp is None:
1506 1290 break
1507 1291 comps[newcomp] = 1
1508 1292 state += 1
1509 1293 outcomps = comps.keys()
1510 1294 outcomps.sort()
1511 1295 #print "T:",text,"OC:",outcomps # dbg
1512 1296 #print "vars:",self.user_ns.keys()
1513 1297 return outcomps
1514 1298
1515 1299 def set_custom_completer(self,completer,pos=0):
1516 1300 """Adds a new custom completer function.
1517 1301
1518 1302 The position argument (defaults to 0) is the index in the completers
1519 1303 list where you want the completer to be inserted."""
1520 1304
1521 1305 newcomp = new.instancemethod(completer,self.Completer,
1522 1306 self.Completer.__class__)
1523 1307 self.Completer.matchers.insert(pos,newcomp)
1524 1308
1525 1309 def set_completer(self):
1526 1310 """Reset readline's completer to be our own."""
1527 1311 self.readline.set_completer(self.Completer.complete)
1528 1312
1529 1313 def set_completer_frame(self, frame=None):
1530 1314 """Set the frame of the completer."""
1531 1315 if frame:
1532 1316 self.Completer.namespace = frame.f_locals
1533 1317 self.Completer.global_namespace = frame.f_globals
1534 1318 else:
1535 1319 self.Completer.namespace = self.user_ns
1536 1320 self.Completer.global_namespace = self.user_global_ns
1537 1321
1538 1322 #-------------------------------------------------------------------------
1539 1323 # Things related to readline
1540 1324 #-------------------------------------------------------------------------
1541 1325
1542 1326 def init_readline(self):
1543 1327 """Command history completion/saving/reloading."""
1544 1328
1545 1329 if self.readline_use:
1546 1330 import IPython.utils.rlineimpl as readline
1547 1331
1548 1332 self.rl_next_input = None
1549 1333 self.rl_do_indent = False
1550 1334
1551 1335 if not self.readline_use or not readline.have_readline:
1552 1336 self.has_readline = False
1553 1337 self.readline = None
1554 1338 # Set a number of methods that depend on readline to be no-op
1555 1339 self.savehist = no_op
1556 1340 self.reloadhist = no_op
1557 1341 self.set_completer = no_op
1558 1342 self.set_custom_completer = no_op
1559 1343 self.set_completer_frame = no_op
1560 1344 warn('Readline services not available or not loaded.')
1561 1345 else:
1562 1346 self.has_readline = True
1563 1347 self.readline = readline
1564 1348 sys.modules['readline'] = readline
1565 1349 import atexit
1566 1350 from IPython.core.completer import IPCompleter
1567 1351 self.Completer = IPCompleter(self,
1568 1352 self.user_ns,
1569 1353 self.user_global_ns,
1570 1354 self.readline_omit__names,
1571 1355 self.alias_manager.alias_table)
1572 1356 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1573 1357 self.strdispatchers['complete_command'] = sdisp
1574 1358 self.Completer.custom_completers = sdisp
1575 1359 # Platform-specific configuration
1576 1360 if os.name == 'nt':
1577 1361 self.readline_startup_hook = readline.set_pre_input_hook
1578 1362 else:
1579 1363 self.readline_startup_hook = readline.set_startup_hook
1580 1364
1581 1365 # Load user's initrc file (readline config)
1582 1366 # Or if libedit is used, load editrc.
1583 1367 inputrc_name = os.environ.get('INPUTRC')
1584 1368 if inputrc_name is None:
1585 1369 home_dir = get_home_dir()
1586 1370 if home_dir is not None:
1587 1371 inputrc_name = '.inputrc'
1588 1372 if readline.uses_libedit:
1589 1373 inputrc_name = '.editrc'
1590 1374 inputrc_name = os.path.join(home_dir, inputrc_name)
1591 1375 if os.path.isfile(inputrc_name):
1592 1376 try:
1593 1377 readline.read_init_file(inputrc_name)
1594 1378 except:
1595 1379 warn('Problems reading readline initialization file <%s>'
1596 1380 % inputrc_name)
1597 1381
1598 1382 # save this in sys so embedded copies can restore it properly
1599 1383 sys.ipcompleter = self.Completer.complete
1600 1384 self.set_completer()
1601 1385
1602 1386 # Configure readline according to user's prefs
1603 1387 # This is only done if GNU readline is being used. If libedit
1604 1388 # is being used (as on Leopard) the readline config is
1605 1389 # not run as the syntax for libedit is different.
1606 1390 if not readline.uses_libedit:
1607 1391 for rlcommand in self.readline_parse_and_bind:
1608 1392 #print "loading rl:",rlcommand # dbg
1609 1393 readline.parse_and_bind(rlcommand)
1610 1394
1611 1395 # Remove some chars from the delimiters list. If we encounter
1612 1396 # unicode chars, discard them.
1613 1397 delims = readline.get_completer_delims().encode("ascii", "ignore")
1614 1398 delims = delims.translate(string._idmap,
1615 1399 self.readline_remove_delims)
1616 1400 readline.set_completer_delims(delims)
1617 1401 # otherwise we end up with a monster history after a while:
1618 1402 readline.set_history_length(1000)
1619 1403 try:
1620 1404 #print '*** Reading readline history' # dbg
1621 1405 readline.read_history_file(self.histfile)
1622 1406 except IOError:
1623 1407 pass # It doesn't exist yet.
1624 1408
1625 1409 atexit.register(self.atexit_operations)
1626 1410 del atexit
1627 1411
1628 1412 # Configure auto-indent for all platforms
1629 1413 self.set_autoindent(self.autoindent)
1630 1414
1631 1415 def set_next_input(self, s):
1632 1416 """ Sets the 'default' input string for the next command line.
1633 1417
1634 1418 Requires readline.
1635 1419
1636 1420 Example:
1637 1421
1638 1422 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1639 1423 [D:\ipython]|2> Hello Word_ # cursor is here
1640 1424 """
1641 1425
1642 1426 self.rl_next_input = s
1643 1427
1428 # Maybe move this to the terminal subclass?
1644 1429 def pre_readline(self):
1645 1430 """readline hook to be used at the start of each line.
1646 1431
1647 1432 Currently it handles auto-indent only."""
1648 1433
1649 #debugx('self.indent_current_nsp','pre_readline:')
1650
1651 1434 if self.rl_do_indent:
1652 1435 self.readline.insert_text(self._indent_current_str())
1653 1436 if self.rl_next_input is not None:
1654 1437 self.readline.insert_text(self.rl_next_input)
1655 1438 self.rl_next_input = None
1656 1439
1657 1440 def _indent_current_str(self):
1658 1441 """return the current level of indentation as a string"""
1659 1442 return self.indent_current_nsp * ' '
1660 1443
1661 1444 #-------------------------------------------------------------------------
1662 1445 # Things related to magics
1663 1446 #-------------------------------------------------------------------------
1664 1447
1665 1448 def init_magics(self):
1666 1449 # Set user colors (don't do it in the constructor above so that it
1667 1450 # doesn't crash if colors option is invalid)
1668 1451 self.magic_colors(self.colors)
1669 1452 # History was moved to a separate module
1670 1453 from . import history
1671 1454 history.init_ipython(self)
1672 1455
1673 1456 def magic(self,arg_s):
1674 1457 """Call a magic function by name.
1675 1458
1676 1459 Input: a string containing the name of the magic function to call and any
1677 1460 additional arguments to be passed to the magic.
1678 1461
1679 1462 magic('name -opt foo bar') is equivalent to typing at the ipython
1680 1463 prompt:
1681 1464
1682 1465 In[1]: %name -opt foo bar
1683 1466
1684 1467 To call a magic without arguments, simply use magic('name').
1685 1468
1686 1469 This provides a proper Python function to call IPython's magics in any
1687 1470 valid Python code you can type at the interpreter, including loops and
1688 1471 compound statements.
1689 1472 """
1690 1473 args = arg_s.split(' ',1)
1691 1474 magic_name = args[0]
1692 1475 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1693 1476
1694 1477 try:
1695 1478 magic_args = args[1]
1696 1479 except IndexError:
1697 1480 magic_args = ''
1698 1481 fn = getattr(self,'magic_'+magic_name,None)
1699 1482 if fn is None:
1700 1483 error("Magic function `%s` not found." % magic_name)
1701 1484 else:
1702 1485 magic_args = self.var_expand(magic_args,1)
1703 1486 with nested(self.builtin_trap,):
1704 1487 result = fn(magic_args)
1705 1488 return result
1706 1489
1707 1490 def define_magic(self, magicname, func):
1708 1491 """Expose own function as magic function for ipython
1709 1492
1710 1493 def foo_impl(self,parameter_s=''):
1711 1494 'My very own magic!. (Use docstrings, IPython reads them).'
1712 1495 print 'Magic function. Passed parameter is between < >:'
1713 1496 print '<%s>' % parameter_s
1714 1497 print 'The self object is:',self
1715 1498
1716 1499 self.define_magic('foo',foo_impl)
1717 1500 """
1718 1501
1719 1502 import new
1720 1503 im = new.instancemethod(func,self, self.__class__)
1721 1504 old = getattr(self, "magic_" + magicname, None)
1722 1505 setattr(self, "magic_" + magicname, im)
1723 1506 return old
1724 1507
1725 1508 #-------------------------------------------------------------------------
1726 1509 # Things related to macros
1727 1510 #-------------------------------------------------------------------------
1728 1511
1729 1512 def define_macro(self, name, themacro):
1730 1513 """Define a new macro
1731 1514
1732 1515 Parameters
1733 1516 ----------
1734 1517 name : str
1735 1518 The name of the macro.
1736 1519 themacro : str or Macro
1737 1520 The action to do upon invoking the macro. If a string, a new
1738 1521 Macro object is created by passing the string to it.
1739 1522 """
1740 1523
1741 1524 from IPython.core import macro
1742 1525
1743 1526 if isinstance(themacro, basestring):
1744 1527 themacro = macro.Macro(themacro)
1745 1528 if not isinstance(themacro, macro.Macro):
1746 1529 raise ValueError('A macro must be a string or a Macro instance.')
1747 1530 self.user_ns[name] = themacro
1748 1531
1749 1532 #-------------------------------------------------------------------------
1750 1533 # Things related to the running of system commands
1751 1534 #-------------------------------------------------------------------------
1752 1535
1753 1536 def system(self, cmd):
1754 1537 """Make a system call, using IPython."""
1755 1538 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1756 1539
1757 1540 #-------------------------------------------------------------------------
1758 1541 # Things related to aliases
1759 1542 #-------------------------------------------------------------------------
1760 1543
1761 1544 def init_alias(self):
1762 1545 self.alias_manager = AliasManager(shell=self, config=self.config)
1763 1546 self.ns_table['alias'] = self.alias_manager.alias_table,
1764 1547
1765 1548 #-------------------------------------------------------------------------
1766 1549 # Things related to extensions and plugins
1767 1550 #-------------------------------------------------------------------------
1768 1551
1769 1552 def init_extension_manager(self):
1770 1553 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1771 1554
1772 1555 def init_plugin_manager(self):
1773 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 1570 # Things related to the running of code
1777 1571 #-------------------------------------------------------------------------
1778 1572
1779 1573 def ex(self, cmd):
1780 1574 """Execute a normal python statement in user namespace."""
1781 1575 with nested(self.builtin_trap,):
1782 1576 exec cmd in self.user_global_ns, self.user_ns
1783 1577
1784 1578 def ev(self, expr):
1785 1579 """Evaluate python expression expr in user namespace.
1786 1580
1787 1581 Returns the result of evaluation
1788 1582 """
1789 1583 with nested(self.builtin_trap,):
1790 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 1586 def safe_execfile(self, fname, *where, **kw):
1964 1587 """A safe version of the builtin execfile().
1965 1588
1966 1589 This version will never throw an exception, but instead print
1967 1590 helpful error messages to the screen. This only works on pure
1968 1591 Python files with the .py extension.
1969 1592
1970 1593 Parameters
1971 1594 ----------
1972 1595 fname : string
1973 1596 The name of the file to be executed.
1974 1597 where : tuple
1975 1598 One or two namespaces, passed to execfile() as (globals,locals).
1976 1599 If only one is given, it is passed as both.
1977 1600 exit_ignore : bool (False)
1978 1601 If True, then silence SystemExit for non-zero status (it is always
1979 1602 silenced for zero status, as it is so common).
1980 1603 """
1981 1604 kw.setdefault('exit_ignore', False)
1982 1605
1983 1606 fname = os.path.abspath(os.path.expanduser(fname))
1984 1607
1985 1608 # Make sure we have a .py file
1986 1609 if not fname.endswith('.py'):
1987 1610 warn('File must end with .py to be run using execfile: <%s>' % fname)
1988 1611
1989 1612 # Make sure we can open the file
1990 1613 try:
1991 1614 with open(fname) as thefile:
1992 1615 pass
1993 1616 except:
1994 1617 warn('Could not open file <%s> for safe execution.' % fname)
1995 1618 return
1996 1619
1997 1620 # Find things also in current directory. This is needed to mimic the
1998 1621 # behavior of running a script from the system command line, where
1999 1622 # Python inserts the script's directory into sys.path
2000 1623 dname = os.path.dirname(fname)
2001 1624
2002 1625 with prepended_to_syspath(dname):
2003 1626 try:
2004 1627 execfile(fname,*where)
2005 1628 except SystemExit, status:
2006 1629 # If the call was made with 0 or None exit status (sys.exit(0)
2007 1630 # or sys.exit() ), don't bother showing a traceback, as both of
2008 1631 # these are considered normal by the OS:
2009 1632 # > python -c'import sys;sys.exit(0)'; echo $?
2010 1633 # 0
2011 1634 # > python -c'import sys;sys.exit()'; echo $?
2012 1635 # 0
2013 1636 # For other exit status, we show the exception unless
2014 1637 # explicitly silenced, but only in short form.
2015 1638 if status.code not in (0, None) and not kw['exit_ignore']:
2016 1639 self.showtraceback(exception_only=True)
2017 1640 except:
2018 1641 self.showtraceback()
2019 1642
2020 1643 def safe_execfile_ipy(self, fname):
2021 1644 """Like safe_execfile, but for .ipy files with IPython syntax.
2022 1645
2023 1646 Parameters
2024 1647 ----------
2025 1648 fname : str
2026 1649 The name of the file to execute. The filename must have a
2027 1650 .ipy extension.
2028 1651 """
2029 1652 fname = os.path.abspath(os.path.expanduser(fname))
2030 1653
2031 1654 # Make sure we have a .py file
2032 1655 if not fname.endswith('.ipy'):
2033 1656 warn('File must end with .py to be run using execfile: <%s>' % fname)
2034 1657
2035 1658 # Make sure we can open the file
2036 1659 try:
2037 1660 with open(fname) as thefile:
2038 1661 pass
2039 1662 except:
2040 1663 warn('Could not open file <%s> for safe execution.' % fname)
2041 1664 return
2042 1665
2043 1666 # Find things also in current directory. This is needed to mimic the
2044 1667 # behavior of running a script from the system command line, where
2045 1668 # Python inserts the script's directory into sys.path
2046 1669 dname = os.path.dirname(fname)
2047 1670
2048 1671 with prepended_to_syspath(dname):
2049 1672 try:
2050 1673 with open(fname) as thefile:
2051 1674 script = thefile.read()
2052 1675 # self.runlines currently captures all exceptions
2053 1676 # raise in user code. It would be nice if there were
2054 1677 # versions of runlines, execfile that did raise, so
2055 1678 # we could catch the errors.
2056 1679 self.runlines(script, clean=True)
2057 1680 except:
2058 1681 self.showtraceback()
2059 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 1684 def runlines(self, lines, clean=False):
2099 1685 """Run a string of one or more lines of source.
2100 1686
2101 1687 This method is capable of running a string containing multiple source
2102 1688 lines, as if they had been entered at the IPython prompt. Since it
2103 1689 exposes IPython's processing machinery, the given strings can contain
2104 1690 magic calls (%magic), special shell access (!cmd), etc.
2105 1691 """
2106 1692
2107 1693 if isinstance(lines, (list, tuple)):
2108 1694 lines = '\n'.join(lines)
2109 1695
2110 1696 if clean:
2111 lines = self.cleanup_ipy_script(lines)
1697 lines = self._cleanup_ipy_script(lines)
2112 1698
2113 1699 # We must start with a clean buffer, in case this is run from an
2114 1700 # interactive IPython session (via a magic, for example).
2115 1701 self.resetbuffer()
2116 1702 lines = lines.splitlines()
2117 1703 more = 0
2118 1704
2119 1705 with nested(self.builtin_trap, self.display_trap):
2120 1706 for line in lines:
2121 1707 # skip blank lines so we don't mess up the prompt counter, but do
2122 1708 # NOT skip even a blank line if we are in a code block (more is
2123 1709 # true)
2124 1710
2125 1711 if line or more:
2126 1712 # push to raw history, so hist line numbers stay in sync
2127 1713 self.input_hist_raw.append("# " + line + "\n")
2128 1714 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2129 1715 more = self.push_line(prefiltered)
2130 1716 # IPython's runsource returns None if there was an error
2131 1717 # compiling the code. This allows us to stop processing right
2132 1718 # away, so the user gets the error message at the right place.
2133 1719 if more is None:
2134 1720 break
2135 1721 else:
2136 1722 self.input_hist_raw.append("\n")
2137 1723 # final newline in case the input didn't have it, so that the code
2138 1724 # actually does get executed
2139 1725 if more:
2140 1726 self.push_line('\n')
2141 1727
2142 1728 def runsource(self, source, filename='<input>', symbol='single'):
2143 1729 """Compile and run some source in the interpreter.
2144 1730
2145 1731 Arguments are as for compile_command().
2146 1732
2147 1733 One several things can happen:
2148 1734
2149 1735 1) The input is incorrect; compile_command() raised an
2150 1736 exception (SyntaxError or OverflowError). A syntax traceback
2151 1737 will be printed by calling the showsyntaxerror() method.
2152 1738
2153 1739 2) The input is incomplete, and more input is required;
2154 1740 compile_command() returned None. Nothing happens.
2155 1741
2156 1742 3) The input is complete; compile_command() returned a code
2157 1743 object. The code is executed by calling self.runcode() (which
2158 1744 also handles run-time exceptions, except for SystemExit).
2159 1745
2160 1746 The return value is:
2161 1747
2162 1748 - True in case 2
2163 1749
2164 1750 - False in the other cases, unless an exception is raised, where
2165 1751 None is returned instead. This can be used by external callers to
2166 1752 know whether to continue feeding input or not.
2167 1753
2168 1754 The return value can be used to decide whether to use sys.ps1 or
2169 1755 sys.ps2 to prompt the next line."""
2170 1756
2171 1757 # if the source code has leading blanks, add 'if 1:\n' to it
2172 1758 # this allows execution of indented pasted code. It is tempting
2173 1759 # to add '\n' at the end of source to run commands like ' a=1'
2174 1760 # directly, but this fails for more complicated scenarios
2175 1761 source=source.encode(self.stdin_encoding)
2176 1762 if source[:1] in [' ', '\t']:
2177 1763 source = 'if 1:\n%s' % source
2178 1764
2179 1765 try:
2180 1766 code = self.compile(source,filename,symbol)
2181 1767 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2182 1768 # Case 1
2183 1769 self.showsyntaxerror(filename)
2184 1770 return None
2185 1771
2186 1772 if code is None:
2187 1773 # Case 2
2188 1774 return True
2189 1775
2190 1776 # Case 3
2191 1777 # We store the code object so that threaded shells and
2192 1778 # custom exception handlers can access all this info if needed.
2193 1779 # The source corresponding to this can be obtained from the
2194 1780 # buffer attribute as '\n'.join(self.buffer).
2195 1781 self.code_to_run = code
2196 1782 # now actually execute the code object
2197 1783 if self.runcode(code) == 0:
2198 1784 return False
2199 1785 else:
2200 1786 return None
2201 1787
2202 1788 def runcode(self,code_obj):
2203 1789 """Execute a code object.
2204 1790
2205 1791 When an exception occurs, self.showtraceback() is called to display a
2206 1792 traceback.
2207 1793
2208 1794 Return value: a flag indicating whether the code to be run completed
2209 1795 successfully:
2210 1796
2211 1797 - 0: successful execution.
2212 1798 - 1: an error occurred.
2213 1799 """
2214 1800
2215 1801 # Set our own excepthook in case the user code tries to call it
2216 1802 # directly, so that the IPython crash handler doesn't get triggered
2217 1803 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2218 1804
2219 1805 # we save the original sys.excepthook in the instance, in case config
2220 1806 # code (such as magics) needs access to it.
2221 1807 self.sys_excepthook = old_excepthook
2222 1808 outflag = 1 # happens in more places, so it's easier as default
2223 1809 try:
2224 1810 try:
2225 1811 self.hooks.pre_runcode_hook()
2226 1812 exec code_obj in self.user_global_ns, self.user_ns
2227 1813 finally:
2228 1814 # Reset our crash handler in place
2229 1815 sys.excepthook = old_excepthook
2230 1816 except SystemExit:
2231 1817 self.resetbuffer()
2232 1818 self.showtraceback(exception_only=True)
2233 1819 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2234 1820 except self.custom_exceptions:
2235 1821 etype,value,tb = sys.exc_info()
2236 1822 self.CustomTB(etype,value,tb)
2237 1823 except:
2238 1824 self.showtraceback()
2239 1825 else:
2240 1826 outflag = 0
2241 1827 if softspace(sys.stdout, 0):
2242 1828 print
2243 1829 # Flush out code object which has been run (and source)
2244 1830 self.code_to_run = None
2245 1831 return outflag
2246 1832
2247 1833 def push_line(self, line):
2248 1834 """Push a line to the interpreter.
2249 1835
2250 1836 The line should not have a trailing newline; it may have
2251 1837 internal newlines. The line is appended to a buffer and the
2252 1838 interpreter's runsource() method is called with the
2253 1839 concatenated contents of the buffer as source. If this
2254 1840 indicates that the command was executed or invalid, the buffer
2255 1841 is reset; otherwise, the command is incomplete, and the buffer
2256 1842 is left as it was after the line was appended. The return
2257 1843 value is 1 if more input is required, 0 if the line was dealt
2258 1844 with in some way (this is the same as runsource()).
2259 1845 """
2260 1846
2261 1847 # autoindent management should be done here, and not in the
2262 1848 # interactive loop, since that one is only seen by keyboard input. We
2263 1849 # need this done correctly even for code run via runlines (which uses
2264 1850 # push).
2265 1851
2266 1852 #print 'push line: <%s>' % line # dbg
2267 1853 for subline in line.splitlines():
2268 1854 self._autoindent_update(subline)
2269 1855 self.buffer.append(line)
2270 1856 more = self.runsource('\n'.join(self.buffer), self.filename)
2271 1857 if not more:
2272 1858 self.resetbuffer()
2273 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 1902 def _autoindent_update(self,line):
2276 1903 """Keep track of the indent level."""
2277 1904
2278 1905 #debugx('line')
2279 1906 #debugx('self.indent_current_nsp')
2280 1907 if self.autoindent:
2281 1908 if line:
2282 1909 inisp = num_ini_spaces(line)
2283 1910 if inisp < self.indent_current_nsp:
2284 1911 self.indent_current_nsp = inisp
2285 1912
2286 1913 if line[-1] == ':':
2287 1914 self.indent_current_nsp += 4
2288 1915 elif dedent_re.match(line):
2289 1916 self.indent_current_nsp -= 4
2290 1917 else:
2291 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):
2373 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
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
1924 def enable_pylab(self, gui=None):
1925 raise NotImplementedError('Implement enable_pylab in a subclass')
2378 1926
2379 1927 #-------------------------------------------------------------------------
2380 1928 # Utilities
2381 1929 #-------------------------------------------------------------------------
2382 1930
2383 1931 def getoutput(self, cmd):
2384 1932 return getoutput(self.var_expand(cmd,depth=2),
2385 1933 header=self.system_header,
2386 1934 verbose=self.system_verbose)
2387 1935
2388 1936 def getoutputerror(self, cmd):
2389 1937 return getoutputerror(self.var_expand(cmd,depth=2),
2390 1938 header=self.system_header,
2391 1939 verbose=self.system_verbose)
2392 1940
2393 1941 def var_expand(self,cmd,depth=0):
2394 1942 """Expand python variables in a string.
2395 1943
2396 1944 The depth argument indicates how many frames above the caller should
2397 1945 be walked to look for the local namespace where to expand variables.
2398 1946
2399 1947 The global namespace for expansion is always the user's interactive
2400 1948 namespace.
2401 1949 """
2402 1950
2403 1951 return str(ItplNS(cmd,
2404 1952 self.user_ns, # globals
2405 1953 # Skip our own frame in searching for locals:
2406 1954 sys._getframe(depth+1).f_locals # locals
2407 1955 ))
2408 1956
2409 1957 def mktempfile(self,data=None):
2410 1958 """Make a new tempfile and return its filename.
2411 1959
2412 1960 This makes a call to tempfile.mktemp, but it registers the created
2413 1961 filename internally so ipython cleans it up at exit time.
2414 1962
2415 1963 Optional inputs:
2416 1964
2417 1965 - data(None): if data is given, it gets written out to the temp file
2418 1966 immediately, and the file is closed again."""
2419 1967
2420 1968 filename = tempfile.mktemp('.py','ipython_edit_')
2421 1969 self.tempfiles.append(filename)
2422 1970
2423 1971 if data:
2424 1972 tmp_file = open(filename,'w')
2425 1973 tmp_file.write(data)
2426 1974 tmp_file.close()
2427 1975 return filename
2428 1976
1977 # TODO: This should be removed when Term is refactored.
2429 1978 def write(self,data):
2430 1979 """Write a string to the default output"""
2431 1980 Term.cout.write(data)
2432 1981
1982 # TODO: This should be removed when Term is refactored.
2433 1983 def write_err(self,data):
2434 1984 """Write a string to the default error output"""
2435 1985 Term.cerr.write(data)
2436 1986
2437 1987 def ask_yes_no(self,prompt,default=True):
2438 1988 if self.quiet:
2439 1989 return True
2440 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 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 1996 def atexit_operations(self):
2496 1997 """This will be executed at the time of exit.
2497 1998
2498 1999 Saving of persistent data should be performed here.
2499 2000 """
2500 2001 self.savehist()
2501 2002
2502 2003 # Cleanup all tempfiles left around
2503 2004 for tfile in self.tempfiles:
2504 2005 try:
2505 2006 os.unlink(tfile)
2506 2007 except OSError:
2507 2008 pass
2508 2009
2509 2010 # Clear all user namespaces to release all references cleanly.
2510 2011 self.reset()
2511 2012
2512 2013 # Run user hooks
2513 2014 self.hooks.shutdown_hook()
2514 2015
2515 2016 def cleanup(self):
2516 2017 self.restore_sys_module_state()
2517 2018
2518 2019
2519 2020 class InteractiveShellABC(object):
2520 2021 """An abstract base class for InteractiveShell."""
2521 2022 __metaclass__ = abc.ABCMeta
2522 2023
2523 2024 InteractiveShellABC.register(InteractiveShell)
@@ -1,30 +1,30 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 This module is *completely* deprecated and should no longer be used for
5 5 any purpose. Currently, we have a few parts of the core that have
6 6 not been componentized and thus, still rely on this module. When everything
7 7 has been made into a component, this module will be sent to deathrow.
8 8 """
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Copyright (C) 2008-2009 The IPython Development Team
12 12 #
13 13 # Distributed under the terms of the BSD License. The full license is in
14 14 # the file COPYING, distributed as part of this software.
15 15 #-----------------------------------------------------------------------------
16 16
17 17 #-----------------------------------------------------------------------------
18 18 # Imports
19 19 #-----------------------------------------------------------------------------
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Classes and functions
23 23 #-----------------------------------------------------------------------------
24 24
25 25
26 26 def get():
27 27 """Get the global InteractiveShell instance."""
28 from IPython.core.interactiveshell import InteractiveShell
29 return InteractiveShell.instance()
28 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
29 return TerminalInteractiveShell.instance()
30 30
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,62 +1,59 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3
4 4 def test_import_completer():
5 5 from IPython.core import completer
6 6
7 7 def test_import_crashhandler():
8 8 from IPython.core import crashhandler
9 9
10 10 def test_import_debugger():
11 11 from IPython.core import debugger
12 12
13 13 def test_import_fakemodule():
14 14 from IPython.core import fakemodule
15 15
16 16 def test_import_excolors():
17 17 from IPython.core import excolors
18 18
19 19 def test_import_history():
20 20 from IPython.core import history
21 21
22 22 def test_import_hooks():
23 23 from IPython.core import hooks
24 24
25 25 def test_import_ipapi():
26 26 from IPython.core import ipapi
27 27
28 28 def test_import_interactiveshell():
29 29 from IPython.core import interactiveshell
30 30
31 31 def test_import_logger():
32 32 from IPython.core import logger
33 33
34 34 def test_import_macro():
35 35 from IPython.core import macro
36 36
37 37 def test_import_magic():
38 38 from IPython.core import magic
39 39
40 40 def test_import_oinspect():
41 41 from IPython.core import oinspect
42 42
43 def test_import_outputtrap():
44 from IPython.core import outputtrap
45
46 43 def test_import_prefilter():
47 44 from IPython.core import prefilter
48 45
49 46 def test_import_prompts():
50 47 from IPython.core import prompts
51 48
52 49 def test_import_release():
53 50 from IPython.core import release
54 51
55 52 def test_import_shadowns():
56 53 from IPython.core import shadowns
57 54
58 55 def test_import_ultratb():
59 56 from IPython.core import ultratb
60 57
61 58 def test_import_usage():
62 59 from IPython.core import usage
1 NO CONTENT: file renamed from IPython/core/outputtrap.py to IPython/deathrow/outputtrap.py
@@ -1,275 +1,272 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 An embedded IPython shell.
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Fernando Perez
10 10
11 11 Notes
12 12 -----
13 13 """
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Copyright (C) 2008-2009 The IPython Development Team
17 17 #
18 18 # Distributed under the terms of the BSD License. The full license is in
19 19 # the file COPYING, distributed as part of this software.
20 20 #-----------------------------------------------------------------------------
21 21
22 22 #-----------------------------------------------------------------------------
23 23 # Imports
24 24 #-----------------------------------------------------------------------------
25 25
26 26 from __future__ import with_statement
27 27 import __main__
28 28
29 29 import sys
30 30 from contextlib import nested
31 31
32 32 from IPython.core import ultratb
33 from IPython.core.interactiveshell import InteractiveShell
33 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
34 34 from IPython.frontend.terminal.ipapp import load_default_config
35 35
36 36 from IPython.utils.traitlets import Bool, Str, CBool
37 37 from IPython.utils.io import ask_yes_no
38 38
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Classes and functions
42 42 #-----------------------------------------------------------------------------
43 43
44 44 # This is an additional magic that is exposed in embedded shells.
45 45 def kill_embedded(self,parameter_s=''):
46 46 """%kill_embedded : deactivate for good the current embedded IPython.
47 47
48 48 This function (after asking for confirmation) sets an internal flag so that
49 49 an embedded IPython will never activate again. This is useful to
50 50 permanently disable a shell that is being called inside a loop: once you've
51 51 figured out what you needed from it, you may then kill it and the program
52 52 will then continue to run without the interactive shell interfering again.
53 53 """
54 54
55 55 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
56 56 "(y/n)? [y/N] ",'n')
57 57 if kill:
58 58 self.embedded_active = False
59 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 64 dummy_mode = Bool(False)
65 65 exit_msg = Str('')
66 66 embedded = CBool(True)
67 67 embedded_active = CBool(True)
68 68 # Like the base class display_banner is not configurable, but here it
69 69 # is True by default.
70 70 display_banner = CBool(True)
71 71
72 def __init__(self, parent=None, config=None, ipython_dir=None, usage=None,
73 user_ns=None, user_global_ns=None,
74 banner1=None, banner2=None, display_banner=None,
75 custom_exceptions=((),None), exit_msg=''):
72 def __init__(self, config=None, ipython_dir=None, user_ns=None,
73 user_global_ns=None, custom_exceptions=((),None),
74 usage=None, banner1=None, banner2=None,
75 display_banner=None, exit_msg=u''):
76 76
77 77 self.save_sys_ipcompleter()
78 78
79 79 super(InteractiveShellEmbed,self).__init__(
80 parent=parent, config=config, ipython_dir=ipython_dir, usage=usage,
81 user_ns=user_ns, user_global_ns=user_global_ns,
82 banner1=banner1, banner2=banner2, display_banner=display_banner,
83 custom_exceptions=custom_exceptions)
80 config=config, ipython_dir=ipython_dir, user_ns=user_ns,
81 user_global_ns=user_global_ns, custom_exceptions=custom_exceptions,
82 usage=usage, banner1=banner1, banner2=banner2,
83 display_banner=display_banner
84 )
84 85
85 86 self.exit_msg = exit_msg
86 87 self.define_magic("kill_embedded", kill_embedded)
87 88
88 89 # don't use the ipython crash handler so that user exceptions aren't
89 90 # trapped
90 91 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
91 92 mode=self.xmode,
92 93 call_pdb=self.pdb)
93 94
94 95 self.restore_sys_ipcompleter()
95 96
96 97 def init_sys_modules(self):
97 98 pass
98 99
99 100 def save_sys_ipcompleter(self):
100 101 """Save readline completer status."""
101 102 try:
102 103 #print 'Save completer',sys.ipcompleter # dbg
103 104 self.sys_ipcompleter_orig = sys.ipcompleter
104 105 except:
105 106 pass # not nested with IPython
106 107
107 108 def restore_sys_ipcompleter(self):
108 109 """Restores the readline completer which was in place.
109 110
110 111 This allows embedded IPython within IPython not to disrupt the
111 112 parent's completion.
112 113 """
113 114 try:
114 115 self.readline.set_completer(self.sys_ipcompleter_orig)
115 116 sys.ipcompleter = self.sys_ipcompleter_orig
116 117 except:
117 118 pass
118 119
119 120 def __call__(self, header='', local_ns=None, global_ns=None, dummy=None,
120 121 stack_depth=1):
121 122 """Activate the interactive interpreter.
122 123
123 124 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
124 125 the interpreter shell with the given local and global namespaces, and
125 126 optionally print a header string at startup.
126 127
127 128 The shell can be globally activated/deactivated using the
128 129 set/get_dummy_mode methods. This allows you to turn off a shell used
129 130 for debugging globally.
130 131
131 132 However, *each* time you call the shell you can override the current
132 133 state of dummy_mode with the optional keyword parameter 'dummy'. For
133 134 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
134 135 can still have a specific call work by making it as IPShell(dummy=0).
135 136
136 137 The optional keyword parameter dummy controls whether the call
137 138 actually does anything.
138 139 """
139 140
140 141 # If the user has turned it off, go away
141 142 if not self.embedded_active:
142 143 return
143 144
144 145 # Normal exits from interactive mode set this flag, so the shell can't
145 146 # re-enter (it checks this variable at the start of interactive mode).
146 147 self.exit_now = False
147 148
148 149 # Allow the dummy parameter to override the global __dummy_mode
149 150 if dummy or (dummy != 0 and self.dummy_mode):
150 151 return
151 152
152 153 if self.has_readline:
153 154 self.set_completer()
154 155
155 156 # self.banner is auto computed
156 157 if header:
157 158 self.old_banner2 = self.banner2
158 159 self.banner2 = self.banner2 + '\n' + header + '\n'
159 160 else:
160 161 self.old_banner2 = ''
161 162
162 163 # Call the embedding code with a stack depth of 1 so it can skip over
163 164 # our call and get the original caller's namespaces.
164 165 self.mainloop(local_ns, global_ns, stack_depth=stack_depth)
165 166
166 167 self.banner2 = self.old_banner2
167 168
168 169 if self.exit_msg is not None:
169 170 print self.exit_msg
170 171
171 172 self.restore_sys_ipcompleter()
172 173
173 174 def mainloop(self, local_ns=None, global_ns=None, stack_depth=0,
174 175 display_banner=None):
175 176 """Embeds IPython into a running python program.
176 177
177 178 Input:
178 179
179 180 - header: An optional header message can be specified.
180 181
181 182 - local_ns, global_ns: working namespaces. If given as None, the
182 183 IPython-initialized one is updated with __main__.__dict__, so that
183 184 program variables become visible but user-specific configuration
184 185 remains possible.
185 186
186 187 - stack_depth: specifies how many levels in the stack to go to
187 188 looking for namespaces (when local_ns and global_ns are None). This
188 189 allows an intermediate caller to make sure that this function gets
189 190 the namespace from the intended level in the stack. By default (0)
190 191 it will get its locals and globals from the immediate caller.
191 192
192 193 Warning: it's possible to use this in a program which is being run by
193 194 IPython itself (via %run), but some funny things will happen (a few
194 195 globals get overwritten). In the future this will be cleaned up, as
195 196 there is no fundamental reason why it can't work perfectly."""
196 197
197 198 # Get locals and globals from caller
198 199 if local_ns is None or global_ns is None:
199 200 call_frame = sys._getframe(stack_depth).f_back
200 201
201 202 if local_ns is None:
202 203 local_ns = call_frame.f_locals
203 204 if global_ns is None:
204 205 global_ns = call_frame.f_globals
205 206
206 207 # Update namespaces and fire up interpreter
207 208
208 209 # The global one is easy, we can just throw it in
209 210 self.user_global_ns = global_ns
210 211
211 212 # but the user/local one is tricky: ipython needs it to store internal
212 213 # data, but we also need the locals. We'll copy locals in the user
213 214 # one, but will track what got copied so we can delete them at exit.
214 215 # This is so that a later embedded call doesn't see locals from a
215 216 # previous call (which most likely existed in a separate scope).
216 217 local_varnames = local_ns.keys()
217 218 self.user_ns.update(local_ns)
218 219 #self.user_ns['local_ns'] = local_ns # dbg
219 220
220 221 # Patch for global embedding to make sure that things don't overwrite
221 222 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
222 223 # FIXME. Test this a bit more carefully (the if.. is new)
223 224 if local_ns is None and global_ns is None:
224 225 self.user_global_ns.update(__main__.__dict__)
225 226
226 227 # make sure the tab-completer has the correct frame information, so it
227 228 # actually completes using the frame's locals/globals
228 229 self.set_completer_frame()
229 230
230 231 with nested(self.builtin_trap, self.display_trap):
231 232 self.interact(display_banner=display_banner)
232 233
233 234 # now, purge out the user namespace from anything we might have added
234 235 # from the caller's local namespace
235 236 delvar = self.user_ns.pop
236 237 for var in local_varnames:
237 238 delvar(var,None)
238 239
239 240
240 241 _embedded_shell = None
241 242
242 243
243 def embed(header='', config=None, usage=None, banner1=None, banner2=None,
244 display_banner=True, exit_msg=''):
244 def embed(**kwargs):
245 245 """Call this to embed IPython at the current point in your program.
246 246
247 247 The first invocation of this will create an :class:`InteractiveShellEmbed`
248 248 instance and then call it. Consecutive calls just call the already
249 249 created instance.
250 250
251 251 Here is a simple example::
252 252
253 253 from IPython import embed
254 254 a = 10
255 255 b = 20
256 256 embed('First time')
257 257 c = 30
258 258 d = 40
259 259 embed
260 260
261 261 Full customization can be done by passing a :class:`Struct` in as the
262 262 config argument.
263 263 """
264 config = kwargs.get('config')
265 header = kwargs.pop('header', u'')
264 266 if config is None:
265 267 config = load_default_config()
266 config.InteractiveShellEmbed = config.InteractiveShell
268 config.InteractiveShellEmbed = config.TerminalInteractiveShell
267 269 global _embedded_shell
268 270 if _embedded_shell is None:
269 _embedded_shell = InteractiveShellEmbed(
270 config=config, usage=usage,
271 banner1=banner1, banner2=banner2,
272 display_banner=display_banner, exit_msg=exit_msg
273 )
271 _embedded_shell = InteractiveShellEmbed(**kwargs)
274 272 _embedded_shell(header=header, stack_depth=2)
275
@@ -1,665 +1,665 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 The :class:`~IPython.core.application.Application` object for the command
5 5 line :command:`ipython` program.
6 6
7 7 Authors
8 8 -------
9 9
10 10 * Brian Granger
11 11 * Fernando Perez
12 12 """
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Copyright (C) 2008-2010 The IPython Development Team
16 16 #
17 17 # Distributed under the terms of the BSD License. The full license is in
18 18 # the file COPYING, distributed as part of this software.
19 19 #-----------------------------------------------------------------------------
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Imports
23 23 #-----------------------------------------------------------------------------
24 24
25 25 from __future__ import absolute_import
26 26
27 27 import logging
28 28 import os
29 29 import sys
30 30
31 31 from IPython.core import release
32 32 from IPython.core.crashhandler import CrashHandler
33 33 from IPython.core.application import Application, BaseAppConfigLoader
34 from IPython.core.interactiveshell import InteractiveShell
34 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
35 35 from IPython.config.loader import (
36 36 Config,
37 37 PyFileConfigLoader
38 38 )
39 39 from IPython.lib import inputhook
40 40 from IPython.utils.path import filefind, get_ipython_dir
41 41 from IPython.core import usage
42 42
43 43 #-----------------------------------------------------------------------------
44 44 # Globals, utilities and helpers
45 45 #-----------------------------------------------------------------------------
46 46
47 47 #: The default config file name for this application.
48 48 default_config_file_name = u'ipython_config.py'
49 49
50 50
51 51 class IPAppConfigLoader(BaseAppConfigLoader):
52 52
53 53 def _add_arguments(self):
54 54 super(IPAppConfigLoader, self)._add_arguments()
55 55 paa = self.parser.add_argument
56 56 paa('-p',
57 57 '--profile', dest='Global.profile', type=unicode,
58 58 help=
59 59 """The string name of the ipython profile to be used. Assume that your
60 60 config file is ipython_config-<name>.py (looks in current dir first,
61 61 then in IPYTHON_DIR). This is a quick way to keep and load multiple
62 62 config files for different tasks, especially if include your basic one
63 63 in your more specialized ones. You can keep a basic
64 64 IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which
65 65 include this one and load extra things for particular tasks.""",
66 66 metavar='Global.profile')
67 67 paa('--config-file',
68 68 dest='Global.config_file', type=unicode,
69 69 help=
70 70 """Set the config file name to override default. Normally IPython
71 71 loads ipython_config.py (from current directory) or
72 72 IPYTHON_DIR/ipython_config.py. If the loading of your config file
73 73 fails, IPython starts with a bare bones configuration (no modules
74 74 loaded at all).""",
75 75 metavar='Global.config_file')
76 76 paa('--autocall',
77 77 dest='InteractiveShell.autocall', type=int,
78 78 help=
79 79 """Make IPython automatically call any callable object even if you
80 80 didn't type explicit parentheses. For example, 'str 43' becomes
81 81 'str(43)' automatically. The value can be '0' to disable the feature,
82 82 '1' for 'smart' autocall, where it is not applied if there are no more
83 83 arguments on the line, and '2' for 'full' autocall, where all callable
84 84 objects are automatically called (even if no arguments are present).
85 85 The default is '1'.""",
86 86 metavar='InteractiveShell.autocall')
87 87 paa('--autoindent',
88 action='store_true', dest='InteractiveShell.autoindent',
88 action='store_true', dest='TerminalInteractiveShell.autoindent',
89 89 help='Turn on autoindenting.')
90 90 paa('--no-autoindent',
91 action='store_false', dest='InteractiveShell.autoindent',
91 action='store_false', dest='TerminalInteractiveShell.autoindent',
92 92 help='Turn off autoindenting.')
93 93 paa('--automagic',
94 94 action='store_true', dest='InteractiveShell.automagic',
95 95 help=
96 96 """Turn on the auto calling of magic commands. Type %%magic at the
97 97 IPython prompt for more information.""")
98 98 paa('--no-automagic',
99 99 action='store_false', dest='InteractiveShell.automagic',
100 100 help='Turn off the auto calling of magic commands.')
101 101 paa('--autoedit-syntax',
102 action='store_true', dest='InteractiveShell.autoedit_syntax',
102 action='store_true', dest='TerminalInteractiveShell.autoedit_syntax',
103 103 help='Turn on auto editing of files with syntax errors.')
104 104 paa('--no-autoedit-syntax',
105 action='store_false', dest='InteractiveShell.autoedit_syntax',
105 action='store_false', dest='TerminalInteractiveShell.autoedit_syntax',
106 106 help='Turn off auto editing of files with syntax errors.')
107 107 paa('--banner',
108 108 action='store_true', dest='Global.display_banner',
109 109 help='Display a banner upon starting IPython.')
110 110 paa('--no-banner',
111 111 action='store_false', dest='Global.display_banner',
112 112 help="Don't display a banner upon starting IPython.")
113 113 paa('--cache-size',
114 114 type=int, dest='InteractiveShell.cache_size',
115 115 help=
116 116 """Set the size of the output cache. The default is 1000, you can
117 117 change it permanently in your config file. Setting it to 0 completely
118 118 disables the caching system, and the minimum value accepted is 20 (if
119 119 you provide a value less than 20, it is reset to 0 and a warning is
120 120 issued). This limit is defined because otherwise you'll spend more
121 121 time re-flushing a too small cache than working""",
122 122 metavar='InteractiveShell.cache_size')
123 123 paa('--classic',
124 124 action='store_true', dest='Global.classic',
125 125 help="Gives IPython a similar feel to the classic Python prompt.")
126 126 paa('--colors',
127 127 type=str, dest='InteractiveShell.colors',
128 128 help="Set the color scheme (NoColor, Linux, and LightBG).",
129 129 metavar='InteractiveShell.colors')
130 130 paa('--color-info',
131 131 action='store_true', dest='InteractiveShell.color_info',
132 132 help=
133 133 """IPython can display information about objects via a set of func-
134 134 tions, and optionally can use colors for this, syntax highlighting
135 135 source code and various other elements. However, because this
136 136 information is passed through a pager (like 'less') and many pagers get
137 137 confused with color codes, this option is off by default. You can test
138 138 it and turn it on permanently in your ipython_config.py file if it
139 139 works for you. Test it and turn it on permanently if it works with
140 140 your system. The magic function %%color_info allows you to toggle this
141 141 inter- actively for testing.""")
142 142 paa('--no-color-info',
143 143 action='store_false', dest='InteractiveShell.color_info',
144 144 help="Disable using colors for info related things.")
145 145 paa('--confirm-exit',
146 action='store_true', dest='InteractiveShell.confirm_exit',
146 action='store_true', dest='TerminalInteractiveShell.confirm_exit',
147 147 help=
148 148 """Set to confirm when you try to exit IPython with an EOF (Control-D
149 149 in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or
150 150 '%%Exit', you can force a direct exit without any confirmation.""")
151 151 paa('--no-confirm-exit',
152 action='store_false', dest='InteractiveShell.confirm_exit',
152 action='store_false', dest='TerminalInteractiveShell.confirm_exit',
153 153 help="Don't prompt the user when exiting.")
154 154 paa('--deep-reload',
155 155 action='store_true', dest='InteractiveShell.deep_reload',
156 156 help=
157 157 """Enable deep (recursive) reloading by default. IPython can use the
158 158 deep_reload module which reloads changes in modules recursively (it
159 159 replaces the reload() function, so you don't need to change anything to
160 160 use it). deep_reload() forces a full reload of modules whose code may
161 161 have changed, which the default reload() function does not. When
162 162 deep_reload is off, IPython will use the normal reload(), but
163 163 deep_reload will still be available as dreload(). This fea- ture is off
164 164 by default [which means that you have both normal reload() and
165 165 dreload()].""")
166 166 paa('--no-deep-reload',
167 167 action='store_false', dest='InteractiveShell.deep_reload',
168 168 help="Disable deep (recursive) reloading by default.")
169 169 paa('--editor',
170 type=str, dest='InteractiveShell.editor',
170 type=str, dest='TerminalInteractiveShell.editor',
171 171 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
172 metavar='InteractiveShell.editor')
172 metavar='TerminalInteractiveShell.editor')
173 173 paa('--log','-l',
174 174 action='store_true', dest='InteractiveShell.logstart',
175 175 help="Start logging to the default log file (./ipython_log.py).")
176 176 paa('--logfile','-lf',
177 177 type=unicode, dest='InteractiveShell.logfile',
178 178 help="Start logging to logfile with this name.",
179 179 metavar='InteractiveShell.logfile')
180 180 paa('--log-append','-la',
181 181 type=unicode, dest='InteractiveShell.logappend',
182 182 help="Start logging to the given file in append mode.",
183 183 metavar='InteractiveShell.logfile')
184 184 paa('--pdb',
185 185 action='store_true', dest='InteractiveShell.pdb',
186 186 help="Enable auto calling the pdb debugger after every exception.")
187 187 paa('--no-pdb',
188 188 action='store_false', dest='InteractiveShell.pdb',
189 189 help="Disable auto calling the pdb debugger after every exception.")
190 190 paa('--pprint',
191 191 action='store_true', dest='InteractiveShell.pprint',
192 192 help="Enable auto pretty printing of results.")
193 193 paa('--no-pprint',
194 194 action='store_false', dest='InteractiveShell.pprint',
195 195 help="Disable auto auto pretty printing of results.")
196 196 paa('--prompt-in1','-pi1',
197 197 type=str, dest='InteractiveShell.prompt_in1',
198 198 help=
199 199 """Set the main input prompt ('In [\#]: '). Note that if you are using
200 200 numbered prompts, the number is represented with a '\#' in the string.
201 201 Don't forget to quote strings with spaces embedded in them. Most
202 202 bash-like escapes can be used to customize IPython's prompts, as well
203 203 as a few additional ones which are IPython-spe- cific. All valid
204 204 prompt escapes are described in detail in the Customization section of
205 205 the IPython manual.""",
206 206 metavar='InteractiveShell.prompt_in1')
207 207 paa('--prompt-in2','-pi2',
208 208 type=str, dest='InteractiveShell.prompt_in2',
209 209 help=
210 210 """Set the secondary input prompt (' .\D.: '). Similar to the previous
211 211 option, but used for the continuation prompts. The special sequence
212 212 '\D' is similar to '\#', but with all digits replaced by dots (so you
213 213 can have your continuation prompt aligned with your input prompt).
214 214 Default: ' .\D.: ' (note three spaces at the start for alignment with
215 215 'In [\#]')""",
216 216 metavar='InteractiveShell.prompt_in2')
217 217 paa('--prompt-out','-po',
218 218 type=str, dest='InteractiveShell.prompt_out',
219 219 help="Set the output prompt ('Out[\#]:')",
220 220 metavar='InteractiveShell.prompt_out')
221 221 paa('--quick',
222 222 action='store_true', dest='Global.quick',
223 223 help="Enable quick startup with no config files.")
224 224 paa('--readline',
225 225 action='store_true', dest='InteractiveShell.readline_use',
226 226 help="Enable readline for command line usage.")
227 227 paa('--no-readline',
228 228 action='store_false', dest='InteractiveShell.readline_use',
229 229 help="Disable readline for command line usage.")
230 230 paa('--screen-length','-sl',
231 type=int, dest='InteractiveShell.screen_length',
231 type=int, dest='TerminalInteractiveShell.screen_length',
232 232 help=
233 233 """Number of lines of your screen, used to control printing of very
234 234 long strings. Strings longer than this number of lines will be sent
235 235 through a pager instead of directly printed. The default value for
236 236 this is 0, which means IPython will auto-detect your screen size every
237 237 time it needs to print certain potentially long strings (this doesn't
238 238 change the behavior of the 'print' keyword, it's only triggered
239 239 internally). If for some reason this isn't working well (it needs
240 240 curses support), specify it yourself. Otherwise don't change the
241 241 default.""",
242 metavar='InteractiveShell.screen_length')
242 metavar='TerminalInteractiveShell.screen_length')
243 243 paa('--separate-in','-si',
244 type=str, dest='InteractiveShell.separate_in',
244 type=str, dest='TerminalInteractiveShell.separate_in',
245 245 help="Separator before input prompts. Default '\\n'.",
246 metavar='InteractiveShell.separate_in')
246 metavar='TerminalInteractiveShell.separate_in')
247 247 paa('--separate-out','-so',
248 type=str, dest='InteractiveShell.separate_out',
248 type=str, dest='TerminalInteractiveShell.separate_out',
249 249 help="Separator before output prompts. Default 0 (nothing).",
250 metavar='InteractiveShell.separate_out')
250 metavar='TerminalInteractiveShell.separate_out')
251 251 paa('--separate-out2','-so2',
252 type=str, dest='InteractiveShell.separate_out2',
252 type=str, dest='TerminalInteractiveShell.separate_out2',
253 253 help="Separator after output prompts. Default 0 (nonight).",
254 metavar='InteractiveShell.separate_out2')
254 metavar='TerminalInteractiveShell.separate_out2')
255 255 paa('--no-sep',
256 256 action='store_true', dest='Global.nosep',
257 257 help="Eliminate all spacing between prompts.")
258 258 paa('--term-title',
259 action='store_true', dest='InteractiveShell.term_title',
259 action='store_true', dest='TerminalInteractiveShell.term_title',
260 260 help="Enable auto setting the terminal title.")
261 261 paa('--no-term-title',
262 action='store_false', dest='InteractiveShell.term_title',
262 action='store_false', dest='TerminalInteractiveShell.term_title',
263 263 help="Disable auto setting the terminal title.")
264 264 paa('--xmode',
265 265 type=str, dest='InteractiveShell.xmode',
266 266 help=
267 267 """Exception reporting mode ('Plain','Context','Verbose'). Plain:
268 268 similar to python's normal traceback printing. Context: prints 5 lines
269 269 of context source code around each line in the traceback. Verbose:
270 270 similar to Context, but additionally prints the variables currently
271 271 visible where the exception happened (shortening their strings if too
272 272 long). This can potentially be very slow, if you happen to have a huge
273 273 data structure whose string representation is complex to compute.
274 274 Your computer may appear to freeze for a while with cpu usage at 100%%.
275 275 If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting
276 276 it more than once).
277 277 """,
278 278 metavar='InteractiveShell.xmode')
279 279 paa('--ext',
280 280 type=str, dest='Global.extra_extension',
281 281 help="The dotted module name of an IPython extension to load.",
282 282 metavar='Global.extra_extension')
283 283 paa('-c',
284 284 type=str, dest='Global.code_to_run',
285 285 help="Execute the given command string.",
286 286 metavar='Global.code_to_run')
287 287 paa('-i',
288 288 action='store_true', dest='Global.force_interact',
289 289 help=
290 290 "If running code from the command line, become interactive afterwards.")
291 291
292 292 # Options to start with GUI control enabled from the beginning
293 293 paa('--gui',
294 294 type=str, dest='Global.gui',
295 295 help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
296 296 metavar='gui-mode')
297 297 paa('--pylab','-pylab',
298 298 type=str, dest='Global.pylab',
299 299 nargs='?', const='auto', metavar='gui-mode',
300 300 help="Pre-load matplotlib and numpy for interactive use. "+
301 301 "If no value is given, the gui backend is matplotlib's, else use "+
302 302 "one of: ['tk', 'qt', 'wx', 'gtk'].")
303 303
304 304 # Legacy GUI options. Leave them in for backwards compatibility, but the
305 305 # 'thread' names are really a misnomer now.
306 306 paa('--wthread', '-wthread',
307 307 action='store_true', dest='Global.wthread',
308 308 help=
309 309 """Enable wxPython event loop integration. (DEPRECATED, use --gui wx)""")
310 310 paa('--q4thread', '--qthread', '-q4thread', '-qthread',
311 311 action='store_true', dest='Global.q4thread',
312 312 help=
313 313 """Enable Qt4 event loop integration. Qt3 is no longer supported.
314 314 (DEPRECATED, use --gui qt)""")
315 315 paa('--gthread', '-gthread',
316 316 action='store_true', dest='Global.gthread',
317 317 help=
318 318 """Enable GTK event loop integration. (DEPRECATED, use --gui gtk)""")
319 319
320 320
321 321 #-----------------------------------------------------------------------------
322 322 # Crash handler for this application
323 323 #-----------------------------------------------------------------------------
324 324
325 325
326 326 _message_template = """\
327 327 Oops, $self.app_name crashed. We do our best to make it stable, but...
328 328
329 329 A crash report was automatically generated with the following information:
330 330 - A verbatim copy of the crash traceback.
331 331 - A copy of your input history during this session.
332 332 - Data on your current $self.app_name configuration.
333 333
334 334 It was left in the file named:
335 335 \t'$self.crash_report_fname'
336 336 If you can email this file to the developers, the information in it will help
337 337 them in understanding and correcting the problem.
338 338
339 339 You can mail it to: $self.contact_name at $self.contact_email
340 340 with the subject '$self.app_name Crash Report'.
341 341
342 342 If you want to do it now, the following command will work (under Unix):
343 343 mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
344 344
345 345 To ensure accurate tracking of this issue, please file a report about it at:
346 346 $self.bug_tracker
347 347 """
348 348
349 349 class IPAppCrashHandler(CrashHandler):
350 350 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
351 351
352 352 message_template = _message_template
353 353
354 354 def __init__(self, app):
355 355 contact_name = release.authors['Fernando'][0]
356 356 contact_email = release.authors['Fernando'][1]
357 357 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
358 358 super(IPAppCrashHandler,self).__init__(
359 359 app, contact_name, contact_email, bug_tracker
360 360 )
361 361
362 362 def make_report(self,traceback):
363 363 """Return a string containing a crash report."""
364 364
365 365 sec_sep = self.section_sep
366 366 # Start with parent report
367 367 report = [super(IPAppCrashHandler, self).make_report(traceback)]
368 368 # Add interactive-specific info we may have
369 369 rpt_add = report.append
370 370 try:
371 371 rpt_add(sec_sep+"History of session input:")
372 372 for line in self.app.shell.user_ns['_ih']:
373 373 rpt_add(line)
374 374 rpt_add('\n*** Last line of input (may not be in above history):\n')
375 375 rpt_add(self.app.shell._last_input_line+'\n')
376 376 except:
377 377 pass
378 378
379 379 return ''.join(report)
380 380
381 381
382 382 #-----------------------------------------------------------------------------
383 383 # Main classes and functions
384 384 #-----------------------------------------------------------------------------
385 385
386 386 class IPythonApp(Application):
387 387 name = u'ipython'
388 388 #: argparse formats better the 'usage' than the 'description' field
389 389 description = None
390 390 usage = usage.cl_usage
391 391 command_line_loader = IPAppConfigLoader
392 392 default_config_file_name = default_config_file_name
393 393 crash_handler_class = IPAppCrashHandler
394 394
395 395 def create_default_config(self):
396 396 super(IPythonApp, self).create_default_config()
397 397 # Eliminate multiple lookups
398 398 Global = self.default_config.Global
399 399
400 400 # Set all default values
401 401 Global.display_banner = True
402 402
403 403 # If the -c flag is given or a file is given to run at the cmd line
404 404 # like "ipython foo.py", normally we exit without starting the main
405 405 # loop. The force_interact config variable allows a user to override
406 406 # this and interact. It is also set by the -i cmd line flag, just
407 407 # like Python.
408 408 Global.force_interact = False
409 409
410 410 # By default always interact by starting the IPython mainloop.
411 411 Global.interact = True
412 412
413 413 # No GUI integration by default
414 414 Global.gui = False
415 415 # Pylab off by default
416 416 Global.pylab = False
417 417
418 418 # Deprecated versions of gui support that used threading, we support
419 419 # them just for bacwards compatibility as an alternate spelling for
420 420 # '--gui X'
421 421 Global.qthread = False
422 422 Global.q4thread = False
423 423 Global.wthread = False
424 424 Global.gthread = False
425 425
426 426 def load_file_config(self):
427 427 if hasattr(self.command_line_config.Global, 'quick'):
428 428 if self.command_line_config.Global.quick:
429 429 self.file_config = Config()
430 430 return
431 431 super(IPythonApp, self).load_file_config()
432 432
433 433 def post_load_file_config(self):
434 434 if hasattr(self.command_line_config.Global, 'extra_extension'):
435 435 if not hasattr(self.file_config.Global, 'extensions'):
436 436 self.file_config.Global.extensions = []
437 437 self.file_config.Global.extensions.append(
438 438 self.command_line_config.Global.extra_extension)
439 439 del self.command_line_config.Global.extra_extension
440 440
441 441 def pre_construct(self):
442 442 config = self.master_config
443 443
444 444 if hasattr(config.Global, 'classic'):
445 445 if config.Global.classic:
446 446 config.InteractiveShell.cache_size = 0
447 447 config.InteractiveShell.pprint = 0
448 448 config.InteractiveShell.prompt_in1 = '>>> '
449 449 config.InteractiveShell.prompt_in2 = '... '
450 450 config.InteractiveShell.prompt_out = ''
451 config.InteractiveShell.separate_in = \
452 config.InteractiveShell.separate_out = \
453 config.InteractiveShell.separate_out2 = ''
451 config.TerminalInteractiveShell.separate_in = \
452 config.TerminalInteractiveShell.separate_out = \
453 config.TerminalInteractiveShell.separate_out2 = ''
454 454 config.InteractiveShell.colors = 'NoColor'
455 455 config.InteractiveShell.xmode = 'Plain'
456 456
457 457 if hasattr(config.Global, 'nosep'):
458 458 if config.Global.nosep:
459 config.InteractiveShell.separate_in = \
460 config.InteractiveShell.separate_out = \
461 config.InteractiveShell.separate_out2 = ''
459 config.TerminalInteractiveShell.separate_in = \
460 config.TerminalInteractiveShell.separate_out = \
461 config.TerminalInteractiveShell.separate_out2 = ''
462 462
463 463 # if there is code of files to run from the cmd line, don't interact
464 464 # unless the -i flag (Global.force_interact) is true.
465 465 code_to_run = config.Global.get('code_to_run','')
466 466 file_to_run = False
467 467 if self.extra_args and self.extra_args[0]:
468 468 file_to_run = True
469 469 if file_to_run or code_to_run:
470 470 if not config.Global.force_interact:
471 471 config.Global.interact = False
472 472
473 473 def construct(self):
474 474 # I am a little hesitant to put these into InteractiveShell itself.
475 475 # But that might be the place for them
476 476 sys.path.insert(0, '')
477 477
478 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 481 def post_construct(self):
482 482 """Do actions after construct, but before starting the app."""
483 483 config = self.master_config
484 484
485 485 # shell.display_banner should always be False for the terminal
486 486 # based app, because we call shell.show_banner() by hand below
487 487 # so the banner shows *before* all extension loading stuff.
488 488 self.shell.display_banner = False
489 489 if config.Global.display_banner and \
490 490 config.Global.interact:
491 491 self.shell.show_banner()
492 492
493 493 # Make sure there is a space below the banner.
494 494 if self.log_level <= logging.INFO: print
495 495
496 496 # Now a variety of things that happen after the banner is printed.
497 497 self._enable_gui_pylab()
498 498 self._load_extensions()
499 499 self._run_exec_lines()
500 500 self._run_exec_files()
501 501 self._run_cmd_line_code()
502 502
503 503 def _enable_gui_pylab(self):
504 504 """Enable GUI event loop integration, taking pylab into account."""
505 505 Global = self.master_config.Global
506 506
507 507 # Select which gui to use
508 508 if Global.gui:
509 509 gui = Global.gui
510 510 # The following are deprecated, but there's likely to be a lot of use
511 511 # of this form out there, so we might as well support it for now. But
512 512 # the --gui option above takes precedence.
513 513 elif Global.wthread:
514 514 gui = inputhook.GUI_WX
515 515 elif Global.qthread:
516 516 gui = inputhook.GUI_QT
517 517 elif Global.gthread:
518 518 gui = inputhook.GUI_GTK
519 519 else:
520 520 gui = None
521 521
522 522 # Using --pylab will also require gui activation, though which toolkit
523 523 # to use may be chosen automatically based on mpl configuration.
524 524 if Global.pylab:
525 525 activate = self.shell.enable_pylab
526 526 if Global.pylab == 'auto':
527 527 gui = None
528 528 else:
529 529 gui = Global.pylab
530 530 else:
531 531 # Enable only GUI integration, no pylab
532 532 activate = inputhook.enable_gui
533 533
534 534 if gui or Global.pylab:
535 535 try:
536 536 self.log.info("Enabling GUI event loop integration, "
537 537 "toolkit=%s, pylab=%s" % (gui, Global.pylab) )
538 538 activate(gui)
539 539 except:
540 540 self.log.warn("Error in enabling GUI event loop integration:")
541 541 self.shell.showtraceback()
542 542
543 543 def _load_extensions(self):
544 544 """Load all IPython extensions in Global.extensions.
545 545
546 546 This uses the :meth:`ExtensionManager.load_extensions` to load all
547 547 the extensions listed in ``self.master_config.Global.extensions``.
548 548 """
549 549 try:
550 550 if hasattr(self.master_config.Global, 'extensions'):
551 551 self.log.debug("Loading IPython extensions...")
552 552 extensions = self.master_config.Global.extensions
553 553 for ext in extensions:
554 554 try:
555 555 self.log.info("Loading IPython extension: %s" % ext)
556 556 self.shell.extension_manager.load_extension(ext)
557 557 except:
558 558 self.log.warn("Error in loading extension: %s" % ext)
559 559 self.shell.showtraceback()
560 560 except:
561 561 self.log.warn("Unknown error in loading extensions:")
562 562 self.shell.showtraceback()
563 563
564 564 def _run_exec_lines(self):
565 565 """Run lines of code in Global.exec_lines in the user's namespace."""
566 566 try:
567 567 if hasattr(self.master_config.Global, 'exec_lines'):
568 568 self.log.debug("Running code from Global.exec_lines...")
569 569 exec_lines = self.master_config.Global.exec_lines
570 570 for line in exec_lines:
571 571 try:
572 572 self.log.info("Running code in user namespace: %s" %
573 573 line)
574 574 self.shell.runlines(line)
575 575 except:
576 576 self.log.warn("Error in executing line in user "
577 577 "namespace: %s" % line)
578 578 self.shell.showtraceback()
579 579 except:
580 580 self.log.warn("Unknown error in handling Global.exec_lines:")
581 581 self.shell.showtraceback()
582 582
583 583 def _exec_file(self, fname):
584 584 full_filename = filefind(fname, [u'.', self.ipython_dir])
585 585 if os.path.isfile(full_filename):
586 586 if full_filename.endswith(u'.py'):
587 587 self.log.info("Running file in user namespace: %s" %
588 588 full_filename)
589 589 # Ensure that __file__ is always defined to match Python behavior
590 590 self.shell.user_ns['__file__'] = fname
591 591 try:
592 592 self.shell.safe_execfile(full_filename, self.shell.user_ns)
593 593 finally:
594 594 del self.shell.user_ns['__file__']
595 595 elif full_filename.endswith('.ipy'):
596 596 self.log.info("Running file in user namespace: %s" %
597 597 full_filename)
598 598 self.shell.safe_execfile_ipy(full_filename)
599 599 else:
600 600 self.log.warn("File does not have a .py or .ipy extension: <%s>"
601 601 % full_filename)
602 602 def _run_exec_files(self):
603 603 try:
604 604 if hasattr(self.master_config.Global, 'exec_files'):
605 605 self.log.debug("Running files in Global.exec_files...")
606 606 exec_files = self.master_config.Global.exec_files
607 607 for fname in exec_files:
608 608 self._exec_file(fname)
609 609 except:
610 610 self.log.warn("Unknown error in handling Global.exec_files:")
611 611 self.shell.showtraceback()
612 612
613 613 def _run_cmd_line_code(self):
614 614 if hasattr(self.master_config.Global, 'code_to_run'):
615 615 line = self.master_config.Global.code_to_run
616 616 try:
617 617 self.log.info("Running code given at command line (-c): %s" %
618 618 line)
619 619 self.shell.runlines(line)
620 620 except:
621 621 self.log.warn("Error in executing line in user namespace: %s" %
622 622 line)
623 623 self.shell.showtraceback()
624 624 return
625 625 # Like Python itself, ignore the second if the first of these is present
626 626 try:
627 627 fname = self.extra_args[0]
628 628 except:
629 629 pass
630 630 else:
631 631 try:
632 632 self._exec_file(fname)
633 633 except:
634 634 self.log.warn("Error in executing file in user namespace: %s" %
635 635 fname)
636 636 self.shell.showtraceback()
637 637
638 638 def start_app(self):
639 639 if self.master_config.Global.interact:
640 640 self.log.debug("Starting IPython's mainloop...")
641 641 self.shell.mainloop()
642 642 else:
643 643 self.log.debug("IPython not interactive, start_app is no-op...")
644 644
645 645
646 646 def load_default_config(ipython_dir=None):
647 647 """Load the default config file from the default ipython_dir.
648 648
649 649 This is useful for embedded shells.
650 650 """
651 651 if ipython_dir is None:
652 652 ipython_dir = get_ipython_dir()
653 653 cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
654 654 config = cl.load_config()
655 655 return config
656 656
657 657
658 658 def launch_new_instance():
659 659 """Create and run a full blown IPython instance"""
660 660 app = IPythonApp()
661 661 app.start()
662 662
663 663
664 664 if __name__ == '__main__':
665 665 launch_new_instance()
@@ -1,174 +1,173 b''
1 1 """Global IPython app to support test running.
2 2
3 3 We must start our own ipython object and heavily muck with it so that all the
4 4 modifications IPython makes to system behavior don't send the doctest machinery
5 5 into a fit. This code should be considered a gross hack, but it gets the job
6 6 done.
7 7 """
8 8
9 9 from __future__ import absolute_import
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Copyright (C) 2009 The IPython Development Team
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21
22 22 import __builtin__
23 23 import commands
24 24 import os
25 25 import sys
26 26
27 27 from . import tools
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Functions
31 31 #-----------------------------------------------------------------------------
32 32
33 33 # Hack to modify the %run command so we can sync the user's namespace with the
34 34 # test globals. Once we move over to a clean magic system, this will be done
35 35 # with much less ugliness.
36 36
37 37 class py_file_finder(object):
38 38 def __init__(self,test_filename):
39 39 self.test_filename = test_filename
40 40
41 41 def __call__(self,name):
42 42 from IPython.utils.path import get_py_filename
43 43 try:
44 44 return get_py_filename(name)
45 45 except IOError:
46 46 test_dir = os.path.dirname(self.test_filename)
47 47 new_path = os.path.join(test_dir,name)
48 48 return get_py_filename(new_path)
49 49
50 50
51 51 def _run_ns_sync(self,arg_s,runner=None):
52 52 """Modified version of %run that syncs testing namespaces.
53 53
54 54 This is strictly needed for running doctests that call %run.
55 55 """
56 56 #print >> sys.stderr, 'in run_ns_sync', arg_s # dbg
57 57
58 58 _ip = get_ipython()
59 59 finder = py_file_finder(arg_s)
60 60 out = _ip.magic_run_ori(arg_s,runner,finder)
61 61 return out
62 62
63 63
64 64 class ipnsdict(dict):
65 65 """A special subclass of dict for use as an IPython namespace in doctests.
66 66
67 67 This subclass adds a simple checkpointing capability so that when testing
68 68 machinery clears it (we use it as the test execution context), it doesn't
69 69 get completely destroyed.
70 70 """
71 71
72 72 def __init__(self,*a):
73 73 dict.__init__(self,*a)
74 74 self._savedict = {}
75 75
76 76 def clear(self):
77 77 dict.clear(self)
78 78 self.update(self._savedict)
79 79
80 80 def _checkpoint(self):
81 81 self._savedict.clear()
82 82 self._savedict.update(self)
83 83
84 84 def update(self,other):
85 85 self._checkpoint()
86 86 dict.update(self,other)
87 87
88 88 # If '_' is in the namespace, python won't set it when executing code,
89 89 # and we have examples that test it. So we ensure that the namespace
90 90 # is always 'clean' of it before it's used for test code execution.
91 91 self.pop('_',None)
92 92
93 93 # The builtins namespace must *always* be the real __builtin__ module,
94 94 # else weird stuff happens. The main ipython code does have provisions
95 95 # to ensure this after %run, but since in this class we do some
96 96 # aggressive low-level cleaning of the execution namespace, we need to
97 97 # correct for that ourselves, to ensure consitency with the 'real'
98 98 # ipython.
99 99 self['__builtins__'] = __builtin__
100 100
101 101
102 102 def get_ipython():
103 103 # This will get replaced by the real thing once we start IPython below
104 104 return start_ipython()
105 105
106 106
107 107 def start_ipython():
108 108 """Start a global IPython shell, which we need for IPython-specific syntax.
109 109 """
110 110 global get_ipython
111 111
112 112 # This function should only ever run once!
113 113 if hasattr(start_ipython, 'already_called'):
114 114 return
115 115 start_ipython.already_called = True
116 116
117 # Ok, first time we're called, go ahead
118 from IPython.core import interactiveshell
117 from IPython.frontend.terminal import interactiveshell
119 118
120 119 def xsys(cmd):
121 120 """Execute a command and print its output.
122 121
123 122 This is just a convenience function to replace the IPython system call
124 123 with one that is more doctest-friendly.
125 124 """
126 125 cmd = _ip.var_expand(cmd,depth=1)
127 126 sys.stdout.write(commands.getoutput(cmd))
128 127 sys.stdout.flush()
129 128
130 129 # Store certain global objects that IPython modifies
131 130 _displayhook = sys.displayhook
132 131 _excepthook = sys.excepthook
133 132 _main = sys.modules.get('__main__')
134 133
135 134 # Create custom argv and namespaces for our IPython to be test-friendly
136 135 config = tools.default_config()
137 136
138 137 # Create and initialize our test-friendly IPython instance.
139 shell = interactiveshell.InteractiveShell.instance(
138 shell = interactiveshell.TerminalInteractiveShell.instance(
140 139 config=config,
141 140 user_ns=ipnsdict(), user_global_ns={}
142 141 )
143 142
144 143 # A few more tweaks needed for playing nicely with doctests...
145 144
146 145 # These traps are normally only active for interactive use, set them
147 146 # permanently since we'll be mocking interactive sessions.
148 147 shell.builtin_trap.set()
149 148
150 149 # Set error printing to stdout so nose can doctest exceptions
151 150 shell.InteractiveTB.out_stream = 'stdout'
152 151
153 152 # Modify the IPython system call with one that uses getoutput, so that we
154 153 # can capture subcommands and print them to Python's stdout, otherwise the
155 154 # doctest machinery would miss them.
156 155 shell.system = xsys
157 156
158 157 # IPython is ready, now clean up some global state...
159 158
160 159 # Deactivate the various python system hooks added by ipython for
161 160 # interactive convenience so we don't confuse the doctest system
162 161 sys.modules['__main__'] = _main
163 162 sys.displayhook = _displayhook
164 163 sys.excepthook = _excepthook
165 164
166 165 # So that ipython magics and aliases can be doctested (they work by making
167 166 # a call into a global _ip object). Also make the top-level get_ipython
168 167 # now return this without recursively calling here again.
169 168 _ip = shell
170 169 get_ipython = _ip.get_ipython
171 170 __builtin__._ip = _ip
172 171 __builtin__.get_ipython = get_ipython
173 172
174 173 return _ip
@@ -1,277 +1,277 b''
1 1 """Generic testing tools that do NOT depend on Twisted.
2 2
3 3 In particular, this module exposes a set of top-level assert* functions that
4 4 can be used in place of nose.tools.assert* in method generators (the ones in
5 5 nose can not, at least as of nose 0.10.4).
6 6
7 7 Note: our testing package contains testing.util, which does depend on Twisted
8 8 and provides utilities for tests that manage Deferreds. All testing support
9 9 tools that only depend on nose, IPython or the standard library should go here
10 10 instead.
11 11
12 12
13 13 Authors
14 14 -------
15 15 - Fernando Perez <Fernando.Perez@berkeley.edu>
16 16 """
17 17
18 18 from __future__ import absolute_import
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Copyright (C) 2009 The IPython Development Team
22 22 #
23 23 # Distributed under the terms of the BSD License. The full license is in
24 24 # the file COPYING, distributed as part of this software.
25 25 #-----------------------------------------------------------------------------
26 26
27 27 #-----------------------------------------------------------------------------
28 28 # Imports
29 29 #-----------------------------------------------------------------------------
30 30
31 31 import os
32 32 import re
33 33 import sys
34 34
35 35 try:
36 36 # These tools are used by parts of the runtime, so we make the nose
37 37 # dependency optional at this point. Nose is a hard dependency to run the
38 38 # test suite, but NOT to use ipython itself.
39 39 import nose.tools as nt
40 40 has_nose = True
41 41 except ImportError:
42 42 has_nose = False
43 43
44 44 from IPython.config.loader import Config
45 45 from IPython.utils.process import find_cmd, getoutputerror
46 46 from IPython.utils.text import list_strings
47 47 from IPython.utils.io import temp_pyfile
48 48
49 49 from . import decorators as dec
50 50
51 51 #-----------------------------------------------------------------------------
52 52 # Globals
53 53 #-----------------------------------------------------------------------------
54 54
55 55 # Make a bunch of nose.tools assert wrappers that can be used in test
56 56 # generators. This will expose an assert* function for each one in nose.tools.
57 57
58 58 _tpl = """
59 59 def %(name)s(*a,**kw):
60 60 return nt.%(name)s(*a,**kw)
61 61 """
62 62
63 63 if has_nose:
64 64 for _x in [a for a in dir(nt) if a.startswith('assert')]:
65 65 exec _tpl % dict(name=_x)
66 66
67 67 #-----------------------------------------------------------------------------
68 68 # Functions and classes
69 69 #-----------------------------------------------------------------------------
70 70
71 71 # The docstring for full_path doctests differently on win32 (different path
72 72 # separator) so just skip the doctest there. The example remains informative.
73 73 doctest_deco = dec.skip_doctest if sys.platform == 'win32' else dec.null_deco
74 74
75 75 @doctest_deco
76 76 def full_path(startPath,files):
77 77 """Make full paths for all the listed files, based on startPath.
78 78
79 79 Only the base part of startPath is kept, since this routine is typically
80 80 used with a script's __file__ variable as startPath. The base of startPath
81 81 is then prepended to all the listed files, forming the output list.
82 82
83 83 Parameters
84 84 ----------
85 85 startPath : string
86 86 Initial path to use as the base for the results. This path is split
87 87 using os.path.split() and only its first component is kept.
88 88
89 89 files : string or list
90 90 One or more files.
91 91
92 92 Examples
93 93 --------
94 94
95 95 >>> full_path('/foo/bar.py',['a.txt','b.txt'])
96 96 ['/foo/a.txt', '/foo/b.txt']
97 97
98 98 >>> full_path('/foo',['a.txt','b.txt'])
99 99 ['/a.txt', '/b.txt']
100 100
101 101 If a single file is given, the output is still a list:
102 102 >>> full_path('/foo','a.txt')
103 103 ['/a.txt']
104 104 """
105 105
106 106 files = list_strings(files)
107 107 base = os.path.split(startPath)[0]
108 108 return [ os.path.join(base,f) for f in files ]
109 109
110 110
111 111 def parse_test_output(txt):
112 112 """Parse the output of a test run and return errors, failures.
113 113
114 114 Parameters
115 115 ----------
116 116 txt : str
117 117 Text output of a test run, assumed to contain a line of one of the
118 118 following forms::
119 119 'FAILED (errors=1)'
120 120 'FAILED (failures=1)'
121 121 'FAILED (errors=1, failures=1)'
122 122
123 123 Returns
124 124 -------
125 125 nerr, nfail: number of errors and failures.
126 126 """
127 127
128 128 err_m = re.search(r'^FAILED \(errors=(\d+)\)', txt, re.MULTILINE)
129 129 if err_m:
130 130 nerr = int(err_m.group(1))
131 131 nfail = 0
132 132 return nerr, nfail
133 133
134 134 fail_m = re.search(r'^FAILED \(failures=(\d+)\)', txt, re.MULTILINE)
135 135 if fail_m:
136 136 nerr = 0
137 137 nfail = int(fail_m.group(1))
138 138 return nerr, nfail
139 139
140 140 both_m = re.search(r'^FAILED \(errors=(\d+), failures=(\d+)\)', txt,
141 141 re.MULTILINE)
142 142 if both_m:
143 143 nerr = int(both_m.group(1))
144 144 nfail = int(both_m.group(2))
145 145 return nerr, nfail
146 146
147 147 # If the input didn't match any of these forms, assume no error/failures
148 148 return 0, 0
149 149
150 150
151 151 # So nose doesn't think this is a test
152 152 parse_test_output.__test__ = False
153 153
154 154
155 155 def default_argv():
156 156 """Return a valid default argv for creating testing instances of ipython"""
157 157
158 158 return ['--quick', # so no config file is loaded
159 159 # Other defaults to minimize side effects on stdout
160 160 '--colors=NoColor', '--no-term-title','--no-banner',
161 161 '--autocall=0']
162 162
163 163
164 164 def default_config():
165 165 """Return a config object with good defaults for testing."""
166 166 config = Config()
167 config.InteractiveShell.colors = 'NoColor'
168 config.InteractiveShell.term_title = False,
169 config.InteractiveShell.autocall = 0
167 config.TerminalInteractiveShell.colors = 'NoColor'
168 config.TerminalTerminalInteractiveShell.term_title = False,
169 config.TerminalInteractiveShell.autocall = 0
170 170 return config
171 171
172 172
173 173 def ipexec(fname, options=None):
174 174 """Utility to call 'ipython filename'.
175 175
176 176 Starts IPython witha minimal and safe configuration to make startup as fast
177 177 as possible.
178 178
179 179 Note that this starts IPython in a subprocess!
180 180
181 181 Parameters
182 182 ----------
183 183 fname : str
184 184 Name of file to be executed (should have .py or .ipy extension).
185 185
186 186 options : optional, list
187 187 Extra command-line flags to be passed to IPython.
188 188
189 189 Returns
190 190 -------
191 191 (stdout, stderr) of ipython subprocess.
192 192 """
193 193 if options is None: options = []
194 194
195 195 # For these subprocess calls, eliminate all prompt printing so we only see
196 196 # output from script execution
197 197 prompt_opts = ['--prompt-in1=""', '--prompt-in2=""', '--prompt-out=""']
198 198 cmdargs = ' '.join(default_argv() + prompt_opts + options)
199 199
200 200 _ip = get_ipython()
201 201 test_dir = os.path.dirname(__file__)
202 202
203 203 ipython_cmd = find_cmd('ipython')
204 204 # Absolute path for filename
205 205 full_fname = os.path.join(test_dir, fname)
206 206 full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname)
207 207 #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg
208 208 return getoutputerror(full_cmd)
209 209
210 210
211 211 def ipexec_validate(fname, expected_out, expected_err='',
212 212 options=None):
213 213 """Utility to call 'ipython filename' and validate output/error.
214 214
215 215 This function raises an AssertionError if the validation fails.
216 216
217 217 Note that this starts IPython in a subprocess!
218 218
219 219 Parameters
220 220 ----------
221 221 fname : str
222 222 Name of the file to be executed (should have .py or .ipy extension).
223 223
224 224 expected_out : str
225 225 Expected stdout of the process.
226 226
227 227 expected_err : optional, str
228 228 Expected stderr of the process.
229 229
230 230 options : optional, list
231 231 Extra command-line flags to be passed to IPython.
232 232
233 233 Returns
234 234 -------
235 235 None
236 236 """
237 237
238 238 import nose.tools as nt
239 239
240 240 out, err = ipexec(fname)
241 241 #print 'OUT', out # dbg
242 242 #print 'ERR', err # dbg
243 243 # If there are any errors, we must check those befor stdout, as they may be
244 244 # more informative than simply having an empty stdout.
245 245 if err:
246 246 if expected_err:
247 247 nt.assert_equals(err.strip(), expected_err.strip())
248 248 else:
249 249 raise ValueError('Running file %r produced error: %r' %
250 250 (fname, err))
251 251 # If no errors or output on stderr was expected, match stdout
252 252 nt.assert_equals(out.strip(), expected_out.strip())
253 253
254 254
255 255 class TempFileMixin(object):
256 256 """Utility class to create temporary Python/IPython files.
257 257
258 258 Meant as a mixin class for test cases."""
259 259
260 260 def mktmp(self, src, ext='.py'):
261 261 """Make a valid python temp file."""
262 262 fname, f = temp_pyfile(src, ext)
263 263 self.tmpfile = f
264 264 self.fname = fname
265 265
266 266 def teardown(self):
267 267 if hasattr(self, 'tmpfile'):
268 268 # If the tmpfile wasn't made because of skipped tests, like in
269 269 # win32, there's nothing to cleanup.
270 270 self.tmpfile.close()
271 271 try:
272 272 os.unlink(self.fname)
273 273 except:
274 274 # On Windows, even though we close the file, we still can't
275 275 # delete it. I have no clue why
276 276 pass
277 277
@@ -1,473 +1,484 b''
1 1 # encoding: utf-8
2 2 """
3 3 Utilities for working with strings and text.
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2008-2009 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 import __main__
18 18
19 19 import os
20 20 import re
21 21 import shutil
22 22 import types
23 23
24 24 from IPython.external.path import path
25 25
26 26 from IPython.utils.generics import result_display
27 27 from IPython.utils.io import nlprint
28 28 from IPython.utils.data import flatten
29 29
30 30 #-----------------------------------------------------------------------------
31 31 # Code
32 32 #-----------------------------------------------------------------------------
33 33
34 34 StringTypes = types.StringTypes
35 35
36 36
37 37 def unquote_ends(istr):
38 38 """Remove a single pair of quotes from the endpoints of a string."""
39 39
40 40 if not istr:
41 41 return istr
42 42 if (istr[0]=="'" and istr[-1]=="'") or \
43 43 (istr[0]=='"' and istr[-1]=='"'):
44 44 return istr[1:-1]
45 45 else:
46 46 return istr
47 47
48 48
49 49 class LSString(str):
50 50 """String derivative with a special access attributes.
51 51
52 52 These are normal strings, but with the special attributes:
53 53
54 54 .l (or .list) : value as list (split on newlines).
55 55 .n (or .nlstr): original value (the string itself).
56 56 .s (or .spstr): value as whitespace-separated string.
57 57 .p (or .paths): list of path objects
58 58
59 59 Any values which require transformations are computed only once and
60 60 cached.
61 61
62 62 Such strings are very useful to efficiently interact with the shell, which
63 63 typically only understands whitespace-separated options for commands."""
64 64
65 65 def get_list(self):
66 66 try:
67 67 return self.__list
68 68 except AttributeError:
69 69 self.__list = self.split('\n')
70 70 return self.__list
71 71
72 72 l = list = property(get_list)
73 73
74 74 def get_spstr(self):
75 75 try:
76 76 return self.__spstr
77 77 except AttributeError:
78 78 self.__spstr = self.replace('\n',' ')
79 79 return self.__spstr
80 80
81 81 s = spstr = property(get_spstr)
82 82
83 83 def get_nlstr(self):
84 84 return self
85 85
86 86 n = nlstr = property(get_nlstr)
87 87
88 88 def get_paths(self):
89 89 try:
90 90 return self.__paths
91 91 except AttributeError:
92 92 self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)]
93 93 return self.__paths
94 94
95 95 p = paths = property(get_paths)
96 96
97 97
98 98 def print_lsstring(arg):
99 99 """ Prettier (non-repr-like) and more informative printer for LSString """
100 100 print "LSString (.p, .n, .l, .s available). Value:"
101 101 print arg
102 102
103 103
104 104 print_lsstring = result_display.when_type(LSString)(print_lsstring)
105 105
106 106
107 107 class SList(list):
108 108 """List derivative with a special access attributes.
109 109
110 110 These are normal lists, but with the special attributes:
111 111
112 112 .l (or .list) : value as list (the list itself).
113 113 .n (or .nlstr): value as a string, joined on newlines.
114 114 .s (or .spstr): value as a string, joined on spaces.
115 115 .p (or .paths): list of path objects
116 116
117 117 Any values which require transformations are computed only once and
118 118 cached."""
119 119
120 120 def get_list(self):
121 121 return self
122 122
123 123 l = list = property(get_list)
124 124
125 125 def get_spstr(self):
126 126 try:
127 127 return self.__spstr
128 128 except AttributeError:
129 129 self.__spstr = ' '.join(self)
130 130 return self.__spstr
131 131
132 132 s = spstr = property(get_spstr)
133 133
134 134 def get_nlstr(self):
135 135 try:
136 136 return self.__nlstr
137 137 except AttributeError:
138 138 self.__nlstr = '\n'.join(self)
139 139 return self.__nlstr
140 140
141 141 n = nlstr = property(get_nlstr)
142 142
143 143 def get_paths(self):
144 144 try:
145 145 return self.__paths
146 146 except AttributeError:
147 147 self.__paths = [path(p) for p in self if os.path.exists(p)]
148 148 return self.__paths
149 149
150 150 p = paths = property(get_paths)
151 151
152 152 def grep(self, pattern, prune = False, field = None):
153 153 """ Return all strings matching 'pattern' (a regex or callable)
154 154
155 155 This is case-insensitive. If prune is true, return all items
156 156 NOT matching the pattern.
157 157
158 158 If field is specified, the match must occur in the specified
159 159 whitespace-separated field.
160 160
161 161 Examples::
162 162
163 163 a.grep( lambda x: x.startswith('C') )
164 164 a.grep('Cha.*log', prune=1)
165 165 a.grep('chm', field=-1)
166 166 """
167 167
168 168 def match_target(s):
169 169 if field is None:
170 170 return s
171 171 parts = s.split()
172 172 try:
173 173 tgt = parts[field]
174 174 return tgt
175 175 except IndexError:
176 176 return ""
177 177
178 178 if isinstance(pattern, basestring):
179 179 pred = lambda x : re.search(pattern, x, re.IGNORECASE)
180 180 else:
181 181 pred = pattern
182 182 if not prune:
183 183 return SList([el for el in self if pred(match_target(el))])
184 184 else:
185 185 return SList([el for el in self if not pred(match_target(el))])
186 186
187 187 def fields(self, *fields):
188 188 """ Collect whitespace-separated fields from string list
189 189
190 190 Allows quick awk-like usage of string lists.
191 191
192 192 Example data (in var a, created by 'a = !ls -l')::
193 193 -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog
194 194 drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython
195 195
196 196 a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+']
197 197 a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+']
198 198 (note the joining by space).
199 199 a.fields(-1) is ['ChangeLog', 'IPython']
200 200
201 201 IndexErrors are ignored.
202 202
203 203 Without args, fields() just split()'s the strings.
204 204 """
205 205 if len(fields) == 0:
206 206 return [el.split() for el in self]
207 207
208 208 res = SList()
209 209 for el in [f.split() for f in self]:
210 210 lineparts = []
211 211
212 212 for fd in fields:
213 213 try:
214 214 lineparts.append(el[fd])
215 215 except IndexError:
216 216 pass
217 217 if lineparts:
218 218 res.append(" ".join(lineparts))
219 219
220 220 return res
221 221
222 222 def sort(self,field= None, nums = False):
223 223 """ sort by specified fields (see fields())
224 224
225 225 Example::
226 226 a.sort(1, nums = True)
227 227
228 228 Sorts a by second field, in numerical order (so that 21 > 3)
229 229
230 230 """
231 231
232 232 #decorate, sort, undecorate
233 233 if field is not None:
234 234 dsu = [[SList([line]).fields(field), line] for line in self]
235 235 else:
236 236 dsu = [[line, line] for line in self]
237 237 if nums:
238 238 for i in range(len(dsu)):
239 239 numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()])
240 240 try:
241 241 n = int(numstr)
242 242 except ValueError:
243 243 n = 0;
244 244 dsu[i][0] = n
245 245
246 246
247 247 dsu.sort()
248 248 return SList([t[1] for t in dsu])
249 249
250 250
251 251 def print_slist(arg):
252 252 """ Prettier (non-repr-like) and more informative printer for SList """
253 253 print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):"
254 254 if hasattr(arg, 'hideonce') and arg.hideonce:
255 255 arg.hideonce = False
256 256 return
257 257
258 258 nlprint(arg)
259 259
260 260
261 261 print_slist = result_display.when_type(SList)(print_slist)
262 262
263 263
264 264 def esc_quotes(strng):
265 265 """Return the input string with single and double quotes escaped out"""
266 266
267 267 return strng.replace('"','\\"').replace("'","\\'")
268 268
269 269
270 270 def make_quoted_expr(s):
271 271 """Return string s in appropriate quotes, using raw string if possible.
272 272
273 273 XXX - example removed because it caused encoding errors in documentation
274 274 generation. We need a new example that doesn't contain invalid chars.
275 275
276 276 Note the use of raw string and padding at the end to allow trailing
277 277 backslash.
278 278 """
279 279
280 280 tail = ''
281 281 tailpadding = ''
282 282 raw = ''
283 283 if "\\" in s:
284 284 raw = 'r'
285 285 if s.endswith('\\'):
286 286 tail = '[:-1]'
287 287 tailpadding = '_'
288 288 if '"' not in s:
289 289 quote = '"'
290 290 elif "'" not in s:
291 291 quote = "'"
292 292 elif '"""' not in s and not s.endswith('"'):
293 293 quote = '"""'
294 294 elif "'''" not in s and not s.endswith("'"):
295 295 quote = "'''"
296 296 else:
297 297 # give up, backslash-escaped string will do
298 298 return '"%s"' % esc_quotes(s)
299 299 res = raw + quote + s + tailpadding + quote + tail
300 300 return res
301 301
302 302
303 303 def qw(words,flat=0,sep=None,maxsplit=-1):
304 304 """Similar to Perl's qw() operator, but with some more options.
305 305
306 306 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
307 307
308 308 words can also be a list itself, and with flat=1, the output will be
309 309 recursively flattened.
310 310
311 311 Examples:
312 312
313 313 >>> qw('1 2')
314 314 ['1', '2']
315 315
316 316 >>> qw(['a b','1 2',['m n','p q']])
317 317 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
318 318
319 319 >>> qw(['a b','1 2',['m n','p q']],flat=1)
320 320 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q']
321 321 """
322 322
323 323 if type(words) in StringTypes:
324 324 return [word.strip() for word in words.split(sep,maxsplit)
325 325 if word and not word.isspace() ]
326 326 if flat:
327 327 return flatten(map(qw,words,[1]*len(words)))
328 328 return map(qw,words)
329 329
330 330
331 331 def qwflat(words,sep=None,maxsplit=-1):
332 332 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
333 333 return qw(words,1,sep,maxsplit)
334 334
335 335
336 336 def qw_lol(indata):
337 337 """qw_lol('a b') -> [['a','b']],
338 338 otherwise it's just a call to qw().
339 339
340 340 We need this to make sure the modules_some keys *always* end up as a
341 341 list of lists."""
342 342
343 343 if type(indata) in StringTypes:
344 344 return [qw(indata)]
345 345 else:
346 346 return qw(indata)
347 347
348 348
349 349 def grep(pat,list,case=1):
350 350 """Simple minded grep-like function.
351 351 grep(pat,list) returns occurrences of pat in list, None on failure.
352 352
353 353 It only does simple string matching, with no support for regexps. Use the
354 354 option case=0 for case-insensitive matching."""
355 355
356 356 # This is pretty crude. At least it should implement copying only references
357 357 # to the original data in case it's big. Now it copies the data for output.
358 358 out=[]
359 359 if case:
360 360 for term in list:
361 361 if term.find(pat)>-1: out.append(term)
362 362 else:
363 363 lpat=pat.lower()
364 364 for term in list:
365 365 if term.lower().find(lpat)>-1: out.append(term)
366 366
367 367 if len(out): return out
368 368 else: return None
369 369
370 370
371 371 def dgrep(pat,*opts):
372 372 """Return grep() on dir()+dir(__builtins__).
373 373
374 374 A very common use of grep() when working interactively."""
375 375
376 376 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
377 377
378 378
379 379 def idgrep(pat):
380 380 """Case-insensitive dgrep()"""
381 381
382 382 return dgrep(pat,0)
383 383
384 384
385 385 def igrep(pat,list):
386 386 """Synonym for case-insensitive grep."""
387 387
388 388 return grep(pat,list,case=0)
389 389
390 390
391 391 def indent(str,nspaces=4,ntabs=0):
392 392 """Indent a string a given number of spaces or tabstops.
393 393
394 394 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
395 395 """
396 396 if str is None:
397 397 return
398 398 ind = '\t'*ntabs+' '*nspaces
399 399 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
400 400 if outstr.endswith(os.linesep+ind):
401 401 return outstr[:-len(ind)]
402 402 else:
403 403 return outstr
404 404
405 405 def native_line_ends(filename,backup=1):
406 406 """Convert (in-place) a file to line-ends native to the current OS.
407 407
408 408 If the optional backup argument is given as false, no backup of the
409 409 original file is left. """
410 410
411 411 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
412 412
413 413 bak_filename = filename + backup_suffixes[os.name]
414 414
415 415 original = open(filename).read()
416 416 shutil.copy2(filename,bak_filename)
417 417 try:
418 418 new = open(filename,'wb')
419 419 new.write(os.linesep.join(original.splitlines()))
420 420 new.write(os.linesep) # ALWAYS put an eol at the end of the file
421 421 new.close()
422 422 except:
423 423 os.rename(bak_filename,filename)
424 424 if not backup:
425 425 try:
426 426 os.remove(bak_filename)
427 427 except:
428 428 pass
429 429
430 430
431 431 def list_strings(arg):
432 432 """Always return a list of strings, given a string or list of strings
433 433 as input.
434 434
435 435 :Examples:
436 436
437 437 In [7]: list_strings('A single string')
438 438 Out[7]: ['A single string']
439 439
440 440 In [8]: list_strings(['A single string in a list'])
441 441 Out[8]: ['A single string in a list']
442 442
443 443 In [9]: list_strings(['A','list','of','strings'])
444 444 Out[9]: ['A', 'list', 'of', 'strings']
445 445 """
446 446
447 447 if isinstance(arg,basestring): return [arg]
448 448 else: return arg
449 449
450 450
451 451 def marquee(txt='',width=78,mark='*'):
452 452 """Return the input string centered in a 'marquee'.
453 453
454 454 :Examples:
455 455
456 456 In [16]: marquee('A test',40)
457 457 Out[16]: '**************** A test ****************'
458 458
459 459 In [17]: marquee('A test',40,'-')
460 460 Out[17]: '---------------- A test ----------------'
461 461
462 462 In [18]: marquee('A test',40,' ')
463 463 Out[18]: ' A test '
464 464
465 465 """
466 466 if not txt:
467 467 return (mark*width)[:width]
468 468 nmark = (width-len(txt)-2)/len(mark)/2
469 469 if nmark < 0: nmark =0
470 470 marks = mark*nmark
471 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