##// END OF EJS Templates
Remove usage of deprecated .message attribute of exceptions....
Brian Granger -
Show More
@@ -1,2477 +1,2477 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Main IPython Component
3 Main IPython Component
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
9 # Copyright (C) 2008-2009 The IPython Development Team
9 # Copyright (C) 2008-2009 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 from __future__ import with_statement
19 from __future__ import with_statement
20
20
21 import __builtin__
21 import __builtin__
22 import StringIO
22 import StringIO
23 import bdb
23 import bdb
24 import codeop
24 import codeop
25 import exceptions
25 import exceptions
26 import new
26 import new
27 import os
27 import os
28 import re
28 import re
29 import string
29 import string
30 import sys
30 import sys
31 import tempfile
31 import tempfile
32 from contextlib import nested
32 from contextlib import nested
33
33
34 from IPython.core import ultratb
34 from IPython.core import ultratb
35 from IPython.core import debugger, oinspect
35 from IPython.core import debugger, oinspect
36 from IPython.core import shadowns
36 from IPython.core import shadowns
37 from IPython.core import history as ipcorehist
37 from IPython.core import history as ipcorehist
38 from IPython.core import prefilter
38 from IPython.core import prefilter
39 from IPython.core.alias import AliasManager
39 from IPython.core.alias import AliasManager
40 from IPython.core.builtin_trap import BuiltinTrap
40 from IPython.core.builtin_trap import BuiltinTrap
41 from IPython.core.display_trap import DisplayTrap
41 from IPython.core.display_trap import DisplayTrap
42 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
42 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
43 from IPython.core.logger import Logger
43 from IPython.core.logger import Logger
44 from IPython.core.magic import Magic
44 from IPython.core.magic import Magic
45 from IPython.core.prompts import CachedOutput
45 from IPython.core.prompts import CachedOutput
46 from IPython.core.prefilter import PrefilterManager
46 from IPython.core.prefilter import PrefilterManager
47 from IPython.core.component import Component
47 from IPython.core.component import Component
48 from IPython.core.usage import interactive_usage, default_banner
48 from IPython.core.usage import interactive_usage, default_banner
49 from IPython.core.error import TryNext, UsageError
49 from IPython.core.error import TryNext, UsageError
50
50
51 from IPython.utils import pickleshare
51 from IPython.utils import pickleshare
52 from IPython.external.Itpl import ItplNS
52 from IPython.external.Itpl import ItplNS
53 from IPython.lib.backgroundjobs import BackgroundJobManager
53 from IPython.lib.backgroundjobs import BackgroundJobManager
54 from IPython.utils.ipstruct import Struct
54 from IPython.utils.ipstruct import Struct
55 from IPython.utils import PyColorize
55 from IPython.utils import PyColorize
56 from IPython.utils.genutils import *
56 from IPython.utils.genutils import *
57 from IPython.utils.genutils import get_ipython_dir
57 from IPython.utils.genutils import get_ipython_dir
58 from IPython.utils.platutils import toggle_set_term_title, set_term_title
58 from IPython.utils.platutils import toggle_set_term_title, set_term_title
59 from IPython.utils.strdispatch import StrDispatch
59 from IPython.utils.strdispatch import StrDispatch
60 from IPython.utils.syspathcontext import prepended_to_syspath
60 from IPython.utils.syspathcontext import prepended_to_syspath
61
61
62 # from IPython.utils import growl
62 # from IPython.utils import growl
63 # growl.start("IPython")
63 # growl.start("IPython")
64
64
65 from IPython.utils.traitlets import (
65 from IPython.utils.traitlets import (
66 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode
66 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode
67 )
67 )
68
68
69 #-----------------------------------------------------------------------------
69 #-----------------------------------------------------------------------------
70 # Globals
70 # Globals
71 #-----------------------------------------------------------------------------
71 #-----------------------------------------------------------------------------
72
72
73
73
74 # store the builtin raw_input globally, and use this always, in case user code
74 # store the builtin raw_input globally, and use this always, in case user code
75 # overwrites it (like wx.py.PyShell does)
75 # overwrites it (like wx.py.PyShell does)
76 raw_input_original = raw_input
76 raw_input_original = raw_input
77
77
78 # compiled regexps for autoindent management
78 # compiled regexps for autoindent management
79 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
79 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
80
80
81
81
82 #-----------------------------------------------------------------------------
82 #-----------------------------------------------------------------------------
83 # Utilities
83 # Utilities
84 #-----------------------------------------------------------------------------
84 #-----------------------------------------------------------------------------
85
85
86
86
87 ini_spaces_re = re.compile(r'^(\s+)')
87 ini_spaces_re = re.compile(r'^(\s+)')
88
88
89
89
90 def num_ini_spaces(strng):
90 def num_ini_spaces(strng):
91 """Return the number of initial spaces in a string"""
91 """Return the number of initial spaces in a string"""
92
92
93 ini_spaces = ini_spaces_re.match(strng)
93 ini_spaces = ini_spaces_re.match(strng)
94 if ini_spaces:
94 if ini_spaces:
95 return ini_spaces.end()
95 return ini_spaces.end()
96 else:
96 else:
97 return 0
97 return 0
98
98
99
99
100 def softspace(file, newvalue):
100 def softspace(file, newvalue):
101 """Copied from code.py, to remove the dependency"""
101 """Copied from code.py, to remove the dependency"""
102
102
103 oldvalue = 0
103 oldvalue = 0
104 try:
104 try:
105 oldvalue = file.softspace
105 oldvalue = file.softspace
106 except AttributeError:
106 except AttributeError:
107 pass
107 pass
108 try:
108 try:
109 file.softspace = newvalue
109 file.softspace = newvalue
110 except (AttributeError, TypeError):
110 except (AttributeError, TypeError):
111 # "attribute-less object" or "read-only attributes"
111 # "attribute-less object" or "read-only attributes"
112 pass
112 pass
113 return oldvalue
113 return oldvalue
114
114
115
115
116 class SpaceInInput(exceptions.Exception): pass
116 class SpaceInInput(exceptions.Exception): pass
117
117
118 class Bunch: pass
118 class Bunch: pass
119
119
120 class InputList(list):
120 class InputList(list):
121 """Class to store user input.
121 """Class to store user input.
122
122
123 It's basically a list, but slices return a string instead of a list, thus
123 It's basically a list, but slices return a string instead of a list, thus
124 allowing things like (assuming 'In' is an instance):
124 allowing things like (assuming 'In' is an instance):
125
125
126 exec In[4:7]
126 exec In[4:7]
127
127
128 or
128 or
129
129
130 exec In[5:9] + In[14] + In[21:25]"""
130 exec In[5:9] + In[14] + In[21:25]"""
131
131
132 def __getslice__(self,i,j):
132 def __getslice__(self,i,j):
133 return ''.join(list.__getslice__(self,i,j))
133 return ''.join(list.__getslice__(self,i,j))
134
134
135
135
136 class SyntaxTB(ultratb.ListTB):
136 class SyntaxTB(ultratb.ListTB):
137 """Extension which holds some state: the last exception value"""
137 """Extension which holds some state: the last exception value"""
138
138
139 def __init__(self,color_scheme = 'NoColor'):
139 def __init__(self,color_scheme = 'NoColor'):
140 ultratb.ListTB.__init__(self,color_scheme)
140 ultratb.ListTB.__init__(self,color_scheme)
141 self.last_syntax_error = None
141 self.last_syntax_error = None
142
142
143 def __call__(self, etype, value, elist):
143 def __call__(self, etype, value, elist):
144 self.last_syntax_error = value
144 self.last_syntax_error = value
145 ultratb.ListTB.__call__(self,etype,value,elist)
145 ultratb.ListTB.__call__(self,etype,value,elist)
146
146
147 def clear_err_state(self):
147 def clear_err_state(self):
148 """Return the current error state and clear it"""
148 """Return the current error state and clear it"""
149 e = self.last_syntax_error
149 e = self.last_syntax_error
150 self.last_syntax_error = None
150 self.last_syntax_error = None
151 return e
151 return e
152
152
153
153
154 def get_default_editor():
154 def get_default_editor():
155 try:
155 try:
156 ed = os.environ['EDITOR']
156 ed = os.environ['EDITOR']
157 except KeyError:
157 except KeyError:
158 if os.name == 'posix':
158 if os.name == 'posix':
159 ed = 'vi' # the only one guaranteed to be there!
159 ed = 'vi' # the only one guaranteed to be there!
160 else:
160 else:
161 ed = 'notepad' # same in Windows!
161 ed = 'notepad' # same in Windows!
162 return ed
162 return ed
163
163
164
164
165 class SeparateStr(Str):
165 class SeparateStr(Str):
166 """A Str subclass to validate separate_in, separate_out, etc.
166 """A Str subclass to validate separate_in, separate_out, etc.
167
167
168 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
168 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
169 """
169 """
170
170
171 def validate(self, obj, value):
171 def validate(self, obj, value):
172 if value == '0': value = ''
172 if value == '0': value = ''
173 value = value.replace('\\n','\n')
173 value = value.replace('\\n','\n')
174 return super(SeparateStr, self).validate(obj, value)
174 return super(SeparateStr, self).validate(obj, value)
175
175
176
176
177 #-----------------------------------------------------------------------------
177 #-----------------------------------------------------------------------------
178 # Main IPython class
178 # Main IPython class
179 #-----------------------------------------------------------------------------
179 #-----------------------------------------------------------------------------
180
180
181
181
182 class InteractiveShell(Component, Magic):
182 class InteractiveShell(Component, Magic):
183 """An enhanced, interactive shell for Python."""
183 """An enhanced, interactive shell for Python."""
184
184
185 autocall = Enum((0,1,2), config=True)
185 autocall = Enum((0,1,2), config=True)
186 autoedit_syntax = CBool(False, config=True)
186 autoedit_syntax = CBool(False, config=True)
187 autoindent = CBool(True, config=True)
187 autoindent = CBool(True, config=True)
188 automagic = CBool(True, config=True)
188 automagic = CBool(True, config=True)
189 banner = Str('')
189 banner = Str('')
190 banner1 = Str(default_banner, config=True)
190 banner1 = Str(default_banner, config=True)
191 banner2 = Str('', config=True)
191 banner2 = Str('', config=True)
192 cache_size = Int(1000, config=True)
192 cache_size = Int(1000, config=True)
193 color_info = CBool(True, config=True)
193 color_info = CBool(True, config=True)
194 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
194 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
195 default_value='LightBG', config=True)
195 default_value='LightBG', config=True)
196 confirm_exit = CBool(True, config=True)
196 confirm_exit = CBool(True, config=True)
197 debug = CBool(False, config=True)
197 debug = CBool(False, config=True)
198 deep_reload = CBool(False, config=True)
198 deep_reload = CBool(False, config=True)
199 # This display_banner only controls whether or not self.show_banner()
199 # This display_banner only controls whether or not self.show_banner()
200 # is called when mainloop/interact are called. The default is False
200 # is called when mainloop/interact are called. The default is False
201 # because for the terminal based application, the banner behavior
201 # because for the terminal based application, the banner behavior
202 # is controlled by Global.display_banner, which IPythonApp looks at
202 # is controlled by Global.display_banner, which IPythonApp looks at
203 # to determine if *it* should call show_banner() by hand or not.
203 # to determine if *it* should call show_banner() by hand or not.
204 display_banner = CBool(False) # This isn't configurable!
204 display_banner = CBool(False) # This isn't configurable!
205 embedded = CBool(False)
205 embedded = CBool(False)
206 embedded_active = CBool(False)
206 embedded_active = CBool(False)
207 editor = Str(get_default_editor(), config=True)
207 editor = Str(get_default_editor(), config=True)
208 filename = Str("<ipython console>")
208 filename = Str("<ipython console>")
209 ipythondir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
209 ipythondir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
210 logstart = CBool(False, config=True)
210 logstart = CBool(False, config=True)
211 logfile = Str('', config=True)
211 logfile = Str('', config=True)
212 logappend = Str('', config=True)
212 logappend = Str('', config=True)
213 object_info_string_level = Enum((0,1,2), default_value=0,
213 object_info_string_level = Enum((0,1,2), default_value=0,
214 config=True)
214 config=True)
215 pager = Str('less', config=True)
215 pager = Str('less', config=True)
216 pdb = CBool(False, config=True)
216 pdb = CBool(False, config=True)
217 pprint = CBool(True, config=True)
217 pprint = CBool(True, config=True)
218 profile = Str('', config=True)
218 profile = Str('', config=True)
219 prompt_in1 = Str('In [\\#]: ', config=True)
219 prompt_in1 = Str('In [\\#]: ', config=True)
220 prompt_in2 = Str(' .\\D.: ', config=True)
220 prompt_in2 = Str(' .\\D.: ', config=True)
221 prompt_out = Str('Out[\\#]: ', config=True)
221 prompt_out = Str('Out[\\#]: ', config=True)
222 prompts_pad_left = CBool(True, config=True)
222 prompts_pad_left = CBool(True, config=True)
223 quiet = CBool(False, config=True)
223 quiet = CBool(False, config=True)
224
224
225 readline_use = CBool(True, config=True)
225 readline_use = CBool(True, config=True)
226 readline_merge_completions = CBool(True, config=True)
226 readline_merge_completions = CBool(True, config=True)
227 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
227 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
228 readline_remove_delims = Str('-/~', config=True)
228 readline_remove_delims = Str('-/~', config=True)
229 readline_parse_and_bind = List([
229 readline_parse_and_bind = List([
230 'tab: complete',
230 'tab: complete',
231 '"\C-l": possible-completions',
231 '"\C-l": possible-completions',
232 'set show-all-if-ambiguous on',
232 'set show-all-if-ambiguous on',
233 '"\C-o": tab-insert',
233 '"\C-o": tab-insert',
234 '"\M-i": " "',
234 '"\M-i": " "',
235 '"\M-o": "\d\d\d\d"',
235 '"\M-o": "\d\d\d\d"',
236 '"\M-I": "\d\d\d\d"',
236 '"\M-I": "\d\d\d\d"',
237 '"\C-r": reverse-search-history',
237 '"\C-r": reverse-search-history',
238 '"\C-s": forward-search-history',
238 '"\C-s": forward-search-history',
239 '"\C-p": history-search-backward',
239 '"\C-p": history-search-backward',
240 '"\C-n": history-search-forward',
240 '"\C-n": history-search-forward',
241 '"\e[A": history-search-backward',
241 '"\e[A": history-search-backward',
242 '"\e[B": history-search-forward',
242 '"\e[B": history-search-forward',
243 '"\C-k": kill-line',
243 '"\C-k": kill-line',
244 '"\C-u": unix-line-discard',
244 '"\C-u": unix-line-discard',
245 ], allow_none=False, config=True)
245 ], allow_none=False, config=True)
246
246
247 screen_length = Int(0, config=True)
247 screen_length = Int(0, config=True)
248
248
249 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
249 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
250 separate_in = SeparateStr('\n', config=True)
250 separate_in = SeparateStr('\n', config=True)
251 separate_out = SeparateStr('', config=True)
251 separate_out = SeparateStr('', config=True)
252 separate_out2 = SeparateStr('', config=True)
252 separate_out2 = SeparateStr('', config=True)
253
253
254 system_header = Str('IPython system call: ', config=True)
254 system_header = Str('IPython system call: ', config=True)
255 system_verbose = CBool(False, config=True)
255 system_verbose = CBool(False, config=True)
256 term_title = CBool(False, config=True)
256 term_title = CBool(False, config=True)
257 wildcards_case_sensitive = CBool(True, config=True)
257 wildcards_case_sensitive = CBool(True, config=True)
258 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
258 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
259 default_value='Context', config=True)
259 default_value='Context', config=True)
260
260
261 autoexec = List(allow_none=False)
261 autoexec = List(allow_none=False)
262
262
263 # class attribute to indicate whether the class supports threads or not.
263 # class attribute to indicate whether the class supports threads or not.
264 # Subclasses with thread support should override this as needed.
264 # Subclasses with thread support should override this as needed.
265 isthreaded = False
265 isthreaded = False
266
266
267 def __init__(self, parent=None, config=None, ipythondir=None, usage=None,
267 def __init__(self, parent=None, config=None, ipythondir=None, usage=None,
268 user_ns=None, user_global_ns=None,
268 user_ns=None, user_global_ns=None,
269 banner1=None, banner2=None, display_banner=None,
269 banner1=None, banner2=None, display_banner=None,
270 custom_exceptions=((),None)):
270 custom_exceptions=((),None)):
271
271
272 # This is where traitlets with a config_key argument are updated
272 # This is where traitlets with a config_key argument are updated
273 # from the values on config.
273 # from the values on config.
274 super(InteractiveShell, self).__init__(parent, config=config)
274 super(InteractiveShell, self).__init__(parent, config=config)
275
275
276 # These are relatively independent and stateless
276 # These are relatively independent and stateless
277 self.init_ipythondir(ipythondir)
277 self.init_ipythondir(ipythondir)
278 self.init_instance_attrs()
278 self.init_instance_attrs()
279 self.init_term_title()
279 self.init_term_title()
280 self.init_usage(usage)
280 self.init_usage(usage)
281 self.init_banner(banner1, banner2, display_banner)
281 self.init_banner(banner1, banner2, display_banner)
282
282
283 # Create namespaces (user_ns, user_global_ns, etc.)
283 # Create namespaces (user_ns, user_global_ns, etc.)
284 self.init_create_namespaces(user_ns, user_global_ns)
284 self.init_create_namespaces(user_ns, user_global_ns)
285 # This has to be done after init_create_namespaces because it uses
285 # This has to be done after init_create_namespaces because it uses
286 # something in self.user_ns, but before init_sys_modules, which
286 # something in self.user_ns, but before init_sys_modules, which
287 # is the first thing to modify sys.
287 # is the first thing to modify sys.
288 self.save_sys_module_state()
288 self.save_sys_module_state()
289 self.init_sys_modules()
289 self.init_sys_modules()
290
290
291 self.init_history()
291 self.init_history()
292 self.init_encoding()
292 self.init_encoding()
293 self.init_prefilter()
293 self.init_prefilter()
294
294
295 Magic.__init__(self, self)
295 Magic.__init__(self, self)
296
296
297 self.init_syntax_highlighting()
297 self.init_syntax_highlighting()
298 self.init_hooks()
298 self.init_hooks()
299 self.init_pushd_popd_magic()
299 self.init_pushd_popd_magic()
300 self.init_traceback_handlers(custom_exceptions)
300 self.init_traceback_handlers(custom_exceptions)
301 self.init_user_ns()
301 self.init_user_ns()
302 self.init_logger()
302 self.init_logger()
303 self.init_alias()
303 self.init_alias()
304 self.init_builtins()
304 self.init_builtins()
305
305
306 # pre_config_initialization
306 # pre_config_initialization
307 self.init_shadow_hist()
307 self.init_shadow_hist()
308
308
309 # The next section should contain averything that was in ipmaker.
309 # The next section should contain averything that was in ipmaker.
310 self.init_logstart()
310 self.init_logstart()
311
311
312 # The following was in post_config_initialization
312 # The following was in post_config_initialization
313 self.init_inspector()
313 self.init_inspector()
314 self.init_readline()
314 self.init_readline()
315 self.init_prompts()
315 self.init_prompts()
316 self.init_displayhook()
316 self.init_displayhook()
317 self.init_reload_doctest()
317 self.init_reload_doctest()
318 self.init_magics()
318 self.init_magics()
319 self.init_pdb()
319 self.init_pdb()
320 self.hooks.late_startup_hook()
320 self.hooks.late_startup_hook()
321
321
322 def get_ipython(self):
322 def get_ipython(self):
323 return self
323 return self
324
324
325 #-------------------------------------------------------------------------
325 #-------------------------------------------------------------------------
326 # Traitlet changed handlers
326 # Traitlet changed handlers
327 #-------------------------------------------------------------------------
327 #-------------------------------------------------------------------------
328
328
329 def _banner1_changed(self):
329 def _banner1_changed(self):
330 self.compute_banner()
330 self.compute_banner()
331
331
332 def _banner2_changed(self):
332 def _banner2_changed(self):
333 self.compute_banner()
333 self.compute_banner()
334
334
335 def _ipythondir_changed(self, name, new):
335 def _ipythondir_changed(self, name, new):
336 if not os.path.isdir(new):
336 if not os.path.isdir(new):
337 os.makedirs(new, mode = 0777)
337 os.makedirs(new, mode = 0777)
338 if not os.path.isdir(self.ipython_extension_dir):
338 if not os.path.isdir(self.ipython_extension_dir):
339 os.makedirs(self.ipython_extension_dir, mode = 0777)
339 os.makedirs(self.ipython_extension_dir, mode = 0777)
340
340
341 @property
341 @property
342 def ipython_extension_dir(self):
342 def ipython_extension_dir(self):
343 return os.path.join(self.ipythondir, 'extensions')
343 return os.path.join(self.ipythondir, 'extensions')
344
344
345 @property
345 @property
346 def usable_screen_length(self):
346 def usable_screen_length(self):
347 if self.screen_length == 0:
347 if self.screen_length == 0:
348 return 0
348 return 0
349 else:
349 else:
350 num_lines_bot = self.separate_in.count('\n')+1
350 num_lines_bot = self.separate_in.count('\n')+1
351 return self.screen_length - num_lines_bot
351 return self.screen_length - num_lines_bot
352
352
353 def _term_title_changed(self, name, new_value):
353 def _term_title_changed(self, name, new_value):
354 self.init_term_title()
354 self.init_term_title()
355
355
356 def set_autoindent(self,value=None):
356 def set_autoindent(self,value=None):
357 """Set the autoindent flag, checking for readline support.
357 """Set the autoindent flag, checking for readline support.
358
358
359 If called with no arguments, it acts as a toggle."""
359 If called with no arguments, it acts as a toggle."""
360
360
361 if not self.has_readline:
361 if not self.has_readline:
362 if os.name == 'posix':
362 if os.name == 'posix':
363 warn("The auto-indent feature requires the readline library")
363 warn("The auto-indent feature requires the readline library")
364 self.autoindent = 0
364 self.autoindent = 0
365 return
365 return
366 if value is None:
366 if value is None:
367 self.autoindent = not self.autoindent
367 self.autoindent = not self.autoindent
368 else:
368 else:
369 self.autoindent = value
369 self.autoindent = value
370
370
371 #-------------------------------------------------------------------------
371 #-------------------------------------------------------------------------
372 # init_* methods called by __init__
372 # init_* methods called by __init__
373 #-------------------------------------------------------------------------
373 #-------------------------------------------------------------------------
374
374
375 def init_ipythondir(self, ipythondir):
375 def init_ipythondir(self, ipythondir):
376 if ipythondir is not None:
376 if ipythondir is not None:
377 self.ipythondir = ipythondir
377 self.ipythondir = ipythondir
378 self.config.Global.ipythondir = self.ipythondir
378 self.config.Global.ipythondir = self.ipythondir
379 return
379 return
380
380
381 if hasattr(self.config.Global, 'ipythondir'):
381 if hasattr(self.config.Global, 'ipythondir'):
382 self.ipythondir = self.config.Global.ipythondir
382 self.ipythondir = self.config.Global.ipythondir
383 else:
383 else:
384 self.ipythondir = get_ipython_dir()
384 self.ipythondir = get_ipython_dir()
385
385
386 # All children can just read this
386 # All children can just read this
387 self.config.Global.ipythondir = self.ipythondir
387 self.config.Global.ipythondir = self.ipythondir
388
388
389 def init_instance_attrs(self):
389 def init_instance_attrs(self):
390 self.jobs = BackgroundJobManager()
390 self.jobs = BackgroundJobManager()
391 self.more = False
391 self.more = False
392
392
393 # command compiler
393 # command compiler
394 self.compile = codeop.CommandCompiler()
394 self.compile = codeop.CommandCompiler()
395
395
396 # User input buffer
396 # User input buffer
397 self.buffer = []
397 self.buffer = []
398
398
399 # Make an empty namespace, which extension writers can rely on both
399 # Make an empty namespace, which extension writers can rely on both
400 # existing and NEVER being used by ipython itself. This gives them a
400 # existing and NEVER being used by ipython itself. This gives them a
401 # convenient location for storing additional information and state
401 # convenient location for storing additional information and state
402 # their extensions may require, without fear of collisions with other
402 # their extensions may require, without fear of collisions with other
403 # ipython names that may develop later.
403 # ipython names that may develop later.
404 self.meta = Struct()
404 self.meta = Struct()
405
405
406 # Object variable to store code object waiting execution. This is
406 # Object variable to store code object waiting execution. This is
407 # used mainly by the multithreaded shells, but it can come in handy in
407 # used mainly by the multithreaded shells, but it can come in handy in
408 # other situations. No need to use a Queue here, since it's a single
408 # other situations. No need to use a Queue here, since it's a single
409 # item which gets cleared once run.
409 # item which gets cleared once run.
410 self.code_to_run = None
410 self.code_to_run = None
411
411
412 # Flag to mark unconditional exit
412 # Flag to mark unconditional exit
413 self.exit_now = False
413 self.exit_now = False
414
414
415 # Temporary files used for various purposes. Deleted at exit.
415 # Temporary files used for various purposes. Deleted at exit.
416 self.tempfiles = []
416 self.tempfiles = []
417
417
418 # Keep track of readline usage (later set by init_readline)
418 # Keep track of readline usage (later set by init_readline)
419 self.has_readline = False
419 self.has_readline = False
420
420
421 # keep track of where we started running (mainly for crash post-mortem)
421 # keep track of where we started running (mainly for crash post-mortem)
422 # This is not being used anywhere currently.
422 # This is not being used anywhere currently.
423 self.starting_dir = os.getcwd()
423 self.starting_dir = os.getcwd()
424
424
425 # Indentation management
425 # Indentation management
426 self.indent_current_nsp = 0
426 self.indent_current_nsp = 0
427
427
428 def init_term_title(self):
428 def init_term_title(self):
429 # Enable or disable the terminal title.
429 # Enable or disable the terminal title.
430 if self.term_title:
430 if self.term_title:
431 toggle_set_term_title(True)
431 toggle_set_term_title(True)
432 set_term_title('IPython: ' + abbrev_cwd())
432 set_term_title('IPython: ' + abbrev_cwd())
433 else:
433 else:
434 toggle_set_term_title(False)
434 toggle_set_term_title(False)
435
435
436 def init_usage(self, usage=None):
436 def init_usage(self, usage=None):
437 if usage is None:
437 if usage is None:
438 self.usage = interactive_usage
438 self.usage = interactive_usage
439 else:
439 else:
440 self.usage = usage
440 self.usage = usage
441
441
442 def init_encoding(self):
442 def init_encoding(self):
443 # Get system encoding at startup time. Certain terminals (like Emacs
443 # Get system encoding at startup time. Certain terminals (like Emacs
444 # under Win32 have it set to None, and we need to have a known valid
444 # under Win32 have it set to None, and we need to have a known valid
445 # encoding to use in the raw_input() method
445 # encoding to use in the raw_input() method
446 try:
446 try:
447 self.stdin_encoding = sys.stdin.encoding or 'ascii'
447 self.stdin_encoding = sys.stdin.encoding or 'ascii'
448 except AttributeError:
448 except AttributeError:
449 self.stdin_encoding = 'ascii'
449 self.stdin_encoding = 'ascii'
450
450
451 def init_syntax_highlighting(self):
451 def init_syntax_highlighting(self):
452 # Python source parser/formatter for syntax highlighting
452 # Python source parser/formatter for syntax highlighting
453 pyformat = PyColorize.Parser().format
453 pyformat = PyColorize.Parser().format
454 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
454 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
455
455
456 def init_pushd_popd_magic(self):
456 def init_pushd_popd_magic(self):
457 # for pushd/popd management
457 # for pushd/popd management
458 try:
458 try:
459 self.home_dir = get_home_dir()
459 self.home_dir = get_home_dir()
460 except HomeDirError, msg:
460 except HomeDirError, msg:
461 fatal(msg)
461 fatal(msg)
462
462
463 self.dir_stack = []
463 self.dir_stack = []
464
464
465 def init_logger(self):
465 def init_logger(self):
466 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
466 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
467 # local shortcut, this is used a LOT
467 # local shortcut, this is used a LOT
468 self.log = self.logger.log
468 self.log = self.logger.log
469
469
470 def init_logstart(self):
470 def init_logstart(self):
471 if self.logappend:
471 if self.logappend:
472 self.magic_logstart(self.logappend + ' append')
472 self.magic_logstart(self.logappend + ' append')
473 elif self.logfile:
473 elif self.logfile:
474 self.magic_logstart(self.logfile)
474 self.magic_logstart(self.logfile)
475 elif self.logstart:
475 elif self.logstart:
476 self.magic_logstart()
476 self.magic_logstart()
477
477
478 def init_builtins(self):
478 def init_builtins(self):
479 self.builtin_trap = BuiltinTrap(self)
479 self.builtin_trap = BuiltinTrap(self)
480
480
481 def init_inspector(self):
481 def init_inspector(self):
482 # Object inspector
482 # Object inspector
483 self.inspector = oinspect.Inspector(oinspect.InspectColors,
483 self.inspector = oinspect.Inspector(oinspect.InspectColors,
484 PyColorize.ANSICodeColors,
484 PyColorize.ANSICodeColors,
485 'NoColor',
485 'NoColor',
486 self.object_info_string_level)
486 self.object_info_string_level)
487
487
488 def init_prompts(self):
488 def init_prompts(self):
489 # Initialize cache, set in/out prompts and printing system
489 # Initialize cache, set in/out prompts and printing system
490 self.outputcache = CachedOutput(self,
490 self.outputcache = CachedOutput(self,
491 self.cache_size,
491 self.cache_size,
492 self.pprint,
492 self.pprint,
493 input_sep = self.separate_in,
493 input_sep = self.separate_in,
494 output_sep = self.separate_out,
494 output_sep = self.separate_out,
495 output_sep2 = self.separate_out2,
495 output_sep2 = self.separate_out2,
496 ps1 = self.prompt_in1,
496 ps1 = self.prompt_in1,
497 ps2 = self.prompt_in2,
497 ps2 = self.prompt_in2,
498 ps_out = self.prompt_out,
498 ps_out = self.prompt_out,
499 pad_left = self.prompts_pad_left)
499 pad_left = self.prompts_pad_left)
500
500
501 # user may have over-ridden the default print hook:
501 # user may have over-ridden the default print hook:
502 try:
502 try:
503 self.outputcache.__class__.display = self.hooks.display
503 self.outputcache.__class__.display = self.hooks.display
504 except AttributeError:
504 except AttributeError:
505 pass
505 pass
506
506
507 def init_displayhook(self):
507 def init_displayhook(self):
508 self.display_trap = DisplayTrap(self, self.outputcache)
508 self.display_trap = DisplayTrap(self, self.outputcache)
509
509
510 def init_reload_doctest(self):
510 def init_reload_doctest(self):
511 # Do a proper resetting of doctest, including the necessary displayhook
511 # Do a proper resetting of doctest, including the necessary displayhook
512 # monkeypatching
512 # monkeypatching
513 try:
513 try:
514 doctest_reload()
514 doctest_reload()
515 except ImportError:
515 except ImportError:
516 warn("doctest module does not exist.")
516 warn("doctest module does not exist.")
517
517
518 #-------------------------------------------------------------------------
518 #-------------------------------------------------------------------------
519 # Things related to the banner
519 # Things related to the banner
520 #-------------------------------------------------------------------------
520 #-------------------------------------------------------------------------
521
521
522 def init_banner(self, banner1, banner2, display_banner):
522 def init_banner(self, banner1, banner2, display_banner):
523 if banner1 is not None:
523 if banner1 is not None:
524 self.banner1 = banner1
524 self.banner1 = banner1
525 if banner2 is not None:
525 if banner2 is not None:
526 self.banner2 = banner2
526 self.banner2 = banner2
527 if display_banner is not None:
527 if display_banner is not None:
528 self.display_banner = display_banner
528 self.display_banner = display_banner
529 self.compute_banner()
529 self.compute_banner()
530
530
531 def show_banner(self, banner=None):
531 def show_banner(self, banner=None):
532 if banner is None:
532 if banner is None:
533 banner = self.banner
533 banner = self.banner
534 self.write(banner)
534 self.write(banner)
535
535
536 def compute_banner(self):
536 def compute_banner(self):
537 self.banner = self.banner1 + '\n'
537 self.banner = self.banner1 + '\n'
538 if self.profile:
538 if self.profile:
539 self.banner += '\nIPython profile: %s\n' % self.profile
539 self.banner += '\nIPython profile: %s\n' % self.profile
540 if self.banner2:
540 if self.banner2:
541 self.banner += '\n' + self.banner2 + '\n'
541 self.banner += '\n' + self.banner2 + '\n'
542
542
543 #-------------------------------------------------------------------------
543 #-------------------------------------------------------------------------
544 # Things related to injections into the sys module
544 # Things related to injections into the sys module
545 #-------------------------------------------------------------------------
545 #-------------------------------------------------------------------------
546
546
547 def save_sys_module_state(self):
547 def save_sys_module_state(self):
548 """Save the state of hooks in the sys module.
548 """Save the state of hooks in the sys module.
549
549
550 This has to be called after self.user_ns is created.
550 This has to be called after self.user_ns is created.
551 """
551 """
552 self._orig_sys_module_state = {}
552 self._orig_sys_module_state = {}
553 self._orig_sys_module_state['stdin'] = sys.stdin
553 self._orig_sys_module_state['stdin'] = sys.stdin
554 self._orig_sys_module_state['stdout'] = sys.stdout
554 self._orig_sys_module_state['stdout'] = sys.stdout
555 self._orig_sys_module_state['stderr'] = sys.stderr
555 self._orig_sys_module_state['stderr'] = sys.stderr
556 self._orig_sys_module_state['excepthook'] = sys.excepthook
556 self._orig_sys_module_state['excepthook'] = sys.excepthook
557 try:
557 try:
558 self._orig_sys_modules_main_name = self.user_ns['__name__']
558 self._orig_sys_modules_main_name = self.user_ns['__name__']
559 except KeyError:
559 except KeyError:
560 pass
560 pass
561
561
562 def restore_sys_module_state(self):
562 def restore_sys_module_state(self):
563 """Restore the state of the sys module."""
563 """Restore the state of the sys module."""
564 try:
564 try:
565 for k, v in self._orig_sys_module_state.items():
565 for k, v in self._orig_sys_module_state.items():
566 setattr(sys, k, v)
566 setattr(sys, k, v)
567 except AttributeError:
567 except AttributeError:
568 pass
568 pass
569 try:
569 try:
570 delattr(sys, 'ipcompleter')
570 delattr(sys, 'ipcompleter')
571 except AttributeError:
571 except AttributeError:
572 pass
572 pass
573 # Reset what what done in self.init_sys_modules
573 # Reset what what done in self.init_sys_modules
574 try:
574 try:
575 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
575 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
576 except (AttributeError, KeyError):
576 except (AttributeError, KeyError):
577 pass
577 pass
578
578
579 #-------------------------------------------------------------------------
579 #-------------------------------------------------------------------------
580 # Things related to hooks
580 # Things related to hooks
581 #-------------------------------------------------------------------------
581 #-------------------------------------------------------------------------
582
582
583 def init_hooks(self):
583 def init_hooks(self):
584 # hooks holds pointers used for user-side customizations
584 # hooks holds pointers used for user-side customizations
585 self.hooks = Struct()
585 self.hooks = Struct()
586
586
587 self.strdispatchers = {}
587 self.strdispatchers = {}
588
588
589 # Set all default hooks, defined in the IPython.hooks module.
589 # Set all default hooks, defined in the IPython.hooks module.
590 import IPython.core.hooks
590 import IPython.core.hooks
591 hooks = IPython.core.hooks
591 hooks = IPython.core.hooks
592 for hook_name in hooks.__all__:
592 for hook_name in hooks.__all__:
593 # default hooks have priority 100, i.e. low; user hooks should have
593 # default hooks have priority 100, i.e. low; user hooks should have
594 # 0-100 priority
594 # 0-100 priority
595 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
595 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
596
596
597 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
597 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
598 """set_hook(name,hook) -> sets an internal IPython hook.
598 """set_hook(name,hook) -> sets an internal IPython hook.
599
599
600 IPython exposes some of its internal API as user-modifiable hooks. By
600 IPython exposes some of its internal API as user-modifiable hooks. By
601 adding your function to one of these hooks, you can modify IPython's
601 adding your function to one of these hooks, you can modify IPython's
602 behavior to call at runtime your own routines."""
602 behavior to call at runtime your own routines."""
603
603
604 # At some point in the future, this should validate the hook before it
604 # At some point in the future, this should validate the hook before it
605 # accepts it. Probably at least check that the hook takes the number
605 # accepts it. Probably at least check that the hook takes the number
606 # of args it's supposed to.
606 # of args it's supposed to.
607
607
608 f = new.instancemethod(hook,self,self.__class__)
608 f = new.instancemethod(hook,self,self.__class__)
609
609
610 # check if the hook is for strdispatcher first
610 # check if the hook is for strdispatcher first
611 if str_key is not None:
611 if str_key is not None:
612 sdp = self.strdispatchers.get(name, StrDispatch())
612 sdp = self.strdispatchers.get(name, StrDispatch())
613 sdp.add_s(str_key, f, priority )
613 sdp.add_s(str_key, f, priority )
614 self.strdispatchers[name] = sdp
614 self.strdispatchers[name] = sdp
615 return
615 return
616 if re_key is not None:
616 if re_key is not None:
617 sdp = self.strdispatchers.get(name, StrDispatch())
617 sdp = self.strdispatchers.get(name, StrDispatch())
618 sdp.add_re(re.compile(re_key), f, priority )
618 sdp.add_re(re.compile(re_key), f, priority )
619 self.strdispatchers[name] = sdp
619 self.strdispatchers[name] = sdp
620 return
620 return
621
621
622 dp = getattr(self.hooks, name, None)
622 dp = getattr(self.hooks, name, None)
623 if name not in IPython.core.hooks.__all__:
623 if name not in IPython.core.hooks.__all__:
624 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
624 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
625 if not dp:
625 if not dp:
626 dp = IPython.core.hooks.CommandChainDispatcher()
626 dp = IPython.core.hooks.CommandChainDispatcher()
627
627
628 try:
628 try:
629 dp.add(f,priority)
629 dp.add(f,priority)
630 except AttributeError:
630 except AttributeError:
631 # it was not commandchain, plain old func - replace
631 # it was not commandchain, plain old func - replace
632 dp = f
632 dp = f
633
633
634 setattr(self.hooks,name, dp)
634 setattr(self.hooks,name, dp)
635
635
636 #-------------------------------------------------------------------------
636 #-------------------------------------------------------------------------
637 # Things related to the "main" module
637 # Things related to the "main" module
638 #-------------------------------------------------------------------------
638 #-------------------------------------------------------------------------
639
639
640 def new_main_mod(self,ns=None):
640 def new_main_mod(self,ns=None):
641 """Return a new 'main' module object for user code execution.
641 """Return a new 'main' module object for user code execution.
642 """
642 """
643 main_mod = self._user_main_module
643 main_mod = self._user_main_module
644 init_fakemod_dict(main_mod,ns)
644 init_fakemod_dict(main_mod,ns)
645 return main_mod
645 return main_mod
646
646
647 def cache_main_mod(self,ns,fname):
647 def cache_main_mod(self,ns,fname):
648 """Cache a main module's namespace.
648 """Cache a main module's namespace.
649
649
650 When scripts are executed via %run, we must keep a reference to the
650 When scripts are executed via %run, we must keep a reference to the
651 namespace of their __main__ module (a FakeModule instance) around so
651 namespace of their __main__ module (a FakeModule instance) around so
652 that Python doesn't clear it, rendering objects defined therein
652 that Python doesn't clear it, rendering objects defined therein
653 useless.
653 useless.
654
654
655 This method keeps said reference in a private dict, keyed by the
655 This method keeps said reference in a private dict, keyed by the
656 absolute path of the module object (which corresponds to the script
656 absolute path of the module object (which corresponds to the script
657 path). This way, for multiple executions of the same script we only
657 path). This way, for multiple executions of the same script we only
658 keep one copy of the namespace (the last one), thus preventing memory
658 keep one copy of the namespace (the last one), thus preventing memory
659 leaks from old references while allowing the objects from the last
659 leaks from old references while allowing the objects from the last
660 execution to be accessible.
660 execution to be accessible.
661
661
662 Note: we can not allow the actual FakeModule instances to be deleted,
662 Note: we can not allow the actual FakeModule instances to be deleted,
663 because of how Python tears down modules (it hard-sets all their
663 because of how Python tears down modules (it hard-sets all their
664 references to None without regard for reference counts). This method
664 references to None without regard for reference counts). This method
665 must therefore make a *copy* of the given namespace, to allow the
665 must therefore make a *copy* of the given namespace, to allow the
666 original module's __dict__ to be cleared and reused.
666 original module's __dict__ to be cleared and reused.
667
667
668
668
669 Parameters
669 Parameters
670 ----------
670 ----------
671 ns : a namespace (a dict, typically)
671 ns : a namespace (a dict, typically)
672
672
673 fname : str
673 fname : str
674 Filename associated with the namespace.
674 Filename associated with the namespace.
675
675
676 Examples
676 Examples
677 --------
677 --------
678
678
679 In [10]: import IPython
679 In [10]: import IPython
680
680
681 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
681 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
682
682
683 In [12]: IPython.__file__ in _ip._main_ns_cache
683 In [12]: IPython.__file__ in _ip._main_ns_cache
684 Out[12]: True
684 Out[12]: True
685 """
685 """
686 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
686 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
687
687
688 def clear_main_mod_cache(self):
688 def clear_main_mod_cache(self):
689 """Clear the cache of main modules.
689 """Clear the cache of main modules.
690
690
691 Mainly for use by utilities like %reset.
691 Mainly for use by utilities like %reset.
692
692
693 Examples
693 Examples
694 --------
694 --------
695
695
696 In [15]: import IPython
696 In [15]: import IPython
697
697
698 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
698 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
699
699
700 In [17]: len(_ip._main_ns_cache) > 0
700 In [17]: len(_ip._main_ns_cache) > 0
701 Out[17]: True
701 Out[17]: True
702
702
703 In [18]: _ip.clear_main_mod_cache()
703 In [18]: _ip.clear_main_mod_cache()
704
704
705 In [19]: len(_ip._main_ns_cache) == 0
705 In [19]: len(_ip._main_ns_cache) == 0
706 Out[19]: True
706 Out[19]: True
707 """
707 """
708 self._main_ns_cache.clear()
708 self._main_ns_cache.clear()
709
709
710 #-------------------------------------------------------------------------
710 #-------------------------------------------------------------------------
711 # Things related to debugging
711 # Things related to debugging
712 #-------------------------------------------------------------------------
712 #-------------------------------------------------------------------------
713
713
714 def init_pdb(self):
714 def init_pdb(self):
715 # Set calling of pdb on exceptions
715 # Set calling of pdb on exceptions
716 # self.call_pdb is a property
716 # self.call_pdb is a property
717 self.call_pdb = self.pdb
717 self.call_pdb = self.pdb
718
718
719 def _get_call_pdb(self):
719 def _get_call_pdb(self):
720 return self._call_pdb
720 return self._call_pdb
721
721
722 def _set_call_pdb(self,val):
722 def _set_call_pdb(self,val):
723
723
724 if val not in (0,1,False,True):
724 if val not in (0,1,False,True):
725 raise ValueError,'new call_pdb value must be boolean'
725 raise ValueError,'new call_pdb value must be boolean'
726
726
727 # store value in instance
727 # store value in instance
728 self._call_pdb = val
728 self._call_pdb = val
729
729
730 # notify the actual exception handlers
730 # notify the actual exception handlers
731 self.InteractiveTB.call_pdb = val
731 self.InteractiveTB.call_pdb = val
732 if self.isthreaded:
732 if self.isthreaded:
733 try:
733 try:
734 self.sys_excepthook.call_pdb = val
734 self.sys_excepthook.call_pdb = val
735 except:
735 except:
736 warn('Failed to activate pdb for threaded exception handler')
736 warn('Failed to activate pdb for threaded exception handler')
737
737
738 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
738 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
739 'Control auto-activation of pdb at exceptions')
739 'Control auto-activation of pdb at exceptions')
740
740
741 def debugger(self,force=False):
741 def debugger(self,force=False):
742 """Call the pydb/pdb debugger.
742 """Call the pydb/pdb debugger.
743
743
744 Keywords:
744 Keywords:
745
745
746 - force(False): by default, this routine checks the instance call_pdb
746 - force(False): by default, this routine checks the instance call_pdb
747 flag and does not actually invoke the debugger if the flag is false.
747 flag and does not actually invoke the debugger if the flag is false.
748 The 'force' option forces the debugger to activate even if the flag
748 The 'force' option forces the debugger to activate even if the flag
749 is false.
749 is false.
750 """
750 """
751
751
752 if not (force or self.call_pdb):
752 if not (force or self.call_pdb):
753 return
753 return
754
754
755 if not hasattr(sys,'last_traceback'):
755 if not hasattr(sys,'last_traceback'):
756 error('No traceback has been produced, nothing to debug.')
756 error('No traceback has been produced, nothing to debug.')
757 return
757 return
758
758
759 # use pydb if available
759 # use pydb if available
760 if debugger.has_pydb:
760 if debugger.has_pydb:
761 from pydb import pm
761 from pydb import pm
762 else:
762 else:
763 # fallback to our internal debugger
763 # fallback to our internal debugger
764 pm = lambda : self.InteractiveTB.debugger(force=True)
764 pm = lambda : self.InteractiveTB.debugger(force=True)
765 self.history_saving_wrapper(pm)()
765 self.history_saving_wrapper(pm)()
766
766
767 #-------------------------------------------------------------------------
767 #-------------------------------------------------------------------------
768 # Things related to IPython's various namespaces
768 # Things related to IPython's various namespaces
769 #-------------------------------------------------------------------------
769 #-------------------------------------------------------------------------
770
770
771 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
771 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
772 # Create the namespace where the user will operate. user_ns is
772 # Create the namespace where the user will operate. user_ns is
773 # normally the only one used, and it is passed to the exec calls as
773 # normally the only one used, and it is passed to the exec calls as
774 # the locals argument. But we do carry a user_global_ns namespace
774 # the locals argument. But we do carry a user_global_ns namespace
775 # given as the exec 'globals' argument, This is useful in embedding
775 # given as the exec 'globals' argument, This is useful in embedding
776 # situations where the ipython shell opens in a context where the
776 # situations where the ipython shell opens in a context where the
777 # distinction between locals and globals is meaningful. For
777 # distinction between locals and globals is meaningful. For
778 # non-embedded contexts, it is just the same object as the user_ns dict.
778 # non-embedded contexts, it is just the same object as the user_ns dict.
779
779
780 # FIXME. For some strange reason, __builtins__ is showing up at user
780 # FIXME. For some strange reason, __builtins__ is showing up at user
781 # level as a dict instead of a module. This is a manual fix, but I
781 # level as a dict instead of a module. This is a manual fix, but I
782 # should really track down where the problem is coming from. Alex
782 # should really track down where the problem is coming from. Alex
783 # Schmolck reported this problem first.
783 # Schmolck reported this problem first.
784
784
785 # A useful post by Alex Martelli on this topic:
785 # A useful post by Alex Martelli on this topic:
786 # Re: inconsistent value from __builtins__
786 # Re: inconsistent value from __builtins__
787 # Von: Alex Martelli <aleaxit@yahoo.com>
787 # Von: Alex Martelli <aleaxit@yahoo.com>
788 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
788 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
789 # Gruppen: comp.lang.python
789 # Gruppen: comp.lang.python
790
790
791 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
791 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
792 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
792 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
793 # > <type 'dict'>
793 # > <type 'dict'>
794 # > >>> print type(__builtins__)
794 # > >>> print type(__builtins__)
795 # > <type 'module'>
795 # > <type 'module'>
796 # > Is this difference in return value intentional?
796 # > Is this difference in return value intentional?
797
797
798 # Well, it's documented that '__builtins__' can be either a dictionary
798 # Well, it's documented that '__builtins__' can be either a dictionary
799 # or a module, and it's been that way for a long time. Whether it's
799 # or a module, and it's been that way for a long time. Whether it's
800 # intentional (or sensible), I don't know. In any case, the idea is
800 # intentional (or sensible), I don't know. In any case, the idea is
801 # that if you need to access the built-in namespace directly, you
801 # that if you need to access the built-in namespace directly, you
802 # should start with "import __builtin__" (note, no 's') which will
802 # should start with "import __builtin__" (note, no 's') which will
803 # definitely give you a module. Yeah, it's somewhat confusing:-(.
803 # definitely give you a module. Yeah, it's somewhat confusing:-(.
804
804
805 # These routines return properly built dicts as needed by the rest of
805 # These routines return properly built dicts as needed by the rest of
806 # the code, and can also be used by extension writers to generate
806 # the code, and can also be used by extension writers to generate
807 # properly initialized namespaces.
807 # properly initialized namespaces.
808 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
808 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
809 user_global_ns)
809 user_global_ns)
810
810
811 # Assign namespaces
811 # Assign namespaces
812 # This is the namespace where all normal user variables live
812 # This is the namespace where all normal user variables live
813 self.user_ns = user_ns
813 self.user_ns = user_ns
814 self.user_global_ns = user_global_ns
814 self.user_global_ns = user_global_ns
815
815
816 # An auxiliary namespace that checks what parts of the user_ns were
816 # An auxiliary namespace that checks what parts of the user_ns were
817 # loaded at startup, so we can list later only variables defined in
817 # loaded at startup, so we can list later only variables defined in
818 # actual interactive use. Since it is always a subset of user_ns, it
818 # actual interactive use. Since it is always a subset of user_ns, it
819 # doesn't need to be seaparately tracked in the ns_table
819 # doesn't need to be seaparately tracked in the ns_table
820 self.user_config_ns = {}
820 self.user_config_ns = {}
821
821
822 # A namespace to keep track of internal data structures to prevent
822 # A namespace to keep track of internal data structures to prevent
823 # them from cluttering user-visible stuff. Will be updated later
823 # them from cluttering user-visible stuff. Will be updated later
824 self.internal_ns = {}
824 self.internal_ns = {}
825
825
826 # Now that FakeModule produces a real module, we've run into a nasty
826 # Now that FakeModule produces a real module, we've run into a nasty
827 # problem: after script execution (via %run), the module where the user
827 # problem: after script execution (via %run), the module where the user
828 # code ran is deleted. Now that this object is a true module (needed
828 # code ran is deleted. Now that this object is a true module (needed
829 # so docetst and other tools work correctly), the Python module
829 # so docetst and other tools work correctly), the Python module
830 # teardown mechanism runs over it, and sets to None every variable
830 # teardown mechanism runs over it, and sets to None every variable
831 # present in that module. Top-level references to objects from the
831 # present in that module. Top-level references to objects from the
832 # script survive, because the user_ns is updated with them. However,
832 # script survive, because the user_ns is updated with them. However,
833 # calling functions defined in the script that use other things from
833 # calling functions defined in the script that use other things from
834 # the script will fail, because the function's closure had references
834 # the script will fail, because the function's closure had references
835 # to the original objects, which are now all None. So we must protect
835 # to the original objects, which are now all None. So we must protect
836 # these modules from deletion by keeping a cache.
836 # these modules from deletion by keeping a cache.
837 #
837 #
838 # To avoid keeping stale modules around (we only need the one from the
838 # To avoid keeping stale modules around (we only need the one from the
839 # last run), we use a dict keyed with the full path to the script, so
839 # last run), we use a dict keyed with the full path to the script, so
840 # only the last version of the module is held in the cache. Note,
840 # only the last version of the module is held in the cache. Note,
841 # however, that we must cache the module *namespace contents* (their
841 # however, that we must cache the module *namespace contents* (their
842 # __dict__). Because if we try to cache the actual modules, old ones
842 # __dict__). Because if we try to cache the actual modules, old ones
843 # (uncached) could be destroyed while still holding references (such as
843 # (uncached) could be destroyed while still holding references (such as
844 # those held by GUI objects that tend to be long-lived)>
844 # those held by GUI objects that tend to be long-lived)>
845 #
845 #
846 # The %reset command will flush this cache. See the cache_main_mod()
846 # The %reset command will flush this cache. See the cache_main_mod()
847 # and clear_main_mod_cache() methods for details on use.
847 # and clear_main_mod_cache() methods for details on use.
848
848
849 # This is the cache used for 'main' namespaces
849 # This is the cache used for 'main' namespaces
850 self._main_ns_cache = {}
850 self._main_ns_cache = {}
851 # And this is the single instance of FakeModule whose __dict__ we keep
851 # And this is the single instance of FakeModule whose __dict__ we keep
852 # copying and clearing for reuse on each %run
852 # copying and clearing for reuse on each %run
853 self._user_main_module = FakeModule()
853 self._user_main_module = FakeModule()
854
854
855 # A table holding all the namespaces IPython deals with, so that
855 # A table holding all the namespaces IPython deals with, so that
856 # introspection facilities can search easily.
856 # introspection facilities can search easily.
857 self.ns_table = {'user':user_ns,
857 self.ns_table = {'user':user_ns,
858 'user_global':user_global_ns,
858 'user_global':user_global_ns,
859 'internal':self.internal_ns,
859 'internal':self.internal_ns,
860 'builtin':__builtin__.__dict__
860 'builtin':__builtin__.__dict__
861 }
861 }
862
862
863 # Similarly, track all namespaces where references can be held and that
863 # Similarly, track all namespaces where references can be held and that
864 # we can safely clear (so it can NOT include builtin). This one can be
864 # we can safely clear (so it can NOT include builtin). This one can be
865 # a simple list.
865 # a simple list.
866 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
866 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
867 self.internal_ns, self._main_ns_cache ]
867 self.internal_ns, self._main_ns_cache ]
868
868
869 def init_sys_modules(self):
869 def init_sys_modules(self):
870 # We need to insert into sys.modules something that looks like a
870 # We need to insert into sys.modules something that looks like a
871 # module but which accesses the IPython namespace, for shelve and
871 # module but which accesses the IPython namespace, for shelve and
872 # pickle to work interactively. Normally they rely on getting
872 # pickle to work interactively. Normally they rely on getting
873 # everything out of __main__, but for embedding purposes each IPython
873 # everything out of __main__, but for embedding purposes each IPython
874 # instance has its own private namespace, so we can't go shoving
874 # instance has its own private namespace, so we can't go shoving
875 # everything into __main__.
875 # everything into __main__.
876
876
877 # note, however, that we should only do this for non-embedded
877 # note, however, that we should only do this for non-embedded
878 # ipythons, which really mimic the __main__.__dict__ with their own
878 # ipythons, which really mimic the __main__.__dict__ with their own
879 # namespace. Embedded instances, on the other hand, should not do
879 # namespace. Embedded instances, on the other hand, should not do
880 # this because they need to manage the user local/global namespaces
880 # this because they need to manage the user local/global namespaces
881 # only, but they live within a 'normal' __main__ (meaning, they
881 # only, but they live within a 'normal' __main__ (meaning, they
882 # shouldn't overtake the execution environment of the script they're
882 # shouldn't overtake the execution environment of the script they're
883 # embedded in).
883 # embedded in).
884
884
885 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
885 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
886
886
887 try:
887 try:
888 main_name = self.user_ns['__name__']
888 main_name = self.user_ns['__name__']
889 except KeyError:
889 except KeyError:
890 raise KeyError('user_ns dictionary MUST have a "__name__" key')
890 raise KeyError('user_ns dictionary MUST have a "__name__" key')
891 else:
891 else:
892 sys.modules[main_name] = FakeModule(self.user_ns)
892 sys.modules[main_name] = FakeModule(self.user_ns)
893
893
894 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
894 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
895 """Return a valid local and global user interactive namespaces.
895 """Return a valid local and global user interactive namespaces.
896
896
897 This builds a dict with the minimal information needed to operate as a
897 This builds a dict with the minimal information needed to operate as a
898 valid IPython user namespace, which you can pass to the various
898 valid IPython user namespace, which you can pass to the various
899 embedding classes in ipython. The default implementation returns the
899 embedding classes in ipython. The default implementation returns the
900 same dict for both the locals and the globals to allow functions to
900 same dict for both the locals and the globals to allow functions to
901 refer to variables in the namespace. Customized implementations can
901 refer to variables in the namespace. Customized implementations can
902 return different dicts. The locals dictionary can actually be anything
902 return different dicts. The locals dictionary can actually be anything
903 following the basic mapping protocol of a dict, but the globals dict
903 following the basic mapping protocol of a dict, but the globals dict
904 must be a true dict, not even a subclass. It is recommended that any
904 must be a true dict, not even a subclass. It is recommended that any
905 custom object for the locals namespace synchronize with the globals
905 custom object for the locals namespace synchronize with the globals
906 dict somehow.
906 dict somehow.
907
907
908 Raises TypeError if the provided globals namespace is not a true dict.
908 Raises TypeError if the provided globals namespace is not a true dict.
909
909
910 :Parameters:
910 :Parameters:
911 user_ns : dict-like, optional
911 user_ns : dict-like, optional
912 The current user namespace. The items in this namespace should
912 The current user namespace. The items in this namespace should
913 be included in the output. If None, an appropriate blank
913 be included in the output. If None, an appropriate blank
914 namespace should be created.
914 namespace should be created.
915 user_global_ns : dict, optional
915 user_global_ns : dict, optional
916 The current user global namespace. The items in this namespace
916 The current user global namespace. The items in this namespace
917 should be included in the output. If None, an appropriate
917 should be included in the output. If None, an appropriate
918 blank namespace should be created.
918 blank namespace should be created.
919
919
920 :Returns:
920 :Returns:
921 A tuple pair of dictionary-like object to be used as the local namespace
921 A tuple pair of dictionary-like object to be used as the local namespace
922 of the interpreter and a dict to be used as the global namespace.
922 of the interpreter and a dict to be used as the global namespace.
923 """
923 """
924
924
925 if user_ns is None:
925 if user_ns is None:
926 # Set __name__ to __main__ to better match the behavior of the
926 # Set __name__ to __main__ to better match the behavior of the
927 # normal interpreter.
927 # normal interpreter.
928 user_ns = {'__name__' :'__main__',
928 user_ns = {'__name__' :'__main__',
929 '__builtins__' : __builtin__,
929 '__builtins__' : __builtin__,
930 }
930 }
931 else:
931 else:
932 user_ns.setdefault('__name__','__main__')
932 user_ns.setdefault('__name__','__main__')
933 user_ns.setdefault('__builtins__',__builtin__)
933 user_ns.setdefault('__builtins__',__builtin__)
934
934
935 if user_global_ns is None:
935 if user_global_ns is None:
936 user_global_ns = user_ns
936 user_global_ns = user_ns
937 if type(user_global_ns) is not dict:
937 if type(user_global_ns) is not dict:
938 raise TypeError("user_global_ns must be a true dict; got %r"
938 raise TypeError("user_global_ns must be a true dict; got %r"
939 % type(user_global_ns))
939 % type(user_global_ns))
940
940
941 return user_ns, user_global_ns
941 return user_ns, user_global_ns
942
942
943 def init_user_ns(self):
943 def init_user_ns(self):
944 """Initialize all user-visible namespaces to their minimum defaults.
944 """Initialize all user-visible namespaces to their minimum defaults.
945
945
946 Certain history lists are also initialized here, as they effectively
946 Certain history lists are also initialized here, as they effectively
947 act as user namespaces.
947 act as user namespaces.
948
948
949 Notes
949 Notes
950 -----
950 -----
951 All data structures here are only filled in, they are NOT reset by this
951 All data structures here are only filled in, they are NOT reset by this
952 method. If they were not empty before, data will simply be added to
952 method. If they were not empty before, data will simply be added to
953 therm.
953 therm.
954 """
954 """
955 # Store myself as the public api!!!
955 # Store myself as the public api!!!
956 self.user_ns['get_ipython'] = self.get_ipython
956 self.user_ns['get_ipython'] = self.get_ipython
957
957
958 # make global variables for user access to the histories
958 # make global variables for user access to the histories
959 self.user_ns['_ih'] = self.input_hist
959 self.user_ns['_ih'] = self.input_hist
960 self.user_ns['_oh'] = self.output_hist
960 self.user_ns['_oh'] = self.output_hist
961 self.user_ns['_dh'] = self.dir_hist
961 self.user_ns['_dh'] = self.dir_hist
962
962
963 # user aliases to input and output histories
963 # user aliases to input and output histories
964 self.user_ns['In'] = self.input_hist
964 self.user_ns['In'] = self.input_hist
965 self.user_ns['Out'] = self.output_hist
965 self.user_ns['Out'] = self.output_hist
966
966
967 self.user_ns['_sh'] = shadowns
967 self.user_ns['_sh'] = shadowns
968
968
969 # Put 'help' in the user namespace
969 # Put 'help' in the user namespace
970 try:
970 try:
971 from site import _Helper
971 from site import _Helper
972 self.user_ns['help'] = _Helper()
972 self.user_ns['help'] = _Helper()
973 except ImportError:
973 except ImportError:
974 warn('help() not available - check site.py')
974 warn('help() not available - check site.py')
975
975
976 def reset(self):
976 def reset(self):
977 """Clear all internal namespaces.
977 """Clear all internal namespaces.
978
978
979 Note that this is much more aggressive than %reset, since it clears
979 Note that this is much more aggressive than %reset, since it clears
980 fully all namespaces, as well as all input/output lists.
980 fully all namespaces, as well as all input/output lists.
981 """
981 """
982 for ns in self.ns_refs_table:
982 for ns in self.ns_refs_table:
983 ns.clear()
983 ns.clear()
984
984
985 self.alias_manager.clear_aliases()
985 self.alias_manager.clear_aliases()
986
986
987 # Clear input and output histories
987 # Clear input and output histories
988 self.input_hist[:] = []
988 self.input_hist[:] = []
989 self.input_hist_raw[:] = []
989 self.input_hist_raw[:] = []
990 self.output_hist.clear()
990 self.output_hist.clear()
991
991
992 # Restore the user namespaces to minimal usability
992 # Restore the user namespaces to minimal usability
993 self.init_user_ns()
993 self.init_user_ns()
994
994
995 # Restore the default and user aliases
995 # Restore the default and user aliases
996 self.alias_manager.init_aliases()
996 self.alias_manager.init_aliases()
997
997
998 def push(self, variables, interactive=True):
998 def push(self, variables, interactive=True):
999 """Inject a group of variables into the IPython user namespace.
999 """Inject a group of variables into the IPython user namespace.
1000
1000
1001 Parameters
1001 Parameters
1002 ----------
1002 ----------
1003 variables : dict, str or list/tuple of str
1003 variables : dict, str or list/tuple of str
1004 The variables to inject into the user's namespace. If a dict,
1004 The variables to inject into the user's namespace. If a dict,
1005 a simple update is done. If a str, the string is assumed to
1005 a simple update is done. If a str, the string is assumed to
1006 have variable names separated by spaces. A list/tuple of str
1006 have variable names separated by spaces. A list/tuple of str
1007 can also be used to give the variable names. If just the variable
1007 can also be used to give the variable names. If just the variable
1008 names are give (list/tuple/str) then the variable values looked
1008 names are give (list/tuple/str) then the variable values looked
1009 up in the callers frame.
1009 up in the callers frame.
1010 interactive : bool
1010 interactive : bool
1011 If True (default), the variables will be listed with the ``who``
1011 If True (default), the variables will be listed with the ``who``
1012 magic.
1012 magic.
1013 """
1013 """
1014 vdict = None
1014 vdict = None
1015
1015
1016 # We need a dict of name/value pairs to do namespace updates.
1016 # We need a dict of name/value pairs to do namespace updates.
1017 if isinstance(variables, dict):
1017 if isinstance(variables, dict):
1018 vdict = variables
1018 vdict = variables
1019 elif isinstance(variables, (basestring, list, tuple)):
1019 elif isinstance(variables, (basestring, list, tuple)):
1020 if isinstance(variables, basestring):
1020 if isinstance(variables, basestring):
1021 vlist = variables.split()
1021 vlist = variables.split()
1022 else:
1022 else:
1023 vlist = variables
1023 vlist = variables
1024 vdict = {}
1024 vdict = {}
1025 cf = sys._getframe(1)
1025 cf = sys._getframe(1)
1026 for name in vlist:
1026 for name in vlist:
1027 try:
1027 try:
1028 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1028 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1029 except:
1029 except:
1030 print ('Could not get variable %s from %s' %
1030 print ('Could not get variable %s from %s' %
1031 (name,cf.f_code.co_name))
1031 (name,cf.f_code.co_name))
1032 else:
1032 else:
1033 raise ValueError('variables must be a dict/str/list/tuple')
1033 raise ValueError('variables must be a dict/str/list/tuple')
1034
1034
1035 # Propagate variables to user namespace
1035 # Propagate variables to user namespace
1036 self.user_ns.update(vdict)
1036 self.user_ns.update(vdict)
1037
1037
1038 # And configure interactive visibility
1038 # And configure interactive visibility
1039 config_ns = self.user_config_ns
1039 config_ns = self.user_config_ns
1040 if interactive:
1040 if interactive:
1041 for name, val in vdict.iteritems():
1041 for name, val in vdict.iteritems():
1042 config_ns.pop(name, None)
1042 config_ns.pop(name, None)
1043 else:
1043 else:
1044 for name,val in vdict.iteritems():
1044 for name,val in vdict.iteritems():
1045 config_ns[name] = val
1045 config_ns[name] = val
1046
1046
1047 #-------------------------------------------------------------------------
1047 #-------------------------------------------------------------------------
1048 # Things related to history management
1048 # Things related to history management
1049 #-------------------------------------------------------------------------
1049 #-------------------------------------------------------------------------
1050
1050
1051 def init_history(self):
1051 def init_history(self):
1052 # List of input with multi-line handling.
1052 # List of input with multi-line handling.
1053 self.input_hist = InputList()
1053 self.input_hist = InputList()
1054 # This one will hold the 'raw' input history, without any
1054 # This one will hold the 'raw' input history, without any
1055 # pre-processing. This will allow users to retrieve the input just as
1055 # pre-processing. This will allow users to retrieve the input just as
1056 # it was exactly typed in by the user, with %hist -r.
1056 # it was exactly typed in by the user, with %hist -r.
1057 self.input_hist_raw = InputList()
1057 self.input_hist_raw = InputList()
1058
1058
1059 # list of visited directories
1059 # list of visited directories
1060 try:
1060 try:
1061 self.dir_hist = [os.getcwd()]
1061 self.dir_hist = [os.getcwd()]
1062 except OSError:
1062 except OSError:
1063 self.dir_hist = []
1063 self.dir_hist = []
1064
1064
1065 # dict of output history
1065 # dict of output history
1066 self.output_hist = {}
1066 self.output_hist = {}
1067
1067
1068 # Now the history file
1068 # Now the history file
1069 if self.profile:
1069 if self.profile:
1070 histfname = 'history-%s' % self.profile
1070 histfname = 'history-%s' % self.profile
1071 else:
1071 else:
1072 histfname = 'history'
1072 histfname = 'history'
1073 self.histfile = os.path.join(self.ipythondir, histfname)
1073 self.histfile = os.path.join(self.ipythondir, histfname)
1074
1074
1075 # Fill the history zero entry, user counter starts at 1
1075 # Fill the history zero entry, user counter starts at 1
1076 self.input_hist.append('\n')
1076 self.input_hist.append('\n')
1077 self.input_hist_raw.append('\n')
1077 self.input_hist_raw.append('\n')
1078
1078
1079 def init_shadow_hist(self):
1079 def init_shadow_hist(self):
1080 try:
1080 try:
1081 self.db = pickleshare.PickleShareDB(self.ipythondir + "/db")
1081 self.db = pickleshare.PickleShareDB(self.ipythondir + "/db")
1082 except exceptions.UnicodeDecodeError:
1082 except exceptions.UnicodeDecodeError:
1083 print "Your ipythondir can't be decoded to unicode!"
1083 print "Your ipythondir can't be decoded to unicode!"
1084 print "Please set HOME environment variable to something that"
1084 print "Please set HOME environment variable to something that"
1085 print r"only has ASCII characters, e.g. c:\home"
1085 print r"only has ASCII characters, e.g. c:\home"
1086 print "Now it is", self.ipythondir
1086 print "Now it is", self.ipythondir
1087 sys.exit()
1087 sys.exit()
1088 self.shadowhist = ipcorehist.ShadowHist(self.db)
1088 self.shadowhist = ipcorehist.ShadowHist(self.db)
1089
1089
1090 def savehist(self):
1090 def savehist(self):
1091 """Save input history to a file (via readline library)."""
1091 """Save input history to a file (via readline library)."""
1092
1092
1093 if not self.has_readline:
1093 if not self.has_readline:
1094 return
1094 return
1095
1095
1096 try:
1096 try:
1097 self.readline.write_history_file(self.histfile)
1097 self.readline.write_history_file(self.histfile)
1098 except:
1098 except:
1099 print 'Unable to save IPython command history to file: ' + \
1099 print 'Unable to save IPython command history to file: ' + \
1100 `self.histfile`
1100 `self.histfile`
1101
1101
1102 def reloadhist(self):
1102 def reloadhist(self):
1103 """Reload the input history from disk file."""
1103 """Reload the input history from disk file."""
1104
1104
1105 if self.has_readline:
1105 if self.has_readline:
1106 try:
1106 try:
1107 self.readline.clear_history()
1107 self.readline.clear_history()
1108 self.readline.read_history_file(self.shell.histfile)
1108 self.readline.read_history_file(self.shell.histfile)
1109 except AttributeError:
1109 except AttributeError:
1110 pass
1110 pass
1111
1111
1112 def history_saving_wrapper(self, func):
1112 def history_saving_wrapper(self, func):
1113 """ Wrap func for readline history saving
1113 """ Wrap func for readline history saving
1114
1114
1115 Convert func into callable that saves & restores
1115 Convert func into callable that saves & restores
1116 history around the call """
1116 history around the call """
1117
1117
1118 if not self.has_readline:
1118 if not self.has_readline:
1119 return func
1119 return func
1120
1120
1121 def wrapper():
1121 def wrapper():
1122 self.savehist()
1122 self.savehist()
1123 try:
1123 try:
1124 func()
1124 func()
1125 finally:
1125 finally:
1126 readline.read_history_file(self.histfile)
1126 readline.read_history_file(self.histfile)
1127 return wrapper
1127 return wrapper
1128
1128
1129 #-------------------------------------------------------------------------
1129 #-------------------------------------------------------------------------
1130 # Things related to exception handling and tracebacks (not debugging)
1130 # Things related to exception handling and tracebacks (not debugging)
1131 #-------------------------------------------------------------------------
1131 #-------------------------------------------------------------------------
1132
1132
1133 def init_traceback_handlers(self, custom_exceptions):
1133 def init_traceback_handlers(self, custom_exceptions):
1134 # Syntax error handler.
1134 # Syntax error handler.
1135 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1135 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1136
1136
1137 # The interactive one is initialized with an offset, meaning we always
1137 # The interactive one is initialized with an offset, meaning we always
1138 # want to remove the topmost item in the traceback, which is our own
1138 # want to remove the topmost item in the traceback, which is our own
1139 # internal code. Valid modes: ['Plain','Context','Verbose']
1139 # internal code. Valid modes: ['Plain','Context','Verbose']
1140 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1140 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1141 color_scheme='NoColor',
1141 color_scheme='NoColor',
1142 tb_offset = 1)
1142 tb_offset = 1)
1143
1143
1144 # IPython itself shouldn't crash. This will produce a detailed
1144 # IPython itself shouldn't crash. This will produce a detailed
1145 # post-mortem if it does. But we only install the crash handler for
1145 # post-mortem if it does. But we only install the crash handler for
1146 # non-threaded shells, the threaded ones use a normal verbose reporter
1146 # non-threaded shells, the threaded ones use a normal verbose reporter
1147 # and lose the crash handler. This is because exceptions in the main
1147 # and lose the crash handler. This is because exceptions in the main
1148 # thread (such as in GUI code) propagate directly to sys.excepthook,
1148 # thread (such as in GUI code) propagate directly to sys.excepthook,
1149 # and there's no point in printing crash dumps for every user exception.
1149 # and there's no point in printing crash dumps for every user exception.
1150 if self.isthreaded:
1150 if self.isthreaded:
1151 ipCrashHandler = ultratb.FormattedTB()
1151 ipCrashHandler = ultratb.FormattedTB()
1152 else:
1152 else:
1153 from IPython.core import crashhandler
1153 from IPython.core import crashhandler
1154 ipCrashHandler = crashhandler.IPythonCrashHandler(self)
1154 ipCrashHandler = crashhandler.IPythonCrashHandler(self)
1155 self.set_crash_handler(ipCrashHandler)
1155 self.set_crash_handler(ipCrashHandler)
1156
1156
1157 # and add any custom exception handlers the user may have specified
1157 # and add any custom exception handlers the user may have specified
1158 self.set_custom_exc(*custom_exceptions)
1158 self.set_custom_exc(*custom_exceptions)
1159
1159
1160 def set_crash_handler(self, crashHandler):
1160 def set_crash_handler(self, crashHandler):
1161 """Set the IPython crash handler.
1161 """Set the IPython crash handler.
1162
1162
1163 This must be a callable with a signature suitable for use as
1163 This must be a callable with a signature suitable for use as
1164 sys.excepthook."""
1164 sys.excepthook."""
1165
1165
1166 # Install the given crash handler as the Python exception hook
1166 # Install the given crash handler as the Python exception hook
1167 sys.excepthook = crashHandler
1167 sys.excepthook = crashHandler
1168
1168
1169 # The instance will store a pointer to this, so that runtime code
1169 # The instance will store a pointer to this, so that runtime code
1170 # (such as magics) can access it. This is because during the
1170 # (such as magics) can access it. This is because during the
1171 # read-eval loop, it gets temporarily overwritten (to deal with GUI
1171 # read-eval loop, it gets temporarily overwritten (to deal with GUI
1172 # frameworks).
1172 # frameworks).
1173 self.sys_excepthook = sys.excepthook
1173 self.sys_excepthook = sys.excepthook
1174
1174
1175 def set_custom_exc(self,exc_tuple,handler):
1175 def set_custom_exc(self,exc_tuple,handler):
1176 """set_custom_exc(exc_tuple,handler)
1176 """set_custom_exc(exc_tuple,handler)
1177
1177
1178 Set a custom exception handler, which will be called if any of the
1178 Set a custom exception handler, which will be called if any of the
1179 exceptions in exc_tuple occur in the mainloop (specifically, in the
1179 exceptions in exc_tuple occur in the mainloop (specifically, in the
1180 runcode() method.
1180 runcode() method.
1181
1181
1182 Inputs:
1182 Inputs:
1183
1183
1184 - exc_tuple: a *tuple* of valid exceptions to call the defined
1184 - exc_tuple: a *tuple* of valid exceptions to call the defined
1185 handler for. It is very important that you use a tuple, and NOT A
1185 handler for. It is very important that you use a tuple, and NOT A
1186 LIST here, because of the way Python's except statement works. If
1186 LIST here, because of the way Python's except statement works. If
1187 you only want to trap a single exception, use a singleton tuple:
1187 you only want to trap a single exception, use a singleton tuple:
1188
1188
1189 exc_tuple == (MyCustomException,)
1189 exc_tuple == (MyCustomException,)
1190
1190
1191 - handler: this must be defined as a function with the following
1191 - handler: this must be defined as a function with the following
1192 basic interface: def my_handler(self,etype,value,tb).
1192 basic interface: def my_handler(self,etype,value,tb).
1193
1193
1194 This will be made into an instance method (via new.instancemethod)
1194 This will be made into an instance method (via new.instancemethod)
1195 of IPython itself, and it will be called if any of the exceptions
1195 of IPython itself, and it will be called if any of the exceptions
1196 listed in the exc_tuple are caught. If the handler is None, an
1196 listed in the exc_tuple are caught. If the handler is None, an
1197 internal basic one is used, which just prints basic info.
1197 internal basic one is used, which just prints basic info.
1198
1198
1199 WARNING: by putting in your own exception handler into IPython's main
1199 WARNING: by putting in your own exception handler into IPython's main
1200 execution loop, you run a very good chance of nasty crashes. This
1200 execution loop, you run a very good chance of nasty crashes. This
1201 facility should only be used if you really know what you are doing."""
1201 facility should only be used if you really know what you are doing."""
1202
1202
1203 assert type(exc_tuple)==type(()) , \
1203 assert type(exc_tuple)==type(()) , \
1204 "The custom exceptions must be given AS A TUPLE."
1204 "The custom exceptions must be given AS A TUPLE."
1205
1205
1206 def dummy_handler(self,etype,value,tb):
1206 def dummy_handler(self,etype,value,tb):
1207 print '*** Simple custom exception handler ***'
1207 print '*** Simple custom exception handler ***'
1208 print 'Exception type :',etype
1208 print 'Exception type :',etype
1209 print 'Exception value:',value
1209 print 'Exception value:',value
1210 print 'Traceback :',tb
1210 print 'Traceback :',tb
1211 print 'Source code :','\n'.join(self.buffer)
1211 print 'Source code :','\n'.join(self.buffer)
1212
1212
1213 if handler is None: handler = dummy_handler
1213 if handler is None: handler = dummy_handler
1214
1214
1215 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1215 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1216 self.custom_exceptions = exc_tuple
1216 self.custom_exceptions = exc_tuple
1217
1217
1218 def excepthook(self, etype, value, tb):
1218 def excepthook(self, etype, value, tb):
1219 """One more defense for GUI apps that call sys.excepthook.
1219 """One more defense for GUI apps that call sys.excepthook.
1220
1220
1221 GUI frameworks like wxPython trap exceptions and call
1221 GUI frameworks like wxPython trap exceptions and call
1222 sys.excepthook themselves. I guess this is a feature that
1222 sys.excepthook themselves. I guess this is a feature that
1223 enables them to keep running after exceptions that would
1223 enables them to keep running after exceptions that would
1224 otherwise kill their mainloop. This is a bother for IPython
1224 otherwise kill their mainloop. This is a bother for IPython
1225 which excepts to catch all of the program exceptions with a try:
1225 which excepts to catch all of the program exceptions with a try:
1226 except: statement.
1226 except: statement.
1227
1227
1228 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1228 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1229 any app directly invokes sys.excepthook, it will look to the user like
1229 any app directly invokes sys.excepthook, it will look to the user like
1230 IPython crashed. In order to work around this, we can disable the
1230 IPython crashed. In order to work around this, we can disable the
1231 CrashHandler and replace it with this excepthook instead, which prints a
1231 CrashHandler and replace it with this excepthook instead, which prints a
1232 regular traceback using our InteractiveTB. In this fashion, apps which
1232 regular traceback using our InteractiveTB. In this fashion, apps which
1233 call sys.excepthook will generate a regular-looking exception from
1233 call sys.excepthook will generate a regular-looking exception from
1234 IPython, and the CrashHandler will only be triggered by real IPython
1234 IPython, and the CrashHandler will only be triggered by real IPython
1235 crashes.
1235 crashes.
1236
1236
1237 This hook should be used sparingly, only in places which are not likely
1237 This hook should be used sparingly, only in places which are not likely
1238 to be true IPython errors.
1238 to be true IPython errors.
1239 """
1239 """
1240 self.showtraceback((etype,value,tb),tb_offset=0)
1240 self.showtraceback((etype,value,tb),tb_offset=0)
1241
1241
1242 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1242 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1243 """Display the exception that just occurred.
1243 """Display the exception that just occurred.
1244
1244
1245 If nothing is known about the exception, this is the method which
1245 If nothing is known about the exception, this is the method which
1246 should be used throughout the code for presenting user tracebacks,
1246 should be used throughout the code for presenting user tracebacks,
1247 rather than directly invoking the InteractiveTB object.
1247 rather than directly invoking the InteractiveTB object.
1248
1248
1249 A specific showsyntaxerror() also exists, but this method can take
1249 A specific showsyntaxerror() also exists, but this method can take
1250 care of calling it if needed, so unless you are explicitly catching a
1250 care of calling it if needed, so unless you are explicitly catching a
1251 SyntaxError exception, don't try to analyze the stack manually and
1251 SyntaxError exception, don't try to analyze the stack manually and
1252 simply call this method."""
1252 simply call this method."""
1253
1253
1254
1254
1255 # Though this won't be called by syntax errors in the input line,
1255 # Though this won't be called by syntax errors in the input line,
1256 # there may be SyntaxError cases whith imported code.
1256 # there may be SyntaxError cases whith imported code.
1257
1257
1258 try:
1258 try:
1259 if exc_tuple is None:
1259 if exc_tuple is None:
1260 etype, value, tb = sys.exc_info()
1260 etype, value, tb = sys.exc_info()
1261 else:
1261 else:
1262 etype, value, tb = exc_tuple
1262 etype, value, tb = exc_tuple
1263
1263
1264 if etype is SyntaxError:
1264 if etype is SyntaxError:
1265 self.showsyntaxerror(filename)
1265 self.showsyntaxerror(filename)
1266 elif etype is UsageError:
1266 elif etype is UsageError:
1267 print "UsageError:", value
1267 print "UsageError:", value
1268 else:
1268 else:
1269 # WARNING: these variables are somewhat deprecated and not
1269 # WARNING: these variables are somewhat deprecated and not
1270 # necessarily safe to use in a threaded environment, but tools
1270 # necessarily safe to use in a threaded environment, but tools
1271 # like pdb depend on their existence, so let's set them. If we
1271 # like pdb depend on their existence, so let's set them. If we
1272 # find problems in the field, we'll need to revisit their use.
1272 # find problems in the field, we'll need to revisit their use.
1273 sys.last_type = etype
1273 sys.last_type = etype
1274 sys.last_value = value
1274 sys.last_value = value
1275 sys.last_traceback = tb
1275 sys.last_traceback = tb
1276
1276
1277 if etype in self.custom_exceptions:
1277 if etype in self.custom_exceptions:
1278 self.CustomTB(etype,value,tb)
1278 self.CustomTB(etype,value,tb)
1279 else:
1279 else:
1280 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1280 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1281 if self.InteractiveTB.call_pdb and self.has_readline:
1281 if self.InteractiveTB.call_pdb and self.has_readline:
1282 # pdb mucks up readline, fix it back
1282 # pdb mucks up readline, fix it back
1283 self.set_completer()
1283 self.set_completer()
1284 except KeyboardInterrupt:
1284 except KeyboardInterrupt:
1285 self.write("\nKeyboardInterrupt\n")
1285 self.write("\nKeyboardInterrupt\n")
1286
1286
1287 def showsyntaxerror(self, filename=None):
1287 def showsyntaxerror(self, filename=None):
1288 """Display the syntax error that just occurred.
1288 """Display the syntax error that just occurred.
1289
1289
1290 This doesn't display a stack trace because there isn't one.
1290 This doesn't display a stack trace because there isn't one.
1291
1291
1292 If a filename is given, it is stuffed in the exception instead
1292 If a filename is given, it is stuffed in the exception instead
1293 of what was there before (because Python's parser always uses
1293 of what was there before (because Python's parser always uses
1294 "<string>" when reading from a string).
1294 "<string>" when reading from a string).
1295 """
1295 """
1296 etype, value, last_traceback = sys.exc_info()
1296 etype, value, last_traceback = sys.exc_info()
1297
1297
1298 # See note about these variables in showtraceback() below
1298 # See note about these variables in showtraceback() below
1299 sys.last_type = etype
1299 sys.last_type = etype
1300 sys.last_value = value
1300 sys.last_value = value
1301 sys.last_traceback = last_traceback
1301 sys.last_traceback = last_traceback
1302
1302
1303 if filename and etype is SyntaxError:
1303 if filename and etype is SyntaxError:
1304 # Work hard to stuff the correct filename in the exception
1304 # Work hard to stuff the correct filename in the exception
1305 try:
1305 try:
1306 msg, (dummy_filename, lineno, offset, line) = value
1306 msg, (dummy_filename, lineno, offset, line) = value
1307 except:
1307 except:
1308 # Not the format we expect; leave it alone
1308 # Not the format we expect; leave it alone
1309 pass
1309 pass
1310 else:
1310 else:
1311 # Stuff in the right filename
1311 # Stuff in the right filename
1312 try:
1312 try:
1313 # Assume SyntaxError is a class exception
1313 # Assume SyntaxError is a class exception
1314 value = SyntaxError(msg, (filename, lineno, offset, line))
1314 value = SyntaxError(msg, (filename, lineno, offset, line))
1315 except:
1315 except:
1316 # If that failed, assume SyntaxError is a string
1316 # If that failed, assume SyntaxError is a string
1317 value = msg, (filename, lineno, offset, line)
1317 value = msg, (filename, lineno, offset, line)
1318 self.SyntaxTB(etype,value,[])
1318 self.SyntaxTB(etype,value,[])
1319
1319
1320 def edit_syntax_error(self):
1320 def edit_syntax_error(self):
1321 """The bottom half of the syntax error handler called in the main loop.
1321 """The bottom half of the syntax error handler called in the main loop.
1322
1322
1323 Loop until syntax error is fixed or user cancels.
1323 Loop until syntax error is fixed or user cancels.
1324 """
1324 """
1325
1325
1326 while self.SyntaxTB.last_syntax_error:
1326 while self.SyntaxTB.last_syntax_error:
1327 # copy and clear last_syntax_error
1327 # copy and clear last_syntax_error
1328 err = self.SyntaxTB.clear_err_state()
1328 err = self.SyntaxTB.clear_err_state()
1329 if not self._should_recompile(err):
1329 if not self._should_recompile(err):
1330 return
1330 return
1331 try:
1331 try:
1332 # may set last_syntax_error again if a SyntaxError is raised
1332 # may set last_syntax_error again if a SyntaxError is raised
1333 self.safe_execfile(err.filename,self.user_ns)
1333 self.safe_execfile(err.filename,self.user_ns)
1334 except:
1334 except:
1335 self.showtraceback()
1335 self.showtraceback()
1336 else:
1336 else:
1337 try:
1337 try:
1338 f = file(err.filename)
1338 f = file(err.filename)
1339 try:
1339 try:
1340 # This should be inside a display_trap block and I
1340 # This should be inside a display_trap block and I
1341 # think it is.
1341 # think it is.
1342 sys.displayhook(f.read())
1342 sys.displayhook(f.read())
1343 finally:
1343 finally:
1344 f.close()
1344 f.close()
1345 except:
1345 except:
1346 self.showtraceback()
1346 self.showtraceback()
1347
1347
1348 def _should_recompile(self,e):
1348 def _should_recompile(self,e):
1349 """Utility routine for edit_syntax_error"""
1349 """Utility routine for edit_syntax_error"""
1350
1350
1351 if e.filename in ('<ipython console>','<input>','<string>',
1351 if e.filename in ('<ipython console>','<input>','<string>',
1352 '<console>','<BackgroundJob compilation>',
1352 '<console>','<BackgroundJob compilation>',
1353 None):
1353 None):
1354
1354
1355 return False
1355 return False
1356 try:
1356 try:
1357 if (self.autoedit_syntax and
1357 if (self.autoedit_syntax and
1358 not self.ask_yes_no('Return to editor to correct syntax error? '
1358 not self.ask_yes_no('Return to editor to correct syntax error? '
1359 '[Y/n] ','y')):
1359 '[Y/n] ','y')):
1360 return False
1360 return False
1361 except EOFError:
1361 except EOFError:
1362 return False
1362 return False
1363
1363
1364 def int0(x):
1364 def int0(x):
1365 try:
1365 try:
1366 return int(x)
1366 return int(x)
1367 except TypeError:
1367 except TypeError:
1368 return 0
1368 return 0
1369 # always pass integer line and offset values to editor hook
1369 # always pass integer line and offset values to editor hook
1370 try:
1370 try:
1371 self.hooks.fix_error_editor(e.filename,
1371 self.hooks.fix_error_editor(e.filename,
1372 int0(e.lineno),int0(e.offset),e.msg)
1372 int0(e.lineno),int0(e.offset),e.msg)
1373 except TryNext:
1373 except TryNext:
1374 warn('Could not open editor')
1374 warn('Could not open editor')
1375 return False
1375 return False
1376 return True
1376 return True
1377
1377
1378 #-------------------------------------------------------------------------
1378 #-------------------------------------------------------------------------
1379 # Things related to tab completion
1379 # Things related to tab completion
1380 #-------------------------------------------------------------------------
1380 #-------------------------------------------------------------------------
1381
1381
1382 def complete(self, text):
1382 def complete(self, text):
1383 """Return a sorted list of all possible completions on text.
1383 """Return a sorted list of all possible completions on text.
1384
1384
1385 Inputs:
1385 Inputs:
1386
1386
1387 - text: a string of text to be completed on.
1387 - text: a string of text to be completed on.
1388
1388
1389 This is a wrapper around the completion mechanism, similar to what
1389 This is a wrapper around the completion mechanism, similar to what
1390 readline does at the command line when the TAB key is hit. By
1390 readline does at the command line when the TAB key is hit. By
1391 exposing it as a method, it can be used by other non-readline
1391 exposing it as a method, it can be used by other non-readline
1392 environments (such as GUIs) for text completion.
1392 environments (such as GUIs) for text completion.
1393
1393
1394 Simple usage example:
1394 Simple usage example:
1395
1395
1396 In [7]: x = 'hello'
1396 In [7]: x = 'hello'
1397
1397
1398 In [8]: x
1398 In [8]: x
1399 Out[8]: 'hello'
1399 Out[8]: 'hello'
1400
1400
1401 In [9]: print x
1401 In [9]: print x
1402 hello
1402 hello
1403
1403
1404 In [10]: _ip.complete('x.l')
1404 In [10]: _ip.complete('x.l')
1405 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1405 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1406 """
1406 """
1407
1407
1408 # Inject names into __builtin__ so we can complete on the added names.
1408 # Inject names into __builtin__ so we can complete on the added names.
1409 with self.builtin_trap:
1409 with self.builtin_trap:
1410 complete = self.Completer.complete
1410 complete = self.Completer.complete
1411 state = 0
1411 state = 0
1412 # use a dict so we get unique keys, since ipyhton's multiple
1412 # use a dict so we get unique keys, since ipyhton's multiple
1413 # completers can return duplicates. When we make 2.4 a requirement,
1413 # completers can return duplicates. When we make 2.4 a requirement,
1414 # start using sets instead, which are faster.
1414 # start using sets instead, which are faster.
1415 comps = {}
1415 comps = {}
1416 while True:
1416 while True:
1417 newcomp = complete(text,state,line_buffer=text)
1417 newcomp = complete(text,state,line_buffer=text)
1418 if newcomp is None:
1418 if newcomp is None:
1419 break
1419 break
1420 comps[newcomp] = 1
1420 comps[newcomp] = 1
1421 state += 1
1421 state += 1
1422 outcomps = comps.keys()
1422 outcomps = comps.keys()
1423 outcomps.sort()
1423 outcomps.sort()
1424 #print "T:",text,"OC:",outcomps # dbg
1424 #print "T:",text,"OC:",outcomps # dbg
1425 #print "vars:",self.user_ns.keys()
1425 #print "vars:",self.user_ns.keys()
1426 return outcomps
1426 return outcomps
1427
1427
1428 def set_custom_completer(self,completer,pos=0):
1428 def set_custom_completer(self,completer,pos=0):
1429 """Adds a new custom completer function.
1429 """Adds a new custom completer function.
1430
1430
1431 The position argument (defaults to 0) is the index in the completers
1431 The position argument (defaults to 0) is the index in the completers
1432 list where you want the completer to be inserted."""
1432 list where you want the completer to be inserted."""
1433
1433
1434 newcomp = new.instancemethod(completer,self.Completer,
1434 newcomp = new.instancemethod(completer,self.Completer,
1435 self.Completer.__class__)
1435 self.Completer.__class__)
1436 self.Completer.matchers.insert(pos,newcomp)
1436 self.Completer.matchers.insert(pos,newcomp)
1437
1437
1438 def set_completer(self):
1438 def set_completer(self):
1439 """Reset readline's completer to be our own."""
1439 """Reset readline's completer to be our own."""
1440 self.readline.set_completer(self.Completer.complete)
1440 self.readline.set_completer(self.Completer.complete)
1441
1441
1442 def set_completer_frame(self, frame=None):
1442 def set_completer_frame(self, frame=None):
1443 """Set the frame of the completer."""
1443 """Set the frame of the completer."""
1444 if frame:
1444 if frame:
1445 self.Completer.namespace = frame.f_locals
1445 self.Completer.namespace = frame.f_locals
1446 self.Completer.global_namespace = frame.f_globals
1446 self.Completer.global_namespace = frame.f_globals
1447 else:
1447 else:
1448 self.Completer.namespace = self.user_ns
1448 self.Completer.namespace = self.user_ns
1449 self.Completer.global_namespace = self.user_global_ns
1449 self.Completer.global_namespace = self.user_global_ns
1450
1450
1451 #-------------------------------------------------------------------------
1451 #-------------------------------------------------------------------------
1452 # Things related to readline
1452 # Things related to readline
1453 #-------------------------------------------------------------------------
1453 #-------------------------------------------------------------------------
1454
1454
1455 def init_readline(self):
1455 def init_readline(self):
1456 """Command history completion/saving/reloading."""
1456 """Command history completion/saving/reloading."""
1457
1457
1458 self.rl_next_input = None
1458 self.rl_next_input = None
1459 self.rl_do_indent = False
1459 self.rl_do_indent = False
1460
1460
1461 if not self.readline_use:
1461 if not self.readline_use:
1462 return
1462 return
1463
1463
1464 import IPython.utils.rlineimpl as readline
1464 import IPython.utils.rlineimpl as readline
1465
1465
1466 if not readline.have_readline:
1466 if not readline.have_readline:
1467 self.has_readline = 0
1467 self.has_readline = 0
1468 self.readline = None
1468 self.readline = None
1469 # no point in bugging windows users with this every time:
1469 # no point in bugging windows users with this every time:
1470 warn('Readline services not available on this platform.')
1470 warn('Readline services not available on this platform.')
1471 else:
1471 else:
1472 sys.modules['readline'] = readline
1472 sys.modules['readline'] = readline
1473 import atexit
1473 import atexit
1474 from IPython.core.completer import IPCompleter
1474 from IPython.core.completer import IPCompleter
1475 self.Completer = IPCompleter(self,
1475 self.Completer = IPCompleter(self,
1476 self.user_ns,
1476 self.user_ns,
1477 self.user_global_ns,
1477 self.user_global_ns,
1478 self.readline_omit__names,
1478 self.readline_omit__names,
1479 self.alias_manager.alias_table)
1479 self.alias_manager.alias_table)
1480 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1480 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1481 self.strdispatchers['complete_command'] = sdisp
1481 self.strdispatchers['complete_command'] = sdisp
1482 self.Completer.custom_completers = sdisp
1482 self.Completer.custom_completers = sdisp
1483 # Platform-specific configuration
1483 # Platform-specific configuration
1484 if os.name == 'nt':
1484 if os.name == 'nt':
1485 self.readline_startup_hook = readline.set_pre_input_hook
1485 self.readline_startup_hook = readline.set_pre_input_hook
1486 else:
1486 else:
1487 self.readline_startup_hook = readline.set_startup_hook
1487 self.readline_startup_hook = readline.set_startup_hook
1488
1488
1489 # Load user's initrc file (readline config)
1489 # Load user's initrc file (readline config)
1490 # Or if libedit is used, load editrc.
1490 # Or if libedit is used, load editrc.
1491 inputrc_name = os.environ.get('INPUTRC')
1491 inputrc_name = os.environ.get('INPUTRC')
1492 if inputrc_name is None:
1492 if inputrc_name is None:
1493 home_dir = get_home_dir()
1493 home_dir = get_home_dir()
1494 if home_dir is not None:
1494 if home_dir is not None:
1495 inputrc_name = '.inputrc'
1495 inputrc_name = '.inputrc'
1496 if readline.uses_libedit:
1496 if readline.uses_libedit:
1497 inputrc_name = '.editrc'
1497 inputrc_name = '.editrc'
1498 inputrc_name = os.path.join(home_dir, inputrc_name)
1498 inputrc_name = os.path.join(home_dir, inputrc_name)
1499 if os.path.isfile(inputrc_name):
1499 if os.path.isfile(inputrc_name):
1500 try:
1500 try:
1501 readline.read_init_file(inputrc_name)
1501 readline.read_init_file(inputrc_name)
1502 except:
1502 except:
1503 warn('Problems reading readline initialization file <%s>'
1503 warn('Problems reading readline initialization file <%s>'
1504 % inputrc_name)
1504 % inputrc_name)
1505
1505
1506 self.has_readline = 1
1506 self.has_readline = 1
1507 self.readline = readline
1507 self.readline = readline
1508 # save this in sys so embedded copies can restore it properly
1508 # save this in sys so embedded copies can restore it properly
1509 sys.ipcompleter = self.Completer.complete
1509 sys.ipcompleter = self.Completer.complete
1510 self.set_completer()
1510 self.set_completer()
1511
1511
1512 # Configure readline according to user's prefs
1512 # Configure readline according to user's prefs
1513 # This is only done if GNU readline is being used. If libedit
1513 # This is only done if GNU readline is being used. If libedit
1514 # is being used (as on Leopard) the readline config is
1514 # is being used (as on Leopard) the readline config is
1515 # not run as the syntax for libedit is different.
1515 # not run as the syntax for libedit is different.
1516 if not readline.uses_libedit:
1516 if not readline.uses_libedit:
1517 for rlcommand in self.readline_parse_and_bind:
1517 for rlcommand in self.readline_parse_and_bind:
1518 #print "loading rl:",rlcommand # dbg
1518 #print "loading rl:",rlcommand # dbg
1519 readline.parse_and_bind(rlcommand)
1519 readline.parse_and_bind(rlcommand)
1520
1520
1521 # Remove some chars from the delimiters list. If we encounter
1521 # Remove some chars from the delimiters list. If we encounter
1522 # unicode chars, discard them.
1522 # unicode chars, discard them.
1523 delims = readline.get_completer_delims().encode("ascii", "ignore")
1523 delims = readline.get_completer_delims().encode("ascii", "ignore")
1524 delims = delims.translate(string._idmap,
1524 delims = delims.translate(string._idmap,
1525 self.readline_remove_delims)
1525 self.readline_remove_delims)
1526 readline.set_completer_delims(delims)
1526 readline.set_completer_delims(delims)
1527 # otherwise we end up with a monster history after a while:
1527 # otherwise we end up with a monster history after a while:
1528 readline.set_history_length(1000)
1528 readline.set_history_length(1000)
1529 try:
1529 try:
1530 #print '*** Reading readline history' # dbg
1530 #print '*** Reading readline history' # dbg
1531 readline.read_history_file(self.histfile)
1531 readline.read_history_file(self.histfile)
1532 except IOError:
1532 except IOError:
1533 pass # It doesn't exist yet.
1533 pass # It doesn't exist yet.
1534
1534
1535 atexit.register(self.atexit_operations)
1535 atexit.register(self.atexit_operations)
1536 del atexit
1536 del atexit
1537
1537
1538 # Configure auto-indent for all platforms
1538 # Configure auto-indent for all platforms
1539 self.set_autoindent(self.autoindent)
1539 self.set_autoindent(self.autoindent)
1540
1540
1541 def set_next_input(self, s):
1541 def set_next_input(self, s):
1542 """ Sets the 'default' input string for the next command line.
1542 """ Sets the 'default' input string for the next command line.
1543
1543
1544 Requires readline.
1544 Requires readline.
1545
1545
1546 Example:
1546 Example:
1547
1547
1548 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1548 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1549 [D:\ipython]|2> Hello Word_ # cursor is here
1549 [D:\ipython]|2> Hello Word_ # cursor is here
1550 """
1550 """
1551
1551
1552 self.rl_next_input = s
1552 self.rl_next_input = s
1553
1553
1554 def pre_readline(self):
1554 def pre_readline(self):
1555 """readline hook to be used at the start of each line.
1555 """readline hook to be used at the start of each line.
1556
1556
1557 Currently it handles auto-indent only."""
1557 Currently it handles auto-indent only."""
1558
1558
1559 #debugx('self.indent_current_nsp','pre_readline:')
1559 #debugx('self.indent_current_nsp','pre_readline:')
1560
1560
1561 if self.rl_do_indent:
1561 if self.rl_do_indent:
1562 self.readline.insert_text(self._indent_current_str())
1562 self.readline.insert_text(self._indent_current_str())
1563 if self.rl_next_input is not None:
1563 if self.rl_next_input is not None:
1564 self.readline.insert_text(self.rl_next_input)
1564 self.readline.insert_text(self.rl_next_input)
1565 self.rl_next_input = None
1565 self.rl_next_input = None
1566
1566
1567 def _indent_current_str(self):
1567 def _indent_current_str(self):
1568 """return the current level of indentation as a string"""
1568 """return the current level of indentation as a string"""
1569 return self.indent_current_nsp * ' '
1569 return self.indent_current_nsp * ' '
1570
1570
1571 #-------------------------------------------------------------------------
1571 #-------------------------------------------------------------------------
1572 # Things related to magics
1572 # Things related to magics
1573 #-------------------------------------------------------------------------
1573 #-------------------------------------------------------------------------
1574
1574
1575 def init_magics(self):
1575 def init_magics(self):
1576 # Set user colors (don't do it in the constructor above so that it
1576 # Set user colors (don't do it in the constructor above so that it
1577 # doesn't crash if colors option is invalid)
1577 # doesn't crash if colors option is invalid)
1578 self.magic_colors(self.colors)
1578 self.magic_colors(self.colors)
1579
1579
1580 def magic(self,arg_s):
1580 def magic(self,arg_s):
1581 """Call a magic function by name.
1581 """Call a magic function by name.
1582
1582
1583 Input: a string containing the name of the magic function to call and any
1583 Input: a string containing the name of the magic function to call and any
1584 additional arguments to be passed to the magic.
1584 additional arguments to be passed to the magic.
1585
1585
1586 magic('name -opt foo bar') is equivalent to typing at the ipython
1586 magic('name -opt foo bar') is equivalent to typing at the ipython
1587 prompt:
1587 prompt:
1588
1588
1589 In[1]: %name -opt foo bar
1589 In[1]: %name -opt foo bar
1590
1590
1591 To call a magic without arguments, simply use magic('name').
1591 To call a magic without arguments, simply use magic('name').
1592
1592
1593 This provides a proper Python function to call IPython's magics in any
1593 This provides a proper Python function to call IPython's magics in any
1594 valid Python code you can type at the interpreter, including loops and
1594 valid Python code you can type at the interpreter, including loops and
1595 compound statements.
1595 compound statements.
1596 """
1596 """
1597
1597
1598 args = arg_s.split(' ',1)
1598 args = arg_s.split(' ',1)
1599 magic_name = args[0]
1599 magic_name = args[0]
1600 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1600 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1601
1601
1602 try:
1602 try:
1603 magic_args = args[1]
1603 magic_args = args[1]
1604 except IndexError:
1604 except IndexError:
1605 magic_args = ''
1605 magic_args = ''
1606 fn = getattr(self,'magic_'+magic_name,None)
1606 fn = getattr(self,'magic_'+magic_name,None)
1607 if fn is None:
1607 if fn is None:
1608 error("Magic function `%s` not found." % magic_name)
1608 error("Magic function `%s` not found." % magic_name)
1609 else:
1609 else:
1610 magic_args = self.var_expand(magic_args,1)
1610 magic_args = self.var_expand(magic_args,1)
1611 with nested(self.builtin_trap,):
1611 with nested(self.builtin_trap,):
1612 result = fn(magic_args)
1612 result = fn(magic_args)
1613 return result
1613 return result
1614
1614
1615 def define_magic(self, magicname, func):
1615 def define_magic(self, magicname, func):
1616 """Expose own function as magic function for ipython
1616 """Expose own function as magic function for ipython
1617
1617
1618 def foo_impl(self,parameter_s=''):
1618 def foo_impl(self,parameter_s=''):
1619 'My very own magic!. (Use docstrings, IPython reads them).'
1619 'My very own magic!. (Use docstrings, IPython reads them).'
1620 print 'Magic function. Passed parameter is between < >:'
1620 print 'Magic function. Passed parameter is between < >:'
1621 print '<%s>' % parameter_s
1621 print '<%s>' % parameter_s
1622 print 'The self object is:',self
1622 print 'The self object is:',self
1623
1623
1624 self.define_magic('foo',foo_impl)
1624 self.define_magic('foo',foo_impl)
1625 """
1625 """
1626
1626
1627 import new
1627 import new
1628 im = new.instancemethod(func,self, self.__class__)
1628 im = new.instancemethod(func,self, self.__class__)
1629 old = getattr(self, "magic_" + magicname, None)
1629 old = getattr(self, "magic_" + magicname, None)
1630 setattr(self, "magic_" + magicname, im)
1630 setattr(self, "magic_" + magicname, im)
1631 return old
1631 return old
1632
1632
1633 #-------------------------------------------------------------------------
1633 #-------------------------------------------------------------------------
1634 # Things related to macros
1634 # Things related to macros
1635 #-------------------------------------------------------------------------
1635 #-------------------------------------------------------------------------
1636
1636
1637 def define_macro(self, name, themacro):
1637 def define_macro(self, name, themacro):
1638 """Define a new macro
1638 """Define a new macro
1639
1639
1640 Parameters
1640 Parameters
1641 ----------
1641 ----------
1642 name : str
1642 name : str
1643 The name of the macro.
1643 The name of the macro.
1644 themacro : str or Macro
1644 themacro : str or Macro
1645 The action to do upon invoking the macro. If a string, a new
1645 The action to do upon invoking the macro. If a string, a new
1646 Macro object is created by passing the string to it.
1646 Macro object is created by passing the string to it.
1647 """
1647 """
1648
1648
1649 from IPython.core import macro
1649 from IPython.core import macro
1650
1650
1651 if isinstance(themacro, basestring):
1651 if isinstance(themacro, basestring):
1652 themacro = macro.Macro(themacro)
1652 themacro = macro.Macro(themacro)
1653 if not isinstance(themacro, macro.Macro):
1653 if not isinstance(themacro, macro.Macro):
1654 raise ValueError('A macro must be a string or a Macro instance.')
1654 raise ValueError('A macro must be a string or a Macro instance.')
1655 self.user_ns[name] = themacro
1655 self.user_ns[name] = themacro
1656
1656
1657 #-------------------------------------------------------------------------
1657 #-------------------------------------------------------------------------
1658 # Things related to the running of system commands
1658 # Things related to the running of system commands
1659 #-------------------------------------------------------------------------
1659 #-------------------------------------------------------------------------
1660
1660
1661 def system(self, cmd):
1661 def system(self, cmd):
1662 """Make a system call, using IPython."""
1662 """Make a system call, using IPython."""
1663 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1663 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1664
1664
1665 #-------------------------------------------------------------------------
1665 #-------------------------------------------------------------------------
1666 # Things related to aliases
1666 # Things related to aliases
1667 #-------------------------------------------------------------------------
1667 #-------------------------------------------------------------------------
1668
1668
1669 def init_alias(self):
1669 def init_alias(self):
1670 self.alias_manager = AliasManager(self, config=self.config)
1670 self.alias_manager = AliasManager(self, config=self.config)
1671 self.ns_table['alias'] = self.alias_manager.alias_table,
1671 self.ns_table['alias'] = self.alias_manager.alias_table,
1672
1672
1673 #-------------------------------------------------------------------------
1673 #-------------------------------------------------------------------------
1674 # Things related to the running of code
1674 # Things related to the running of code
1675 #-------------------------------------------------------------------------
1675 #-------------------------------------------------------------------------
1676
1676
1677 def ex(self, cmd):
1677 def ex(self, cmd):
1678 """Execute a normal python statement in user namespace."""
1678 """Execute a normal python statement in user namespace."""
1679 with nested(self.builtin_trap,):
1679 with nested(self.builtin_trap,):
1680 exec cmd in self.user_global_ns, self.user_ns
1680 exec cmd in self.user_global_ns, self.user_ns
1681
1681
1682 def ev(self, expr):
1682 def ev(self, expr):
1683 """Evaluate python expression expr in user namespace.
1683 """Evaluate python expression expr in user namespace.
1684
1684
1685 Returns the result of evaluation
1685 Returns the result of evaluation
1686 """
1686 """
1687 with nested(self.builtin_trap,):
1687 with nested(self.builtin_trap,):
1688 return eval(expr, self.user_global_ns, self.user_ns)
1688 return eval(expr, self.user_global_ns, self.user_ns)
1689
1689
1690 def mainloop(self, display_banner=None):
1690 def mainloop(self, display_banner=None):
1691 """Start the mainloop.
1691 """Start the mainloop.
1692
1692
1693 If an optional banner argument is given, it will override the
1693 If an optional banner argument is given, it will override the
1694 internally created default banner.
1694 internally created default banner.
1695 """
1695 """
1696
1696
1697 with nested(self.builtin_trap, self.display_trap):
1697 with nested(self.builtin_trap, self.display_trap):
1698
1698
1699 # if you run stuff with -c <cmd>, raw hist is not updated
1699 # if you run stuff with -c <cmd>, raw hist is not updated
1700 # ensure that it's in sync
1700 # ensure that it's in sync
1701 if len(self.input_hist) != len (self.input_hist_raw):
1701 if len(self.input_hist) != len (self.input_hist_raw):
1702 self.input_hist_raw = InputList(self.input_hist)
1702 self.input_hist_raw = InputList(self.input_hist)
1703
1703
1704 while 1:
1704 while 1:
1705 try:
1705 try:
1706 self.interact(display_banner=display_banner)
1706 self.interact(display_banner=display_banner)
1707 #self.interact_with_readline()
1707 #self.interact_with_readline()
1708 # XXX for testing of a readline-decoupled repl loop, call
1708 # XXX for testing of a readline-decoupled repl loop, call
1709 # interact_with_readline above
1709 # interact_with_readline above
1710 break
1710 break
1711 except KeyboardInterrupt:
1711 except KeyboardInterrupt:
1712 # this should not be necessary, but KeyboardInterrupt
1712 # this should not be necessary, but KeyboardInterrupt
1713 # handling seems rather unpredictable...
1713 # handling seems rather unpredictable...
1714 self.write("\nKeyboardInterrupt in interact()\n")
1714 self.write("\nKeyboardInterrupt in interact()\n")
1715
1715
1716 def interact_prompt(self):
1716 def interact_prompt(self):
1717 """ Print the prompt (in read-eval-print loop)
1717 """ Print the prompt (in read-eval-print loop)
1718
1718
1719 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1719 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1720 used in standard IPython flow.
1720 used in standard IPython flow.
1721 """
1721 """
1722 if self.more:
1722 if self.more:
1723 try:
1723 try:
1724 prompt = self.hooks.generate_prompt(True)
1724 prompt = self.hooks.generate_prompt(True)
1725 except:
1725 except:
1726 self.showtraceback()
1726 self.showtraceback()
1727 if self.autoindent:
1727 if self.autoindent:
1728 self.rl_do_indent = True
1728 self.rl_do_indent = True
1729
1729
1730 else:
1730 else:
1731 try:
1731 try:
1732 prompt = self.hooks.generate_prompt(False)
1732 prompt = self.hooks.generate_prompt(False)
1733 except:
1733 except:
1734 self.showtraceback()
1734 self.showtraceback()
1735 self.write(prompt)
1735 self.write(prompt)
1736
1736
1737 def interact_handle_input(self,line):
1737 def interact_handle_input(self,line):
1738 """ Handle the input line (in read-eval-print loop)
1738 """ Handle the input line (in read-eval-print loop)
1739
1739
1740 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1740 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1741 used in standard IPython flow.
1741 used in standard IPython flow.
1742 """
1742 """
1743 if line.lstrip() == line:
1743 if line.lstrip() == line:
1744 self.shadowhist.add(line.strip())
1744 self.shadowhist.add(line.strip())
1745 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1745 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1746
1746
1747 if line.strip():
1747 if line.strip():
1748 if self.more:
1748 if self.more:
1749 self.input_hist_raw[-1] += '%s\n' % line
1749 self.input_hist_raw[-1] += '%s\n' % line
1750 else:
1750 else:
1751 self.input_hist_raw.append('%s\n' % line)
1751 self.input_hist_raw.append('%s\n' % line)
1752
1752
1753
1753
1754 self.more = self.push_line(lineout)
1754 self.more = self.push_line(lineout)
1755 if (self.SyntaxTB.last_syntax_error and
1755 if (self.SyntaxTB.last_syntax_error and
1756 self.autoedit_syntax):
1756 self.autoedit_syntax):
1757 self.edit_syntax_error()
1757 self.edit_syntax_error()
1758
1758
1759 def interact_with_readline(self):
1759 def interact_with_readline(self):
1760 """ Demo of using interact_handle_input, interact_prompt
1760 """ Demo of using interact_handle_input, interact_prompt
1761
1761
1762 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1762 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1763 it should work like this.
1763 it should work like this.
1764 """
1764 """
1765 self.readline_startup_hook(self.pre_readline)
1765 self.readline_startup_hook(self.pre_readline)
1766 while not self.exit_now:
1766 while not self.exit_now:
1767 self.interact_prompt()
1767 self.interact_prompt()
1768 if self.more:
1768 if self.more:
1769 self.rl_do_indent = True
1769 self.rl_do_indent = True
1770 else:
1770 else:
1771 self.rl_do_indent = False
1771 self.rl_do_indent = False
1772 line = raw_input_original().decode(self.stdin_encoding)
1772 line = raw_input_original().decode(self.stdin_encoding)
1773 self.interact_handle_input(line)
1773 self.interact_handle_input(line)
1774
1774
1775 def interact(self, display_banner=None):
1775 def interact(self, display_banner=None):
1776 """Closely emulate the interactive Python console."""
1776 """Closely emulate the interactive Python console."""
1777
1777
1778 # batch run -> do not interact
1778 # batch run -> do not interact
1779 if self.exit_now:
1779 if self.exit_now:
1780 return
1780 return
1781
1781
1782 if display_banner is None:
1782 if display_banner is None:
1783 display_banner = self.display_banner
1783 display_banner = self.display_banner
1784 if display_banner:
1784 if display_banner:
1785 self.show_banner()
1785 self.show_banner()
1786
1786
1787 more = 0
1787 more = 0
1788
1788
1789 # Mark activity in the builtins
1789 # Mark activity in the builtins
1790 __builtin__.__dict__['__IPYTHON__active'] += 1
1790 __builtin__.__dict__['__IPYTHON__active'] += 1
1791
1791
1792 if self.has_readline:
1792 if self.has_readline:
1793 self.readline_startup_hook(self.pre_readline)
1793 self.readline_startup_hook(self.pre_readline)
1794 # exit_now is set by a call to %Exit or %Quit, through the
1794 # exit_now is set by a call to %Exit or %Quit, through the
1795 # ask_exit callback.
1795 # ask_exit callback.
1796
1796
1797 while not self.exit_now:
1797 while not self.exit_now:
1798 self.hooks.pre_prompt_hook()
1798 self.hooks.pre_prompt_hook()
1799 if more:
1799 if more:
1800 try:
1800 try:
1801 prompt = self.hooks.generate_prompt(True)
1801 prompt = self.hooks.generate_prompt(True)
1802 except:
1802 except:
1803 self.showtraceback()
1803 self.showtraceback()
1804 if self.autoindent:
1804 if self.autoindent:
1805 self.rl_do_indent = True
1805 self.rl_do_indent = True
1806
1806
1807 else:
1807 else:
1808 try:
1808 try:
1809 prompt = self.hooks.generate_prompt(False)
1809 prompt = self.hooks.generate_prompt(False)
1810 except:
1810 except:
1811 self.showtraceback()
1811 self.showtraceback()
1812 try:
1812 try:
1813 line = self.raw_input(prompt, more)
1813 line = self.raw_input(prompt, more)
1814 if self.exit_now:
1814 if self.exit_now:
1815 # quick exit on sys.std[in|out] close
1815 # quick exit on sys.std[in|out] close
1816 break
1816 break
1817 if self.autoindent:
1817 if self.autoindent:
1818 self.rl_do_indent = False
1818 self.rl_do_indent = False
1819
1819
1820 except KeyboardInterrupt:
1820 except KeyboardInterrupt:
1821 #double-guard against keyboardinterrupts during kbdint handling
1821 #double-guard against keyboardinterrupts during kbdint handling
1822 try:
1822 try:
1823 self.write('\nKeyboardInterrupt\n')
1823 self.write('\nKeyboardInterrupt\n')
1824 self.resetbuffer()
1824 self.resetbuffer()
1825 # keep cache in sync with the prompt counter:
1825 # keep cache in sync with the prompt counter:
1826 self.outputcache.prompt_count -= 1
1826 self.outputcache.prompt_count -= 1
1827
1827
1828 if self.autoindent:
1828 if self.autoindent:
1829 self.indent_current_nsp = 0
1829 self.indent_current_nsp = 0
1830 more = 0
1830 more = 0
1831 except KeyboardInterrupt:
1831 except KeyboardInterrupt:
1832 pass
1832 pass
1833 except EOFError:
1833 except EOFError:
1834 if self.autoindent:
1834 if self.autoindent:
1835 self.rl_do_indent = False
1835 self.rl_do_indent = False
1836 self.readline_startup_hook(None)
1836 self.readline_startup_hook(None)
1837 self.write('\n')
1837 self.write('\n')
1838 self.exit()
1838 self.exit()
1839 except bdb.BdbQuit:
1839 except bdb.BdbQuit:
1840 warn('The Python debugger has exited with a BdbQuit exception.\n'
1840 warn('The Python debugger has exited with a BdbQuit exception.\n'
1841 'Because of how pdb handles the stack, it is impossible\n'
1841 'Because of how pdb handles the stack, it is impossible\n'
1842 'for IPython to properly format this particular exception.\n'
1842 'for IPython to properly format this particular exception.\n'
1843 'IPython will resume normal operation.')
1843 'IPython will resume normal operation.')
1844 except:
1844 except:
1845 # exceptions here are VERY RARE, but they can be triggered
1845 # exceptions here are VERY RARE, but they can be triggered
1846 # asynchronously by signal handlers, for example.
1846 # asynchronously by signal handlers, for example.
1847 self.showtraceback()
1847 self.showtraceback()
1848 else:
1848 else:
1849 more = self.push_line(line)
1849 more = self.push_line(line)
1850 if (self.SyntaxTB.last_syntax_error and
1850 if (self.SyntaxTB.last_syntax_error and
1851 self.autoedit_syntax):
1851 self.autoedit_syntax):
1852 self.edit_syntax_error()
1852 self.edit_syntax_error()
1853
1853
1854 # We are off again...
1854 # We are off again...
1855 __builtin__.__dict__['__IPYTHON__active'] -= 1
1855 __builtin__.__dict__['__IPYTHON__active'] -= 1
1856
1856
1857 def safe_execfile(self, fname, *where, **kw):
1857 def safe_execfile(self, fname, *where, **kw):
1858 """A safe version of the builtin execfile().
1858 """A safe version of the builtin execfile().
1859
1859
1860 This version will never throw an exception, but instead print
1860 This version will never throw an exception, but instead print
1861 helpful error messages to the screen. This only works on pure
1861 helpful error messages to the screen. This only works on pure
1862 Python files with the .py extension.
1862 Python files with the .py extension.
1863
1863
1864 Parameters
1864 Parameters
1865 ----------
1865 ----------
1866 fname : string
1866 fname : string
1867 The name of the file to be executed.
1867 The name of the file to be executed.
1868 where : tuple
1868 where : tuple
1869 One or two namespaces, passed to execfile() as (globals,locals).
1869 One or two namespaces, passed to execfile() as (globals,locals).
1870 If only one is given, it is passed as both.
1870 If only one is given, it is passed as both.
1871 exit_ignore : bool (False)
1871 exit_ignore : bool (False)
1872 If True, then don't print errors for non-zero exit statuses.
1872 If True, then don't print errors for non-zero exit statuses.
1873 """
1873 """
1874 kw.setdefault('exit_ignore', False)
1874 kw.setdefault('exit_ignore', False)
1875
1875
1876 fname = os.path.abspath(os.path.expanduser(fname))
1876 fname = os.path.abspath(os.path.expanduser(fname))
1877
1877
1878 # Make sure we have a .py file
1878 # Make sure we have a .py file
1879 if not fname.endswith('.py'):
1879 if not fname.endswith('.py'):
1880 warn('File must end with .py to be run using execfile: <%s>' % fname)
1880 warn('File must end with .py to be run using execfile: <%s>' % fname)
1881
1881
1882 # Make sure we can open the file
1882 # Make sure we can open the file
1883 try:
1883 try:
1884 with open(fname) as thefile:
1884 with open(fname) as thefile:
1885 pass
1885 pass
1886 except:
1886 except:
1887 warn('Could not open file <%s> for safe execution.' % fname)
1887 warn('Could not open file <%s> for safe execution.' % fname)
1888 return
1888 return
1889
1889
1890 # Find things also in current directory. This is needed to mimic the
1890 # Find things also in current directory. This is needed to mimic the
1891 # behavior of running a script from the system command line, where
1891 # behavior of running a script from the system command line, where
1892 # Python inserts the script's directory into sys.path
1892 # Python inserts the script's directory into sys.path
1893 dname = os.path.dirname(fname)
1893 dname = os.path.dirname(fname)
1894
1894
1895 with prepended_to_syspath(dname):
1895 with prepended_to_syspath(dname):
1896 try:
1896 try:
1897 if sys.platform == 'win32' and sys.version_info < (2,5,1):
1897 if sys.platform == 'win32' and sys.version_info < (2,5,1):
1898 # Work around a bug in Python for Windows. The bug was
1898 # Work around a bug in Python for Windows. The bug was
1899 # fixed in in Python 2.5 r54159 and 54158, but that's still
1899 # fixed in in Python 2.5 r54159 and 54158, but that's still
1900 # SVN Python as of March/07. For details, see:
1900 # SVN Python as of March/07. For details, see:
1901 # http://projects.scipy.org/ipython/ipython/ticket/123
1901 # http://projects.scipy.org/ipython/ipython/ticket/123
1902 try:
1902 try:
1903 globs,locs = where[0:2]
1903 globs,locs = where[0:2]
1904 except:
1904 except:
1905 try:
1905 try:
1906 globs = locs = where[0]
1906 globs = locs = where[0]
1907 except:
1907 except:
1908 globs = locs = globals()
1908 globs = locs = globals()
1909 exec file(fname) in globs,locs
1909 exec file(fname) in globs,locs
1910 else:
1910 else:
1911 execfile(fname,*where)
1911 execfile(fname,*where)
1912 except SyntaxError:
1912 except SyntaxError:
1913 self.showsyntaxerror()
1913 self.showsyntaxerror()
1914 warn('Failure executing file: <%s>' % fname)
1914 warn('Failure executing file: <%s>' % fname)
1915 except SystemExit, status:
1915 except SystemExit, status:
1916 # Code that correctly sets the exit status flag to success (0)
1916 # Code that correctly sets the exit status flag to success (0)
1917 # shouldn't be bothered with a traceback. Note that a plain
1917 # shouldn't be bothered with a traceback. Note that a plain
1918 # sys.exit() does NOT set the message to 0 (it's empty) so that
1918 # sys.exit() does NOT set the message to 0 (it's empty) so that
1919 # will still get a traceback. Note that the structure of the
1919 # will still get a traceback. Note that the structure of the
1920 # SystemExit exception changed between Python 2.4 and 2.5, so
1920 # SystemExit exception changed between Python 2.4 and 2.5, so
1921 # the checks must be done in a version-dependent way.
1921 # the checks must be done in a version-dependent way.
1922 show = False
1922 show = False
1923 if status.message!=0 and not kw['exit_ignore']:
1923 if status.args[0]==0 and not kw['exit_ignore']:
1924 show = True
1924 show = True
1925 if show:
1925 if show:
1926 self.showtraceback()
1926 self.showtraceback()
1927 warn('Failure executing file: <%s>' % fname)
1927 warn('Failure executing file: <%s>' % fname)
1928 except:
1928 except:
1929 self.showtraceback()
1929 self.showtraceback()
1930 warn('Failure executing file: <%s>' % fname)
1930 warn('Failure executing file: <%s>' % fname)
1931
1931
1932 def safe_execfile_ipy(self, fname):
1932 def safe_execfile_ipy(self, fname):
1933 """Like safe_execfile, but for .ipy files with IPython syntax.
1933 """Like safe_execfile, but for .ipy files with IPython syntax.
1934
1934
1935 Parameters
1935 Parameters
1936 ----------
1936 ----------
1937 fname : str
1937 fname : str
1938 The name of the file to execute. The filename must have a
1938 The name of the file to execute. The filename must have a
1939 .ipy extension.
1939 .ipy extension.
1940 """
1940 """
1941 fname = os.path.abspath(os.path.expanduser(fname))
1941 fname = os.path.abspath(os.path.expanduser(fname))
1942
1942
1943 # Make sure we have a .py file
1943 # Make sure we have a .py file
1944 if not fname.endswith('.ipy'):
1944 if not fname.endswith('.ipy'):
1945 warn('File must end with .py to be run using execfile: <%s>' % fname)
1945 warn('File must end with .py to be run using execfile: <%s>' % fname)
1946
1946
1947 # Make sure we can open the file
1947 # Make sure we can open the file
1948 try:
1948 try:
1949 with open(fname) as thefile:
1949 with open(fname) as thefile:
1950 pass
1950 pass
1951 except:
1951 except:
1952 warn('Could not open file <%s> for safe execution.' % fname)
1952 warn('Could not open file <%s> for safe execution.' % fname)
1953 return
1953 return
1954
1954
1955 # Find things also in current directory. This is needed to mimic the
1955 # Find things also in current directory. This is needed to mimic the
1956 # behavior of running a script from the system command line, where
1956 # behavior of running a script from the system command line, where
1957 # Python inserts the script's directory into sys.path
1957 # Python inserts the script's directory into sys.path
1958 dname = os.path.dirname(fname)
1958 dname = os.path.dirname(fname)
1959
1959
1960 with prepended_to_syspath(dname):
1960 with prepended_to_syspath(dname):
1961 try:
1961 try:
1962 with open(fname) as thefile:
1962 with open(fname) as thefile:
1963 script = thefile.read()
1963 script = thefile.read()
1964 # self.runlines currently captures all exceptions
1964 # self.runlines currently captures all exceptions
1965 # raise in user code. It would be nice if there were
1965 # raise in user code. It would be nice if there were
1966 # versions of runlines, execfile that did raise, so
1966 # versions of runlines, execfile that did raise, so
1967 # we could catch the errors.
1967 # we could catch the errors.
1968 self.runlines(script, clean=True)
1968 self.runlines(script, clean=True)
1969 except:
1969 except:
1970 self.showtraceback()
1970 self.showtraceback()
1971 warn('Unknown failure executing file: <%s>' % fname)
1971 warn('Unknown failure executing file: <%s>' % fname)
1972
1972
1973 def _is_secondary_block_start(self, s):
1973 def _is_secondary_block_start(self, s):
1974 if not s.endswith(':'):
1974 if not s.endswith(':'):
1975 return False
1975 return False
1976 if (s.startswith('elif') or
1976 if (s.startswith('elif') or
1977 s.startswith('else') or
1977 s.startswith('else') or
1978 s.startswith('except') or
1978 s.startswith('except') or
1979 s.startswith('finally')):
1979 s.startswith('finally')):
1980 return True
1980 return True
1981
1981
1982 def cleanup_ipy_script(self, script):
1982 def cleanup_ipy_script(self, script):
1983 """Make a script safe for self.runlines()
1983 """Make a script safe for self.runlines()
1984
1984
1985 Currently, IPython is lines based, with blocks being detected by
1985 Currently, IPython is lines based, with blocks being detected by
1986 empty lines. This is a problem for block based scripts that may
1986 empty lines. This is a problem for block based scripts that may
1987 not have empty lines after blocks. This script adds those empty
1987 not have empty lines after blocks. This script adds those empty
1988 lines to make scripts safe for running in the current line based
1988 lines to make scripts safe for running in the current line based
1989 IPython.
1989 IPython.
1990 """
1990 """
1991 res = []
1991 res = []
1992 lines = script.splitlines()
1992 lines = script.splitlines()
1993 level = 0
1993 level = 0
1994
1994
1995 for l in lines:
1995 for l in lines:
1996 lstripped = l.lstrip()
1996 lstripped = l.lstrip()
1997 stripped = l.strip()
1997 stripped = l.strip()
1998 if not stripped:
1998 if not stripped:
1999 continue
1999 continue
2000 newlevel = len(l) - len(lstripped)
2000 newlevel = len(l) - len(lstripped)
2001 if level > 0 and newlevel == 0 and \
2001 if level > 0 and newlevel == 0 and \
2002 not self._is_secondary_block_start(stripped):
2002 not self._is_secondary_block_start(stripped):
2003 # add empty line
2003 # add empty line
2004 res.append('')
2004 res.append('')
2005 res.append(l)
2005 res.append(l)
2006 level = newlevel
2006 level = newlevel
2007
2007
2008 return '\n'.join(res) + '\n'
2008 return '\n'.join(res) + '\n'
2009
2009
2010 def runlines(self, lines, clean=False):
2010 def runlines(self, lines, clean=False):
2011 """Run a string of one or more lines of source.
2011 """Run a string of one or more lines of source.
2012
2012
2013 This method is capable of running a string containing multiple source
2013 This method is capable of running a string containing multiple source
2014 lines, as if they had been entered at the IPython prompt. Since it
2014 lines, as if they had been entered at the IPython prompt. Since it
2015 exposes IPython's processing machinery, the given strings can contain
2015 exposes IPython's processing machinery, the given strings can contain
2016 magic calls (%magic), special shell access (!cmd), etc.
2016 magic calls (%magic), special shell access (!cmd), etc.
2017 """
2017 """
2018
2018
2019 if isinstance(lines, (list, tuple)):
2019 if isinstance(lines, (list, tuple)):
2020 lines = '\n'.join(lines)
2020 lines = '\n'.join(lines)
2021
2021
2022 if clean:
2022 if clean:
2023 lines = self.cleanup_ipy_script(lines)
2023 lines = self.cleanup_ipy_script(lines)
2024
2024
2025 # We must start with a clean buffer, in case this is run from an
2025 # We must start with a clean buffer, in case this is run from an
2026 # interactive IPython session (via a magic, for example).
2026 # interactive IPython session (via a magic, for example).
2027 self.resetbuffer()
2027 self.resetbuffer()
2028 lines = lines.splitlines()
2028 lines = lines.splitlines()
2029 more = 0
2029 more = 0
2030
2030
2031 with nested(self.builtin_trap, self.display_trap):
2031 with nested(self.builtin_trap, self.display_trap):
2032 for line in lines:
2032 for line in lines:
2033 # skip blank lines so we don't mess up the prompt counter, but do
2033 # skip blank lines so we don't mess up the prompt counter, but do
2034 # NOT skip even a blank line if we are in a code block (more is
2034 # NOT skip even a blank line if we are in a code block (more is
2035 # true)
2035 # true)
2036
2036
2037 if line or more:
2037 if line or more:
2038 # push to raw history, so hist line numbers stay in sync
2038 # push to raw history, so hist line numbers stay in sync
2039 self.input_hist_raw.append("# " + line + "\n")
2039 self.input_hist_raw.append("# " + line + "\n")
2040 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2040 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2041 more = self.push_line(prefiltered)
2041 more = self.push_line(prefiltered)
2042 # IPython's runsource returns None if there was an error
2042 # IPython's runsource returns None if there was an error
2043 # compiling the code. This allows us to stop processing right
2043 # compiling the code. This allows us to stop processing right
2044 # away, so the user gets the error message at the right place.
2044 # away, so the user gets the error message at the right place.
2045 if more is None:
2045 if more is None:
2046 break
2046 break
2047 else:
2047 else:
2048 self.input_hist_raw.append("\n")
2048 self.input_hist_raw.append("\n")
2049 # final newline in case the input didn't have it, so that the code
2049 # final newline in case the input didn't have it, so that the code
2050 # actually does get executed
2050 # actually does get executed
2051 if more:
2051 if more:
2052 self.push_line('\n')
2052 self.push_line('\n')
2053
2053
2054 def runsource(self, source, filename='<input>', symbol='single'):
2054 def runsource(self, source, filename='<input>', symbol='single'):
2055 """Compile and run some source in the interpreter.
2055 """Compile and run some source in the interpreter.
2056
2056
2057 Arguments are as for compile_command().
2057 Arguments are as for compile_command().
2058
2058
2059 One several things can happen:
2059 One several things can happen:
2060
2060
2061 1) The input is incorrect; compile_command() raised an
2061 1) The input is incorrect; compile_command() raised an
2062 exception (SyntaxError or OverflowError). A syntax traceback
2062 exception (SyntaxError or OverflowError). A syntax traceback
2063 will be printed by calling the showsyntaxerror() method.
2063 will be printed by calling the showsyntaxerror() method.
2064
2064
2065 2) The input is incomplete, and more input is required;
2065 2) The input is incomplete, and more input is required;
2066 compile_command() returned None. Nothing happens.
2066 compile_command() returned None. Nothing happens.
2067
2067
2068 3) The input is complete; compile_command() returned a code
2068 3) The input is complete; compile_command() returned a code
2069 object. The code is executed by calling self.runcode() (which
2069 object. The code is executed by calling self.runcode() (which
2070 also handles run-time exceptions, except for SystemExit).
2070 also handles run-time exceptions, except for SystemExit).
2071
2071
2072 The return value is:
2072 The return value is:
2073
2073
2074 - True in case 2
2074 - True in case 2
2075
2075
2076 - False in the other cases, unless an exception is raised, where
2076 - False in the other cases, unless an exception is raised, where
2077 None is returned instead. This can be used by external callers to
2077 None is returned instead. This can be used by external callers to
2078 know whether to continue feeding input or not.
2078 know whether to continue feeding input or not.
2079
2079
2080 The return value can be used to decide whether to use sys.ps1 or
2080 The return value can be used to decide whether to use sys.ps1 or
2081 sys.ps2 to prompt the next line."""
2081 sys.ps2 to prompt the next line."""
2082
2082
2083 # if the source code has leading blanks, add 'if 1:\n' to it
2083 # if the source code has leading blanks, add 'if 1:\n' to it
2084 # this allows execution of indented pasted code. It is tempting
2084 # this allows execution of indented pasted code. It is tempting
2085 # to add '\n' at the end of source to run commands like ' a=1'
2085 # to add '\n' at the end of source to run commands like ' a=1'
2086 # directly, but this fails for more complicated scenarios
2086 # directly, but this fails for more complicated scenarios
2087 source=source.encode(self.stdin_encoding)
2087 source=source.encode(self.stdin_encoding)
2088 if source[:1] in [' ', '\t']:
2088 if source[:1] in [' ', '\t']:
2089 source = 'if 1:\n%s' % source
2089 source = 'if 1:\n%s' % source
2090
2090
2091 try:
2091 try:
2092 code = self.compile(source,filename,symbol)
2092 code = self.compile(source,filename,symbol)
2093 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2093 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2094 # Case 1
2094 # Case 1
2095 self.showsyntaxerror(filename)
2095 self.showsyntaxerror(filename)
2096 return None
2096 return None
2097
2097
2098 if code is None:
2098 if code is None:
2099 # Case 2
2099 # Case 2
2100 return True
2100 return True
2101
2101
2102 # Case 3
2102 # Case 3
2103 # We store the code object so that threaded shells and
2103 # We store the code object so that threaded shells and
2104 # custom exception handlers can access all this info if needed.
2104 # custom exception handlers can access all this info if needed.
2105 # The source corresponding to this can be obtained from the
2105 # The source corresponding to this can be obtained from the
2106 # buffer attribute as '\n'.join(self.buffer).
2106 # buffer attribute as '\n'.join(self.buffer).
2107 self.code_to_run = code
2107 self.code_to_run = code
2108 # now actually execute the code object
2108 # now actually execute the code object
2109 if self.runcode(code) == 0:
2109 if self.runcode(code) == 0:
2110 return False
2110 return False
2111 else:
2111 else:
2112 return None
2112 return None
2113
2113
2114 def runcode(self,code_obj):
2114 def runcode(self,code_obj):
2115 """Execute a code object.
2115 """Execute a code object.
2116
2116
2117 When an exception occurs, self.showtraceback() is called to display a
2117 When an exception occurs, self.showtraceback() is called to display a
2118 traceback.
2118 traceback.
2119
2119
2120 Return value: a flag indicating whether the code to be run completed
2120 Return value: a flag indicating whether the code to be run completed
2121 successfully:
2121 successfully:
2122
2122
2123 - 0: successful execution.
2123 - 0: successful execution.
2124 - 1: an error occurred.
2124 - 1: an error occurred.
2125 """
2125 """
2126
2126
2127 # Set our own excepthook in case the user code tries to call it
2127 # Set our own excepthook in case the user code tries to call it
2128 # directly, so that the IPython crash handler doesn't get triggered
2128 # directly, so that the IPython crash handler doesn't get triggered
2129 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2129 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2130
2130
2131 # we save the original sys.excepthook in the instance, in case config
2131 # we save the original sys.excepthook in the instance, in case config
2132 # code (such as magics) needs access to it.
2132 # code (such as magics) needs access to it.
2133 self.sys_excepthook = old_excepthook
2133 self.sys_excepthook = old_excepthook
2134 outflag = 1 # happens in more places, so it's easier as default
2134 outflag = 1 # happens in more places, so it's easier as default
2135 try:
2135 try:
2136 try:
2136 try:
2137 self.hooks.pre_runcode_hook()
2137 self.hooks.pre_runcode_hook()
2138 exec code_obj in self.user_global_ns, self.user_ns
2138 exec code_obj in self.user_global_ns, self.user_ns
2139 finally:
2139 finally:
2140 # Reset our crash handler in place
2140 # Reset our crash handler in place
2141 sys.excepthook = old_excepthook
2141 sys.excepthook = old_excepthook
2142 except SystemExit:
2142 except SystemExit:
2143 self.resetbuffer()
2143 self.resetbuffer()
2144 self.showtraceback()
2144 self.showtraceback()
2145 warn("Type %exit or %quit to exit IPython "
2145 warn("Type %exit or %quit to exit IPython "
2146 "(%Exit or %Quit do so unconditionally).",level=1)
2146 "(%Exit or %Quit do so unconditionally).",level=1)
2147 except self.custom_exceptions:
2147 except self.custom_exceptions:
2148 etype,value,tb = sys.exc_info()
2148 etype,value,tb = sys.exc_info()
2149 self.CustomTB(etype,value,tb)
2149 self.CustomTB(etype,value,tb)
2150 except:
2150 except:
2151 self.showtraceback()
2151 self.showtraceback()
2152 else:
2152 else:
2153 outflag = 0
2153 outflag = 0
2154 if softspace(sys.stdout, 0):
2154 if softspace(sys.stdout, 0):
2155 print
2155 print
2156 # Flush out code object which has been run (and source)
2156 # Flush out code object which has been run (and source)
2157 self.code_to_run = None
2157 self.code_to_run = None
2158 return outflag
2158 return outflag
2159
2159
2160 def push_line(self, line):
2160 def push_line(self, line):
2161 """Push a line to the interpreter.
2161 """Push a line to the interpreter.
2162
2162
2163 The line should not have a trailing newline; it may have
2163 The line should not have a trailing newline; it may have
2164 internal newlines. The line is appended to a buffer and the
2164 internal newlines. The line is appended to a buffer and the
2165 interpreter's runsource() method is called with the
2165 interpreter's runsource() method is called with the
2166 concatenated contents of the buffer as source. If this
2166 concatenated contents of the buffer as source. If this
2167 indicates that the command was executed or invalid, the buffer
2167 indicates that the command was executed or invalid, the buffer
2168 is reset; otherwise, the command is incomplete, and the buffer
2168 is reset; otherwise, the command is incomplete, and the buffer
2169 is left as it was after the line was appended. The return
2169 is left as it was after the line was appended. The return
2170 value is 1 if more input is required, 0 if the line was dealt
2170 value is 1 if more input is required, 0 if the line was dealt
2171 with in some way (this is the same as runsource()).
2171 with in some way (this is the same as runsource()).
2172 """
2172 """
2173
2173
2174 # autoindent management should be done here, and not in the
2174 # autoindent management should be done here, and not in the
2175 # interactive loop, since that one is only seen by keyboard input. We
2175 # interactive loop, since that one is only seen by keyboard input. We
2176 # need this done correctly even for code run via runlines (which uses
2176 # need this done correctly even for code run via runlines (which uses
2177 # push).
2177 # push).
2178
2178
2179 #print 'push line: <%s>' % line # dbg
2179 #print 'push line: <%s>' % line # dbg
2180 for subline in line.splitlines():
2180 for subline in line.splitlines():
2181 self._autoindent_update(subline)
2181 self._autoindent_update(subline)
2182 self.buffer.append(line)
2182 self.buffer.append(line)
2183 more = self.runsource('\n'.join(self.buffer), self.filename)
2183 more = self.runsource('\n'.join(self.buffer), self.filename)
2184 if not more:
2184 if not more:
2185 self.resetbuffer()
2185 self.resetbuffer()
2186 return more
2186 return more
2187
2187
2188 def _autoindent_update(self,line):
2188 def _autoindent_update(self,line):
2189 """Keep track of the indent level."""
2189 """Keep track of the indent level."""
2190
2190
2191 #debugx('line')
2191 #debugx('line')
2192 #debugx('self.indent_current_nsp')
2192 #debugx('self.indent_current_nsp')
2193 if self.autoindent:
2193 if self.autoindent:
2194 if line:
2194 if line:
2195 inisp = num_ini_spaces(line)
2195 inisp = num_ini_spaces(line)
2196 if inisp < self.indent_current_nsp:
2196 if inisp < self.indent_current_nsp:
2197 self.indent_current_nsp = inisp
2197 self.indent_current_nsp = inisp
2198
2198
2199 if line[-1] == ':':
2199 if line[-1] == ':':
2200 self.indent_current_nsp += 4
2200 self.indent_current_nsp += 4
2201 elif dedent_re.match(line):
2201 elif dedent_re.match(line):
2202 self.indent_current_nsp -= 4
2202 self.indent_current_nsp -= 4
2203 else:
2203 else:
2204 self.indent_current_nsp = 0
2204 self.indent_current_nsp = 0
2205
2205
2206 def resetbuffer(self):
2206 def resetbuffer(self):
2207 """Reset the input buffer."""
2207 """Reset the input buffer."""
2208 self.buffer[:] = []
2208 self.buffer[:] = []
2209
2209
2210 def raw_input(self,prompt='',continue_prompt=False):
2210 def raw_input(self,prompt='',continue_prompt=False):
2211 """Write a prompt and read a line.
2211 """Write a prompt and read a line.
2212
2212
2213 The returned line does not include the trailing newline.
2213 The returned line does not include the trailing newline.
2214 When the user enters the EOF key sequence, EOFError is raised.
2214 When the user enters the EOF key sequence, EOFError is raised.
2215
2215
2216 Optional inputs:
2216 Optional inputs:
2217
2217
2218 - prompt(''): a string to be printed to prompt the user.
2218 - prompt(''): a string to be printed to prompt the user.
2219
2219
2220 - continue_prompt(False): whether this line is the first one or a
2220 - continue_prompt(False): whether this line is the first one or a
2221 continuation in a sequence of inputs.
2221 continuation in a sequence of inputs.
2222 """
2222 """
2223 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2223 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2224
2224
2225 # Code run by the user may have modified the readline completer state.
2225 # Code run by the user may have modified the readline completer state.
2226 # We must ensure that our completer is back in place.
2226 # We must ensure that our completer is back in place.
2227
2227
2228 if self.has_readline:
2228 if self.has_readline:
2229 self.set_completer()
2229 self.set_completer()
2230
2230
2231 try:
2231 try:
2232 line = raw_input_original(prompt).decode(self.stdin_encoding)
2232 line = raw_input_original(prompt).decode(self.stdin_encoding)
2233 except ValueError:
2233 except ValueError:
2234 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2234 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2235 " or sys.stdout.close()!\nExiting IPython!")
2235 " or sys.stdout.close()!\nExiting IPython!")
2236 self.ask_exit()
2236 self.ask_exit()
2237 return ""
2237 return ""
2238
2238
2239 # Try to be reasonably smart about not re-indenting pasted input more
2239 # Try to be reasonably smart about not re-indenting pasted input more
2240 # than necessary. We do this by trimming out the auto-indent initial
2240 # than necessary. We do this by trimming out the auto-indent initial
2241 # spaces, if the user's actual input started itself with whitespace.
2241 # spaces, if the user's actual input started itself with whitespace.
2242 #debugx('self.buffer[-1]')
2242 #debugx('self.buffer[-1]')
2243
2243
2244 if self.autoindent:
2244 if self.autoindent:
2245 if num_ini_spaces(line) > self.indent_current_nsp:
2245 if num_ini_spaces(line) > self.indent_current_nsp:
2246 line = line[self.indent_current_nsp:]
2246 line = line[self.indent_current_nsp:]
2247 self.indent_current_nsp = 0
2247 self.indent_current_nsp = 0
2248
2248
2249 # store the unfiltered input before the user has any chance to modify
2249 # store the unfiltered input before the user has any chance to modify
2250 # it.
2250 # it.
2251 if line.strip():
2251 if line.strip():
2252 if continue_prompt:
2252 if continue_prompt:
2253 self.input_hist_raw[-1] += '%s\n' % line
2253 self.input_hist_raw[-1] += '%s\n' % line
2254 if self.has_readline and self.readline_use:
2254 if self.has_readline and self.readline_use:
2255 try:
2255 try:
2256 histlen = self.readline.get_current_history_length()
2256 histlen = self.readline.get_current_history_length()
2257 if histlen > 1:
2257 if histlen > 1:
2258 newhist = self.input_hist_raw[-1].rstrip()
2258 newhist = self.input_hist_raw[-1].rstrip()
2259 self.readline.remove_history_item(histlen-1)
2259 self.readline.remove_history_item(histlen-1)
2260 self.readline.replace_history_item(histlen-2,
2260 self.readline.replace_history_item(histlen-2,
2261 newhist.encode(self.stdin_encoding))
2261 newhist.encode(self.stdin_encoding))
2262 except AttributeError:
2262 except AttributeError:
2263 pass # re{move,place}_history_item are new in 2.4.
2263 pass # re{move,place}_history_item are new in 2.4.
2264 else:
2264 else:
2265 self.input_hist_raw.append('%s\n' % line)
2265 self.input_hist_raw.append('%s\n' % line)
2266 # only entries starting at first column go to shadow history
2266 # only entries starting at first column go to shadow history
2267 if line.lstrip() == line:
2267 if line.lstrip() == line:
2268 self.shadowhist.add(line.strip())
2268 self.shadowhist.add(line.strip())
2269 elif not continue_prompt:
2269 elif not continue_prompt:
2270 self.input_hist_raw.append('\n')
2270 self.input_hist_raw.append('\n')
2271 try:
2271 try:
2272 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2272 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2273 except:
2273 except:
2274 # blanket except, in case a user-defined prefilter crashes, so it
2274 # blanket except, in case a user-defined prefilter crashes, so it
2275 # can't take all of ipython with it.
2275 # can't take all of ipython with it.
2276 self.showtraceback()
2276 self.showtraceback()
2277 return ''
2277 return ''
2278 else:
2278 else:
2279 return lineout
2279 return lineout
2280
2280
2281 #-------------------------------------------------------------------------
2281 #-------------------------------------------------------------------------
2282 # Working with components
2282 # Working with components
2283 #-------------------------------------------------------------------------
2283 #-------------------------------------------------------------------------
2284
2284
2285 def get_component(self, name=None, klass=None):
2285 def get_component(self, name=None, klass=None):
2286 """Fetch a component by name and klass in my tree."""
2286 """Fetch a component by name and klass in my tree."""
2287 c = Component.get_instances(root=self, name=name, klass=klass)
2287 c = Component.get_instances(root=self, name=name, klass=klass)
2288 if len(c) == 1:
2288 if len(c) == 1:
2289 return c[0]
2289 return c[0]
2290 else:
2290 else:
2291 return c
2291 return c
2292
2292
2293 #-------------------------------------------------------------------------
2293 #-------------------------------------------------------------------------
2294 # IPython extensions
2294 # IPython extensions
2295 #-------------------------------------------------------------------------
2295 #-------------------------------------------------------------------------
2296
2296
2297 def load_extension(self, module_str):
2297 def load_extension(self, module_str):
2298 """Load an IPython extension by its module name.
2298 """Load an IPython extension by its module name.
2299
2299
2300 An IPython extension is an importable Python module that has
2300 An IPython extension is an importable Python module that has
2301 a function with the signature::
2301 a function with the signature::
2302
2302
2303 def load_ipython_extension(ipython):
2303 def load_ipython_extension(ipython):
2304 # Do things with ipython
2304 # Do things with ipython
2305
2305
2306 This function is called after your extension is imported and the
2306 This function is called after your extension is imported and the
2307 currently active :class:`InteractiveShell` instance is passed as
2307 currently active :class:`InteractiveShell` instance is passed as
2308 the only argument. You can do anything you want with IPython at
2308 the only argument. You can do anything you want with IPython at
2309 that point, including defining new magic and aliases, adding new
2309 that point, including defining new magic and aliases, adding new
2310 components, etc.
2310 components, etc.
2311
2311
2312 The :func:`load_ipython_extension` will be called again is you
2312 The :func:`load_ipython_extension` will be called again is you
2313 load or reload the extension again. It is up to the extension
2313 load or reload the extension again. It is up to the extension
2314 author to add code to manage that.
2314 author to add code to manage that.
2315
2315
2316 You can put your extension modules anywhere you want, as long as
2316 You can put your extension modules anywhere you want, as long as
2317 they can be imported by Python's standard import mechanism. However,
2317 they can be imported by Python's standard import mechanism. However,
2318 to make it easy to write extensions, you can also put your extensions
2318 to make it easy to write extensions, you can also put your extensions
2319 in ``os.path.join(self.ipythondir, 'extensions')``. This directory
2319 in ``os.path.join(self.ipythondir, 'extensions')``. This directory
2320 is added to ``sys.path`` automatically.
2320 is added to ``sys.path`` automatically.
2321 """
2321 """
2322 from IPython.utils.syspathcontext import prepended_to_syspath
2322 from IPython.utils.syspathcontext import prepended_to_syspath
2323
2323
2324 if module_str not in sys.modules:
2324 if module_str not in sys.modules:
2325 with prepended_to_syspath(self.ipython_extension_dir):
2325 with prepended_to_syspath(self.ipython_extension_dir):
2326 __import__(module_str)
2326 __import__(module_str)
2327 mod = sys.modules[module_str]
2327 mod = sys.modules[module_str]
2328 self._call_load_ipython_extension(mod)
2328 self._call_load_ipython_extension(mod)
2329
2329
2330 def unload_extension(self, module_str):
2330 def unload_extension(self, module_str):
2331 """Unload an IPython extension by its module name.
2331 """Unload an IPython extension by its module name.
2332
2332
2333 This function looks up the extension's name in ``sys.modules`` and
2333 This function looks up the extension's name in ``sys.modules`` and
2334 simply calls ``mod.unload_ipython_extension(self)``.
2334 simply calls ``mod.unload_ipython_extension(self)``.
2335 """
2335 """
2336 if module_str in sys.modules:
2336 if module_str in sys.modules:
2337 mod = sys.modules[module_str]
2337 mod = sys.modules[module_str]
2338 self._call_unload_ipython_extension(mod)
2338 self._call_unload_ipython_extension(mod)
2339
2339
2340 def reload_extension(self, module_str):
2340 def reload_extension(self, module_str):
2341 """Reload an IPython extension by calling reload.
2341 """Reload an IPython extension by calling reload.
2342
2342
2343 If the module has not been loaded before,
2343 If the module has not been loaded before,
2344 :meth:`InteractiveShell.load_extension` is called. Otherwise
2344 :meth:`InteractiveShell.load_extension` is called. Otherwise
2345 :func:`reload` is called and then the :func:`load_ipython_extension`
2345 :func:`reload` is called and then the :func:`load_ipython_extension`
2346 function of the module, if it exists is called.
2346 function of the module, if it exists is called.
2347 """
2347 """
2348 from IPython.utils.syspathcontext import prepended_to_syspath
2348 from IPython.utils.syspathcontext import prepended_to_syspath
2349
2349
2350 with prepended_to_syspath(self.ipython_extension_dir):
2350 with prepended_to_syspath(self.ipython_extension_dir):
2351 if module_str in sys.modules:
2351 if module_str in sys.modules:
2352 mod = sys.modules[module_str]
2352 mod = sys.modules[module_str]
2353 reload(mod)
2353 reload(mod)
2354 self._call_load_ipython_extension(mod)
2354 self._call_load_ipython_extension(mod)
2355 else:
2355 else:
2356 self.load_extension(module_str)
2356 self.load_extension(module_str)
2357
2357
2358 def _call_load_ipython_extension(self, mod):
2358 def _call_load_ipython_extension(self, mod):
2359 if hasattr(mod, 'load_ipython_extension'):
2359 if hasattr(mod, 'load_ipython_extension'):
2360 mod.load_ipython_extension(self)
2360 mod.load_ipython_extension(self)
2361
2361
2362 def _call_unload_ipython_extension(self, mod):
2362 def _call_unload_ipython_extension(self, mod):
2363 if hasattr(mod, 'unload_ipython_extension'):
2363 if hasattr(mod, 'unload_ipython_extension'):
2364 mod.unload_ipython_extension(self)
2364 mod.unload_ipython_extension(self)
2365
2365
2366 #-------------------------------------------------------------------------
2366 #-------------------------------------------------------------------------
2367 # Things related to the prefilter
2367 # Things related to the prefilter
2368 #-------------------------------------------------------------------------
2368 #-------------------------------------------------------------------------
2369
2369
2370 def init_prefilter(self):
2370 def init_prefilter(self):
2371 self.prefilter_manager = PrefilterManager(self, config=self.config)
2371 self.prefilter_manager = PrefilterManager(self, config=self.config)
2372
2372
2373 #-------------------------------------------------------------------------
2373 #-------------------------------------------------------------------------
2374 # Utilities
2374 # Utilities
2375 #-------------------------------------------------------------------------
2375 #-------------------------------------------------------------------------
2376
2376
2377 def getoutput(self, cmd):
2377 def getoutput(self, cmd):
2378 return getoutput(self.var_expand(cmd,depth=2),
2378 return getoutput(self.var_expand(cmd,depth=2),
2379 header=self.system_header,
2379 header=self.system_header,
2380 verbose=self.system_verbose)
2380 verbose=self.system_verbose)
2381
2381
2382 def getoutputerror(self, cmd):
2382 def getoutputerror(self, cmd):
2383 return getoutputerror(self.var_expand(cmd,depth=2),
2383 return getoutputerror(self.var_expand(cmd,depth=2),
2384 header=self.system_header,
2384 header=self.system_header,
2385 verbose=self.system_verbose)
2385 verbose=self.system_verbose)
2386
2386
2387 def var_expand(self,cmd,depth=0):
2387 def var_expand(self,cmd,depth=0):
2388 """Expand python variables in a string.
2388 """Expand python variables in a string.
2389
2389
2390 The depth argument indicates how many frames above the caller should
2390 The depth argument indicates how many frames above the caller should
2391 be walked to look for the local namespace where to expand variables.
2391 be walked to look for the local namespace where to expand variables.
2392
2392
2393 The global namespace for expansion is always the user's interactive
2393 The global namespace for expansion is always the user's interactive
2394 namespace.
2394 namespace.
2395 """
2395 """
2396
2396
2397 return str(ItplNS(cmd,
2397 return str(ItplNS(cmd,
2398 self.user_ns, # globals
2398 self.user_ns, # globals
2399 # Skip our own frame in searching for locals:
2399 # Skip our own frame in searching for locals:
2400 sys._getframe(depth+1).f_locals # locals
2400 sys._getframe(depth+1).f_locals # locals
2401 ))
2401 ))
2402
2402
2403 def mktempfile(self,data=None):
2403 def mktempfile(self,data=None):
2404 """Make a new tempfile and return its filename.
2404 """Make a new tempfile and return its filename.
2405
2405
2406 This makes a call to tempfile.mktemp, but it registers the created
2406 This makes a call to tempfile.mktemp, but it registers the created
2407 filename internally so ipython cleans it up at exit time.
2407 filename internally so ipython cleans it up at exit time.
2408
2408
2409 Optional inputs:
2409 Optional inputs:
2410
2410
2411 - data(None): if data is given, it gets written out to the temp file
2411 - data(None): if data is given, it gets written out to the temp file
2412 immediately, and the file is closed again."""
2412 immediately, and the file is closed again."""
2413
2413
2414 filename = tempfile.mktemp('.py','ipython_edit_')
2414 filename = tempfile.mktemp('.py','ipython_edit_')
2415 self.tempfiles.append(filename)
2415 self.tempfiles.append(filename)
2416
2416
2417 if data:
2417 if data:
2418 tmp_file = open(filename,'w')
2418 tmp_file = open(filename,'w')
2419 tmp_file.write(data)
2419 tmp_file.write(data)
2420 tmp_file.close()
2420 tmp_file.close()
2421 return filename
2421 return filename
2422
2422
2423 def write(self,data):
2423 def write(self,data):
2424 """Write a string to the default output"""
2424 """Write a string to the default output"""
2425 Term.cout.write(data)
2425 Term.cout.write(data)
2426
2426
2427 def write_err(self,data):
2427 def write_err(self,data):
2428 """Write a string to the default error output"""
2428 """Write a string to the default error output"""
2429 Term.cerr.write(data)
2429 Term.cerr.write(data)
2430
2430
2431 def ask_yes_no(self,prompt,default=True):
2431 def ask_yes_no(self,prompt,default=True):
2432 if self.quiet:
2432 if self.quiet:
2433 return True
2433 return True
2434 return ask_yes_no(prompt,default)
2434 return ask_yes_no(prompt,default)
2435
2435
2436 #-------------------------------------------------------------------------
2436 #-------------------------------------------------------------------------
2437 # Things related to IPython exiting
2437 # Things related to IPython exiting
2438 #-------------------------------------------------------------------------
2438 #-------------------------------------------------------------------------
2439
2439
2440 def ask_exit(self):
2440 def ask_exit(self):
2441 """ Call for exiting. Can be overiden and used as a callback. """
2441 """ Call for exiting. Can be overiden and used as a callback. """
2442 self.exit_now = True
2442 self.exit_now = True
2443
2443
2444 def exit(self):
2444 def exit(self):
2445 """Handle interactive exit.
2445 """Handle interactive exit.
2446
2446
2447 This method calls the ask_exit callback."""
2447 This method calls the ask_exit callback."""
2448 if self.confirm_exit:
2448 if self.confirm_exit:
2449 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2449 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2450 self.ask_exit()
2450 self.ask_exit()
2451 else:
2451 else:
2452 self.ask_exit()
2452 self.ask_exit()
2453
2453
2454 def atexit_operations(self):
2454 def atexit_operations(self):
2455 """This will be executed at the time of exit.
2455 """This will be executed at the time of exit.
2456
2456
2457 Saving of persistent data should be performed here.
2457 Saving of persistent data should be performed here.
2458 """
2458 """
2459 self.savehist()
2459 self.savehist()
2460
2460
2461 # Cleanup all tempfiles left around
2461 # Cleanup all tempfiles left around
2462 for tfile in self.tempfiles:
2462 for tfile in self.tempfiles:
2463 try:
2463 try:
2464 os.unlink(tfile)
2464 os.unlink(tfile)
2465 except OSError:
2465 except OSError:
2466 pass
2466 pass
2467
2467
2468 # Clear all user namespaces to release all references cleanly.
2468 # Clear all user namespaces to release all references cleanly.
2469 self.reset()
2469 self.reset()
2470
2470
2471 # Run user hooks
2471 # Run user hooks
2472 self.hooks.shutdown_hook()
2472 self.hooks.shutdown_hook()
2473
2473
2474 def cleanup(self):
2474 def cleanup(self):
2475 self.restore_sys_module_state()
2475 self.restore_sys_module_state()
2476
2476
2477
2477
@@ -1,119 +1,118 b''
1 """
1 """
2 Entry point for a simple application giving a graphical frontend to
2 Entry point for a simple application giving a graphical frontend to
3 ipython.
3 ipython.
4 """
4 """
5
5
6 try:
6 try:
7 import wx
7 import wx
8 except ImportError, e:
8 except ImportError, e:
9 e.message = """%s
9 e.args[0] = """%s
10 ________________________________________________________________________________
10 ________________________________________________________________________________
11 You need wxPython to run this application.
11 You need wxPython to run this application.
12 """ % e.message
12 """ % e.args[0]
13 e.args = (e.message, ) + e.args[1:]
14 raise e
13 raise e
15
14
16 from wx_frontend import WxController
15 from wx_frontend import WxController
17 import __builtin__
16 import __builtin__
18
17
19
18
20 class IPythonXController(WxController):
19 class IPythonXController(WxController):
21 """ Sub class of WxController that adds some application-specific
20 """ Sub class of WxController that adds some application-specific
22 bindings.
21 bindings.
23 """
22 """
24
23
25 debug = False
24 debug = False
26
25
27 def __init__(self, *args, **kwargs):
26 def __init__(self, *args, **kwargs):
28 WxController.__init__(self, *args, **kwargs)
27 WxController.__init__(self, *args, **kwargs)
29 self.ipython0.ask_exit = self.do_exit
28 self.ipython0.ask_exit = self.do_exit
30 # Scroll to top
29 # Scroll to top
31 maxrange = self.GetScrollRange(wx.VERTICAL)
30 maxrange = self.GetScrollRange(wx.VERTICAL)
32 self.ScrollLines(-maxrange)
31 self.ScrollLines(-maxrange)
33
32
34
33
35 def _on_key_down(self, event, skip=True):
34 def _on_key_down(self, event, skip=True):
36 # Intercept Ctrl-D to quit
35 # Intercept Ctrl-D to quit
37 if event.KeyCode == ord('D') and event.ControlDown() and \
36 if event.KeyCode == ord('D') and event.ControlDown() and \
38 self.input_buffer == '' and \
37 self.input_buffer == '' and \
39 self._input_state == 'readline':
38 self._input_state == 'readline':
40 wx.CallAfter(self.ask_exit)
39 wx.CallAfter(self.ask_exit)
41 else:
40 else:
42 WxController._on_key_down(self, event, skip=skip)
41 WxController._on_key_down(self, event, skip=skip)
43
42
44
43
45 def ask_exit(self):
44 def ask_exit(self):
46 """ Ask the user whether to exit.
45 """ Ask the user whether to exit.
47 """
46 """
48 self._input_state = 'subprocess'
47 self._input_state = 'subprocess'
49 self.write('\n', refresh=False)
48 self.write('\n', refresh=False)
50 self.capture_output()
49 self.capture_output()
51 self.ipython0.shell.exit()
50 self.ipython0.shell.exit()
52 self.release_output()
51 self.release_output()
53 if not self.ipython0.exit_now:
52 if not self.ipython0.exit_now:
54 wx.CallAfter(self.new_prompt,
53 wx.CallAfter(self.new_prompt,
55 self.input_prompt_template.substitute(
54 self.input_prompt_template.substitute(
56 number=self.last_result['number'] + 1))
55 number=self.last_result['number'] + 1))
57 else:
56 else:
58 wx.CallAfter(wx.GetApp().Exit)
57 wx.CallAfter(wx.GetApp().Exit)
59 self.write('Exiting ...', refresh=False)
58 self.write('Exiting ...', refresh=False)
60
59
61
60
62 def do_exit(self):
61 def do_exit(self):
63 """ Exits the interpreter, kills the windows.
62 """ Exits the interpreter, kills the windows.
64 """
63 """
65 WxController.do_exit(self)
64 WxController.do_exit(self)
66 self.release_output()
65 self.release_output()
67 wx.CallAfter(wx.Exit)
66 wx.CallAfter(wx.Exit)
68
67
69
68
70
69
71 class IPythonX(wx.Frame):
70 class IPythonX(wx.Frame):
72 """ Main frame of the IPythonX app.
71 """ Main frame of the IPythonX app.
73 """
72 """
74
73
75 def __init__(self, parent, id, title, debug=False):
74 def __init__(self, parent, id, title, debug=False):
76 wx.Frame.__init__(self, parent, id, title, size=(300,250))
75 wx.Frame.__init__(self, parent, id, title, size=(300,250))
77 self._sizer = wx.BoxSizer(wx.VERTICAL)
76 self._sizer = wx.BoxSizer(wx.VERTICAL)
78 self.shell = IPythonXController(self, debug=debug)
77 self.shell = IPythonXController(self, debug=debug)
79 self._sizer.Add(self.shell, 1, wx.EXPAND)
78 self._sizer.Add(self.shell, 1, wx.EXPAND)
80 self.SetSizer(self._sizer)
79 self.SetSizer(self._sizer)
81 self.SetAutoLayout(1)
80 self.SetAutoLayout(1)
82 self.Show(True)
81 self.Show(True)
83 wx.EVT_CLOSE(self, self.on_close)
82 wx.EVT_CLOSE(self, self.on_close)
84
83
85
84
86 def on_close(self, event):
85 def on_close(self, event):
87 """ Called on closing the windows.
86 """ Called on closing the windows.
88
87
89 Stops the event loop, to close all the child windows.
88 Stops the event loop, to close all the child windows.
90 """
89 """
91 wx.CallAfter(wx.Exit)
90 wx.CallAfter(wx.Exit)
92
91
93
92
94 def main():
93 def main():
95 from optparse import OptionParser
94 from optparse import OptionParser
96 usage = """usage: %prog [options]
95 usage = """usage: %prog [options]
97
96
98 Simple graphical frontend to IPython, using WxWidgets."""
97 Simple graphical frontend to IPython, using WxWidgets."""
99 parser = OptionParser(usage=usage)
98 parser = OptionParser(usage=usage)
100 parser.add_option("-d", "--debug",
99 parser.add_option("-d", "--debug",
101 action="store_true", dest="debug", default=False,
100 action="store_true", dest="debug", default=False,
102 help="Enable debug message for the wx frontend.")
101 help="Enable debug message for the wx frontend.")
103
102
104 options, args = parser.parse_args()
103 options, args = parser.parse_args()
105
104
106 # Clear the options, to avoid having the ipython0 instance complain
105 # Clear the options, to avoid having the ipython0 instance complain
107 import sys
106 import sys
108 sys.argv = sys.argv[:1]
107 sys.argv = sys.argv[:1]
109
108
110 app = wx.PySimpleApp()
109 app = wx.PySimpleApp()
111 frame = IPythonX(None, wx.ID_ANY, 'IPythonX', debug=options.debug)
110 frame = IPythonX(None, wx.ID_ANY, 'IPythonX', debug=options.debug)
112 frame.shell.SetFocus()
111 frame.shell.SetFocus()
113 frame.shell.app = app
112 frame.shell.app = app
114 frame.SetSize((680, 460))
113 frame.SetSize((680, 460))
115
114
116 app.MainLoop()
115 app.MainLoop()
117
116
118 if __name__ == '__main__':
117 if __name__ == '__main__':
119 main()
118 main()
@@ -1,205 +1,207 b''
1 # encoding: utf-8
1 # encoding: utf-8
2
2
3 """Classes and functions for kernel related errors and exceptions."""
3 """Classes and functions for kernel related errors and exceptions."""
4
4
5 __docformat__ = "restructuredtext en"
5 __docformat__ = "restructuredtext en"
6
6
7 # Tell nose to skip this module
7 # Tell nose to skip this module
8 __test__ = {}
8 __test__ = {}
9
9
10 #-------------------------------------------------------------------------------
10 #-------------------------------------------------------------------------------
11 # Copyright (C) 2008 The IPython Development Team
11 # Copyright (C) 2008 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 from twisted.python import failure
20 from twisted.python import failure
21
21
22 from IPython.kernel.core import error
22 from IPython.kernel.core import error
23
23
24 #-------------------------------------------------------------------------------
24 #-------------------------------------------------------------------------------
25 # Error classes
25 # Error classes
26 #-------------------------------------------------------------------------------
26 #-------------------------------------------------------------------------------
27
27
28 class KernelError(error.IPythonError):
28 class KernelError(error.IPythonError):
29 pass
29 pass
30
30
31 class NotDefined(KernelError):
31 class NotDefined(KernelError):
32 def __init__(self, name):
32 def __init__(self, name):
33 self.name = name
33 self.name = name
34 self.args = (name,)
34 self.args = (name,)
35
35
36 def __repr__(self):
36 def __repr__(self):
37 return '<NotDefined: %s>' % self.name
37 return '<NotDefined: %s>' % self.name
38
38
39 __str__ = __repr__
39 __str__ = __repr__
40
40
41 class QueueCleared(KernelError):
41 class QueueCleared(KernelError):
42 pass
42 pass
43
43
44 class IdInUse(KernelError):
44 class IdInUse(KernelError):
45 pass
45 pass
46
46
47 class ProtocolError(KernelError):
47 class ProtocolError(KernelError):
48 pass
48 pass
49
49
50 class ConnectionError(KernelError):
50 class ConnectionError(KernelError):
51 pass
51 pass
52
52
53 class InvalidEngineID(KernelError):
53 class InvalidEngineID(KernelError):
54 pass
54 pass
55
55
56 class NoEnginesRegistered(KernelError):
56 class NoEnginesRegistered(KernelError):
57 pass
57 pass
58
58
59 class InvalidClientID(KernelError):
59 class InvalidClientID(KernelError):
60 pass
60 pass
61
61
62 class InvalidDeferredID(KernelError):
62 class InvalidDeferredID(KernelError):
63 pass
63 pass
64
64
65 class SerializationError(KernelError):
65 class SerializationError(KernelError):
66 pass
66 pass
67
67
68 class MessageSizeError(KernelError):
68 class MessageSizeError(KernelError):
69 pass
69 pass
70
70
71 class PBMessageSizeError(MessageSizeError):
71 class PBMessageSizeError(MessageSizeError):
72 pass
72 pass
73
73
74 class ResultNotCompleted(KernelError):
74 class ResultNotCompleted(KernelError):
75 pass
75 pass
76
76
77 class ResultAlreadyRetrieved(KernelError):
77 class ResultAlreadyRetrieved(KernelError):
78 pass
78 pass
79
79
80 class ClientError(KernelError):
80 class ClientError(KernelError):
81 pass
81 pass
82
82
83 class TaskAborted(KernelError):
83 class TaskAborted(KernelError):
84 pass
84 pass
85
85
86 class TaskTimeout(KernelError):
86 class TaskTimeout(KernelError):
87 pass
87 pass
88
88
89 class NotAPendingResult(KernelError):
89 class NotAPendingResult(KernelError):
90 pass
90 pass
91
91
92 class UnpickleableException(KernelError):
92 class UnpickleableException(KernelError):
93 pass
93 pass
94
94
95 class AbortedPendingDeferredError(KernelError):
95 class AbortedPendingDeferredError(KernelError):
96 pass
96 pass
97
97
98 class InvalidProperty(KernelError):
98 class InvalidProperty(KernelError):
99 pass
99 pass
100
100
101 class MissingBlockArgument(KernelError):
101 class MissingBlockArgument(KernelError):
102 pass
102 pass
103
103
104 class StopLocalExecution(KernelError):
104 class StopLocalExecution(KernelError):
105 pass
105 pass
106
106
107 class SecurityError(KernelError):
107 class SecurityError(KernelError):
108 pass
108 pass
109
109
110 class FileTimeoutError(KernelError):
110 class FileTimeoutError(KernelError):
111 pass
111 pass
112
112
113 class TaskRejectError(KernelError):
113 class TaskRejectError(KernelError):
114 """Exception to raise when a task should be rejected by an engine.
114 """Exception to raise when a task should be rejected by an engine.
115
115
116 This exception can be used to allow a task running on an engine to test
116 This exception can be used to allow a task running on an engine to test
117 if the engine (or the user's namespace on the engine) has the needed
117 if the engine (or the user's namespace on the engine) has the needed
118 task dependencies. If not, the task should raise this exception. For
118 task dependencies. If not, the task should raise this exception. For
119 the task to be retried on another engine, the task should be created
119 the task to be retried on another engine, the task should be created
120 with the `retries` argument > 1.
120 with the `retries` argument > 1.
121
121
122 The advantage of this approach over our older properties system is that
122 The advantage of this approach over our older properties system is that
123 tasks have full access to the user's namespace on the engines and the
123 tasks have full access to the user's namespace on the engines and the
124 properties don't have to be managed or tested by the controller.
124 properties don't have to be managed or tested by the controller.
125 """
125 """
126
126
127 class CompositeError(KernelError):
127 class CompositeError(KernelError):
128 def __init__(self, message, elist):
128 def __init__(self, message, elist):
129 Exception.__init__(self, *(message, elist))
129 Exception.__init__(self, *(message, elist))
130 self.message = message
130 # Don't use pack_exception because it will conflict with the .message
131 # attribute that is being deprecated in 2.6 and beyond.
132 self.msg = message
131 self.elist = elist
133 self.elist = elist
132
134
133 def _get_engine_str(self, ev):
135 def _get_engine_str(self, ev):
134 try:
136 try:
135 ei = ev._ipython_engine_info
137 ei = ev._ipython_engine_info
136 except AttributeError:
138 except AttributeError:
137 return '[Engine Exception]'
139 return '[Engine Exception]'
138 else:
140 else:
139 return '[%i:%s]: ' % (ei['engineid'], ei['method'])
141 return '[%i:%s]: ' % (ei['engineid'], ei['method'])
140
142
141 def _get_traceback(self, ev):
143 def _get_traceback(self, ev):
142 try:
144 try:
143 tb = ev._ipython_traceback_text
145 tb = ev._ipython_traceback_text
144 except AttributeError:
146 except AttributeError:
145 return 'No traceback available'
147 return 'No traceback available'
146 else:
148 else:
147 return tb
149 return tb
148
150
149 def __str__(self):
151 def __str__(self):
150 s = str(self.message)
152 s = str(self.msg)
151 for et, ev, etb in self.elist:
153 for et, ev, etb in self.elist:
152 engine_str = self._get_engine_str(ev)
154 engine_str = self._get_engine_str(ev)
153 s = s + '\n' + engine_str + str(et.__name__) + ': ' + str(ev)
155 s = s + '\n' + engine_str + str(et.__name__) + ': ' + str(ev)
154 return s
156 return s
155
157
156 def print_tracebacks(self, excid=None):
158 def print_tracebacks(self, excid=None):
157 if excid is None:
159 if excid is None:
158 for (et,ev,etb) in self.elist:
160 for (et,ev,etb) in self.elist:
159 print self._get_engine_str(ev)
161 print self._get_engine_str(ev)
160 print self._get_traceback(ev)
162 print self._get_traceback(ev)
161 print
163 print
162 else:
164 else:
163 try:
165 try:
164 et,ev,etb = self.elist[excid]
166 et,ev,etb = self.elist[excid]
165 except:
167 except:
166 raise IndexError("an exception with index %i does not exist"%excid)
168 raise IndexError("an exception with index %i does not exist"%excid)
167 else:
169 else:
168 print self._get_engine_str(ev)
170 print self._get_engine_str(ev)
169 print self._get_traceback(ev)
171 print self._get_traceback(ev)
170
172
171 def raise_exception(self, excid=0):
173 def raise_exception(self, excid=0):
172 try:
174 try:
173 et,ev,etb = self.elist[excid]
175 et,ev,etb = self.elist[excid]
174 except:
176 except:
175 raise IndexError("an exception with index %i does not exist"%excid)
177 raise IndexError("an exception with index %i does not exist"%excid)
176 else:
178 else:
177 raise et, ev, etb
179 raise et, ev, etb
178
180
179 def collect_exceptions(rlist, method):
181 def collect_exceptions(rlist, method):
180 elist = []
182 elist = []
181 for r in rlist:
183 for r in rlist:
182 if isinstance(r, failure.Failure):
184 if isinstance(r, failure.Failure):
183 r.cleanFailure()
185 r.cleanFailure()
184 et, ev, etb = r.type, r.value, r.tb
186 et, ev, etb = r.type, r.value, r.tb
185 # Sometimes we could have CompositeError in our list. Just take
187 # Sometimes we could have CompositeError in our list. Just take
186 # the errors out of them and put them in our new list. This
188 # the errors out of them and put them in our new list. This
187 # has the effect of flattening lists of CompositeErrors into one
189 # has the effect of flattening lists of CompositeErrors into one
188 # CompositeError
190 # CompositeError
189 if et==CompositeError:
191 if et==CompositeError:
190 for e in ev.elist:
192 for e in ev.elist:
191 elist.append(e)
193 elist.append(e)
192 else:
194 else:
193 elist.append((et, ev, etb))
195 elist.append((et, ev, etb))
194 if len(elist)==0:
196 if len(elist)==0:
195 return rlist
197 return rlist
196 else:
198 else:
197 msg = "one or more exceptions from call to method: %s" % (method)
199 msg = "one or more exceptions from call to method: %s" % (method)
198 # This silliness is needed so the debugger has access to the exception
200 # This silliness is needed so the debugger has access to the exception
199 # instance (e in this case)
201 # instance (e in this case)
200 try:
202 try:
201 raise CompositeError(msg, elist)
203 raise CompositeError(msg, elist)
202 except CompositeError, e:
204 except CompositeError, e:
203 raise e
205 raise e
204
206
205
207
General Comments 0
You need to be logged in to leave comments. Login now