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