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