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