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 | # Get the config being loaded so we can set attributes on it |
|
1 | # Get the config being loaded so we can set attributes on it | |
2 | c = get_config() |
|
2 | c = get_config() | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Global options |
|
5 | # Global options | |
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | # c.Global.display_banner = True |
|
8 | # c.Global.display_banner = True | |
9 |
|
9 | |||
10 | # c.Global.classic = False |
|
10 | # c.Global.classic = False | |
11 |
|
11 | |||
12 | # c.Global.nosep = True |
|
12 | # c.Global.nosep = True | |
13 |
|
13 | |||
14 | # Set this to determine the detail of what is logged at startup. |
|
14 | # Set this to determine the detail of what is logged at startup. | |
15 | # The default is 30 and possible values are 0,10,20,30,40,50. |
|
15 | # The default is 30 and possible values are 0,10,20,30,40,50. | |
16 | # c.Global.log_level = 20 |
|
16 | # c.Global.log_level = 20 | |
17 |
|
17 | |||
18 | # This should be a list of importable Python modules that have an |
|
18 | # This should be a list of importable Python modules that have an | |
19 | # load_in_ipython(ip) method. This method gets called when the extension |
|
19 | # load_in_ipython(ip) method. This method gets called when the extension | |
20 | # is loaded. You can put your extensions anywhere they can be imported |
|
20 | # is loaded. You can put your extensions anywhere they can be imported | |
21 | # but we add the extensions subdir of the ipython directory to sys.path |
|
21 | # but we add the extensions subdir of the ipython directory to sys.path | |
22 | # during extension loading, so you can put them there as well. |
|
22 | # during extension loading, so you can put them there as well. | |
23 | # c.Global.extensions = [ |
|
23 | # c.Global.extensions = [ | |
24 | # 'myextension' |
|
24 | # 'myextension' | |
25 | # ] |
|
25 | # ] | |
26 |
|
26 | |||
27 | # These lines are run in IPython in the user's namespace after extensions |
|
27 | # These lines are run in IPython in the user's namespace after extensions | |
28 | # are loaded. They can contain full IPython syntax with magics etc. |
|
28 | # are loaded. They can contain full IPython syntax with magics etc. | |
29 | # c.Global.exec_lines = [ |
|
29 | # c.Global.exec_lines = [ | |
30 | # 'import numpy', |
|
30 | # 'import numpy', | |
31 | # 'a = 10; b = 20', |
|
31 | # 'a = 10; b = 20', | |
32 | # '1/0' |
|
32 | # '1/0' | |
33 | # ] |
|
33 | # ] | |
34 |
|
34 | |||
35 | # These files are run in IPython in the user's namespace. Files with a .py |
|
35 | # These files are run in IPython in the user's namespace. Files with a .py | |
36 | # extension need to be pure Python. Files with a .ipy extension can have |
|
36 | # extension need to be pure Python. Files with a .ipy extension can have | |
37 | # custom IPython syntax (like magics, etc.). |
|
37 | # custom IPython syntax (like magics, etc.). | |
38 | # These files need to be in the cwd, the ipython_dir or be absolute paths. |
|
38 | # These files need to be in the cwd, the ipython_dir or be absolute paths. | |
39 | # c.Global.exec_files = [ |
|
39 | # c.Global.exec_files = [ | |
40 | # 'mycode.py', |
|
40 | # 'mycode.py', | |
41 | # 'fancy.ipy' |
|
41 | # 'fancy.ipy' | |
42 | # ] |
|
42 | # ] | |
43 |
|
43 | |||
44 | #----------------------------------------------------------------------------- |
|
44 | #----------------------------------------------------------------------------- | |
45 | # InteractiveShell options |
|
45 | # InteractiveShell options | |
46 | #----------------------------------------------------------------------------- |
|
46 | #----------------------------------------------------------------------------- | |
47 |
|
47 | |||
48 | # c.InteractiveShell.autocall = 1 |
|
48 | # c.InteractiveShell.autocall = 1 | |
49 |
|
49 | |||
50 | # c.InteractiveShell.autoedit_syntax = False |
|
50 | # c.TerminalInteractiveShell.autoedit_syntax = False | |
51 |
|
51 | |||
52 | # c.InteractiveShell.autoindent = True |
|
52 | # c.TerminalInteractiveShell.autoindent = True | |
53 |
|
53 | |||
54 | # c.InteractiveShell.automagic = False |
|
54 | # c.InteractiveShell.automagic = False | |
55 |
|
55 | |||
56 | # c.InteractiveShell.banner1 = 'This if for overriding the default IPython banner' |
|
56 | # c.TerminalTerminalInteractiveShell.banner1 = 'This if for overriding the default IPython banner' | |
57 |
|
57 | |||
58 | # c.InteractiveShell.banner2 = "This is for extra banner text" |
|
58 | # c.TerminalTerminalInteractiveShell.banner2 = "This is for extra banner text" | |
59 |
|
59 | |||
60 | # c.InteractiveShell.cache_size = 1000 |
|
60 | # c.InteractiveShell.cache_size = 1000 | |
61 |
|
61 | |||
62 | # c.InteractiveShell.colors = 'LightBG' |
|
62 | # c.InteractiveShell.colors = 'LightBG' | |
63 |
|
63 | |||
64 | # c.InteractiveShell.color_info = True |
|
64 | # c.InteractiveShell.color_info = True | |
65 |
|
65 | |||
66 | # c.InteractiveShell.confirm_exit = True |
|
66 | # c.TerminalInteractiveShell.confirm_exit = True | |
67 |
|
67 | |||
68 | # c.InteractiveShell.deep_reload = False |
|
68 | # c.InteractiveShell.deep_reload = False | |
69 |
|
69 | |||
70 | # c.InteractiveShell.editor = 'nano' |
|
70 | # c.TerminalInteractiveShell.editor = 'nano' | |
71 |
|
71 | |||
72 | # c.InteractiveShell.logstart = True |
|
72 | # c.InteractiveShell.logstart = True | |
73 |
|
73 | |||
74 | # c.InteractiveShell.logfile = u'ipython_log.py' |
|
74 | # c.InteractiveShell.logfile = u'ipython_log.py' | |
75 |
|
75 | |||
76 | # c.InteractiveShell.logappend = u'mylog.py' |
|
76 | # c.InteractiveShell.logappend = u'mylog.py' | |
77 |
|
77 | |||
78 | # c.InteractiveShell.object_info_string_level = 0 |
|
78 | # c.InteractiveShell.object_info_string_level = 0 | |
79 |
|
79 | |||
80 | # c.InteractiveShell.pager = 'less' |
|
80 | # c.TerminalInteractiveShell.pager = 'less' | |
81 |
|
81 | |||
82 | # c.InteractiveShell.pdb = False |
|
82 | # c.InteractiveShell.pdb = False | |
83 |
|
83 | |||
84 | # c.InteractiveShell.pprint = True |
|
84 | # c.InteractiveShell.pprint = True | |
85 |
|
85 | |||
86 | # c.InteractiveShell.prompt_in1 = 'In [\#]: ' |
|
86 | # c.InteractiveShell.prompt_in1 = 'In [\#]: ' | |
87 | # c.InteractiveShell.prompt_in2 = ' .\D.: ' |
|
87 | # c.InteractiveShell.prompt_in2 = ' .\D.: ' | |
88 | # c.InteractiveShell.prompt_out = 'Out[\#]: ' |
|
88 | # c.InteractiveShell.prompt_out = 'Out[\#]: ' | |
89 | # c.InteractiveShell.prompts_pad_left = True |
|
89 | # c.InteractiveShell.prompts_pad_left = True | |
90 |
|
90 | |||
91 | # c.InteractiveShell.quiet = False |
|
91 | # c.InteractiveShell.quiet = False | |
92 |
|
92 | |||
93 | # Readline |
|
93 | # Readline | |
94 | # c.InteractiveShell.readline_use = True |
|
94 | # c.InteractiveShell.readline_use = True | |
95 |
|
95 | |||
96 | # c.InteractiveShell.readline_parse_and_bind = [ |
|
96 | # c.InteractiveShell.readline_parse_and_bind = [ | |
97 | # 'tab: complete', |
|
97 | # 'tab: complete', | |
98 | # '"\C-l": possible-completions', |
|
98 | # '"\C-l": possible-completions', | |
99 | # 'set show-all-if-ambiguous on', |
|
99 | # 'set show-all-if-ambiguous on', | |
100 | # '"\C-o": tab-insert', |
|
100 | # '"\C-o": tab-insert', | |
101 | # '"\M-i": " "', |
|
101 | # '"\M-i": " "', | |
102 | # '"\M-o": "\d\d\d\d"', |
|
102 | # '"\M-o": "\d\d\d\d"', | |
103 | # '"\M-I": "\d\d\d\d"', |
|
103 | # '"\M-I": "\d\d\d\d"', | |
104 | # '"\C-r": reverse-search-history', |
|
104 | # '"\C-r": reverse-search-history', | |
105 | # '"\C-s": forward-search-history', |
|
105 | # '"\C-s": forward-search-history', | |
106 | # '"\C-p": history-search-backward', |
|
106 | # '"\C-p": history-search-backward', | |
107 | # '"\C-n": history-search-forward', |
|
107 | # '"\C-n": history-search-forward', | |
108 | # '"\e[A": history-search-backward', |
|
108 | # '"\e[A": history-search-backward', | |
109 | # '"\e[B": history-search-forward', |
|
109 | # '"\e[B": history-search-forward', | |
110 | # '"\C-k": kill-line', |
|
110 | # '"\C-k": kill-line', | |
111 | # '"\C-u": unix-line-discard', |
|
111 | # '"\C-u": unix-line-discard', | |
112 | # ] |
|
112 | # ] | |
113 | # c.InteractiveShell.readline_remove_delims = '-/~' |
|
113 | # c.InteractiveShell.readline_remove_delims = '-/~' | |
114 | # c.InteractiveShell.readline_merge_completions = True |
|
114 | # c.InteractiveShell.readline_merge_completions = True | |
115 | # c.InteractiveShell.readline_omit__names = 0 |
|
115 | # c.InteractiveShell.readline_omit__names = 0 | |
116 |
|
116 | |||
117 | # c.InteractiveShell.screen_length = 0 |
|
117 | # c.TerminalInteractiveShell.screen_length = 0 | |
118 |
|
118 | |||
119 | # c.InteractiveShell.separate_in = '\n' |
|
119 | # c.TerminalInteractiveShell.separate_in = '\n' | |
120 | # c.InteractiveShell.separate_out = '' |
|
120 | # c.TerminalInteractiveShell.separate_out = '' | |
121 | # c.InteractiveShell.separate_out2 = '' |
|
121 | # c.TerminalInteractiveShell.separate_out2 = '' | |
122 |
|
122 | |||
123 | # c.InteractiveShell.system_header = "IPython system call: " |
|
123 | # c.InteractiveShell.system_header = "IPython system call: " | |
124 |
|
124 | |||
125 | # c.InteractiveShell.system_verbose = True |
|
125 | # c.InteractiveShell.system_verbose = True | |
126 |
|
126 | |||
127 | # c.InteractiveShell.term_title = False |
|
127 | # c.TerminalInteractiveShell.term_title = False | |
128 |
|
128 | |||
129 | # c.InteractiveShell.wildcards_case_sensitive = True |
|
129 | # c.InteractiveShell.wildcards_case_sensitive = True | |
130 |
|
130 | |||
131 | # c.InteractiveShell.xmode = 'Context' |
|
131 | # c.InteractiveShell.xmode = 'Context' | |
132 |
|
132 | |||
133 | #----------------------------------------------------------------------------- |
|
133 | #----------------------------------------------------------------------------- | |
134 | # PrefilterManager options |
|
134 | # PrefilterManager options | |
135 | #----------------------------------------------------------------------------- |
|
135 | #----------------------------------------------------------------------------- | |
136 |
|
136 | |||
137 | # c.PrefilterManager.multi_line_specials = True |
|
137 | # c.PrefilterManager.multi_line_specials = True | |
138 |
|
138 | |||
139 | #----------------------------------------------------------------------------- |
|
139 | #----------------------------------------------------------------------------- | |
140 | # AliasManager options |
|
140 | # AliasManager options | |
141 | #----------------------------------------------------------------------------- |
|
141 | #----------------------------------------------------------------------------- | |
142 |
|
142 | |||
143 | # Do this to disable all defaults |
|
143 | # Do this to disable all defaults | |
144 | # c.AliasManager.default_aliases = [] |
|
144 | # c.AliasManager.default_aliases = [] | |
145 |
|
145 | |||
146 | # c.AliasManager.user_aliases = [ |
|
146 | # c.AliasManager.user_aliases = [ | |
147 | # ('foo', 'echo Hi') |
|
147 | # ('foo', 'echo Hi') | |
148 | # ] |
|
148 | # ] |
@@ -1,29 +1,29 b'' | |||||
1 | c = get_config() |
|
1 | c = get_config() | |
2 |
|
2 | |||
3 | # This can be used at any point in a config file to load a sub config |
|
3 | # This can be used at any point in a config file to load a sub config | |
4 | # and merge it into the current one. |
|
4 | # and merge it into the current one. | |
5 | load_subconfig('ipython_config.py') |
|
5 | load_subconfig('ipython_config.py') | |
6 |
|
6 | |||
7 | c.InteractiveShell.prompt_in1 = '\C_LightGreen\u@\h\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> ' |
|
7 | c.InteractiveShell.prompt_in1 = '\C_LightGreen\u@\h\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> ' | |
8 | c.InteractiveShell.prompt_in2 = '\C_Green|\C_LightGreen\D\C_Green> ' |
|
8 | c.InteractiveShell.prompt_in2 = '\C_Green|\C_LightGreen\D\C_Green> ' | |
9 | c.InteractiveShell.prompt_out = '<\#> ' |
|
9 | c.InteractiveShell.prompt_out = '<\#> ' | |
10 |
|
10 | |||
11 | c.InteractiveShell.prompts_pad_left = True |
|
11 | c.InteractiveShell.prompts_pad_left = True | |
12 |
|
12 | |||
13 | c.InteractiveShell.separate_in = '' |
|
13 | c.TerminalInteractiveShell.separate_in = '' | |
14 | c.InteractiveShell.separate_out = '' |
|
14 | c.TerminalInteractiveShell.separate_out = '' | |
15 | c.InteractiveShell.separate_out2 = '' |
|
15 | c.TerminalInteractiveShell.separate_out2 = '' | |
16 |
|
16 | |||
17 | c.PrefilterManager.multi_line_specials = True |
|
17 | c.PrefilterManager.multi_line_specials = True | |
18 |
|
18 | |||
19 | lines = """ |
|
19 | lines = """ | |
20 | %rehashx |
|
20 | %rehashx | |
21 | """ |
|
21 | """ | |
22 |
|
22 | |||
23 | # You have to make sure that attributes that are containers already |
|
23 | # You have to make sure that attributes that are containers already | |
24 | # exist before using them. Simple assigning a new list will override |
|
24 | # exist before using them. Simple assigning a new list will override | |
25 | # all previous values. |
|
25 | # all previous values. | |
26 | if hasattr(c.Global, 'exec_lines'): |
|
26 | if hasattr(c.Global, 'exec_lines'): | |
27 | c.Global.exec_lines.append(lines) |
|
27 | c.Global.exec_lines.append(lines) | |
28 | else: |
|
28 | else: | |
29 | c.Global.exec_lines = [lines] No newline at end of file |
|
29 | c.Global.exec_lines = [lines] |
This diff has been collapsed as it changes many lines, (637 lines changed) Show them Hide them | |||||
@@ -1,2523 +1,2024 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Main IPython class.""" |
|
2 | """Main IPython class.""" | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> | |
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | |
7 | # Copyright (C) 2008-2010 The IPython Development Team |
|
7 | # Copyright (C) 2008-2010 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | from __future__ import with_statement |
|
17 | from __future__ import with_statement | |
18 | from __future__ import absolute_import |
|
18 | from __future__ import absolute_import | |
19 |
|
19 | |||
20 | import __builtin__ |
|
20 | import __builtin__ | |
21 | import abc |
|
21 | import abc | |
22 | import bdb |
|
|||
23 | import codeop |
|
22 | import codeop | |
24 | import exceptions |
|
23 | import exceptions | |
25 | import new |
|
24 | import new | |
26 | import os |
|
25 | import os | |
27 | import re |
|
26 | import re | |
28 | import string |
|
27 | import string | |
29 | import sys |
|
28 | import sys | |
30 | import tempfile |
|
29 | import tempfile | |
31 | from contextlib import nested |
|
30 | from contextlib import nested | |
32 |
|
31 | |||
33 | from IPython.core import debugger, oinspect |
|
32 | from IPython.core import debugger, oinspect | |
34 | from IPython.core import history as ipcorehist |
|
33 | from IPython.core import history as ipcorehist | |
35 | from IPython.core import prefilter |
|
34 | from IPython.core import prefilter | |
36 | from IPython.core import shadowns |
|
35 | from IPython.core import shadowns | |
37 | from IPython.core import ultratb |
|
36 | from IPython.core import ultratb | |
38 | from IPython.core.alias import AliasManager |
|
37 | from IPython.core.alias import AliasManager | |
39 | from IPython.core.builtin_trap import BuiltinTrap |
|
38 | from IPython.core.builtin_trap import BuiltinTrap | |
40 | from IPython.config.configurable import Configurable |
|
39 | from IPython.config.configurable import Configurable | |
41 | from IPython.core.display_trap import DisplayTrap |
|
40 | from IPython.core.display_trap import DisplayTrap | |
42 |
from IPython.core.error import |
|
41 | from IPython.core.error import UsageError | |
43 | from IPython.core.extensions import ExtensionManager |
|
42 | from IPython.core.extensions import ExtensionManager | |
44 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict |
|
43 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict | |
|
44 | from IPython.core.inputlist import InputList | |||
45 | from IPython.core.logger import Logger |
|
45 | from IPython.core.logger import Logger | |
46 | from IPython.core.magic import Magic |
|
46 | from IPython.core.magic import Magic | |
47 | from IPython.core.plugin import PluginManager |
|
47 | from IPython.core.plugin import PluginManager | |
48 | from IPython.core.prefilter import PrefilterManager |
|
48 | from IPython.core.prefilter import PrefilterManager | |
49 | from IPython.core.prompts import CachedOutput |
|
49 | from IPython.core.prompts import CachedOutput | |
50 | from IPython.core.usage import interactive_usage, default_banner |
|
|||
51 | import IPython.core.hooks |
|
50 | import IPython.core.hooks | |
52 | from IPython.external.Itpl import ItplNS |
|
51 | from IPython.external.Itpl import ItplNS | |
53 | from IPython.lib.inputhook import enable_gui |
|
|||
54 | from IPython.lib.backgroundjobs import BackgroundJobManager |
|
|||
55 | from IPython.lib.pylabtools import pylab_activate |
|
|||
56 | from IPython.utils import PyColorize |
|
52 | from IPython.utils import PyColorize | |
57 | from IPython.utils import pickleshare |
|
53 | from IPython.utils import pickleshare | |
58 | from IPython.utils.doctestreload import doctest_reload |
|
54 | from IPython.utils.doctestreload import doctest_reload | |
59 | from IPython.utils.ipstruct import Struct |
|
55 | from IPython.utils.ipstruct import Struct | |
60 | from IPython.utils.io import Term, ask_yes_no |
|
56 | from IPython.utils.io import Term, ask_yes_no | |
61 | from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError |
|
57 | from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError | |
62 |
from IPython.utils.process import |
|
58 | from IPython.utils.process import getoutput, getoutputerror | |
63 | abbrev_cwd, |
|
|||
64 | getoutput, |
|
|||
65 | getoutputerror |
|
|||
66 | ) |
|
|||
67 | # import IPython.utils.rlineimpl as readline |
|
|||
68 | from IPython.utils.strdispatch import StrDispatch |
|
59 | from IPython.utils.strdispatch import StrDispatch | |
69 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
60 | from IPython.utils.syspathcontext import prepended_to_syspath | |
70 | from IPython.utils.terminal import toggle_set_term_title, set_term_title |
|
61 | from IPython.utils.text import num_ini_spaces | |
71 | from IPython.utils.warn import warn, error, fatal |
|
62 | from IPython.utils.warn import warn, error, fatal | |
72 | from IPython.utils.traitlets import ( |
|
63 | from IPython.utils.traitlets import ( | |
73 | Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode, Instance |
|
64 | Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode, Instance | |
74 | ) |
|
65 | ) | |
75 |
|
66 | |||
76 | # from IPython.utils import growl |
|
67 | # from IPython.utils import growl | |
77 | # growl.start("IPython") |
|
68 | # growl.start("IPython") | |
78 |
|
69 | |||
79 | #----------------------------------------------------------------------------- |
|
70 | #----------------------------------------------------------------------------- | |
80 | # Globals |
|
71 | # Globals | |
81 | #----------------------------------------------------------------------------- |
|
72 | #----------------------------------------------------------------------------- | |
82 |
|
73 | |||
83 | # store the builtin raw_input globally, and use this always, in case user code |
|
|||
84 | # overwrites it (like wx.py.PyShell does) |
|
|||
85 | raw_input_original = raw_input |
|
|||
86 |
|
||||
87 | # compiled regexps for autoindent management |
|
74 | # compiled regexps for autoindent management | |
88 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
75 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
89 |
|
76 | |||
90 | #----------------------------------------------------------------------------- |
|
77 | #----------------------------------------------------------------------------- | |
91 | # Utilities |
|
78 | # Utilities | |
92 | #----------------------------------------------------------------------------- |
|
79 | #----------------------------------------------------------------------------- | |
93 |
|
80 | |||
94 | ini_spaces_re = re.compile(r'^(\s+)') |
|
81 | # store the builtin raw_input globally, and use this always, in case user code | |
95 |
|
82 | # overwrites it (like wx.py.PyShell does) | ||
96 |
|
83 | raw_input_original = raw_input | ||
97 | def num_ini_spaces(strng): |
|
|||
98 | """Return the number of initial spaces in a string""" |
|
|||
99 |
|
||||
100 | ini_spaces = ini_spaces_re.match(strng) |
|
|||
101 | if ini_spaces: |
|
|||
102 | return ini_spaces.end() |
|
|||
103 | else: |
|
|||
104 | return 0 |
|
|||
105 |
|
||||
106 |
|
84 | |||
107 | def softspace(file, newvalue): |
|
85 | def softspace(file, newvalue): | |
108 | """Copied from code.py, to remove the dependency""" |
|
86 | """Copied from code.py, to remove the dependency""" | |
109 |
|
87 | |||
110 | oldvalue = 0 |
|
88 | oldvalue = 0 | |
111 | try: |
|
89 | try: | |
112 | oldvalue = file.softspace |
|
90 | oldvalue = file.softspace | |
113 | except AttributeError: |
|
91 | except AttributeError: | |
114 | pass |
|
92 | pass | |
115 | try: |
|
93 | try: | |
116 | file.softspace = newvalue |
|
94 | file.softspace = newvalue | |
117 | except (AttributeError, TypeError): |
|
95 | except (AttributeError, TypeError): | |
118 | # "attribute-less object" or "read-only attributes" |
|
96 | # "attribute-less object" or "read-only attributes" | |
119 | pass |
|
97 | pass | |
120 | return oldvalue |
|
98 | return oldvalue | |
121 |
|
99 | |||
122 |
|
100 | |||
123 | def no_op(*a, **kw): pass |
|
101 | def no_op(*a, **kw): pass | |
124 |
|
102 | |||
125 | class SpaceInInput(exceptions.Exception): pass |
|
103 | class SpaceInInput(exceptions.Exception): pass | |
126 |
|
104 | |||
127 | class Bunch: pass |
|
105 | class Bunch: pass | |
128 |
|
106 | |||
129 | class InputList(list): |
|
|||
130 | """Class to store user input. |
|
|||
131 |
|
||||
132 | It's basically a list, but slices return a string instead of a list, thus |
|
|||
133 | allowing things like (assuming 'In' is an instance): |
|
|||
134 |
|
||||
135 | exec In[4:7] |
|
|||
136 |
|
||||
137 | or |
|
|||
138 |
|
||||
139 | exec In[5:9] + In[14] + In[21:25]""" |
|
|||
140 |
|
||||
141 | def __getslice__(self,i,j): |
|
|||
142 | return ''.join(list.__getslice__(self,i,j)) |
|
|||
143 |
|
||||
144 |
|
||||
145 | class SyntaxTB(ultratb.ListTB): |
|
107 | class SyntaxTB(ultratb.ListTB): | |
146 | """Extension which holds some state: the last exception value""" |
|
108 | """Extension which holds some state: the last exception value""" | |
147 |
|
109 | |||
148 | def __init__(self,color_scheme = 'NoColor'): |
|
110 | def __init__(self,color_scheme = 'NoColor'): | |
149 | ultratb.ListTB.__init__(self,color_scheme) |
|
111 | ultratb.ListTB.__init__(self,color_scheme) | |
150 | self.last_syntax_error = None |
|
112 | self.last_syntax_error = None | |
151 |
|
113 | |||
152 | def __call__(self, etype, value, elist): |
|
114 | def __call__(self, etype, value, elist): | |
153 | self.last_syntax_error = value |
|
115 | self.last_syntax_error = value | |
154 | ultratb.ListTB.__call__(self,etype,value,elist) |
|
116 | ultratb.ListTB.__call__(self,etype,value,elist) | |
155 |
|
117 | |||
156 | def clear_err_state(self): |
|
118 | def clear_err_state(self): | |
157 | """Return the current error state and clear it""" |
|
119 | """Return the current error state and clear it""" | |
158 | e = self.last_syntax_error |
|
120 | e = self.last_syntax_error | |
159 | self.last_syntax_error = None |
|
121 | self.last_syntax_error = None | |
160 | return e |
|
122 | return e | |
161 |
|
123 | |||
162 |
|
124 | |||
163 | def get_default_editor(): |
|
|||
164 | try: |
|
|||
165 | ed = os.environ['EDITOR'] |
|
|||
166 | except KeyError: |
|
|||
167 | if os.name == 'posix': |
|
|||
168 | ed = 'vi' # the only one guaranteed to be there! |
|
|||
169 | else: |
|
|||
170 | ed = 'notepad' # same in Windows! |
|
|||
171 | return ed |
|
|||
172 |
|
||||
173 |
|
||||
174 | def get_default_colors(): |
|
125 | def get_default_colors(): | |
175 | if sys.platform=='darwin': |
|
126 | if sys.platform=='darwin': | |
176 | return "LightBG" |
|
127 | return "LightBG" | |
177 | elif os.name=='nt': |
|
128 | elif os.name=='nt': | |
178 | return 'Linux' |
|
129 | return 'Linux' | |
179 | else: |
|
130 | else: | |
180 | return 'Linux' |
|
131 | return 'Linux' | |
181 |
|
132 | |||
182 |
|
133 | |||
183 | class SeparateStr(Str): |
|
|||
184 | """A Str subclass to validate separate_in, separate_out, etc. |
|
|||
185 |
|
||||
186 | This is a Str based trait that converts '0'->'' and '\\n'->'\n'. |
|
|||
187 | """ |
|
|||
188 |
|
||||
189 | def validate(self, obj, value): |
|
|||
190 | if value == '0': value = '' |
|
|||
191 | value = value.replace('\\n','\n') |
|
|||
192 | return super(SeparateStr, self).validate(obj, value) |
|
|||
193 |
|
||||
194 |
|
||||
195 | #----------------------------------------------------------------------------- |
|
134 | #----------------------------------------------------------------------------- | |
196 | # Main IPython class |
|
135 | # Main IPython class | |
197 | #----------------------------------------------------------------------------- |
|
136 | #----------------------------------------------------------------------------- | |
198 |
|
137 | |||
199 |
|
138 | |||
200 | class InteractiveShell(Configurable, Magic): |
|
139 | class InteractiveShell(Configurable, Magic): | |
201 | """An enhanced, interactive shell for Python.""" |
|
140 | """An enhanced, interactive shell for Python.""" | |
202 |
|
141 | |||
203 | autocall = Enum((0,1,2), default_value=1, config=True) |
|
142 | autocall = Enum((0,1,2), default_value=1, config=True) | |
204 | autoedit_syntax = CBool(False, config=True) |
|
|||
205 | autoindent = CBool(True, config=True) |
|
|||
206 | automagic = CBool(True, config=True) |
|
143 | automagic = CBool(True, config=True) | |
207 | banner = Str('') |
|
|||
208 | banner1 = Str(default_banner, config=True) |
|
|||
209 | banner2 = Str('', config=True) |
|
|||
210 | cache_size = Int(1000, config=True) |
|
144 | cache_size = Int(1000, config=True) | |
211 | color_info = CBool(True, config=True) |
|
145 | color_info = CBool(True, config=True) | |
212 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), |
|
146 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), | |
213 | default_value=get_default_colors(), config=True) |
|
147 | default_value=get_default_colors(), config=True) | |
214 | confirm_exit = CBool(True, config=True) |
|
|||
215 | debug = CBool(False, config=True) |
|
148 | debug = CBool(False, config=True) | |
216 | deep_reload = CBool(False, config=True) |
|
149 | deep_reload = CBool(False, config=True) | |
217 | # This display_banner only controls whether or not self.show_banner() |
|
|||
218 | # is called when mainloop/interact are called. The default is False |
|
|||
219 | # because for the terminal based application, the banner behavior |
|
|||
220 | # is controlled by Global.display_banner, which IPythonApp looks at |
|
|||
221 | # to determine if *it* should call show_banner() by hand or not. |
|
|||
222 | display_banner = CBool(False) # This isn't configurable! |
|
|||
223 | embedded = CBool(False) |
|
|||
224 | embedded_active = CBool(False) |
|
|||
225 | editor = Str(get_default_editor(), config=True) |
|
|||
226 | filename = Str("<ipython console>") |
|
150 | filename = Str("<ipython console>") | |
227 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ |
|
151 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ | |
228 | logstart = CBool(False, config=True) |
|
152 | logstart = CBool(False, config=True) | |
229 | logfile = Str('', config=True) |
|
153 | logfile = Str('', config=True) | |
230 | logappend = Str('', config=True) |
|
154 | logappend = Str('', config=True) | |
231 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
155 | object_info_string_level = Enum((0,1,2), default_value=0, | |
232 | config=True) |
|
156 | config=True) | |
233 | pager = Str('less', config=True) |
|
|||
234 | pdb = CBool(False, config=True) |
|
157 | pdb = CBool(False, config=True) | |
235 | pprint = CBool(True, config=True) |
|
158 | pprint = CBool(True, config=True) | |
236 | profile = Str('', config=True) |
|
159 | profile = Str('', config=True) | |
237 | prompt_in1 = Str('In [\\#]: ', config=True) |
|
160 | prompt_in1 = Str('In [\\#]: ', config=True) | |
238 | prompt_in2 = Str(' .\\D.: ', config=True) |
|
161 | prompt_in2 = Str(' .\\D.: ', config=True) | |
239 | prompt_out = Str('Out[\\#]: ', config=True) |
|
162 | prompt_out = Str('Out[\\#]: ', config=True) | |
240 | prompts_pad_left = CBool(True, config=True) |
|
163 | prompts_pad_left = CBool(True, config=True) | |
241 | quiet = CBool(False, config=True) |
|
164 | quiet = CBool(False, config=True) | |
242 |
|
165 | |||
|
166 | # The readline stuff will eventually be moved to the terminal subclass | |||
|
167 | # but for now, we can't do that as readline is welded in everywhere. | |||
243 | readline_use = CBool(True, config=True) |
|
168 | readline_use = CBool(True, config=True) | |
244 | readline_merge_completions = CBool(True, config=True) |
|
169 | readline_merge_completions = CBool(True, config=True) | |
245 | readline_omit__names = Enum((0,1,2), default_value=0, config=True) |
|
170 | readline_omit__names = Enum((0,1,2), default_value=0, config=True) | |
246 | readline_remove_delims = Str('-/~', config=True) |
|
171 | readline_remove_delims = Str('-/~', config=True) | |
247 | readline_parse_and_bind = List([ |
|
172 | readline_parse_and_bind = List([ | |
248 | 'tab: complete', |
|
173 | 'tab: complete', | |
249 | '"\C-l": clear-screen', |
|
174 | '"\C-l": clear-screen', | |
250 | 'set show-all-if-ambiguous on', |
|
175 | 'set show-all-if-ambiguous on', | |
251 | '"\C-o": tab-insert', |
|
176 | '"\C-o": tab-insert', | |
252 | '"\M-i": " "', |
|
177 | '"\M-i": " "', | |
253 | '"\M-o": "\d\d\d\d"', |
|
178 | '"\M-o": "\d\d\d\d"', | |
254 | '"\M-I": "\d\d\d\d"', |
|
179 | '"\M-I": "\d\d\d\d"', | |
255 | '"\C-r": reverse-search-history', |
|
180 | '"\C-r": reverse-search-history', | |
256 | '"\C-s": forward-search-history', |
|
181 | '"\C-s": forward-search-history', | |
257 | '"\C-p": history-search-backward', |
|
182 | '"\C-p": history-search-backward', | |
258 | '"\C-n": history-search-forward', |
|
183 | '"\C-n": history-search-forward', | |
259 | '"\e[A": history-search-backward', |
|
184 | '"\e[A": history-search-backward', | |
260 | '"\e[B": history-search-forward', |
|
185 | '"\e[B": history-search-forward', | |
261 | '"\C-k": kill-line', |
|
186 | '"\C-k": kill-line', | |
262 | '"\C-u": unix-line-discard', |
|
187 | '"\C-u": unix-line-discard', | |
263 | ], allow_none=False, config=True) |
|
188 | ], allow_none=False, config=True) | |
264 |
|
189 | |||
265 | screen_length = Int(0, config=True) |
|
|||
266 |
|
||||
267 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
|||
268 | separate_in = SeparateStr('\n', config=True) |
|
|||
269 | separate_out = SeparateStr('', config=True) |
|
|||
270 | separate_out2 = SeparateStr('', config=True) |
|
|||
271 |
|
||||
272 | system_header = Str('IPython system call: ', config=True) |
|
190 | system_header = Str('IPython system call: ', config=True) | |
273 | system_verbose = CBool(False, config=True) |
|
191 | system_verbose = CBool(False, config=True) | |
274 | term_title = CBool(False, config=True) |
|
|||
275 | wildcards_case_sensitive = CBool(True, config=True) |
|
192 | wildcards_case_sensitive = CBool(True, config=True) | |
276 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
193 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | |
277 | default_value='Context', config=True) |
|
194 | default_value='Context', config=True) | |
278 |
|
195 | |||
279 | autoexec = List(allow_none=False) |
|
|||
280 |
|
||||
281 | # class attribute to indicate whether the class supports threads or not. |
|
|||
282 | # Subclasses with thread support should override this as needed. |
|
|||
283 | isthreaded = False |
|
|||
284 |
|
||||
285 | # Subcomponents of InteractiveShell |
|
196 | # Subcomponents of InteractiveShell | |
286 | alias_manager = Instance('IPython.core.alias.AliasManager') |
|
197 | alias_manager = Instance('IPython.core.alias.AliasManager') | |
287 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') |
|
198 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') | |
288 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') |
|
199 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') | |
289 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') |
|
200 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') | |
290 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') |
|
201 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') | |
291 | plugin_manager = Instance('IPython.core.plugin.PluginManager') |
|
202 | plugin_manager = Instance('IPython.core.plugin.PluginManager') | |
292 |
|
203 | |||
293 |
def __init__(self, config=None, ipython_dir=None, |
|
204 | def __init__(self, config=None, ipython_dir=None, | |
294 | user_ns=None, user_global_ns=None, |
|
205 | user_ns=None, user_global_ns=None, | |
295 | banner1=None, banner2=None, display_banner=None, |
|
|||
296 | custom_exceptions=((),None)): |
|
206 | custom_exceptions=((),None)): | |
297 |
|
207 | |||
298 | # This is where traits with a config_key argument are updated |
|
208 | # This is where traits with a config_key argument are updated | |
299 | # from the values on config. |
|
209 | # from the values on config. | |
300 | super(InteractiveShell, self).__init__(config=config) |
|
210 | super(InteractiveShell, self).__init__(config=config) | |
301 |
|
211 | |||
302 | # These are relatively independent and stateless |
|
212 | # These are relatively independent and stateless | |
303 | self.init_ipython_dir(ipython_dir) |
|
213 | self.init_ipython_dir(ipython_dir) | |
304 | self.init_instance_attrs() |
|
214 | self.init_instance_attrs() | |
305 | self.init_term_title() |
|
|||
306 | self.init_usage(usage) |
|
|||
307 | self.init_banner(banner1, banner2, display_banner) |
|
|||
308 |
|
215 | |||
309 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
216 | # Create namespaces (user_ns, user_global_ns, etc.) | |
310 | self.init_create_namespaces(user_ns, user_global_ns) |
|
217 | self.init_create_namespaces(user_ns, user_global_ns) | |
311 | # This has to be done after init_create_namespaces because it uses |
|
218 | # This has to be done after init_create_namespaces because it uses | |
312 | # something in self.user_ns, but before init_sys_modules, which |
|
219 | # something in self.user_ns, but before init_sys_modules, which | |
313 | # is the first thing to modify sys. |
|
220 | # is the first thing to modify sys. | |
314 | self.save_sys_module_state() |
|
221 | self.save_sys_module_state() | |
315 | self.init_sys_modules() |
|
222 | self.init_sys_modules() | |
316 |
|
223 | |||
317 | self.init_history() |
|
224 | self.init_history() | |
318 | self.init_encoding() |
|
225 | self.init_encoding() | |
319 | self.init_prefilter() |
|
226 | self.init_prefilter() | |
320 |
|
227 | |||
321 | Magic.__init__(self, self) |
|
228 | Magic.__init__(self, self) | |
322 |
|
229 | |||
323 | self.init_syntax_highlighting() |
|
230 | self.init_syntax_highlighting() | |
324 | self.init_hooks() |
|
231 | self.init_hooks() | |
325 | self.init_pushd_popd_magic() |
|
232 | self.init_pushd_popd_magic() | |
326 | self.init_traceback_handlers(custom_exceptions) |
|
233 | self.init_traceback_handlers(custom_exceptions) | |
327 | self.init_user_ns() |
|
234 | self.init_user_ns() | |
328 | self.init_logger() |
|
235 | self.init_logger() | |
329 | self.init_alias() |
|
236 | self.init_alias() | |
330 | self.init_builtins() |
|
237 | self.init_builtins() | |
331 |
|
238 | |||
332 | # pre_config_initialization |
|
239 | # pre_config_initialization | |
333 | self.init_shadow_hist() |
|
240 | self.init_shadow_hist() | |
334 |
|
241 | |||
335 | # The next section should contain averything that was in ipmaker. |
|
242 | # The next section should contain averything that was in ipmaker. | |
336 | self.init_logstart() |
|
243 | self.init_logstart() | |
337 |
|
244 | |||
338 | # The following was in post_config_initialization |
|
245 | # The following was in post_config_initialization | |
339 | self.init_inspector() |
|
246 | self.init_inspector() | |
340 | self.init_readline() |
|
247 | self.init_readline() | |
341 | self.init_prompts() |
|
248 | self.init_prompts() | |
342 | self.init_displayhook() |
|
249 | self.init_displayhook() | |
343 | self.init_reload_doctest() |
|
250 | self.init_reload_doctest() | |
344 | self.init_magics() |
|
251 | self.init_magics() | |
345 | self.init_pdb() |
|
252 | self.init_pdb() | |
346 | self.init_extension_manager() |
|
253 | self.init_extension_manager() | |
347 | self.init_plugin_manager() |
|
254 | self.init_plugin_manager() | |
348 | self.hooks.late_startup_hook() |
|
255 | self.hooks.late_startup_hook() | |
349 |
|
256 | |||
350 | @classmethod |
|
257 | @classmethod | |
351 | def instance(cls, *args, **kwargs): |
|
258 | def instance(cls, *args, **kwargs): | |
352 | """Returns a global InteractiveShell instance.""" |
|
259 | """Returns a global InteractiveShell instance.""" | |
353 | if not hasattr(cls, "_instance"): |
|
260 | if not hasattr(cls, "_instance"): | |
354 | cls._instance = cls(*args, **kwargs) |
|
261 | cls._instance = cls(*args, **kwargs) | |
355 | return cls._instance |
|
262 | return cls._instance | |
356 |
|
263 | |||
357 | @classmethod |
|
264 | @classmethod | |
358 | def initialized(cls): |
|
265 | def initialized(cls): | |
359 | return hasattr(cls, "_instance") |
|
266 | return hasattr(cls, "_instance") | |
360 |
|
267 | |||
361 | def get_ipython(self): |
|
268 | def get_ipython(self): | |
362 | """Return the currently running IPython instance.""" |
|
269 | """Return the currently running IPython instance.""" | |
363 | return self |
|
270 | return self | |
364 |
|
271 | |||
365 | #------------------------------------------------------------------------- |
|
272 | #------------------------------------------------------------------------- | |
366 | # Trait changed handlers |
|
273 | # Trait changed handlers | |
367 | #------------------------------------------------------------------------- |
|
274 | #------------------------------------------------------------------------- | |
368 |
|
275 | |||
369 | def _banner1_changed(self): |
|
|||
370 | self.compute_banner() |
|
|||
371 |
|
||||
372 | def _banner2_changed(self): |
|
|||
373 | self.compute_banner() |
|
|||
374 |
|
||||
375 | def _ipython_dir_changed(self, name, new): |
|
276 | def _ipython_dir_changed(self, name, new): | |
376 | if not os.path.isdir(new): |
|
277 | if not os.path.isdir(new): | |
377 | os.makedirs(new, mode = 0777) |
|
278 | os.makedirs(new, mode = 0777) | |
378 |
|
279 | |||
379 | @property |
|
|||
380 | def usable_screen_length(self): |
|
|||
381 | if self.screen_length == 0: |
|
|||
382 | return 0 |
|
|||
383 | else: |
|
|||
384 | num_lines_bot = self.separate_in.count('\n')+1 |
|
|||
385 | return self.screen_length - num_lines_bot |
|
|||
386 |
|
||||
387 | def _term_title_changed(self, name, new_value): |
|
|||
388 | self.init_term_title() |
|
|||
389 |
|
||||
390 | def set_autoindent(self,value=None): |
|
280 | def set_autoindent(self,value=None): | |
391 | """Set the autoindent flag, checking for readline support. |
|
281 | """Set the autoindent flag, checking for readline support. | |
392 |
|
282 | |||
393 | If called with no arguments, it acts as a toggle.""" |
|
283 | If called with no arguments, it acts as a toggle.""" | |
394 |
|
284 | |||
395 | if not self.has_readline: |
|
285 | if not self.has_readline: | |
396 | if os.name == 'posix': |
|
286 | if os.name == 'posix': | |
397 | warn("The auto-indent feature requires the readline library") |
|
287 | warn("The auto-indent feature requires the readline library") | |
398 | self.autoindent = 0 |
|
288 | self.autoindent = 0 | |
399 | return |
|
289 | return | |
400 | if value is None: |
|
290 | if value is None: | |
401 | self.autoindent = not self.autoindent |
|
291 | self.autoindent = not self.autoindent | |
402 | else: |
|
292 | else: | |
403 | self.autoindent = value |
|
293 | self.autoindent = value | |
404 |
|
294 | |||
405 | #------------------------------------------------------------------------- |
|
295 | #------------------------------------------------------------------------- | |
406 | # init_* methods called by __init__ |
|
296 | # init_* methods called by __init__ | |
407 | #------------------------------------------------------------------------- |
|
297 | #------------------------------------------------------------------------- | |
408 |
|
298 | |||
409 | def init_ipython_dir(self, ipython_dir): |
|
299 | def init_ipython_dir(self, ipython_dir): | |
410 | if ipython_dir is not None: |
|
300 | if ipython_dir is not None: | |
411 | self.ipython_dir = ipython_dir |
|
301 | self.ipython_dir = ipython_dir | |
412 | self.config.Global.ipython_dir = self.ipython_dir |
|
302 | self.config.Global.ipython_dir = self.ipython_dir | |
413 | return |
|
303 | return | |
414 |
|
304 | |||
415 | if hasattr(self.config.Global, 'ipython_dir'): |
|
305 | if hasattr(self.config.Global, 'ipython_dir'): | |
416 | self.ipython_dir = self.config.Global.ipython_dir |
|
306 | self.ipython_dir = self.config.Global.ipython_dir | |
417 | else: |
|
307 | else: | |
418 | self.ipython_dir = get_ipython_dir() |
|
308 | self.ipython_dir = get_ipython_dir() | |
419 |
|
309 | |||
420 | # All children can just read this |
|
310 | # All children can just read this | |
421 | self.config.Global.ipython_dir = self.ipython_dir |
|
311 | self.config.Global.ipython_dir = self.ipython_dir | |
422 |
|
312 | |||
423 | def init_instance_attrs(self): |
|
313 | def init_instance_attrs(self): | |
424 | self.jobs = BackgroundJobManager() |
|
|||
425 | self.more = False |
|
314 | self.more = False | |
426 |
|
315 | |||
427 | # command compiler |
|
316 | # command compiler | |
428 | self.compile = codeop.CommandCompiler() |
|
317 | self.compile = codeop.CommandCompiler() | |
429 |
|
318 | |||
430 | # User input buffer |
|
319 | # User input buffer | |
431 | self.buffer = [] |
|
320 | self.buffer = [] | |
432 |
|
321 | |||
433 | # Make an empty namespace, which extension writers can rely on both |
|
322 | # Make an empty namespace, which extension writers can rely on both | |
434 | # existing and NEVER being used by ipython itself. This gives them a |
|
323 | # existing and NEVER being used by ipython itself. This gives them a | |
435 | # convenient location for storing additional information and state |
|
324 | # convenient location for storing additional information and state | |
436 | # their extensions may require, without fear of collisions with other |
|
325 | # their extensions may require, without fear of collisions with other | |
437 | # ipython names that may develop later. |
|
326 | # ipython names that may develop later. | |
438 | self.meta = Struct() |
|
327 | self.meta = Struct() | |
439 |
|
328 | |||
440 | # Object variable to store code object waiting execution. This is |
|
329 | # Object variable to store code object waiting execution. This is | |
441 | # used mainly by the multithreaded shells, but it can come in handy in |
|
330 | # used mainly by the multithreaded shells, but it can come in handy in | |
442 | # other situations. No need to use a Queue here, since it's a single |
|
331 | # other situations. No need to use a Queue here, since it's a single | |
443 | # item which gets cleared once run. |
|
332 | # item which gets cleared once run. | |
444 | self.code_to_run = None |
|
333 | self.code_to_run = None | |
445 |
|
334 | |||
446 | # Flag to mark unconditional exit |
|
|||
447 | self.exit_now = False |
|
|||
448 |
|
||||
449 | # Temporary files used for various purposes. Deleted at exit. |
|
335 | # Temporary files used for various purposes. Deleted at exit. | |
450 | self.tempfiles = [] |
|
336 | self.tempfiles = [] | |
451 |
|
337 | |||
452 | # Keep track of readline usage (later set by init_readline) |
|
338 | # Keep track of readline usage (later set by init_readline) | |
453 | self.has_readline = False |
|
339 | self.has_readline = False | |
454 |
|
340 | |||
455 | # keep track of where we started running (mainly for crash post-mortem) |
|
341 | # keep track of where we started running (mainly for crash post-mortem) | |
456 | # This is not being used anywhere currently. |
|
342 | # This is not being used anywhere currently. | |
457 | self.starting_dir = os.getcwd() |
|
343 | self.starting_dir = os.getcwd() | |
458 |
|
344 | |||
459 | # Indentation management |
|
345 | # Indentation management | |
460 | self.indent_current_nsp = 0 |
|
346 | self.indent_current_nsp = 0 | |
461 |
|
347 | |||
462 | def init_term_title(self): |
|
|||
463 | # Enable or disable the terminal title. |
|
|||
464 | if self.term_title: |
|
|||
465 | toggle_set_term_title(True) |
|
|||
466 | set_term_title('IPython: ' + abbrev_cwd()) |
|
|||
467 | else: |
|
|||
468 | toggle_set_term_title(False) |
|
|||
469 |
|
||||
470 | def init_usage(self, usage=None): |
|
|||
471 | if usage is None: |
|
|||
472 | self.usage = interactive_usage |
|
|||
473 | else: |
|
|||
474 | self.usage = usage |
|
|||
475 |
|
||||
476 | def init_encoding(self): |
|
348 | def init_encoding(self): | |
477 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
349 | # Get system encoding at startup time. Certain terminals (like Emacs | |
478 | # under Win32 have it set to None, and we need to have a known valid |
|
350 | # under Win32 have it set to None, and we need to have a known valid | |
479 | # encoding to use in the raw_input() method |
|
351 | # encoding to use in the raw_input() method | |
480 | try: |
|
352 | try: | |
481 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
353 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
482 | except AttributeError: |
|
354 | except AttributeError: | |
483 | self.stdin_encoding = 'ascii' |
|
355 | self.stdin_encoding = 'ascii' | |
484 |
|
356 | |||
485 | def init_syntax_highlighting(self): |
|
357 | def init_syntax_highlighting(self): | |
486 | # Python source parser/formatter for syntax highlighting |
|
358 | # Python source parser/formatter for syntax highlighting | |
487 | pyformat = PyColorize.Parser().format |
|
359 | pyformat = PyColorize.Parser().format | |
488 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
360 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) | |
489 |
|
361 | |||
490 | def init_pushd_popd_magic(self): |
|
362 | def init_pushd_popd_magic(self): | |
491 | # for pushd/popd management |
|
363 | # for pushd/popd management | |
492 | try: |
|
364 | try: | |
493 | self.home_dir = get_home_dir() |
|
365 | self.home_dir = get_home_dir() | |
494 | except HomeDirError, msg: |
|
366 | except HomeDirError, msg: | |
495 | fatal(msg) |
|
367 | fatal(msg) | |
496 |
|
368 | |||
497 | self.dir_stack = [] |
|
369 | self.dir_stack = [] | |
498 |
|
370 | |||
499 | def init_logger(self): |
|
371 | def init_logger(self): | |
500 | self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') |
|
372 | self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') | |
501 | # local shortcut, this is used a LOT |
|
373 | # local shortcut, this is used a LOT | |
502 | self.log = self.logger.log |
|
374 | self.log = self.logger.log | |
503 |
|
375 | |||
504 | def init_logstart(self): |
|
376 | def init_logstart(self): | |
505 | if self.logappend: |
|
377 | if self.logappend: | |
506 | self.magic_logstart(self.logappend + ' append') |
|
378 | self.magic_logstart(self.logappend + ' append') | |
507 | elif self.logfile: |
|
379 | elif self.logfile: | |
508 | self.magic_logstart(self.logfile) |
|
380 | self.magic_logstart(self.logfile) | |
509 | elif self.logstart: |
|
381 | elif self.logstart: | |
510 | self.magic_logstart() |
|
382 | self.magic_logstart() | |
511 |
|
383 | |||
512 | def init_builtins(self): |
|
384 | def init_builtins(self): | |
513 | self.builtin_trap = BuiltinTrap(shell=self) |
|
385 | self.builtin_trap = BuiltinTrap(shell=self) | |
514 |
|
386 | |||
515 | def init_inspector(self): |
|
387 | def init_inspector(self): | |
516 | # Object inspector |
|
388 | # Object inspector | |
517 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
389 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
518 | PyColorize.ANSICodeColors, |
|
390 | PyColorize.ANSICodeColors, | |
519 | 'NoColor', |
|
391 | 'NoColor', | |
520 | self.object_info_string_level) |
|
392 | self.object_info_string_level) | |
521 |
|
393 | |||
522 | def init_prompts(self): |
|
394 | def init_prompts(self): | |
523 | # Initialize cache, set in/out prompts and printing system |
|
395 | # Initialize cache, set in/out prompts and printing system | |
524 | self.outputcache = CachedOutput(self, |
|
396 | self.outputcache = CachedOutput(self, | |
525 | self.cache_size, |
|
397 | self.cache_size, | |
526 | self.pprint, |
|
398 | self.pprint, | |
527 | input_sep = self.separate_in, |
|
399 | input_sep = self.separate_in, | |
528 | output_sep = self.separate_out, |
|
400 | output_sep = self.separate_out, | |
529 | output_sep2 = self.separate_out2, |
|
401 | output_sep2 = self.separate_out2, | |
530 | ps1 = self.prompt_in1, |
|
402 | ps1 = self.prompt_in1, | |
531 | ps2 = self.prompt_in2, |
|
403 | ps2 = self.prompt_in2, | |
532 | ps_out = self.prompt_out, |
|
404 | ps_out = self.prompt_out, | |
533 | pad_left = self.prompts_pad_left) |
|
405 | pad_left = self.prompts_pad_left) | |
534 |
|
406 | |||
535 | # user may have over-ridden the default print hook: |
|
407 | # user may have over-ridden the default print hook: | |
536 | try: |
|
408 | try: | |
537 | self.outputcache.__class__.display = self.hooks.display |
|
409 | self.outputcache.__class__.display = self.hooks.display | |
538 | except AttributeError: |
|
410 | except AttributeError: | |
539 | pass |
|
411 | pass | |
540 |
|
412 | |||
541 | def init_displayhook(self): |
|
413 | def init_displayhook(self): | |
542 | self.display_trap = DisplayTrap(hook=self.outputcache) |
|
414 | self.display_trap = DisplayTrap(hook=self.outputcache) | |
543 |
|
415 | |||
544 | def init_reload_doctest(self): |
|
416 | def init_reload_doctest(self): | |
545 | # Do a proper resetting of doctest, including the necessary displayhook |
|
417 | # Do a proper resetting of doctest, including the necessary displayhook | |
546 | # monkeypatching |
|
418 | # monkeypatching | |
547 | try: |
|
419 | try: | |
548 | doctest_reload() |
|
420 | doctest_reload() | |
549 | except ImportError: |
|
421 | except ImportError: | |
550 | warn("doctest module does not exist.") |
|
422 | warn("doctest module does not exist.") | |
551 |
|
423 | |||
552 | #------------------------------------------------------------------------- |
|
424 | #------------------------------------------------------------------------- | |
553 | # Things related to the banner |
|
|||
554 | #------------------------------------------------------------------------- |
|
|||
555 |
|
||||
556 | def init_banner(self, banner1, banner2, display_banner): |
|
|||
557 | if banner1 is not None: |
|
|||
558 | self.banner1 = banner1 |
|
|||
559 | if banner2 is not None: |
|
|||
560 | self.banner2 = banner2 |
|
|||
561 | if display_banner is not None: |
|
|||
562 | self.display_banner = display_banner |
|
|||
563 | self.compute_banner() |
|
|||
564 |
|
||||
565 | def show_banner(self, banner=None): |
|
|||
566 | if banner is None: |
|
|||
567 | banner = self.banner |
|
|||
568 | self.write(banner) |
|
|||
569 |
|
||||
570 | def compute_banner(self): |
|
|||
571 | self.banner = self.banner1 + '\n' |
|
|||
572 | if self.profile: |
|
|||
573 | self.banner += '\nIPython profile: %s\n' % self.profile |
|
|||
574 | if self.banner2: |
|
|||
575 | self.banner += '\n' + self.banner2 + '\n' |
|
|||
576 |
|
||||
577 | #------------------------------------------------------------------------- |
|
|||
578 | # Things related to injections into the sys module |
|
425 | # Things related to injections into the sys module | |
579 | #------------------------------------------------------------------------- |
|
426 | #------------------------------------------------------------------------- | |
580 |
|
427 | |||
581 | def save_sys_module_state(self): |
|
428 | def save_sys_module_state(self): | |
582 | """Save the state of hooks in the sys module. |
|
429 | """Save the state of hooks in the sys module. | |
583 |
|
430 | |||
584 | This has to be called after self.user_ns is created. |
|
431 | This has to be called after self.user_ns is created. | |
585 | """ |
|
432 | """ | |
586 | self._orig_sys_module_state = {} |
|
433 | self._orig_sys_module_state = {} | |
587 | self._orig_sys_module_state['stdin'] = sys.stdin |
|
434 | self._orig_sys_module_state['stdin'] = sys.stdin | |
588 | self._orig_sys_module_state['stdout'] = sys.stdout |
|
435 | self._orig_sys_module_state['stdout'] = sys.stdout | |
589 | self._orig_sys_module_state['stderr'] = sys.stderr |
|
436 | self._orig_sys_module_state['stderr'] = sys.stderr | |
590 | self._orig_sys_module_state['excepthook'] = sys.excepthook |
|
437 | self._orig_sys_module_state['excepthook'] = sys.excepthook | |
591 | try: |
|
438 | try: | |
592 | self._orig_sys_modules_main_name = self.user_ns['__name__'] |
|
439 | self._orig_sys_modules_main_name = self.user_ns['__name__'] | |
593 | except KeyError: |
|
440 | except KeyError: | |
594 | pass |
|
441 | pass | |
595 |
|
442 | |||
596 | def restore_sys_module_state(self): |
|
443 | def restore_sys_module_state(self): | |
597 | """Restore the state of the sys module.""" |
|
444 | """Restore the state of the sys module.""" | |
598 | try: |
|
445 | try: | |
599 | for k, v in self._orig_sys_module_state.items(): |
|
446 | for k, v in self._orig_sys_module_state.items(): | |
600 | setattr(sys, k, v) |
|
447 | setattr(sys, k, v) | |
601 | except AttributeError: |
|
448 | except AttributeError: | |
602 | pass |
|
449 | pass | |
603 | try: |
|
450 | try: | |
604 | delattr(sys, 'ipcompleter') |
|
451 | delattr(sys, 'ipcompleter') | |
605 | except AttributeError: |
|
452 | except AttributeError: | |
606 | pass |
|
453 | pass | |
607 | # Reset what what done in self.init_sys_modules |
|
454 | # Reset what what done in self.init_sys_modules | |
608 | try: |
|
455 | try: | |
609 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name |
|
456 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name | |
610 | except (AttributeError, KeyError): |
|
457 | except (AttributeError, KeyError): | |
611 | pass |
|
458 | pass | |
612 |
|
459 | |||
613 | #------------------------------------------------------------------------- |
|
460 | #------------------------------------------------------------------------- | |
614 | # Things related to hooks |
|
461 | # Things related to hooks | |
615 | #------------------------------------------------------------------------- |
|
462 | #------------------------------------------------------------------------- | |
616 |
|
463 | |||
617 | def init_hooks(self): |
|
464 | def init_hooks(self): | |
618 | # hooks holds pointers used for user-side customizations |
|
465 | # hooks holds pointers used for user-side customizations | |
619 | self.hooks = Struct() |
|
466 | self.hooks = Struct() | |
620 |
|
467 | |||
621 | self.strdispatchers = {} |
|
468 | self.strdispatchers = {} | |
622 |
|
469 | |||
623 | # Set all default hooks, defined in the IPython.hooks module. |
|
470 | # Set all default hooks, defined in the IPython.hooks module. | |
624 | hooks = IPython.core.hooks |
|
471 | hooks = IPython.core.hooks | |
625 | for hook_name in hooks.__all__: |
|
472 | for hook_name in hooks.__all__: | |
626 | # default hooks have priority 100, i.e. low; user hooks should have |
|
473 | # default hooks have priority 100, i.e. low; user hooks should have | |
627 | # 0-100 priority |
|
474 | # 0-100 priority | |
628 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
475 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) | |
629 |
|
476 | |||
630 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
477 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): | |
631 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
478 | """set_hook(name,hook) -> sets an internal IPython hook. | |
632 |
|
479 | |||
633 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
480 | IPython exposes some of its internal API as user-modifiable hooks. By | |
634 | adding your function to one of these hooks, you can modify IPython's |
|
481 | adding your function to one of these hooks, you can modify IPython's | |
635 | behavior to call at runtime your own routines.""" |
|
482 | behavior to call at runtime your own routines.""" | |
636 |
|
483 | |||
637 | # At some point in the future, this should validate the hook before it |
|
484 | # At some point in the future, this should validate the hook before it | |
638 | # accepts it. Probably at least check that the hook takes the number |
|
485 | # accepts it. Probably at least check that the hook takes the number | |
639 | # of args it's supposed to. |
|
486 | # of args it's supposed to. | |
640 |
|
487 | |||
641 | f = new.instancemethod(hook,self,self.__class__) |
|
488 | f = new.instancemethod(hook,self,self.__class__) | |
642 |
|
489 | |||
643 | # check if the hook is for strdispatcher first |
|
490 | # check if the hook is for strdispatcher first | |
644 | if str_key is not None: |
|
491 | if str_key is not None: | |
645 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
492 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
646 | sdp.add_s(str_key, f, priority ) |
|
493 | sdp.add_s(str_key, f, priority ) | |
647 | self.strdispatchers[name] = sdp |
|
494 | self.strdispatchers[name] = sdp | |
648 | return |
|
495 | return | |
649 | if re_key is not None: |
|
496 | if re_key is not None: | |
650 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
497 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
651 | sdp.add_re(re.compile(re_key), f, priority ) |
|
498 | sdp.add_re(re.compile(re_key), f, priority ) | |
652 | self.strdispatchers[name] = sdp |
|
499 | self.strdispatchers[name] = sdp | |
653 | return |
|
500 | return | |
654 |
|
501 | |||
655 | dp = getattr(self.hooks, name, None) |
|
502 | dp = getattr(self.hooks, name, None) | |
656 | if name not in IPython.core.hooks.__all__: |
|
503 | if name not in IPython.core.hooks.__all__: | |
657 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ ) |
|
504 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ ) | |
658 | if not dp: |
|
505 | if not dp: | |
659 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
506 | dp = IPython.core.hooks.CommandChainDispatcher() | |
660 |
|
507 | |||
661 | try: |
|
508 | try: | |
662 | dp.add(f,priority) |
|
509 | dp.add(f,priority) | |
663 | except AttributeError: |
|
510 | except AttributeError: | |
664 | # it was not commandchain, plain old func - replace |
|
511 | # it was not commandchain, plain old func - replace | |
665 | dp = f |
|
512 | dp = f | |
666 |
|
513 | |||
667 | setattr(self.hooks,name, dp) |
|
514 | setattr(self.hooks,name, dp) | |
668 |
|
515 | |||
669 | #------------------------------------------------------------------------- |
|
516 | #------------------------------------------------------------------------- | |
670 | # Things related to the "main" module |
|
517 | # Things related to the "main" module | |
671 | #------------------------------------------------------------------------- |
|
518 | #------------------------------------------------------------------------- | |
672 |
|
519 | |||
673 | def new_main_mod(self,ns=None): |
|
520 | def new_main_mod(self,ns=None): | |
674 | """Return a new 'main' module object for user code execution. |
|
521 | """Return a new 'main' module object for user code execution. | |
675 | """ |
|
522 | """ | |
676 | main_mod = self._user_main_module |
|
523 | main_mod = self._user_main_module | |
677 | init_fakemod_dict(main_mod,ns) |
|
524 | init_fakemod_dict(main_mod,ns) | |
678 | return main_mod |
|
525 | return main_mod | |
679 |
|
526 | |||
680 | def cache_main_mod(self,ns,fname): |
|
527 | def cache_main_mod(self,ns,fname): | |
681 | """Cache a main module's namespace. |
|
528 | """Cache a main module's namespace. | |
682 |
|
529 | |||
683 | When scripts are executed via %run, we must keep a reference to the |
|
530 | When scripts are executed via %run, we must keep a reference to the | |
684 | namespace of their __main__ module (a FakeModule instance) around so |
|
531 | namespace of their __main__ module (a FakeModule instance) around so | |
685 | that Python doesn't clear it, rendering objects defined therein |
|
532 | that Python doesn't clear it, rendering objects defined therein | |
686 | useless. |
|
533 | useless. | |
687 |
|
534 | |||
688 | This method keeps said reference in a private dict, keyed by the |
|
535 | This method keeps said reference in a private dict, keyed by the | |
689 | absolute path of the module object (which corresponds to the script |
|
536 | absolute path of the module object (which corresponds to the script | |
690 | path). This way, for multiple executions of the same script we only |
|
537 | path). This way, for multiple executions of the same script we only | |
691 | keep one copy of the namespace (the last one), thus preventing memory |
|
538 | keep one copy of the namespace (the last one), thus preventing memory | |
692 | leaks from old references while allowing the objects from the last |
|
539 | leaks from old references while allowing the objects from the last | |
693 | execution to be accessible. |
|
540 | execution to be accessible. | |
694 |
|
541 | |||
695 | Note: we can not allow the actual FakeModule instances to be deleted, |
|
542 | Note: we can not allow the actual FakeModule instances to be deleted, | |
696 | because of how Python tears down modules (it hard-sets all their |
|
543 | because of how Python tears down modules (it hard-sets all their | |
697 | references to None without regard for reference counts). This method |
|
544 | references to None without regard for reference counts). This method | |
698 | must therefore make a *copy* of the given namespace, to allow the |
|
545 | must therefore make a *copy* of the given namespace, to allow the | |
699 | original module's __dict__ to be cleared and reused. |
|
546 | original module's __dict__ to be cleared and reused. | |
700 |
|
547 | |||
701 |
|
548 | |||
702 | Parameters |
|
549 | Parameters | |
703 | ---------- |
|
550 | ---------- | |
704 | ns : a namespace (a dict, typically) |
|
551 | ns : a namespace (a dict, typically) | |
705 |
|
552 | |||
706 | fname : str |
|
553 | fname : str | |
707 | Filename associated with the namespace. |
|
554 | Filename associated with the namespace. | |
708 |
|
555 | |||
709 | Examples |
|
556 | Examples | |
710 | -------- |
|
557 | -------- | |
711 |
|
558 | |||
712 | In [10]: import IPython |
|
559 | In [10]: import IPython | |
713 |
|
560 | |||
714 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
561 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
715 |
|
562 | |||
716 | In [12]: IPython.__file__ in _ip._main_ns_cache |
|
563 | In [12]: IPython.__file__ in _ip._main_ns_cache | |
717 | Out[12]: True |
|
564 | Out[12]: True | |
718 | """ |
|
565 | """ | |
719 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() |
|
566 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() | |
720 |
|
567 | |||
721 | def clear_main_mod_cache(self): |
|
568 | def clear_main_mod_cache(self): | |
722 | """Clear the cache of main modules. |
|
569 | """Clear the cache of main modules. | |
723 |
|
570 | |||
724 | Mainly for use by utilities like %reset. |
|
571 | Mainly for use by utilities like %reset. | |
725 |
|
572 | |||
726 | Examples |
|
573 | Examples | |
727 | -------- |
|
574 | -------- | |
728 |
|
575 | |||
729 | In [15]: import IPython |
|
576 | In [15]: import IPython | |
730 |
|
577 | |||
731 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
578 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
732 |
|
579 | |||
733 | In [17]: len(_ip._main_ns_cache) > 0 |
|
580 | In [17]: len(_ip._main_ns_cache) > 0 | |
734 | Out[17]: True |
|
581 | Out[17]: True | |
735 |
|
582 | |||
736 | In [18]: _ip.clear_main_mod_cache() |
|
583 | In [18]: _ip.clear_main_mod_cache() | |
737 |
|
584 | |||
738 | In [19]: len(_ip._main_ns_cache) == 0 |
|
585 | In [19]: len(_ip._main_ns_cache) == 0 | |
739 | Out[19]: True |
|
586 | Out[19]: True | |
740 | """ |
|
587 | """ | |
741 | self._main_ns_cache.clear() |
|
588 | self._main_ns_cache.clear() | |
742 |
|
589 | |||
743 | #------------------------------------------------------------------------- |
|
590 | #------------------------------------------------------------------------- | |
744 | # Things related to debugging |
|
591 | # Things related to debugging | |
745 | #------------------------------------------------------------------------- |
|
592 | #------------------------------------------------------------------------- | |
746 |
|
593 | |||
747 | def init_pdb(self): |
|
594 | def init_pdb(self): | |
748 | # Set calling of pdb on exceptions |
|
595 | # Set calling of pdb on exceptions | |
749 | # self.call_pdb is a property |
|
596 | # self.call_pdb is a property | |
750 | self.call_pdb = self.pdb |
|
597 | self.call_pdb = self.pdb | |
751 |
|
598 | |||
752 | def _get_call_pdb(self): |
|
599 | def _get_call_pdb(self): | |
753 | return self._call_pdb |
|
600 | return self._call_pdb | |
754 |
|
601 | |||
755 | def _set_call_pdb(self,val): |
|
602 | def _set_call_pdb(self,val): | |
756 |
|
603 | |||
757 | if val not in (0,1,False,True): |
|
604 | if val not in (0,1,False,True): | |
758 | raise ValueError,'new call_pdb value must be boolean' |
|
605 | raise ValueError,'new call_pdb value must be boolean' | |
759 |
|
606 | |||
760 | # store value in instance |
|
607 | # store value in instance | |
761 | self._call_pdb = val |
|
608 | self._call_pdb = val | |
762 |
|
609 | |||
763 | # notify the actual exception handlers |
|
610 | # notify the actual exception handlers | |
764 | self.InteractiveTB.call_pdb = val |
|
611 | self.InteractiveTB.call_pdb = val | |
765 | if self.isthreaded: |
|
|||
766 | try: |
|
|||
767 | self.sys_excepthook.call_pdb = val |
|
|||
768 | except: |
|
|||
769 | warn('Failed to activate pdb for threaded exception handler') |
|
|||
770 |
|
612 | |||
771 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
613 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
772 | 'Control auto-activation of pdb at exceptions') |
|
614 | 'Control auto-activation of pdb at exceptions') | |
773 |
|
615 | |||
774 | def debugger(self,force=False): |
|
616 | def debugger(self,force=False): | |
775 | """Call the pydb/pdb debugger. |
|
617 | """Call the pydb/pdb debugger. | |
776 |
|
618 | |||
777 | Keywords: |
|
619 | Keywords: | |
778 |
|
620 | |||
779 | - force(False): by default, this routine checks the instance call_pdb |
|
621 | - force(False): by default, this routine checks the instance call_pdb | |
780 | flag and does not actually invoke the debugger if the flag is false. |
|
622 | flag and does not actually invoke the debugger if the flag is false. | |
781 | The 'force' option forces the debugger to activate even if the flag |
|
623 | The 'force' option forces the debugger to activate even if the flag | |
782 | is false. |
|
624 | is false. | |
783 | """ |
|
625 | """ | |
784 |
|
626 | |||
785 | if not (force or self.call_pdb): |
|
627 | if not (force or self.call_pdb): | |
786 | return |
|
628 | return | |
787 |
|
629 | |||
788 | if not hasattr(sys,'last_traceback'): |
|
630 | if not hasattr(sys,'last_traceback'): | |
789 | error('No traceback has been produced, nothing to debug.') |
|
631 | error('No traceback has been produced, nothing to debug.') | |
790 | return |
|
632 | return | |
791 |
|
633 | |||
792 | # use pydb if available |
|
634 | # use pydb if available | |
793 | if debugger.has_pydb: |
|
635 | if debugger.has_pydb: | |
794 | from pydb import pm |
|
636 | from pydb import pm | |
795 | else: |
|
637 | else: | |
796 | # fallback to our internal debugger |
|
638 | # fallback to our internal debugger | |
797 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
639 | pm = lambda : self.InteractiveTB.debugger(force=True) | |
798 | self.history_saving_wrapper(pm)() |
|
640 | self.history_saving_wrapper(pm)() | |
799 |
|
641 | |||
800 | #------------------------------------------------------------------------- |
|
642 | #------------------------------------------------------------------------- | |
801 | # Things related to IPython's various namespaces |
|
643 | # Things related to IPython's various namespaces | |
802 | #------------------------------------------------------------------------- |
|
644 | #------------------------------------------------------------------------- | |
803 |
|
645 | |||
804 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): |
|
646 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): | |
805 | # Create the namespace where the user will operate. user_ns is |
|
647 | # Create the namespace where the user will operate. user_ns is | |
806 | # normally the only one used, and it is passed to the exec calls as |
|
648 | # normally the only one used, and it is passed to the exec calls as | |
807 | # the locals argument. But we do carry a user_global_ns namespace |
|
649 | # the locals argument. But we do carry a user_global_ns namespace | |
808 | # given as the exec 'globals' argument, This is useful in embedding |
|
650 | # given as the exec 'globals' argument, This is useful in embedding | |
809 | # situations where the ipython shell opens in a context where the |
|
651 | # situations where the ipython shell opens in a context where the | |
810 | # distinction between locals and globals is meaningful. For |
|
652 | # distinction between locals and globals is meaningful. For | |
811 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
653 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
812 |
|
654 | |||
813 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
655 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
814 | # level as a dict instead of a module. This is a manual fix, but I |
|
656 | # level as a dict instead of a module. This is a manual fix, but I | |
815 | # should really track down where the problem is coming from. Alex |
|
657 | # should really track down where the problem is coming from. Alex | |
816 | # Schmolck reported this problem first. |
|
658 | # Schmolck reported this problem first. | |
817 |
|
659 | |||
818 | # A useful post by Alex Martelli on this topic: |
|
660 | # A useful post by Alex Martelli on this topic: | |
819 | # Re: inconsistent value from __builtins__ |
|
661 | # Re: inconsistent value from __builtins__ | |
820 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
662 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
821 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
663 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
822 | # Gruppen: comp.lang.python |
|
664 | # Gruppen: comp.lang.python | |
823 |
|
665 | |||
824 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
666 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
825 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
667 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
826 | # > <type 'dict'> |
|
668 | # > <type 'dict'> | |
827 | # > >>> print type(__builtins__) |
|
669 | # > >>> print type(__builtins__) | |
828 | # > <type 'module'> |
|
670 | # > <type 'module'> | |
829 | # > Is this difference in return value intentional? |
|
671 | # > Is this difference in return value intentional? | |
830 |
|
672 | |||
831 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
673 | # Well, it's documented that '__builtins__' can be either a dictionary | |
832 | # or a module, and it's been that way for a long time. Whether it's |
|
674 | # or a module, and it's been that way for a long time. Whether it's | |
833 | # intentional (or sensible), I don't know. In any case, the idea is |
|
675 | # intentional (or sensible), I don't know. In any case, the idea is | |
834 | # that if you need to access the built-in namespace directly, you |
|
676 | # that if you need to access the built-in namespace directly, you | |
835 | # should start with "import __builtin__" (note, no 's') which will |
|
677 | # should start with "import __builtin__" (note, no 's') which will | |
836 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
678 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
837 |
|
679 | |||
838 | # These routines return properly built dicts as needed by the rest of |
|
680 | # These routines return properly built dicts as needed by the rest of | |
839 | # the code, and can also be used by extension writers to generate |
|
681 | # the code, and can also be used by extension writers to generate | |
840 | # properly initialized namespaces. |
|
682 | # properly initialized namespaces. | |
841 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns) |
|
683 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns) | |
842 |
|
684 | |||
843 | # Assign namespaces |
|
685 | # Assign namespaces | |
844 | # This is the namespace where all normal user variables live |
|
686 | # This is the namespace where all normal user variables live | |
845 | self.user_ns = user_ns |
|
687 | self.user_ns = user_ns | |
846 | self.user_global_ns = user_global_ns |
|
688 | self.user_global_ns = user_global_ns | |
847 |
|
689 | |||
848 | # An auxiliary namespace that checks what parts of the user_ns were |
|
690 | # An auxiliary namespace that checks what parts of the user_ns were | |
849 | # loaded at startup, so we can list later only variables defined in |
|
691 | # loaded at startup, so we can list later only variables defined in | |
850 | # actual interactive use. Since it is always a subset of user_ns, it |
|
692 | # actual interactive use. Since it is always a subset of user_ns, it | |
851 | # doesn't need to be separately tracked in the ns_table. |
|
693 | # doesn't need to be separately tracked in the ns_table. | |
852 | self.user_ns_hidden = {} |
|
694 | self.user_ns_hidden = {} | |
853 |
|
695 | |||
854 | # A namespace to keep track of internal data structures to prevent |
|
696 | # A namespace to keep track of internal data structures to prevent | |
855 | # them from cluttering user-visible stuff. Will be updated later |
|
697 | # them from cluttering user-visible stuff. Will be updated later | |
856 | self.internal_ns = {} |
|
698 | self.internal_ns = {} | |
857 |
|
699 | |||
858 | # Now that FakeModule produces a real module, we've run into a nasty |
|
700 | # Now that FakeModule produces a real module, we've run into a nasty | |
859 | # problem: after script execution (via %run), the module where the user |
|
701 | # problem: after script execution (via %run), the module where the user | |
860 | # code ran is deleted. Now that this object is a true module (needed |
|
702 | # code ran is deleted. Now that this object is a true module (needed | |
861 | # so docetst and other tools work correctly), the Python module |
|
703 | # so docetst and other tools work correctly), the Python module | |
862 | # teardown mechanism runs over it, and sets to None every variable |
|
704 | # teardown mechanism runs over it, and sets to None every variable | |
863 | # present in that module. Top-level references to objects from the |
|
705 | # present in that module. Top-level references to objects from the | |
864 | # script survive, because the user_ns is updated with them. However, |
|
706 | # script survive, because the user_ns is updated with them. However, | |
865 | # calling functions defined in the script that use other things from |
|
707 | # calling functions defined in the script that use other things from | |
866 | # the script will fail, because the function's closure had references |
|
708 | # the script will fail, because the function's closure had references | |
867 | # to the original objects, which are now all None. So we must protect |
|
709 | # to the original objects, which are now all None. So we must protect | |
868 | # these modules from deletion by keeping a cache. |
|
710 | # these modules from deletion by keeping a cache. | |
869 | # |
|
711 | # | |
870 | # To avoid keeping stale modules around (we only need the one from the |
|
712 | # To avoid keeping stale modules around (we only need the one from the | |
871 | # last run), we use a dict keyed with the full path to the script, so |
|
713 | # last run), we use a dict keyed with the full path to the script, so | |
872 | # only the last version of the module is held in the cache. Note, |
|
714 | # only the last version of the module is held in the cache. Note, | |
873 | # however, that we must cache the module *namespace contents* (their |
|
715 | # however, that we must cache the module *namespace contents* (their | |
874 | # __dict__). Because if we try to cache the actual modules, old ones |
|
716 | # __dict__). Because if we try to cache the actual modules, old ones | |
875 | # (uncached) could be destroyed while still holding references (such as |
|
717 | # (uncached) could be destroyed while still holding references (such as | |
876 | # those held by GUI objects that tend to be long-lived)> |
|
718 | # those held by GUI objects that tend to be long-lived)> | |
877 | # |
|
719 | # | |
878 | # The %reset command will flush this cache. See the cache_main_mod() |
|
720 | # The %reset command will flush this cache. See the cache_main_mod() | |
879 | # and clear_main_mod_cache() methods for details on use. |
|
721 | # and clear_main_mod_cache() methods for details on use. | |
880 |
|
722 | |||
881 | # This is the cache used for 'main' namespaces |
|
723 | # This is the cache used for 'main' namespaces | |
882 | self._main_ns_cache = {} |
|
724 | self._main_ns_cache = {} | |
883 | # And this is the single instance of FakeModule whose __dict__ we keep |
|
725 | # And this is the single instance of FakeModule whose __dict__ we keep | |
884 | # copying and clearing for reuse on each %run |
|
726 | # copying and clearing for reuse on each %run | |
885 | self._user_main_module = FakeModule() |
|
727 | self._user_main_module = FakeModule() | |
886 |
|
728 | |||
887 | # A table holding all the namespaces IPython deals with, so that |
|
729 | # A table holding all the namespaces IPython deals with, so that | |
888 | # introspection facilities can search easily. |
|
730 | # introspection facilities can search easily. | |
889 | self.ns_table = {'user':user_ns, |
|
731 | self.ns_table = {'user':user_ns, | |
890 | 'user_global':user_global_ns, |
|
732 | 'user_global':user_global_ns, | |
891 | 'internal':self.internal_ns, |
|
733 | 'internal':self.internal_ns, | |
892 | 'builtin':__builtin__.__dict__ |
|
734 | 'builtin':__builtin__.__dict__ | |
893 | } |
|
735 | } | |
894 |
|
736 | |||
895 | # Similarly, track all namespaces where references can be held and that |
|
737 | # Similarly, track all namespaces where references can be held and that | |
896 | # we can safely clear (so it can NOT include builtin). This one can be |
|
738 | # we can safely clear (so it can NOT include builtin). This one can be | |
897 | # a simple list. |
|
739 | # a simple list. | |
898 | self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden, |
|
740 | self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden, | |
899 | self.internal_ns, self._main_ns_cache ] |
|
741 | self.internal_ns, self._main_ns_cache ] | |
900 |
|
742 | |||
901 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): |
|
743 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): | |
902 | """Return a valid local and global user interactive namespaces. |
|
744 | """Return a valid local and global user interactive namespaces. | |
903 |
|
745 | |||
904 | This builds a dict with the minimal information needed to operate as a |
|
746 | This builds a dict with the minimal information needed to operate as a | |
905 | valid IPython user namespace, which you can pass to the various |
|
747 | valid IPython user namespace, which you can pass to the various | |
906 | embedding classes in ipython. The default implementation returns the |
|
748 | embedding classes in ipython. The default implementation returns the | |
907 | same dict for both the locals and the globals to allow functions to |
|
749 | same dict for both the locals and the globals to allow functions to | |
908 | refer to variables in the namespace. Customized implementations can |
|
750 | refer to variables in the namespace. Customized implementations can | |
909 | return different dicts. The locals dictionary can actually be anything |
|
751 | return different dicts. The locals dictionary can actually be anything | |
910 | following the basic mapping protocol of a dict, but the globals dict |
|
752 | following the basic mapping protocol of a dict, but the globals dict | |
911 | must be a true dict, not even a subclass. It is recommended that any |
|
753 | must be a true dict, not even a subclass. It is recommended that any | |
912 | custom object for the locals namespace synchronize with the globals |
|
754 | custom object for the locals namespace synchronize with the globals | |
913 | dict somehow. |
|
755 | dict somehow. | |
914 |
|
756 | |||
915 | Raises TypeError if the provided globals namespace is not a true dict. |
|
757 | Raises TypeError if the provided globals namespace is not a true dict. | |
916 |
|
758 | |||
917 | Parameters |
|
759 | Parameters | |
918 | ---------- |
|
760 | ---------- | |
919 | user_ns : dict-like, optional |
|
761 | user_ns : dict-like, optional | |
920 | The current user namespace. The items in this namespace should |
|
762 | The current user namespace. The items in this namespace should | |
921 | be included in the output. If None, an appropriate blank |
|
763 | be included in the output. If None, an appropriate blank | |
922 | namespace should be created. |
|
764 | namespace should be created. | |
923 | user_global_ns : dict, optional |
|
765 | user_global_ns : dict, optional | |
924 | The current user global namespace. The items in this namespace |
|
766 | The current user global namespace. The items in this namespace | |
925 | should be included in the output. If None, an appropriate |
|
767 | should be included in the output. If None, an appropriate | |
926 | blank namespace should be created. |
|
768 | blank namespace should be created. | |
927 |
|
769 | |||
928 | Returns |
|
770 | Returns | |
929 | ------- |
|
771 | ------- | |
930 | A pair of dictionary-like object to be used as the local namespace |
|
772 | A pair of dictionary-like object to be used as the local namespace | |
931 | of the interpreter and a dict to be used as the global namespace. |
|
773 | of the interpreter and a dict to be used as the global namespace. | |
932 | """ |
|
774 | """ | |
933 |
|
775 | |||
934 |
|
776 | |||
935 | # We must ensure that __builtin__ (without the final 's') is always |
|
777 | # We must ensure that __builtin__ (without the final 's') is always | |
936 | # available and pointing to the __builtin__ *module*. For more details: |
|
778 | # available and pointing to the __builtin__ *module*. For more details: | |
937 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
779 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
938 |
|
780 | |||
939 | if user_ns is None: |
|
781 | if user_ns is None: | |
940 | # Set __name__ to __main__ to better match the behavior of the |
|
782 | # Set __name__ to __main__ to better match the behavior of the | |
941 | # normal interpreter. |
|
783 | # normal interpreter. | |
942 | user_ns = {'__name__' :'__main__', |
|
784 | user_ns = {'__name__' :'__main__', | |
943 | '__builtin__' : __builtin__, |
|
785 | '__builtin__' : __builtin__, | |
944 | '__builtins__' : __builtin__, |
|
786 | '__builtins__' : __builtin__, | |
945 | } |
|
787 | } | |
946 | else: |
|
788 | else: | |
947 | user_ns.setdefault('__name__','__main__') |
|
789 | user_ns.setdefault('__name__','__main__') | |
948 | user_ns.setdefault('__builtin__',__builtin__) |
|
790 | user_ns.setdefault('__builtin__',__builtin__) | |
949 | user_ns.setdefault('__builtins__',__builtin__) |
|
791 | user_ns.setdefault('__builtins__',__builtin__) | |
950 |
|
792 | |||
951 | if user_global_ns is None: |
|
793 | if user_global_ns is None: | |
952 | user_global_ns = user_ns |
|
794 | user_global_ns = user_ns | |
953 | if type(user_global_ns) is not dict: |
|
795 | if type(user_global_ns) is not dict: | |
954 | raise TypeError("user_global_ns must be a true dict; got %r" |
|
796 | raise TypeError("user_global_ns must be a true dict; got %r" | |
955 | % type(user_global_ns)) |
|
797 | % type(user_global_ns)) | |
956 |
|
798 | |||
957 | return user_ns, user_global_ns |
|
799 | return user_ns, user_global_ns | |
958 |
|
800 | |||
959 | def init_sys_modules(self): |
|
801 | def init_sys_modules(self): | |
960 | # We need to insert into sys.modules something that looks like a |
|
802 | # We need to insert into sys.modules something that looks like a | |
961 | # module but which accesses the IPython namespace, for shelve and |
|
803 | # module but which accesses the IPython namespace, for shelve and | |
962 | # pickle to work interactively. Normally they rely on getting |
|
804 | # pickle to work interactively. Normally they rely on getting | |
963 | # everything out of __main__, but for embedding purposes each IPython |
|
805 | # everything out of __main__, but for embedding purposes each IPython | |
964 | # instance has its own private namespace, so we can't go shoving |
|
806 | # instance has its own private namespace, so we can't go shoving | |
965 | # everything into __main__. |
|
807 | # everything into __main__. | |
966 |
|
808 | |||
967 | # note, however, that we should only do this for non-embedded |
|
809 | # note, however, that we should only do this for non-embedded | |
968 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
810 | # ipythons, which really mimic the __main__.__dict__ with their own | |
969 | # namespace. Embedded instances, on the other hand, should not do |
|
811 | # namespace. Embedded instances, on the other hand, should not do | |
970 | # this because they need to manage the user local/global namespaces |
|
812 | # this because they need to manage the user local/global namespaces | |
971 | # only, but they live within a 'normal' __main__ (meaning, they |
|
813 | # only, but they live within a 'normal' __main__ (meaning, they | |
972 | # shouldn't overtake the execution environment of the script they're |
|
814 | # shouldn't overtake the execution environment of the script they're | |
973 | # embedded in). |
|
815 | # embedded in). | |
974 |
|
816 | |||
975 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
817 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
976 |
|
818 | |||
977 | try: |
|
819 | try: | |
978 | main_name = self.user_ns['__name__'] |
|
820 | main_name = self.user_ns['__name__'] | |
979 | except KeyError: |
|
821 | except KeyError: | |
980 | raise KeyError('user_ns dictionary MUST have a "__name__" key') |
|
822 | raise KeyError('user_ns dictionary MUST have a "__name__" key') | |
981 | else: |
|
823 | else: | |
982 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
824 | sys.modules[main_name] = FakeModule(self.user_ns) | |
983 |
|
825 | |||
984 | def init_user_ns(self): |
|
826 | def init_user_ns(self): | |
985 | """Initialize all user-visible namespaces to their minimum defaults. |
|
827 | """Initialize all user-visible namespaces to their minimum defaults. | |
986 |
|
828 | |||
987 | Certain history lists are also initialized here, as they effectively |
|
829 | Certain history lists are also initialized here, as they effectively | |
988 | act as user namespaces. |
|
830 | act as user namespaces. | |
989 |
|
831 | |||
990 | Notes |
|
832 | Notes | |
991 | ----- |
|
833 | ----- | |
992 | All data structures here are only filled in, they are NOT reset by this |
|
834 | All data structures here are only filled in, they are NOT reset by this | |
993 | method. If they were not empty before, data will simply be added to |
|
835 | method. If they were not empty before, data will simply be added to | |
994 | therm. |
|
836 | therm. | |
995 | """ |
|
837 | """ | |
996 | # This function works in two parts: first we put a few things in |
|
838 | # This function works in two parts: first we put a few things in | |
997 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
839 | # user_ns, and we sync that contents into user_ns_hidden so that these | |
998 | # initial variables aren't shown by %who. After the sync, we add the |
|
840 | # initial variables aren't shown by %who. After the sync, we add the | |
999 | # rest of what we *do* want the user to see with %who even on a new |
|
841 | # rest of what we *do* want the user to see with %who even on a new | |
1000 | # session (probably nothing, so theye really only see their own stuff) |
|
842 | # session (probably nothing, so theye really only see their own stuff) | |
1001 |
|
843 | |||
1002 | # The user dict must *always* have a __builtin__ reference to the |
|
844 | # The user dict must *always* have a __builtin__ reference to the | |
1003 | # Python standard __builtin__ namespace, which must be imported. |
|
845 | # Python standard __builtin__ namespace, which must be imported. | |
1004 | # This is so that certain operations in prompt evaluation can be |
|
846 | # This is so that certain operations in prompt evaluation can be | |
1005 | # reliably executed with builtins. Note that we can NOT use |
|
847 | # reliably executed with builtins. Note that we can NOT use | |
1006 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
848 | # __builtins__ (note the 's'), because that can either be a dict or a | |
1007 | # module, and can even mutate at runtime, depending on the context |
|
849 | # module, and can even mutate at runtime, depending on the context | |
1008 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
850 | # (Python makes no guarantees on it). In contrast, __builtin__ is | |
1009 | # always a module object, though it must be explicitly imported. |
|
851 | # always a module object, though it must be explicitly imported. | |
1010 |
|
852 | |||
1011 | # For more details: |
|
853 | # For more details: | |
1012 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
854 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1013 | ns = dict(__builtin__ = __builtin__) |
|
855 | ns = dict(__builtin__ = __builtin__) | |
1014 |
|
856 | |||
1015 | # Put 'help' in the user namespace |
|
857 | # Put 'help' in the user namespace | |
1016 | try: |
|
858 | try: | |
1017 | from site import _Helper |
|
859 | from site import _Helper | |
1018 | ns['help'] = _Helper() |
|
860 | ns['help'] = _Helper() | |
1019 | except ImportError: |
|
861 | except ImportError: | |
1020 | warn('help() not available - check site.py') |
|
862 | warn('help() not available - check site.py') | |
1021 |
|
863 | |||
1022 | # make global variables for user access to the histories |
|
864 | # make global variables for user access to the histories | |
1023 | ns['_ih'] = self.input_hist |
|
865 | ns['_ih'] = self.input_hist | |
1024 | ns['_oh'] = self.output_hist |
|
866 | ns['_oh'] = self.output_hist | |
1025 | ns['_dh'] = self.dir_hist |
|
867 | ns['_dh'] = self.dir_hist | |
1026 |
|
868 | |||
1027 | ns['_sh'] = shadowns |
|
869 | ns['_sh'] = shadowns | |
1028 |
|
870 | |||
1029 | # user aliases to input and output histories. These shouldn't show up |
|
871 | # user aliases to input and output histories. These shouldn't show up | |
1030 | # in %who, as they can have very large reprs. |
|
872 | # in %who, as they can have very large reprs. | |
1031 | ns['In'] = self.input_hist |
|
873 | ns['In'] = self.input_hist | |
1032 | ns['Out'] = self.output_hist |
|
874 | ns['Out'] = self.output_hist | |
1033 |
|
875 | |||
1034 | # Store myself as the public api!!! |
|
876 | # Store myself as the public api!!! | |
1035 | ns['get_ipython'] = self.get_ipython |
|
877 | ns['get_ipython'] = self.get_ipython | |
1036 |
|
878 | |||
1037 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
879 | # Sync what we've added so far to user_ns_hidden so these aren't seen | |
1038 | # by %who |
|
880 | # by %who | |
1039 | self.user_ns_hidden.update(ns) |
|
881 | self.user_ns_hidden.update(ns) | |
1040 |
|
882 | |||
1041 | # Anything put into ns now would show up in %who. Think twice before |
|
883 | # Anything put into ns now would show up in %who. Think twice before | |
1042 | # putting anything here, as we really want %who to show the user their |
|
884 | # putting anything here, as we really want %who to show the user their | |
1043 | # stuff, not our variables. |
|
885 | # stuff, not our variables. | |
1044 |
|
886 | |||
1045 | # Finally, update the real user's namespace |
|
887 | # Finally, update the real user's namespace | |
1046 | self.user_ns.update(ns) |
|
888 | self.user_ns.update(ns) | |
1047 |
|
889 | |||
1048 |
|
890 | |||
1049 | def reset(self): |
|
891 | def reset(self): | |
1050 | """Clear all internal namespaces. |
|
892 | """Clear all internal namespaces. | |
1051 |
|
893 | |||
1052 | Note that this is much more aggressive than %reset, since it clears |
|
894 | Note that this is much more aggressive than %reset, since it clears | |
1053 | fully all namespaces, as well as all input/output lists. |
|
895 | fully all namespaces, as well as all input/output lists. | |
1054 | """ |
|
896 | """ | |
1055 | for ns in self.ns_refs_table: |
|
897 | for ns in self.ns_refs_table: | |
1056 | ns.clear() |
|
898 | ns.clear() | |
1057 |
|
899 | |||
1058 | self.alias_manager.clear_aliases() |
|
900 | self.alias_manager.clear_aliases() | |
1059 |
|
901 | |||
1060 | # Clear input and output histories |
|
902 | # Clear input and output histories | |
1061 | self.input_hist[:] = [] |
|
903 | self.input_hist[:] = [] | |
1062 | self.input_hist_raw[:] = [] |
|
904 | self.input_hist_raw[:] = [] | |
1063 | self.output_hist.clear() |
|
905 | self.output_hist.clear() | |
1064 |
|
906 | |||
1065 | # Restore the user namespaces to minimal usability |
|
907 | # Restore the user namespaces to minimal usability | |
1066 | self.init_user_ns() |
|
908 | self.init_user_ns() | |
1067 |
|
909 | |||
1068 | # Restore the default and user aliases |
|
910 | # Restore the default and user aliases | |
1069 | self.alias_manager.init_aliases() |
|
911 | self.alias_manager.init_aliases() | |
1070 |
|
912 | |||
1071 | def reset_selective(self, regex=None): |
|
913 | def reset_selective(self, regex=None): | |
1072 | """Clear selective variables from internal namespaces based on a specified regular expression. |
|
914 | """Clear selective variables from internal namespaces based on a specified regular expression. | |
1073 |
|
915 | |||
1074 | Parameters |
|
916 | Parameters | |
1075 | ---------- |
|
917 | ---------- | |
1076 | regex : string or compiled pattern, optional |
|
918 | regex : string or compiled pattern, optional | |
1077 | A regular expression pattern that will be used in searching variable names in the users |
|
919 | A regular expression pattern that will be used in searching variable names in the users | |
1078 | namespaces. |
|
920 | namespaces. | |
1079 | """ |
|
921 | """ | |
1080 | if regex is not None: |
|
922 | if regex is not None: | |
1081 | try: |
|
923 | try: | |
1082 | m = re.compile(regex) |
|
924 | m = re.compile(regex) | |
1083 | except TypeError: |
|
925 | except TypeError: | |
1084 | raise TypeError('regex must be a string or compiled pattern') |
|
926 | raise TypeError('regex must be a string or compiled pattern') | |
1085 | # Search for keys in each namespace that match the given regex |
|
927 | # Search for keys in each namespace that match the given regex | |
1086 | # If a match is found, delete the key/value pair. |
|
928 | # If a match is found, delete the key/value pair. | |
1087 | for ns in self.ns_refs_table: |
|
929 | for ns in self.ns_refs_table: | |
1088 | for var in ns: |
|
930 | for var in ns: | |
1089 | if m.search(var): |
|
931 | if m.search(var): | |
1090 | del ns[var] |
|
932 | del ns[var] | |
1091 |
|
933 | |||
1092 | def push(self, variables, interactive=True): |
|
934 | def push(self, variables, interactive=True): | |
1093 | """Inject a group of variables into the IPython user namespace. |
|
935 | """Inject a group of variables into the IPython user namespace. | |
1094 |
|
936 | |||
1095 | Parameters |
|
937 | Parameters | |
1096 | ---------- |
|
938 | ---------- | |
1097 | variables : dict, str or list/tuple of str |
|
939 | variables : dict, str or list/tuple of str | |
1098 | The variables to inject into the user's namespace. If a dict, |
|
940 | The variables to inject into the user's namespace. If a dict, | |
1099 | a simple update is done. If a str, the string is assumed to |
|
941 | a simple update is done. If a str, the string is assumed to | |
1100 | have variable names separated by spaces. A list/tuple of str |
|
942 | have variable names separated by spaces. A list/tuple of str | |
1101 | can also be used to give the variable names. If just the variable |
|
943 | can also be used to give the variable names. If just the variable | |
1102 | names are give (list/tuple/str) then the variable values looked |
|
944 | names are give (list/tuple/str) then the variable values looked | |
1103 | up in the callers frame. |
|
945 | up in the callers frame. | |
1104 | interactive : bool |
|
946 | interactive : bool | |
1105 | If True (default), the variables will be listed with the ``who`` |
|
947 | If True (default), the variables will be listed with the ``who`` | |
1106 | magic. |
|
948 | magic. | |
1107 | """ |
|
949 | """ | |
1108 | vdict = None |
|
950 | vdict = None | |
1109 |
|
951 | |||
1110 | # We need a dict of name/value pairs to do namespace updates. |
|
952 | # We need a dict of name/value pairs to do namespace updates. | |
1111 | if isinstance(variables, dict): |
|
953 | if isinstance(variables, dict): | |
1112 | vdict = variables |
|
954 | vdict = variables | |
1113 | elif isinstance(variables, (basestring, list, tuple)): |
|
955 | elif isinstance(variables, (basestring, list, tuple)): | |
1114 | if isinstance(variables, basestring): |
|
956 | if isinstance(variables, basestring): | |
1115 | vlist = variables.split() |
|
957 | vlist = variables.split() | |
1116 | else: |
|
958 | else: | |
1117 | vlist = variables |
|
959 | vlist = variables | |
1118 | vdict = {} |
|
960 | vdict = {} | |
1119 | cf = sys._getframe(1) |
|
961 | cf = sys._getframe(1) | |
1120 | for name in vlist: |
|
962 | for name in vlist: | |
1121 | try: |
|
963 | try: | |
1122 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
964 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
1123 | except: |
|
965 | except: | |
1124 | print ('Could not get variable %s from %s' % |
|
966 | print ('Could not get variable %s from %s' % | |
1125 | (name,cf.f_code.co_name)) |
|
967 | (name,cf.f_code.co_name)) | |
1126 | else: |
|
968 | else: | |
1127 | raise ValueError('variables must be a dict/str/list/tuple') |
|
969 | raise ValueError('variables must be a dict/str/list/tuple') | |
1128 |
|
970 | |||
1129 | # Propagate variables to user namespace |
|
971 | # Propagate variables to user namespace | |
1130 | self.user_ns.update(vdict) |
|
972 | self.user_ns.update(vdict) | |
1131 |
|
973 | |||
1132 | # And configure interactive visibility |
|
974 | # And configure interactive visibility | |
1133 | config_ns = self.user_ns_hidden |
|
975 | config_ns = self.user_ns_hidden | |
1134 | if interactive: |
|
976 | if interactive: | |
1135 | for name, val in vdict.iteritems(): |
|
977 | for name, val in vdict.iteritems(): | |
1136 | config_ns.pop(name, None) |
|
978 | config_ns.pop(name, None) | |
1137 | else: |
|
979 | else: | |
1138 | for name,val in vdict.iteritems(): |
|
980 | for name,val in vdict.iteritems(): | |
1139 | config_ns[name] = val |
|
981 | config_ns[name] = val | |
1140 |
|
982 | |||
1141 | #------------------------------------------------------------------------- |
|
983 | #------------------------------------------------------------------------- | |
1142 | # Things related to history management |
|
984 | # Things related to history management | |
1143 | #------------------------------------------------------------------------- |
|
985 | #------------------------------------------------------------------------- | |
1144 |
|
986 | |||
1145 | def init_history(self): |
|
987 | def init_history(self): | |
1146 | # List of input with multi-line handling. |
|
988 | # List of input with multi-line handling. | |
1147 | self.input_hist = InputList() |
|
989 | self.input_hist = InputList() | |
1148 | # This one will hold the 'raw' input history, without any |
|
990 | # This one will hold the 'raw' input history, without any | |
1149 | # pre-processing. This will allow users to retrieve the input just as |
|
991 | # pre-processing. This will allow users to retrieve the input just as | |
1150 | # it was exactly typed in by the user, with %hist -r. |
|
992 | # it was exactly typed in by the user, with %hist -r. | |
1151 | self.input_hist_raw = InputList() |
|
993 | self.input_hist_raw = InputList() | |
1152 |
|
994 | |||
1153 | # list of visited directories |
|
995 | # list of visited directories | |
1154 | try: |
|
996 | try: | |
1155 | self.dir_hist = [os.getcwd()] |
|
997 | self.dir_hist = [os.getcwd()] | |
1156 | except OSError: |
|
998 | except OSError: | |
1157 | self.dir_hist = [] |
|
999 | self.dir_hist = [] | |
1158 |
|
1000 | |||
1159 | # dict of output history |
|
1001 | # dict of output history | |
1160 | self.output_hist = {} |
|
1002 | self.output_hist = {} | |
1161 |
|
1003 | |||
1162 | # Now the history file |
|
1004 | # Now the history file | |
1163 | if self.profile: |
|
1005 | if self.profile: | |
1164 | histfname = 'history-%s' % self.profile |
|
1006 | histfname = 'history-%s' % self.profile | |
1165 | else: |
|
1007 | else: | |
1166 | histfname = 'history' |
|
1008 | histfname = 'history' | |
1167 | self.histfile = os.path.join(self.ipython_dir, histfname) |
|
1009 | self.histfile = os.path.join(self.ipython_dir, histfname) | |
1168 |
|
1010 | |||
1169 | # Fill the history zero entry, user counter starts at 1 |
|
1011 | # Fill the history zero entry, user counter starts at 1 | |
1170 | self.input_hist.append('\n') |
|
1012 | self.input_hist.append('\n') | |
1171 | self.input_hist_raw.append('\n') |
|
1013 | self.input_hist_raw.append('\n') | |
1172 |
|
1014 | |||
1173 | def init_shadow_hist(self): |
|
1015 | def init_shadow_hist(self): | |
1174 | try: |
|
1016 | try: | |
1175 | self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db") |
|
1017 | self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db") | |
1176 | except exceptions.UnicodeDecodeError: |
|
1018 | except exceptions.UnicodeDecodeError: | |
1177 | print "Your ipython_dir can't be decoded to unicode!" |
|
1019 | print "Your ipython_dir can't be decoded to unicode!" | |
1178 | print "Please set HOME environment variable to something that" |
|
1020 | print "Please set HOME environment variable to something that" | |
1179 | print r"only has ASCII characters, e.g. c:\home" |
|
1021 | print r"only has ASCII characters, e.g. c:\home" | |
1180 | print "Now it is", self.ipython_dir |
|
1022 | print "Now it is", self.ipython_dir | |
1181 | sys.exit() |
|
1023 | sys.exit() | |
1182 | self.shadowhist = ipcorehist.ShadowHist(self.db) |
|
1024 | self.shadowhist = ipcorehist.ShadowHist(self.db) | |
1183 |
|
1025 | |||
1184 | def savehist(self): |
|
1026 | def savehist(self): | |
1185 | """Save input history to a file (via readline library).""" |
|
1027 | """Save input history to a file (via readline library).""" | |
1186 |
|
1028 | |||
1187 | try: |
|
1029 | try: | |
1188 | self.readline.write_history_file(self.histfile) |
|
1030 | self.readline.write_history_file(self.histfile) | |
1189 | except: |
|
1031 | except: | |
1190 | print 'Unable to save IPython command history to file: ' + \ |
|
1032 | print 'Unable to save IPython command history to file: ' + \ | |
1191 | `self.histfile` |
|
1033 | `self.histfile` | |
1192 |
|
1034 | |||
1193 | def reloadhist(self): |
|
1035 | def reloadhist(self): | |
1194 | """Reload the input history from disk file.""" |
|
1036 | """Reload the input history from disk file.""" | |
1195 |
|
1037 | |||
1196 | try: |
|
1038 | try: | |
1197 | self.readline.clear_history() |
|
1039 | self.readline.clear_history() | |
1198 | self.readline.read_history_file(self.shell.histfile) |
|
1040 | self.readline.read_history_file(self.shell.histfile) | |
1199 | except AttributeError: |
|
1041 | except AttributeError: | |
1200 | pass |
|
1042 | pass | |
1201 |
|
1043 | |||
1202 | def history_saving_wrapper(self, func): |
|
1044 | def history_saving_wrapper(self, func): | |
1203 | """ Wrap func for readline history saving |
|
1045 | """ Wrap func for readline history saving | |
1204 |
|
1046 | |||
1205 | Convert func into callable that saves & restores |
|
1047 | Convert func into callable that saves & restores | |
1206 | history around the call """ |
|
1048 | history around the call """ | |
1207 |
|
1049 | |||
1208 | if self.has_readline: |
|
1050 | if self.has_readline: | |
1209 | from IPython.utils import rlineimpl as readline |
|
1051 | from IPython.utils import rlineimpl as readline | |
1210 | else: |
|
1052 | else: | |
1211 | return func |
|
1053 | return func | |
1212 |
|
1054 | |||
1213 | def wrapper(): |
|
1055 | def wrapper(): | |
1214 | self.savehist() |
|
1056 | self.savehist() | |
1215 | try: |
|
1057 | try: | |
1216 | func() |
|
1058 | func() | |
1217 | finally: |
|
1059 | finally: | |
1218 | readline.read_history_file(self.histfile) |
|
1060 | readline.read_history_file(self.histfile) | |
1219 | return wrapper |
|
1061 | return wrapper | |
1220 |
|
1062 | |||
1221 | #------------------------------------------------------------------------- |
|
1063 | #------------------------------------------------------------------------- | |
1222 | # Things related to exception handling and tracebacks (not debugging) |
|
1064 | # Things related to exception handling and tracebacks (not debugging) | |
1223 | #------------------------------------------------------------------------- |
|
1065 | #------------------------------------------------------------------------- | |
1224 |
|
1066 | |||
1225 | def init_traceback_handlers(self, custom_exceptions): |
|
1067 | def init_traceback_handlers(self, custom_exceptions): | |
1226 | # Syntax error handler. |
|
1068 | # Syntax error handler. | |
1227 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
1069 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') | |
1228 |
|
1070 | |||
1229 | # The interactive one is initialized with an offset, meaning we always |
|
1071 | # The interactive one is initialized with an offset, meaning we always | |
1230 | # want to remove the topmost item in the traceback, which is our own |
|
1072 | # want to remove the topmost item in the traceback, which is our own | |
1231 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1073 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
1232 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1074 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1233 | color_scheme='NoColor', |
|
1075 | color_scheme='NoColor', | |
1234 | tb_offset = 1) |
|
1076 | tb_offset = 1) | |
1235 |
|
1077 | |||
1236 | # The instance will store a pointer to the system-wide exception hook, |
|
1078 | # The instance will store a pointer to the system-wide exception hook, | |
1237 | # so that runtime code (such as magics) can access it. This is because |
|
1079 | # so that runtime code (such as magics) can access it. This is because | |
1238 | # during the read-eval loop, it may get temporarily overwritten. |
|
1080 | # during the read-eval loop, it may get temporarily overwritten. | |
1239 | self.sys_excepthook = sys.excepthook |
|
1081 | self.sys_excepthook = sys.excepthook | |
1240 |
|
1082 | |||
1241 | # and add any custom exception handlers the user may have specified |
|
1083 | # and add any custom exception handlers the user may have specified | |
1242 | self.set_custom_exc(*custom_exceptions) |
|
1084 | self.set_custom_exc(*custom_exceptions) | |
1243 |
|
1085 | |||
1244 | # Set the exception mode |
|
1086 | # Set the exception mode | |
1245 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1087 | self.InteractiveTB.set_mode(mode=self.xmode) | |
1246 |
|
1088 | |||
1247 | def set_custom_exc(self,exc_tuple,handler): |
|
1089 | def set_custom_exc(self,exc_tuple,handler): | |
1248 | """set_custom_exc(exc_tuple,handler) |
|
1090 | """set_custom_exc(exc_tuple,handler) | |
1249 |
|
1091 | |||
1250 | Set a custom exception handler, which will be called if any of the |
|
1092 | Set a custom exception handler, which will be called if any of the | |
1251 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1093 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1252 | runcode() method. |
|
1094 | runcode() method. | |
1253 |
|
1095 | |||
1254 | Inputs: |
|
1096 | Inputs: | |
1255 |
|
1097 | |||
1256 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
1098 | - exc_tuple: a *tuple* of valid exceptions to call the defined | |
1257 | handler for. It is very important that you use a tuple, and NOT A |
|
1099 | handler for. It is very important that you use a tuple, and NOT A | |
1258 | LIST here, because of the way Python's except statement works. If |
|
1100 | LIST here, because of the way Python's except statement works. If | |
1259 | you only want to trap a single exception, use a singleton tuple: |
|
1101 | you only want to trap a single exception, use a singleton tuple: | |
1260 |
|
1102 | |||
1261 | exc_tuple == (MyCustomException,) |
|
1103 | exc_tuple == (MyCustomException,) | |
1262 |
|
1104 | |||
1263 | - handler: this must be defined as a function with the following |
|
1105 | - handler: this must be defined as a function with the following | |
1264 | basic interface: def my_handler(self,etype,value,tb). |
|
1106 | basic interface: def my_handler(self,etype,value,tb). | |
1265 |
|
1107 | |||
1266 | This will be made into an instance method (via new.instancemethod) |
|
1108 | This will be made into an instance method (via new.instancemethod) | |
1267 | of IPython itself, and it will be called if any of the exceptions |
|
1109 | of IPython itself, and it will be called if any of the exceptions | |
1268 | listed in the exc_tuple are caught. If the handler is None, an |
|
1110 | listed in the exc_tuple are caught. If the handler is None, an | |
1269 | internal basic one is used, which just prints basic info. |
|
1111 | internal basic one is used, which just prints basic info. | |
1270 |
|
1112 | |||
1271 | WARNING: by putting in your own exception handler into IPython's main |
|
1113 | WARNING: by putting in your own exception handler into IPython's main | |
1272 | execution loop, you run a very good chance of nasty crashes. This |
|
1114 | execution loop, you run a very good chance of nasty crashes. This | |
1273 | facility should only be used if you really know what you are doing.""" |
|
1115 | facility should only be used if you really know what you are doing.""" | |
1274 |
|
1116 | |||
1275 | assert type(exc_tuple)==type(()) , \ |
|
1117 | assert type(exc_tuple)==type(()) , \ | |
1276 | "The custom exceptions must be given AS A TUPLE." |
|
1118 | "The custom exceptions must be given AS A TUPLE." | |
1277 |
|
1119 | |||
1278 | def dummy_handler(self,etype,value,tb): |
|
1120 | def dummy_handler(self,etype,value,tb): | |
1279 | print '*** Simple custom exception handler ***' |
|
1121 | print '*** Simple custom exception handler ***' | |
1280 | print 'Exception type :',etype |
|
1122 | print 'Exception type :',etype | |
1281 | print 'Exception value:',value |
|
1123 | print 'Exception value:',value | |
1282 | print 'Traceback :',tb |
|
1124 | print 'Traceback :',tb | |
1283 | print 'Source code :','\n'.join(self.buffer) |
|
1125 | print 'Source code :','\n'.join(self.buffer) | |
1284 |
|
1126 | |||
1285 | if handler is None: handler = dummy_handler |
|
1127 | if handler is None: handler = dummy_handler | |
1286 |
|
1128 | |||
1287 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
1129 | self.CustomTB = new.instancemethod(handler,self,self.__class__) | |
1288 | self.custom_exceptions = exc_tuple |
|
1130 | self.custom_exceptions = exc_tuple | |
1289 |
|
1131 | |||
1290 | def excepthook(self, etype, value, tb): |
|
1132 | def excepthook(self, etype, value, tb): | |
1291 | """One more defense for GUI apps that call sys.excepthook. |
|
1133 | """One more defense for GUI apps that call sys.excepthook. | |
1292 |
|
1134 | |||
1293 | GUI frameworks like wxPython trap exceptions and call |
|
1135 | GUI frameworks like wxPython trap exceptions and call | |
1294 | sys.excepthook themselves. I guess this is a feature that |
|
1136 | sys.excepthook themselves. I guess this is a feature that | |
1295 | enables them to keep running after exceptions that would |
|
1137 | enables them to keep running after exceptions that would | |
1296 | otherwise kill their mainloop. This is a bother for IPython |
|
1138 | otherwise kill their mainloop. This is a bother for IPython | |
1297 | which excepts to catch all of the program exceptions with a try: |
|
1139 | which excepts to catch all of the program exceptions with a try: | |
1298 | except: statement. |
|
1140 | except: statement. | |
1299 |
|
1141 | |||
1300 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1142 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1301 | any app directly invokes sys.excepthook, it will look to the user like |
|
1143 | any app directly invokes sys.excepthook, it will look to the user like | |
1302 | IPython crashed. In order to work around this, we can disable the |
|
1144 | IPython crashed. In order to work around this, we can disable the | |
1303 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1145 | CrashHandler and replace it with this excepthook instead, which prints a | |
1304 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1146 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1305 | call sys.excepthook will generate a regular-looking exception from |
|
1147 | call sys.excepthook will generate a regular-looking exception from | |
1306 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1148 | IPython, and the CrashHandler will only be triggered by real IPython | |
1307 | crashes. |
|
1149 | crashes. | |
1308 |
|
1150 | |||
1309 | This hook should be used sparingly, only in places which are not likely |
|
1151 | This hook should be used sparingly, only in places which are not likely | |
1310 | to be true IPython errors. |
|
1152 | to be true IPython errors. | |
1311 | """ |
|
1153 | """ | |
1312 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1154 | self.showtraceback((etype,value,tb),tb_offset=0) | |
1313 |
|
1155 | |||
1314 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, |
|
1156 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, | |
1315 | exception_only=False): |
|
1157 | exception_only=False): | |
1316 | """Display the exception that just occurred. |
|
1158 | """Display the exception that just occurred. | |
1317 |
|
1159 | |||
1318 | If nothing is known about the exception, this is the method which |
|
1160 | If nothing is known about the exception, this is the method which | |
1319 | should be used throughout the code for presenting user tracebacks, |
|
1161 | should be used throughout the code for presenting user tracebacks, | |
1320 | rather than directly invoking the InteractiveTB object. |
|
1162 | rather than directly invoking the InteractiveTB object. | |
1321 |
|
1163 | |||
1322 | A specific showsyntaxerror() also exists, but this method can take |
|
1164 | A specific showsyntaxerror() also exists, but this method can take | |
1323 | care of calling it if needed, so unless you are explicitly catching a |
|
1165 | care of calling it if needed, so unless you are explicitly catching a | |
1324 | SyntaxError exception, don't try to analyze the stack manually and |
|
1166 | SyntaxError exception, don't try to analyze the stack manually and | |
1325 | simply call this method.""" |
|
1167 | simply call this method.""" | |
1326 |
|
1168 | |||
1327 | try: |
|
1169 | try: | |
1328 | if exc_tuple is None: |
|
1170 | if exc_tuple is None: | |
1329 | etype, value, tb = sys.exc_info() |
|
1171 | etype, value, tb = sys.exc_info() | |
1330 | else: |
|
1172 | else: | |
1331 | etype, value, tb = exc_tuple |
|
1173 | etype, value, tb = exc_tuple | |
1332 |
|
1174 | |||
1333 | if etype is None: |
|
1175 | if etype is None: | |
1334 | if hasattr(sys, 'last_type'): |
|
1176 | if hasattr(sys, 'last_type'): | |
1335 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1177 | etype, value, tb = sys.last_type, sys.last_value, \ | |
1336 | sys.last_traceback |
|
1178 | sys.last_traceback | |
1337 | else: |
|
1179 | else: | |
1338 | self.write('No traceback available to show.\n') |
|
1180 | self.write('No traceback available to show.\n') | |
1339 | return |
|
1181 | return | |
1340 |
|
1182 | |||
1341 | if etype is SyntaxError: |
|
1183 | if etype is SyntaxError: | |
1342 | # Though this won't be called by syntax errors in the input |
|
1184 | # Though this won't be called by syntax errors in the input | |
1343 | # line, there may be SyntaxError cases whith imported code. |
|
1185 | # line, there may be SyntaxError cases whith imported code. | |
1344 | self.showsyntaxerror(filename) |
|
1186 | self.showsyntaxerror(filename) | |
1345 | elif etype is UsageError: |
|
1187 | elif etype is UsageError: | |
1346 | print "UsageError:", value |
|
1188 | print "UsageError:", value | |
1347 | else: |
|
1189 | else: | |
1348 | # WARNING: these variables are somewhat deprecated and not |
|
1190 | # WARNING: these variables are somewhat deprecated and not | |
1349 | # necessarily safe to use in a threaded environment, but tools |
|
1191 | # necessarily safe to use in a threaded environment, but tools | |
1350 | # like pdb depend on their existence, so let's set them. If we |
|
1192 | # like pdb depend on their existence, so let's set them. If we | |
1351 | # find problems in the field, we'll need to revisit their use. |
|
1193 | # find problems in the field, we'll need to revisit their use. | |
1352 | sys.last_type = etype |
|
1194 | sys.last_type = etype | |
1353 | sys.last_value = value |
|
1195 | sys.last_value = value | |
1354 | sys.last_traceback = tb |
|
1196 | sys.last_traceback = tb | |
1355 |
|
1197 | |||
1356 | if etype in self.custom_exceptions: |
|
1198 | if etype in self.custom_exceptions: | |
1357 | self.CustomTB(etype,value,tb) |
|
1199 | self.CustomTB(etype,value,tb) | |
1358 | else: |
|
1200 | else: | |
1359 | if exception_only: |
|
1201 | if exception_only: | |
1360 | m = ('An exception has occurred, use %tb to see the ' |
|
1202 | m = ('An exception has occurred, use %tb to see the ' | |
1361 | 'full traceback.') |
|
1203 | 'full traceback.') | |
1362 | print m |
|
1204 | print m | |
1363 | self.InteractiveTB.show_exception_only(etype, value) |
|
1205 | self.InteractiveTB.show_exception_only(etype, value) | |
1364 | else: |
|
1206 | else: | |
1365 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) |
|
1207 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) | |
1366 | if self.InteractiveTB.call_pdb: |
|
1208 | if self.InteractiveTB.call_pdb: | |
1367 | # pdb mucks up readline, fix it back |
|
1209 | # pdb mucks up readline, fix it back | |
1368 | self.set_completer() |
|
1210 | self.set_completer() | |
1369 |
|
1211 | |||
1370 | except KeyboardInterrupt: |
|
1212 | except KeyboardInterrupt: | |
1371 | self.write("\nKeyboardInterrupt\n") |
|
1213 | self.write("\nKeyboardInterrupt\n") | |
1372 |
|
1214 | |||
1373 |
|
1215 | |||
1374 | def showsyntaxerror(self, filename=None): |
|
1216 | def showsyntaxerror(self, filename=None): | |
1375 | """Display the syntax error that just occurred. |
|
1217 | """Display the syntax error that just occurred. | |
1376 |
|
1218 | |||
1377 | This doesn't display a stack trace because there isn't one. |
|
1219 | This doesn't display a stack trace because there isn't one. | |
1378 |
|
1220 | |||
1379 | If a filename is given, it is stuffed in the exception instead |
|
1221 | If a filename is given, it is stuffed in the exception instead | |
1380 | of what was there before (because Python's parser always uses |
|
1222 | of what was there before (because Python's parser always uses | |
1381 | "<string>" when reading from a string). |
|
1223 | "<string>" when reading from a string). | |
1382 | """ |
|
1224 | """ | |
1383 | etype, value, last_traceback = sys.exc_info() |
|
1225 | etype, value, last_traceback = sys.exc_info() | |
1384 |
|
1226 | |||
1385 | # See note about these variables in showtraceback() above |
|
1227 | # See note about these variables in showtraceback() above | |
1386 | sys.last_type = etype |
|
1228 | sys.last_type = etype | |
1387 | sys.last_value = value |
|
1229 | sys.last_value = value | |
1388 | sys.last_traceback = last_traceback |
|
1230 | sys.last_traceback = last_traceback | |
1389 |
|
1231 | |||
1390 | if filename and etype is SyntaxError: |
|
1232 | if filename and etype is SyntaxError: | |
1391 | # Work hard to stuff the correct filename in the exception |
|
1233 | # Work hard to stuff the correct filename in the exception | |
1392 | try: |
|
1234 | try: | |
1393 | msg, (dummy_filename, lineno, offset, line) = value |
|
1235 | msg, (dummy_filename, lineno, offset, line) = value | |
1394 | except: |
|
1236 | except: | |
1395 | # Not the format we expect; leave it alone |
|
1237 | # Not the format we expect; leave it alone | |
1396 | pass |
|
1238 | pass | |
1397 | else: |
|
1239 | else: | |
1398 | # Stuff in the right filename |
|
1240 | # Stuff in the right filename | |
1399 | try: |
|
1241 | try: | |
1400 | # Assume SyntaxError is a class exception |
|
1242 | # Assume SyntaxError is a class exception | |
1401 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1243 | value = SyntaxError(msg, (filename, lineno, offset, line)) | |
1402 | except: |
|
1244 | except: | |
1403 | # If that failed, assume SyntaxError is a string |
|
1245 | # If that failed, assume SyntaxError is a string | |
1404 | value = msg, (filename, lineno, offset, line) |
|
1246 | value = msg, (filename, lineno, offset, line) | |
1405 | self.SyntaxTB(etype,value,[]) |
|
1247 | self.SyntaxTB(etype,value,[]) | |
1406 |
|
1248 | |||
1407 | def edit_syntax_error(self): |
|
|||
1408 | """The bottom half of the syntax error handler called in the main loop. |
|
|||
1409 |
|
||||
1410 | Loop until syntax error is fixed or user cancels. |
|
|||
1411 | """ |
|
|||
1412 |
|
||||
1413 | while self.SyntaxTB.last_syntax_error: |
|
|||
1414 | # copy and clear last_syntax_error |
|
|||
1415 | err = self.SyntaxTB.clear_err_state() |
|
|||
1416 | if not self._should_recompile(err): |
|
|||
1417 | return |
|
|||
1418 | try: |
|
|||
1419 | # may set last_syntax_error again if a SyntaxError is raised |
|
|||
1420 | self.safe_execfile(err.filename,self.user_ns) |
|
|||
1421 | except: |
|
|||
1422 | self.showtraceback() |
|
|||
1423 | else: |
|
|||
1424 | try: |
|
|||
1425 | f = file(err.filename) |
|
|||
1426 | try: |
|
|||
1427 | # This should be inside a display_trap block and I |
|
|||
1428 | # think it is. |
|
|||
1429 | sys.displayhook(f.read()) |
|
|||
1430 | finally: |
|
|||
1431 | f.close() |
|
|||
1432 | except: |
|
|||
1433 | self.showtraceback() |
|
|||
1434 |
|
||||
1435 | def _should_recompile(self,e): |
|
|||
1436 | """Utility routine for edit_syntax_error""" |
|
|||
1437 |
|
||||
1438 | if e.filename in ('<ipython console>','<input>','<string>', |
|
|||
1439 | '<console>','<BackgroundJob compilation>', |
|
|||
1440 | None): |
|
|||
1441 |
|
||||
1442 | return False |
|
|||
1443 | try: |
|
|||
1444 | if (self.autoedit_syntax and |
|
|||
1445 | not self.ask_yes_no('Return to editor to correct syntax error? ' |
|
|||
1446 | '[Y/n] ','y')): |
|
|||
1447 | return False |
|
|||
1448 | except EOFError: |
|
|||
1449 | return False |
|
|||
1450 |
|
||||
1451 | def int0(x): |
|
|||
1452 | try: |
|
|||
1453 | return int(x) |
|
|||
1454 | except TypeError: |
|
|||
1455 | return 0 |
|
|||
1456 | # always pass integer line and offset values to editor hook |
|
|||
1457 | try: |
|
|||
1458 | self.hooks.fix_error_editor(e.filename, |
|
|||
1459 | int0(e.lineno),int0(e.offset),e.msg) |
|
|||
1460 | except TryNext: |
|
|||
1461 | warn('Could not open editor') |
|
|||
1462 | return False |
|
|||
1463 | return True |
|
|||
1464 |
|
||||
1465 | #------------------------------------------------------------------------- |
|
1249 | #------------------------------------------------------------------------- | |
1466 | # Things related to tab completion |
|
1250 | # Things related to tab completion | |
1467 | #------------------------------------------------------------------------- |
|
1251 | #------------------------------------------------------------------------- | |
1468 |
|
1252 | |||
1469 | def complete(self, text): |
|
1253 | def complete(self, text): | |
1470 | """Return a sorted list of all possible completions on text. |
|
1254 | """Return a sorted list of all possible completions on text. | |
1471 |
|
1255 | |||
1472 | Inputs: |
|
1256 | Inputs: | |
1473 |
|
1257 | |||
1474 | - text: a string of text to be completed on. |
|
1258 | - text: a string of text to be completed on. | |
1475 |
|
1259 | |||
1476 | This is a wrapper around the completion mechanism, similar to what |
|
1260 | This is a wrapper around the completion mechanism, similar to what | |
1477 | readline does at the command line when the TAB key is hit. By |
|
1261 | readline does at the command line when the TAB key is hit. By | |
1478 | exposing it as a method, it can be used by other non-readline |
|
1262 | exposing it as a method, it can be used by other non-readline | |
1479 | environments (such as GUIs) for text completion. |
|
1263 | environments (such as GUIs) for text completion. | |
1480 |
|
1264 | |||
1481 | Simple usage example: |
|
1265 | Simple usage example: | |
1482 |
|
1266 | |||
1483 | In [7]: x = 'hello' |
|
1267 | In [7]: x = 'hello' | |
1484 |
|
1268 | |||
1485 | In [8]: x |
|
1269 | In [8]: x | |
1486 | Out[8]: 'hello' |
|
1270 | Out[8]: 'hello' | |
1487 |
|
1271 | |||
1488 | In [9]: print x |
|
1272 | In [9]: print x | |
1489 | hello |
|
1273 | hello | |
1490 |
|
1274 | |||
1491 | In [10]: _ip.complete('x.l') |
|
1275 | In [10]: _ip.complete('x.l') | |
1492 | Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] |
|
1276 | Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] | |
1493 | """ |
|
1277 | """ | |
1494 |
|
1278 | |||
1495 | # Inject names into __builtin__ so we can complete on the added names. |
|
1279 | # Inject names into __builtin__ so we can complete on the added names. | |
1496 | with self.builtin_trap: |
|
1280 | with self.builtin_trap: | |
1497 | complete = self.Completer.complete |
|
1281 | complete = self.Completer.complete | |
1498 | state = 0 |
|
1282 | state = 0 | |
1499 | # use a dict so we get unique keys, since ipyhton's multiple |
|
1283 | # use a dict so we get unique keys, since ipyhton's multiple | |
1500 | # completers can return duplicates. When we make 2.4 a requirement, |
|
1284 | # completers can return duplicates. When we make 2.4 a requirement, | |
1501 | # start using sets instead, which are faster. |
|
1285 | # start using sets instead, which are faster. | |
1502 | comps = {} |
|
1286 | comps = {} | |
1503 | while True: |
|
1287 | while True: | |
1504 | newcomp = complete(text,state,line_buffer=text) |
|
1288 | newcomp = complete(text,state,line_buffer=text) | |
1505 | if newcomp is None: |
|
1289 | if newcomp is None: | |
1506 | break |
|
1290 | break | |
1507 | comps[newcomp] = 1 |
|
1291 | comps[newcomp] = 1 | |
1508 | state += 1 |
|
1292 | state += 1 | |
1509 | outcomps = comps.keys() |
|
1293 | outcomps = comps.keys() | |
1510 | outcomps.sort() |
|
1294 | outcomps.sort() | |
1511 | #print "T:",text,"OC:",outcomps # dbg |
|
1295 | #print "T:",text,"OC:",outcomps # dbg | |
1512 | #print "vars:",self.user_ns.keys() |
|
1296 | #print "vars:",self.user_ns.keys() | |
1513 | return outcomps |
|
1297 | return outcomps | |
1514 |
|
1298 | |||
1515 | def set_custom_completer(self,completer,pos=0): |
|
1299 | def set_custom_completer(self,completer,pos=0): | |
1516 | """Adds a new custom completer function. |
|
1300 | """Adds a new custom completer function. | |
1517 |
|
1301 | |||
1518 | The position argument (defaults to 0) is the index in the completers |
|
1302 | The position argument (defaults to 0) is the index in the completers | |
1519 | list where you want the completer to be inserted.""" |
|
1303 | list where you want the completer to be inserted.""" | |
1520 |
|
1304 | |||
1521 | newcomp = new.instancemethod(completer,self.Completer, |
|
1305 | newcomp = new.instancemethod(completer,self.Completer, | |
1522 | self.Completer.__class__) |
|
1306 | self.Completer.__class__) | |
1523 | self.Completer.matchers.insert(pos,newcomp) |
|
1307 | self.Completer.matchers.insert(pos,newcomp) | |
1524 |
|
1308 | |||
1525 | def set_completer(self): |
|
1309 | def set_completer(self): | |
1526 | """Reset readline's completer to be our own.""" |
|
1310 | """Reset readline's completer to be our own.""" | |
1527 | self.readline.set_completer(self.Completer.complete) |
|
1311 | self.readline.set_completer(self.Completer.complete) | |
1528 |
|
1312 | |||
1529 | def set_completer_frame(self, frame=None): |
|
1313 | def set_completer_frame(self, frame=None): | |
1530 | """Set the frame of the completer.""" |
|
1314 | """Set the frame of the completer.""" | |
1531 | if frame: |
|
1315 | if frame: | |
1532 | self.Completer.namespace = frame.f_locals |
|
1316 | self.Completer.namespace = frame.f_locals | |
1533 | self.Completer.global_namespace = frame.f_globals |
|
1317 | self.Completer.global_namespace = frame.f_globals | |
1534 | else: |
|
1318 | else: | |
1535 | self.Completer.namespace = self.user_ns |
|
1319 | self.Completer.namespace = self.user_ns | |
1536 | self.Completer.global_namespace = self.user_global_ns |
|
1320 | self.Completer.global_namespace = self.user_global_ns | |
1537 |
|
1321 | |||
1538 | #------------------------------------------------------------------------- |
|
1322 | #------------------------------------------------------------------------- | |
1539 | # Things related to readline |
|
1323 | # Things related to readline | |
1540 | #------------------------------------------------------------------------- |
|
1324 | #------------------------------------------------------------------------- | |
1541 |
|
1325 | |||
1542 | def init_readline(self): |
|
1326 | def init_readline(self): | |
1543 | """Command history completion/saving/reloading.""" |
|
1327 | """Command history completion/saving/reloading.""" | |
1544 |
|
1328 | |||
1545 | if self.readline_use: |
|
1329 | if self.readline_use: | |
1546 | import IPython.utils.rlineimpl as readline |
|
1330 | import IPython.utils.rlineimpl as readline | |
1547 |
|
1331 | |||
1548 | self.rl_next_input = None |
|
1332 | self.rl_next_input = None | |
1549 | self.rl_do_indent = False |
|
1333 | self.rl_do_indent = False | |
1550 |
|
1334 | |||
1551 | if not self.readline_use or not readline.have_readline: |
|
1335 | if not self.readline_use or not readline.have_readline: | |
1552 | self.has_readline = False |
|
1336 | self.has_readline = False | |
1553 | self.readline = None |
|
1337 | self.readline = None | |
1554 | # Set a number of methods that depend on readline to be no-op |
|
1338 | # Set a number of methods that depend on readline to be no-op | |
1555 | self.savehist = no_op |
|
1339 | self.savehist = no_op | |
1556 | self.reloadhist = no_op |
|
1340 | self.reloadhist = no_op | |
1557 | self.set_completer = no_op |
|
1341 | self.set_completer = no_op | |
1558 | self.set_custom_completer = no_op |
|
1342 | self.set_custom_completer = no_op | |
1559 | self.set_completer_frame = no_op |
|
1343 | self.set_completer_frame = no_op | |
1560 | warn('Readline services not available or not loaded.') |
|
1344 | warn('Readline services not available or not loaded.') | |
1561 | else: |
|
1345 | else: | |
1562 | self.has_readline = True |
|
1346 | self.has_readline = True | |
1563 | self.readline = readline |
|
1347 | self.readline = readline | |
1564 | sys.modules['readline'] = readline |
|
1348 | sys.modules['readline'] = readline | |
1565 | import atexit |
|
1349 | import atexit | |
1566 | from IPython.core.completer import IPCompleter |
|
1350 | from IPython.core.completer import IPCompleter | |
1567 | self.Completer = IPCompleter(self, |
|
1351 | self.Completer = IPCompleter(self, | |
1568 | self.user_ns, |
|
1352 | self.user_ns, | |
1569 | self.user_global_ns, |
|
1353 | self.user_global_ns, | |
1570 | self.readline_omit__names, |
|
1354 | self.readline_omit__names, | |
1571 | self.alias_manager.alias_table) |
|
1355 | self.alias_manager.alias_table) | |
1572 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1356 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
1573 | self.strdispatchers['complete_command'] = sdisp |
|
1357 | self.strdispatchers['complete_command'] = sdisp | |
1574 | self.Completer.custom_completers = sdisp |
|
1358 | self.Completer.custom_completers = sdisp | |
1575 | # Platform-specific configuration |
|
1359 | # Platform-specific configuration | |
1576 | if os.name == 'nt': |
|
1360 | if os.name == 'nt': | |
1577 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1361 | self.readline_startup_hook = readline.set_pre_input_hook | |
1578 | else: |
|
1362 | else: | |
1579 | self.readline_startup_hook = readline.set_startup_hook |
|
1363 | self.readline_startup_hook = readline.set_startup_hook | |
1580 |
|
1364 | |||
1581 | # Load user's initrc file (readline config) |
|
1365 | # Load user's initrc file (readline config) | |
1582 | # Or if libedit is used, load editrc. |
|
1366 | # Or if libedit is used, load editrc. | |
1583 | inputrc_name = os.environ.get('INPUTRC') |
|
1367 | inputrc_name = os.environ.get('INPUTRC') | |
1584 | if inputrc_name is None: |
|
1368 | if inputrc_name is None: | |
1585 | home_dir = get_home_dir() |
|
1369 | home_dir = get_home_dir() | |
1586 | if home_dir is not None: |
|
1370 | if home_dir is not None: | |
1587 | inputrc_name = '.inputrc' |
|
1371 | inputrc_name = '.inputrc' | |
1588 | if readline.uses_libedit: |
|
1372 | if readline.uses_libedit: | |
1589 | inputrc_name = '.editrc' |
|
1373 | inputrc_name = '.editrc' | |
1590 | inputrc_name = os.path.join(home_dir, inputrc_name) |
|
1374 | inputrc_name = os.path.join(home_dir, inputrc_name) | |
1591 | if os.path.isfile(inputrc_name): |
|
1375 | if os.path.isfile(inputrc_name): | |
1592 | try: |
|
1376 | try: | |
1593 | readline.read_init_file(inputrc_name) |
|
1377 | readline.read_init_file(inputrc_name) | |
1594 | except: |
|
1378 | except: | |
1595 | warn('Problems reading readline initialization file <%s>' |
|
1379 | warn('Problems reading readline initialization file <%s>' | |
1596 | % inputrc_name) |
|
1380 | % inputrc_name) | |
1597 |
|
1381 | |||
1598 | # save this in sys so embedded copies can restore it properly |
|
1382 | # save this in sys so embedded copies can restore it properly | |
1599 | sys.ipcompleter = self.Completer.complete |
|
1383 | sys.ipcompleter = self.Completer.complete | |
1600 | self.set_completer() |
|
1384 | self.set_completer() | |
1601 |
|
1385 | |||
1602 | # Configure readline according to user's prefs |
|
1386 | # Configure readline according to user's prefs | |
1603 | # This is only done if GNU readline is being used. If libedit |
|
1387 | # This is only done if GNU readline is being used. If libedit | |
1604 | # is being used (as on Leopard) the readline config is |
|
1388 | # is being used (as on Leopard) the readline config is | |
1605 | # not run as the syntax for libedit is different. |
|
1389 | # not run as the syntax for libedit is different. | |
1606 | if not readline.uses_libedit: |
|
1390 | if not readline.uses_libedit: | |
1607 | for rlcommand in self.readline_parse_and_bind: |
|
1391 | for rlcommand in self.readline_parse_and_bind: | |
1608 | #print "loading rl:",rlcommand # dbg |
|
1392 | #print "loading rl:",rlcommand # dbg | |
1609 | readline.parse_and_bind(rlcommand) |
|
1393 | readline.parse_and_bind(rlcommand) | |
1610 |
|
1394 | |||
1611 | # Remove some chars from the delimiters list. If we encounter |
|
1395 | # Remove some chars from the delimiters list. If we encounter | |
1612 | # unicode chars, discard them. |
|
1396 | # unicode chars, discard them. | |
1613 | delims = readline.get_completer_delims().encode("ascii", "ignore") |
|
1397 | delims = readline.get_completer_delims().encode("ascii", "ignore") | |
1614 | delims = delims.translate(string._idmap, |
|
1398 | delims = delims.translate(string._idmap, | |
1615 | self.readline_remove_delims) |
|
1399 | self.readline_remove_delims) | |
1616 | readline.set_completer_delims(delims) |
|
1400 | readline.set_completer_delims(delims) | |
1617 | # otherwise we end up with a monster history after a while: |
|
1401 | # otherwise we end up with a monster history after a while: | |
1618 | readline.set_history_length(1000) |
|
1402 | readline.set_history_length(1000) | |
1619 | try: |
|
1403 | try: | |
1620 | #print '*** Reading readline history' # dbg |
|
1404 | #print '*** Reading readline history' # dbg | |
1621 | readline.read_history_file(self.histfile) |
|
1405 | readline.read_history_file(self.histfile) | |
1622 | except IOError: |
|
1406 | except IOError: | |
1623 | pass # It doesn't exist yet. |
|
1407 | pass # It doesn't exist yet. | |
1624 |
|
1408 | |||
1625 | atexit.register(self.atexit_operations) |
|
1409 | atexit.register(self.atexit_operations) | |
1626 | del atexit |
|
1410 | del atexit | |
1627 |
|
1411 | |||
1628 | # Configure auto-indent for all platforms |
|
1412 | # Configure auto-indent for all platforms | |
1629 | self.set_autoindent(self.autoindent) |
|
1413 | self.set_autoindent(self.autoindent) | |
1630 |
|
1414 | |||
1631 | def set_next_input(self, s): |
|
1415 | def set_next_input(self, s): | |
1632 | """ Sets the 'default' input string for the next command line. |
|
1416 | """ Sets the 'default' input string for the next command line. | |
1633 |
|
1417 | |||
1634 | Requires readline. |
|
1418 | Requires readline. | |
1635 |
|
1419 | |||
1636 | Example: |
|
1420 | Example: | |
1637 |
|
1421 | |||
1638 | [D:\ipython]|1> _ip.set_next_input("Hello Word") |
|
1422 | [D:\ipython]|1> _ip.set_next_input("Hello Word") | |
1639 | [D:\ipython]|2> Hello Word_ # cursor is here |
|
1423 | [D:\ipython]|2> Hello Word_ # cursor is here | |
1640 | """ |
|
1424 | """ | |
1641 |
|
1425 | |||
1642 | self.rl_next_input = s |
|
1426 | self.rl_next_input = s | |
1643 |
|
1427 | |||
|
1428 | # Maybe move this to the terminal subclass? | |||
1644 | def pre_readline(self): |
|
1429 | def pre_readline(self): | |
1645 | """readline hook to be used at the start of each line. |
|
1430 | """readline hook to be used at the start of each line. | |
1646 |
|
1431 | |||
1647 | Currently it handles auto-indent only.""" |
|
1432 | Currently it handles auto-indent only.""" | |
1648 |
|
1433 | |||
1649 | #debugx('self.indent_current_nsp','pre_readline:') |
|
|||
1650 |
|
||||
1651 | if self.rl_do_indent: |
|
1434 | if self.rl_do_indent: | |
1652 | self.readline.insert_text(self._indent_current_str()) |
|
1435 | self.readline.insert_text(self._indent_current_str()) | |
1653 | if self.rl_next_input is not None: |
|
1436 | if self.rl_next_input is not None: | |
1654 | self.readline.insert_text(self.rl_next_input) |
|
1437 | self.readline.insert_text(self.rl_next_input) | |
1655 | self.rl_next_input = None |
|
1438 | self.rl_next_input = None | |
1656 |
|
1439 | |||
1657 | def _indent_current_str(self): |
|
1440 | def _indent_current_str(self): | |
1658 | """return the current level of indentation as a string""" |
|
1441 | """return the current level of indentation as a string""" | |
1659 | return self.indent_current_nsp * ' ' |
|
1442 | return self.indent_current_nsp * ' ' | |
1660 |
|
1443 | |||
1661 | #------------------------------------------------------------------------- |
|
1444 | #------------------------------------------------------------------------- | |
1662 | # Things related to magics |
|
1445 | # Things related to magics | |
1663 | #------------------------------------------------------------------------- |
|
1446 | #------------------------------------------------------------------------- | |
1664 |
|
1447 | |||
1665 | def init_magics(self): |
|
1448 | def init_magics(self): | |
1666 | # Set user colors (don't do it in the constructor above so that it |
|
1449 | # Set user colors (don't do it in the constructor above so that it | |
1667 | # doesn't crash if colors option is invalid) |
|
1450 | # doesn't crash if colors option is invalid) | |
1668 | self.magic_colors(self.colors) |
|
1451 | self.magic_colors(self.colors) | |
1669 | # History was moved to a separate module |
|
1452 | # History was moved to a separate module | |
1670 | from . import history |
|
1453 | from . import history | |
1671 | history.init_ipython(self) |
|
1454 | history.init_ipython(self) | |
1672 |
|
1455 | |||
1673 | def magic(self,arg_s): |
|
1456 | def magic(self,arg_s): | |
1674 | """Call a magic function by name. |
|
1457 | """Call a magic function by name. | |
1675 |
|
1458 | |||
1676 | Input: a string containing the name of the magic function to call and any |
|
1459 | Input: a string containing the name of the magic function to call and any | |
1677 | additional arguments to be passed to the magic. |
|
1460 | additional arguments to be passed to the magic. | |
1678 |
|
1461 | |||
1679 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
1462 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
1680 | prompt: |
|
1463 | prompt: | |
1681 |
|
1464 | |||
1682 | In[1]: %name -opt foo bar |
|
1465 | In[1]: %name -opt foo bar | |
1683 |
|
1466 | |||
1684 | To call a magic without arguments, simply use magic('name'). |
|
1467 | To call a magic without arguments, simply use magic('name'). | |
1685 |
|
1468 | |||
1686 | This provides a proper Python function to call IPython's magics in any |
|
1469 | This provides a proper Python function to call IPython's magics in any | |
1687 | valid Python code you can type at the interpreter, including loops and |
|
1470 | valid Python code you can type at the interpreter, including loops and | |
1688 | compound statements. |
|
1471 | compound statements. | |
1689 | """ |
|
1472 | """ | |
1690 | args = arg_s.split(' ',1) |
|
1473 | args = arg_s.split(' ',1) | |
1691 | magic_name = args[0] |
|
1474 | magic_name = args[0] | |
1692 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
1475 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
1693 |
|
1476 | |||
1694 | try: |
|
1477 | try: | |
1695 | magic_args = args[1] |
|
1478 | magic_args = args[1] | |
1696 | except IndexError: |
|
1479 | except IndexError: | |
1697 | magic_args = '' |
|
1480 | magic_args = '' | |
1698 | fn = getattr(self,'magic_'+magic_name,None) |
|
1481 | fn = getattr(self,'magic_'+magic_name,None) | |
1699 | if fn is None: |
|
1482 | if fn is None: | |
1700 | error("Magic function `%s` not found." % magic_name) |
|
1483 | error("Magic function `%s` not found." % magic_name) | |
1701 | else: |
|
1484 | else: | |
1702 | magic_args = self.var_expand(magic_args,1) |
|
1485 | magic_args = self.var_expand(magic_args,1) | |
1703 | with nested(self.builtin_trap,): |
|
1486 | with nested(self.builtin_trap,): | |
1704 | result = fn(magic_args) |
|
1487 | result = fn(magic_args) | |
1705 | return result |
|
1488 | return result | |
1706 |
|
1489 | |||
1707 | def define_magic(self, magicname, func): |
|
1490 | def define_magic(self, magicname, func): | |
1708 | """Expose own function as magic function for ipython |
|
1491 | """Expose own function as magic function for ipython | |
1709 |
|
1492 | |||
1710 | def foo_impl(self,parameter_s=''): |
|
1493 | def foo_impl(self,parameter_s=''): | |
1711 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
1494 | 'My very own magic!. (Use docstrings, IPython reads them).' | |
1712 | print 'Magic function. Passed parameter is between < >:' |
|
1495 | print 'Magic function. Passed parameter is between < >:' | |
1713 | print '<%s>' % parameter_s |
|
1496 | print '<%s>' % parameter_s | |
1714 | print 'The self object is:',self |
|
1497 | print 'The self object is:',self | |
1715 |
|
1498 | |||
1716 | self.define_magic('foo',foo_impl) |
|
1499 | self.define_magic('foo',foo_impl) | |
1717 | """ |
|
1500 | """ | |
1718 |
|
1501 | |||
1719 | import new |
|
1502 | import new | |
1720 | im = new.instancemethod(func,self, self.__class__) |
|
1503 | im = new.instancemethod(func,self, self.__class__) | |
1721 | old = getattr(self, "magic_" + magicname, None) |
|
1504 | old = getattr(self, "magic_" + magicname, None) | |
1722 | setattr(self, "magic_" + magicname, im) |
|
1505 | setattr(self, "magic_" + magicname, im) | |
1723 | return old |
|
1506 | return old | |
1724 |
|
1507 | |||
1725 | #------------------------------------------------------------------------- |
|
1508 | #------------------------------------------------------------------------- | |
1726 | # Things related to macros |
|
1509 | # Things related to macros | |
1727 | #------------------------------------------------------------------------- |
|
1510 | #------------------------------------------------------------------------- | |
1728 |
|
1511 | |||
1729 | def define_macro(self, name, themacro): |
|
1512 | def define_macro(self, name, themacro): | |
1730 | """Define a new macro |
|
1513 | """Define a new macro | |
1731 |
|
1514 | |||
1732 | Parameters |
|
1515 | Parameters | |
1733 | ---------- |
|
1516 | ---------- | |
1734 | name : str |
|
1517 | name : str | |
1735 | The name of the macro. |
|
1518 | The name of the macro. | |
1736 | themacro : str or Macro |
|
1519 | themacro : str or Macro | |
1737 | The action to do upon invoking the macro. If a string, a new |
|
1520 | The action to do upon invoking the macro. If a string, a new | |
1738 | Macro object is created by passing the string to it. |
|
1521 | Macro object is created by passing the string to it. | |
1739 | """ |
|
1522 | """ | |
1740 |
|
1523 | |||
1741 | from IPython.core import macro |
|
1524 | from IPython.core import macro | |
1742 |
|
1525 | |||
1743 | if isinstance(themacro, basestring): |
|
1526 | if isinstance(themacro, basestring): | |
1744 | themacro = macro.Macro(themacro) |
|
1527 | themacro = macro.Macro(themacro) | |
1745 | if not isinstance(themacro, macro.Macro): |
|
1528 | if not isinstance(themacro, macro.Macro): | |
1746 | raise ValueError('A macro must be a string or a Macro instance.') |
|
1529 | raise ValueError('A macro must be a string or a Macro instance.') | |
1747 | self.user_ns[name] = themacro |
|
1530 | self.user_ns[name] = themacro | |
1748 |
|
1531 | |||
1749 | #------------------------------------------------------------------------- |
|
1532 | #------------------------------------------------------------------------- | |
1750 | # Things related to the running of system commands |
|
1533 | # Things related to the running of system commands | |
1751 | #------------------------------------------------------------------------- |
|
1534 | #------------------------------------------------------------------------- | |
1752 |
|
1535 | |||
1753 | def system(self, cmd): |
|
1536 | def system(self, cmd): | |
1754 | """Make a system call, using IPython.""" |
|
1537 | """Make a system call, using IPython.""" | |
1755 | return self.hooks.shell_hook(self.var_expand(cmd, depth=2)) |
|
1538 | return self.hooks.shell_hook(self.var_expand(cmd, depth=2)) | |
1756 |
|
1539 | |||
1757 | #------------------------------------------------------------------------- |
|
1540 | #------------------------------------------------------------------------- | |
1758 | # Things related to aliases |
|
1541 | # Things related to aliases | |
1759 | #------------------------------------------------------------------------- |
|
1542 | #------------------------------------------------------------------------- | |
1760 |
|
1543 | |||
1761 | def init_alias(self): |
|
1544 | def init_alias(self): | |
1762 | self.alias_manager = AliasManager(shell=self, config=self.config) |
|
1545 | self.alias_manager = AliasManager(shell=self, config=self.config) | |
1763 | self.ns_table['alias'] = self.alias_manager.alias_table, |
|
1546 | self.ns_table['alias'] = self.alias_manager.alias_table, | |
1764 |
|
1547 | |||
1765 | #------------------------------------------------------------------------- |
|
1548 | #------------------------------------------------------------------------- | |
1766 | # Things related to extensions and plugins |
|
1549 | # Things related to extensions and plugins | |
1767 | #------------------------------------------------------------------------- |
|
1550 | #------------------------------------------------------------------------- | |
1768 |
|
1551 | |||
1769 | def init_extension_manager(self): |
|
1552 | def init_extension_manager(self): | |
1770 | self.extension_manager = ExtensionManager(shell=self, config=self.config) |
|
1553 | self.extension_manager = ExtensionManager(shell=self, config=self.config) | |
1771 |
|
1554 | |||
1772 | def init_plugin_manager(self): |
|
1555 | def init_plugin_manager(self): | |
1773 | self.plugin_manager = PluginManager(config=self.config) |
|
1556 | self.plugin_manager = PluginManager(config=self.config) | |
1774 |
|
1557 | |||
1775 | #------------------------------------------------------------------------- |
|
1558 | #------------------------------------------------------------------------- | |
|
1559 | # Things related to the prefilter | |||
|
1560 | #------------------------------------------------------------------------- | |||
|
1561 | ||||
|
1562 | def init_prefilter(self): | |||
|
1563 | self.prefilter_manager = PrefilterManager(shell=self, config=self.config) | |||
|
1564 | # Ultimately this will be refactored in the new interpreter code, but | |||
|
1565 | # for now, we should expose the main prefilter method (there's legacy | |||
|
1566 | # code out there that may rely on this). | |||
|
1567 | self.prefilter = self.prefilter_manager.prefilter_lines | |||
|
1568 | ||||
|
1569 | #------------------------------------------------------------------------- | |||
1776 | # Things related to the running of code |
|
1570 | # Things related to the running of code | |
1777 | #------------------------------------------------------------------------- |
|
1571 | #------------------------------------------------------------------------- | |
1778 |
|
1572 | |||
1779 | def ex(self, cmd): |
|
1573 | def ex(self, cmd): | |
1780 | """Execute a normal python statement in user namespace.""" |
|
1574 | """Execute a normal python statement in user namespace.""" | |
1781 | with nested(self.builtin_trap,): |
|
1575 | with nested(self.builtin_trap,): | |
1782 | exec cmd in self.user_global_ns, self.user_ns |
|
1576 | exec cmd in self.user_global_ns, self.user_ns | |
1783 |
|
1577 | |||
1784 | def ev(self, expr): |
|
1578 | def ev(self, expr): | |
1785 | """Evaluate python expression expr in user namespace. |
|
1579 | """Evaluate python expression expr in user namespace. | |
1786 |
|
1580 | |||
1787 | Returns the result of evaluation |
|
1581 | Returns the result of evaluation | |
1788 | """ |
|
1582 | """ | |
1789 | with nested(self.builtin_trap,): |
|
1583 | with nested(self.builtin_trap,): | |
1790 | return eval(expr, self.user_global_ns, self.user_ns) |
|
1584 | return eval(expr, self.user_global_ns, self.user_ns) | |
1791 |
|
1585 | |||
1792 | def mainloop(self, display_banner=None): |
|
|||
1793 | """Start the mainloop. |
|
|||
1794 |
|
||||
1795 | If an optional banner argument is given, it will override the |
|
|||
1796 | internally created default banner. |
|
|||
1797 | """ |
|
|||
1798 |
|
||||
1799 | with nested(self.builtin_trap, self.display_trap): |
|
|||
1800 |
|
||||
1801 | # if you run stuff with -c <cmd>, raw hist is not updated |
|
|||
1802 | # ensure that it's in sync |
|
|||
1803 | if len(self.input_hist) != len (self.input_hist_raw): |
|
|||
1804 | self.input_hist_raw = InputList(self.input_hist) |
|
|||
1805 |
|
||||
1806 | while 1: |
|
|||
1807 | try: |
|
|||
1808 | self.interact(display_banner=display_banner) |
|
|||
1809 | #self.interact_with_readline() |
|
|||
1810 | # XXX for testing of a readline-decoupled repl loop, call |
|
|||
1811 | # interact_with_readline above |
|
|||
1812 | break |
|
|||
1813 | except KeyboardInterrupt: |
|
|||
1814 | # this should not be necessary, but KeyboardInterrupt |
|
|||
1815 | # handling seems rather unpredictable... |
|
|||
1816 | self.write("\nKeyboardInterrupt in interact()\n") |
|
|||
1817 |
|
||||
1818 | def interact_prompt(self): |
|
|||
1819 | """ Print the prompt (in read-eval-print loop) |
|
|||
1820 |
|
||||
1821 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not |
|
|||
1822 | used in standard IPython flow. |
|
|||
1823 | """ |
|
|||
1824 | if self.more: |
|
|||
1825 | try: |
|
|||
1826 | prompt = self.hooks.generate_prompt(True) |
|
|||
1827 | except: |
|
|||
1828 | self.showtraceback() |
|
|||
1829 | if self.autoindent: |
|
|||
1830 | self.rl_do_indent = True |
|
|||
1831 |
|
||||
1832 | else: |
|
|||
1833 | try: |
|
|||
1834 | prompt = self.hooks.generate_prompt(False) |
|
|||
1835 | except: |
|
|||
1836 | self.showtraceback() |
|
|||
1837 | self.write(prompt) |
|
|||
1838 |
|
||||
1839 | def interact_handle_input(self,line): |
|
|||
1840 | """ Handle the input line (in read-eval-print loop) |
|
|||
1841 |
|
||||
1842 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not |
|
|||
1843 | used in standard IPython flow. |
|
|||
1844 | """ |
|
|||
1845 | if line.lstrip() == line: |
|
|||
1846 | self.shadowhist.add(line.strip()) |
|
|||
1847 | lineout = self.prefilter_manager.prefilter_lines(line,self.more) |
|
|||
1848 |
|
||||
1849 | if line.strip(): |
|
|||
1850 | if self.more: |
|
|||
1851 | self.input_hist_raw[-1] += '%s\n' % line |
|
|||
1852 | else: |
|
|||
1853 | self.input_hist_raw.append('%s\n' % line) |
|
|||
1854 |
|
||||
1855 |
|
||||
1856 | self.more = self.push_line(lineout) |
|
|||
1857 | if (self.SyntaxTB.last_syntax_error and |
|
|||
1858 | self.autoedit_syntax): |
|
|||
1859 | self.edit_syntax_error() |
|
|||
1860 |
|
||||
1861 | def interact_with_readline(self): |
|
|||
1862 | """ Demo of using interact_handle_input, interact_prompt |
|
|||
1863 |
|
||||
1864 | This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI), |
|
|||
1865 | it should work like this. |
|
|||
1866 | """ |
|
|||
1867 | self.readline_startup_hook(self.pre_readline) |
|
|||
1868 | while not self.exit_now: |
|
|||
1869 | self.interact_prompt() |
|
|||
1870 | if self.more: |
|
|||
1871 | self.rl_do_indent = True |
|
|||
1872 | else: |
|
|||
1873 | self.rl_do_indent = False |
|
|||
1874 | line = raw_input_original().decode(self.stdin_encoding) |
|
|||
1875 | self.interact_handle_input(line) |
|
|||
1876 |
|
||||
1877 | def interact(self, display_banner=None): |
|
|||
1878 | """Closely emulate the interactive Python console.""" |
|
|||
1879 |
|
||||
1880 | # batch run -> do not interact |
|
|||
1881 | if self.exit_now: |
|
|||
1882 | return |
|
|||
1883 |
|
||||
1884 | if display_banner is None: |
|
|||
1885 | display_banner = self.display_banner |
|
|||
1886 | if display_banner: |
|
|||
1887 | self.show_banner() |
|
|||
1888 |
|
||||
1889 | more = 0 |
|
|||
1890 |
|
||||
1891 | # Mark activity in the builtins |
|
|||
1892 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
|||
1893 |
|
||||
1894 | if self.has_readline: |
|
|||
1895 | self.readline_startup_hook(self.pre_readline) |
|
|||
1896 | # exit_now is set by a call to %Exit or %Quit, through the |
|
|||
1897 | # ask_exit callback. |
|
|||
1898 |
|
||||
1899 | while not self.exit_now: |
|
|||
1900 | self.hooks.pre_prompt_hook() |
|
|||
1901 | if more: |
|
|||
1902 | try: |
|
|||
1903 | prompt = self.hooks.generate_prompt(True) |
|
|||
1904 | except: |
|
|||
1905 | self.showtraceback() |
|
|||
1906 | if self.autoindent: |
|
|||
1907 | self.rl_do_indent = True |
|
|||
1908 |
|
||||
1909 | else: |
|
|||
1910 | try: |
|
|||
1911 | prompt = self.hooks.generate_prompt(False) |
|
|||
1912 | except: |
|
|||
1913 | self.showtraceback() |
|
|||
1914 | try: |
|
|||
1915 | line = self.raw_input(prompt, more) |
|
|||
1916 | if self.exit_now: |
|
|||
1917 | # quick exit on sys.std[in|out] close |
|
|||
1918 | break |
|
|||
1919 | if self.autoindent: |
|
|||
1920 | self.rl_do_indent = False |
|
|||
1921 |
|
||||
1922 | except KeyboardInterrupt: |
|
|||
1923 | #double-guard against keyboardinterrupts during kbdint handling |
|
|||
1924 | try: |
|
|||
1925 | self.write('\nKeyboardInterrupt\n') |
|
|||
1926 | self.resetbuffer() |
|
|||
1927 | # keep cache in sync with the prompt counter: |
|
|||
1928 | self.outputcache.prompt_count -= 1 |
|
|||
1929 |
|
||||
1930 | if self.autoindent: |
|
|||
1931 | self.indent_current_nsp = 0 |
|
|||
1932 | more = 0 |
|
|||
1933 | except KeyboardInterrupt: |
|
|||
1934 | pass |
|
|||
1935 | except EOFError: |
|
|||
1936 | if self.autoindent: |
|
|||
1937 | self.rl_do_indent = False |
|
|||
1938 | if self.has_readline: |
|
|||
1939 | self.readline_startup_hook(None) |
|
|||
1940 | self.write('\n') |
|
|||
1941 | self.exit() |
|
|||
1942 | except bdb.BdbQuit: |
|
|||
1943 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
|||
1944 | 'Because of how pdb handles the stack, it is impossible\n' |
|
|||
1945 | 'for IPython to properly format this particular exception.\n' |
|
|||
1946 | 'IPython will resume normal operation.') |
|
|||
1947 | except: |
|
|||
1948 | # exceptions here are VERY RARE, but they can be triggered |
|
|||
1949 | # asynchronously by signal handlers, for example. |
|
|||
1950 | self.showtraceback() |
|
|||
1951 | else: |
|
|||
1952 | more = self.push_line(line) |
|
|||
1953 | if (self.SyntaxTB.last_syntax_error and |
|
|||
1954 | self.autoedit_syntax): |
|
|||
1955 | self.edit_syntax_error() |
|
|||
1956 |
|
||||
1957 | # We are off again... |
|
|||
1958 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
|||
1959 |
|
||||
1960 | # Turn off the exit flag, so the mainloop can be restarted if desired |
|
|||
1961 | self.exit_now = False |
|
|||
1962 |
|
||||
1963 | def safe_execfile(self, fname, *where, **kw): |
|
1586 | def safe_execfile(self, fname, *where, **kw): | |
1964 | """A safe version of the builtin execfile(). |
|
1587 | """A safe version of the builtin execfile(). | |
1965 |
|
1588 | |||
1966 | This version will never throw an exception, but instead print |
|
1589 | This version will never throw an exception, but instead print | |
1967 | helpful error messages to the screen. This only works on pure |
|
1590 | helpful error messages to the screen. This only works on pure | |
1968 | Python files with the .py extension. |
|
1591 | Python files with the .py extension. | |
1969 |
|
1592 | |||
1970 | Parameters |
|
1593 | Parameters | |
1971 | ---------- |
|
1594 | ---------- | |
1972 | fname : string |
|
1595 | fname : string | |
1973 | The name of the file to be executed. |
|
1596 | The name of the file to be executed. | |
1974 | where : tuple |
|
1597 | where : tuple | |
1975 | One or two namespaces, passed to execfile() as (globals,locals). |
|
1598 | One or two namespaces, passed to execfile() as (globals,locals). | |
1976 | If only one is given, it is passed as both. |
|
1599 | If only one is given, it is passed as both. | |
1977 | exit_ignore : bool (False) |
|
1600 | exit_ignore : bool (False) | |
1978 | If True, then silence SystemExit for non-zero status (it is always |
|
1601 | If True, then silence SystemExit for non-zero status (it is always | |
1979 | silenced for zero status, as it is so common). |
|
1602 | silenced for zero status, as it is so common). | |
1980 | """ |
|
1603 | """ | |
1981 | kw.setdefault('exit_ignore', False) |
|
1604 | kw.setdefault('exit_ignore', False) | |
1982 |
|
1605 | |||
1983 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
1606 | fname = os.path.abspath(os.path.expanduser(fname)) | |
1984 |
|
1607 | |||
1985 | # Make sure we have a .py file |
|
1608 | # Make sure we have a .py file | |
1986 | if not fname.endswith('.py'): |
|
1609 | if not fname.endswith('.py'): | |
1987 | warn('File must end with .py to be run using execfile: <%s>' % fname) |
|
1610 | warn('File must end with .py to be run using execfile: <%s>' % fname) | |
1988 |
|
1611 | |||
1989 | # Make sure we can open the file |
|
1612 | # Make sure we can open the file | |
1990 | try: |
|
1613 | try: | |
1991 | with open(fname) as thefile: |
|
1614 | with open(fname) as thefile: | |
1992 | pass |
|
1615 | pass | |
1993 | except: |
|
1616 | except: | |
1994 | warn('Could not open file <%s> for safe execution.' % fname) |
|
1617 | warn('Could not open file <%s> for safe execution.' % fname) | |
1995 | return |
|
1618 | return | |
1996 |
|
1619 | |||
1997 | # Find things also in current directory. This is needed to mimic the |
|
1620 | # Find things also in current directory. This is needed to mimic the | |
1998 | # behavior of running a script from the system command line, where |
|
1621 | # behavior of running a script from the system command line, where | |
1999 | # Python inserts the script's directory into sys.path |
|
1622 | # Python inserts the script's directory into sys.path | |
2000 | dname = os.path.dirname(fname) |
|
1623 | dname = os.path.dirname(fname) | |
2001 |
|
1624 | |||
2002 | with prepended_to_syspath(dname): |
|
1625 | with prepended_to_syspath(dname): | |
2003 | try: |
|
1626 | try: | |
2004 | execfile(fname,*where) |
|
1627 | execfile(fname,*where) | |
2005 | except SystemExit, status: |
|
1628 | except SystemExit, status: | |
2006 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
1629 | # If the call was made with 0 or None exit status (sys.exit(0) | |
2007 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
1630 | # or sys.exit() ), don't bother showing a traceback, as both of | |
2008 | # these are considered normal by the OS: |
|
1631 | # these are considered normal by the OS: | |
2009 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
1632 | # > python -c'import sys;sys.exit(0)'; echo $? | |
2010 | # 0 |
|
1633 | # 0 | |
2011 | # > python -c'import sys;sys.exit()'; echo $? |
|
1634 | # > python -c'import sys;sys.exit()'; echo $? | |
2012 | # 0 |
|
1635 | # 0 | |
2013 | # For other exit status, we show the exception unless |
|
1636 | # For other exit status, we show the exception unless | |
2014 | # explicitly silenced, but only in short form. |
|
1637 | # explicitly silenced, but only in short form. | |
2015 | if status.code not in (0, None) and not kw['exit_ignore']: |
|
1638 | if status.code not in (0, None) and not kw['exit_ignore']: | |
2016 | self.showtraceback(exception_only=True) |
|
1639 | self.showtraceback(exception_only=True) | |
2017 | except: |
|
1640 | except: | |
2018 | self.showtraceback() |
|
1641 | self.showtraceback() | |
2019 |
|
1642 | |||
2020 | def safe_execfile_ipy(self, fname): |
|
1643 | def safe_execfile_ipy(self, fname): | |
2021 | """Like safe_execfile, but for .ipy files with IPython syntax. |
|
1644 | """Like safe_execfile, but for .ipy files with IPython syntax. | |
2022 |
|
1645 | |||
2023 | Parameters |
|
1646 | Parameters | |
2024 | ---------- |
|
1647 | ---------- | |
2025 | fname : str |
|
1648 | fname : str | |
2026 | The name of the file to execute. The filename must have a |
|
1649 | The name of the file to execute. The filename must have a | |
2027 | .ipy extension. |
|
1650 | .ipy extension. | |
2028 | """ |
|
1651 | """ | |
2029 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
1652 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2030 |
|
1653 | |||
2031 | # Make sure we have a .py file |
|
1654 | # Make sure we have a .py file | |
2032 | if not fname.endswith('.ipy'): |
|
1655 | if not fname.endswith('.ipy'): | |
2033 | warn('File must end with .py to be run using execfile: <%s>' % fname) |
|
1656 | warn('File must end with .py to be run using execfile: <%s>' % fname) | |
2034 |
|
1657 | |||
2035 | # Make sure we can open the file |
|
1658 | # Make sure we can open the file | |
2036 | try: |
|
1659 | try: | |
2037 | with open(fname) as thefile: |
|
1660 | with open(fname) as thefile: | |
2038 | pass |
|
1661 | pass | |
2039 | except: |
|
1662 | except: | |
2040 | warn('Could not open file <%s> for safe execution.' % fname) |
|
1663 | warn('Could not open file <%s> for safe execution.' % fname) | |
2041 | return |
|
1664 | return | |
2042 |
|
1665 | |||
2043 | # Find things also in current directory. This is needed to mimic the |
|
1666 | # Find things also in current directory. This is needed to mimic the | |
2044 | # behavior of running a script from the system command line, where |
|
1667 | # behavior of running a script from the system command line, where | |
2045 | # Python inserts the script's directory into sys.path |
|
1668 | # Python inserts the script's directory into sys.path | |
2046 | dname = os.path.dirname(fname) |
|
1669 | dname = os.path.dirname(fname) | |
2047 |
|
1670 | |||
2048 | with prepended_to_syspath(dname): |
|
1671 | with prepended_to_syspath(dname): | |
2049 | try: |
|
1672 | try: | |
2050 | with open(fname) as thefile: |
|
1673 | with open(fname) as thefile: | |
2051 | script = thefile.read() |
|
1674 | script = thefile.read() | |
2052 | # self.runlines currently captures all exceptions |
|
1675 | # self.runlines currently captures all exceptions | |
2053 | # raise in user code. It would be nice if there were |
|
1676 | # raise in user code. It would be nice if there were | |
2054 | # versions of runlines, execfile that did raise, so |
|
1677 | # versions of runlines, execfile that did raise, so | |
2055 | # we could catch the errors. |
|
1678 | # we could catch the errors. | |
2056 | self.runlines(script, clean=True) |
|
1679 | self.runlines(script, clean=True) | |
2057 | except: |
|
1680 | except: | |
2058 | self.showtraceback() |
|
1681 | self.showtraceback() | |
2059 | warn('Unknown failure executing file: <%s>' % fname) |
|
1682 | warn('Unknown failure executing file: <%s>' % fname) | |
2060 |
|
||||
2061 | def _is_secondary_block_start(self, s): |
|
|||
2062 | if not s.endswith(':'): |
|
|||
2063 | return False |
|
|||
2064 | if (s.startswith('elif') or |
|
|||
2065 | s.startswith('else') or |
|
|||
2066 | s.startswith('except') or |
|
|||
2067 | s.startswith('finally')): |
|
|||
2068 | return True |
|
|||
2069 |
|
||||
2070 | def cleanup_ipy_script(self, script): |
|
|||
2071 | """Make a script safe for self.runlines() |
|
|||
2072 |
|
||||
2073 | Currently, IPython is lines based, with blocks being detected by |
|
|||
2074 | empty lines. This is a problem for block based scripts that may |
|
|||
2075 | not have empty lines after blocks. This script adds those empty |
|
|||
2076 | lines to make scripts safe for running in the current line based |
|
|||
2077 | IPython. |
|
|||
2078 | """ |
|
|||
2079 | res = [] |
|
|||
2080 | lines = script.splitlines() |
|
|||
2081 | level = 0 |
|
|||
2082 |
|
||||
2083 | for l in lines: |
|
|||
2084 | lstripped = l.lstrip() |
|
|||
2085 | stripped = l.strip() |
|
|||
2086 | if not stripped: |
|
|||
2087 | continue |
|
|||
2088 | newlevel = len(l) - len(lstripped) |
|
|||
2089 | if level > 0 and newlevel == 0 and \ |
|
|||
2090 | not self._is_secondary_block_start(stripped): |
|
|||
2091 | # add empty line |
|
|||
2092 | res.append('') |
|
|||
2093 | res.append(l) |
|
|||
2094 | level = newlevel |
|
|||
2095 |
|
||||
2096 | return '\n'.join(res) + '\n' |
|
|||
2097 |
|
1683 | |||
2098 | def runlines(self, lines, clean=False): |
|
1684 | def runlines(self, lines, clean=False): | |
2099 | """Run a string of one or more lines of source. |
|
1685 | """Run a string of one or more lines of source. | |
2100 |
|
1686 | |||
2101 | This method is capable of running a string containing multiple source |
|
1687 | This method is capable of running a string containing multiple source | |
2102 | lines, as if they had been entered at the IPython prompt. Since it |
|
1688 | lines, as if they had been entered at the IPython prompt. Since it | |
2103 | exposes IPython's processing machinery, the given strings can contain |
|
1689 | exposes IPython's processing machinery, the given strings can contain | |
2104 | magic calls (%magic), special shell access (!cmd), etc. |
|
1690 | magic calls (%magic), special shell access (!cmd), etc. | |
2105 | """ |
|
1691 | """ | |
2106 |
|
1692 | |||
2107 | if isinstance(lines, (list, tuple)): |
|
1693 | if isinstance(lines, (list, tuple)): | |
2108 | lines = '\n'.join(lines) |
|
1694 | lines = '\n'.join(lines) | |
2109 |
|
1695 | |||
2110 | if clean: |
|
1696 | if clean: | |
2111 | lines = self.cleanup_ipy_script(lines) |
|
1697 | lines = self._cleanup_ipy_script(lines) | |
2112 |
|
1698 | |||
2113 | # We must start with a clean buffer, in case this is run from an |
|
1699 | # We must start with a clean buffer, in case this is run from an | |
2114 | # interactive IPython session (via a magic, for example). |
|
1700 | # interactive IPython session (via a magic, for example). | |
2115 | self.resetbuffer() |
|
1701 | self.resetbuffer() | |
2116 | lines = lines.splitlines() |
|
1702 | lines = lines.splitlines() | |
2117 | more = 0 |
|
1703 | more = 0 | |
2118 |
|
1704 | |||
2119 | with nested(self.builtin_trap, self.display_trap): |
|
1705 | with nested(self.builtin_trap, self.display_trap): | |
2120 | for line in lines: |
|
1706 | for line in lines: | |
2121 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1707 | # skip blank lines so we don't mess up the prompt counter, but do | |
2122 | # NOT skip even a blank line if we are in a code block (more is |
|
1708 | # NOT skip even a blank line if we are in a code block (more is | |
2123 | # true) |
|
1709 | # true) | |
2124 |
|
1710 | |||
2125 | if line or more: |
|
1711 | if line or more: | |
2126 | # push to raw history, so hist line numbers stay in sync |
|
1712 | # push to raw history, so hist line numbers stay in sync | |
2127 | self.input_hist_raw.append("# " + line + "\n") |
|
1713 | self.input_hist_raw.append("# " + line + "\n") | |
2128 | prefiltered = self.prefilter_manager.prefilter_lines(line,more) |
|
1714 | prefiltered = self.prefilter_manager.prefilter_lines(line,more) | |
2129 | more = self.push_line(prefiltered) |
|
1715 | more = self.push_line(prefiltered) | |
2130 | # IPython's runsource returns None if there was an error |
|
1716 | # IPython's runsource returns None if there was an error | |
2131 | # compiling the code. This allows us to stop processing right |
|
1717 | # compiling the code. This allows us to stop processing right | |
2132 | # away, so the user gets the error message at the right place. |
|
1718 | # away, so the user gets the error message at the right place. | |
2133 | if more is None: |
|
1719 | if more is None: | |
2134 | break |
|
1720 | break | |
2135 | else: |
|
1721 | else: | |
2136 | self.input_hist_raw.append("\n") |
|
1722 | self.input_hist_raw.append("\n") | |
2137 | # final newline in case the input didn't have it, so that the code |
|
1723 | # final newline in case the input didn't have it, so that the code | |
2138 | # actually does get executed |
|
1724 | # actually does get executed | |
2139 | if more: |
|
1725 | if more: | |
2140 | self.push_line('\n') |
|
1726 | self.push_line('\n') | |
2141 |
|
1727 | |||
2142 | def runsource(self, source, filename='<input>', symbol='single'): |
|
1728 | def runsource(self, source, filename='<input>', symbol='single'): | |
2143 | """Compile and run some source in the interpreter. |
|
1729 | """Compile and run some source in the interpreter. | |
2144 |
|
1730 | |||
2145 | Arguments are as for compile_command(). |
|
1731 | Arguments are as for compile_command(). | |
2146 |
|
1732 | |||
2147 | One several things can happen: |
|
1733 | One several things can happen: | |
2148 |
|
1734 | |||
2149 | 1) The input is incorrect; compile_command() raised an |
|
1735 | 1) The input is incorrect; compile_command() raised an | |
2150 | exception (SyntaxError or OverflowError). A syntax traceback |
|
1736 | exception (SyntaxError or OverflowError). A syntax traceback | |
2151 | will be printed by calling the showsyntaxerror() method. |
|
1737 | will be printed by calling the showsyntaxerror() method. | |
2152 |
|
1738 | |||
2153 | 2) The input is incomplete, and more input is required; |
|
1739 | 2) The input is incomplete, and more input is required; | |
2154 | compile_command() returned None. Nothing happens. |
|
1740 | compile_command() returned None. Nothing happens. | |
2155 |
|
1741 | |||
2156 | 3) The input is complete; compile_command() returned a code |
|
1742 | 3) The input is complete; compile_command() returned a code | |
2157 | object. The code is executed by calling self.runcode() (which |
|
1743 | object. The code is executed by calling self.runcode() (which | |
2158 | also handles run-time exceptions, except for SystemExit). |
|
1744 | also handles run-time exceptions, except for SystemExit). | |
2159 |
|
1745 | |||
2160 | The return value is: |
|
1746 | The return value is: | |
2161 |
|
1747 | |||
2162 | - True in case 2 |
|
1748 | - True in case 2 | |
2163 |
|
1749 | |||
2164 | - False in the other cases, unless an exception is raised, where |
|
1750 | - False in the other cases, unless an exception is raised, where | |
2165 | None is returned instead. This can be used by external callers to |
|
1751 | None is returned instead. This can be used by external callers to | |
2166 | know whether to continue feeding input or not. |
|
1752 | know whether to continue feeding input or not. | |
2167 |
|
1753 | |||
2168 | The return value can be used to decide whether to use sys.ps1 or |
|
1754 | The return value can be used to decide whether to use sys.ps1 or | |
2169 | sys.ps2 to prompt the next line.""" |
|
1755 | sys.ps2 to prompt the next line.""" | |
2170 |
|
1756 | |||
2171 | # if the source code has leading blanks, add 'if 1:\n' to it |
|
1757 | # if the source code has leading blanks, add 'if 1:\n' to it | |
2172 | # this allows execution of indented pasted code. It is tempting |
|
1758 | # this allows execution of indented pasted code. It is tempting | |
2173 | # to add '\n' at the end of source to run commands like ' a=1' |
|
1759 | # to add '\n' at the end of source to run commands like ' a=1' | |
2174 | # directly, but this fails for more complicated scenarios |
|
1760 | # directly, but this fails for more complicated scenarios | |
2175 | source=source.encode(self.stdin_encoding) |
|
1761 | source=source.encode(self.stdin_encoding) | |
2176 | if source[:1] in [' ', '\t']: |
|
1762 | if source[:1] in [' ', '\t']: | |
2177 | source = 'if 1:\n%s' % source |
|
1763 | source = 'if 1:\n%s' % source | |
2178 |
|
1764 | |||
2179 | try: |
|
1765 | try: | |
2180 | code = self.compile(source,filename,symbol) |
|
1766 | code = self.compile(source,filename,symbol) | |
2181 | except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): |
|
1767 | except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): | |
2182 | # Case 1 |
|
1768 | # Case 1 | |
2183 | self.showsyntaxerror(filename) |
|
1769 | self.showsyntaxerror(filename) | |
2184 | return None |
|
1770 | return None | |
2185 |
|
1771 | |||
2186 | if code is None: |
|
1772 | if code is None: | |
2187 | # Case 2 |
|
1773 | # Case 2 | |
2188 | return True |
|
1774 | return True | |
2189 |
|
1775 | |||
2190 | # Case 3 |
|
1776 | # Case 3 | |
2191 | # We store the code object so that threaded shells and |
|
1777 | # We store the code object so that threaded shells and | |
2192 | # custom exception handlers can access all this info if needed. |
|
1778 | # custom exception handlers can access all this info if needed. | |
2193 | # The source corresponding to this can be obtained from the |
|
1779 | # The source corresponding to this can be obtained from the | |
2194 | # buffer attribute as '\n'.join(self.buffer). |
|
1780 | # buffer attribute as '\n'.join(self.buffer). | |
2195 | self.code_to_run = code |
|
1781 | self.code_to_run = code | |
2196 | # now actually execute the code object |
|
1782 | # now actually execute the code object | |
2197 | if self.runcode(code) == 0: |
|
1783 | if self.runcode(code) == 0: | |
2198 | return False |
|
1784 | return False | |
2199 | else: |
|
1785 | else: | |
2200 | return None |
|
1786 | return None | |
2201 |
|
1787 | |||
2202 | def runcode(self,code_obj): |
|
1788 | def runcode(self,code_obj): | |
2203 | """Execute a code object. |
|
1789 | """Execute a code object. | |
2204 |
|
1790 | |||
2205 | When an exception occurs, self.showtraceback() is called to display a |
|
1791 | When an exception occurs, self.showtraceback() is called to display a | |
2206 | traceback. |
|
1792 | traceback. | |
2207 |
|
1793 | |||
2208 | Return value: a flag indicating whether the code to be run completed |
|
1794 | Return value: a flag indicating whether the code to be run completed | |
2209 | successfully: |
|
1795 | successfully: | |
2210 |
|
1796 | |||
2211 | - 0: successful execution. |
|
1797 | - 0: successful execution. | |
2212 | - 1: an error occurred. |
|
1798 | - 1: an error occurred. | |
2213 | """ |
|
1799 | """ | |
2214 |
|
1800 | |||
2215 | # Set our own excepthook in case the user code tries to call it |
|
1801 | # Set our own excepthook in case the user code tries to call it | |
2216 | # directly, so that the IPython crash handler doesn't get triggered |
|
1802 | # directly, so that the IPython crash handler doesn't get triggered | |
2217 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
1803 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook | |
2218 |
|
1804 | |||
2219 | # we save the original sys.excepthook in the instance, in case config |
|
1805 | # we save the original sys.excepthook in the instance, in case config | |
2220 | # code (such as magics) needs access to it. |
|
1806 | # code (such as magics) needs access to it. | |
2221 | self.sys_excepthook = old_excepthook |
|
1807 | self.sys_excepthook = old_excepthook | |
2222 | outflag = 1 # happens in more places, so it's easier as default |
|
1808 | outflag = 1 # happens in more places, so it's easier as default | |
2223 | try: |
|
1809 | try: | |
2224 | try: |
|
1810 | try: | |
2225 | self.hooks.pre_runcode_hook() |
|
1811 | self.hooks.pre_runcode_hook() | |
2226 | exec code_obj in self.user_global_ns, self.user_ns |
|
1812 | exec code_obj in self.user_global_ns, self.user_ns | |
2227 | finally: |
|
1813 | finally: | |
2228 | # Reset our crash handler in place |
|
1814 | # Reset our crash handler in place | |
2229 | sys.excepthook = old_excepthook |
|
1815 | sys.excepthook = old_excepthook | |
2230 | except SystemExit: |
|
1816 | except SystemExit: | |
2231 | self.resetbuffer() |
|
1817 | self.resetbuffer() | |
2232 | self.showtraceback(exception_only=True) |
|
1818 | self.showtraceback(exception_only=True) | |
2233 | warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1) |
|
1819 | warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1) | |
2234 | except self.custom_exceptions: |
|
1820 | except self.custom_exceptions: | |
2235 | etype,value,tb = sys.exc_info() |
|
1821 | etype,value,tb = sys.exc_info() | |
2236 | self.CustomTB(etype,value,tb) |
|
1822 | self.CustomTB(etype,value,tb) | |
2237 | except: |
|
1823 | except: | |
2238 | self.showtraceback() |
|
1824 | self.showtraceback() | |
2239 | else: |
|
1825 | else: | |
2240 | outflag = 0 |
|
1826 | outflag = 0 | |
2241 | if softspace(sys.stdout, 0): |
|
1827 | if softspace(sys.stdout, 0): | |
2242 |
|
1828 | |||
2243 | # Flush out code object which has been run (and source) |
|
1829 | # Flush out code object which has been run (and source) | |
2244 | self.code_to_run = None |
|
1830 | self.code_to_run = None | |
2245 | return outflag |
|
1831 | return outflag | |
2246 |
|
1832 | |||
2247 | def push_line(self, line): |
|
1833 | def push_line(self, line): | |
2248 | """Push a line to the interpreter. |
|
1834 | """Push a line to the interpreter. | |
2249 |
|
1835 | |||
2250 | The line should not have a trailing newline; it may have |
|
1836 | The line should not have a trailing newline; it may have | |
2251 | internal newlines. The line is appended to a buffer and the |
|
1837 | internal newlines. The line is appended to a buffer and the | |
2252 | interpreter's runsource() method is called with the |
|
1838 | interpreter's runsource() method is called with the | |
2253 | concatenated contents of the buffer as source. If this |
|
1839 | concatenated contents of the buffer as source. If this | |
2254 | indicates that the command was executed or invalid, the buffer |
|
1840 | indicates that the command was executed or invalid, the buffer | |
2255 | is reset; otherwise, the command is incomplete, and the buffer |
|
1841 | is reset; otherwise, the command is incomplete, and the buffer | |
2256 | is left as it was after the line was appended. The return |
|
1842 | is left as it was after the line was appended. The return | |
2257 | value is 1 if more input is required, 0 if the line was dealt |
|
1843 | value is 1 if more input is required, 0 if the line was dealt | |
2258 | with in some way (this is the same as runsource()). |
|
1844 | with in some way (this is the same as runsource()). | |
2259 | """ |
|
1845 | """ | |
2260 |
|
1846 | |||
2261 | # autoindent management should be done here, and not in the |
|
1847 | # autoindent management should be done here, and not in the | |
2262 | # interactive loop, since that one is only seen by keyboard input. We |
|
1848 | # interactive loop, since that one is only seen by keyboard input. We | |
2263 | # need this done correctly even for code run via runlines (which uses |
|
1849 | # need this done correctly even for code run via runlines (which uses | |
2264 | # push). |
|
1850 | # push). | |
2265 |
|
1851 | |||
2266 | #print 'push line: <%s>' % line # dbg |
|
1852 | #print 'push line: <%s>' % line # dbg | |
2267 | for subline in line.splitlines(): |
|
1853 | for subline in line.splitlines(): | |
2268 | self._autoindent_update(subline) |
|
1854 | self._autoindent_update(subline) | |
2269 | self.buffer.append(line) |
|
1855 | self.buffer.append(line) | |
2270 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
1856 | more = self.runsource('\n'.join(self.buffer), self.filename) | |
2271 | if not more: |
|
1857 | if not more: | |
2272 | self.resetbuffer() |
|
1858 | self.resetbuffer() | |
2273 | return more |
|
1859 | return more | |
2274 |
|
1860 | |||
|
1861 | def resetbuffer(self): | |||
|
1862 | """Reset the input buffer.""" | |||
|
1863 | self.buffer[:] = [] | |||
|
1864 | ||||
|
1865 | def _is_secondary_block_start(self, s): | |||
|
1866 | if not s.endswith(':'): | |||
|
1867 | return False | |||
|
1868 | if (s.startswith('elif') or | |||
|
1869 | s.startswith('else') or | |||
|
1870 | s.startswith('except') or | |||
|
1871 | s.startswith('finally')): | |||
|
1872 | return True | |||
|
1873 | ||||
|
1874 | def _cleanup_ipy_script(self, script): | |||
|
1875 | """Make a script safe for self.runlines() | |||
|
1876 | ||||
|
1877 | Currently, IPython is lines based, with blocks being detected by | |||
|
1878 | empty lines. This is a problem for block based scripts that may | |||
|
1879 | not have empty lines after blocks. This script adds those empty | |||
|
1880 | lines to make scripts safe for running in the current line based | |||
|
1881 | IPython. | |||
|
1882 | """ | |||
|
1883 | res = [] | |||
|
1884 | lines = script.splitlines() | |||
|
1885 | level = 0 | |||
|
1886 | ||||
|
1887 | for l in lines: | |||
|
1888 | lstripped = l.lstrip() | |||
|
1889 | stripped = l.strip() | |||
|
1890 | if not stripped: | |||
|
1891 | continue | |||
|
1892 | newlevel = len(l) - len(lstripped) | |||
|
1893 | if level > 0 and newlevel == 0 and \ | |||
|
1894 | not self._is_secondary_block_start(stripped): | |||
|
1895 | # add empty line | |||
|
1896 | res.append('') | |||
|
1897 | res.append(l) | |||
|
1898 | level = newlevel | |||
|
1899 | ||||
|
1900 | return '\n'.join(res) + '\n' | |||
|
1901 | ||||
2275 | def _autoindent_update(self,line): |
|
1902 | def _autoindent_update(self,line): | |
2276 | """Keep track of the indent level.""" |
|
1903 | """Keep track of the indent level.""" | |
2277 |
|
1904 | |||
2278 | #debugx('line') |
|
1905 | #debugx('line') | |
2279 | #debugx('self.indent_current_nsp') |
|
1906 | #debugx('self.indent_current_nsp') | |
2280 | if self.autoindent: |
|
1907 | if self.autoindent: | |
2281 | if line: |
|
1908 | if line: | |
2282 | inisp = num_ini_spaces(line) |
|
1909 | inisp = num_ini_spaces(line) | |
2283 | if inisp < self.indent_current_nsp: |
|
1910 | if inisp < self.indent_current_nsp: | |
2284 | self.indent_current_nsp = inisp |
|
1911 | self.indent_current_nsp = inisp | |
2285 |
|
1912 | |||
2286 | if line[-1] == ':': |
|
1913 | if line[-1] == ':': | |
2287 | self.indent_current_nsp += 4 |
|
1914 | self.indent_current_nsp += 4 | |
2288 | elif dedent_re.match(line): |
|
1915 | elif dedent_re.match(line): | |
2289 | self.indent_current_nsp -= 4 |
|
1916 | self.indent_current_nsp -= 4 | |
2290 | else: |
|
1917 | else: | |
2291 | self.indent_current_nsp = 0 |
|
1918 | self.indent_current_nsp = 0 | |
2292 |
|
1919 | |||
2293 | def resetbuffer(self): |
|
|||
2294 | """Reset the input buffer.""" |
|
|||
2295 | self.buffer[:] = [] |
|
|||
2296 |
|
||||
2297 | def raw_input(self,prompt='',continue_prompt=False): |
|
|||
2298 | """Write a prompt and read a line. |
|
|||
2299 |
|
||||
2300 | The returned line does not include the trailing newline. |
|
|||
2301 | When the user enters the EOF key sequence, EOFError is raised. |
|
|||
2302 |
|
||||
2303 | Optional inputs: |
|
|||
2304 |
|
||||
2305 | - prompt(''): a string to be printed to prompt the user. |
|
|||
2306 |
|
||||
2307 | - continue_prompt(False): whether this line is the first one or a |
|
|||
2308 | continuation in a sequence of inputs. |
|
|||
2309 | """ |
|
|||
2310 | # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt)) |
|
|||
2311 |
|
||||
2312 | # Code run by the user may have modified the readline completer state. |
|
|||
2313 | # We must ensure that our completer is back in place. |
|
|||
2314 |
|
||||
2315 | if self.has_readline: |
|
|||
2316 | self.set_completer() |
|
|||
2317 |
|
||||
2318 | try: |
|
|||
2319 | line = raw_input_original(prompt).decode(self.stdin_encoding) |
|
|||
2320 | except ValueError: |
|
|||
2321 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" |
|
|||
2322 | " or sys.stdout.close()!\nExiting IPython!") |
|
|||
2323 | self.ask_exit() |
|
|||
2324 | return "" |
|
|||
2325 |
|
||||
2326 | # Try to be reasonably smart about not re-indenting pasted input more |
|
|||
2327 | # than necessary. We do this by trimming out the auto-indent initial |
|
|||
2328 | # spaces, if the user's actual input started itself with whitespace. |
|
|||
2329 | #debugx('self.buffer[-1]') |
|
|||
2330 |
|
||||
2331 | if self.autoindent: |
|
|||
2332 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
|||
2333 | line = line[self.indent_current_nsp:] |
|
|||
2334 | self.indent_current_nsp = 0 |
|
|||
2335 |
|
||||
2336 | # store the unfiltered input before the user has any chance to modify |
|
|||
2337 | # it. |
|
|||
2338 | if line.strip(): |
|
|||
2339 | if continue_prompt: |
|
|||
2340 | self.input_hist_raw[-1] += '%s\n' % line |
|
|||
2341 | if self.has_readline and self.readline_use: |
|
|||
2342 | try: |
|
|||
2343 | histlen = self.readline.get_current_history_length() |
|
|||
2344 | if histlen > 1: |
|
|||
2345 | newhist = self.input_hist_raw[-1].rstrip() |
|
|||
2346 | self.readline.remove_history_item(histlen-1) |
|
|||
2347 | self.readline.replace_history_item(histlen-2, |
|
|||
2348 | newhist.encode(self.stdin_encoding)) |
|
|||
2349 | except AttributeError: |
|
|||
2350 | pass # re{move,place}_history_item are new in 2.4. |
|
|||
2351 | else: |
|
|||
2352 | self.input_hist_raw.append('%s\n' % line) |
|
|||
2353 | # only entries starting at first column go to shadow history |
|
|||
2354 | if line.lstrip() == line: |
|
|||
2355 | self.shadowhist.add(line.strip()) |
|
|||
2356 | elif not continue_prompt: |
|
|||
2357 | self.input_hist_raw.append('\n') |
|
|||
2358 | try: |
|
|||
2359 | lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt) |
|
|||
2360 | except: |
|
|||
2361 | # blanket except, in case a user-defined prefilter crashes, so it |
|
|||
2362 | # can't take all of ipython with it. |
|
|||
2363 | self.showtraceback() |
|
|||
2364 | return '' |
|
|||
2365 | else: |
|
|||
2366 | return lineout |
|
|||
2367 |
|
||||
2368 | #------------------------------------------------------------------------- |
|
1920 | #------------------------------------------------------------------------- | |
2369 |
# Things related to |
|
1921 | # Things related to GUI support and pylab | |
2370 | #------------------------------------------------------------------------- |
|
1922 | #------------------------------------------------------------------------- | |
2371 |
|
1923 | |||
2372 | def init_prefilter(self): |
|
1924 | def enable_pylab(self, gui=None): | |
2373 | self.prefilter_manager = PrefilterManager(shell=self, config=self.config) |
|
1925 | raise NotImplementedError('Implement enable_pylab in a subclass') | |
2374 | # Ultimately this will be refactored in the new interpreter code, but |
|
|||
2375 | # for now, we should expose the main prefilter method (there's legacy |
|
|||
2376 | # code out there that may rely on this). |
|
|||
2377 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
|||
2378 |
|
1926 | |||
2379 | #------------------------------------------------------------------------- |
|
1927 | #------------------------------------------------------------------------- | |
2380 | # Utilities |
|
1928 | # Utilities | |
2381 | #------------------------------------------------------------------------- |
|
1929 | #------------------------------------------------------------------------- | |
2382 |
|
1930 | |||
2383 | def getoutput(self, cmd): |
|
1931 | def getoutput(self, cmd): | |
2384 | return getoutput(self.var_expand(cmd,depth=2), |
|
1932 | return getoutput(self.var_expand(cmd,depth=2), | |
2385 | header=self.system_header, |
|
1933 | header=self.system_header, | |
2386 | verbose=self.system_verbose) |
|
1934 | verbose=self.system_verbose) | |
2387 |
|
1935 | |||
2388 | def getoutputerror(self, cmd): |
|
1936 | def getoutputerror(self, cmd): | |
2389 | return getoutputerror(self.var_expand(cmd,depth=2), |
|
1937 | return getoutputerror(self.var_expand(cmd,depth=2), | |
2390 | header=self.system_header, |
|
1938 | header=self.system_header, | |
2391 | verbose=self.system_verbose) |
|
1939 | verbose=self.system_verbose) | |
2392 |
|
1940 | |||
2393 | def var_expand(self,cmd,depth=0): |
|
1941 | def var_expand(self,cmd,depth=0): | |
2394 | """Expand python variables in a string. |
|
1942 | """Expand python variables in a string. | |
2395 |
|
1943 | |||
2396 | The depth argument indicates how many frames above the caller should |
|
1944 | The depth argument indicates how many frames above the caller should | |
2397 | be walked to look for the local namespace where to expand variables. |
|
1945 | be walked to look for the local namespace where to expand variables. | |
2398 |
|
1946 | |||
2399 | The global namespace for expansion is always the user's interactive |
|
1947 | The global namespace for expansion is always the user's interactive | |
2400 | namespace. |
|
1948 | namespace. | |
2401 | """ |
|
1949 | """ | |
2402 |
|
1950 | |||
2403 | return str(ItplNS(cmd, |
|
1951 | return str(ItplNS(cmd, | |
2404 | self.user_ns, # globals |
|
1952 | self.user_ns, # globals | |
2405 | # Skip our own frame in searching for locals: |
|
1953 | # Skip our own frame in searching for locals: | |
2406 | sys._getframe(depth+1).f_locals # locals |
|
1954 | sys._getframe(depth+1).f_locals # locals | |
2407 | )) |
|
1955 | )) | |
2408 |
|
1956 | |||
2409 | def mktempfile(self,data=None): |
|
1957 | def mktempfile(self,data=None): | |
2410 | """Make a new tempfile and return its filename. |
|
1958 | """Make a new tempfile and return its filename. | |
2411 |
|
1959 | |||
2412 | This makes a call to tempfile.mktemp, but it registers the created |
|
1960 | This makes a call to tempfile.mktemp, but it registers the created | |
2413 | filename internally so ipython cleans it up at exit time. |
|
1961 | filename internally so ipython cleans it up at exit time. | |
2414 |
|
1962 | |||
2415 | Optional inputs: |
|
1963 | Optional inputs: | |
2416 |
|
1964 | |||
2417 | - data(None): if data is given, it gets written out to the temp file |
|
1965 | - data(None): if data is given, it gets written out to the temp file | |
2418 | immediately, and the file is closed again.""" |
|
1966 | immediately, and the file is closed again.""" | |
2419 |
|
1967 | |||
2420 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
1968 | filename = tempfile.mktemp('.py','ipython_edit_') | |
2421 | self.tempfiles.append(filename) |
|
1969 | self.tempfiles.append(filename) | |
2422 |
|
1970 | |||
2423 | if data: |
|
1971 | if data: | |
2424 | tmp_file = open(filename,'w') |
|
1972 | tmp_file = open(filename,'w') | |
2425 | tmp_file.write(data) |
|
1973 | tmp_file.write(data) | |
2426 | tmp_file.close() |
|
1974 | tmp_file.close() | |
2427 | return filename |
|
1975 | return filename | |
2428 |
|
1976 | |||
|
1977 | # TODO: This should be removed when Term is refactored. | |||
2429 | def write(self,data): |
|
1978 | def write(self,data): | |
2430 | """Write a string to the default output""" |
|
1979 | """Write a string to the default output""" | |
2431 | Term.cout.write(data) |
|
1980 | Term.cout.write(data) | |
2432 |
|
1981 | |||
|
1982 | # TODO: This should be removed when Term is refactored. | |||
2433 | def write_err(self,data): |
|
1983 | def write_err(self,data): | |
2434 | """Write a string to the default error output""" |
|
1984 | """Write a string to the default error output""" | |
2435 | Term.cerr.write(data) |
|
1985 | Term.cerr.write(data) | |
2436 |
|
1986 | |||
2437 | def ask_yes_no(self,prompt,default=True): |
|
1987 | def ask_yes_no(self,prompt,default=True): | |
2438 | if self.quiet: |
|
1988 | if self.quiet: | |
2439 | return True |
|
1989 | return True | |
2440 | return ask_yes_no(prompt,default) |
|
1990 | return ask_yes_no(prompt,default) | |
2441 |
|
1991 | |||
2442 | #------------------------------------------------------------------------- |
|
1992 | #------------------------------------------------------------------------- | |
2443 | # Things related to GUI support and pylab |
|
|||
2444 | #------------------------------------------------------------------------- |
|
|||
2445 |
|
||||
2446 | def enable_pylab(self, gui=None): |
|
|||
2447 | """Activate pylab support at runtime. |
|
|||
2448 |
|
||||
2449 | This turns on support for matplotlib, preloads into the interactive |
|
|||
2450 | namespace all of numpy and pylab, and configures IPython to correcdtly |
|
|||
2451 | interact with the GUI event loop. The GUI backend to be used can be |
|
|||
2452 | optionally selected with the optional :param:`gui` argument. |
|
|||
2453 |
|
||||
2454 | Parameters |
|
|||
2455 | ---------- |
|
|||
2456 | gui : optional, string |
|
|||
2457 |
|
||||
2458 | If given, dictates the choice of matplotlib GUI backend to use |
|
|||
2459 | (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or |
|
|||
2460 | 'gtk'), otherwise we use the default chosen by matplotlib (as |
|
|||
2461 | dictated by the matplotlib build-time options plus the user's |
|
|||
2462 | matplotlibrc configuration file). |
|
|||
2463 | """ |
|
|||
2464 | # We want to prevent the loading of pylab to pollute the user's |
|
|||
2465 | # namespace as shown by the %who* magics, so we execute the activation |
|
|||
2466 | # code in an empty namespace, and we update *both* user_ns and |
|
|||
2467 | # user_ns_hidden with this information. |
|
|||
2468 | ns = {} |
|
|||
2469 | gui = pylab_activate(ns, gui) |
|
|||
2470 | self.user_ns.update(ns) |
|
|||
2471 | self.user_ns_hidden.update(ns) |
|
|||
2472 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
|||
2473 | # plot updates into account |
|
|||
2474 | enable_gui(gui) |
|
|||
2475 | self.magic_run = self._pylab_magic_run |
|
|||
2476 |
|
||||
2477 | #------------------------------------------------------------------------- |
|
|||
2478 | # Things related to IPython exiting |
|
1993 | # Things related to IPython exiting | |
2479 | #------------------------------------------------------------------------- |
|
1994 | #------------------------------------------------------------------------- | |
2480 |
|
1995 | |||
2481 | def ask_exit(self): |
|
|||
2482 | """ Ask the shell to exit. Can be overiden and used as a callback. """ |
|
|||
2483 | self.exit_now = True |
|
|||
2484 |
|
||||
2485 | def exit(self): |
|
|||
2486 | """Handle interactive exit. |
|
|||
2487 |
|
||||
2488 | This method calls the ask_exit callback.""" |
|
|||
2489 | if self.confirm_exit: |
|
|||
2490 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
|||
2491 | self.ask_exit() |
|
|||
2492 | else: |
|
|||
2493 | self.ask_exit() |
|
|||
2494 |
|
||||
2495 | def atexit_operations(self): |
|
1996 | def atexit_operations(self): | |
2496 | """This will be executed at the time of exit. |
|
1997 | """This will be executed at the time of exit. | |
2497 |
|
1998 | |||
2498 | Saving of persistent data should be performed here. |
|
1999 | Saving of persistent data should be performed here. | |
2499 | """ |
|
2000 | """ | |
2500 | self.savehist() |
|
2001 | self.savehist() | |
2501 |
|
2002 | |||
2502 | # Cleanup all tempfiles left around |
|
2003 | # Cleanup all tempfiles left around | |
2503 | for tfile in self.tempfiles: |
|
2004 | for tfile in self.tempfiles: | |
2504 | try: |
|
2005 | try: | |
2505 | os.unlink(tfile) |
|
2006 | os.unlink(tfile) | |
2506 | except OSError: |
|
2007 | except OSError: | |
2507 | pass |
|
2008 | pass | |
2508 |
|
2009 | |||
2509 | # Clear all user namespaces to release all references cleanly. |
|
2010 | # Clear all user namespaces to release all references cleanly. | |
2510 | self.reset() |
|
2011 | self.reset() | |
2511 |
|
2012 | |||
2512 | # Run user hooks |
|
2013 | # Run user hooks | |
2513 | self.hooks.shutdown_hook() |
|
2014 | self.hooks.shutdown_hook() | |
2514 |
|
2015 | |||
2515 | def cleanup(self): |
|
2016 | def cleanup(self): | |
2516 | self.restore_sys_module_state() |
|
2017 | self.restore_sys_module_state() | |
2517 |
|
2018 | |||
2518 |
|
2019 | |||
2519 | class InteractiveShellABC(object): |
|
2020 | class InteractiveShellABC(object): | |
2520 | """An abstract base class for InteractiveShell.""" |
|
2021 | """An abstract base class for InteractiveShell.""" | |
2521 | __metaclass__ = abc.ABCMeta |
|
2022 | __metaclass__ = abc.ABCMeta | |
2522 |
|
2023 | |||
2523 | InteractiveShellABC.register(InteractiveShell) |
|
2024 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,30 +1,30 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | This module is *completely* deprecated and should no longer be used for |
|
4 | This module is *completely* deprecated and should no longer be used for | |
5 | any purpose. Currently, we have a few parts of the core that have |
|
5 | any purpose. Currently, we have a few parts of the core that have | |
6 | not been componentized and thus, still rely on this module. When everything |
|
6 | not been componentized and thus, still rely on this module. When everything | |
7 | has been made into a component, this module will be sent to deathrow. |
|
7 | has been made into a component, this module will be sent to deathrow. | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Copyright (C) 2008-2009 The IPython Development Team |
|
11 | # Copyright (C) 2008-2009 The IPython Development Team | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | # Distributed under the terms of the BSD License. The full license is in | |
14 | # the file COPYING, distributed as part of this software. |
|
14 | # the file COPYING, distributed as part of this software. | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | # Imports |
|
18 | # Imports | |
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 |
|
20 | |||
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | # Classes and functions |
|
22 | # Classes and functions | |
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 |
|
24 | |||
25 |
|
25 | |||
26 | def get(): |
|
26 | def get(): | |
27 | """Get the global InteractiveShell instance.""" |
|
27 | """Get the global InteractiveShell instance.""" | |
28 |
from IPython. |
|
28 | from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell | |
29 | return InteractiveShell.instance() |
|
29 | return TerminalInteractiveShell.instance() | |
30 |
|
30 |
1 | NO CONTENT: modified file |
|
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 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 |
|
3 | |||
4 | def test_import_completer(): |
|
4 | def test_import_completer(): | |
5 | from IPython.core import completer |
|
5 | from IPython.core import completer | |
6 |
|
6 | |||
7 | def test_import_crashhandler(): |
|
7 | def test_import_crashhandler(): | |
8 | from IPython.core import crashhandler |
|
8 | from IPython.core import crashhandler | |
9 |
|
9 | |||
10 | def test_import_debugger(): |
|
10 | def test_import_debugger(): | |
11 | from IPython.core import debugger |
|
11 | from IPython.core import debugger | |
12 |
|
12 | |||
13 | def test_import_fakemodule(): |
|
13 | def test_import_fakemodule(): | |
14 | from IPython.core import fakemodule |
|
14 | from IPython.core import fakemodule | |
15 |
|
15 | |||
16 | def test_import_excolors(): |
|
16 | def test_import_excolors(): | |
17 | from IPython.core import excolors |
|
17 | from IPython.core import excolors | |
18 |
|
18 | |||
19 | def test_import_history(): |
|
19 | def test_import_history(): | |
20 | from IPython.core import history |
|
20 | from IPython.core import history | |
21 |
|
21 | |||
22 | def test_import_hooks(): |
|
22 | def test_import_hooks(): | |
23 | from IPython.core import hooks |
|
23 | from IPython.core import hooks | |
24 |
|
24 | |||
25 | def test_import_ipapi(): |
|
25 | def test_import_ipapi(): | |
26 | from IPython.core import ipapi |
|
26 | from IPython.core import ipapi | |
27 |
|
27 | |||
28 | def test_import_interactiveshell(): |
|
28 | def test_import_interactiveshell(): | |
29 | from IPython.core import interactiveshell |
|
29 | from IPython.core import interactiveshell | |
30 |
|
30 | |||
31 | def test_import_logger(): |
|
31 | def test_import_logger(): | |
32 | from IPython.core import logger |
|
32 | from IPython.core import logger | |
33 |
|
33 | |||
34 | def test_import_macro(): |
|
34 | def test_import_macro(): | |
35 | from IPython.core import macro |
|
35 | from IPython.core import macro | |
36 |
|
36 | |||
37 | def test_import_magic(): |
|
37 | def test_import_magic(): | |
38 | from IPython.core import magic |
|
38 | from IPython.core import magic | |
39 |
|
39 | |||
40 | def test_import_oinspect(): |
|
40 | def test_import_oinspect(): | |
41 | from IPython.core import oinspect |
|
41 | from IPython.core import oinspect | |
42 |
|
42 | |||
43 | def test_import_outputtrap(): |
|
|||
44 | from IPython.core import outputtrap |
|
|||
45 |
|
||||
46 | def test_import_prefilter(): |
|
43 | def test_import_prefilter(): | |
47 | from IPython.core import prefilter |
|
44 | from IPython.core import prefilter | |
48 |
|
45 | |||
49 | def test_import_prompts(): |
|
46 | def test_import_prompts(): | |
50 | from IPython.core import prompts |
|
47 | from IPython.core import prompts | |
51 |
|
48 | |||
52 | def test_import_release(): |
|
49 | def test_import_release(): | |
53 | from IPython.core import release |
|
50 | from IPython.core import release | |
54 |
|
51 | |||
55 | def test_import_shadowns(): |
|
52 | def test_import_shadowns(): | |
56 | from IPython.core import shadowns |
|
53 | from IPython.core import shadowns | |
57 |
|
54 | |||
58 | def test_import_ultratb(): |
|
55 | def test_import_ultratb(): | |
59 | from IPython.core import ultratb |
|
56 | from IPython.core import ultratb | |
60 |
|
57 | |||
61 | def test_import_usage(): |
|
58 | def test_import_usage(): | |
62 | from IPython.core import usage |
|
59 | from IPython.core import usage |
1 | NO CONTENT: file renamed from IPython/core/outputtrap.py to IPython/deathrow/outputtrap.py |
|
NO CONTENT: file renamed from IPython/core/outputtrap.py to IPython/deathrow/outputtrap.py |
@@ -1,275 +1,272 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | An embedded IPython shell. |
|
4 | An embedded IPython shell. | |
5 |
|
5 | |||
6 | Authors: |
|
6 | Authors: | |
7 |
|
7 | |||
8 | * Brian Granger |
|
8 | * Brian Granger | |
9 | * Fernando Perez |
|
9 | * Fernando Perez | |
10 |
|
10 | |||
11 | Notes |
|
11 | Notes | |
12 | ----- |
|
12 | ----- | |
13 | """ |
|
13 | """ | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Copyright (C) 2008-2009 The IPython Development Team |
|
16 | # Copyright (C) 2008-2009 The IPython Development Team | |
17 | # |
|
17 | # | |
18 | # Distributed under the terms of the BSD License. The full license is in |
|
18 | # Distributed under the terms of the BSD License. The full license is in | |
19 | # the file COPYING, distributed as part of this software. |
|
19 | # the file COPYING, distributed as part of this software. | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | # Imports |
|
23 | # Imports | |
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 |
|
25 | |||
26 | from __future__ import with_statement |
|
26 | from __future__ import with_statement | |
27 | import __main__ |
|
27 | import __main__ | |
28 |
|
28 | |||
29 | import sys |
|
29 | import sys | |
30 | from contextlib import nested |
|
30 | from contextlib import nested | |
31 |
|
31 | |||
32 | from IPython.core import ultratb |
|
32 | from IPython.core import ultratb | |
33 |
from IPython. |
|
33 | from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell | |
34 | from IPython.frontend.terminal.ipapp import load_default_config |
|
34 | from IPython.frontend.terminal.ipapp import load_default_config | |
35 |
|
35 | |||
36 | from IPython.utils.traitlets import Bool, Str, CBool |
|
36 | from IPython.utils.traitlets import Bool, Str, CBool | |
37 | from IPython.utils.io import ask_yes_no |
|
37 | from IPython.utils.io import ask_yes_no | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | #----------------------------------------------------------------------------- |
|
40 | #----------------------------------------------------------------------------- | |
41 | # Classes and functions |
|
41 | # Classes and functions | |
42 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
43 |
|
43 | |||
44 | # This is an additional magic that is exposed in embedded shells. |
|
44 | # This is an additional magic that is exposed in embedded shells. | |
45 | def kill_embedded(self,parameter_s=''): |
|
45 | def kill_embedded(self,parameter_s=''): | |
46 | """%kill_embedded : deactivate for good the current embedded IPython. |
|
46 | """%kill_embedded : deactivate for good the current embedded IPython. | |
47 |
|
47 | |||
48 | This function (after asking for confirmation) sets an internal flag so that |
|
48 | This function (after asking for confirmation) sets an internal flag so that | |
49 | an embedded IPython will never activate again. This is useful to |
|
49 | an embedded IPython will never activate again. This is useful to | |
50 | permanently disable a shell that is being called inside a loop: once you've |
|
50 | permanently disable a shell that is being called inside a loop: once you've | |
51 | figured out what you needed from it, you may then kill it and the program |
|
51 | figured out what you needed from it, you may then kill it and the program | |
52 | will then continue to run without the interactive shell interfering again. |
|
52 | will then continue to run without the interactive shell interfering again. | |
53 | """ |
|
53 | """ | |
54 |
|
54 | |||
55 | kill = ask_yes_no("Are you sure you want to kill this embedded instance " |
|
55 | kill = ask_yes_no("Are you sure you want to kill this embedded instance " | |
56 | "(y/n)? [y/N] ",'n') |
|
56 | "(y/n)? [y/N] ",'n') | |
57 | if kill: |
|
57 | if kill: | |
58 | self.embedded_active = False |
|
58 | self.embedded_active = False | |
59 | print "This embedded IPython will not reactivate anymore once you exit." |
|
59 | print "This embedded IPython will not reactivate anymore once you exit." | |
60 |
|
60 | |||
61 |
|
61 | |||
62 | class InteractiveShellEmbed(InteractiveShell): |
|
62 | class InteractiveShellEmbed(TerminalInteractiveShell): | |
63 |
|
63 | |||
64 | dummy_mode = Bool(False) |
|
64 | dummy_mode = Bool(False) | |
65 | exit_msg = Str('') |
|
65 | exit_msg = Str('') | |
66 | embedded = CBool(True) |
|
66 | embedded = CBool(True) | |
67 | embedded_active = CBool(True) |
|
67 | embedded_active = CBool(True) | |
68 | # Like the base class display_banner is not configurable, but here it |
|
68 | # Like the base class display_banner is not configurable, but here it | |
69 | # is True by default. |
|
69 | # is True by default. | |
70 | display_banner = CBool(True) |
|
70 | display_banner = CBool(True) | |
71 |
|
71 | |||
72 |
def __init__(self |
|
72 | def __init__(self, config=None, ipython_dir=None, user_ns=None, | |
73 |
user_ns=None, |
|
73 | user_global_ns=None, custom_exceptions=((),None), | |
74 |
banner1=None, banner2=None, |
|
74 | usage=None, banner1=None, banner2=None, | |
75 |
|
|
75 | display_banner=None, exit_msg=u''): | |
76 |
|
76 | |||
77 | self.save_sys_ipcompleter() |
|
77 | self.save_sys_ipcompleter() | |
78 |
|
78 | |||
79 | super(InteractiveShellEmbed,self).__init__( |
|
79 | super(InteractiveShellEmbed,self).__init__( | |
80 |
|
|
80 | config=config, ipython_dir=ipython_dir, user_ns=user_ns, | |
81 |
|
|
81 | user_global_ns=user_global_ns, custom_exceptions=custom_exceptions, | |
82 |
banner1=banner1, banner2=banner2, |
|
82 | usage=usage, banner1=banner1, banner2=banner2, | |
83 | custom_exceptions=custom_exceptions) |
|
83 | display_banner=display_banner | |
|
84 | ) | |||
84 |
|
85 | |||
85 | self.exit_msg = exit_msg |
|
86 | self.exit_msg = exit_msg | |
86 | self.define_magic("kill_embedded", kill_embedded) |
|
87 | self.define_magic("kill_embedded", kill_embedded) | |
87 |
|
88 | |||
88 | # don't use the ipython crash handler so that user exceptions aren't |
|
89 | # don't use the ipython crash handler so that user exceptions aren't | |
89 | # trapped |
|
90 | # trapped | |
90 | sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors, |
|
91 | sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors, | |
91 | mode=self.xmode, |
|
92 | mode=self.xmode, | |
92 | call_pdb=self.pdb) |
|
93 | call_pdb=self.pdb) | |
93 |
|
94 | |||
94 | self.restore_sys_ipcompleter() |
|
95 | self.restore_sys_ipcompleter() | |
95 |
|
96 | |||
96 | def init_sys_modules(self): |
|
97 | def init_sys_modules(self): | |
97 | pass |
|
98 | pass | |
98 |
|
99 | |||
99 | def save_sys_ipcompleter(self): |
|
100 | def save_sys_ipcompleter(self): | |
100 | """Save readline completer status.""" |
|
101 | """Save readline completer status.""" | |
101 | try: |
|
102 | try: | |
102 | #print 'Save completer',sys.ipcompleter # dbg |
|
103 | #print 'Save completer',sys.ipcompleter # dbg | |
103 | self.sys_ipcompleter_orig = sys.ipcompleter |
|
104 | self.sys_ipcompleter_orig = sys.ipcompleter | |
104 | except: |
|
105 | except: | |
105 | pass # not nested with IPython |
|
106 | pass # not nested with IPython | |
106 |
|
107 | |||
107 | def restore_sys_ipcompleter(self): |
|
108 | def restore_sys_ipcompleter(self): | |
108 | """Restores the readline completer which was in place. |
|
109 | """Restores the readline completer which was in place. | |
109 |
|
110 | |||
110 | This allows embedded IPython within IPython not to disrupt the |
|
111 | This allows embedded IPython within IPython not to disrupt the | |
111 | parent's completion. |
|
112 | parent's completion. | |
112 | """ |
|
113 | """ | |
113 | try: |
|
114 | try: | |
114 | self.readline.set_completer(self.sys_ipcompleter_orig) |
|
115 | self.readline.set_completer(self.sys_ipcompleter_orig) | |
115 | sys.ipcompleter = self.sys_ipcompleter_orig |
|
116 | sys.ipcompleter = self.sys_ipcompleter_orig | |
116 | except: |
|
117 | except: | |
117 | pass |
|
118 | pass | |
118 |
|
119 | |||
119 | def __call__(self, header='', local_ns=None, global_ns=None, dummy=None, |
|
120 | def __call__(self, header='', local_ns=None, global_ns=None, dummy=None, | |
120 | stack_depth=1): |
|
121 | stack_depth=1): | |
121 | """Activate the interactive interpreter. |
|
122 | """Activate the interactive interpreter. | |
122 |
|
123 | |||
123 | __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start |
|
124 | __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start | |
124 | the interpreter shell with the given local and global namespaces, and |
|
125 | the interpreter shell with the given local and global namespaces, and | |
125 | optionally print a header string at startup. |
|
126 | optionally print a header string at startup. | |
126 |
|
127 | |||
127 | The shell can be globally activated/deactivated using the |
|
128 | The shell can be globally activated/deactivated using the | |
128 | set/get_dummy_mode methods. This allows you to turn off a shell used |
|
129 | set/get_dummy_mode methods. This allows you to turn off a shell used | |
129 | for debugging globally. |
|
130 | for debugging globally. | |
130 |
|
131 | |||
131 | However, *each* time you call the shell you can override the current |
|
132 | However, *each* time you call the shell you can override the current | |
132 | state of dummy_mode with the optional keyword parameter 'dummy'. For |
|
133 | state of dummy_mode with the optional keyword parameter 'dummy'. For | |
133 | example, if you set dummy mode on with IPShell.set_dummy_mode(1), you |
|
134 | example, if you set dummy mode on with IPShell.set_dummy_mode(1), you | |
134 | can still have a specific call work by making it as IPShell(dummy=0). |
|
135 | can still have a specific call work by making it as IPShell(dummy=0). | |
135 |
|
136 | |||
136 | The optional keyword parameter dummy controls whether the call |
|
137 | The optional keyword parameter dummy controls whether the call | |
137 | actually does anything. |
|
138 | actually does anything. | |
138 | """ |
|
139 | """ | |
139 |
|
140 | |||
140 | # If the user has turned it off, go away |
|
141 | # If the user has turned it off, go away | |
141 | if not self.embedded_active: |
|
142 | if not self.embedded_active: | |
142 | return |
|
143 | return | |
143 |
|
144 | |||
144 | # Normal exits from interactive mode set this flag, so the shell can't |
|
145 | # Normal exits from interactive mode set this flag, so the shell can't | |
145 | # re-enter (it checks this variable at the start of interactive mode). |
|
146 | # re-enter (it checks this variable at the start of interactive mode). | |
146 | self.exit_now = False |
|
147 | self.exit_now = False | |
147 |
|
148 | |||
148 | # Allow the dummy parameter to override the global __dummy_mode |
|
149 | # Allow the dummy parameter to override the global __dummy_mode | |
149 | if dummy or (dummy != 0 and self.dummy_mode): |
|
150 | if dummy or (dummy != 0 and self.dummy_mode): | |
150 | return |
|
151 | return | |
151 |
|
152 | |||
152 | if self.has_readline: |
|
153 | if self.has_readline: | |
153 | self.set_completer() |
|
154 | self.set_completer() | |
154 |
|
155 | |||
155 | # self.banner is auto computed |
|
156 | # self.banner is auto computed | |
156 | if header: |
|
157 | if header: | |
157 | self.old_banner2 = self.banner2 |
|
158 | self.old_banner2 = self.banner2 | |
158 | self.banner2 = self.banner2 + '\n' + header + '\n' |
|
159 | self.banner2 = self.banner2 + '\n' + header + '\n' | |
159 | else: |
|
160 | else: | |
160 | self.old_banner2 = '' |
|
161 | self.old_banner2 = '' | |
161 |
|
162 | |||
162 | # Call the embedding code with a stack depth of 1 so it can skip over |
|
163 | # Call the embedding code with a stack depth of 1 so it can skip over | |
163 | # our call and get the original caller's namespaces. |
|
164 | # our call and get the original caller's namespaces. | |
164 | self.mainloop(local_ns, global_ns, stack_depth=stack_depth) |
|
165 | self.mainloop(local_ns, global_ns, stack_depth=stack_depth) | |
165 |
|
166 | |||
166 | self.banner2 = self.old_banner2 |
|
167 | self.banner2 = self.old_banner2 | |
167 |
|
168 | |||
168 | if self.exit_msg is not None: |
|
169 | if self.exit_msg is not None: | |
169 | print self.exit_msg |
|
170 | print self.exit_msg | |
170 |
|
171 | |||
171 | self.restore_sys_ipcompleter() |
|
172 | self.restore_sys_ipcompleter() | |
172 |
|
173 | |||
173 | def mainloop(self, local_ns=None, global_ns=None, stack_depth=0, |
|
174 | def mainloop(self, local_ns=None, global_ns=None, stack_depth=0, | |
174 | display_banner=None): |
|
175 | display_banner=None): | |
175 | """Embeds IPython into a running python program. |
|
176 | """Embeds IPython into a running python program. | |
176 |
|
177 | |||
177 | Input: |
|
178 | Input: | |
178 |
|
179 | |||
179 | - header: An optional header message can be specified. |
|
180 | - header: An optional header message can be specified. | |
180 |
|
181 | |||
181 | - local_ns, global_ns: working namespaces. If given as None, the |
|
182 | - local_ns, global_ns: working namespaces. If given as None, the | |
182 | IPython-initialized one is updated with __main__.__dict__, so that |
|
183 | IPython-initialized one is updated with __main__.__dict__, so that | |
183 | program variables become visible but user-specific configuration |
|
184 | program variables become visible but user-specific configuration | |
184 | remains possible. |
|
185 | remains possible. | |
185 |
|
186 | |||
186 | - stack_depth: specifies how many levels in the stack to go to |
|
187 | - stack_depth: specifies how many levels in the stack to go to | |
187 | looking for namespaces (when local_ns and global_ns are None). This |
|
188 | looking for namespaces (when local_ns and global_ns are None). This | |
188 | allows an intermediate caller to make sure that this function gets |
|
189 | allows an intermediate caller to make sure that this function gets | |
189 | the namespace from the intended level in the stack. By default (0) |
|
190 | the namespace from the intended level in the stack. By default (0) | |
190 | it will get its locals and globals from the immediate caller. |
|
191 | it will get its locals and globals from the immediate caller. | |
191 |
|
192 | |||
192 | Warning: it's possible to use this in a program which is being run by |
|
193 | Warning: it's possible to use this in a program which is being run by | |
193 | IPython itself (via %run), but some funny things will happen (a few |
|
194 | IPython itself (via %run), but some funny things will happen (a few | |
194 | globals get overwritten). In the future this will be cleaned up, as |
|
195 | globals get overwritten). In the future this will be cleaned up, as | |
195 | there is no fundamental reason why it can't work perfectly.""" |
|
196 | there is no fundamental reason why it can't work perfectly.""" | |
196 |
|
197 | |||
197 | # Get locals and globals from caller |
|
198 | # Get locals and globals from caller | |
198 | if local_ns is None or global_ns is None: |
|
199 | if local_ns is None or global_ns is None: | |
199 | call_frame = sys._getframe(stack_depth).f_back |
|
200 | call_frame = sys._getframe(stack_depth).f_back | |
200 |
|
201 | |||
201 | if local_ns is None: |
|
202 | if local_ns is None: | |
202 | local_ns = call_frame.f_locals |
|
203 | local_ns = call_frame.f_locals | |
203 | if global_ns is None: |
|
204 | if global_ns is None: | |
204 | global_ns = call_frame.f_globals |
|
205 | global_ns = call_frame.f_globals | |
205 |
|
206 | |||
206 | # Update namespaces and fire up interpreter |
|
207 | # Update namespaces and fire up interpreter | |
207 |
|
208 | |||
208 | # The global one is easy, we can just throw it in |
|
209 | # The global one is easy, we can just throw it in | |
209 | self.user_global_ns = global_ns |
|
210 | self.user_global_ns = global_ns | |
210 |
|
211 | |||
211 | # but the user/local one is tricky: ipython needs it to store internal |
|
212 | # but the user/local one is tricky: ipython needs it to store internal | |
212 | # data, but we also need the locals. We'll copy locals in the user |
|
213 | # data, but we also need the locals. We'll copy locals in the user | |
213 | # one, but will track what got copied so we can delete them at exit. |
|
214 | # one, but will track what got copied so we can delete them at exit. | |
214 | # This is so that a later embedded call doesn't see locals from a |
|
215 | # This is so that a later embedded call doesn't see locals from a | |
215 | # previous call (which most likely existed in a separate scope). |
|
216 | # previous call (which most likely existed in a separate scope). | |
216 | local_varnames = local_ns.keys() |
|
217 | local_varnames = local_ns.keys() | |
217 | self.user_ns.update(local_ns) |
|
218 | self.user_ns.update(local_ns) | |
218 | #self.user_ns['local_ns'] = local_ns # dbg |
|
219 | #self.user_ns['local_ns'] = local_ns # dbg | |
219 |
|
220 | |||
220 | # Patch for global embedding to make sure that things don't overwrite |
|
221 | # Patch for global embedding to make sure that things don't overwrite | |
221 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
222 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> | |
222 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
223 | # FIXME. Test this a bit more carefully (the if.. is new) | |
223 | if local_ns is None and global_ns is None: |
|
224 | if local_ns is None and global_ns is None: | |
224 | self.user_global_ns.update(__main__.__dict__) |
|
225 | self.user_global_ns.update(__main__.__dict__) | |
225 |
|
226 | |||
226 | # make sure the tab-completer has the correct frame information, so it |
|
227 | # make sure the tab-completer has the correct frame information, so it | |
227 | # actually completes using the frame's locals/globals |
|
228 | # actually completes using the frame's locals/globals | |
228 | self.set_completer_frame() |
|
229 | self.set_completer_frame() | |
229 |
|
230 | |||
230 | with nested(self.builtin_trap, self.display_trap): |
|
231 | with nested(self.builtin_trap, self.display_trap): | |
231 | self.interact(display_banner=display_banner) |
|
232 | self.interact(display_banner=display_banner) | |
232 |
|
233 | |||
233 | # now, purge out the user namespace from anything we might have added |
|
234 | # now, purge out the user namespace from anything we might have added | |
234 | # from the caller's local namespace |
|
235 | # from the caller's local namespace | |
235 | delvar = self.user_ns.pop |
|
236 | delvar = self.user_ns.pop | |
236 | for var in local_varnames: |
|
237 | for var in local_varnames: | |
237 | delvar(var,None) |
|
238 | delvar(var,None) | |
238 |
|
239 | |||
239 |
|
240 | |||
240 | _embedded_shell = None |
|
241 | _embedded_shell = None | |
241 |
|
242 | |||
242 |
|
243 | |||
243 | def embed(header='', config=None, usage=None, banner1=None, banner2=None, |
|
244 | def embed(**kwargs): | |
244 | display_banner=True, exit_msg=''): |
|
|||
245 | """Call this to embed IPython at the current point in your program. |
|
245 | """Call this to embed IPython at the current point in your program. | |
246 |
|
246 | |||
247 | The first invocation of this will create an :class:`InteractiveShellEmbed` |
|
247 | The first invocation of this will create an :class:`InteractiveShellEmbed` | |
248 | instance and then call it. Consecutive calls just call the already |
|
248 | instance and then call it. Consecutive calls just call the already | |
249 | created instance. |
|
249 | created instance. | |
250 |
|
250 | |||
251 | Here is a simple example:: |
|
251 | Here is a simple example:: | |
252 |
|
252 | |||
253 | from IPython import embed |
|
253 | from IPython import embed | |
254 | a = 10 |
|
254 | a = 10 | |
255 | b = 20 |
|
255 | b = 20 | |
256 | embed('First time') |
|
256 | embed('First time') | |
257 | c = 30 |
|
257 | c = 30 | |
258 | d = 40 |
|
258 | d = 40 | |
259 | embed |
|
259 | embed | |
260 |
|
260 | |||
261 | Full customization can be done by passing a :class:`Struct` in as the |
|
261 | Full customization can be done by passing a :class:`Struct` in as the | |
262 | config argument. |
|
262 | config argument. | |
263 | """ |
|
263 | """ | |
|
264 | config = kwargs.get('config') | |||
|
265 | header = kwargs.pop('header', u'') | |||
264 | if config is None: |
|
266 | if config is None: | |
265 | config = load_default_config() |
|
267 | config = load_default_config() | |
266 | config.InteractiveShellEmbed = config.InteractiveShell |
|
268 | config.InteractiveShellEmbed = config.TerminalInteractiveShell | |
267 | global _embedded_shell |
|
269 | global _embedded_shell | |
268 | if _embedded_shell is None: |
|
270 | if _embedded_shell is None: | |
269 | _embedded_shell = InteractiveShellEmbed( |
|
271 | _embedded_shell = InteractiveShellEmbed(**kwargs) | |
270 | config=config, usage=usage, |
|
|||
271 | banner1=banner1, banner2=banner2, |
|
|||
272 | display_banner=display_banner, exit_msg=exit_msg |
|
|||
273 | ) |
|
|||
274 | _embedded_shell(header=header, stack_depth=2) |
|
272 | _embedded_shell(header=header, stack_depth=2) | |
275 |
|
@@ -1,665 +1,665 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | The :class:`~IPython.core.application.Application` object for the command |
|
4 | The :class:`~IPython.core.application.Application` object for the command | |
5 | line :command:`ipython` program. |
|
5 | line :command:`ipython` program. | |
6 |
|
6 | |||
7 | Authors |
|
7 | Authors | |
8 | ------- |
|
8 | ------- | |
9 |
|
9 | |||
10 | * Brian Granger |
|
10 | * Brian Granger | |
11 | * Fernando Perez |
|
11 | * Fernando Perez | |
12 | """ |
|
12 | """ | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Copyright (C) 2008-2010 The IPython Development Team |
|
15 | # Copyright (C) 2008-2010 The IPython Development Team | |
16 | # |
|
16 | # | |
17 | # Distributed under the terms of the BSD License. The full license is in |
|
17 | # Distributed under the terms of the BSD License. The full license is in | |
18 | # the file COPYING, distributed as part of this software. |
|
18 | # the file COPYING, distributed as part of this software. | |
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 |
|
20 | |||
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | # Imports |
|
22 | # Imports | |
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 |
|
24 | |||
25 | from __future__ import absolute_import |
|
25 | from __future__ import absolute_import | |
26 |
|
26 | |||
27 | import logging |
|
27 | import logging | |
28 | import os |
|
28 | import os | |
29 | import sys |
|
29 | import sys | |
30 |
|
30 | |||
31 | from IPython.core import release |
|
31 | from IPython.core import release | |
32 | from IPython.core.crashhandler import CrashHandler |
|
32 | from IPython.core.crashhandler import CrashHandler | |
33 | from IPython.core.application import Application, BaseAppConfigLoader |
|
33 | from IPython.core.application import Application, BaseAppConfigLoader | |
34 |
from IPython. |
|
34 | from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell | |
35 | from IPython.config.loader import ( |
|
35 | from IPython.config.loader import ( | |
36 | Config, |
|
36 | Config, | |
37 | PyFileConfigLoader |
|
37 | PyFileConfigLoader | |
38 | ) |
|
38 | ) | |
39 | from IPython.lib import inputhook |
|
39 | from IPython.lib import inputhook | |
40 | from IPython.utils.path import filefind, get_ipython_dir |
|
40 | from IPython.utils.path import filefind, get_ipython_dir | |
41 | from IPython.core import usage |
|
41 | from IPython.core import usage | |
42 |
|
42 | |||
43 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
44 | # Globals, utilities and helpers |
|
44 | # Globals, utilities and helpers | |
45 | #----------------------------------------------------------------------------- |
|
45 | #----------------------------------------------------------------------------- | |
46 |
|
46 | |||
47 | #: The default config file name for this application. |
|
47 | #: The default config file name for this application. | |
48 | default_config_file_name = u'ipython_config.py' |
|
48 | default_config_file_name = u'ipython_config.py' | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | class IPAppConfigLoader(BaseAppConfigLoader): |
|
51 | class IPAppConfigLoader(BaseAppConfigLoader): | |
52 |
|
52 | |||
53 | def _add_arguments(self): |
|
53 | def _add_arguments(self): | |
54 | super(IPAppConfigLoader, self)._add_arguments() |
|
54 | super(IPAppConfigLoader, self)._add_arguments() | |
55 | paa = self.parser.add_argument |
|
55 | paa = self.parser.add_argument | |
56 | paa('-p', |
|
56 | paa('-p', | |
57 | '--profile', dest='Global.profile', type=unicode, |
|
57 | '--profile', dest='Global.profile', type=unicode, | |
58 | help= |
|
58 | help= | |
59 | """The string name of the ipython profile to be used. Assume that your |
|
59 | """The string name of the ipython profile to be used. Assume that your | |
60 | config file is ipython_config-<name>.py (looks in current dir first, |
|
60 | config file is ipython_config-<name>.py (looks in current dir first, | |
61 | then in IPYTHON_DIR). This is a quick way to keep and load multiple |
|
61 | then in IPYTHON_DIR). This is a quick way to keep and load multiple | |
62 | config files for different tasks, especially if include your basic one |
|
62 | config files for different tasks, especially if include your basic one | |
63 | in your more specialized ones. You can keep a basic |
|
63 | in your more specialized ones. You can keep a basic | |
64 | IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which |
|
64 | IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which | |
65 | include this one and load extra things for particular tasks.""", |
|
65 | include this one and load extra things for particular tasks.""", | |
66 | metavar='Global.profile') |
|
66 | metavar='Global.profile') | |
67 | paa('--config-file', |
|
67 | paa('--config-file', | |
68 | dest='Global.config_file', type=unicode, |
|
68 | dest='Global.config_file', type=unicode, | |
69 | help= |
|
69 | help= | |
70 | """Set the config file name to override default. Normally IPython |
|
70 | """Set the config file name to override default. Normally IPython | |
71 | loads ipython_config.py (from current directory) or |
|
71 | loads ipython_config.py (from current directory) or | |
72 | IPYTHON_DIR/ipython_config.py. If the loading of your config file |
|
72 | IPYTHON_DIR/ipython_config.py. If the loading of your config file | |
73 | fails, IPython starts with a bare bones configuration (no modules |
|
73 | fails, IPython starts with a bare bones configuration (no modules | |
74 | loaded at all).""", |
|
74 | loaded at all).""", | |
75 | metavar='Global.config_file') |
|
75 | metavar='Global.config_file') | |
76 | paa('--autocall', |
|
76 | paa('--autocall', | |
77 | dest='InteractiveShell.autocall', type=int, |
|
77 | dest='InteractiveShell.autocall', type=int, | |
78 | help= |
|
78 | help= | |
79 | """Make IPython automatically call any callable object even if you |
|
79 | """Make IPython automatically call any callable object even if you | |
80 | didn't type explicit parentheses. For example, 'str 43' becomes |
|
80 | didn't type explicit parentheses. For example, 'str 43' becomes | |
81 | 'str(43)' automatically. The value can be '0' to disable the feature, |
|
81 | 'str(43)' automatically. The value can be '0' to disable the feature, | |
82 | '1' for 'smart' autocall, where it is not applied if there are no more |
|
82 | '1' for 'smart' autocall, where it is not applied if there are no more | |
83 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
83 | arguments on the line, and '2' for 'full' autocall, where all callable | |
84 | objects are automatically called (even if no arguments are present). |
|
84 | objects are automatically called (even if no arguments are present). | |
85 | The default is '1'.""", |
|
85 | The default is '1'.""", | |
86 | metavar='InteractiveShell.autocall') |
|
86 | metavar='InteractiveShell.autocall') | |
87 | paa('--autoindent', |
|
87 | paa('--autoindent', | |
88 | action='store_true', dest='InteractiveShell.autoindent', |
|
88 | action='store_true', dest='TerminalInteractiveShell.autoindent', | |
89 | help='Turn on autoindenting.') |
|
89 | help='Turn on autoindenting.') | |
90 | paa('--no-autoindent', |
|
90 | paa('--no-autoindent', | |
91 | action='store_false', dest='InteractiveShell.autoindent', |
|
91 | action='store_false', dest='TerminalInteractiveShell.autoindent', | |
92 | help='Turn off autoindenting.') |
|
92 | help='Turn off autoindenting.') | |
93 | paa('--automagic', |
|
93 | paa('--automagic', | |
94 | action='store_true', dest='InteractiveShell.automagic', |
|
94 | action='store_true', dest='InteractiveShell.automagic', | |
95 | help= |
|
95 | help= | |
96 | """Turn on the auto calling of magic commands. Type %%magic at the |
|
96 | """Turn on the auto calling of magic commands. Type %%magic at the | |
97 | IPython prompt for more information.""") |
|
97 | IPython prompt for more information.""") | |
98 | paa('--no-automagic', |
|
98 | paa('--no-automagic', | |
99 | action='store_false', dest='InteractiveShell.automagic', |
|
99 | action='store_false', dest='InteractiveShell.automagic', | |
100 | help='Turn off the auto calling of magic commands.') |
|
100 | help='Turn off the auto calling of magic commands.') | |
101 | paa('--autoedit-syntax', |
|
101 | paa('--autoedit-syntax', | |
102 | action='store_true', dest='InteractiveShell.autoedit_syntax', |
|
102 | action='store_true', dest='TerminalInteractiveShell.autoedit_syntax', | |
103 | help='Turn on auto editing of files with syntax errors.') |
|
103 | help='Turn on auto editing of files with syntax errors.') | |
104 | paa('--no-autoedit-syntax', |
|
104 | paa('--no-autoedit-syntax', | |
105 | action='store_false', dest='InteractiveShell.autoedit_syntax', |
|
105 | action='store_false', dest='TerminalInteractiveShell.autoedit_syntax', | |
106 | help='Turn off auto editing of files with syntax errors.') |
|
106 | help='Turn off auto editing of files with syntax errors.') | |
107 | paa('--banner', |
|
107 | paa('--banner', | |
108 | action='store_true', dest='Global.display_banner', |
|
108 | action='store_true', dest='Global.display_banner', | |
109 | help='Display a banner upon starting IPython.') |
|
109 | help='Display a banner upon starting IPython.') | |
110 | paa('--no-banner', |
|
110 | paa('--no-banner', | |
111 | action='store_false', dest='Global.display_banner', |
|
111 | action='store_false', dest='Global.display_banner', | |
112 | help="Don't display a banner upon starting IPython.") |
|
112 | help="Don't display a banner upon starting IPython.") | |
113 | paa('--cache-size', |
|
113 | paa('--cache-size', | |
114 | type=int, dest='InteractiveShell.cache_size', |
|
114 | type=int, dest='InteractiveShell.cache_size', | |
115 | help= |
|
115 | help= | |
116 | """Set the size of the output cache. The default is 1000, you can |
|
116 | """Set the size of the output cache. The default is 1000, you can | |
117 | change it permanently in your config file. Setting it to 0 completely |
|
117 | change it permanently in your config file. Setting it to 0 completely | |
118 | disables the caching system, and the minimum value accepted is 20 (if |
|
118 | disables the caching system, and the minimum value accepted is 20 (if | |
119 | you provide a value less than 20, it is reset to 0 and a warning is |
|
119 | you provide a value less than 20, it is reset to 0 and a warning is | |
120 | issued). This limit is defined because otherwise you'll spend more |
|
120 | issued). This limit is defined because otherwise you'll spend more | |
121 | time re-flushing a too small cache than working""", |
|
121 | time re-flushing a too small cache than working""", | |
122 | metavar='InteractiveShell.cache_size') |
|
122 | metavar='InteractiveShell.cache_size') | |
123 | paa('--classic', |
|
123 | paa('--classic', | |
124 | action='store_true', dest='Global.classic', |
|
124 | action='store_true', dest='Global.classic', | |
125 | help="Gives IPython a similar feel to the classic Python prompt.") |
|
125 | help="Gives IPython a similar feel to the classic Python prompt.") | |
126 | paa('--colors', |
|
126 | paa('--colors', | |
127 | type=str, dest='InteractiveShell.colors', |
|
127 | type=str, dest='InteractiveShell.colors', | |
128 | help="Set the color scheme (NoColor, Linux, and LightBG).", |
|
128 | help="Set the color scheme (NoColor, Linux, and LightBG).", | |
129 | metavar='InteractiveShell.colors') |
|
129 | metavar='InteractiveShell.colors') | |
130 | paa('--color-info', |
|
130 | paa('--color-info', | |
131 | action='store_true', dest='InteractiveShell.color_info', |
|
131 | action='store_true', dest='InteractiveShell.color_info', | |
132 | help= |
|
132 | help= | |
133 | """IPython can display information about objects via a set of func- |
|
133 | """IPython can display information about objects via a set of func- | |
134 | tions, and optionally can use colors for this, syntax highlighting |
|
134 | tions, and optionally can use colors for this, syntax highlighting | |
135 | source code and various other elements. However, because this |
|
135 | source code and various other elements. However, because this | |
136 | information is passed through a pager (like 'less') and many pagers get |
|
136 | information is passed through a pager (like 'less') and many pagers get | |
137 | confused with color codes, this option is off by default. You can test |
|
137 | confused with color codes, this option is off by default. You can test | |
138 | it and turn it on permanently in your ipython_config.py file if it |
|
138 | it and turn it on permanently in your ipython_config.py file if it | |
139 | works for you. Test it and turn it on permanently if it works with |
|
139 | works for you. Test it and turn it on permanently if it works with | |
140 | your system. The magic function %%color_info allows you to toggle this |
|
140 | your system. The magic function %%color_info allows you to toggle this | |
141 | inter- actively for testing.""") |
|
141 | inter- actively for testing.""") | |
142 | paa('--no-color-info', |
|
142 | paa('--no-color-info', | |
143 | action='store_false', dest='InteractiveShell.color_info', |
|
143 | action='store_false', dest='InteractiveShell.color_info', | |
144 | help="Disable using colors for info related things.") |
|
144 | help="Disable using colors for info related things.") | |
145 | paa('--confirm-exit', |
|
145 | paa('--confirm-exit', | |
146 | action='store_true', dest='InteractiveShell.confirm_exit', |
|
146 | action='store_true', dest='TerminalInteractiveShell.confirm_exit', | |
147 | help= |
|
147 | help= | |
148 | """Set to confirm when you try to exit IPython with an EOF (Control-D |
|
148 | """Set to confirm when you try to exit IPython with an EOF (Control-D | |
149 | in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or |
|
149 | in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or | |
150 | '%%Exit', you can force a direct exit without any confirmation.""") |
|
150 | '%%Exit', you can force a direct exit without any confirmation.""") | |
151 | paa('--no-confirm-exit', |
|
151 | paa('--no-confirm-exit', | |
152 | action='store_false', dest='InteractiveShell.confirm_exit', |
|
152 | action='store_false', dest='TerminalInteractiveShell.confirm_exit', | |
153 | help="Don't prompt the user when exiting.") |
|
153 | help="Don't prompt the user when exiting.") | |
154 | paa('--deep-reload', |
|
154 | paa('--deep-reload', | |
155 | action='store_true', dest='InteractiveShell.deep_reload', |
|
155 | action='store_true', dest='InteractiveShell.deep_reload', | |
156 | help= |
|
156 | help= | |
157 | """Enable deep (recursive) reloading by default. IPython can use the |
|
157 | """Enable deep (recursive) reloading by default. IPython can use the | |
158 | deep_reload module which reloads changes in modules recursively (it |
|
158 | deep_reload module which reloads changes in modules recursively (it | |
159 | replaces the reload() function, so you don't need to change anything to |
|
159 | replaces the reload() function, so you don't need to change anything to | |
160 | use it). deep_reload() forces a full reload of modules whose code may |
|
160 | use it). deep_reload() forces a full reload of modules whose code may | |
161 | have changed, which the default reload() function does not. When |
|
161 | have changed, which the default reload() function does not. When | |
162 | deep_reload is off, IPython will use the normal reload(), but |
|
162 | deep_reload is off, IPython will use the normal reload(), but | |
163 | deep_reload will still be available as dreload(). This fea- ture is off |
|
163 | deep_reload will still be available as dreload(). This fea- ture is off | |
164 | by default [which means that you have both normal reload() and |
|
164 | by default [which means that you have both normal reload() and | |
165 | dreload()].""") |
|
165 | dreload()].""") | |
166 | paa('--no-deep-reload', |
|
166 | paa('--no-deep-reload', | |
167 | action='store_false', dest='InteractiveShell.deep_reload', |
|
167 | action='store_false', dest='InteractiveShell.deep_reload', | |
168 | help="Disable deep (recursive) reloading by default.") |
|
168 | help="Disable deep (recursive) reloading by default.") | |
169 | paa('--editor', |
|
169 | paa('--editor', | |
170 | type=str, dest='InteractiveShell.editor', |
|
170 | type=str, dest='TerminalInteractiveShell.editor', | |
171 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad).", |
|
171 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad).", | |
172 | metavar='InteractiveShell.editor') |
|
172 | metavar='TerminalInteractiveShell.editor') | |
173 | paa('--log','-l', |
|
173 | paa('--log','-l', | |
174 | action='store_true', dest='InteractiveShell.logstart', |
|
174 | action='store_true', dest='InteractiveShell.logstart', | |
175 | help="Start logging to the default log file (./ipython_log.py).") |
|
175 | help="Start logging to the default log file (./ipython_log.py).") | |
176 | paa('--logfile','-lf', |
|
176 | paa('--logfile','-lf', | |
177 | type=unicode, dest='InteractiveShell.logfile', |
|
177 | type=unicode, dest='InteractiveShell.logfile', | |
178 | help="Start logging to logfile with this name.", |
|
178 | help="Start logging to logfile with this name.", | |
179 | metavar='InteractiveShell.logfile') |
|
179 | metavar='InteractiveShell.logfile') | |
180 | paa('--log-append','-la', |
|
180 | paa('--log-append','-la', | |
181 | type=unicode, dest='InteractiveShell.logappend', |
|
181 | type=unicode, dest='InteractiveShell.logappend', | |
182 | help="Start logging to the given file in append mode.", |
|
182 | help="Start logging to the given file in append mode.", | |
183 | metavar='InteractiveShell.logfile') |
|
183 | metavar='InteractiveShell.logfile') | |
184 | paa('--pdb', |
|
184 | paa('--pdb', | |
185 | action='store_true', dest='InteractiveShell.pdb', |
|
185 | action='store_true', dest='InteractiveShell.pdb', | |
186 | help="Enable auto calling the pdb debugger after every exception.") |
|
186 | help="Enable auto calling the pdb debugger after every exception.") | |
187 | paa('--no-pdb', |
|
187 | paa('--no-pdb', | |
188 | action='store_false', dest='InteractiveShell.pdb', |
|
188 | action='store_false', dest='InteractiveShell.pdb', | |
189 | help="Disable auto calling the pdb debugger after every exception.") |
|
189 | help="Disable auto calling the pdb debugger after every exception.") | |
190 | paa('--pprint', |
|
190 | paa('--pprint', | |
191 | action='store_true', dest='InteractiveShell.pprint', |
|
191 | action='store_true', dest='InteractiveShell.pprint', | |
192 | help="Enable auto pretty printing of results.") |
|
192 | help="Enable auto pretty printing of results.") | |
193 | paa('--no-pprint', |
|
193 | paa('--no-pprint', | |
194 | action='store_false', dest='InteractiveShell.pprint', |
|
194 | action='store_false', dest='InteractiveShell.pprint', | |
195 | help="Disable auto auto pretty printing of results.") |
|
195 | help="Disable auto auto pretty printing of results.") | |
196 | paa('--prompt-in1','-pi1', |
|
196 | paa('--prompt-in1','-pi1', | |
197 | type=str, dest='InteractiveShell.prompt_in1', |
|
197 | type=str, dest='InteractiveShell.prompt_in1', | |
198 | help= |
|
198 | help= | |
199 | """Set the main input prompt ('In [\#]: '). Note that if you are using |
|
199 | """Set the main input prompt ('In [\#]: '). Note that if you are using | |
200 | numbered prompts, the number is represented with a '\#' in the string. |
|
200 | numbered prompts, the number is represented with a '\#' in the string. | |
201 | Don't forget to quote strings with spaces embedded in them. Most |
|
201 | Don't forget to quote strings with spaces embedded in them. Most | |
202 | bash-like escapes can be used to customize IPython's prompts, as well |
|
202 | bash-like escapes can be used to customize IPython's prompts, as well | |
203 | as a few additional ones which are IPython-spe- cific. All valid |
|
203 | as a few additional ones which are IPython-spe- cific. All valid | |
204 | prompt escapes are described in detail in the Customization section of |
|
204 | prompt escapes are described in detail in the Customization section of | |
205 | the IPython manual.""", |
|
205 | the IPython manual.""", | |
206 | metavar='InteractiveShell.prompt_in1') |
|
206 | metavar='InteractiveShell.prompt_in1') | |
207 | paa('--prompt-in2','-pi2', |
|
207 | paa('--prompt-in2','-pi2', | |
208 | type=str, dest='InteractiveShell.prompt_in2', |
|
208 | type=str, dest='InteractiveShell.prompt_in2', | |
209 | help= |
|
209 | help= | |
210 | """Set the secondary input prompt (' .\D.: '). Similar to the previous |
|
210 | """Set the secondary input prompt (' .\D.: '). Similar to the previous | |
211 | option, but used for the continuation prompts. The special sequence |
|
211 | option, but used for the continuation prompts. The special sequence | |
212 | '\D' is similar to '\#', but with all digits replaced by dots (so you |
|
212 | '\D' is similar to '\#', but with all digits replaced by dots (so you | |
213 | can have your continuation prompt aligned with your input prompt). |
|
213 | can have your continuation prompt aligned with your input prompt). | |
214 | Default: ' .\D.: ' (note three spaces at the start for alignment with |
|
214 | Default: ' .\D.: ' (note three spaces at the start for alignment with | |
215 | 'In [\#]')""", |
|
215 | 'In [\#]')""", | |
216 | metavar='InteractiveShell.prompt_in2') |
|
216 | metavar='InteractiveShell.prompt_in2') | |
217 | paa('--prompt-out','-po', |
|
217 | paa('--prompt-out','-po', | |
218 | type=str, dest='InteractiveShell.prompt_out', |
|
218 | type=str, dest='InteractiveShell.prompt_out', | |
219 | help="Set the output prompt ('Out[\#]:')", |
|
219 | help="Set the output prompt ('Out[\#]:')", | |
220 | metavar='InteractiveShell.prompt_out') |
|
220 | metavar='InteractiveShell.prompt_out') | |
221 | paa('--quick', |
|
221 | paa('--quick', | |
222 | action='store_true', dest='Global.quick', |
|
222 | action='store_true', dest='Global.quick', | |
223 | help="Enable quick startup with no config files.") |
|
223 | help="Enable quick startup with no config files.") | |
224 | paa('--readline', |
|
224 | paa('--readline', | |
225 | action='store_true', dest='InteractiveShell.readline_use', |
|
225 | action='store_true', dest='InteractiveShell.readline_use', | |
226 | help="Enable readline for command line usage.") |
|
226 | help="Enable readline for command line usage.") | |
227 | paa('--no-readline', |
|
227 | paa('--no-readline', | |
228 | action='store_false', dest='InteractiveShell.readline_use', |
|
228 | action='store_false', dest='InteractiveShell.readline_use', | |
229 | help="Disable readline for command line usage.") |
|
229 | help="Disable readline for command line usage.") | |
230 | paa('--screen-length','-sl', |
|
230 | paa('--screen-length','-sl', | |
231 | type=int, dest='InteractiveShell.screen_length', |
|
231 | type=int, dest='TerminalInteractiveShell.screen_length', | |
232 | help= |
|
232 | help= | |
233 | """Number of lines of your screen, used to control printing of very |
|
233 | """Number of lines of your screen, used to control printing of very | |
234 | long strings. Strings longer than this number of lines will be sent |
|
234 | long strings. Strings longer than this number of lines will be sent | |
235 | through a pager instead of directly printed. The default value for |
|
235 | through a pager instead of directly printed. The default value for | |
236 | this is 0, which means IPython will auto-detect your screen size every |
|
236 | this is 0, which means IPython will auto-detect your screen size every | |
237 | time it needs to print certain potentially long strings (this doesn't |
|
237 | time it needs to print certain potentially long strings (this doesn't | |
238 | change the behavior of the 'print' keyword, it's only triggered |
|
238 | change the behavior of the 'print' keyword, it's only triggered | |
239 | internally). If for some reason this isn't working well (it needs |
|
239 | internally). If for some reason this isn't working well (it needs | |
240 | curses support), specify it yourself. Otherwise don't change the |
|
240 | curses support), specify it yourself. Otherwise don't change the | |
241 | default.""", |
|
241 | default.""", | |
242 | metavar='InteractiveShell.screen_length') |
|
242 | metavar='TerminalInteractiveShell.screen_length') | |
243 | paa('--separate-in','-si', |
|
243 | paa('--separate-in','-si', | |
244 | type=str, dest='InteractiveShell.separate_in', |
|
244 | type=str, dest='TerminalInteractiveShell.separate_in', | |
245 | help="Separator before input prompts. Default '\\n'.", |
|
245 | help="Separator before input prompts. Default '\\n'.", | |
246 | metavar='InteractiveShell.separate_in') |
|
246 | metavar='TerminalInteractiveShell.separate_in') | |
247 | paa('--separate-out','-so', |
|
247 | paa('--separate-out','-so', | |
248 | type=str, dest='InteractiveShell.separate_out', |
|
248 | type=str, dest='TerminalInteractiveShell.separate_out', | |
249 | help="Separator before output prompts. Default 0 (nothing).", |
|
249 | help="Separator before output prompts. Default 0 (nothing).", | |
250 | metavar='InteractiveShell.separate_out') |
|
250 | metavar='TerminalInteractiveShell.separate_out') | |
251 | paa('--separate-out2','-so2', |
|
251 | paa('--separate-out2','-so2', | |
252 | type=str, dest='InteractiveShell.separate_out2', |
|
252 | type=str, dest='TerminalInteractiveShell.separate_out2', | |
253 | help="Separator after output prompts. Default 0 (nonight).", |
|
253 | help="Separator after output prompts. Default 0 (nonight).", | |
254 | metavar='InteractiveShell.separate_out2') |
|
254 | metavar='TerminalInteractiveShell.separate_out2') | |
255 | paa('--no-sep', |
|
255 | paa('--no-sep', | |
256 | action='store_true', dest='Global.nosep', |
|
256 | action='store_true', dest='Global.nosep', | |
257 | help="Eliminate all spacing between prompts.") |
|
257 | help="Eliminate all spacing between prompts.") | |
258 | paa('--term-title', |
|
258 | paa('--term-title', | |
259 | action='store_true', dest='InteractiveShell.term_title', |
|
259 | action='store_true', dest='TerminalInteractiveShell.term_title', | |
260 | help="Enable auto setting the terminal title.") |
|
260 | help="Enable auto setting the terminal title.") | |
261 | paa('--no-term-title', |
|
261 | paa('--no-term-title', | |
262 | action='store_false', dest='InteractiveShell.term_title', |
|
262 | action='store_false', dest='TerminalInteractiveShell.term_title', | |
263 | help="Disable auto setting the terminal title.") |
|
263 | help="Disable auto setting the terminal title.") | |
264 | paa('--xmode', |
|
264 | paa('--xmode', | |
265 | type=str, dest='InteractiveShell.xmode', |
|
265 | type=str, dest='InteractiveShell.xmode', | |
266 | help= |
|
266 | help= | |
267 | """Exception reporting mode ('Plain','Context','Verbose'). Plain: |
|
267 | """Exception reporting mode ('Plain','Context','Verbose'). Plain: | |
268 | similar to python's normal traceback printing. Context: prints 5 lines |
|
268 | similar to python's normal traceback printing. Context: prints 5 lines | |
269 | of context source code around each line in the traceback. Verbose: |
|
269 | of context source code around each line in the traceback. Verbose: | |
270 | similar to Context, but additionally prints the variables currently |
|
270 | similar to Context, but additionally prints the variables currently | |
271 | visible where the exception happened (shortening their strings if too |
|
271 | visible where the exception happened (shortening their strings if too | |
272 | long). This can potentially be very slow, if you happen to have a huge |
|
272 | long). This can potentially be very slow, if you happen to have a huge | |
273 | data structure whose string representation is complex to compute. |
|
273 | data structure whose string representation is complex to compute. | |
274 | Your computer may appear to freeze for a while with cpu usage at 100%%. |
|
274 | Your computer may appear to freeze for a while with cpu usage at 100%%. | |
275 | If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting |
|
275 | If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting | |
276 | it more than once). |
|
276 | it more than once). | |
277 | """, |
|
277 | """, | |
278 | metavar='InteractiveShell.xmode') |
|
278 | metavar='InteractiveShell.xmode') | |
279 | paa('--ext', |
|
279 | paa('--ext', | |
280 | type=str, dest='Global.extra_extension', |
|
280 | type=str, dest='Global.extra_extension', | |
281 | help="The dotted module name of an IPython extension to load.", |
|
281 | help="The dotted module name of an IPython extension to load.", | |
282 | metavar='Global.extra_extension') |
|
282 | metavar='Global.extra_extension') | |
283 | paa('-c', |
|
283 | paa('-c', | |
284 | type=str, dest='Global.code_to_run', |
|
284 | type=str, dest='Global.code_to_run', | |
285 | help="Execute the given command string.", |
|
285 | help="Execute the given command string.", | |
286 | metavar='Global.code_to_run') |
|
286 | metavar='Global.code_to_run') | |
287 | paa('-i', |
|
287 | paa('-i', | |
288 | action='store_true', dest='Global.force_interact', |
|
288 | action='store_true', dest='Global.force_interact', | |
289 | help= |
|
289 | help= | |
290 | "If running code from the command line, become interactive afterwards.") |
|
290 | "If running code from the command line, become interactive afterwards.") | |
291 |
|
291 | |||
292 | # Options to start with GUI control enabled from the beginning |
|
292 | # Options to start with GUI control enabled from the beginning | |
293 | paa('--gui', |
|
293 | paa('--gui', | |
294 | type=str, dest='Global.gui', |
|
294 | type=str, dest='Global.gui', | |
295 | help="Enable GUI event loop integration ('qt', 'wx', 'gtk').", |
|
295 | help="Enable GUI event loop integration ('qt', 'wx', 'gtk').", | |
296 | metavar='gui-mode') |
|
296 | metavar='gui-mode') | |
297 | paa('--pylab','-pylab', |
|
297 | paa('--pylab','-pylab', | |
298 | type=str, dest='Global.pylab', |
|
298 | type=str, dest='Global.pylab', | |
299 | nargs='?', const='auto', metavar='gui-mode', |
|
299 | nargs='?', const='auto', metavar='gui-mode', | |
300 | help="Pre-load matplotlib and numpy for interactive use. "+ |
|
300 | help="Pre-load matplotlib and numpy for interactive use. "+ | |
301 | "If no value is given, the gui backend is matplotlib's, else use "+ |
|
301 | "If no value is given, the gui backend is matplotlib's, else use "+ | |
302 | "one of: ['tk', 'qt', 'wx', 'gtk'].") |
|
302 | "one of: ['tk', 'qt', 'wx', 'gtk'].") | |
303 |
|
303 | |||
304 | # Legacy GUI options. Leave them in for backwards compatibility, but the |
|
304 | # Legacy GUI options. Leave them in for backwards compatibility, but the | |
305 | # 'thread' names are really a misnomer now. |
|
305 | # 'thread' names are really a misnomer now. | |
306 | paa('--wthread', '-wthread', |
|
306 | paa('--wthread', '-wthread', | |
307 | action='store_true', dest='Global.wthread', |
|
307 | action='store_true', dest='Global.wthread', | |
308 | help= |
|
308 | help= | |
309 | """Enable wxPython event loop integration. (DEPRECATED, use --gui wx)""") |
|
309 | """Enable wxPython event loop integration. (DEPRECATED, use --gui wx)""") | |
310 | paa('--q4thread', '--qthread', '-q4thread', '-qthread', |
|
310 | paa('--q4thread', '--qthread', '-q4thread', '-qthread', | |
311 | action='store_true', dest='Global.q4thread', |
|
311 | action='store_true', dest='Global.q4thread', | |
312 | help= |
|
312 | help= | |
313 | """Enable Qt4 event loop integration. Qt3 is no longer supported. |
|
313 | """Enable Qt4 event loop integration. Qt3 is no longer supported. | |
314 | (DEPRECATED, use --gui qt)""") |
|
314 | (DEPRECATED, use --gui qt)""") | |
315 | paa('--gthread', '-gthread', |
|
315 | paa('--gthread', '-gthread', | |
316 | action='store_true', dest='Global.gthread', |
|
316 | action='store_true', dest='Global.gthread', | |
317 | help= |
|
317 | help= | |
318 | """Enable GTK event loop integration. (DEPRECATED, use --gui gtk)""") |
|
318 | """Enable GTK event loop integration. (DEPRECATED, use --gui gtk)""") | |
319 |
|
319 | |||
320 |
|
320 | |||
321 | #----------------------------------------------------------------------------- |
|
321 | #----------------------------------------------------------------------------- | |
322 | # Crash handler for this application |
|
322 | # Crash handler for this application | |
323 | #----------------------------------------------------------------------------- |
|
323 | #----------------------------------------------------------------------------- | |
324 |
|
324 | |||
325 |
|
325 | |||
326 | _message_template = """\ |
|
326 | _message_template = """\ | |
327 | Oops, $self.app_name crashed. We do our best to make it stable, but... |
|
327 | Oops, $self.app_name crashed. We do our best to make it stable, but... | |
328 |
|
328 | |||
329 | A crash report was automatically generated with the following information: |
|
329 | A crash report was automatically generated with the following information: | |
330 | - A verbatim copy of the crash traceback. |
|
330 | - A verbatim copy of the crash traceback. | |
331 | - A copy of your input history during this session. |
|
331 | - A copy of your input history during this session. | |
332 | - Data on your current $self.app_name configuration. |
|
332 | - Data on your current $self.app_name configuration. | |
333 |
|
333 | |||
334 | It was left in the file named: |
|
334 | It was left in the file named: | |
335 | \t'$self.crash_report_fname' |
|
335 | \t'$self.crash_report_fname' | |
336 | If you can email this file to the developers, the information in it will help |
|
336 | If you can email this file to the developers, the information in it will help | |
337 | them in understanding and correcting the problem. |
|
337 | them in understanding and correcting the problem. | |
338 |
|
338 | |||
339 | You can mail it to: $self.contact_name at $self.contact_email |
|
339 | You can mail it to: $self.contact_name at $self.contact_email | |
340 | with the subject '$self.app_name Crash Report'. |
|
340 | with the subject '$self.app_name Crash Report'. | |
341 |
|
341 | |||
342 | If you want to do it now, the following command will work (under Unix): |
|
342 | If you want to do it now, the following command will work (under Unix): | |
343 | mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname |
|
343 | mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname | |
344 |
|
344 | |||
345 | To ensure accurate tracking of this issue, please file a report about it at: |
|
345 | To ensure accurate tracking of this issue, please file a report about it at: | |
346 | $self.bug_tracker |
|
346 | $self.bug_tracker | |
347 | """ |
|
347 | """ | |
348 |
|
348 | |||
349 | class IPAppCrashHandler(CrashHandler): |
|
349 | class IPAppCrashHandler(CrashHandler): | |
350 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" |
|
350 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" | |
351 |
|
351 | |||
352 | message_template = _message_template |
|
352 | message_template = _message_template | |
353 |
|
353 | |||
354 | def __init__(self, app): |
|
354 | def __init__(self, app): | |
355 | contact_name = release.authors['Fernando'][0] |
|
355 | contact_name = release.authors['Fernando'][0] | |
356 | contact_email = release.authors['Fernando'][1] |
|
356 | contact_email = release.authors['Fernando'][1] | |
357 | bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug' |
|
357 | bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug' | |
358 | super(IPAppCrashHandler,self).__init__( |
|
358 | super(IPAppCrashHandler,self).__init__( | |
359 | app, contact_name, contact_email, bug_tracker |
|
359 | app, contact_name, contact_email, bug_tracker | |
360 | ) |
|
360 | ) | |
361 |
|
361 | |||
362 | def make_report(self,traceback): |
|
362 | def make_report(self,traceback): | |
363 | """Return a string containing a crash report.""" |
|
363 | """Return a string containing a crash report.""" | |
364 |
|
364 | |||
365 | sec_sep = self.section_sep |
|
365 | sec_sep = self.section_sep | |
366 | # Start with parent report |
|
366 | # Start with parent report | |
367 | report = [super(IPAppCrashHandler, self).make_report(traceback)] |
|
367 | report = [super(IPAppCrashHandler, self).make_report(traceback)] | |
368 | # Add interactive-specific info we may have |
|
368 | # Add interactive-specific info we may have | |
369 | rpt_add = report.append |
|
369 | rpt_add = report.append | |
370 | try: |
|
370 | try: | |
371 | rpt_add(sec_sep+"History of session input:") |
|
371 | rpt_add(sec_sep+"History of session input:") | |
372 | for line in self.app.shell.user_ns['_ih']: |
|
372 | for line in self.app.shell.user_ns['_ih']: | |
373 | rpt_add(line) |
|
373 | rpt_add(line) | |
374 | rpt_add('\n*** Last line of input (may not be in above history):\n') |
|
374 | rpt_add('\n*** Last line of input (may not be in above history):\n') | |
375 | rpt_add(self.app.shell._last_input_line+'\n') |
|
375 | rpt_add(self.app.shell._last_input_line+'\n') | |
376 | except: |
|
376 | except: | |
377 | pass |
|
377 | pass | |
378 |
|
378 | |||
379 | return ''.join(report) |
|
379 | return ''.join(report) | |
380 |
|
380 | |||
381 |
|
381 | |||
382 | #----------------------------------------------------------------------------- |
|
382 | #----------------------------------------------------------------------------- | |
383 | # Main classes and functions |
|
383 | # Main classes and functions | |
384 | #----------------------------------------------------------------------------- |
|
384 | #----------------------------------------------------------------------------- | |
385 |
|
385 | |||
386 | class IPythonApp(Application): |
|
386 | class IPythonApp(Application): | |
387 | name = u'ipython' |
|
387 | name = u'ipython' | |
388 | #: argparse formats better the 'usage' than the 'description' field |
|
388 | #: argparse formats better the 'usage' than the 'description' field | |
389 | description = None |
|
389 | description = None | |
390 | usage = usage.cl_usage |
|
390 | usage = usage.cl_usage | |
391 | command_line_loader = IPAppConfigLoader |
|
391 | command_line_loader = IPAppConfigLoader | |
392 | default_config_file_name = default_config_file_name |
|
392 | default_config_file_name = default_config_file_name | |
393 | crash_handler_class = IPAppCrashHandler |
|
393 | crash_handler_class = IPAppCrashHandler | |
394 |
|
394 | |||
395 | def create_default_config(self): |
|
395 | def create_default_config(self): | |
396 | super(IPythonApp, self).create_default_config() |
|
396 | super(IPythonApp, self).create_default_config() | |
397 | # Eliminate multiple lookups |
|
397 | # Eliminate multiple lookups | |
398 | Global = self.default_config.Global |
|
398 | Global = self.default_config.Global | |
399 |
|
399 | |||
400 | # Set all default values |
|
400 | # Set all default values | |
401 | Global.display_banner = True |
|
401 | Global.display_banner = True | |
402 |
|
402 | |||
403 | # If the -c flag is given or a file is given to run at the cmd line |
|
403 | # If the -c flag is given or a file is given to run at the cmd line | |
404 | # like "ipython foo.py", normally we exit without starting the main |
|
404 | # like "ipython foo.py", normally we exit without starting the main | |
405 | # loop. The force_interact config variable allows a user to override |
|
405 | # loop. The force_interact config variable allows a user to override | |
406 | # this and interact. It is also set by the -i cmd line flag, just |
|
406 | # this and interact. It is also set by the -i cmd line flag, just | |
407 | # like Python. |
|
407 | # like Python. | |
408 | Global.force_interact = False |
|
408 | Global.force_interact = False | |
409 |
|
409 | |||
410 | # By default always interact by starting the IPython mainloop. |
|
410 | # By default always interact by starting the IPython mainloop. | |
411 | Global.interact = True |
|
411 | Global.interact = True | |
412 |
|
412 | |||
413 | # No GUI integration by default |
|
413 | # No GUI integration by default | |
414 | Global.gui = False |
|
414 | Global.gui = False | |
415 | # Pylab off by default |
|
415 | # Pylab off by default | |
416 | Global.pylab = False |
|
416 | Global.pylab = False | |
417 |
|
417 | |||
418 | # Deprecated versions of gui support that used threading, we support |
|
418 | # Deprecated versions of gui support that used threading, we support | |
419 | # them just for bacwards compatibility as an alternate spelling for |
|
419 | # them just for bacwards compatibility as an alternate spelling for | |
420 | # '--gui X' |
|
420 | # '--gui X' | |
421 | Global.qthread = False |
|
421 | Global.qthread = False | |
422 | Global.q4thread = False |
|
422 | Global.q4thread = False | |
423 | Global.wthread = False |
|
423 | Global.wthread = False | |
424 | Global.gthread = False |
|
424 | Global.gthread = False | |
425 |
|
425 | |||
426 | def load_file_config(self): |
|
426 | def load_file_config(self): | |
427 | if hasattr(self.command_line_config.Global, 'quick'): |
|
427 | if hasattr(self.command_line_config.Global, 'quick'): | |
428 | if self.command_line_config.Global.quick: |
|
428 | if self.command_line_config.Global.quick: | |
429 | self.file_config = Config() |
|
429 | self.file_config = Config() | |
430 | return |
|
430 | return | |
431 | super(IPythonApp, self).load_file_config() |
|
431 | super(IPythonApp, self).load_file_config() | |
432 |
|
432 | |||
433 | def post_load_file_config(self): |
|
433 | def post_load_file_config(self): | |
434 | if hasattr(self.command_line_config.Global, 'extra_extension'): |
|
434 | if hasattr(self.command_line_config.Global, 'extra_extension'): | |
435 | if not hasattr(self.file_config.Global, 'extensions'): |
|
435 | if not hasattr(self.file_config.Global, 'extensions'): | |
436 | self.file_config.Global.extensions = [] |
|
436 | self.file_config.Global.extensions = [] | |
437 | self.file_config.Global.extensions.append( |
|
437 | self.file_config.Global.extensions.append( | |
438 | self.command_line_config.Global.extra_extension) |
|
438 | self.command_line_config.Global.extra_extension) | |
439 | del self.command_line_config.Global.extra_extension |
|
439 | del self.command_line_config.Global.extra_extension | |
440 |
|
440 | |||
441 | def pre_construct(self): |
|
441 | def pre_construct(self): | |
442 | config = self.master_config |
|
442 | config = self.master_config | |
443 |
|
443 | |||
444 | if hasattr(config.Global, 'classic'): |
|
444 | if hasattr(config.Global, 'classic'): | |
445 | if config.Global.classic: |
|
445 | if config.Global.classic: | |
446 | config.InteractiveShell.cache_size = 0 |
|
446 | config.InteractiveShell.cache_size = 0 | |
447 | config.InteractiveShell.pprint = 0 |
|
447 | config.InteractiveShell.pprint = 0 | |
448 | config.InteractiveShell.prompt_in1 = '>>> ' |
|
448 | config.InteractiveShell.prompt_in1 = '>>> ' | |
449 | config.InteractiveShell.prompt_in2 = '... ' |
|
449 | config.InteractiveShell.prompt_in2 = '... ' | |
450 | config.InteractiveShell.prompt_out = '' |
|
450 | config.InteractiveShell.prompt_out = '' | |
451 | config.InteractiveShell.separate_in = \ |
|
451 | config.TerminalInteractiveShell.separate_in = \ | |
452 | config.InteractiveShell.separate_out = \ |
|
452 | config.TerminalInteractiveShell.separate_out = \ | |
453 | config.InteractiveShell.separate_out2 = '' |
|
453 | config.TerminalInteractiveShell.separate_out2 = '' | |
454 | config.InteractiveShell.colors = 'NoColor' |
|
454 | config.InteractiveShell.colors = 'NoColor' | |
455 | config.InteractiveShell.xmode = 'Plain' |
|
455 | config.InteractiveShell.xmode = 'Plain' | |
456 |
|
456 | |||
457 | if hasattr(config.Global, 'nosep'): |
|
457 | if hasattr(config.Global, 'nosep'): | |
458 | if config.Global.nosep: |
|
458 | if config.Global.nosep: | |
459 | config.InteractiveShell.separate_in = \ |
|
459 | config.TerminalInteractiveShell.separate_in = \ | |
460 | config.InteractiveShell.separate_out = \ |
|
460 | config.TerminalInteractiveShell.separate_out = \ | |
461 | config.InteractiveShell.separate_out2 = '' |
|
461 | config.TerminalInteractiveShell.separate_out2 = '' | |
462 |
|
462 | |||
463 | # if there is code of files to run from the cmd line, don't interact |
|
463 | # if there is code of files to run from the cmd line, don't interact | |
464 | # unless the -i flag (Global.force_interact) is true. |
|
464 | # unless the -i flag (Global.force_interact) is true. | |
465 | code_to_run = config.Global.get('code_to_run','') |
|
465 | code_to_run = config.Global.get('code_to_run','') | |
466 | file_to_run = False |
|
466 | file_to_run = False | |
467 | if self.extra_args and self.extra_args[0]: |
|
467 | if self.extra_args and self.extra_args[0]: | |
468 | file_to_run = True |
|
468 | file_to_run = True | |
469 | if file_to_run or code_to_run: |
|
469 | if file_to_run or code_to_run: | |
470 | if not config.Global.force_interact: |
|
470 | if not config.Global.force_interact: | |
471 | config.Global.interact = False |
|
471 | config.Global.interact = False | |
472 |
|
472 | |||
473 | def construct(self): |
|
473 | def construct(self): | |
474 | # I am a little hesitant to put these into InteractiveShell itself. |
|
474 | # I am a little hesitant to put these into InteractiveShell itself. | |
475 | # But that might be the place for them |
|
475 | # But that might be the place for them | |
476 | sys.path.insert(0, '') |
|
476 | sys.path.insert(0, '') | |
477 |
|
477 | |||
478 | # Create an InteractiveShell instance. |
|
478 | # Create an InteractiveShell instance. | |
479 | self.shell = InteractiveShell.instance(config=self.master_config) |
|
479 | self.shell = TerminalInteractiveShell.instance(config=self.master_config) | |
480 |
|
480 | |||
481 | def post_construct(self): |
|
481 | def post_construct(self): | |
482 | """Do actions after construct, but before starting the app.""" |
|
482 | """Do actions after construct, but before starting the app.""" | |
483 | config = self.master_config |
|
483 | config = self.master_config | |
484 |
|
484 | |||
485 | # shell.display_banner should always be False for the terminal |
|
485 | # shell.display_banner should always be False for the terminal | |
486 | # based app, because we call shell.show_banner() by hand below |
|
486 | # based app, because we call shell.show_banner() by hand below | |
487 | # so the banner shows *before* all extension loading stuff. |
|
487 | # so the banner shows *before* all extension loading stuff. | |
488 | self.shell.display_banner = False |
|
488 | self.shell.display_banner = False | |
489 | if config.Global.display_banner and \ |
|
489 | if config.Global.display_banner and \ | |
490 | config.Global.interact: |
|
490 | config.Global.interact: | |
491 | self.shell.show_banner() |
|
491 | self.shell.show_banner() | |
492 |
|
492 | |||
493 | # Make sure there is a space below the banner. |
|
493 | # Make sure there is a space below the banner. | |
494 | if self.log_level <= logging.INFO: print |
|
494 | if self.log_level <= logging.INFO: print | |
495 |
|
495 | |||
496 | # Now a variety of things that happen after the banner is printed. |
|
496 | # Now a variety of things that happen after the banner is printed. | |
497 | self._enable_gui_pylab() |
|
497 | self._enable_gui_pylab() | |
498 | self._load_extensions() |
|
498 | self._load_extensions() | |
499 | self._run_exec_lines() |
|
499 | self._run_exec_lines() | |
500 | self._run_exec_files() |
|
500 | self._run_exec_files() | |
501 | self._run_cmd_line_code() |
|
501 | self._run_cmd_line_code() | |
502 |
|
502 | |||
503 | def _enable_gui_pylab(self): |
|
503 | def _enable_gui_pylab(self): | |
504 | """Enable GUI event loop integration, taking pylab into account.""" |
|
504 | """Enable GUI event loop integration, taking pylab into account.""" | |
505 | Global = self.master_config.Global |
|
505 | Global = self.master_config.Global | |
506 |
|
506 | |||
507 | # Select which gui to use |
|
507 | # Select which gui to use | |
508 | if Global.gui: |
|
508 | if Global.gui: | |
509 | gui = Global.gui |
|
509 | gui = Global.gui | |
510 | # The following are deprecated, but there's likely to be a lot of use |
|
510 | # The following are deprecated, but there's likely to be a lot of use | |
511 | # of this form out there, so we might as well support it for now. But |
|
511 | # of this form out there, so we might as well support it for now. But | |
512 | # the --gui option above takes precedence. |
|
512 | # the --gui option above takes precedence. | |
513 | elif Global.wthread: |
|
513 | elif Global.wthread: | |
514 | gui = inputhook.GUI_WX |
|
514 | gui = inputhook.GUI_WX | |
515 | elif Global.qthread: |
|
515 | elif Global.qthread: | |
516 | gui = inputhook.GUI_QT |
|
516 | gui = inputhook.GUI_QT | |
517 | elif Global.gthread: |
|
517 | elif Global.gthread: | |
518 | gui = inputhook.GUI_GTK |
|
518 | gui = inputhook.GUI_GTK | |
519 | else: |
|
519 | else: | |
520 | gui = None |
|
520 | gui = None | |
521 |
|
521 | |||
522 | # Using --pylab will also require gui activation, though which toolkit |
|
522 | # Using --pylab will also require gui activation, though which toolkit | |
523 | # to use may be chosen automatically based on mpl configuration. |
|
523 | # to use may be chosen automatically based on mpl configuration. | |
524 | if Global.pylab: |
|
524 | if Global.pylab: | |
525 | activate = self.shell.enable_pylab |
|
525 | activate = self.shell.enable_pylab | |
526 | if Global.pylab == 'auto': |
|
526 | if Global.pylab == 'auto': | |
527 | gui = None |
|
527 | gui = None | |
528 | else: |
|
528 | else: | |
529 | gui = Global.pylab |
|
529 | gui = Global.pylab | |
530 | else: |
|
530 | else: | |
531 | # Enable only GUI integration, no pylab |
|
531 | # Enable only GUI integration, no pylab | |
532 | activate = inputhook.enable_gui |
|
532 | activate = inputhook.enable_gui | |
533 |
|
533 | |||
534 | if gui or Global.pylab: |
|
534 | if gui or Global.pylab: | |
535 | try: |
|
535 | try: | |
536 | self.log.info("Enabling GUI event loop integration, " |
|
536 | self.log.info("Enabling GUI event loop integration, " | |
537 | "toolkit=%s, pylab=%s" % (gui, Global.pylab) ) |
|
537 | "toolkit=%s, pylab=%s" % (gui, Global.pylab) ) | |
538 | activate(gui) |
|
538 | activate(gui) | |
539 | except: |
|
539 | except: | |
540 | self.log.warn("Error in enabling GUI event loop integration:") |
|
540 | self.log.warn("Error in enabling GUI event loop integration:") | |
541 | self.shell.showtraceback() |
|
541 | self.shell.showtraceback() | |
542 |
|
542 | |||
543 | def _load_extensions(self): |
|
543 | def _load_extensions(self): | |
544 | """Load all IPython extensions in Global.extensions. |
|
544 | """Load all IPython extensions in Global.extensions. | |
545 |
|
545 | |||
546 | This uses the :meth:`ExtensionManager.load_extensions` to load all |
|
546 | This uses the :meth:`ExtensionManager.load_extensions` to load all | |
547 | the extensions listed in ``self.master_config.Global.extensions``. |
|
547 | the extensions listed in ``self.master_config.Global.extensions``. | |
548 | """ |
|
548 | """ | |
549 | try: |
|
549 | try: | |
550 | if hasattr(self.master_config.Global, 'extensions'): |
|
550 | if hasattr(self.master_config.Global, 'extensions'): | |
551 | self.log.debug("Loading IPython extensions...") |
|
551 | self.log.debug("Loading IPython extensions...") | |
552 | extensions = self.master_config.Global.extensions |
|
552 | extensions = self.master_config.Global.extensions | |
553 | for ext in extensions: |
|
553 | for ext in extensions: | |
554 | try: |
|
554 | try: | |
555 | self.log.info("Loading IPython extension: %s" % ext) |
|
555 | self.log.info("Loading IPython extension: %s" % ext) | |
556 | self.shell.extension_manager.load_extension(ext) |
|
556 | self.shell.extension_manager.load_extension(ext) | |
557 | except: |
|
557 | except: | |
558 | self.log.warn("Error in loading extension: %s" % ext) |
|
558 | self.log.warn("Error in loading extension: %s" % ext) | |
559 | self.shell.showtraceback() |
|
559 | self.shell.showtraceback() | |
560 | except: |
|
560 | except: | |
561 | self.log.warn("Unknown error in loading extensions:") |
|
561 | self.log.warn("Unknown error in loading extensions:") | |
562 | self.shell.showtraceback() |
|
562 | self.shell.showtraceback() | |
563 |
|
563 | |||
564 | def _run_exec_lines(self): |
|
564 | def _run_exec_lines(self): | |
565 | """Run lines of code in Global.exec_lines in the user's namespace.""" |
|
565 | """Run lines of code in Global.exec_lines in the user's namespace.""" | |
566 | try: |
|
566 | try: | |
567 | if hasattr(self.master_config.Global, 'exec_lines'): |
|
567 | if hasattr(self.master_config.Global, 'exec_lines'): | |
568 | self.log.debug("Running code from Global.exec_lines...") |
|
568 | self.log.debug("Running code from Global.exec_lines...") | |
569 | exec_lines = self.master_config.Global.exec_lines |
|
569 | exec_lines = self.master_config.Global.exec_lines | |
570 | for line in exec_lines: |
|
570 | for line in exec_lines: | |
571 | try: |
|
571 | try: | |
572 | self.log.info("Running code in user namespace: %s" % |
|
572 | self.log.info("Running code in user namespace: %s" % | |
573 | line) |
|
573 | line) | |
574 | self.shell.runlines(line) |
|
574 | self.shell.runlines(line) | |
575 | except: |
|
575 | except: | |
576 | self.log.warn("Error in executing line in user " |
|
576 | self.log.warn("Error in executing line in user " | |
577 | "namespace: %s" % line) |
|
577 | "namespace: %s" % line) | |
578 | self.shell.showtraceback() |
|
578 | self.shell.showtraceback() | |
579 | except: |
|
579 | except: | |
580 | self.log.warn("Unknown error in handling Global.exec_lines:") |
|
580 | self.log.warn("Unknown error in handling Global.exec_lines:") | |
581 | self.shell.showtraceback() |
|
581 | self.shell.showtraceback() | |
582 |
|
582 | |||
583 | def _exec_file(self, fname): |
|
583 | def _exec_file(self, fname): | |
584 | full_filename = filefind(fname, [u'.', self.ipython_dir]) |
|
584 | full_filename = filefind(fname, [u'.', self.ipython_dir]) | |
585 | if os.path.isfile(full_filename): |
|
585 | if os.path.isfile(full_filename): | |
586 | if full_filename.endswith(u'.py'): |
|
586 | if full_filename.endswith(u'.py'): | |
587 | self.log.info("Running file in user namespace: %s" % |
|
587 | self.log.info("Running file in user namespace: %s" % | |
588 | full_filename) |
|
588 | full_filename) | |
589 | # Ensure that __file__ is always defined to match Python behavior |
|
589 | # Ensure that __file__ is always defined to match Python behavior | |
590 | self.shell.user_ns['__file__'] = fname |
|
590 | self.shell.user_ns['__file__'] = fname | |
591 | try: |
|
591 | try: | |
592 | self.shell.safe_execfile(full_filename, self.shell.user_ns) |
|
592 | self.shell.safe_execfile(full_filename, self.shell.user_ns) | |
593 | finally: |
|
593 | finally: | |
594 | del self.shell.user_ns['__file__'] |
|
594 | del self.shell.user_ns['__file__'] | |
595 | elif full_filename.endswith('.ipy'): |
|
595 | elif full_filename.endswith('.ipy'): | |
596 | self.log.info("Running file in user namespace: %s" % |
|
596 | self.log.info("Running file in user namespace: %s" % | |
597 | full_filename) |
|
597 | full_filename) | |
598 | self.shell.safe_execfile_ipy(full_filename) |
|
598 | self.shell.safe_execfile_ipy(full_filename) | |
599 | else: |
|
599 | else: | |
600 | self.log.warn("File does not have a .py or .ipy extension: <%s>" |
|
600 | self.log.warn("File does not have a .py or .ipy extension: <%s>" | |
601 | % full_filename) |
|
601 | % full_filename) | |
602 | def _run_exec_files(self): |
|
602 | def _run_exec_files(self): | |
603 | try: |
|
603 | try: | |
604 | if hasattr(self.master_config.Global, 'exec_files'): |
|
604 | if hasattr(self.master_config.Global, 'exec_files'): | |
605 | self.log.debug("Running files in Global.exec_files...") |
|
605 | self.log.debug("Running files in Global.exec_files...") | |
606 | exec_files = self.master_config.Global.exec_files |
|
606 | exec_files = self.master_config.Global.exec_files | |
607 | for fname in exec_files: |
|
607 | for fname in exec_files: | |
608 | self._exec_file(fname) |
|
608 | self._exec_file(fname) | |
609 | except: |
|
609 | except: | |
610 | self.log.warn("Unknown error in handling Global.exec_files:") |
|
610 | self.log.warn("Unknown error in handling Global.exec_files:") | |
611 | self.shell.showtraceback() |
|
611 | self.shell.showtraceback() | |
612 |
|
612 | |||
613 | def _run_cmd_line_code(self): |
|
613 | def _run_cmd_line_code(self): | |
614 | if hasattr(self.master_config.Global, 'code_to_run'): |
|
614 | if hasattr(self.master_config.Global, 'code_to_run'): | |
615 | line = self.master_config.Global.code_to_run |
|
615 | line = self.master_config.Global.code_to_run | |
616 | try: |
|
616 | try: | |
617 | self.log.info("Running code given at command line (-c): %s" % |
|
617 | self.log.info("Running code given at command line (-c): %s" % | |
618 | line) |
|
618 | line) | |
619 | self.shell.runlines(line) |
|
619 | self.shell.runlines(line) | |
620 | except: |
|
620 | except: | |
621 | self.log.warn("Error in executing line in user namespace: %s" % |
|
621 | self.log.warn("Error in executing line in user namespace: %s" % | |
622 | line) |
|
622 | line) | |
623 | self.shell.showtraceback() |
|
623 | self.shell.showtraceback() | |
624 | return |
|
624 | return | |
625 | # Like Python itself, ignore the second if the first of these is present |
|
625 | # Like Python itself, ignore the second if the first of these is present | |
626 | try: |
|
626 | try: | |
627 | fname = self.extra_args[0] |
|
627 | fname = self.extra_args[0] | |
628 | except: |
|
628 | except: | |
629 | pass |
|
629 | pass | |
630 | else: |
|
630 | else: | |
631 | try: |
|
631 | try: | |
632 | self._exec_file(fname) |
|
632 | self._exec_file(fname) | |
633 | except: |
|
633 | except: | |
634 | self.log.warn("Error in executing file in user namespace: %s" % |
|
634 | self.log.warn("Error in executing file in user namespace: %s" % | |
635 | fname) |
|
635 | fname) | |
636 | self.shell.showtraceback() |
|
636 | self.shell.showtraceback() | |
637 |
|
637 | |||
638 | def start_app(self): |
|
638 | def start_app(self): | |
639 | if self.master_config.Global.interact: |
|
639 | if self.master_config.Global.interact: | |
640 | self.log.debug("Starting IPython's mainloop...") |
|
640 | self.log.debug("Starting IPython's mainloop...") | |
641 | self.shell.mainloop() |
|
641 | self.shell.mainloop() | |
642 | else: |
|
642 | else: | |
643 | self.log.debug("IPython not interactive, start_app is no-op...") |
|
643 | self.log.debug("IPython not interactive, start_app is no-op...") | |
644 |
|
644 | |||
645 |
|
645 | |||
646 | def load_default_config(ipython_dir=None): |
|
646 | def load_default_config(ipython_dir=None): | |
647 | """Load the default config file from the default ipython_dir. |
|
647 | """Load the default config file from the default ipython_dir. | |
648 |
|
648 | |||
649 | This is useful for embedded shells. |
|
649 | This is useful for embedded shells. | |
650 | """ |
|
650 | """ | |
651 | if ipython_dir is None: |
|
651 | if ipython_dir is None: | |
652 | ipython_dir = get_ipython_dir() |
|
652 | ipython_dir = get_ipython_dir() | |
653 | cl = PyFileConfigLoader(default_config_file_name, ipython_dir) |
|
653 | cl = PyFileConfigLoader(default_config_file_name, ipython_dir) | |
654 | config = cl.load_config() |
|
654 | config = cl.load_config() | |
655 | return config |
|
655 | return config | |
656 |
|
656 | |||
657 |
|
657 | |||
658 | def launch_new_instance(): |
|
658 | def launch_new_instance(): | |
659 | """Create and run a full blown IPython instance""" |
|
659 | """Create and run a full blown IPython instance""" | |
660 | app = IPythonApp() |
|
660 | app = IPythonApp() | |
661 | app.start() |
|
661 | app.start() | |
662 |
|
662 | |||
663 |
|
663 | |||
664 | if __name__ == '__main__': |
|
664 | if __name__ == '__main__': | |
665 | launch_new_instance() |
|
665 | launch_new_instance() |
@@ -1,174 +1,173 b'' | |||||
1 | """Global IPython app to support test running. |
|
1 | """Global IPython app to support test running. | |
2 |
|
2 | |||
3 | We must start our own ipython object and heavily muck with it so that all the |
|
3 | We must start our own ipython object and heavily muck with it so that all the | |
4 | modifications IPython makes to system behavior don't send the doctest machinery |
|
4 | modifications IPython makes to system behavior don't send the doctest machinery | |
5 | into a fit. This code should be considered a gross hack, but it gets the job |
|
5 | into a fit. This code should be considered a gross hack, but it gets the job | |
6 | done. |
|
6 | done. | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | from __future__ import absolute_import |
|
9 | from __future__ import absolute_import | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Copyright (C) 2009 The IPython Development Team |
|
12 | # Copyright (C) 2009 The IPython Development Team | |
13 | # |
|
13 | # | |
14 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | # Distributed under the terms of the BSD License. The full license is in | |
15 | # the file COPYING, distributed as part of this software. |
|
15 | # the file COPYING, distributed as part of this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | import __builtin__ |
|
22 | import __builtin__ | |
23 | import commands |
|
23 | import commands | |
24 | import os |
|
24 | import os | |
25 | import sys |
|
25 | import sys | |
26 |
|
26 | |||
27 | from . import tools |
|
27 | from . import tools | |
28 |
|
28 | |||
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | # Functions |
|
30 | # Functions | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 | # Hack to modify the %run command so we can sync the user's namespace with the |
|
33 | # Hack to modify the %run command so we can sync the user's namespace with the | |
34 | # test globals. Once we move over to a clean magic system, this will be done |
|
34 | # test globals. Once we move over to a clean magic system, this will be done | |
35 | # with much less ugliness. |
|
35 | # with much less ugliness. | |
36 |
|
36 | |||
37 | class py_file_finder(object): |
|
37 | class py_file_finder(object): | |
38 | def __init__(self,test_filename): |
|
38 | def __init__(self,test_filename): | |
39 | self.test_filename = test_filename |
|
39 | self.test_filename = test_filename | |
40 |
|
40 | |||
41 | def __call__(self,name): |
|
41 | def __call__(self,name): | |
42 | from IPython.utils.path import get_py_filename |
|
42 | from IPython.utils.path import get_py_filename | |
43 | try: |
|
43 | try: | |
44 | return get_py_filename(name) |
|
44 | return get_py_filename(name) | |
45 | except IOError: |
|
45 | except IOError: | |
46 | test_dir = os.path.dirname(self.test_filename) |
|
46 | test_dir = os.path.dirname(self.test_filename) | |
47 | new_path = os.path.join(test_dir,name) |
|
47 | new_path = os.path.join(test_dir,name) | |
48 | return get_py_filename(new_path) |
|
48 | return get_py_filename(new_path) | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | def _run_ns_sync(self,arg_s,runner=None): |
|
51 | def _run_ns_sync(self,arg_s,runner=None): | |
52 | """Modified version of %run that syncs testing namespaces. |
|
52 | """Modified version of %run that syncs testing namespaces. | |
53 |
|
53 | |||
54 | This is strictly needed for running doctests that call %run. |
|
54 | This is strictly needed for running doctests that call %run. | |
55 | """ |
|
55 | """ | |
56 | #print >> sys.stderr, 'in run_ns_sync', arg_s # dbg |
|
56 | #print >> sys.stderr, 'in run_ns_sync', arg_s # dbg | |
57 |
|
57 | |||
58 | _ip = get_ipython() |
|
58 | _ip = get_ipython() | |
59 | finder = py_file_finder(arg_s) |
|
59 | finder = py_file_finder(arg_s) | |
60 | out = _ip.magic_run_ori(arg_s,runner,finder) |
|
60 | out = _ip.magic_run_ori(arg_s,runner,finder) | |
61 | return out |
|
61 | return out | |
62 |
|
62 | |||
63 |
|
63 | |||
64 | class ipnsdict(dict): |
|
64 | class ipnsdict(dict): | |
65 | """A special subclass of dict for use as an IPython namespace in doctests. |
|
65 | """A special subclass of dict for use as an IPython namespace in doctests. | |
66 |
|
66 | |||
67 | This subclass adds a simple checkpointing capability so that when testing |
|
67 | This subclass adds a simple checkpointing capability so that when testing | |
68 | machinery clears it (we use it as the test execution context), it doesn't |
|
68 | machinery clears it (we use it as the test execution context), it doesn't | |
69 | get completely destroyed. |
|
69 | get completely destroyed. | |
70 | """ |
|
70 | """ | |
71 |
|
71 | |||
72 | def __init__(self,*a): |
|
72 | def __init__(self,*a): | |
73 | dict.__init__(self,*a) |
|
73 | dict.__init__(self,*a) | |
74 | self._savedict = {} |
|
74 | self._savedict = {} | |
75 |
|
75 | |||
76 | def clear(self): |
|
76 | def clear(self): | |
77 | dict.clear(self) |
|
77 | dict.clear(self) | |
78 | self.update(self._savedict) |
|
78 | self.update(self._savedict) | |
79 |
|
79 | |||
80 | def _checkpoint(self): |
|
80 | def _checkpoint(self): | |
81 | self._savedict.clear() |
|
81 | self._savedict.clear() | |
82 | self._savedict.update(self) |
|
82 | self._savedict.update(self) | |
83 |
|
83 | |||
84 | def update(self,other): |
|
84 | def update(self,other): | |
85 | self._checkpoint() |
|
85 | self._checkpoint() | |
86 | dict.update(self,other) |
|
86 | dict.update(self,other) | |
87 |
|
87 | |||
88 | # If '_' is in the namespace, python won't set it when executing code, |
|
88 | # If '_' is in the namespace, python won't set it when executing code, | |
89 | # and we have examples that test it. So we ensure that the namespace |
|
89 | # and we have examples that test it. So we ensure that the namespace | |
90 | # is always 'clean' of it before it's used for test code execution. |
|
90 | # is always 'clean' of it before it's used for test code execution. | |
91 | self.pop('_',None) |
|
91 | self.pop('_',None) | |
92 |
|
92 | |||
93 | # The builtins namespace must *always* be the real __builtin__ module, |
|
93 | # The builtins namespace must *always* be the real __builtin__ module, | |
94 | # else weird stuff happens. The main ipython code does have provisions |
|
94 | # else weird stuff happens. The main ipython code does have provisions | |
95 | # to ensure this after %run, but since in this class we do some |
|
95 | # to ensure this after %run, but since in this class we do some | |
96 | # aggressive low-level cleaning of the execution namespace, we need to |
|
96 | # aggressive low-level cleaning of the execution namespace, we need to | |
97 | # correct for that ourselves, to ensure consitency with the 'real' |
|
97 | # correct for that ourselves, to ensure consitency with the 'real' | |
98 | # ipython. |
|
98 | # ipython. | |
99 | self['__builtins__'] = __builtin__ |
|
99 | self['__builtins__'] = __builtin__ | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | def get_ipython(): |
|
102 | def get_ipython(): | |
103 | # This will get replaced by the real thing once we start IPython below |
|
103 | # This will get replaced by the real thing once we start IPython below | |
104 | return start_ipython() |
|
104 | return start_ipython() | |
105 |
|
105 | |||
106 |
|
106 | |||
107 | def start_ipython(): |
|
107 | def start_ipython(): | |
108 | """Start a global IPython shell, which we need for IPython-specific syntax. |
|
108 | """Start a global IPython shell, which we need for IPython-specific syntax. | |
109 | """ |
|
109 | """ | |
110 | global get_ipython |
|
110 | global get_ipython | |
111 |
|
111 | |||
112 | # This function should only ever run once! |
|
112 | # This function should only ever run once! | |
113 | if hasattr(start_ipython, 'already_called'): |
|
113 | if hasattr(start_ipython, 'already_called'): | |
114 | return |
|
114 | return | |
115 | start_ipython.already_called = True |
|
115 | start_ipython.already_called = True | |
116 |
|
116 | |||
117 | # Ok, first time we're called, go ahead |
|
117 | from IPython.frontend.terminal import interactiveshell | |
118 | from IPython.core import interactiveshell |
|
|||
119 |
|
118 | |||
120 | def xsys(cmd): |
|
119 | def xsys(cmd): | |
121 | """Execute a command and print its output. |
|
120 | """Execute a command and print its output. | |
122 |
|
121 | |||
123 | This is just a convenience function to replace the IPython system call |
|
122 | This is just a convenience function to replace the IPython system call | |
124 | with one that is more doctest-friendly. |
|
123 | with one that is more doctest-friendly. | |
125 | """ |
|
124 | """ | |
126 | cmd = _ip.var_expand(cmd,depth=1) |
|
125 | cmd = _ip.var_expand(cmd,depth=1) | |
127 | sys.stdout.write(commands.getoutput(cmd)) |
|
126 | sys.stdout.write(commands.getoutput(cmd)) | |
128 | sys.stdout.flush() |
|
127 | sys.stdout.flush() | |
129 |
|
128 | |||
130 | # Store certain global objects that IPython modifies |
|
129 | # Store certain global objects that IPython modifies | |
131 | _displayhook = sys.displayhook |
|
130 | _displayhook = sys.displayhook | |
132 | _excepthook = sys.excepthook |
|
131 | _excepthook = sys.excepthook | |
133 | _main = sys.modules.get('__main__') |
|
132 | _main = sys.modules.get('__main__') | |
134 |
|
133 | |||
135 | # Create custom argv and namespaces for our IPython to be test-friendly |
|
134 | # Create custom argv and namespaces for our IPython to be test-friendly | |
136 | config = tools.default_config() |
|
135 | config = tools.default_config() | |
137 |
|
136 | |||
138 | # Create and initialize our test-friendly IPython instance. |
|
137 | # Create and initialize our test-friendly IPython instance. | |
139 | shell = interactiveshell.InteractiveShell.instance( |
|
138 | shell = interactiveshell.TerminalInteractiveShell.instance( | |
140 | config=config, |
|
139 | config=config, | |
141 | user_ns=ipnsdict(), user_global_ns={} |
|
140 | user_ns=ipnsdict(), user_global_ns={} | |
142 | ) |
|
141 | ) | |
143 |
|
142 | |||
144 | # A few more tweaks needed for playing nicely with doctests... |
|
143 | # A few more tweaks needed for playing nicely with doctests... | |
145 |
|
144 | |||
146 | # These traps are normally only active for interactive use, set them |
|
145 | # These traps are normally only active for interactive use, set them | |
147 | # permanently since we'll be mocking interactive sessions. |
|
146 | # permanently since we'll be mocking interactive sessions. | |
148 | shell.builtin_trap.set() |
|
147 | shell.builtin_trap.set() | |
149 |
|
148 | |||
150 | # Set error printing to stdout so nose can doctest exceptions |
|
149 | # Set error printing to stdout so nose can doctest exceptions | |
151 | shell.InteractiveTB.out_stream = 'stdout' |
|
150 | shell.InteractiveTB.out_stream = 'stdout' | |
152 |
|
151 | |||
153 | # Modify the IPython system call with one that uses getoutput, so that we |
|
152 | # Modify the IPython system call with one that uses getoutput, so that we | |
154 | # can capture subcommands and print them to Python's stdout, otherwise the |
|
153 | # can capture subcommands and print them to Python's stdout, otherwise the | |
155 | # doctest machinery would miss them. |
|
154 | # doctest machinery would miss them. | |
156 | shell.system = xsys |
|
155 | shell.system = xsys | |
157 |
|
156 | |||
158 | # IPython is ready, now clean up some global state... |
|
157 | # IPython is ready, now clean up some global state... | |
159 |
|
158 | |||
160 | # Deactivate the various python system hooks added by ipython for |
|
159 | # Deactivate the various python system hooks added by ipython for | |
161 | # interactive convenience so we don't confuse the doctest system |
|
160 | # interactive convenience so we don't confuse the doctest system | |
162 | sys.modules['__main__'] = _main |
|
161 | sys.modules['__main__'] = _main | |
163 | sys.displayhook = _displayhook |
|
162 | sys.displayhook = _displayhook | |
164 | sys.excepthook = _excepthook |
|
163 | sys.excepthook = _excepthook | |
165 |
|
164 | |||
166 | # So that ipython magics and aliases can be doctested (they work by making |
|
165 | # So that ipython magics and aliases can be doctested (they work by making | |
167 | # a call into a global _ip object). Also make the top-level get_ipython |
|
166 | # a call into a global _ip object). Also make the top-level get_ipython | |
168 | # now return this without recursively calling here again. |
|
167 | # now return this without recursively calling here again. | |
169 | _ip = shell |
|
168 | _ip = shell | |
170 | get_ipython = _ip.get_ipython |
|
169 | get_ipython = _ip.get_ipython | |
171 | __builtin__._ip = _ip |
|
170 | __builtin__._ip = _ip | |
172 | __builtin__.get_ipython = get_ipython |
|
171 | __builtin__.get_ipython = get_ipython | |
173 |
|
172 | |||
174 | return _ip |
|
173 | return _ip |
@@ -1,277 +1,277 b'' | |||||
1 | """Generic testing tools that do NOT depend on Twisted. |
|
1 | """Generic testing tools that do NOT depend on Twisted. | |
2 |
|
2 | |||
3 | In particular, this module exposes a set of top-level assert* functions that |
|
3 | In particular, this module exposes a set of top-level assert* functions that | |
4 | can be used in place of nose.tools.assert* in method generators (the ones in |
|
4 | can be used in place of nose.tools.assert* in method generators (the ones in | |
5 | nose can not, at least as of nose 0.10.4). |
|
5 | nose can not, at least as of nose 0.10.4). | |
6 |
|
6 | |||
7 | Note: our testing package contains testing.util, which does depend on Twisted |
|
7 | Note: our testing package contains testing.util, which does depend on Twisted | |
8 | and provides utilities for tests that manage Deferreds. All testing support |
|
8 | and provides utilities for tests that manage Deferreds. All testing support | |
9 | tools that only depend on nose, IPython or the standard library should go here |
|
9 | tools that only depend on nose, IPython or the standard library should go here | |
10 | instead. |
|
10 | instead. | |
11 |
|
11 | |||
12 |
|
12 | |||
13 | Authors |
|
13 | Authors | |
14 | ------- |
|
14 | ------- | |
15 | - Fernando Perez <Fernando.Perez@berkeley.edu> |
|
15 | - Fernando Perez <Fernando.Perez@berkeley.edu> | |
16 | """ |
|
16 | """ | |
17 |
|
17 | |||
18 | from __future__ import absolute_import |
|
18 | from __future__ import absolute_import | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Copyright (C) 2009 The IPython Development Team |
|
21 | # Copyright (C) 2009 The IPython Development Team | |
22 | # |
|
22 | # | |
23 | # Distributed under the terms of the BSD License. The full license is in |
|
23 | # Distributed under the terms of the BSD License. The full license is in | |
24 | # the file COPYING, distributed as part of this software. |
|
24 | # the file COPYING, distributed as part of this software. | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 |
|
26 | |||
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 | # Imports |
|
28 | # Imports | |
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 |
|
30 | |||
31 | import os |
|
31 | import os | |
32 | import re |
|
32 | import re | |
33 | import sys |
|
33 | import sys | |
34 |
|
34 | |||
35 | try: |
|
35 | try: | |
36 | # These tools are used by parts of the runtime, so we make the nose |
|
36 | # These tools are used by parts of the runtime, so we make the nose | |
37 | # dependency optional at this point. Nose is a hard dependency to run the |
|
37 | # dependency optional at this point. Nose is a hard dependency to run the | |
38 | # test suite, but NOT to use ipython itself. |
|
38 | # test suite, but NOT to use ipython itself. | |
39 | import nose.tools as nt |
|
39 | import nose.tools as nt | |
40 | has_nose = True |
|
40 | has_nose = True | |
41 | except ImportError: |
|
41 | except ImportError: | |
42 | has_nose = False |
|
42 | has_nose = False | |
43 |
|
43 | |||
44 | from IPython.config.loader import Config |
|
44 | from IPython.config.loader import Config | |
45 | from IPython.utils.process import find_cmd, getoutputerror |
|
45 | from IPython.utils.process import find_cmd, getoutputerror | |
46 | from IPython.utils.text import list_strings |
|
46 | from IPython.utils.text import list_strings | |
47 | from IPython.utils.io import temp_pyfile |
|
47 | from IPython.utils.io import temp_pyfile | |
48 |
|
48 | |||
49 | from . import decorators as dec |
|
49 | from . import decorators as dec | |
50 |
|
50 | |||
51 | #----------------------------------------------------------------------------- |
|
51 | #----------------------------------------------------------------------------- | |
52 | # Globals |
|
52 | # Globals | |
53 | #----------------------------------------------------------------------------- |
|
53 | #----------------------------------------------------------------------------- | |
54 |
|
54 | |||
55 | # Make a bunch of nose.tools assert wrappers that can be used in test |
|
55 | # Make a bunch of nose.tools assert wrappers that can be used in test | |
56 | # generators. This will expose an assert* function for each one in nose.tools. |
|
56 | # generators. This will expose an assert* function for each one in nose.tools. | |
57 |
|
57 | |||
58 | _tpl = """ |
|
58 | _tpl = """ | |
59 | def %(name)s(*a,**kw): |
|
59 | def %(name)s(*a,**kw): | |
60 | return nt.%(name)s(*a,**kw) |
|
60 | return nt.%(name)s(*a,**kw) | |
61 | """ |
|
61 | """ | |
62 |
|
62 | |||
63 | if has_nose: |
|
63 | if has_nose: | |
64 | for _x in [a for a in dir(nt) if a.startswith('assert')]: |
|
64 | for _x in [a for a in dir(nt) if a.startswith('assert')]: | |
65 | exec _tpl % dict(name=_x) |
|
65 | exec _tpl % dict(name=_x) | |
66 |
|
66 | |||
67 | #----------------------------------------------------------------------------- |
|
67 | #----------------------------------------------------------------------------- | |
68 | # Functions and classes |
|
68 | # Functions and classes | |
69 | #----------------------------------------------------------------------------- |
|
69 | #----------------------------------------------------------------------------- | |
70 |
|
70 | |||
71 | # The docstring for full_path doctests differently on win32 (different path |
|
71 | # The docstring for full_path doctests differently on win32 (different path | |
72 | # separator) so just skip the doctest there. The example remains informative. |
|
72 | # separator) so just skip the doctest there. The example remains informative. | |
73 | doctest_deco = dec.skip_doctest if sys.platform == 'win32' else dec.null_deco |
|
73 | doctest_deco = dec.skip_doctest if sys.platform == 'win32' else dec.null_deco | |
74 |
|
74 | |||
75 | @doctest_deco |
|
75 | @doctest_deco | |
76 | def full_path(startPath,files): |
|
76 | def full_path(startPath,files): | |
77 | """Make full paths for all the listed files, based on startPath. |
|
77 | """Make full paths for all the listed files, based on startPath. | |
78 |
|
78 | |||
79 | Only the base part of startPath is kept, since this routine is typically |
|
79 | Only the base part of startPath is kept, since this routine is typically | |
80 | used with a script's __file__ variable as startPath. The base of startPath |
|
80 | used with a script's __file__ variable as startPath. The base of startPath | |
81 | is then prepended to all the listed files, forming the output list. |
|
81 | is then prepended to all the listed files, forming the output list. | |
82 |
|
82 | |||
83 | Parameters |
|
83 | Parameters | |
84 | ---------- |
|
84 | ---------- | |
85 | startPath : string |
|
85 | startPath : string | |
86 | Initial path to use as the base for the results. This path is split |
|
86 | Initial path to use as the base for the results. This path is split | |
87 | using os.path.split() and only its first component is kept. |
|
87 | using os.path.split() and only its first component is kept. | |
88 |
|
88 | |||
89 | files : string or list |
|
89 | files : string or list | |
90 | One or more files. |
|
90 | One or more files. | |
91 |
|
91 | |||
92 | Examples |
|
92 | Examples | |
93 | -------- |
|
93 | -------- | |
94 |
|
94 | |||
95 | >>> full_path('/foo/bar.py',['a.txt','b.txt']) |
|
95 | >>> full_path('/foo/bar.py',['a.txt','b.txt']) | |
96 | ['/foo/a.txt', '/foo/b.txt'] |
|
96 | ['/foo/a.txt', '/foo/b.txt'] | |
97 |
|
97 | |||
98 | >>> full_path('/foo',['a.txt','b.txt']) |
|
98 | >>> full_path('/foo',['a.txt','b.txt']) | |
99 | ['/a.txt', '/b.txt'] |
|
99 | ['/a.txt', '/b.txt'] | |
100 |
|
100 | |||
101 | If a single file is given, the output is still a list: |
|
101 | If a single file is given, the output is still a list: | |
102 | >>> full_path('/foo','a.txt') |
|
102 | >>> full_path('/foo','a.txt') | |
103 | ['/a.txt'] |
|
103 | ['/a.txt'] | |
104 | """ |
|
104 | """ | |
105 |
|
105 | |||
106 | files = list_strings(files) |
|
106 | files = list_strings(files) | |
107 | base = os.path.split(startPath)[0] |
|
107 | base = os.path.split(startPath)[0] | |
108 | return [ os.path.join(base,f) for f in files ] |
|
108 | return [ os.path.join(base,f) for f in files ] | |
109 |
|
109 | |||
110 |
|
110 | |||
111 | def parse_test_output(txt): |
|
111 | def parse_test_output(txt): | |
112 | """Parse the output of a test run and return errors, failures. |
|
112 | """Parse the output of a test run and return errors, failures. | |
113 |
|
113 | |||
114 | Parameters |
|
114 | Parameters | |
115 | ---------- |
|
115 | ---------- | |
116 | txt : str |
|
116 | txt : str | |
117 | Text output of a test run, assumed to contain a line of one of the |
|
117 | Text output of a test run, assumed to contain a line of one of the | |
118 | following forms:: |
|
118 | following forms:: | |
119 | 'FAILED (errors=1)' |
|
119 | 'FAILED (errors=1)' | |
120 | 'FAILED (failures=1)' |
|
120 | 'FAILED (failures=1)' | |
121 | 'FAILED (errors=1, failures=1)' |
|
121 | 'FAILED (errors=1, failures=1)' | |
122 |
|
122 | |||
123 | Returns |
|
123 | Returns | |
124 | ------- |
|
124 | ------- | |
125 | nerr, nfail: number of errors and failures. |
|
125 | nerr, nfail: number of errors and failures. | |
126 | """ |
|
126 | """ | |
127 |
|
127 | |||
128 | err_m = re.search(r'^FAILED \(errors=(\d+)\)', txt, re.MULTILINE) |
|
128 | err_m = re.search(r'^FAILED \(errors=(\d+)\)', txt, re.MULTILINE) | |
129 | if err_m: |
|
129 | if err_m: | |
130 | nerr = int(err_m.group(1)) |
|
130 | nerr = int(err_m.group(1)) | |
131 | nfail = 0 |
|
131 | nfail = 0 | |
132 | return nerr, nfail |
|
132 | return nerr, nfail | |
133 |
|
133 | |||
134 | fail_m = re.search(r'^FAILED \(failures=(\d+)\)', txt, re.MULTILINE) |
|
134 | fail_m = re.search(r'^FAILED \(failures=(\d+)\)', txt, re.MULTILINE) | |
135 | if fail_m: |
|
135 | if fail_m: | |
136 | nerr = 0 |
|
136 | nerr = 0 | |
137 | nfail = int(fail_m.group(1)) |
|
137 | nfail = int(fail_m.group(1)) | |
138 | return nerr, nfail |
|
138 | return nerr, nfail | |
139 |
|
139 | |||
140 | both_m = re.search(r'^FAILED \(errors=(\d+), failures=(\d+)\)', txt, |
|
140 | both_m = re.search(r'^FAILED \(errors=(\d+), failures=(\d+)\)', txt, | |
141 | re.MULTILINE) |
|
141 | re.MULTILINE) | |
142 | if both_m: |
|
142 | if both_m: | |
143 | nerr = int(both_m.group(1)) |
|
143 | nerr = int(both_m.group(1)) | |
144 | nfail = int(both_m.group(2)) |
|
144 | nfail = int(both_m.group(2)) | |
145 | return nerr, nfail |
|
145 | return nerr, nfail | |
146 |
|
146 | |||
147 | # If the input didn't match any of these forms, assume no error/failures |
|
147 | # If the input didn't match any of these forms, assume no error/failures | |
148 | return 0, 0 |
|
148 | return 0, 0 | |
149 |
|
149 | |||
150 |
|
150 | |||
151 | # So nose doesn't think this is a test |
|
151 | # So nose doesn't think this is a test | |
152 | parse_test_output.__test__ = False |
|
152 | parse_test_output.__test__ = False | |
153 |
|
153 | |||
154 |
|
154 | |||
155 | def default_argv(): |
|
155 | def default_argv(): | |
156 | """Return a valid default argv for creating testing instances of ipython""" |
|
156 | """Return a valid default argv for creating testing instances of ipython""" | |
157 |
|
157 | |||
158 | return ['--quick', # so no config file is loaded |
|
158 | return ['--quick', # so no config file is loaded | |
159 | # Other defaults to minimize side effects on stdout |
|
159 | # Other defaults to minimize side effects on stdout | |
160 | '--colors=NoColor', '--no-term-title','--no-banner', |
|
160 | '--colors=NoColor', '--no-term-title','--no-banner', | |
161 | '--autocall=0'] |
|
161 | '--autocall=0'] | |
162 |
|
162 | |||
163 |
|
163 | |||
164 | def default_config(): |
|
164 | def default_config(): | |
165 | """Return a config object with good defaults for testing.""" |
|
165 | """Return a config object with good defaults for testing.""" | |
166 | config = Config() |
|
166 | config = Config() | |
167 | config.InteractiveShell.colors = 'NoColor' |
|
167 | config.TerminalInteractiveShell.colors = 'NoColor' | |
168 | config.InteractiveShell.term_title = False, |
|
168 | config.TerminalTerminalInteractiveShell.term_title = False, | |
169 | config.InteractiveShell.autocall = 0 |
|
169 | config.TerminalInteractiveShell.autocall = 0 | |
170 | return config |
|
170 | return config | |
171 |
|
171 | |||
172 |
|
172 | |||
173 | def ipexec(fname, options=None): |
|
173 | def ipexec(fname, options=None): | |
174 | """Utility to call 'ipython filename'. |
|
174 | """Utility to call 'ipython filename'. | |
175 |
|
175 | |||
176 | Starts IPython witha minimal and safe configuration to make startup as fast |
|
176 | Starts IPython witha minimal and safe configuration to make startup as fast | |
177 | as possible. |
|
177 | as possible. | |
178 |
|
178 | |||
179 | Note that this starts IPython in a subprocess! |
|
179 | Note that this starts IPython in a subprocess! | |
180 |
|
180 | |||
181 | Parameters |
|
181 | Parameters | |
182 | ---------- |
|
182 | ---------- | |
183 | fname : str |
|
183 | fname : str | |
184 | Name of file to be executed (should have .py or .ipy extension). |
|
184 | Name of file to be executed (should have .py or .ipy extension). | |
185 |
|
185 | |||
186 | options : optional, list |
|
186 | options : optional, list | |
187 | Extra command-line flags to be passed to IPython. |
|
187 | Extra command-line flags to be passed to IPython. | |
188 |
|
188 | |||
189 | Returns |
|
189 | Returns | |
190 | ------- |
|
190 | ------- | |
191 | (stdout, stderr) of ipython subprocess. |
|
191 | (stdout, stderr) of ipython subprocess. | |
192 | """ |
|
192 | """ | |
193 | if options is None: options = [] |
|
193 | if options is None: options = [] | |
194 |
|
194 | |||
195 | # For these subprocess calls, eliminate all prompt printing so we only see |
|
195 | # For these subprocess calls, eliminate all prompt printing so we only see | |
196 | # output from script execution |
|
196 | # output from script execution | |
197 | prompt_opts = ['--prompt-in1=""', '--prompt-in2=""', '--prompt-out=""'] |
|
197 | prompt_opts = ['--prompt-in1=""', '--prompt-in2=""', '--prompt-out=""'] | |
198 | cmdargs = ' '.join(default_argv() + prompt_opts + options) |
|
198 | cmdargs = ' '.join(default_argv() + prompt_opts + options) | |
199 |
|
199 | |||
200 | _ip = get_ipython() |
|
200 | _ip = get_ipython() | |
201 | test_dir = os.path.dirname(__file__) |
|
201 | test_dir = os.path.dirname(__file__) | |
202 |
|
202 | |||
203 | ipython_cmd = find_cmd('ipython') |
|
203 | ipython_cmd = find_cmd('ipython') | |
204 | # Absolute path for filename |
|
204 | # Absolute path for filename | |
205 | full_fname = os.path.join(test_dir, fname) |
|
205 | full_fname = os.path.join(test_dir, fname) | |
206 | full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname) |
|
206 | full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname) | |
207 | #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg |
|
207 | #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg | |
208 | return getoutputerror(full_cmd) |
|
208 | return getoutputerror(full_cmd) | |
209 |
|
209 | |||
210 |
|
210 | |||
211 | def ipexec_validate(fname, expected_out, expected_err='', |
|
211 | def ipexec_validate(fname, expected_out, expected_err='', | |
212 | options=None): |
|
212 | options=None): | |
213 | """Utility to call 'ipython filename' and validate output/error. |
|
213 | """Utility to call 'ipython filename' and validate output/error. | |
214 |
|
214 | |||
215 | This function raises an AssertionError if the validation fails. |
|
215 | This function raises an AssertionError if the validation fails. | |
216 |
|
216 | |||
217 | Note that this starts IPython in a subprocess! |
|
217 | Note that this starts IPython in a subprocess! | |
218 |
|
218 | |||
219 | Parameters |
|
219 | Parameters | |
220 | ---------- |
|
220 | ---------- | |
221 | fname : str |
|
221 | fname : str | |
222 | Name of the file to be executed (should have .py or .ipy extension). |
|
222 | Name of the file to be executed (should have .py or .ipy extension). | |
223 |
|
223 | |||
224 | expected_out : str |
|
224 | expected_out : str | |
225 | Expected stdout of the process. |
|
225 | Expected stdout of the process. | |
226 |
|
226 | |||
227 | expected_err : optional, str |
|
227 | expected_err : optional, str | |
228 | Expected stderr of the process. |
|
228 | Expected stderr of the process. | |
229 |
|
229 | |||
230 | options : optional, list |
|
230 | options : optional, list | |
231 | Extra command-line flags to be passed to IPython. |
|
231 | Extra command-line flags to be passed to IPython. | |
232 |
|
232 | |||
233 | Returns |
|
233 | Returns | |
234 | ------- |
|
234 | ------- | |
235 | None |
|
235 | None | |
236 | """ |
|
236 | """ | |
237 |
|
237 | |||
238 | import nose.tools as nt |
|
238 | import nose.tools as nt | |
239 |
|
239 | |||
240 | out, err = ipexec(fname) |
|
240 | out, err = ipexec(fname) | |
241 | #print 'OUT', out # dbg |
|
241 | #print 'OUT', out # dbg | |
242 | #print 'ERR', err # dbg |
|
242 | #print 'ERR', err # dbg | |
243 | # If there are any errors, we must check those befor stdout, as they may be |
|
243 | # If there are any errors, we must check those befor stdout, as they may be | |
244 | # more informative than simply having an empty stdout. |
|
244 | # more informative than simply having an empty stdout. | |
245 | if err: |
|
245 | if err: | |
246 | if expected_err: |
|
246 | if expected_err: | |
247 | nt.assert_equals(err.strip(), expected_err.strip()) |
|
247 | nt.assert_equals(err.strip(), expected_err.strip()) | |
248 | else: |
|
248 | else: | |
249 | raise ValueError('Running file %r produced error: %r' % |
|
249 | raise ValueError('Running file %r produced error: %r' % | |
250 | (fname, err)) |
|
250 | (fname, err)) | |
251 | # If no errors or output on stderr was expected, match stdout |
|
251 | # If no errors or output on stderr was expected, match stdout | |
252 | nt.assert_equals(out.strip(), expected_out.strip()) |
|
252 | nt.assert_equals(out.strip(), expected_out.strip()) | |
253 |
|
253 | |||
254 |
|
254 | |||
255 | class TempFileMixin(object): |
|
255 | class TempFileMixin(object): | |
256 | """Utility class to create temporary Python/IPython files. |
|
256 | """Utility class to create temporary Python/IPython files. | |
257 |
|
257 | |||
258 | Meant as a mixin class for test cases.""" |
|
258 | Meant as a mixin class for test cases.""" | |
259 |
|
259 | |||
260 | def mktmp(self, src, ext='.py'): |
|
260 | def mktmp(self, src, ext='.py'): | |
261 | """Make a valid python temp file.""" |
|
261 | """Make a valid python temp file.""" | |
262 | fname, f = temp_pyfile(src, ext) |
|
262 | fname, f = temp_pyfile(src, ext) | |
263 | self.tmpfile = f |
|
263 | self.tmpfile = f | |
264 | self.fname = fname |
|
264 | self.fname = fname | |
265 |
|
265 | |||
266 | def teardown(self): |
|
266 | def teardown(self): | |
267 | if hasattr(self, 'tmpfile'): |
|
267 | if hasattr(self, 'tmpfile'): | |
268 | # If the tmpfile wasn't made because of skipped tests, like in |
|
268 | # If the tmpfile wasn't made because of skipped tests, like in | |
269 | # win32, there's nothing to cleanup. |
|
269 | # win32, there's nothing to cleanup. | |
270 | self.tmpfile.close() |
|
270 | self.tmpfile.close() | |
271 | try: |
|
271 | try: | |
272 | os.unlink(self.fname) |
|
272 | os.unlink(self.fname) | |
273 | except: |
|
273 | except: | |
274 | # On Windows, even though we close the file, we still can't |
|
274 | # On Windows, even though we close the file, we still can't | |
275 | # delete it. I have no clue why |
|
275 | # delete it. I have no clue why | |
276 | pass |
|
276 | pass | |
277 |
|
277 |
@@ -1,473 +1,484 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Utilities for working with strings and text. |
|
3 | Utilities for working with strings and text. | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (C) 2008-2009 The IPython Development Team |
|
7 | # Copyright (C) 2008-2009 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | import __main__ |
|
17 | import __main__ | |
18 |
|
18 | |||
19 | import os |
|
19 | import os | |
20 | import re |
|
20 | import re | |
21 | import shutil |
|
21 | import shutil | |
22 | import types |
|
22 | import types | |
23 |
|
23 | |||
24 | from IPython.external.path import path |
|
24 | from IPython.external.path import path | |
25 |
|
25 | |||
26 | from IPython.utils.generics import result_display |
|
26 | from IPython.utils.generics import result_display | |
27 | from IPython.utils.io import nlprint |
|
27 | from IPython.utils.io import nlprint | |
28 | from IPython.utils.data import flatten |
|
28 | from IPython.utils.data import flatten | |
29 |
|
29 | |||
30 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
31 | # Code |
|
31 | # Code | |
32 | #----------------------------------------------------------------------------- |
|
32 | #----------------------------------------------------------------------------- | |
33 |
|
33 | |||
34 | StringTypes = types.StringTypes |
|
34 | StringTypes = types.StringTypes | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | def unquote_ends(istr): |
|
37 | def unquote_ends(istr): | |
38 | """Remove a single pair of quotes from the endpoints of a string.""" |
|
38 | """Remove a single pair of quotes from the endpoints of a string.""" | |
39 |
|
39 | |||
40 | if not istr: |
|
40 | if not istr: | |
41 | return istr |
|
41 | return istr | |
42 | if (istr[0]=="'" and istr[-1]=="'") or \ |
|
42 | if (istr[0]=="'" and istr[-1]=="'") or \ | |
43 | (istr[0]=='"' and istr[-1]=='"'): |
|
43 | (istr[0]=='"' and istr[-1]=='"'): | |
44 | return istr[1:-1] |
|
44 | return istr[1:-1] | |
45 | else: |
|
45 | else: | |
46 | return istr |
|
46 | return istr | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | class LSString(str): |
|
49 | class LSString(str): | |
50 | """String derivative with a special access attributes. |
|
50 | """String derivative with a special access attributes. | |
51 |
|
51 | |||
52 | These are normal strings, but with the special attributes: |
|
52 | These are normal strings, but with the special attributes: | |
53 |
|
53 | |||
54 | .l (or .list) : value as list (split on newlines). |
|
54 | .l (or .list) : value as list (split on newlines). | |
55 | .n (or .nlstr): original value (the string itself). |
|
55 | .n (or .nlstr): original value (the string itself). | |
56 | .s (or .spstr): value as whitespace-separated string. |
|
56 | .s (or .spstr): value as whitespace-separated string. | |
57 | .p (or .paths): list of path objects |
|
57 | .p (or .paths): list of path objects | |
58 |
|
58 | |||
59 | Any values which require transformations are computed only once and |
|
59 | Any values which require transformations are computed only once and | |
60 | cached. |
|
60 | cached. | |
61 |
|
61 | |||
62 | Such strings are very useful to efficiently interact with the shell, which |
|
62 | Such strings are very useful to efficiently interact with the shell, which | |
63 | typically only understands whitespace-separated options for commands.""" |
|
63 | typically only understands whitespace-separated options for commands.""" | |
64 |
|
64 | |||
65 | def get_list(self): |
|
65 | def get_list(self): | |
66 | try: |
|
66 | try: | |
67 | return self.__list |
|
67 | return self.__list | |
68 | except AttributeError: |
|
68 | except AttributeError: | |
69 | self.__list = self.split('\n') |
|
69 | self.__list = self.split('\n') | |
70 | return self.__list |
|
70 | return self.__list | |
71 |
|
71 | |||
72 | l = list = property(get_list) |
|
72 | l = list = property(get_list) | |
73 |
|
73 | |||
74 | def get_spstr(self): |
|
74 | def get_spstr(self): | |
75 | try: |
|
75 | try: | |
76 | return self.__spstr |
|
76 | return self.__spstr | |
77 | except AttributeError: |
|
77 | except AttributeError: | |
78 | self.__spstr = self.replace('\n',' ') |
|
78 | self.__spstr = self.replace('\n',' ') | |
79 | return self.__spstr |
|
79 | return self.__spstr | |
80 |
|
80 | |||
81 | s = spstr = property(get_spstr) |
|
81 | s = spstr = property(get_spstr) | |
82 |
|
82 | |||
83 | def get_nlstr(self): |
|
83 | def get_nlstr(self): | |
84 | return self |
|
84 | return self | |
85 |
|
85 | |||
86 | n = nlstr = property(get_nlstr) |
|
86 | n = nlstr = property(get_nlstr) | |
87 |
|
87 | |||
88 | def get_paths(self): |
|
88 | def get_paths(self): | |
89 | try: |
|
89 | try: | |
90 | return self.__paths |
|
90 | return self.__paths | |
91 | except AttributeError: |
|
91 | except AttributeError: | |
92 | self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)] |
|
92 | self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)] | |
93 | return self.__paths |
|
93 | return self.__paths | |
94 |
|
94 | |||
95 | p = paths = property(get_paths) |
|
95 | p = paths = property(get_paths) | |
96 |
|
96 | |||
97 |
|
97 | |||
98 | def print_lsstring(arg): |
|
98 | def print_lsstring(arg): | |
99 | """ Prettier (non-repr-like) and more informative printer for LSString """ |
|
99 | """ Prettier (non-repr-like) and more informative printer for LSString """ | |
100 | print "LSString (.p, .n, .l, .s available). Value:" |
|
100 | print "LSString (.p, .n, .l, .s available). Value:" | |
101 | print arg |
|
101 | print arg | |
102 |
|
102 | |||
103 |
|
103 | |||
104 | print_lsstring = result_display.when_type(LSString)(print_lsstring) |
|
104 | print_lsstring = result_display.when_type(LSString)(print_lsstring) | |
105 |
|
105 | |||
106 |
|
106 | |||
107 | class SList(list): |
|
107 | class SList(list): | |
108 | """List derivative with a special access attributes. |
|
108 | """List derivative with a special access attributes. | |
109 |
|
109 | |||
110 | These are normal lists, but with the special attributes: |
|
110 | These are normal lists, but with the special attributes: | |
111 |
|
111 | |||
112 | .l (or .list) : value as list (the list itself). |
|
112 | .l (or .list) : value as list (the list itself). | |
113 | .n (or .nlstr): value as a string, joined on newlines. |
|
113 | .n (or .nlstr): value as a string, joined on newlines. | |
114 | .s (or .spstr): value as a string, joined on spaces. |
|
114 | .s (or .spstr): value as a string, joined on spaces. | |
115 | .p (or .paths): list of path objects |
|
115 | .p (or .paths): list of path objects | |
116 |
|
116 | |||
117 | Any values which require transformations are computed only once and |
|
117 | Any values which require transformations are computed only once and | |
118 | cached.""" |
|
118 | cached.""" | |
119 |
|
119 | |||
120 | def get_list(self): |
|
120 | def get_list(self): | |
121 | return self |
|
121 | return self | |
122 |
|
122 | |||
123 | l = list = property(get_list) |
|
123 | l = list = property(get_list) | |
124 |
|
124 | |||
125 | def get_spstr(self): |
|
125 | def get_spstr(self): | |
126 | try: |
|
126 | try: | |
127 | return self.__spstr |
|
127 | return self.__spstr | |
128 | except AttributeError: |
|
128 | except AttributeError: | |
129 | self.__spstr = ' '.join(self) |
|
129 | self.__spstr = ' '.join(self) | |
130 | return self.__spstr |
|
130 | return self.__spstr | |
131 |
|
131 | |||
132 | s = spstr = property(get_spstr) |
|
132 | s = spstr = property(get_spstr) | |
133 |
|
133 | |||
134 | def get_nlstr(self): |
|
134 | def get_nlstr(self): | |
135 | try: |
|
135 | try: | |
136 | return self.__nlstr |
|
136 | return self.__nlstr | |
137 | except AttributeError: |
|
137 | except AttributeError: | |
138 | self.__nlstr = '\n'.join(self) |
|
138 | self.__nlstr = '\n'.join(self) | |
139 | return self.__nlstr |
|
139 | return self.__nlstr | |
140 |
|
140 | |||
141 | n = nlstr = property(get_nlstr) |
|
141 | n = nlstr = property(get_nlstr) | |
142 |
|
142 | |||
143 | def get_paths(self): |
|
143 | def get_paths(self): | |
144 | try: |
|
144 | try: | |
145 | return self.__paths |
|
145 | return self.__paths | |
146 | except AttributeError: |
|
146 | except AttributeError: | |
147 | self.__paths = [path(p) for p in self if os.path.exists(p)] |
|
147 | self.__paths = [path(p) for p in self if os.path.exists(p)] | |
148 | return self.__paths |
|
148 | return self.__paths | |
149 |
|
149 | |||
150 | p = paths = property(get_paths) |
|
150 | p = paths = property(get_paths) | |
151 |
|
151 | |||
152 | def grep(self, pattern, prune = False, field = None): |
|
152 | def grep(self, pattern, prune = False, field = None): | |
153 | """ Return all strings matching 'pattern' (a regex or callable) |
|
153 | """ Return all strings matching 'pattern' (a regex or callable) | |
154 |
|
154 | |||
155 | This is case-insensitive. If prune is true, return all items |
|
155 | This is case-insensitive. If prune is true, return all items | |
156 | NOT matching the pattern. |
|
156 | NOT matching the pattern. | |
157 |
|
157 | |||
158 | If field is specified, the match must occur in the specified |
|
158 | If field is specified, the match must occur in the specified | |
159 | whitespace-separated field. |
|
159 | whitespace-separated field. | |
160 |
|
160 | |||
161 | Examples:: |
|
161 | Examples:: | |
162 |
|
162 | |||
163 | a.grep( lambda x: x.startswith('C') ) |
|
163 | a.grep( lambda x: x.startswith('C') ) | |
164 | a.grep('Cha.*log', prune=1) |
|
164 | a.grep('Cha.*log', prune=1) | |
165 | a.grep('chm', field=-1) |
|
165 | a.grep('chm', field=-1) | |
166 | """ |
|
166 | """ | |
167 |
|
167 | |||
168 | def match_target(s): |
|
168 | def match_target(s): | |
169 | if field is None: |
|
169 | if field is None: | |
170 | return s |
|
170 | return s | |
171 | parts = s.split() |
|
171 | parts = s.split() | |
172 | try: |
|
172 | try: | |
173 | tgt = parts[field] |
|
173 | tgt = parts[field] | |
174 | return tgt |
|
174 | return tgt | |
175 | except IndexError: |
|
175 | except IndexError: | |
176 | return "" |
|
176 | return "" | |
177 |
|
177 | |||
178 | if isinstance(pattern, basestring): |
|
178 | if isinstance(pattern, basestring): | |
179 | pred = lambda x : re.search(pattern, x, re.IGNORECASE) |
|
179 | pred = lambda x : re.search(pattern, x, re.IGNORECASE) | |
180 | else: |
|
180 | else: | |
181 | pred = pattern |
|
181 | pred = pattern | |
182 | if not prune: |
|
182 | if not prune: | |
183 | return SList([el for el in self if pred(match_target(el))]) |
|
183 | return SList([el for el in self if pred(match_target(el))]) | |
184 | else: |
|
184 | else: | |
185 | return SList([el for el in self if not pred(match_target(el))]) |
|
185 | return SList([el for el in self if not pred(match_target(el))]) | |
186 |
|
186 | |||
187 | def fields(self, *fields): |
|
187 | def fields(self, *fields): | |
188 | """ Collect whitespace-separated fields from string list |
|
188 | """ Collect whitespace-separated fields from string list | |
189 |
|
189 | |||
190 | Allows quick awk-like usage of string lists. |
|
190 | Allows quick awk-like usage of string lists. | |
191 |
|
191 | |||
192 | Example data (in var a, created by 'a = !ls -l'):: |
|
192 | Example data (in var a, created by 'a = !ls -l'):: | |
193 | -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog |
|
193 | -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog | |
194 | drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython |
|
194 | drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython | |
195 |
|
195 | |||
196 | a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+'] |
|
196 | a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+'] | |
197 | a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+'] |
|
197 | a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+'] | |
198 | (note the joining by space). |
|
198 | (note the joining by space). | |
199 | a.fields(-1) is ['ChangeLog', 'IPython'] |
|
199 | a.fields(-1) is ['ChangeLog', 'IPython'] | |
200 |
|
200 | |||
201 | IndexErrors are ignored. |
|
201 | IndexErrors are ignored. | |
202 |
|
202 | |||
203 | Without args, fields() just split()'s the strings. |
|
203 | Without args, fields() just split()'s the strings. | |
204 | """ |
|
204 | """ | |
205 | if len(fields) == 0: |
|
205 | if len(fields) == 0: | |
206 | return [el.split() for el in self] |
|
206 | return [el.split() for el in self] | |
207 |
|
207 | |||
208 | res = SList() |
|
208 | res = SList() | |
209 | for el in [f.split() for f in self]: |
|
209 | for el in [f.split() for f in self]: | |
210 | lineparts = [] |
|
210 | lineparts = [] | |
211 |
|
211 | |||
212 | for fd in fields: |
|
212 | for fd in fields: | |
213 | try: |
|
213 | try: | |
214 | lineparts.append(el[fd]) |
|
214 | lineparts.append(el[fd]) | |
215 | except IndexError: |
|
215 | except IndexError: | |
216 | pass |
|
216 | pass | |
217 | if lineparts: |
|
217 | if lineparts: | |
218 | res.append(" ".join(lineparts)) |
|
218 | res.append(" ".join(lineparts)) | |
219 |
|
219 | |||
220 | return res |
|
220 | return res | |
221 |
|
221 | |||
222 | def sort(self,field= None, nums = False): |
|
222 | def sort(self,field= None, nums = False): | |
223 | """ sort by specified fields (see fields()) |
|
223 | """ sort by specified fields (see fields()) | |
224 |
|
224 | |||
225 | Example:: |
|
225 | Example:: | |
226 | a.sort(1, nums = True) |
|
226 | a.sort(1, nums = True) | |
227 |
|
227 | |||
228 | Sorts a by second field, in numerical order (so that 21 > 3) |
|
228 | Sorts a by second field, in numerical order (so that 21 > 3) | |
229 |
|
229 | |||
230 | """ |
|
230 | """ | |
231 |
|
231 | |||
232 | #decorate, sort, undecorate |
|
232 | #decorate, sort, undecorate | |
233 | if field is not None: |
|
233 | if field is not None: | |
234 | dsu = [[SList([line]).fields(field), line] for line in self] |
|
234 | dsu = [[SList([line]).fields(field), line] for line in self] | |
235 | else: |
|
235 | else: | |
236 | dsu = [[line, line] for line in self] |
|
236 | dsu = [[line, line] for line in self] | |
237 | if nums: |
|
237 | if nums: | |
238 | for i in range(len(dsu)): |
|
238 | for i in range(len(dsu)): | |
239 | numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()]) |
|
239 | numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()]) | |
240 | try: |
|
240 | try: | |
241 | n = int(numstr) |
|
241 | n = int(numstr) | |
242 | except ValueError: |
|
242 | except ValueError: | |
243 | n = 0; |
|
243 | n = 0; | |
244 | dsu[i][0] = n |
|
244 | dsu[i][0] = n | |
245 |
|
245 | |||
246 |
|
246 | |||
247 | dsu.sort() |
|
247 | dsu.sort() | |
248 | return SList([t[1] for t in dsu]) |
|
248 | return SList([t[1] for t in dsu]) | |
249 |
|
249 | |||
250 |
|
250 | |||
251 | def print_slist(arg): |
|
251 | def print_slist(arg): | |
252 | """ Prettier (non-repr-like) and more informative printer for SList """ |
|
252 | """ Prettier (non-repr-like) and more informative printer for SList """ | |
253 | print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):" |
|
253 | print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):" | |
254 | if hasattr(arg, 'hideonce') and arg.hideonce: |
|
254 | if hasattr(arg, 'hideonce') and arg.hideonce: | |
255 | arg.hideonce = False |
|
255 | arg.hideonce = False | |
256 | return |
|
256 | return | |
257 |
|
257 | |||
258 | nlprint(arg) |
|
258 | nlprint(arg) | |
259 |
|
259 | |||
260 |
|
260 | |||
261 | print_slist = result_display.when_type(SList)(print_slist) |
|
261 | print_slist = result_display.when_type(SList)(print_slist) | |
262 |
|
262 | |||
263 |
|
263 | |||
264 | def esc_quotes(strng): |
|
264 | def esc_quotes(strng): | |
265 | """Return the input string with single and double quotes escaped out""" |
|
265 | """Return the input string with single and double quotes escaped out""" | |
266 |
|
266 | |||
267 | return strng.replace('"','\\"').replace("'","\\'") |
|
267 | return strng.replace('"','\\"').replace("'","\\'") | |
268 |
|
268 | |||
269 |
|
269 | |||
270 | def make_quoted_expr(s): |
|
270 | def make_quoted_expr(s): | |
271 | """Return string s in appropriate quotes, using raw string if possible. |
|
271 | """Return string s in appropriate quotes, using raw string if possible. | |
272 |
|
272 | |||
273 | XXX - example removed because it caused encoding errors in documentation |
|
273 | XXX - example removed because it caused encoding errors in documentation | |
274 | generation. We need a new example that doesn't contain invalid chars. |
|
274 | generation. We need a new example that doesn't contain invalid chars. | |
275 |
|
275 | |||
276 | Note the use of raw string and padding at the end to allow trailing |
|
276 | Note the use of raw string and padding at the end to allow trailing | |
277 | backslash. |
|
277 | backslash. | |
278 | """ |
|
278 | """ | |
279 |
|
279 | |||
280 | tail = '' |
|
280 | tail = '' | |
281 | tailpadding = '' |
|
281 | tailpadding = '' | |
282 | raw = '' |
|
282 | raw = '' | |
283 | if "\\" in s: |
|
283 | if "\\" in s: | |
284 | raw = 'r' |
|
284 | raw = 'r' | |
285 | if s.endswith('\\'): |
|
285 | if s.endswith('\\'): | |
286 | tail = '[:-1]' |
|
286 | tail = '[:-1]' | |
287 | tailpadding = '_' |
|
287 | tailpadding = '_' | |
288 | if '"' not in s: |
|
288 | if '"' not in s: | |
289 | quote = '"' |
|
289 | quote = '"' | |
290 | elif "'" not in s: |
|
290 | elif "'" not in s: | |
291 | quote = "'" |
|
291 | quote = "'" | |
292 | elif '"""' not in s and not s.endswith('"'): |
|
292 | elif '"""' not in s and not s.endswith('"'): | |
293 | quote = '"""' |
|
293 | quote = '"""' | |
294 | elif "'''" not in s and not s.endswith("'"): |
|
294 | elif "'''" not in s and not s.endswith("'"): | |
295 | quote = "'''" |
|
295 | quote = "'''" | |
296 | else: |
|
296 | else: | |
297 | # give up, backslash-escaped string will do |
|
297 | # give up, backslash-escaped string will do | |
298 | return '"%s"' % esc_quotes(s) |
|
298 | return '"%s"' % esc_quotes(s) | |
299 | res = raw + quote + s + tailpadding + quote + tail |
|
299 | res = raw + quote + s + tailpadding + quote + tail | |
300 | return res |
|
300 | return res | |
301 |
|
301 | |||
302 |
|
302 | |||
303 | def qw(words,flat=0,sep=None,maxsplit=-1): |
|
303 | def qw(words,flat=0,sep=None,maxsplit=-1): | |
304 | """Similar to Perl's qw() operator, but with some more options. |
|
304 | """Similar to Perl's qw() operator, but with some more options. | |
305 |
|
305 | |||
306 | qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit) |
|
306 | qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit) | |
307 |
|
307 | |||
308 | words can also be a list itself, and with flat=1, the output will be |
|
308 | words can also be a list itself, and with flat=1, the output will be | |
309 | recursively flattened. |
|
309 | recursively flattened. | |
310 |
|
310 | |||
311 | Examples: |
|
311 | Examples: | |
312 |
|
312 | |||
313 | >>> qw('1 2') |
|
313 | >>> qw('1 2') | |
314 | ['1', '2'] |
|
314 | ['1', '2'] | |
315 |
|
315 | |||
316 | >>> qw(['a b','1 2',['m n','p q']]) |
|
316 | >>> qw(['a b','1 2',['m n','p q']]) | |
317 | [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]] |
|
317 | [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]] | |
318 |
|
318 | |||
319 | >>> qw(['a b','1 2',['m n','p q']],flat=1) |
|
319 | >>> qw(['a b','1 2',['m n','p q']],flat=1) | |
320 | ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] |
|
320 | ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] | |
321 | """ |
|
321 | """ | |
322 |
|
322 | |||
323 | if type(words) in StringTypes: |
|
323 | if type(words) in StringTypes: | |
324 | return [word.strip() for word in words.split(sep,maxsplit) |
|
324 | return [word.strip() for word in words.split(sep,maxsplit) | |
325 | if word and not word.isspace() ] |
|
325 | if word and not word.isspace() ] | |
326 | if flat: |
|
326 | if flat: | |
327 | return flatten(map(qw,words,[1]*len(words))) |
|
327 | return flatten(map(qw,words,[1]*len(words))) | |
328 | return map(qw,words) |
|
328 | return map(qw,words) | |
329 |
|
329 | |||
330 |
|
330 | |||
331 | def qwflat(words,sep=None,maxsplit=-1): |
|
331 | def qwflat(words,sep=None,maxsplit=-1): | |
332 | """Calls qw(words) in flat mode. It's just a convenient shorthand.""" |
|
332 | """Calls qw(words) in flat mode. It's just a convenient shorthand.""" | |
333 | return qw(words,1,sep,maxsplit) |
|
333 | return qw(words,1,sep,maxsplit) | |
334 |
|
334 | |||
335 |
|
335 | |||
336 | def qw_lol(indata): |
|
336 | def qw_lol(indata): | |
337 | """qw_lol('a b') -> [['a','b']], |
|
337 | """qw_lol('a b') -> [['a','b']], | |
338 | otherwise it's just a call to qw(). |
|
338 | otherwise it's just a call to qw(). | |
339 |
|
339 | |||
340 | We need this to make sure the modules_some keys *always* end up as a |
|
340 | We need this to make sure the modules_some keys *always* end up as a | |
341 | list of lists.""" |
|
341 | list of lists.""" | |
342 |
|
342 | |||
343 | if type(indata) in StringTypes: |
|
343 | if type(indata) in StringTypes: | |
344 | return [qw(indata)] |
|
344 | return [qw(indata)] | |
345 | else: |
|
345 | else: | |
346 | return qw(indata) |
|
346 | return qw(indata) | |
347 |
|
347 | |||
348 |
|
348 | |||
349 | def grep(pat,list,case=1): |
|
349 | def grep(pat,list,case=1): | |
350 | """Simple minded grep-like function. |
|
350 | """Simple minded grep-like function. | |
351 | grep(pat,list) returns occurrences of pat in list, None on failure. |
|
351 | grep(pat,list) returns occurrences of pat in list, None on failure. | |
352 |
|
352 | |||
353 | It only does simple string matching, with no support for regexps. Use the |
|
353 | It only does simple string matching, with no support for regexps. Use the | |
354 | option case=0 for case-insensitive matching.""" |
|
354 | option case=0 for case-insensitive matching.""" | |
355 |
|
355 | |||
356 | # This is pretty crude. At least it should implement copying only references |
|
356 | # This is pretty crude. At least it should implement copying only references | |
357 | # to the original data in case it's big. Now it copies the data for output. |
|
357 | # to the original data in case it's big. Now it copies the data for output. | |
358 | out=[] |
|
358 | out=[] | |
359 | if case: |
|
359 | if case: | |
360 | for term in list: |
|
360 | for term in list: | |
361 | if term.find(pat)>-1: out.append(term) |
|
361 | if term.find(pat)>-1: out.append(term) | |
362 | else: |
|
362 | else: | |
363 | lpat=pat.lower() |
|
363 | lpat=pat.lower() | |
364 | for term in list: |
|
364 | for term in list: | |
365 | if term.lower().find(lpat)>-1: out.append(term) |
|
365 | if term.lower().find(lpat)>-1: out.append(term) | |
366 |
|
366 | |||
367 | if len(out): return out |
|
367 | if len(out): return out | |
368 | else: return None |
|
368 | else: return None | |
369 |
|
369 | |||
370 |
|
370 | |||
371 | def dgrep(pat,*opts): |
|
371 | def dgrep(pat,*opts): | |
372 | """Return grep() on dir()+dir(__builtins__). |
|
372 | """Return grep() on dir()+dir(__builtins__). | |
373 |
|
373 | |||
374 | A very common use of grep() when working interactively.""" |
|
374 | A very common use of grep() when working interactively.""" | |
375 |
|
375 | |||
376 | return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts) |
|
376 | return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts) | |
377 |
|
377 | |||
378 |
|
378 | |||
379 | def idgrep(pat): |
|
379 | def idgrep(pat): | |
380 | """Case-insensitive dgrep()""" |
|
380 | """Case-insensitive dgrep()""" | |
381 |
|
381 | |||
382 | return dgrep(pat,0) |
|
382 | return dgrep(pat,0) | |
383 |
|
383 | |||
384 |
|
384 | |||
385 | def igrep(pat,list): |
|
385 | def igrep(pat,list): | |
386 | """Synonym for case-insensitive grep.""" |
|
386 | """Synonym for case-insensitive grep.""" | |
387 |
|
387 | |||
388 | return grep(pat,list,case=0) |
|
388 | return grep(pat,list,case=0) | |
389 |
|
389 | |||
390 |
|
390 | |||
391 | def indent(str,nspaces=4,ntabs=0): |
|
391 | def indent(str,nspaces=4,ntabs=0): | |
392 | """Indent a string a given number of spaces or tabstops. |
|
392 | """Indent a string a given number of spaces or tabstops. | |
393 |
|
393 | |||
394 | indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. |
|
394 | indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. | |
395 | """ |
|
395 | """ | |
396 | if str is None: |
|
396 | if str is None: | |
397 | return |
|
397 | return | |
398 | ind = '\t'*ntabs+' '*nspaces |
|
398 | ind = '\t'*ntabs+' '*nspaces | |
399 | outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind)) |
|
399 | outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind)) | |
400 | if outstr.endswith(os.linesep+ind): |
|
400 | if outstr.endswith(os.linesep+ind): | |
401 | return outstr[:-len(ind)] |
|
401 | return outstr[:-len(ind)] | |
402 | else: |
|
402 | else: | |
403 | return outstr |
|
403 | return outstr | |
404 |
|
404 | |||
405 | def native_line_ends(filename,backup=1): |
|
405 | def native_line_ends(filename,backup=1): | |
406 | """Convert (in-place) a file to line-ends native to the current OS. |
|
406 | """Convert (in-place) a file to line-ends native to the current OS. | |
407 |
|
407 | |||
408 | If the optional backup argument is given as false, no backup of the |
|
408 | If the optional backup argument is given as false, no backup of the | |
409 | original file is left. """ |
|
409 | original file is left. """ | |
410 |
|
410 | |||
411 | backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'} |
|
411 | backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'} | |
412 |
|
412 | |||
413 | bak_filename = filename + backup_suffixes[os.name] |
|
413 | bak_filename = filename + backup_suffixes[os.name] | |
414 |
|
414 | |||
415 | original = open(filename).read() |
|
415 | original = open(filename).read() | |
416 | shutil.copy2(filename,bak_filename) |
|
416 | shutil.copy2(filename,bak_filename) | |
417 | try: |
|
417 | try: | |
418 | new = open(filename,'wb') |
|
418 | new = open(filename,'wb') | |
419 | new.write(os.linesep.join(original.splitlines())) |
|
419 | new.write(os.linesep.join(original.splitlines())) | |
420 | new.write(os.linesep) # ALWAYS put an eol at the end of the file |
|
420 | new.write(os.linesep) # ALWAYS put an eol at the end of the file | |
421 | new.close() |
|
421 | new.close() | |
422 | except: |
|
422 | except: | |
423 | os.rename(bak_filename,filename) |
|
423 | os.rename(bak_filename,filename) | |
424 | if not backup: |
|
424 | if not backup: | |
425 | try: |
|
425 | try: | |
426 | os.remove(bak_filename) |
|
426 | os.remove(bak_filename) | |
427 | except: |
|
427 | except: | |
428 | pass |
|
428 | pass | |
429 |
|
429 | |||
430 |
|
430 | |||
431 | def list_strings(arg): |
|
431 | def list_strings(arg): | |
432 | """Always return a list of strings, given a string or list of strings |
|
432 | """Always return a list of strings, given a string or list of strings | |
433 | as input. |
|
433 | as input. | |
434 |
|
434 | |||
435 | :Examples: |
|
435 | :Examples: | |
436 |
|
436 | |||
437 | In [7]: list_strings('A single string') |
|
437 | In [7]: list_strings('A single string') | |
438 | Out[7]: ['A single string'] |
|
438 | Out[7]: ['A single string'] | |
439 |
|
439 | |||
440 | In [8]: list_strings(['A single string in a list']) |
|
440 | In [8]: list_strings(['A single string in a list']) | |
441 | Out[8]: ['A single string in a list'] |
|
441 | Out[8]: ['A single string in a list'] | |
442 |
|
442 | |||
443 | In [9]: list_strings(['A','list','of','strings']) |
|
443 | In [9]: list_strings(['A','list','of','strings']) | |
444 | Out[9]: ['A', 'list', 'of', 'strings'] |
|
444 | Out[9]: ['A', 'list', 'of', 'strings'] | |
445 | """ |
|
445 | """ | |
446 |
|
446 | |||
447 | if isinstance(arg,basestring): return [arg] |
|
447 | if isinstance(arg,basestring): return [arg] | |
448 | else: return arg |
|
448 | else: return arg | |
449 |
|
449 | |||
450 |
|
450 | |||
451 | def marquee(txt='',width=78,mark='*'): |
|
451 | def marquee(txt='',width=78,mark='*'): | |
452 | """Return the input string centered in a 'marquee'. |
|
452 | """Return the input string centered in a 'marquee'. | |
453 |
|
453 | |||
454 | :Examples: |
|
454 | :Examples: | |
455 |
|
455 | |||
456 | In [16]: marquee('A test',40) |
|
456 | In [16]: marquee('A test',40) | |
457 | Out[16]: '**************** A test ****************' |
|
457 | Out[16]: '**************** A test ****************' | |
458 |
|
458 | |||
459 | In [17]: marquee('A test',40,'-') |
|
459 | In [17]: marquee('A test',40,'-') | |
460 | Out[17]: '---------------- A test ----------------' |
|
460 | Out[17]: '---------------- A test ----------------' | |
461 |
|
461 | |||
462 | In [18]: marquee('A test',40,' ') |
|
462 | In [18]: marquee('A test',40,' ') | |
463 | Out[18]: ' A test ' |
|
463 | Out[18]: ' A test ' | |
464 |
|
464 | |||
465 | """ |
|
465 | """ | |
466 | if not txt: |
|
466 | if not txt: | |
467 | return (mark*width)[:width] |
|
467 | return (mark*width)[:width] | |
468 | nmark = (width-len(txt)-2)/len(mark)/2 |
|
468 | nmark = (width-len(txt)-2)/len(mark)/2 | |
469 | if nmark < 0: nmark =0 |
|
469 | if nmark < 0: nmark =0 | |
470 | marks = mark*nmark |
|
470 | marks = mark*nmark | |
471 | return '%s %s %s' % (marks,txt,marks) |
|
471 | return '%s %s %s' % (marks,txt,marks) | |
472 |
|
472 | |||
473 |
|
473 | |||
|
474 | ini_spaces_re = re.compile(r'^(\s+)') | |||
|
475 | ||||
|
476 | def num_ini_spaces(strng): | |||
|
477 | """Return the number of initial spaces in a string""" | |||
|
478 | ||||
|
479 | ini_spaces = ini_spaces_re.match(strng) | |||
|
480 | if ini_spaces: | |||
|
481 | return ini_spaces.end() | |||
|
482 | else: | |||
|
483 | return 0 | |||
|
484 |
General Comments 0
You need to be logged in to leave comments.
Login now