##// END OF EJS Templates
pdb support in threaded mode, replaced the crash handler with a verbose...
fperez -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,2592 +1,2620 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Magic functions for InteractiveShell.
3 3
4 $Id: Magic.py 962 2005-12-28 18:04:59Z fperez $"""
4 $Id: Magic.py 965 2005-12-28 23:23:09Z fperez $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
8 8 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
9 9 #
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #*****************************************************************************
13 13
14 14 #****************************************************************************
15 15 # Modules and globals
16 16
17 17 from IPython import Release
18 18 __author__ = '%s <%s>\n%s <%s>' % \
19 19 ( Release.authors['Janko'] + Release.authors['Fernando'] )
20 20 __license__ = Release.license
21 21
22 22 # Python standard modules
23 23 import __builtin__
24 24 import bdb
25 25 import inspect
26 26 import os
27 27 import pdb
28 28 import pydoc
29 29 import sys
30 30 import re
31 31 import tempfile
32 32 import time
33 33 from cStringIO import StringIO
34 34 from getopt import getopt
35 35 from pprint import pprint, pformat
36 36
37 37 # profile isn't bundled by default in Debian for license reasons
38 38 try:
39 39 import profile,pstats
40 40 except ImportError:
41 41 profile = pstats = None
42 42
43 43 # Homebrewed
44 44 from IPython import Debugger, OInspect, wildcard
45 45 from IPython.FakeModule import FakeModule
46 46 from IPython.Itpl import Itpl, itpl, printpl,itplns
47 47 from IPython.PyColorize import Parser
48 48 from IPython.Struct import Struct
49 49 from IPython.genutils import *
50 50
51 51 # Globals to be set later by Magic constructor
52 52 MAGIC_PREFIX = ''
53 53 MAGIC_ESCAPE = ''
54 54
55 55 #***************************************************************************
56 56 # Utility functions
57 57 def magic2python(cmd):
58 58 """Convert a command string of magic syntax to valid Python code."""
59 59
60 60 if cmd.startswith('#'+MAGIC_ESCAPE) or \
61 61 cmd.startswith(MAGIC_ESCAPE):
62 62 if cmd[0]=='#':
63 63 cmd = cmd[1:]
64 64 # we need to return the proper line end later
65 65 if cmd[-1] == '\n':
66 66 endl = '\n'
67 67 else:
68 68 endl = ''
69 69 try:
70 70 func,args = cmd[1:].split(' ',1)
71 71 except:
72 72 func,args = cmd[1:].rstrip(),''
73 73 args = args.replace('"','\\"').replace("'","\\'").rstrip()
74 74 return '%s%s ("%s")%s' % (MAGIC_PREFIX,func,args,endl)
75 75 else:
76 76 return cmd
77 77
78 78 def on_off(tag):
79 79 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
80 80 return ['OFF','ON'][tag]
81 81
82 82
83 83 #****************************************************************************
84 84 # Utility classes
85 85 class Macro:
86 86 """Simple class to store the value of macros as strings.
87 87
88 88 This allows us to later exec them by checking when something is an
89 89 instance of this class."""
90 90
91 91 def __init__(self,cmds):
92 92 """Build a macro from a list of commands."""
93 93
94 94 # Since the list may include multi-line entries, first make sure that
95 95 # they've been all broken up before passing it to magic2python
96 96 cmdlist = map(magic2python,''.join(cmds).split('\n'))
97 97 self.value = '\n'.join(cmdlist)
98 98
99 99 def __str__(self):
100 100 return self.value
101 101
102 102 #***************************************************************************
103 103 # Main class implementing Magic functionality
104 104 class Magic:
105 105 """Magic functions for InteractiveShell.
106 106
107 107 Shell functions which can be reached as %function_name. All magic
108 108 functions should accept a string, which they can parse for their own
109 109 needs. This can make some functions easier to type, eg `%cd ../`
110 110 vs. `%cd("../")`
111 111
112 112 ALL definitions MUST begin with the prefix magic_. The user won't need it
113 113 at the command line, but it is is needed in the definition. """
114 114
115 115 # class globals
116 116 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
117 117 'Automagic is ON, % prefix NOT needed for magic functions.']
118 118
119 119 #......................................................................
120 120 # some utility functions
121 121
122 122 def __init__(self,shell):
123 123 # XXX This is hackish, clean up later to avoid these messy globals
124 124 global MAGIC_PREFIX, MAGIC_ESCAPE
125 125
126 126 self.options_table = {}
127 127 MAGIC_PREFIX = shell.name+'.magic_'
128 128 MAGIC_ESCAPE = shell.ESC_MAGIC
129 129 if profile is None:
130 130 self.magic_prun = self.profile_missing_notice
131 131
132 132 def profile_missing_notice(self, *args, **kwargs):
133 133 error("""\
134 134 The profile module could not be found. If you are a Debian user,
135 135 it has been removed from the standard Debian package because of its non-free
136 136 license. To use profiling, please install"python2.3-profiler" from non-free.""")
137 137
138 138 def default_option(self,fn,optstr):
139 139 """Make an entry in the options_table for fn, with value optstr"""
140 140
141 141 if fn not in self.lsmagic():
142 142 error("%s is not a magic function" % fn)
143 143 self.options_table[fn] = optstr
144 144
145 145 def lsmagic(self):
146 146 """Return a list of currently available magic functions.
147 147
148 148 Gives a list of the bare names after mangling (['ls','cd', ...], not
149 149 ['magic_ls','magic_cd',...]"""
150 150
151 151 # FIXME. This needs a cleanup, in the way the magics list is built.
152 152
153 153 # magics in class definition
154 154 class_magic = lambda fn: fn.startswith('magic_') and \
155 155 callable(Magic.__dict__[fn])
156 156 # in instance namespace (run-time user additions)
157 157 inst_magic = lambda fn: fn.startswith('magic_') and \
158 158 callable(self.__dict__[fn])
159 159 # and bound magics by user (so they can access self):
160 160 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
161 161 callable(self.__class__.__dict__[fn])
162 162 magics = filter(class_magic,Magic.__dict__.keys()) + \
163 163 filter(inst_magic,self.__dict__.keys()) + \
164 164 filter(inst_bound_magic,self.__class__.__dict__.keys())
165 165 out = []
166 166 for fn in magics:
167 167 out.append(fn.replace('magic_','',1))
168 168 out.sort()
169 169 return out
170 170
171 171 def set_shell(self,shell):
172 172 self.shell = shell
173 173 self.alias_table = shell.alias_table
174 174
175 175 def extract_input_slices(self,slices):
176 176 """Return as a string a set of input history slices.
177 177
178 178 The set of slices is given as a list of strings (like ['1','4:8','9'],
179 179 since this function is for use by magic functions which get their
180 180 arguments as strings."""
181 181
182 182 cmds = []
183 183 for chunk in slices:
184 184 if ':' in chunk:
185 185 ini,fin = map(int,chunk.split(':'))
186 186 else:
187 187 ini = int(chunk)
188 188 fin = ini+1
189 189 cmds.append(self.shell.input_hist[ini:fin])
190 190 return cmds
191 191
192 192 def _ofind(self,oname):
193 193 """Find an object in the available namespaces.
194 194
195 195 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
196 196
197 197 Has special code to detect magic functions.
198 198 """
199 199
200 200 oname = oname.strip()
201 201
202 202 # Namespaces to search in:
203 203 user_ns = self.shell.user_ns
204 204 internal_ns = self.shell.internal_ns
205 205 builtin_ns = __builtin__.__dict__
206 206 alias_ns = self.shell.alias_table
207 207
208 208 # Put them in a list. The order is important so that we find things in
209 209 # the same order that Python finds them.
210 210 namespaces = [ ('Interactive',user_ns),
211 211 ('IPython internal',internal_ns),
212 212 ('Python builtin',builtin_ns),
213 213 ('Alias',alias_ns),
214 214 ]
215 215
216 216 # initialize results to 'null'
217 217 found = 0; obj = None; ospace = None; ds = None;
218 218 ismagic = 0; isalias = 0
219 219
220 220 # Look for the given name by splitting it in parts. If the head is
221 221 # found, then we look for all the remaining parts as members, and only
222 222 # declare success if we can find them all.
223 223 oname_parts = oname.split('.')
224 224 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
225 225 for nsname,ns in namespaces:
226 226 try:
227 227 obj = ns[oname_head]
228 228 except KeyError:
229 229 continue
230 230 else:
231 231 for part in oname_rest:
232 232 try:
233 233 obj = getattr(obj,part)
234 234 except:
235 235 # Blanket except b/c some badly implemented objects
236 236 # allow __getattr__ to raise exceptions other than
237 237 # AttributeError, which then crashes IPython.
238 238 break
239 239 else:
240 240 # If we finish the for loop (no break), we got all members
241 241 found = 1
242 242 ospace = nsname
243 243 if ns == alias_ns:
244 244 isalias = 1
245 245 break # namespace loop
246 246
247 247 # Try to see if it's magic
248 248 if not found:
249 249 if oname.startswith(self.shell.ESC_MAGIC):
250 250 oname = oname[1:]
251 251 obj = getattr(self,'magic_'+oname,None)
252 252 if obj is not None:
253 253 found = 1
254 254 ospace = 'IPython internal'
255 255 ismagic = 1
256 256
257 257 # Last try: special-case some literals like '', [], {}, etc:
258 258 if not found and oname_head in ["''",'""','[]','{}','()']:
259 259 obj = eval(oname_head)
260 260 found = 1
261 261 ospace = 'Interactive'
262 262
263 263 return {'found':found, 'obj':obj, 'namespace':ospace,
264 264 'ismagic':ismagic, 'isalias':isalias}
265 265
266 266 def arg_err(self,func):
267 267 """Print docstring if incorrect arguments were passed"""
268 268 print 'Error in arguments:'
269 269 print OInspect.getdoc(func)
270 270
271 271
272 272 def format_latex(self,str):
273 273 """Format a string for latex inclusion."""
274 274
275 275 # Characters that need to be escaped for latex:
276 276 escape_re = re.compile(r'(%|_|\$)',re.MULTILINE)
277 277 # Magic command names as headers:
278 278 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
279 279 re.MULTILINE)
280 280 # Magic commands
281 281 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
282 282 re.MULTILINE)
283 283 # Paragraph continue
284 284 par_re = re.compile(r'\\$',re.MULTILINE)
285 285
286 286 str = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',str)
287 287 str = cmd_re.sub(r'\\texttt{\g<cmd>}',str)
288 288 str = par_re.sub(r'\\\\',str)
289 289 str = escape_re.sub(r'\\\1',str)
290 290 return str
291 291
292 292 def format_screen(self,str):
293 293 """Format a string for screen printing.
294 294
295 295 This removes some latex-type format codes."""
296 296 # Paragraph continue
297 297 par_re = re.compile(r'\\$',re.MULTILINE)
298 298 str = par_re.sub('',str)
299 299 return str
300 300
301 301 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
302 302 """Parse options passed to an argument string.
303 303
304 304 The interface is similar to that of getopt(), but it returns back a
305 305 Struct with the options as keys and the stripped argument string still
306 306 as a string.
307 307
308 308 arg_str is quoted as a true sys.argv vector by using shlex.split.
309 309 This allows us to easily expand variables, glob files, quote
310 310 arguments, etc.
311 311
312 312 Options:
313 313 -mode: default 'string'. If given as 'list', the argument string is
314 314 returned as a list (split on whitespace) instead of a string.
315 315
316 316 -list_all: put all option values in lists. Normally only options
317 317 appearing more than once are put in a list."""
318 318
319 319 # inject default options at the beginning of the input line
320 320 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
321 321 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
322 322
323 323 mode = kw.get('mode','string')
324 324 if mode not in ['string','list']:
325 325 raise ValueError,'incorrect mode given: %s' % mode
326 326 # Get options
327 327 list_all = kw.get('list_all',0)
328 328
329 329 # Check if we have more than one argument to warrant extra processing:
330 330 odict = {} # Dictionary with options
331 331 args = arg_str.split()
332 332 if len(args) >= 1:
333 333 # If the list of inputs only has 0 or 1 thing in it, there's no
334 334 # need to look for options
335 335 argv = shlex_split(arg_str)
336 336 # Do regular option processing
337 337 opts,args = getopt(argv,opt_str,*long_opts)
338 338 for o,a in opts:
339 339 if o.startswith('--'):
340 340 o = o[2:]
341 341 else:
342 342 o = o[1:]
343 343 try:
344 344 odict[o].append(a)
345 345 except AttributeError:
346 346 odict[o] = [odict[o],a]
347 347 except KeyError:
348 348 if list_all:
349 349 odict[o] = [a]
350 350 else:
351 351 odict[o] = a
352 352
353 353 # Prepare opts,args for return
354 354 opts = Struct(odict)
355 355 if mode == 'string':
356 356 args = ' '.join(args)
357 357
358 358 return opts,args
359 359
360 360 #......................................................................
361 361 # And now the actual magic functions
362 362
363 363 # Functions for IPython shell work (vars,funcs, config, etc)
364 364 def magic_lsmagic(self, parameter_s = ''):
365 365 """List currently available magic functions."""
366 366 mesc = self.shell.ESC_MAGIC
367 367 print 'Available magic functions:\n'+mesc+\
368 368 (' '+mesc).join(self.lsmagic())
369 369 print '\n' + Magic.auto_status[self.shell.rc.automagic]
370 370 return None
371 371
372 372 def magic_magic(self, parameter_s = ''):
373 373 """Print information about the magic function system."""
374 374
375 375 mode = ''
376 376 try:
377 377 if parameter_s.split()[0] == '-latex':
378 378 mode = 'latex'
379 379 except:
380 380 pass
381 381
382 382 magic_docs = []
383 383 for fname in self.lsmagic():
384 384 mname = 'magic_' + fname
385 385 for space in (Magic,self,self.__class__):
386 386 try:
387 387 fn = space.__dict__[mname]
388 388 except KeyError:
389 389 pass
390 390 else:
391 391 break
392 392 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
393 393 fname,fn.__doc__))
394 394 magic_docs = ''.join(magic_docs)
395 395
396 396 if mode == 'latex':
397 397 print self.format_latex(magic_docs)
398 398 return
399 399 else:
400 400 magic_docs = self.format_screen(magic_docs)
401 401
402 402 outmsg = """
403 403 IPython's 'magic' functions
404 404 ===========================
405 405
406 406 The magic function system provides a series of functions which allow you to
407 407 control the behavior of IPython itself, plus a lot of system-type
408 408 features. All these functions are prefixed with a % character, but parameters
409 409 are given without parentheses or quotes.
410 410
411 411 NOTE: If you have 'automagic' enabled (via the command line option or with the
412 412 %automagic function), you don't need to type in the % explicitly. By default,
413 413 IPython ships with automagic on, so you should only rarely need the % escape.
414 414
415 415 Example: typing '%cd mydir' (without the quotes) changes you working directory
416 416 to 'mydir', if it exists.
417 417
418 418 You can define your own magic functions to extend the system. See the supplied
419 419 ipythonrc and example-magic.py files for details (in your ipython
420 420 configuration directory, typically $HOME/.ipython/).
421 421
422 422 You can also define your own aliased names for magic functions. In your
423 423 ipythonrc file, placing a line like:
424 424
425 425 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
426 426
427 427 will define %pf as a new name for %profile.
428 428
429 429 You can also call magics in code using the ipmagic() function, which IPython
430 430 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
431 431
432 432 For a list of the available magic functions, use %lsmagic. For a description
433 433 of any of them, type %magic_name?, e.g. '%cd?'.
434 434
435 435 Currently the magic system has the following functions:\n"""
436 436
437 437 mesc = self.shell.ESC_MAGIC
438 438 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
439 439 "\n\n%s%s\n\n%s" % (outmsg,
440 440 magic_docs,mesc,mesc,
441 441 (' '+mesc).join(self.lsmagic()),
442 442 Magic.auto_status[self.shell.rc.automagic] ) )
443 443
444 444 page(outmsg,screen_lines=self.shell.rc.screen_length)
445 445
446 446 def magic_automagic(self, parameter_s = ''):
447 447 """Make magic functions callable without having to type the initial %.
448 448
449 449 Toggles on/off (when off, you must call it as %automagic, of
450 450 course). Note that magic functions have lowest priority, so if there's
451 451 a variable whose name collides with that of a magic fn, automagic
452 452 won't work for that function (you get the variable instead). However,
453 453 if you delete the variable (del var), the previously shadowed magic
454 454 function becomes visible to automagic again."""
455 455
456 456 rc = self.shell.rc
457 457 rc.automagic = not rc.automagic
458 458 print '\n' + Magic.auto_status[rc.automagic]
459 459
460 460 def magic_autocall(self, parameter_s = ''):
461 461 """Make functions callable without having to type parentheses.
462 462
463 463 This toggles the autocall command line option on and off."""
464 464
465 465 rc = self.shell.rc
466 466 rc.autocall = not rc.autocall
467 467 print "Automatic calling is:",['OFF','ON'][rc.autocall]
468 468
469 469 def magic_autoindent(self, parameter_s = ''):
470 470 """Toggle autoindent on/off (if available)."""
471 471
472 472 self.shell.set_autoindent()
473 473 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
474 474
475 475 def magic_system_verbose(self, parameter_s = ''):
476 476 """Toggle verbose printing of system calls on/off."""
477 477
478 478 self.shell.rc_set_toggle('system_verbose')
479 479 print "System verbose printing is:",\
480 480 ['OFF','ON'][self.shell.rc.system_verbose]
481 481
482 482 def magic_history(self, parameter_s = ''):
483 483 """Print input history (_i<n> variables), with most recent last.
484 484
485 485 %history [-n] -> print at most 40 inputs (some may be multi-line)\\
486 486 %history [-n] n -> print at most n inputs\\
487 487 %history [-n] n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
488 488
489 489 Each input's number <n> is shown, and is accessible as the
490 490 automatically generated variable _i<n>. Multi-line statements are
491 491 printed starting at a new line for easy copy/paste.
492 492
493 493 If option -n is used, input numbers are not printed. This is useful if
494 494 you want to get a printout of many lines which can be directly pasted
495 495 into a text editor.
496 496
497 497 This feature is only available if numbered prompts are in use."""
498 498
499 499 if not self.do_full_cache:
500 500 print 'This feature is only available if numbered prompts are in use.'
501 501 return
502 502 opts,args = self.parse_options(parameter_s,'n',mode='list')
503 503
504 504 default_length = 40
505 505 if len(args) == 0:
506 506 final = self.outputcache.prompt_count
507 507 init = max(1,final-default_length)
508 508 elif len(args) == 1:
509 509 final = self.outputcache.prompt_count
510 510 init = max(1,final-int(args[0]))
511 511 elif len(args) == 2:
512 512 init,final = map(int,args)
513 513 else:
514 514 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
515 515 print self.magic_hist.__doc__
516 516 return
517 517 width = len(str(final))
518 518 line_sep = ['','\n']
519 519 input_hist = self.shell.input_hist
520 520 print_nums = not opts.has_key('n')
521 521 for in_num in range(init,final):
522 522 inline = input_hist[in_num]
523 523 multiline = inline.count('\n') > 1
524 524 if print_nums:
525 525 print str(in_num).ljust(width)+':'+ line_sep[multiline],
526 526 if inline.startswith('#'+self.shell.ESC_MAGIC) or \
527 527 inline.startswith('#!'):
528 528 print inline[1:],
529 529 else:
530 530 print inline,
531 531
532 532 def magic_hist(self, parameter_s=''):
533 533 """Alternate name for %history."""
534 534 return self.magic_history(parameter_s)
535 535
536 536 def magic_p(self, parameter_s=''):
537 537 """Just a short alias for Python's 'print'."""
538 538 exec 'print ' + parameter_s in self.shell.user_ns
539 539
540 540 def magic_r(self, parameter_s=''):
541 541 """Repeat previous input.
542 542
543 543 If given an argument, repeats the previous command which starts with
544 544 the same string, otherwise it just repeats the previous input.
545 545
546 546 Shell escaped commands (with ! as first character) are not recognized
547 547 by this system, only pure python code and magic commands.
548 548 """
549 549
550 550 start = parameter_s.strip()
551 551 esc_magic = self.shell.ESC_MAGIC
552 552 # Identify magic commands even if automagic is on (which means
553 553 # the in-memory version is different from that typed by the user).
554 554 if self.shell.rc.automagic:
555 555 start_magic = esc_magic+start
556 556 else:
557 557 start_magic = start
558 558 # Look through the input history in reverse
559 559 for n in range(len(self.shell.input_hist)-2,0,-1):
560 560 input = self.shell.input_hist[n]
561 561 # skip plain 'r' lines so we don't recurse to infinity
562 562 if input != 'ipmagic("r")\n' and \
563 563 (input.startswith(start) or input.startswith(start_magic)):
564 564 #print 'match',`input` # dbg
565 565 if input.startswith(esc_magic):
566 566 input = magic2python(input)
567 567 #print 'modified',`input` # dbg
568 568 print 'Executing:',input,
569 569 exec input in self.shell.user_ns
570 570 return
571 571 print 'No previous input matching `%s` found.' % start
572 572
573 573 def magic_page(self, parameter_s=''):
574 574 """Pretty print the object and display it through a pager.
575 575
576 576 If no parameter is given, use _ (last output)."""
577 577 # After a function contributed by Olivier Aubert, slightly modified.
578 578
579 579 oname = parameter_s and parameter_s or '_'
580 580 info = self._ofind(oname)
581 581 if info['found']:
582 582 page(pformat(info['obj']))
583 583 else:
584 584 print 'Object `%s` not found' % oname
585 585
586 586 def magic_profile(self, parameter_s=''):
587 587 """Print your currently active IPyhton profile."""
588 588 if self.shell.rc.profile:
589 589 printpl('Current IPython profile: $self.shell.rc.profile.')
590 590 else:
591 591 print 'No profile active.'
592 592
593 593 def _inspect(self,meth,oname,**kw):
594 594 """Generic interface to the inspector system.
595 595
596 596 This function is meant to be called by pdef, pdoc & friends."""
597 597
598 598 oname = oname.strip()
599 599 info = Struct(self._ofind(oname))
600 600 if info.found:
601 601 pmethod = getattr(self.shell.inspector,meth)
602 602 formatter = info.ismagic and self.format_screen or None
603 603 if meth == 'pdoc':
604 604 pmethod(info.obj,oname,formatter)
605 605 elif meth == 'pinfo':
606 606 pmethod(info.obj,oname,formatter,info,**kw)
607 607 else:
608 608 pmethod(info.obj,oname)
609 609 else:
610 610 print 'Object `%s` not found.' % oname
611 611 return 'not found' # so callers can take other action
612 612
613 613 def magic_pdef(self, parameter_s=''):
614 614 """Print the definition header for any callable object.
615 615
616 616 If the object is a class, print the constructor information."""
617 617 self._inspect('pdef',parameter_s)
618 618
619 619 def magic_pdoc(self, parameter_s=''):
620 620 """Print the docstring for an object.
621 621
622 622 If the given object is a class, it will print both the class and the
623 623 constructor docstrings."""
624 624 self._inspect('pdoc',parameter_s)
625 625
626 626 def magic_psource(self, parameter_s=''):
627 627 """Print (or run through pager) the source code for an object."""
628 628 self._inspect('psource',parameter_s)
629 629
630 630 def magic_pfile(self, parameter_s=''):
631 631 """Print (or run through pager) the file where an object is defined.
632 632
633 633 The file opens at the line where the object definition begins. IPython
634 634 will honor the environment variable PAGER if set, and otherwise will
635 635 do its best to print the file in a convenient form.
636 636
637 637 If the given argument is not an object currently defined, IPython will
638 638 try to interpret it as a filename (automatically adding a .py extension
639 639 if needed). You can thus use %pfile as a syntax highlighting code
640 640 viewer."""
641 641
642 642 # first interpret argument as an object name
643 643 out = self._inspect('pfile',parameter_s)
644 644 # if not, try the input as a filename
645 645 if out == 'not found':
646 646 try:
647 647 filename = get_py_filename(parameter_s)
648 648 except IOError,msg:
649 649 print msg
650 650 return
651 651 page(self.shell.inspector.format(file(filename).read()))
652 652
653 653 def magic_pinfo(self, parameter_s=''):
654 654 """Provide detailed information about an object.
655 655
656 656 '%pinfo object' is just a synonym for object? or ?object."""
657 657
658 658 #print 'pinfo par: <%s>' % parameter_s # dbg
659 659
660 660 # detail_level: 0 -> obj? , 1 -> obj??
661 661 detail_level = 0
662 662 # We need to detect if we got called as 'pinfo pinfo foo', which can
663 663 # happen if the user types 'pinfo foo?' at the cmd line.
664 664 pinfo,qmark1,oname,qmark2 = \
665 665 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
666 666 if pinfo or qmark1 or qmark2:
667 667 detail_level = 1
668 668 if "*" in oname:
669 669 self.magic_psearch(oname)
670 670 else:
671 671 self._inspect('pinfo',oname,detail_level=detail_level)
672 672
673 673 def magic_psearch(self, parameter_s=''):
674 674 """Search for object in namespaces by wildcard.
675 675
676 676 %psearch [options] PATTERN [OBJECT TYPE]
677 677
678 678 Note: ? can be used as a synonym for %psearch, at the beginning or at
679 679 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
680 680 rest of the command line must be unchanged (options come first), so
681 681 for example the following forms are equivalent
682 682
683 683 %psearch -i a* function
684 684 -i a* function?
685 685 ?-i a* function
686 686
687 687 Arguments:
688 688
689 689 PATTERN
690 690
691 691 where PATTERN is a string containing * as a wildcard similar to its
692 692 use in a shell. The pattern is matched in all namespaces on the
693 693 search path. By default objects starting with a single _ are not
694 694 matched, many IPython generated objects have a single
695 695 underscore. The default is case insensitive matching. Matching is
696 696 also done on the attributes of objects and not only on the objects
697 697 in a module.
698 698
699 699 [OBJECT TYPE]
700 700
701 701 Is the name of a python type from the types module. The name is
702 702 given in lowercase without the ending type, ex. StringType is
703 703 written string. By adding a type here only objects matching the
704 704 given type are matched. Using all here makes the pattern match all
705 705 types (this is the default).
706 706
707 707 Options:
708 708
709 709 -a: makes the pattern match even objects whose names start with a
710 710 single underscore. These names are normally ommitted from the
711 711 search.
712 712
713 713 -i/-c: make the pattern case insensitive/sensitive. If neither of
714 714 these options is given, the default is read from your ipythonrc
715 715 file. The option name which sets this value is
716 716 'wildcards_case_sensitive'. If this option is not specified in your
717 717 ipythonrc file, IPython's internal default is to do a case sensitive
718 718 search.
719 719
720 720 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
721 721 specifiy can be searched in any of the following namespaces:
722 722 'builtin', 'user', 'user_global','internal', 'alias', where
723 723 'builtin' and 'user' are the search defaults. Note that you should
724 724 not use quotes when specifying namespaces.
725 725
726 726 'Builtin' contains the python module builtin, 'user' contains all
727 727 user data, 'alias' only contain the shell aliases and no python
728 728 objects, 'internal' contains objects used by IPython. The
729 729 'user_global' namespace is only used by embedded IPython instances,
730 730 and it contains module-level globals. You can add namespaces to the
731 731 search with -s or exclude them with -e (these options can be given
732 732 more than once).
733 733
734 734 Examples:
735 735
736 736 %psearch a* -> objects beginning with an a
737 737 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
738 738 %psearch a* function -> all functions beginning with an a
739 739 %psearch re.e* -> objects beginning with an e in module re
740 740 %psearch r*.e* -> objects that start with e in modules starting in r
741 741 %psearch r*.* string -> all strings in modules beginning with r
742 742
743 743 Case sensitve search:
744 744
745 745 %psearch -c a* list all object beginning with lower case a
746 746
747 747 Show objects beginning with a single _:
748 748
749 749 %psearch -a _* list objects beginning with a single underscore"""
750 750
751 751 # default namespaces to be searched
752 752 def_search = ['user','builtin']
753 753
754 754 # Process options/args
755 755 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
756 756 opt = opts.get
757 757 shell = self.shell
758 758 psearch = shell.inspector.psearch
759 759
760 760 # select case options
761 761 if opts.has_key('i'):
762 762 ignore_case = True
763 763 elif opts.has_key('c'):
764 764 ignore_case = False
765 765 else:
766 766 ignore_case = not shell.rc.wildcards_case_sensitive
767 767
768 768 # Build list of namespaces to search from user options
769 769 def_search.extend(opt('s',[]))
770 770 ns_exclude = ns_exclude=opt('e',[])
771 771 ns_search = [nm for nm in def_search if nm not in ns_exclude]
772 772
773 773 # Call the actual search
774 774 try:
775 775 psearch(args,shell.ns_table,ns_search,
776 776 show_all=opt('a'),ignore_case=ignore_case)
777 777 except:
778 778 shell.showtraceback()
779 779
780 780 def magic_who_ls(self, parameter_s=''):
781 781 """Return a sorted list of all interactive variables.
782 782
783 783 If arguments are given, only variables of types matching these
784 784 arguments are returned."""
785 785
786 786 user_ns = self.shell.user_ns
787 787 out = []
788 788 typelist = parameter_s.split()
789 789 for i in self.shell.user_ns.keys():
790 790 if not (i.startswith('_') or i.startswith('_i')) \
791 791 and not (self.internal_ns.has_key(i) or
792 792 self.user_config_ns.has_key(i)):
793 793 if typelist:
794 794 if type(user_ns[i]).__name__ in typelist:
795 795 out.append(i)
796 796 else:
797 797 out.append(i)
798 798 out.sort()
799 799 return out
800 800
801 801 def magic_who(self, parameter_s=''):
802 802 """Print all interactive variables, with some minimal formatting.
803 803
804 804 If any arguments are given, only variables whose type matches one of
805 805 these are printed. For example:
806 806
807 807 %who function str
808 808
809 809 will only list functions and strings, excluding all other types of
810 810 variables. To find the proper type names, simply use type(var) at a
811 811 command line to see how python prints type names. For example:
812 812
813 813 In [1]: type('hello')\\
814 814 Out[1]: <type 'str'>
815 815
816 816 indicates that the type name for strings is 'str'.
817 817
818 818 %who always excludes executed names loaded through your configuration
819 819 file and things which are internal to IPython.
820 820
821 821 This is deliberate, as typically you may load many modules and the
822 822 purpose of %who is to show you only what you've manually defined."""
823 823
824 824 varlist = self.magic_who_ls(parameter_s)
825 825 if not varlist:
826 826 print 'Interactive namespace is empty.'
827 827 return
828 828
829 829 # if we have variables, move on...
830 830
831 831 # stupid flushing problem: when prompts have no separators, stdout is
832 832 # getting lost. I'm starting to think this is a python bug. I'm having
833 833 # to force a flush with a print because even a sys.stdout.flush
834 834 # doesn't seem to do anything!
835 835
836 836 count = 0
837 837 for i in varlist:
838 838 print i+'\t',
839 839 count += 1
840 840 if count > 8:
841 841 count = 0
842 842 print
843 843 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
844 844
845 845 print # well, this does force a flush at the expense of an extra \n
846 846
847 847 def magic_whos(self, parameter_s=''):
848 848 """Like %who, but gives some extra information about each variable.
849 849
850 850 The same type filtering of %who can be applied here.
851 851
852 852 For all variables, the type is printed. Additionally it prints:
853 853
854 854 - For {},[],(): their length.
855 855
856 856 - For Numeric arrays, a summary with shape, number of elements,
857 857 typecode and size in memory.
858 858
859 859 - Everything else: a string representation, snipping their middle if
860 860 too long."""
861 861
862 862 varnames = self.magic_who_ls(parameter_s)
863 863 if not varnames:
864 864 print 'Interactive namespace is empty.'
865 865 return
866 866
867 867 # if we have variables, move on...
868 868
869 869 # for these types, show len() instead of data:
870 870 seq_types = [types.DictType,types.ListType,types.TupleType]
871 871
872 872 # for Numeric arrays, display summary info
873 873 try:
874 874 import Numeric
875 875 except ImportError:
876 876 array_type = None
877 877 else:
878 878 array_type = Numeric.ArrayType.__name__
879 879
880 880 # Find all variable names and types so we can figure out column sizes
881 881 get_vars = lambda i: self.shell.user_ns[i]
882 882 type_name = lambda v: type(v).__name__
883 883 varlist = map(get_vars,varnames)
884 884 typelist = map(type_name,varlist)
885 885 # column labels and # of spaces as separator
886 886 varlabel = 'Variable'
887 887 typelabel = 'Type'
888 888 datalabel = 'Data/Info'
889 889 colsep = 3
890 890 # variable format strings
891 891 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
892 892 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
893 893 aformat = "%s: %s elems, type `%s`, %s bytes"
894 894 # find the size of the columns to format the output nicely
895 895 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
896 896 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
897 897 # table header
898 898 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
899 899 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
900 900 # and the table itself
901 901 kb = 1024
902 902 Mb = 1048576 # kb**2
903 903 for vname,var,vtype in zip(varnames,varlist,typelist):
904 904 print itpl(vformat),
905 905 if vtype in seq_types:
906 906 print len(var)
907 907 elif vtype==array_type:
908 908 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
909 909 vsize = Numeric.size(var)
910 910 vbytes = vsize*var.itemsize()
911 911 if vbytes < 100000:
912 912 print aformat % (vshape,vsize,var.typecode(),vbytes)
913 913 else:
914 914 print aformat % (vshape,vsize,var.typecode(),vbytes),
915 915 if vbytes < Mb:
916 916 print '(%s kb)' % (vbytes/kb,)
917 917 else:
918 918 print '(%s Mb)' % (vbytes/Mb,)
919 919 else:
920 920 vstr = str(var)
921 921 if len(vstr) < 50:
922 922 print vstr
923 923 else:
924 924 printpl(vfmt_short)
925 925
926 926 def magic_reset(self, parameter_s=''):
927 927 """Resets the namespace by removing all names defined by the user.
928 928
929 929 Input/Output history are left around in case you need them."""
930 930
931 931 ans = raw_input(
932 932 "Once deleted, variables cannot be recovered. Proceed (y/n)? ")
933 933 if not ans.lower() == 'y':
934 934 print 'Nothing done.'
935 935 return
936 936 user_ns = self.shell.user_ns
937 937 for i in self.magic_who_ls():
938 938 del(user_ns[i])
939 939
940 940 def magic_config(self,parameter_s=''):
941 941 """Show IPython's internal configuration."""
942 942
943 943 page('Current configuration structure:\n'+
944 944 pformat(self.shell.rc.dict()))
945 945
946 946 def magic_logstart(self,parameter_s=''):
947 947 """Start logging anywhere in a session.
948 948
949 949 %logstart [log_name [log_mode]]
950 950
951 951 If no name is given, it defaults to a file named 'ipython.log' in your
952 952 current directory, in 'rotate' mode (see below).
953 953
954 954 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
955 955 history up to that point and then continues logging.
956 956
957 957 %logstart takes a second optional parameter: logging mode. This can be one
958 958 of (note that the modes are given unquoted):\\
959 959 over: overwrite existing log.\\
960 960 backup: rename (if exists) to name~ and start name.\\
961 961 append: well, that says it.\\
962 962 rotate: create rotating logs name.1~, name.2~, etc.
963 963 """
964 964
965 965 #FIXME. This function should all be moved to the Logger class.
966 966
967 967 valid_modes = qw('over backup append rotate')
968 968 if self.LOG:
969 969 print 'Logging is already in place. Logfile:',self.LOG
970 970 return
971 971
972 972 par = parameter_s.strip()
973 973 if not par:
974 974 logname = self.LOGDEF
975 975 logmode = 'rotate' # use rotate for the auto-generated logs
976 976 else:
977 977 try:
978 978 logname,logmode = par.split()
979 979 except:
980 980 try:
981 981 logname = par
982 982 logmode = 'backup'
983 983 except:
984 984 warn('Usage: %log [log_name [log_mode]]')
985 985 return
986 986 if not logmode in valid_modes:
987 987 warn('Logging NOT activated.\n'
988 988 'Usage: %log [log_name [log_mode]]\n'
989 989 'Valid modes: '+str(valid_modes))
990 990 return
991 991
992 992 # If we made it this far, I think we're ok:
993 993 print 'Activating auto-logging.'
994 994 print 'Current session state plus future input saved to:',logname
995 995 print 'Logging mode: ',logmode
996 996 # put logname into rc struct as if it had been called on the command line,
997 997 # so it ends up saved in the log header
998 998 # Save it in case we need to restore it...
999 999 old_logfile = self.shell.rc.opts.get('logfile','')
1000 1000 logname = os.path.expanduser(logname)
1001 1001 self.shell.rc.opts.logfile = logname
1002 1002 self.LOGMODE = logmode # FIXME: this should be set through a function.
1003 1003 try:
1004 1004 header = str(self.LOGHEAD)
1005 1005 self.create_log(header,logname)
1006 1006 self.logstart(header,logname)
1007 1007 except:
1008 1008 self.LOG = '' # we are NOT logging, something went wrong
1009 1009 self.shell.rc.opts.logfile = old_logfile
1010 1010 warn("Couldn't start log: "+str(sys.exc_info()[1]))
1011 1011 else: # log input history up to this point
1012 1012 self.logfile.write(self.shell.user_ns['_ih'][1:])
1013 1013 self.logfile.flush()
1014 1014
1015 1015 def magic_logoff(self,parameter_s=''):
1016 1016 """Temporarily stop logging.
1017 1017
1018 1018 You must have previously started logging."""
1019 1019 self.switch_log(0)
1020 1020
1021 1021 def magic_logon(self,parameter_s=''):
1022 1022 """Restart logging.
1023 1023
1024 1024 This function is for restarting logging which you've temporarily
1025 1025 stopped with %logoff. For starting logging for the first time, you
1026 1026 must use the %logstart function, which allows you to specify an
1027 1027 optional log filename."""
1028 1028
1029 1029 self.switch_log(1)
1030 1030
1031 1031 def magic_logstate(self,parameter_s=''):
1032 1032 """Print the status of the logging system."""
1033 1033
1034 1034 self.logstate()
1035 1035
1036 1036 def magic_pdb(self, parameter_s=''):
1037 1037 """Control the calling of the pdb interactive debugger.
1038 1038
1039 1039 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1040 1040 argument it works as a toggle.
1041 1041
1042 1042 When an exception is triggered, IPython can optionally call the
1043 1043 interactive pdb debugger after the traceback printout. %pdb toggles
1044 1044 this feature on and off."""
1045 1045
1046 1046 par = parameter_s.strip().lower()
1047 1047
1048 1048 if par:
1049 1049 try:
1050 1050 pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1051 1051 except KeyError:
1052 print 'Incorrect argument. Use on/1, off/0 or nothing for a toggle.'
1052 print 'Incorrect argument. Use on/1, off/0, or nothing for a toggle.'
1053 1053 return
1054 1054 else:
1055 1055 self.shell.InteractiveTB.call_pdb = pdb
1056 1056 else:
1057 self.shell.InteractiveTB.call_pdb = 1 - self.shell.InteractiveTB.call_pdb
1058 print 'Automatic pdb calling has been turned',\
1059 on_off(self.shell.InteractiveTB.call_pdb)
1057 new_pdb = not self.shell.InteractiveTB.call_pdb
1058 self.shell.InteractiveTB.call_pdb = new_pdb
1059 if self.shell.isthreaded:
1060 try:
1061 self.sys_excepthook.call_pdb = new_pdb
1062 except:
1063 warn('Failed to activate pdb for threaded exception handler')
1064
1065 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1066
1060 1067
1061 1068
1062 1069 def magic_prun(self, parameter_s ='',user_mode=1,
1063 1070 opts=None,arg_lst=None,prog_ns=None):
1064 1071
1065 1072 """Run a statement through the python code profiler.
1066 1073
1067 1074 Usage:\\
1068 1075 %prun [options] statement
1069 1076
1070 1077 The given statement (which doesn't require quote marks) is run via the
1071 1078 python profiler in a manner similar to the profile.run() function.
1072 1079 Namespaces are internally managed to work correctly; profile.run
1073 1080 cannot be used in IPython because it makes certain assumptions about
1074 1081 namespaces which do not hold under IPython.
1075 1082
1076 1083 Options:
1077 1084
1078 1085 -l <limit>: you can place restrictions on what or how much of the
1079 1086 profile gets printed. The limit value can be:
1080 1087
1081 1088 * A string: only information for function names containing this string
1082 1089 is printed.
1083 1090
1084 1091 * An integer: only these many lines are printed.
1085 1092
1086 1093 * A float (between 0 and 1): this fraction of the report is printed
1087 1094 (for example, use a limit of 0.4 to see the topmost 40% only).
1088 1095
1089 1096 You can combine several limits with repeated use of the option. For
1090 1097 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1091 1098 information about class constructors.
1092 1099
1093 1100 -r: return the pstats.Stats object generated by the profiling. This
1094 1101 object has all the information about the profile in it, and you can
1095 1102 later use it for further analysis or in other functions.
1096 1103
1097 1104 Since magic functions have a particular form of calling which prevents
1098 1105 you from writing something like:\\
1099 1106 In [1]: p = %prun -r print 4 # invalid!\\
1100 1107 you must instead use IPython's automatic variables to assign this:\\
1101 1108 In [1]: %prun -r print 4 \\
1102 1109 Out[1]: <pstats.Stats instance at 0x8222cec>\\
1103 1110 In [2]: stats = _
1104 1111
1105 1112 If you really need to assign this value via an explicit function call,
1106 1113 you can always tap directly into the true name of the magic function
1107 1114 by using the ipmagic function (which IPython automatically adds to the
1108 1115 builtins):\\
1109 1116 In [3]: stats = ipmagic('prun','-r print 4')
1110 1117
1111 1118 You can type ipmagic? for more details on ipmagic.
1112 1119
1113 1120 -s <key>: sort profile by given key. You can provide more than one key
1114 1121 by using the option several times: '-s key1 -s key2 -s key3...'. The
1115 1122 default sorting key is 'time'.
1116 1123
1117 1124 The following is copied verbatim from the profile documentation
1118 1125 referenced below:
1119 1126
1120 1127 When more than one key is provided, additional keys are used as
1121 1128 secondary criteria when the there is equality in all keys selected
1122 1129 before them.
1123 1130
1124 1131 Abbreviations can be used for any key names, as long as the
1125 1132 abbreviation is unambiguous. The following are the keys currently
1126 1133 defined:
1127 1134
1128 1135 Valid Arg Meaning\\
1129 1136 "calls" call count\\
1130 1137 "cumulative" cumulative time\\
1131 1138 "file" file name\\
1132 1139 "module" file name\\
1133 1140 "pcalls" primitive call count\\
1134 1141 "line" line number\\
1135 1142 "name" function name\\
1136 1143 "nfl" name/file/line\\
1137 1144 "stdname" standard name\\
1138 1145 "time" internal time
1139 1146
1140 1147 Note that all sorts on statistics are in descending order (placing
1141 1148 most time consuming items first), where as name, file, and line number
1142 1149 searches are in ascending order (i.e., alphabetical). The subtle
1143 1150 distinction between "nfl" and "stdname" is that the standard name is a
1144 1151 sort of the name as printed, which means that the embedded line
1145 1152 numbers get compared in an odd way. For example, lines 3, 20, and 40
1146 1153 would (if the file names were the same) appear in the string order
1147 1154 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1148 1155 line numbers. In fact, sort_stats("nfl") is the same as
1149 1156 sort_stats("name", "file", "line").
1150 1157
1151 1158 -T <filename>: save profile results as shown on screen to a text
1152 1159 file. The profile is still shown on screen.
1153 1160
1154 1161 -D <filename>: save (via dump_stats) profile statistics to given
1155 1162 filename. This data is in a format understod by the pstats module, and
1156 1163 is generated by a call to the dump_stats() method of profile
1157 1164 objects. The profile is still shown on screen.
1158 1165
1159 1166 If you want to run complete programs under the profiler's control, use
1160 1167 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1161 1168 contains profiler specific options as described here.
1162 1169
1163 1170 You can read the complete documentation for the profile module with:\\
1164 1171 In [1]: import profile; profile.help() """
1165 1172
1166 1173 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1167 1174 # protect user quote marks
1168 1175 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1169 1176
1170 1177 if user_mode: # regular user call
1171 1178 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1172 1179 list_all=1)
1173 1180 namespace = self.shell.user_ns
1174 1181 else: # called to run a program by %run -p
1175 1182 try:
1176 1183 filename = get_py_filename(arg_lst[0])
1177 1184 except IOError,msg:
1178 1185 error(msg)
1179 1186 return
1180 1187
1181 1188 arg_str = 'execfile(filename,prog_ns)'
1182 1189 namespace = locals()
1183 1190
1184 1191 opts.merge(opts_def)
1185 1192
1186 1193 prof = profile.Profile()
1187 1194 try:
1188 1195 prof = prof.runctx(arg_str,namespace,namespace)
1189 1196 sys_exit = ''
1190 1197 except SystemExit:
1191 1198 sys_exit = """*** SystemExit exception caught in code being profiled."""
1192 1199
1193 1200 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1194 1201
1195 1202 lims = opts.l
1196 1203 if lims:
1197 1204 lims = [] # rebuild lims with ints/floats/strings
1198 1205 for lim in opts.l:
1199 1206 try:
1200 1207 lims.append(int(lim))
1201 1208 except ValueError:
1202 1209 try:
1203 1210 lims.append(float(lim))
1204 1211 except ValueError:
1205 1212 lims.append(lim)
1206 1213
1207 1214 # trap output
1208 1215 sys_stdout = sys.stdout
1209 1216 stdout_trap = StringIO()
1210 1217 try:
1211 1218 sys.stdout = stdout_trap
1212 1219 stats.print_stats(*lims)
1213 1220 finally:
1214 1221 sys.stdout = sys_stdout
1215 1222 output = stdout_trap.getvalue()
1216 1223 output = output.rstrip()
1217 1224
1218 1225 page(output,screen_lines=self.shell.rc.screen_length)
1219 1226 print sys_exit,
1220 1227
1221 1228 dump_file = opts.D[0]
1222 1229 text_file = opts.T[0]
1223 1230 if dump_file:
1224 1231 prof.dump_stats(dump_file)
1225 1232 print '\n*** Profile stats marshalled to file',\
1226 1233 `dump_file`+'.',sys_exit
1227 1234 if text_file:
1228 1235 file(text_file,'w').write(output)
1229 1236 print '\n*** Profile printout saved to text file',\
1230 1237 `text_file`+'.',sys_exit
1231 1238
1232 1239 if opts.has_key('r'):
1233 1240 return stats
1234 1241 else:
1235 1242 return None
1236 1243
1237 1244 def magic_run(self, parameter_s ='',runner=None):
1238 1245 """Run the named file inside IPython as a program.
1239 1246
1240 1247 Usage:\\
1241 1248 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1242 1249
1243 1250 Parameters after the filename are passed as command-line arguments to
1244 1251 the program (put in sys.argv). Then, control returns to IPython's
1245 1252 prompt.
1246 1253
1247 1254 This is similar to running at a system prompt:\\
1248 1255 $ python file args\\
1249 1256 but with the advantage of giving you IPython's tracebacks, and of
1250 1257 loading all variables into your interactive namespace for further use
1251 1258 (unless -p is used, see below).
1252 1259
1253 1260 The file is executed in a namespace initially consisting only of
1254 1261 __name__=='__main__' and sys.argv constructed as indicated. It thus
1255 1262 sees its environment as if it were being run as a stand-alone
1256 1263 program. But after execution, the IPython interactive namespace gets
1257 1264 updated with all variables defined in the program (except for __name__
1258 1265 and sys.argv). This allows for very convenient loading of code for
1259 1266 interactive work, while giving each program a 'clean sheet' to run in.
1260 1267
1261 1268 Options:
1262 1269
1263 1270 -n: __name__ is NOT set to '__main__', but to the running file's name
1264 1271 without extension (as python does under import). This allows running
1265 1272 scripts and reloading the definitions in them without calling code
1266 1273 protected by an ' if __name__ == "__main__" ' clause.
1267 1274
1268 1275 -i: run the file in IPython's namespace instead of an empty one. This
1269 1276 is useful if you are experimenting with code written in a text editor
1270 1277 which depends on variables defined interactively.
1271 1278
1272 1279 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1273 1280 being run. This is particularly useful if IPython is being used to
1274 1281 run unittests, which always exit with a sys.exit() call. In such
1275 1282 cases you are interested in the output of the test results, not in
1276 1283 seeing a traceback of the unittest module.
1277 1284
1278 1285 -t: print timing information at the end of the run. IPython will give
1279 1286 you an estimated CPU time consumption for your script, which under
1280 1287 Unix uses the resource module to avoid the wraparound problems of
1281 1288 time.clock(). Under Unix, an estimate of time spent on system tasks
1282 1289 is also given (for Windows platforms this is reported as 0.0).
1283 1290
1284 1291 If -t is given, an additional -N<N> option can be given, where <N>
1285 1292 must be an integer indicating how many times you want the script to
1286 1293 run. The final timing report will include total and per run results.
1287 1294
1288 1295 For example (testing the script uniq_stable.py):
1289 1296
1290 1297 In [1]: run -t uniq_stable
1291 1298
1292 1299 IPython CPU timings (estimated):\\
1293 1300 User : 0.19597 s.\\
1294 1301 System: 0.0 s.\\
1295 1302
1296 1303 In [2]: run -t -N5 uniq_stable
1297 1304
1298 1305 IPython CPU timings (estimated):\\
1299 1306 Total runs performed: 5\\
1300 1307 Times : Total Per run\\
1301 1308 User : 0.910862 s, 0.1821724 s.\\
1302 1309 System: 0.0 s, 0.0 s.
1303 1310
1304 1311 -d: run your program under the control of pdb, the Python debugger.
1305 1312 This allows you to execute your program step by step, watch variables,
1306 1313 etc. Internally, what IPython does is similar to calling:
1307 1314
1308 1315 pdb.run('execfile("YOURFILENAME")')
1309 1316
1310 1317 with a breakpoint set on line 1 of your file. You can change the line
1311 1318 number for this automatic breakpoint to be <N> by using the -bN option
1312 1319 (where N must be an integer). For example:
1313 1320
1314 1321 %run -d -b40 myscript
1315 1322
1316 1323 will set the first breakpoint at line 40 in myscript.py. Note that
1317 1324 the first breakpoint must be set on a line which actually does
1318 1325 something (not a comment or docstring) for it to stop execution.
1319 1326
1320 1327 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1321 1328 first enter 'c' (without qoutes) to start execution up to the first
1322 1329 breakpoint.
1323 1330
1324 1331 Entering 'help' gives information about the use of the debugger. You
1325 1332 can easily see pdb's full documentation with "import pdb;pdb.help()"
1326 1333 at a prompt.
1327 1334
1328 1335 -p: run program under the control of the Python profiler module (which
1329 1336 prints a detailed report of execution times, function calls, etc).
1330 1337
1331 1338 You can pass other options after -p which affect the behavior of the
1332 1339 profiler itself. See the docs for %prun for details.
1333 1340
1334 1341 In this mode, the program's variables do NOT propagate back to the
1335 1342 IPython interactive namespace (because they remain in the namespace
1336 1343 where the profiler executes them).
1337 1344
1338 1345 Internally this triggers a call to %prun, see its documentation for
1339 1346 details on the options available specifically for profiling."""
1340 1347
1341 1348 # get arguments and set sys.argv for program to be run.
1342 1349 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1343 1350 mode='list',list_all=1)
1344 1351
1345 1352 try:
1346 1353 filename = get_py_filename(arg_lst[0])
1347 1354 except IndexError:
1348 1355 warn('you must provide at least a filename.')
1349 1356 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1350 1357 return
1351 1358 except IOError,msg:
1352 1359 error(msg)
1353 1360 return
1354 1361
1355 1362 # Control the response to exit() calls made by the script being run
1356 1363 exit_ignore = opts.has_key('e')
1357 1364
1358 1365 # Make sure that the running script gets a proper sys.argv as if it
1359 1366 # were run from a system shell.
1360 1367 save_argv = sys.argv # save it for later restoring
1361 1368 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1362 1369
1363 1370 if opts.has_key('i'):
1364 1371 prog_ns = self.shell.user_ns
1365 1372 __name__save = self.shell.user_ns['__name__']
1366 1373 prog_ns['__name__'] = '__main__'
1367 1374 else:
1368 1375 if opts.has_key('n'):
1369 1376 name = os.path.splitext(os.path.basename(filename))[0]
1370 1377 else:
1371 1378 name = '__main__'
1372 1379 prog_ns = {'__name__':name}
1373 1380
1374 1381 # pickle fix. See iplib for an explanation
1375 1382 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1376 1383
1377 1384 stats = None
1378 1385 try:
1379 1386 if opts.has_key('p'):
1380 1387 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1381 1388 else:
1382 1389 if opts.has_key('d'):
1383 1390 deb = Debugger.Pdb(self.shell.rc.colors)
1384 1391 # reset Breakpoint state, which is moronically kept
1385 1392 # in a class
1386 1393 bdb.Breakpoint.next = 1
1387 1394 bdb.Breakpoint.bplist = {}
1388 1395 bdb.Breakpoint.bpbynumber = [None]
1389 1396 # Set an initial breakpoint to stop execution
1390 1397 maxtries = 10
1391 1398 bp = int(opts.get('b',[1])[0])
1392 1399 checkline = deb.checkline(filename,bp)
1393 1400 if not checkline:
1394 1401 for bp in range(bp+1,bp+maxtries+1):
1395 1402 if deb.checkline(filename,bp):
1396 1403 break
1397 1404 else:
1398 1405 msg = ("\nI failed to find a valid line to set "
1399 1406 "a breakpoint\n"
1400 1407 "after trying up to line: %s.\n"
1401 1408 "Please set a valid breakpoint manually "
1402 1409 "with the -b option." % bp)
1403 1410 error(msg)
1404 1411 return
1405 1412 # if we find a good linenumber, set the breakpoint
1406 1413 deb.do_break('%s:%s' % (filename,bp))
1407 1414 # Start file run
1408 1415 print "NOTE: Enter 'c' at the",
1409 1416 print "ipdb> prompt to start your script."
1410 1417 try:
1411 1418 deb.run('execfile("%s")' % filename,prog_ns)
1412 1419 except:
1413 1420 etype, value, tb = sys.exc_info()
1414 1421 # Skip three frames in the traceback: the %run one,
1415 1422 # one inside bdb.py, and the command-line typed by the
1416 1423 # user (run by exec in pdb itself).
1417 1424 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1418 1425 else:
1419 1426 if runner is None:
1420 1427 runner = self.shell.safe_execfile
1421 1428 if opts.has_key('t'):
1422 1429 try:
1423 1430 nruns = int(opts['N'][0])
1424 1431 if nruns < 1:
1425 1432 error('Number of runs must be >=1')
1426 1433 return
1427 1434 except (KeyError):
1428 1435 nruns = 1
1429 1436 if nruns == 1:
1430 1437 t0 = clock2()
1431 1438 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1432 1439 t1 = clock2()
1433 1440 t_usr = t1[0]-t0[0]
1434 1441 t_sys = t1[1]-t1[1]
1435 1442 print "\nIPython CPU timings (estimated):"
1436 1443 print " User : %10s s." % t_usr
1437 1444 print " System: %10s s." % t_sys
1438 1445 else:
1439 1446 runs = range(nruns)
1440 1447 t0 = clock2()
1441 1448 for nr in runs:
1442 1449 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1443 1450 t1 = clock2()
1444 1451 t_usr = t1[0]-t0[0]
1445 1452 t_sys = t1[1]-t1[1]
1446 1453 print "\nIPython CPU timings (estimated):"
1447 1454 print "Total runs performed:",nruns
1448 1455 print " Times : %10s %10s" % ('Total','Per run')
1449 1456 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1450 1457 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1451 1458
1452 1459 else:
1453 1460 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1454 1461 if opts.has_key('i'):
1455 1462 self.shell.user_ns['__name__'] = __name__save
1456 1463 else:
1457 1464 # update IPython interactive namespace
1458 1465 del prog_ns['__name__']
1459 1466 self.shell.user_ns.update(prog_ns)
1460 1467 finally:
1461 1468 sys.argv = save_argv
1462 1469 return stats
1463 1470
1464 1471 def magic_runlog(self, parameter_s =''):
1465 1472 """Run files as logs.
1466 1473
1467 1474 Usage:\\
1468 1475 %runlog file1 file2 ...
1469 1476
1470 1477 Run the named files (treating them as log files) in sequence inside
1471 1478 the interpreter, and return to the prompt. This is much slower than
1472 1479 %run because each line is executed in a try/except block, but it
1473 1480 allows running files with syntax errors in them.
1474 1481
1475 1482 Normally IPython will guess when a file is one of its own logfiles, so
1476 1483 you can typically use %run even for logs. This shorthand allows you to
1477 1484 force any file to be treated as a log file."""
1478 1485
1479 1486 for f in parameter_s.split():
1480 1487 self.shell.safe_execfile(f,self.shell.user_ns,
1481 1488 self.shell.user_ns,islog=1)
1482 1489
1483 1490 def magic_time(self,parameter_s = ''):
1484 1491 """Time execution of a Python statement or expression.
1485 1492
1486 1493 The CPU and wall clock times are printed, and the value of the
1487 1494 expression (if any) is returned. Note that under Win32, system time
1488 1495 is always reported as 0, since it can not be measured.
1489 1496
1490 1497 This function provides very basic timing functionality. In Python
1491 1498 2.3, the timeit module offers more control and sophistication, but for
1492 1499 now IPython supports Python 2.2, so we can not rely on timeit being
1493 1500 present.
1494 1501
1495 1502 Some examples:
1496 1503
1497 1504 In [1]: time 2**128
1498 1505 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1499 1506 Wall time: 0.00
1500 1507 Out[1]: 340282366920938463463374607431768211456L
1501 1508
1502 1509 In [2]: n = 1000000
1503 1510
1504 1511 In [3]: time sum(range(n))
1505 1512 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1506 1513 Wall time: 1.37
1507 1514 Out[3]: 499999500000L
1508 1515
1509 1516 In [4]: time print 'hello world'
1510 1517 hello world
1511 1518 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1512 1519 Wall time: 0.00
1513 1520 """
1514 1521
1515 1522 # fail immediately if the given expression can't be compiled
1516 1523 try:
1517 1524 mode = 'eval'
1518 1525 code = compile(parameter_s,'<timed eval>',mode)
1519 1526 except SyntaxError:
1520 1527 mode = 'exec'
1521 1528 code = compile(parameter_s,'<timed exec>',mode)
1522 1529 # skew measurement as little as possible
1523 1530 glob = self.shell.user_ns
1524 1531 clk = clock2
1525 1532 wtime = time.time
1526 1533 # time execution
1527 1534 wall_st = wtime()
1528 1535 if mode=='eval':
1529 1536 st = clk()
1530 1537 out = eval(code,glob)
1531 1538 end = clk()
1532 1539 else:
1533 1540 st = clk()
1534 1541 exec code in glob
1535 1542 end = clk()
1536 1543 out = None
1537 1544 wall_end = wtime()
1538 1545 # Compute actual times and report
1539 1546 wall_time = wall_end-wall_st
1540 1547 cpu_user = end[0]-st[0]
1541 1548 cpu_sys = end[1]-st[1]
1542 1549 cpu_tot = cpu_user+cpu_sys
1543 1550 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1544 1551 (cpu_user,cpu_sys,cpu_tot)
1545 1552 print "Wall time: %.2f" % wall_time
1546 1553 return out
1547 1554
1548 1555 def magic_macro(self,parameter_s = ''):
1549 1556 """Define a set of input lines as a macro for future re-execution.
1550 1557
1551 1558 Usage:\\
1552 1559 %macro name n1:n2 n3:n4 ... n5 .. n6 ...
1553 1560
1554 1561 This will define a global variable called `name` which is a string
1555 1562 made of joining the slices and lines you specify (n1,n2,... numbers
1556 1563 above) from your input history into a single string. This variable
1557 1564 acts like an automatic function which re-executes those lines as if
1558 1565 you had typed them. You just type 'name' at the prompt and the code
1559 1566 executes.
1560 1567
1561 1568 Note that the slices use the standard Python slicing notation (5:8
1562 1569 means include lines numbered 5,6,7).
1563 1570
1564 1571 For example, if your history contains (%hist prints it):
1565 1572
1566 1573 44: x=1\\
1567 1574 45: y=3\\
1568 1575 46: z=x+y\\
1569 1576 47: print x\\
1570 1577 48: a=5\\
1571 1578 49: print 'x',x,'y',y\\
1572 1579
1573 1580 you can create a macro with lines 44 through 47 (included) and line 49
1574 1581 called my_macro with:
1575 1582
1576 1583 In [51]: %macro my_macro 44:48 49
1577 1584
1578 1585 Now, typing `my_macro` (without quotes) will re-execute all this code
1579 1586 in one pass.
1580 1587
1581 1588 You don't need to give the line-numbers in order, and any given line
1582 1589 number can appear multiple times. You can assemble macros with any
1583 1590 lines from your input history in any order.
1584 1591
1585 1592 The macro is a simple object which holds its value in an attribute,
1586 1593 but IPython's display system checks for macros and executes them as
1587 1594 code instead of printing them when you type their name.
1588 1595
1589 1596 You can view a macro's contents by explicitly printing it with:
1590 1597
1591 1598 'print macro_name'.
1592 1599
1593 1600 For one-off cases which DON'T contain magic function calls in them you
1594 1601 can obtain similar results by explicitly executing slices from your
1595 1602 input history with:
1596 1603
1597 1604 In [60]: exec In[44:48]+In[49]"""
1598 1605
1599 1606 args = parameter_s.split()
1600 1607 name,ranges = args[0], args[1:]
1601 1608 #print 'rng',ranges # dbg
1602 1609 cmds = self.extract_input_slices(ranges)
1603 1610 macro = Macro(cmds)
1604 1611 self.shell.user_ns.update({name:macro})
1605 1612 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1606 1613 print 'Macro contents:'
1607 1614 print str(macro).rstrip(),
1608 1615
1609 1616 def magic_save(self,parameter_s = ''):
1610 1617 """Save a set of lines to a given filename.
1611 1618
1612 1619 Usage:\\
1613 1620 %save filename n1:n2 n3:n4 ... n5 .. n6 ...
1614 1621
1615 1622 This function uses the same syntax as %macro for line extraction, but
1616 1623 instead of creating a macro it saves the resulting string to the
1617 1624 filename you specify.
1618 1625
1619 1626 It adds a '.py' extension to the file if you don't do so yourself, and
1620 1627 it asks for confirmation before overwriting existing files."""
1621 1628
1622 1629 args = parameter_s.split()
1623 1630 fname,ranges = args[0], args[1:]
1624 1631 if not fname.endswith('.py'):
1625 1632 fname += '.py'
1626 1633 if os.path.isfile(fname):
1627 1634 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1628 1635 if ans.lower() not in ['y','yes']:
1629 1636 print 'Operation cancelled.'
1630 1637 return
1631 1638 cmds = ''.join(self.extract_input_slices(ranges))
1632 1639 f = file(fname,'w')
1633 1640 f.write(cmds)
1634 1641 f.close()
1635 1642 print 'The following commands were written to file `%s`:' % fname
1636 1643 print cmds
1637 1644
1638 1645 def magic_ed(self,parameter_s = ''):
1639 1646 """Alias to %edit."""
1640 1647 return self.magic_edit(parameter_s)
1641 1648
1642 1649 def magic_edit(self,parameter_s = '',last_call=['','']):
1643 1650 """Bring up an editor and execute the resulting code.
1644 1651
1645 1652 Usage:
1646 1653 %edit [options] [args]
1647 1654
1648 1655 %edit runs IPython's editor hook. The default version of this hook is
1649 1656 set to call the __IPYTHON__.rc.editor command. This is read from your
1650 1657 environment variable $EDITOR. If this isn't found, it will default to
1651 1658 vi under Linux/Unix and to notepad under Windows. See the end of this
1652 1659 docstring for how to change the editor hook.
1653 1660
1654 1661 You can also set the value of this editor via the command line option
1655 1662 '-editor' or in your ipythonrc file. This is useful if you wish to use
1656 1663 specifically for IPython an editor different from your typical default
1657 1664 (and for Windows users who typically don't set environment variables).
1658 1665
1659 1666 This command allows you to conveniently edit multi-line code right in
1660 1667 your IPython session.
1661 1668
1662 1669 If called without arguments, %edit opens up an empty editor with a
1663 1670 temporary file and will execute the contents of this file when you
1664 1671 close it (don't forget to save it!).
1665 1672
1666 1673 Options:
1667 1674
1668 1675 -p: this will call the editor with the same data as the previous time
1669 1676 it was used, regardless of how long ago (in your current session) it
1670 1677 was.
1671 1678
1672 1679 -x: do not execute the edited code immediately upon exit. This is
1673 1680 mainly useful if you are editing programs which need to be called with
1674 1681 command line arguments, which you can then do using %run.
1675 1682
1676 1683 Arguments:
1677 1684
1678 1685 If arguments are given, the following possibilites exist:
1679 1686
1680 1687 - The arguments are numbers or pairs of colon-separated numbers (like
1681 1688 1 4:8 9). These are interpreted as lines of previous input to be
1682 1689 loaded into the editor. The syntax is the same of the %macro command.
1683 1690
1684 1691 - If the argument doesn't start with a number, it is evaluated as a
1685 1692 variable and its contents loaded into the editor. You can thus edit
1686 1693 any string which contains python code (including the result of
1687 1694 previous edits).
1688 1695
1689 1696 - If the argument is the name of an object (other than a string),
1690 1697 IPython will try to locate the file where it was defined and open the
1691 1698 editor at the point where it is defined. You can use `%edit function`
1692 1699 to load an editor exactly at the point where 'function' is defined,
1693 1700 edit it and have the file be executed automatically.
1694 1701
1695 1702 Note: opening at an exact line is only supported under Unix, and some
1696 1703 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1697 1704 '+NUMBER' parameter necessary for this feature. Good editors like
1698 1705 (X)Emacs, vi, jed, pico and joe all do.
1699 1706
1700 1707 - If the argument is not found as a variable, IPython will look for a
1701 1708 file with that name (adding .py if necessary) and load it into the
1702 1709 editor. It will execute its contents with execfile() when you exit,
1703 1710 loading any code in the file into your interactive namespace.
1704 1711
1705 1712 After executing your code, %edit will return as output the code you
1706 1713 typed in the editor (except when it was an existing file). This way
1707 1714 you can reload the code in further invocations of %edit as a variable,
1708 1715 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1709 1716 the output.
1710 1717
1711 1718 Note that %edit is also available through the alias %ed.
1712 1719
1713 1720 This is an example of creating a simple function inside the editor and
1714 1721 then modifying it. First, start up the editor:
1715 1722
1716 1723 In [1]: ed\\
1717 1724 Editing... done. Executing edited code...\\
1718 1725 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1719 1726
1720 1727 We can then call the function foo():
1721 1728
1722 1729 In [2]: foo()\\
1723 1730 foo() was defined in an editing session
1724 1731
1725 1732 Now we edit foo. IPython automatically loads the editor with the
1726 1733 (temporary) file where foo() was previously defined:
1727 1734
1728 1735 In [3]: ed foo\\
1729 1736 Editing... done. Executing edited code...
1730 1737
1731 1738 And if we call foo() again we get the modified version:
1732 1739
1733 1740 In [4]: foo()\\
1734 1741 foo() has now been changed!
1735 1742
1736 1743 Here is an example of how to edit a code snippet successive
1737 1744 times. First we call the editor:
1738 1745
1739 1746 In [8]: ed\\
1740 1747 Editing... done. Executing edited code...\\
1741 1748 hello\\
1742 1749 Out[8]: "print 'hello'\\n"
1743 1750
1744 1751 Now we call it again with the previous output (stored in _):
1745 1752
1746 1753 In [9]: ed _\\
1747 1754 Editing... done. Executing edited code...\\
1748 1755 hello world\\
1749 1756 Out[9]: "print 'hello world'\\n"
1750 1757
1751 1758 Now we call it with the output #8 (stored in _8, also as Out[8]):
1752 1759
1753 1760 In [10]: ed _8\\
1754 1761 Editing... done. Executing edited code...\\
1755 1762 hello again\\
1756 1763 Out[10]: "print 'hello again'\\n"
1757 1764
1758 1765
1759 1766 Changing the default editor hook:
1760 1767
1761 1768 If you wish to write your own editor hook, you can put it in a
1762 1769 configuration file which you load at startup time. The default hook
1763 1770 is defined in the IPython.hooks module, and you can use that as a
1764 1771 starting example for further modifications. That file also has
1765 1772 general instructions on how to set a new hook for use once you've
1766 1773 defined it."""
1767 1774
1768 1775 # FIXME: This function has become a convoluted mess. It needs a
1769 1776 # ground-up rewrite with clean, simple logic.
1770 1777
1771 1778 def make_filename(arg):
1772 1779 "Make a filename from the given args"
1773 1780 try:
1774 1781 filename = get_py_filename(arg)
1775 1782 except IOError:
1776 1783 if args.endswith('.py'):
1777 1784 filename = arg
1778 1785 else:
1779 1786 filename = None
1780 1787 return filename
1781 1788
1782 1789 # custom exceptions
1783 1790 class DataIsObject(Exception): pass
1784 1791
1785 1792 opts,args = self.parse_options(parameter_s,'px')
1786 1793
1787 1794 # Default line number value
1788 1795 lineno = None
1789 1796 if opts.has_key('p'):
1790 1797 args = '_%s' % last_call[0]
1791 1798 if not self.shell.user_ns.has_key(args):
1792 1799 args = last_call[1]
1793 1800
1794 1801 # use last_call to remember the state of the previous call, but don't
1795 1802 # let it be clobbered by successive '-p' calls.
1796 1803 try:
1797 1804 last_call[0] = self.shell.outputcache.prompt_count
1798 1805 if not opts.has_key('p'):
1799 1806 last_call[1] = parameter_s
1800 1807 except:
1801 1808 pass
1802 1809
1803 1810 # by default this is done with temp files, except when the given
1804 1811 # arg is a filename
1805 1812 use_temp = 1
1806 1813
1807 1814 if re.match(r'\d',args):
1808 1815 # Mode where user specifies ranges of lines, like in %macro.
1809 1816 # This means that you can't edit files whose names begin with
1810 1817 # numbers this way. Tough.
1811 1818 ranges = args.split()
1812 1819 data = ''.join(self.extract_input_slices(ranges))
1813 1820 elif args.endswith('.py'):
1814 1821 filename = make_filename(args)
1815 1822 data = ''
1816 1823 use_temp = 0
1817 1824 elif args:
1818 1825 try:
1819 1826 # Load the parameter given as a variable. If not a string,
1820 1827 # process it as an object instead (below)
1821 1828
1822 1829 #print '*** args',args,'type',type(args) # dbg
1823 1830 data = eval(args,self.shell.user_ns)
1824 1831 if not type(data) in StringTypes:
1825 1832 raise DataIsObject
1826 1833 except (NameError,SyntaxError):
1827 1834 # given argument is not a variable, try as a filename
1828 1835 filename = make_filename(args)
1829 1836 if filename is None:
1830 1837 warn("Argument given (%s) can't be found as a variable "
1831 1838 "or as a filename." % args)
1832 1839 return
1833 1840 data = ''
1834 1841 use_temp = 0
1835 1842 except DataIsObject:
1836 1843 # For objects, try to edit the file where they are defined
1837 1844 try:
1838 1845 filename = inspect.getabsfile(data)
1839 1846 datafile = 1
1840 1847 except TypeError:
1841 1848 filename = make_filename(args)
1842 1849 datafile = 1
1843 1850 warn('Could not find file where `%s` is defined.\n'
1844 1851 'Opening a file named `%s`' % (args,filename))
1845 1852 # Now, make sure we can actually read the source (if it was in
1846 1853 # a temp file it's gone by now).
1847 1854 if datafile:
1848 1855 try:
1849 1856 lineno = inspect.getsourcelines(data)[1]
1850 1857 except IOError:
1851 1858 filename = make_filename(args)
1852 1859 if filename is None:
1853 1860 warn('The file `%s` where `%s` was defined cannot '
1854 1861 'be read.' % (filename,data))
1855 1862 return
1856 1863 use_temp = 0
1857 1864 else:
1858 1865 data = ''
1859 1866
1860 1867 if use_temp:
1861 1868 filename = tempfile.mktemp('.py')
1862 1869 self.shell.tempfiles.append(filename)
1863 1870
1864 1871 if data and use_temp:
1865 1872 tmp_file = open(filename,'w')
1866 1873 tmp_file.write(data)
1867 1874 tmp_file.close()
1868 1875
1869 1876 # do actual editing here
1870 1877 print 'Editing...',
1871 1878 sys.stdout.flush()
1872 1879 self.shell.hooks.editor(filename,lineno)
1873 1880 if opts.has_key('x'): # -x prevents actual execution
1874 1881 print
1875 1882 else:
1876 1883 print 'done. Executing edited code...'
1877 1884 try:
1878 1885 self.shell.safe_execfile(filename,self.shell.user_ns)
1879 1886 except IOError,msg:
1880 1887 if msg.filename == filename:
1881 1888 warn('File not found. Did you forget to save?')
1882 1889 return
1883 1890 else:
1884 1891 self.shell.showtraceback()
1885 1892 except:
1886 1893 self.shell.showtraceback()
1887 1894 if use_temp:
1888 1895 contents = open(filename).read()
1889 1896 return contents
1890 1897
1891 1898 def magic_xmode(self,parameter_s = ''):
1892 1899 """Switch modes for the exception handlers.
1893 1900
1894 1901 Valid modes: Plain, Context and Verbose.
1895 1902
1896 1903 If called without arguments, acts as a toggle."""
1897 1904
1905 def xmode_switch_err(name):
1906 warn('Error changing %s exception modes.\n%s' %
1907 (name,sys.exc_info()[1]))
1908
1898 1909 new_mode = parameter_s.strip().capitalize()
1899 1910 try:
1900 self.InteractiveTB.set_mode(mode = new_mode)
1911 self.InteractiveTB.set_mode(mode=new_mode)
1901 1912 print 'Exception reporting mode:',self.InteractiveTB.mode
1902 1913 except:
1903 warn('Error changing exception modes.\n' + str(sys.exc_info()[1]))
1914 xmode_switch_err('user')
1915
1916 # threaded shells use a special handler in sys.excepthook
1917 if self.isthreaded:
1918 try:
1919 self.shell.sys_excepthook.set_mode(mode=new_mode)
1920 except:
1921 xmode_switch_err('threaded')
1904 1922
1905 1923 def magic_colors(self,parameter_s = ''):
1906 1924 """Switch color scheme for prompts, info system and exception handlers.
1907 1925
1908 1926 Currently implemented schemes: NoColor, Linux, LightBG.
1909 1927
1910 1928 Color scheme names are not case-sensitive."""
1929
1930 def color_switch_err(name):
1931 warn('Error changing %s color schemes.\n%s' %
1932 (name,sys.exc_info()[1]))
1933
1911 1934
1912 1935 new_scheme = parameter_s.strip()
1913 1936 if not new_scheme:
1914 1937 print 'You must specify a color scheme.'
1915 1938 return
1916 1939 # Under Windows, check for Gary Bishop's readline, which is necessary
1917 1940 # for ANSI coloring
1918 1941 if os.name in ['nt','dos']:
1919 1942 try:
1920 1943 import readline
1921 1944 except ImportError:
1922 1945 has_readline = 0
1923 1946 else:
1924 1947 try:
1925 1948 readline.GetOutputFile()
1926 1949 except AttributeError:
1927 1950 has_readline = 0
1928 1951 else:
1929 1952 has_readline = 1
1930 1953 if not has_readline:
1931 1954 msg = """\
1932 1955 Proper color support under MS Windows requires Gary Bishop's readline library.
1933 1956 You can find it at:
1934 1957 http://sourceforge.net/projects/uncpythontools
1935 1958 Gary's readline needs the ctypes module, from:
1936 1959 http://starship.python.net/crew/theller/ctypes
1937 1960
1938 1961 Defaulting color scheme to 'NoColor'"""
1939 1962 new_scheme = 'NoColor'
1940 1963 warn(msg)
1941 1964
1942 1965 # Set prompt colors
1943 1966 try:
1944 1967 self.shell.outputcache.set_colors(new_scheme)
1945 1968 except:
1946 warn('Error changing prompt color schemes.\n'
1947 + str(sys.exc_info()[1]))
1969 color_switch_err('prompt')
1948 1970 else:
1949 1971 self.shell.rc.colors = \
1950 1972 self.shell.outputcache.color_table.active_scheme_name
1951 1973 # Set exception colors
1952 1974 try:
1953 1975 self.shell.InteractiveTB.set_colors(scheme = new_scheme)
1954 1976 self.shell.SyntaxTB.set_colors(scheme = new_scheme)
1955 1977 except:
1956 warn('Error changing exception color schemes.\n'
1957 + str(sys.exc_info()[1]))
1978 color_switch_err('exception')
1979
1980 # threaded shells use a verbose traceback in sys.excepthook
1981 if self.isthreaded:
1982 try:
1983 self.shell.sys_excepthook.set_colors(scheme=new_scheme)
1984 except:
1985 color_switch_err('system exception handler')
1986
1958 1987 # Set info (for 'object?') colors
1959 1988 if self.shell.rc.color_info:
1960 1989 try:
1961 1990 self.shell.inspector.set_active_scheme(new_scheme)
1962 1991 except:
1963 warn('Error changing object inspector color schemes.\n'
1964 + str(sys.exc_info()[1]))
1992 color_switch_err('object inspector')
1965 1993 else:
1966 1994 self.shell.inspector.set_active_scheme('NoColor')
1967 1995
1968 1996 def magic_color_info(self,parameter_s = ''):
1969 1997 """Toggle color_info.
1970 1998
1971 1999 The color_info configuration parameter controls whether colors are
1972 2000 used for displaying object details (by things like %psource, %pfile or
1973 2001 the '?' system). This function toggles this value with each call.
1974 2002
1975 2003 Note that unless you have a fairly recent pager (less works better
1976 2004 than more) in your system, using colored object information displays
1977 2005 will not work properly. Test it and see."""
1978 2006
1979 2007 self.shell.rc.color_info = 1 - self.shell.rc.color_info
1980 2008 self.magic_colors(self.shell.rc.colors)
1981 2009 print 'Object introspection functions have now coloring:',
1982 2010 print ['OFF','ON'][self.shell.rc.color_info]
1983 2011
1984 2012 def magic_Pprint(self, parameter_s=''):
1985 2013 """Toggle pretty printing on/off."""
1986 2014
1987 2015 self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint
1988 2016 print 'Pretty printing has been turned', \
1989 2017 ['OFF','ON'][self.shell.outputcache.Pprint]
1990 2018
1991 2019 def magic_exit(self, parameter_s=''):
1992 2020 """Exit IPython, confirming if configured to do so.
1993 2021
1994 2022 You can configure whether IPython asks for confirmation upon exit by
1995 2023 setting the confirm_exit flag in the ipythonrc file."""
1996 2024
1997 2025 self.shell.exit()
1998 2026
1999 2027 def magic_quit(self, parameter_s=''):
2000 2028 """Exit IPython, confirming if configured to do so (like %exit)"""
2001 2029
2002 2030 self.shell.exit()
2003 2031
2004 2032 def magic_Exit(self, parameter_s=''):
2005 2033 """Exit IPython without confirmation."""
2006 2034
2007 2035 self.shell.exit_now = True
2008 2036
2009 2037 def magic_Quit(self, parameter_s=''):
2010 2038 """Exit IPython without confirmation (like %Exit)."""
2011 2039
2012 2040 self.shell.exit_now = True
2013 2041
2014 2042 #......................................................................
2015 2043 # Functions to implement unix shell-type things
2016 2044
2017 2045 def magic_alias(self, parameter_s = ''):
2018 2046 """Define an alias for a system command.
2019 2047
2020 2048 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2021 2049
2022 2050 Then, typing 'alias_name params' will execute the system command 'cmd
2023 2051 params' (from your underlying operating system).
2024 2052
2025 2053 Aliases have lower precedence than magic functions and Python normal
2026 2054 variables, so if 'foo' is both a Python variable and an alias, the
2027 2055 alias can not be executed until 'del foo' removes the Python variable.
2028 2056
2029 2057 You can use the %l specifier in an alias definition to represent the
2030 2058 whole line when the alias is called. For example:
2031 2059
2032 2060 In [2]: alias all echo "Input in brackets: <%l>"\\
2033 2061 In [3]: all hello world\\
2034 2062 Input in brackets: <hello world>
2035 2063
2036 2064 You can also define aliases with parameters using %s specifiers (one
2037 2065 per parameter):
2038 2066
2039 2067 In [1]: alias parts echo first %s second %s\\
2040 2068 In [2]: %parts A B\\
2041 2069 first A second B\\
2042 2070 In [3]: %parts A\\
2043 2071 Incorrect number of arguments: 2 expected.\\
2044 2072 parts is an alias to: 'echo first %s second %s'
2045 2073
2046 2074 Note that %l and %s are mutually exclusive. You can only use one or
2047 2075 the other in your aliases.
2048 2076
2049 2077 Aliases expand Python variables just like system calls using ! or !!
2050 2078 do: all expressions prefixed with '$' get expanded. For details of
2051 2079 the semantic rules, see PEP-215:
2052 2080 http://www.python.org/peps/pep-0215.html. This is the library used by
2053 2081 IPython for variable expansion. If you want to access a true shell
2054 2082 variable, an extra $ is necessary to prevent its expansion by IPython:
2055 2083
2056 2084 In [6]: alias show echo\\
2057 2085 In [7]: PATH='A Python string'\\
2058 2086 In [8]: show $PATH\\
2059 2087 A Python string\\
2060 2088 In [9]: show $$PATH\\
2061 2089 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2062 2090
2063 2091 You can use the alias facility to acess all of $PATH. See the %rehash
2064 2092 and %rehashx functions, which automatically create aliases for the
2065 2093 contents of your $PATH.
2066 2094
2067 2095 If called with no parameters, %alias prints the current alias table."""
2068 2096
2069 2097 par = parameter_s.strip()
2070 2098 if not par:
2071 2099 if self.shell.rc.automagic:
2072 2100 prechar = ''
2073 2101 else:
2074 2102 prechar = self.shell.ESC_MAGIC
2075 2103 print 'Alias\t\tSystem Command\n'+'-'*30
2076 2104 atab = self.shell.alias_table
2077 2105 aliases = atab.keys()
2078 2106 aliases.sort()
2079 2107 for alias in aliases:
2080 2108 print prechar+alias+'\t\t'+atab[alias][1]
2081 2109 print '-'*30+'\nTotal number of aliases:',len(aliases)
2082 2110 return
2083 2111 try:
2084 2112 alias,cmd = par.split(None,1)
2085 2113 except:
2086 2114 print OInspect.getdoc(self.magic_alias)
2087 2115 else:
2088 2116 nargs = cmd.count('%s')
2089 2117 if nargs>0 and cmd.find('%l')>=0:
2090 2118 error('The %s and %l specifiers are mutually exclusive '
2091 2119 'in alias definitions.')
2092 2120 else: # all looks OK
2093 2121 self.shell.alias_table[alias] = (nargs,cmd)
2094 2122 self.shell.alias_table_validate(verbose=1)
2095 2123 # end magic_alias
2096 2124
2097 2125 def magic_unalias(self, parameter_s = ''):
2098 2126 """Remove an alias"""
2099 2127
2100 2128 aname = parameter_s.strip()
2101 2129 if aname in self.shell.alias_table:
2102 2130 del self.shell.alias_table[aname]
2103 2131
2104 2132 def magic_rehash(self, parameter_s = ''):
2105 2133 """Update the alias table with all entries in $PATH.
2106 2134
2107 2135 This version does no checks on execute permissions or whether the
2108 2136 contents of $PATH are truly files (instead of directories or something
2109 2137 else). For such a safer (but slower) version, use %rehashx."""
2110 2138
2111 2139 # This function (and rehashx) manipulate the alias_table directly
2112 2140 # rather than calling magic_alias, for speed reasons. A rehash on a
2113 2141 # typical Linux box involves several thousand entries, so efficiency
2114 2142 # here is a top concern.
2115 2143
2116 2144 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2117 2145 alias_table = self.shell.alias_table
2118 2146 for pdir in path:
2119 2147 for ff in os.listdir(pdir):
2120 2148 # each entry in the alias table must be (N,name), where
2121 2149 # N is the number of positional arguments of the alias.
2122 2150 alias_table[ff] = (0,ff)
2123 2151 # Make sure the alias table doesn't contain keywords or builtins
2124 2152 self.shell.alias_table_validate()
2125 2153 # Call again init_auto_alias() so we get 'rm -i' and other modified
2126 2154 # aliases since %rehash will probably clobber them
2127 2155 self.shell.init_auto_alias()
2128 2156
2129 2157 def magic_rehashx(self, parameter_s = ''):
2130 2158 """Update the alias table with all executable files in $PATH.
2131 2159
2132 2160 This version explicitly checks that every entry in $PATH is a file
2133 2161 with execute access (os.X_OK), so it is much slower than %rehash.
2134 2162
2135 2163 Under Windows, it checks executability as a match agains a
2136 2164 '|'-separated string of extensions, stored in the IPython config
2137 2165 variable win_exec_ext. This defaults to 'exe|com|bat'. """
2138 2166
2139 2167 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2140 2168 alias_table = self.shell.alias_table
2141 2169
2142 2170 if os.name == 'posix':
2143 2171 isexec = lambda fname:os.path.isfile(fname) and \
2144 2172 os.access(fname,os.X_OK)
2145 2173 else:
2146 2174
2147 2175 try:
2148 2176 winext = os.environ['pathext'].replace(';','|').replace('.','')
2149 2177 except KeyError:
2150 2178 winext = 'exe|com|bat'
2151 2179
2152 2180 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2153 2181 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2154 2182 savedir = os.getcwd()
2155 2183 try:
2156 2184 # write the whole loop for posix/Windows so we don't have an if in
2157 2185 # the innermost part
2158 2186 if os.name == 'posix':
2159 2187 for pdir in path:
2160 2188 os.chdir(pdir)
2161 2189 for ff in os.listdir(pdir):
2162 2190 if isexec(ff):
2163 2191 # each entry in the alias table must be (N,name),
2164 2192 # where N is the number of positional arguments of the
2165 2193 # alias.
2166 2194 alias_table[ff] = (0,ff)
2167 2195 else:
2168 2196 for pdir in path:
2169 2197 os.chdir(pdir)
2170 2198 for ff in os.listdir(pdir):
2171 2199 if isexec(ff):
2172 2200 alias_table[execre.sub(r'\1',ff)] = (0,ff)
2173 2201 # Make sure the alias table doesn't contain keywords or builtins
2174 2202 self.shell.alias_table_validate()
2175 2203 # Call again init_auto_alias() so we get 'rm -i' and other
2176 2204 # modified aliases since %rehashx will probably clobber them
2177 2205 self.shell.init_auto_alias()
2178 2206 finally:
2179 2207 os.chdir(savedir)
2180 2208
2181 2209 def magic_pwd(self, parameter_s = ''):
2182 2210 """Return the current working directory path."""
2183 2211 return os.getcwd()
2184 2212
2185 2213 def magic_cd(self, parameter_s=''):
2186 2214 """Change the current working directory.
2187 2215
2188 2216 This command automatically maintains an internal list of directories
2189 2217 you visit during your IPython session, in the variable _dh. The
2190 2218 command %dhist shows this history nicely formatted.
2191 2219
2192 2220 Usage:
2193 2221
2194 2222 cd 'dir': changes to directory 'dir'.
2195 2223
2196 2224 cd -: changes to the last visited directory.
2197 2225
2198 2226 cd -<n>: changes to the n-th directory in the directory history.
2199 2227
2200 2228 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2201 2229 (note: cd <bookmark_name> is enough if there is no
2202 2230 directory <bookmark_name>, but a bookmark with the name exists.)
2203 2231
2204 2232 Options:
2205 2233
2206 2234 -q: quiet. Do not print the working directory after the cd command is
2207 2235 executed. By default IPython's cd command does print this directory,
2208 2236 since the default prompts do not display path information.
2209 2237
2210 2238 Note that !cd doesn't work for this purpose because the shell where
2211 2239 !command runs is immediately discarded after executing 'command'."""
2212 2240
2213 2241 parameter_s = parameter_s.strip()
2214 2242 bkms = self.shell.persist.get("bookmarks",{})
2215 2243
2216 2244 numcd = re.match(r'(-)(\d+)$',parameter_s)
2217 2245 # jump in directory history by number
2218 2246 if numcd:
2219 2247 nn = int(numcd.group(2))
2220 2248 try:
2221 2249 ps = self.shell.user_ns['_dh'][nn]
2222 2250 except IndexError:
2223 2251 print 'The requested directory does not exist in history.'
2224 2252 return
2225 2253 else:
2226 2254 opts = {}
2227 2255 else:
2228 2256 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2229 2257 # jump to previous
2230 2258 if ps == '-':
2231 2259 try:
2232 2260 ps = self.shell.user_ns['_dh'][-2]
2233 2261 except IndexError:
2234 2262 print 'No previous directory to change to.'
2235 2263 return
2236 2264 # jump to bookmark
2237 2265 elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)):
2238 2266 if bkms.has_key(ps):
2239 2267 target = bkms[ps]
2240 2268 print '(bookmark:%s) -> %s' % (ps,target)
2241 2269 ps = target
2242 2270 else:
2243 2271 if bkms:
2244 2272 error("Bookmark '%s' not found. "
2245 2273 "Use '%bookmark -l' to see your bookmarks." % ps)
2246 2274 else:
2247 2275 print "Bookmarks not set - use %bookmark <bookmarkname>"
2248 2276 return
2249 2277
2250 2278 # at this point ps should point to the target dir
2251 2279 if ps:
2252 2280 try:
2253 2281 os.chdir(os.path.expanduser(ps))
2254 2282 except OSError:
2255 2283 print sys.exc_info()[1]
2256 2284 else:
2257 2285 self.shell.user_ns['_dh'].append(os.getcwd())
2258 2286 else:
2259 2287 os.chdir(self.home_dir)
2260 2288 self.shell.user_ns['_dh'].append(os.getcwd())
2261 2289 if not 'q' in opts:
2262 2290 print self.shell.user_ns['_dh'][-1]
2263 2291
2264 2292 def magic_dhist(self, parameter_s=''):
2265 2293 """Print your history of visited directories.
2266 2294
2267 2295 %dhist -> print full history\\
2268 2296 %dhist n -> print last n entries only\\
2269 2297 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2270 2298
2271 2299 This history is automatically maintained by the %cd command, and
2272 2300 always available as the global list variable _dh. You can use %cd -<n>
2273 2301 to go to directory number <n>."""
2274 2302
2275 2303 dh = self.shell.user_ns['_dh']
2276 2304 if parameter_s:
2277 2305 try:
2278 2306 args = map(int,parameter_s.split())
2279 2307 except:
2280 2308 self.arg_err(Magic.magic_dhist)
2281 2309 return
2282 2310 if len(args) == 1:
2283 2311 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2284 2312 elif len(args) == 2:
2285 2313 ini,fin = args
2286 2314 else:
2287 2315 self.arg_err(Magic.magic_dhist)
2288 2316 return
2289 2317 else:
2290 2318 ini,fin = 0,len(dh)
2291 2319 nlprint(dh,
2292 2320 header = 'Directory history (kept in _dh)',
2293 2321 start=ini,stop=fin)
2294 2322
2295 2323 def magic_env(self, parameter_s=''):
2296 2324 """List environment variables."""
2297 2325
2298 2326 # environ is an instance of UserDict
2299 2327 return os.environ.data
2300 2328
2301 2329 def magic_pushd(self, parameter_s=''):
2302 2330 """Place the current dir on stack and change directory.
2303 2331
2304 2332 Usage:\\
2305 2333 %pushd ['dirname']
2306 2334
2307 2335 %pushd with no arguments does a %pushd to your home directory.
2308 2336 """
2309 2337 if parameter_s == '': parameter_s = '~'
2310 2338 if len(self.dir_stack)>0 and os.path.expanduser(parameter_s) != \
2311 2339 os.path.expanduser(self.dir_stack[0]):
2312 2340 try:
2313 2341 self.magic_cd(parameter_s)
2314 2342 self.dir_stack.insert(0,os.getcwd().replace(self.home_dir,'~'))
2315 2343 self.magic_dirs()
2316 2344 except:
2317 2345 print 'Invalid directory'
2318 2346 else:
2319 2347 print 'You are already there!'
2320 2348
2321 2349 def magic_popd(self, parameter_s=''):
2322 2350 """Change to directory popped off the top of the stack.
2323 2351 """
2324 2352 if len (self.dir_stack) > 1:
2325 2353 self.dir_stack.pop(0)
2326 2354 self.magic_cd(self.dir_stack[0])
2327 2355 print self.dir_stack[0]
2328 2356 else:
2329 2357 print "You can't remove the starting directory from the stack:",\
2330 2358 self.dir_stack
2331 2359
2332 2360 def magic_dirs(self, parameter_s=''):
2333 2361 """Return the current directory stack."""
2334 2362
2335 2363 return self.dir_stack[:]
2336 2364
2337 2365 def magic_sc(self, parameter_s=''):
2338 2366 """Shell capture - execute a shell command and capture its output.
2339 2367
2340 2368 %sc [options] varname=command
2341 2369
2342 2370 IPython will run the given command using commands.getoutput(), and
2343 2371 will then update the user's interactive namespace with a variable
2344 2372 called varname, containing the value of the call. Your command can
2345 2373 contain shell wildcards, pipes, etc.
2346 2374
2347 2375 The '=' sign in the syntax is mandatory, and the variable name you
2348 2376 supply must follow Python's standard conventions for valid names.
2349 2377
2350 2378 Options:
2351 2379
2352 2380 -l: list output. Split the output on newlines into a list before
2353 2381 assigning it to the given variable. By default the output is stored
2354 2382 as a single string.
2355 2383
2356 2384 -v: verbose. Print the contents of the variable.
2357 2385
2358 2386 In most cases you should not need to split as a list, because the
2359 2387 returned value is a special type of string which can automatically
2360 2388 provide its contents either as a list (split on newlines) or as a
2361 2389 space-separated string. These are convenient, respectively, either
2362 2390 for sequential processing or to be passed to a shell command.
2363 2391
2364 2392 For example:
2365 2393
2366 2394 # Capture into variable a
2367 2395 In [9]: sc a=ls *py
2368 2396
2369 2397 # a is a string with embedded newlines
2370 2398 In [10]: a
2371 2399 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2372 2400
2373 2401 # which can be seen as a list:
2374 2402 In [11]: a.l
2375 2403 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2376 2404
2377 2405 # or as a whitespace-separated string:
2378 2406 In [12]: a.s
2379 2407 Out[12]: 'setup.py win32_manual_post_install.py'
2380 2408
2381 2409 # a.s is useful to pass as a single command line:
2382 2410 In [13]: !wc -l $a.s
2383 2411 146 setup.py
2384 2412 130 win32_manual_post_install.py
2385 2413 276 total
2386 2414
2387 2415 # while the list form is useful to loop over:
2388 2416 In [14]: for f in a.l:
2389 2417 ....: !wc -l $f
2390 2418 ....:
2391 2419 146 setup.py
2392 2420 130 win32_manual_post_install.py
2393 2421
2394 2422 Similiarly, the lists returned by the -l option are also special, in
2395 2423 the sense that you can equally invoke the .s attribute on them to
2396 2424 automatically get a whitespace-separated string from their contents:
2397 2425
2398 2426 In [1]: sc -l b=ls *py
2399 2427
2400 2428 In [2]: b
2401 2429 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2402 2430
2403 2431 In [3]: b.s
2404 2432 Out[3]: 'setup.py win32_manual_post_install.py'
2405 2433
2406 2434 In summary, both the lists and strings used for ouptut capture have
2407 2435 the following special attributes:
2408 2436
2409 2437 .l (or .list) : value as list.
2410 2438 .n (or .nlstr): value as newline-separated string.
2411 2439 .s (or .spstr): value as space-separated string.
2412 2440 """
2413 2441
2414 2442 opts,args = self.parse_options(parameter_s,'lv')
2415 2443 # Try to get a variable name and command to run
2416 2444 try:
2417 2445 # the variable name must be obtained from the parse_options
2418 2446 # output, which uses shlex.split to strip options out.
2419 2447 var,_ = args.split('=',1)
2420 2448 var = var.strip()
2421 2449 # But the the command has to be extracted from the original input
2422 2450 # parameter_s, not on what parse_options returns, to avoid the
2423 2451 # quote stripping which shlex.split performs on it.
2424 2452 _,cmd = parameter_s.split('=',1)
2425 2453 except ValueError:
2426 2454 var,cmd = '',''
2427 2455 if not var:
2428 2456 error('you must specify a variable to assign the command to.')
2429 2457 return
2430 2458 # If all looks ok, proceed
2431 2459 out,err = self.shell.getoutputerror(cmd)
2432 2460 if err:
2433 2461 print >> Term.cerr,err
2434 2462 if opts.has_key('l'):
2435 2463 out = SList(out.split('\n'))
2436 2464 else:
2437 2465 out = LSString(out)
2438 2466 if opts.has_key('v'):
2439 2467 print '%s ==\n%s' % (var,pformat(out))
2440 2468 self.shell.user_ns.update({var:out})
2441 2469
2442 2470 def magic_sx(self, parameter_s=''):
2443 2471 """Shell execute - run a shell command and capture its output.
2444 2472
2445 2473 %sx command
2446 2474
2447 2475 IPython will run the given command using commands.getoutput(), and
2448 2476 return the result formatted as a list (split on '\\n'). Since the
2449 2477 output is _returned_, it will be stored in ipython's regular output
2450 2478 cache Out[N] and in the '_N' automatic variables.
2451 2479
2452 2480 Notes:
2453 2481
2454 2482 1) If an input line begins with '!!', then %sx is automatically
2455 2483 invoked. That is, while:
2456 2484 !ls
2457 2485 causes ipython to simply issue system('ls'), typing
2458 2486 !!ls
2459 2487 is a shorthand equivalent to:
2460 2488 %sx ls
2461 2489
2462 2490 2) %sx differs from %sc in that %sx automatically splits into a list,
2463 2491 like '%sc -l'. The reason for this is to make it as easy as possible
2464 2492 to process line-oriented shell output via further python commands.
2465 2493 %sc is meant to provide much finer control, but requires more
2466 2494 typing.
2467 2495
2468 2496 3) Just like %sc -l, this is a list with special attributes:
2469 2497
2470 2498 .l (or .list) : value as list.
2471 2499 .n (or .nlstr): value as newline-separated string.
2472 2500 .s (or .spstr): value as whitespace-separated string.
2473 2501
2474 2502 This is very useful when trying to use such lists as arguments to
2475 2503 system commands."""
2476 2504
2477 2505 if parameter_s:
2478 2506 out,err = self.shell.getoutputerror(parameter_s)
2479 2507 if err:
2480 2508 print >> Term.cerr,err
2481 2509 return SList(out.split('\n'))
2482 2510
2483 2511 def magic_bg(self, parameter_s=''):
2484 2512 """Run a job in the background, in a separate thread.
2485 2513
2486 2514 For example,
2487 2515
2488 2516 %bg myfunc(x,y,z=1)
2489 2517
2490 2518 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2491 2519 execution starts, a message will be printed indicating the job
2492 2520 number. If your job number is 5, you can use
2493 2521
2494 2522 myvar = jobs.result(5) or myvar = jobs[5].result
2495 2523
2496 2524 to assign this result to variable 'myvar'.
2497 2525
2498 2526 IPython has a job manager, accessible via the 'jobs' object. You can
2499 2527 type jobs? to get more information about it, and use jobs.<TAB> to see
2500 2528 its attributes. All attributes not starting with an underscore are
2501 2529 meant for public use.
2502 2530
2503 2531 In particular, look at the jobs.new() method, which is used to create
2504 2532 new jobs. This magic %bg function is just a convenience wrapper
2505 2533 around jobs.new(), for expression-based jobs. If you want to create a
2506 2534 new job with an explicit function object and arguments, you must call
2507 2535 jobs.new() directly.
2508 2536
2509 2537 The jobs.new docstring also describes in detail several important
2510 2538 caveats associated with a thread-based model for background job
2511 2539 execution. Type jobs.new? for details.
2512 2540
2513 2541 You can check the status of all jobs with jobs.status().
2514 2542
2515 2543 The jobs variable is set by IPython into the Python builtin namespace.
2516 2544 If you ever declare a variable named 'jobs', you will shadow this
2517 2545 name. You can either delete your global jobs variable to regain
2518 2546 access to the job manager, or make a new name and assign it manually
2519 2547 to the manager (stored in IPython's namespace). For example, to
2520 2548 assign the job manager to the Jobs name, use:
2521 2549
2522 2550 Jobs = __builtins__.jobs"""
2523 2551
2524 2552 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2525 2553
2526 2554 def magic_bookmark(self, parameter_s=''):
2527 2555 """Manage IPython's bookmark system.
2528 2556
2529 2557 %bookmark <name> - set bookmark to current dir
2530 2558 %bookmark <name> <dir> - set bookmark to <dir>
2531 2559 %bookmark -l - list all bookmarks
2532 2560 %bookmark -d <name> - remove bookmark
2533 2561 %bookmark -r - remove all bookmarks
2534 2562
2535 2563 You can later on access a bookmarked folder with:
2536 2564 %cd -b <name>
2537 2565 or simply '%cd <name>' if there is no directory called <name> AND
2538 2566 there is such a bookmark defined.
2539 2567
2540 2568 Your bookmarks persist through IPython sessions, but they are
2541 2569 associated with each profile."""
2542 2570
2543 2571 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2544 2572 if len(args) > 2:
2545 2573 error('You can only give at most two arguments')
2546 2574 return
2547 2575
2548 2576 bkms = self.shell.persist.get('bookmarks',{})
2549 2577
2550 2578 if opts.has_key('d'):
2551 2579 try:
2552 2580 todel = args[0]
2553 2581 except IndexError:
2554 2582 error('You must provide a bookmark to delete')
2555 2583 else:
2556 2584 try:
2557 2585 del bkms[todel]
2558 2586 except:
2559 2587 error("Can't delete bookmark '%s'" % todel)
2560 2588 elif opts.has_key('r'):
2561 2589 bkms = {}
2562 2590 elif opts.has_key('l'):
2563 2591 bks = bkms.keys()
2564 2592 bks.sort()
2565 2593 if bks:
2566 2594 size = max(map(len,bks))
2567 2595 else:
2568 2596 size = 0
2569 2597 fmt = '%-'+str(size)+'s -> %s'
2570 2598 print 'Current bookmarks:'
2571 2599 for bk in bks:
2572 2600 print fmt % (bk,bkms[bk])
2573 2601 else:
2574 2602 if not args:
2575 2603 error("You must specify the bookmark name")
2576 2604 elif len(args)==1:
2577 2605 bkms[args[0]] = os.getcwd()
2578 2606 elif len(args)==2:
2579 2607 bkms[args[0]] = args[1]
2580 2608 self.persist['bookmarks'] = bkms
2581 2609
2582 2610 def magic_pycat(self, parameter_s=''):
2583 2611 """Show a syntax-highlighted file through a pager.
2584 2612
2585 2613 This magic is similar to the cat utility, but it will assume the file
2586 2614 to be Python source and will show it with syntax highlighting. """
2587 2615
2588 2616 filename = get_py_filename(parameter_s)
2589 2617 page(self.shell.colorize(file_read(filename)),
2590 2618 screen_lines=self.shell.rc.screen_length)
2591 2619
2592 2620 # end Magic
@@ -1,913 +1,917 b''
1 1 # -*- coding: utf-8 -*-
2 2 """IPython Shell classes.
3 3
4 4 All the matplotlib support code was co-developed with John Hunter,
5 5 matplotlib's author.
6 6
7 $Id: Shell.py 964 2005-12-28 21:03:01Z fperez $"""
7 $Id: Shell.py 965 2005-12-28 23:23:09Z fperez $"""
8 8
9 9 #*****************************************************************************
10 10 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
11 11 #
12 12 # Distributed under the terms of the BSD License. The full license is in
13 13 # the file COPYING, distributed as part of this software.
14 14 #*****************************************************************************
15 15
16 16 from IPython import Release
17 17 __author__ = '%s <%s>' % Release.authors['Fernando']
18 18 __license__ = Release.license
19 19
20 20 # Code begins
21 21 import __main__
22 22 import __builtin__
23 23 import os
24 24 import sys
25 25 import signal
26 26 import threading
27 27
28 28 import IPython
29 29 from IPython import ultraTB
30 30 from IPython.genutils import Term,warn,error,flag_calls
31 31 from IPython.iplib import InteractiveShell
32 32 from IPython.ipmaker import make_IPython
33 33 from IPython.Magic import Magic
34 34 from IPython.Struct import Struct
35 35
36 36 # global flag to pass around information about Ctrl-C without exceptions
37 37 KBINT = False
38 38
39 39 # global flag to turn on/off Tk support.
40 40 USE_TK = False
41 41
42 42 #-----------------------------------------------------------------------------
43 43 # This class is trivial now, but I want to have it in to publish a clean
44 44 # interface. Later when the internals are reorganized, code that uses this
45 45 # shouldn't have to change.
46 46
47 47 class IPShell:
48 48 """Create an IPython instance."""
49 49
50 50 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
51 51 debug=1,shell_class=InteractiveShell):
52 52 self.IP = make_IPython(argv,user_ns=user_ns,user_global_ns=user_global_ns,
53 53 debug=debug,shell_class=shell_class)
54 54
55 55 def mainloop(self,sys_exit=0,banner=None):
56 56 self.IP.mainloop(banner)
57 57 if sys_exit:
58 58 sys.exit()
59 59
60 60 #-----------------------------------------------------------------------------
61 61 class IPShellEmbed:
62 62 """Allow embedding an IPython shell into a running program.
63 63
64 64 Instances of this class are callable, with the __call__ method being an
65 65 alias to the embed() method of an InteractiveShell instance.
66 66
67 67 Usage (see also the example-embed.py file for a running example):
68 68
69 69 ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override])
70 70
71 71 - argv: list containing valid command-line options for IPython, as they
72 72 would appear in sys.argv[1:].
73 73
74 74 For example, the following command-line options:
75 75
76 76 $ ipython -prompt_in1 'Input <\\#>' -colors LightBG
77 77
78 78 would be passed in the argv list as:
79 79
80 80 ['-prompt_in1','Input <\\#>','-colors','LightBG']
81 81
82 82 - banner: string which gets printed every time the interpreter starts.
83 83
84 84 - exit_msg: string which gets printed every time the interpreter exits.
85 85
86 86 - rc_override: a dict or Struct of configuration options such as those
87 87 used by IPython. These options are read from your ~/.ipython/ipythonrc
88 88 file when the Shell object is created. Passing an explicit rc_override
89 89 dict with any options you want allows you to override those values at
90 90 creation time without having to modify the file. This way you can create
91 91 embeddable instances configured in any way you want without editing any
92 92 global files (thus keeping your interactive IPython configuration
93 93 unchanged).
94 94
95 95 Then the ipshell instance can be called anywhere inside your code:
96 96
97 97 ipshell(header='') -> Opens up an IPython shell.
98 98
99 99 - header: string printed by the IPython shell upon startup. This can let
100 100 you know where in your code you are when dropping into the shell. Note
101 101 that 'banner' gets prepended to all calls, so header is used for
102 102 location-specific information.
103 103
104 104 For more details, see the __call__ method below.
105 105
106 106 When the IPython shell is exited with Ctrl-D, normal program execution
107 107 resumes.
108 108
109 109 This functionality was inspired by a posting on comp.lang.python by cmkl
110 110 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
111 111 by the IDL stop/continue commands."""
112 112
113 113 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None):
114 114 """Note that argv here is a string, NOT a list."""
115 115 self.set_banner(banner)
116 116 self.set_exit_msg(exit_msg)
117 117 self.set_dummy_mode(0)
118 118
119 119 # sys.displayhook is a global, we need to save the user's original
120 120 # Don't rely on __displayhook__, as the user may have changed that.
121 121 self.sys_displayhook_ori = sys.displayhook
122 122
123 123 # save readline completer status
124 124 try:
125 125 #print 'Save completer',sys.ipcompleter # dbg
126 126 self.sys_ipcompleter_ori = sys.ipcompleter
127 127 except:
128 128 pass # not nested with IPython
129 129
130 130 # FIXME. Passing user_ns breaks namespace handling.
131 131 #self.IP = make_IPython(argv,user_ns=__main__.__dict__)
132 132 self.IP = make_IPython(argv,rc_override=rc_override,embedded=True)
133 133
134 134 # copy our own displayhook also
135 135 self.sys_displayhook_embed = sys.displayhook
136 136 # and leave the system's display hook clean
137 137 sys.displayhook = self.sys_displayhook_ori
138 138 # don't use the ipython crash handler so that user exceptions aren't
139 139 # trapped
140 140 sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors,
141 141 mode = self.IP.rc.xmode,
142 142 call_pdb = self.IP.rc.pdb)
143 143 self.restore_system_completer()
144 144
145 145 def restore_system_completer(self):
146 146 """Restores the readline completer which was in place.
147 147
148 148 This allows embedded IPython within IPython not to disrupt the
149 149 parent's completion.
150 150 """
151 151
152 152 try:
153 153 self.IP.readline.set_completer(self.sys_ipcompleter_ori)
154 154 sys.ipcompleter = self.sys_ipcompleter_ori
155 155 except:
156 156 pass
157 157
158 158 def __call__(self,header='',local_ns=None,global_ns=None,dummy=None):
159 159 """Activate the interactive interpreter.
160 160
161 161 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
162 162 the interpreter shell with the given local and global namespaces, and
163 163 optionally print a header string at startup.
164 164
165 165 The shell can be globally activated/deactivated using the
166 166 set/get_dummy_mode methods. This allows you to turn off a shell used
167 167 for debugging globally.
168 168
169 169 However, *each* time you call the shell you can override the current
170 170 state of dummy_mode with the optional keyword parameter 'dummy'. For
171 171 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
172 172 can still have a specific call work by making it as IPShell(dummy=0).
173 173
174 174 The optional keyword parameter dummy controls whether the call
175 175 actually does anything. """
176 176
177 177 # Allow the dummy parameter to override the global __dummy_mode
178 178 if dummy or (dummy != 0 and self.__dummy_mode):
179 179 return
180 180
181 181 # Set global subsystems (display,completions) to our values
182 182 sys.displayhook = self.sys_displayhook_embed
183 183 if self.IP.has_readline:
184 184 self.IP.readline.set_completer(self.IP.Completer.complete)
185 185
186 186 if self.banner and header:
187 187 format = '%s\n%s\n'
188 188 else:
189 189 format = '%s%s\n'
190 190 banner = format % (self.banner,header)
191 191
192 192 # Call the embedding code with a stack depth of 1 so it can skip over
193 193 # our call and get the original caller's namespaces.
194 194 self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1)
195 195
196 196 if self.exit_msg:
197 197 print self.exit_msg
198 198
199 199 # Restore global systems (display, completion)
200 200 sys.displayhook = self.sys_displayhook_ori
201 201 self.restore_system_completer()
202 202
203 203 def set_dummy_mode(self,dummy):
204 204 """Sets the embeddable shell's dummy mode parameter.
205 205
206 206 set_dummy_mode(dummy): dummy = 0 or 1.
207 207
208 208 This parameter is persistent and makes calls to the embeddable shell
209 209 silently return without performing any action. This allows you to
210 210 globally activate or deactivate a shell you're using with a single call.
211 211
212 212 If you need to manually"""
213 213
214 214 if dummy not in [0,1,False,True]:
215 215 raise ValueError,'dummy parameter must be boolean'
216 216 self.__dummy_mode = dummy
217 217
218 218 def get_dummy_mode(self):
219 219 """Return the current value of the dummy mode parameter.
220 220 """
221 221 return self.__dummy_mode
222 222
223 223 def set_banner(self,banner):
224 224 """Sets the global banner.
225 225
226 226 This banner gets prepended to every header printed when the shell
227 227 instance is called."""
228 228
229 229 self.banner = banner
230 230
231 231 def set_exit_msg(self,exit_msg):
232 232 """Sets the global exit_msg.
233 233
234 234 This exit message gets printed upon exiting every time the embedded
235 235 shell is called. It is None by default. """
236 236
237 237 self.exit_msg = exit_msg
238 238
239 239 #-----------------------------------------------------------------------------
240 240 def sigint_handler (signum,stack_frame):
241 241 """Sigint handler for threaded apps.
242 242
243 243 This is a horrible hack to pass information about SIGINT _without_ using
244 244 exceptions, since I haven't been able to properly manage cross-thread
245 245 exceptions in GTK/WX. In fact, I don't think it can be done (or at least
246 246 that's my understanding from a c.l.py thread where this was discussed)."""
247 247
248 248 global KBINT
249 249
250 250 print '\nKeyboardInterrupt - Press <Enter> to continue.',
251 251 Term.cout.flush()
252 252 # Set global flag so that runsource can know that Ctrl-C was hit
253 253 KBINT = True
254 254
255 255 class MTInteractiveShell(InteractiveShell):
256 256 """Simple multi-threaded shell."""
257 257
258 258 # Threading strategy taken from:
259 259 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian
260 260 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
261 261 # from the pygtk mailing list, to avoid lockups with system calls.
262 262
263 # class attribute to indicate whether the class supports threads or not.
264 # Subclasses with thread support should override this as needed.
265 isthreaded = True
266
263 267 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
264 268 user_ns=None,user_global_ns=None,banner2='',**kw):
265 269 """Similar to the normal InteractiveShell, but with threading control"""
266 270
267 271 InteractiveShell.__init__(self,name,usage,rc,user_ns,
268 272 user_global_ns,banner2)
269 273
270 274 # Locking control variable
271 275 self.thread_ready = threading.Condition()
272 276
273 277 # Stuff to do at closing time
274 278 self._kill = False
275 279 on_kill = kw.get('on_kill')
276 280 if on_kill is None:
277 281 on_kill = []
278 282 # Check that all things to kill are callable:
279 283 for t in on_kill:
280 284 if not callable(t):
281 285 raise TypeError,'on_kill must be a list of callables'
282 286 self.on_kill = on_kill
283 287
284 288 def runsource(self, source, filename="<input>", symbol="single"):
285 289 """Compile and run some source in the interpreter.
286 290
287 291 Modified version of code.py's runsource(), to handle threading issues.
288 292 See the original for full docstring details."""
289 293
290 294 global KBINT
291 295
292 296 # If Ctrl-C was typed, we reset the flag and return right away
293 297 if KBINT:
294 298 KBINT = False
295 299 return False
296 300
297 301 try:
298 302 code = self.compile(source, filename, symbol)
299 303 except (OverflowError, SyntaxError, ValueError):
300 304 # Case 1
301 305 self.showsyntaxerror(filename)
302 306 return False
303 307
304 308 if code is None:
305 309 # Case 2
306 310 return True
307 311
308 312 # Case 3
309 313 # Store code in self, so the execution thread can handle it
310 314 self.thread_ready.acquire()
311 315 self.code_to_run = code
312 316 self.thread_ready.wait() # Wait until processed in timeout interval
313 317 self.thread_ready.release()
314 318
315 319 return False
316 320
317 321 def runcode(self):
318 322 """Execute a code object.
319 323
320 324 Multithreaded wrapper around IPython's runcode()."""
321 325
322 326 # lock thread-protected stuff
323 327 self.thread_ready.acquire()
324 328
325 329 # Install sigint handler
326 330 try:
327 331 signal.signal(signal.SIGINT, sigint_handler)
328 332 except SystemError:
329 333 # This happens under Windows, which seems to have all sorts
330 334 # of problems with signal handling. Oh well...
331 335 pass
332 336
333 337 if self._kill:
334 338 print >>Term.cout, 'Closing threads...',
335 339 Term.cout.flush()
336 340 for tokill in self.on_kill:
337 341 tokill()
338 342 print >>Term.cout, 'Done.'
339 343
340 344 # Run pending code by calling parent class
341 345 if self.code_to_run is not None:
342 346 self.thread_ready.notify()
343 347 InteractiveShell.runcode(self,self.code_to_run)
344 348
345 349 # We're done with thread-protected variables
346 350 self.thread_ready.release()
347 351 # This MUST return true for gtk threading to work
348 352 return True
349 353
350 354 def kill (self):
351 355 """Kill the thread, returning when it has been shut down."""
352 356 self.thread_ready.acquire()
353 357 self._kill = True
354 358 self.thread_ready.release()
355 359
356 360 class MatplotlibShellBase:
357 361 """Mixin class to provide the necessary modifications to regular IPython
358 362 shell classes for matplotlib support.
359 363
360 364 Given Python's MRO, this should be used as the FIRST class in the
361 365 inheritance hierarchy, so that it overrides the relevant methods."""
362 366
363 367 def _matplotlib_config(self,name):
364 368 """Return various items needed to setup the user's shell with matplotlib"""
365 369
366 370 # Initialize matplotlib to interactive mode always
367 371 import matplotlib
368 372 from matplotlib import backends
369 373 matplotlib.interactive(True)
370 374
371 375 def use(arg):
372 376 """IPython wrapper for matplotlib's backend switcher.
373 377
374 378 In interactive use, we can not allow switching to a different
375 379 interactive backend, since thread conflicts will most likely crash
376 380 the python interpreter. This routine does a safety check first,
377 381 and refuses to perform a dangerous switch. It still allows
378 382 switching to non-interactive backends."""
379 383
380 384 if arg in backends.interactive_bk and arg != self.mpl_backend:
381 385 m=('invalid matplotlib backend switch.\n'
382 386 'This script attempted to switch to the interactive '
383 387 'backend: `%s`\n'
384 388 'Your current choice of interactive backend is: `%s`\n\n'
385 389 'Switching interactive matplotlib backends at runtime\n'
386 390 'would crash the python interpreter, '
387 391 'and IPython has blocked it.\n\n'
388 392 'You need to either change your choice of matplotlib backend\n'
389 393 'by editing your .matplotlibrc file, or run this script as a \n'
390 394 'standalone file from the command line, not using IPython.\n' %
391 395 (arg,self.mpl_backend) )
392 396 raise RuntimeError, m
393 397 else:
394 398 self.mpl_use(arg)
395 399 self.mpl_use._called = True
396 400
397 401 self.matplotlib = matplotlib
398 402 self.mpl_backend = matplotlib.rcParams['backend']
399 403
400 404 # we also need to block switching of interactive backends by use()
401 405 self.mpl_use = matplotlib.use
402 406 self.mpl_use._called = False
403 407 # overwrite the original matplotlib.use with our wrapper
404 408 matplotlib.use = use
405 409
406 410
407 411 # This must be imported last in the matplotlib series, after
408 412 # backend/interactivity choices have been made
409 413 try:
410 414 import matplotlib.pylab as pylab
411 415 self.pylab = pylab
412 416 self.pylab_name = 'pylab'
413 417 except ImportError:
414 418 import matplotlib.matlab as matlab
415 419 self.pylab = matlab
416 420 self.pylab_name = 'matlab'
417 421
418 422 self.pylab.show._needmain = False
419 423 # We need to detect at runtime whether show() is called by the user.
420 424 # For this, we wrap it into a decorator which adds a 'called' flag.
421 425 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
422 426
423 427 # Build a user namespace initialized with matplotlib/matlab features.
424 428 user_ns = {'__name__':'__main__',
425 429 '__builtins__' : __builtin__ }
426 430
427 431 # Be careful not to remove the final \n in the code string below, or
428 432 # things will break badly with py22 (I think it's a python bug, 2.3 is
429 433 # OK).
430 434 pname = self.pylab_name # Python can't interpolate dotted var names
431 435 exec ("import matplotlib\n"
432 436 "import matplotlib.%(pname)s as %(pname)s\n"
433 437 "from matplotlib.%(pname)s import *\n" % locals()) in user_ns
434 438
435 439 # Build matplotlib info banner
436 440 b="""
437 441 Welcome to pylab, a matplotlib-based Python environment.
438 442 For more information, type 'help(pylab)'.
439 443 """
440 444 return user_ns,b
441 445
442 446 def mplot_exec(self,fname,*where,**kw):
443 447 """Execute a matplotlib script.
444 448
445 449 This is a call to execfile(), but wrapped in safeties to properly
446 450 handle interactive rendering and backend switching."""
447 451
448 452 #print '*** Matplotlib runner ***' # dbg
449 453 # turn off rendering until end of script
450 454 isInteractive = self.matplotlib.rcParams['interactive']
451 455 self.matplotlib.interactive(False)
452 456 self.safe_execfile(fname,*where,**kw)
453 457 self.matplotlib.interactive(isInteractive)
454 458 # make rendering call now, if the user tried to do it
455 459 if self.pylab.draw_if_interactive.called:
456 460 self.pylab.draw()
457 461 self.pylab.draw_if_interactive.called = False
458 462
459 463 # if a backend switch was performed, reverse it now
460 464 if self.mpl_use._called:
461 465 self.matplotlib.rcParams['backend'] = self.mpl_backend
462 466
463 467 def magic_run(self,parameter_s=''):
464 468 Magic.magic_run(self,parameter_s,runner=self.mplot_exec)
465 469
466 470 # Fix the docstring so users see the original as well
467 471 magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__,
468 472 "\n *** Modified %run for Matplotlib,"
469 473 " with proper interactive handling ***")
470 474
471 475 # Now we provide 2 versions of a matplotlib-aware IPython base shells, single
472 476 # and multithreaded. Note that these are meant for internal use, the IPShell*
473 477 # classes below are the ones meant for public consumption.
474 478
475 479 class MatplotlibShell(MatplotlibShellBase,InteractiveShell):
476 480 """Single-threaded shell with matplotlib support."""
477 481
478 482 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
479 483 user_ns=None,user_global_ns=None,**kw):
480 484 user_ns,b2 = self._matplotlib_config(name)
481 485 InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
482 486 banner2=b2,**kw)
483 487
484 488 class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell):
485 489 """Multi-threaded shell with matplotlib support."""
486 490
487 491 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
488 492 user_ns=None,user_global_ns=None, **kw):
489 493 user_ns,b2 = self._matplotlib_config(name)
490 494 MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
491 495 banner2=b2,**kw)
492 496
493 497 #-----------------------------------------------------------------------------
494 498 # Utility functions for the different GUI enabled IPShell* classes.
495 499
496 500 def get_tk():
497 501 """Tries to import Tkinter and returns a withdrawn Tkinter root
498 502 window. If Tkinter is already imported or not available, this
499 503 returns None. This function calls `hijack_tk` underneath.
500 504 """
501 505 if not USE_TK or sys.modules.has_key('Tkinter'):
502 506 return None
503 507 else:
504 508 try:
505 509 import Tkinter
506 510 except ImportError:
507 511 return None
508 512 else:
509 513 hijack_tk()
510 514 r = Tkinter.Tk()
511 515 r.withdraw()
512 516 return r
513 517
514 518 def hijack_tk():
515 519 """Modifies Tkinter's mainloop with a dummy so when a module calls
516 520 mainloop, it does not block.
517 521
518 522 """
519 523 def misc_mainloop(self, n=0):
520 524 pass
521 525 def tkinter_mainloop(n=0):
522 526 pass
523 527
524 528 import Tkinter
525 529 Tkinter.Misc.mainloop = misc_mainloop
526 530 Tkinter.mainloop = tkinter_mainloop
527 531
528 532 def update_tk(tk):
529 533 """Updates the Tkinter event loop. This is typically called from
530 534 the respective WX or GTK mainloops.
531 535 """
532 536 if tk:
533 537 tk.update()
534 538
535 539 def hijack_wx():
536 540 """Modifies wxPython's MainLoop with a dummy so user code does not
537 541 block IPython. The hijacked mainloop function is returned.
538 542 """
539 543 def dummy_mainloop(*args, **kw):
540 544 pass
541 545 import wxPython
542 546 ver = wxPython.__version__
543 547 orig_mainloop = None
544 548 if ver[:3] >= '2.5':
545 549 import wx
546 550 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
547 551 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
548 552 else: raise AttributeError('Could not find wx core module')
549 553 orig_mainloop = core.PyApp_MainLoop
550 554 core.PyApp_MainLoop = dummy_mainloop
551 555 elif ver[:3] == '2.4':
552 556 orig_mainloop = wxPython.wxc.wxPyApp_MainLoop
553 557 wxPython.wxc.wxPyApp_MainLoop = dummy_mainloop
554 558 else:
555 559 warn("Unable to find either wxPython version 2.4 or >= 2.5.")
556 560 return orig_mainloop
557 561
558 562 def hijack_gtk():
559 563 """Modifies pyGTK's mainloop with a dummy so user code does not
560 564 block IPython. This function returns the original `gtk.mainloop`
561 565 function that has been hijacked.
562 566 """
563 567 def dummy_mainloop(*args, **kw):
564 568 pass
565 569 import gtk
566 570 if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main
567 571 else: orig_mainloop = gtk.mainloop
568 572 gtk.mainloop = dummy_mainloop
569 573 gtk.main = dummy_mainloop
570 574 return orig_mainloop
571 575
572 576 #-----------------------------------------------------------------------------
573 577 # The IPShell* classes below are the ones meant to be run by external code as
574 578 # IPython instances. Note that unless a specific threading strategy is
575 579 # desired, the factory function start() below should be used instead (it
576 580 # selects the proper threaded class).
577 581
578 582 class IPShellGTK(threading.Thread):
579 583 """Run a gtk mainloop() in a separate thread.
580 584
581 585 Python commands can be passed to the thread where they will be executed.
582 586 This is implemented by periodically checking for passed code using a
583 587 GTK timeout callback."""
584 588
585 589 TIMEOUT = 100 # Millisecond interval between timeouts.
586 590
587 591 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
588 592 debug=1,shell_class=MTInteractiveShell):
589 593
590 594 import gtk
591 595
592 596 self.gtk = gtk
593 597 self.gtk_mainloop = hijack_gtk()
594 598
595 599 # Allows us to use both Tk and GTK.
596 600 self.tk = get_tk()
597 601
598 602 if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit
599 603 else: mainquit = self.gtk.mainquit
600 604
601 605 self.IP = make_IPython(argv,user_ns=user_ns,
602 606 user_global_ns=user_global_ns,
603 607 debug=debug,
604 608 shell_class=shell_class,
605 609 on_kill=[mainquit])
606 610
607 611 # HACK: slot for banner in self; it will be passed to the mainloop
608 612 # method only and .run() needs it. The actual value will be set by
609 613 # .mainloop().
610 614 self._banner = None
611 615
612 616 threading.Thread.__init__(self)
613 617
614 618 def run(self):
615 619 self.IP.mainloop(self._banner)
616 620 self.IP.kill()
617 621
618 622 def mainloop(self,sys_exit=0,banner=None):
619 623
620 624 self._banner = banner
621 625
622 626 if self.gtk.pygtk_version >= (2,4,0):
623 627 import gobject
624 628 gobject.idle_add(self.on_timer)
625 629 else:
626 630 self.gtk.idle_add(self.on_timer)
627 631
628 632 if sys.platform != 'win32':
629 633 try:
630 634 if self.gtk.gtk_version[0] >= 2:
631 635 self.gtk.threads_init()
632 636 except AttributeError:
633 637 pass
634 638 except RuntimeError:
635 639 error('Your pyGTK likely has not been compiled with '
636 640 'threading support.\n'
637 641 'The exception printout is below.\n'
638 642 'You can either rebuild pyGTK with threads, or '
639 643 'try using \n'
640 644 'matplotlib with a different backend (like Tk or WX).\n'
641 645 'Note that matplotlib will most likely not work in its '
642 646 'current state!')
643 647 self.IP.InteractiveTB()
644 648 self.start()
645 649 self.gtk.threads_enter()
646 650 self.gtk_mainloop()
647 651 self.gtk.threads_leave()
648 652 self.join()
649 653
650 654 def on_timer(self):
651 655 update_tk(self.tk)
652 656 return self.IP.runcode()
653 657
654 658
655 659 class IPShellWX(threading.Thread):
656 660 """Run a wx mainloop() in a separate thread.
657 661
658 662 Python commands can be passed to the thread where they will be executed.
659 663 This is implemented by periodically checking for passed code using a
660 664 GTK timeout callback."""
661 665
662 666 TIMEOUT = 100 # Millisecond interval between timeouts.
663 667
664 668 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
665 669 debug=1,shell_class=MTInteractiveShell):
666 670
667 671 import wxPython.wx as wx
668 672
669 673 threading.Thread.__init__(self)
670 674 self.wx = wx
671 675 self.wx_mainloop = hijack_wx()
672 676
673 677 # Allows us to use both Tk and GTK.
674 678 self.tk = get_tk()
675 679
676 680 self.IP = make_IPython(argv,user_ns=user_ns,
677 681 user_global_ns=user_global_ns,
678 682 debug=debug,
679 683 shell_class=shell_class,
680 684 on_kill=[self.wxexit])
681 685 # HACK: slot for banner in self; it will be passed to the mainloop
682 686 # method only and .run() needs it. The actual value will be set by
683 687 # .mainloop().
684 688 self._banner = None
685 689
686 690 self.app = None
687 691
688 692 def wxexit(self, *args):
689 693 if self.app is not None:
690 694 self.app.agent.timer.Stop()
691 695 self.app.ExitMainLoop()
692 696
693 697 def run(self):
694 698 self.IP.mainloop(self._banner)
695 699 self.IP.kill()
696 700
697 701 def mainloop(self,sys_exit=0,banner=None):
698 702
699 703 self._banner = banner
700 704
701 705 self.start()
702 706
703 707 class TimerAgent(self.wx.wxMiniFrame):
704 708 wx = self.wx
705 709 IP = self.IP
706 710 tk = self.tk
707 711 def __init__(self, parent, interval):
708 712 style = self.wx.wxDEFAULT_FRAME_STYLE | self.wx.wxTINY_CAPTION_HORIZ
709 713 self.wx.wxMiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200),
710 714 size=(100, 100),style=style)
711 715 self.Show(False)
712 716 self.interval = interval
713 717 self.timerId = self.wx.wxNewId()
714 718
715 719 def StartWork(self):
716 720 self.timer = self.wx.wxTimer(self, self.timerId)
717 721 self.wx.EVT_TIMER(self, self.timerId, self.OnTimer)
718 722 self.timer.Start(self.interval)
719 723
720 724 def OnTimer(self, event):
721 725 update_tk(self.tk)
722 726 self.IP.runcode()
723 727
724 728 class App(self.wx.wxApp):
725 729 wx = self.wx
726 730 TIMEOUT = self.TIMEOUT
727 731 def OnInit(self):
728 732 'Create the main window and insert the custom frame'
729 733 self.agent = TimerAgent(None, self.TIMEOUT)
730 734 self.agent.Show(self.wx.false)
731 735 self.agent.StartWork()
732 736 return self.wx.true
733 737
734 738 self.app = App(redirect=False)
735 739 self.wx_mainloop(self.app)
736 740 self.join()
737 741
738 742
739 743 class IPShellQt(threading.Thread):
740 744 """Run a Qt event loop in a separate thread.
741 745
742 746 Python commands can be passed to the thread where they will be executed.
743 747 This is implemented by periodically checking for passed code using a
744 748 Qt timer / slot."""
745 749
746 750 TIMEOUT = 100 # Millisecond interval between timeouts.
747 751
748 752 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
749 753 debug=0,shell_class=MTInteractiveShell):
750 754
751 755 import qt
752 756
753 757 class newQApplication:
754 758 def __init__( self ):
755 759 self.QApplication = qt.QApplication
756 760
757 761 def __call__( *args, **kwargs ):
758 762 return qt.qApp
759 763
760 764 def exec_loop( *args, **kwargs ):
761 765 pass
762 766
763 767 def __getattr__( self, name ):
764 768 return getattr( self.QApplication, name )
765 769
766 770 qt.QApplication = newQApplication()
767 771
768 772 # Allows us to use both Tk and QT.
769 773 self.tk = get_tk()
770 774
771 775 self.IP = make_IPython(argv,user_ns=user_ns,
772 776 user_global_ns=user_global_ns,
773 777 debug=debug,
774 778 shell_class=shell_class,
775 779 on_kill=[qt.qApp.exit])
776 780
777 781 # HACK: slot for banner in self; it will be passed to the mainloop
778 782 # method only and .run() needs it. The actual value will be set by
779 783 # .mainloop().
780 784 self._banner = None
781 785
782 786 threading.Thread.__init__(self)
783 787
784 788 def run(self):
785 789 self.IP.mainloop(self._banner)
786 790 self.IP.kill()
787 791
788 792 def mainloop(self,sys_exit=0,banner=None):
789 793
790 794 import qt
791 795
792 796 self._banner = banner
793 797
794 798 if qt.QApplication.startingUp():
795 799 a = qt.QApplication.QApplication(sys.argv)
796 800 self.timer = qt.QTimer()
797 801 qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer )
798 802
799 803 self.start()
800 804 self.timer.start( self.TIMEOUT, True )
801 805 while True:
802 806 if self.IP._kill: break
803 807 qt.qApp.exec_loop()
804 808 self.join()
805 809
806 810 def on_timer(self):
807 811 update_tk(self.tk)
808 812 result = self.IP.runcode()
809 813 self.timer.start( self.TIMEOUT, True )
810 814 return result
811 815
812 816 # A set of matplotlib public IPython shell classes, for single-threaded
813 817 # (Tk* and FLTK* backends) and multithreaded (GTK* and WX* backends) use.
814 818 class IPShellMatplotlib(IPShell):
815 819 """Subclass IPShell with MatplotlibShell as the internal shell.
816 820
817 821 Single-threaded class, meant for the Tk* and FLTK* backends.
818 822
819 823 Having this on a separate class simplifies the external driver code."""
820 824
821 825 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
822 826 IPShell.__init__(self,argv,user_ns,user_global_ns,debug,
823 827 shell_class=MatplotlibShell)
824 828
825 829 class IPShellMatplotlibGTK(IPShellGTK):
826 830 """Subclass IPShellGTK with MatplotlibMTShell as the internal shell.
827 831
828 832 Multi-threaded class, meant for the GTK* backends."""
829 833
830 834 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
831 835 IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug,
832 836 shell_class=MatplotlibMTShell)
833 837
834 838 class IPShellMatplotlibWX(IPShellWX):
835 839 """Subclass IPShellWX with MatplotlibMTShell as the internal shell.
836 840
837 841 Multi-threaded class, meant for the WX* backends."""
838 842
839 843 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
840 844 IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug,
841 845 shell_class=MatplotlibMTShell)
842 846
843 847 class IPShellMatplotlibQt(IPShellQt):
844 848 """Subclass IPShellQt with MatplotlibMTShell as the internal shell.
845 849
846 850 Multi-threaded class, meant for the Qt* backends."""
847 851
848 852 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
849 853 IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug,
850 854 shell_class=MatplotlibMTShell)
851 855
852 856 #-----------------------------------------------------------------------------
853 857 # Factory functions to actually start the proper thread-aware shell
854 858
855 859 def _matplotlib_shell_class():
856 860 """Factory function to handle shell class selection for matplotlib.
857 861
858 862 The proper shell class to use depends on the matplotlib backend, since
859 863 each backend requires a different threading strategy."""
860 864
861 865 try:
862 866 import matplotlib
863 867 except ImportError:
864 868 error('matplotlib could NOT be imported! Starting normal IPython.')
865 869 sh_class = IPShell
866 870 else:
867 871 backend = matplotlib.rcParams['backend']
868 872 if backend.startswith('GTK'):
869 873 sh_class = IPShellMatplotlibGTK
870 874 elif backend.startswith('WX'):
871 875 sh_class = IPShellMatplotlibWX
872 876 elif backend.startswith('Qt'):
873 877 sh_class = IPShellMatplotlibQt
874 878 else:
875 879 sh_class = IPShellMatplotlib
876 880 #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg
877 881 return sh_class
878 882
879 883 # This is the one which should be called by external code.
880 884 def start():
881 885 """Return a running shell instance, dealing with threading options.
882 886
883 887 This is a factory function which will instantiate the proper IPython shell
884 888 based on the user's threading choice. Such a selector is needed because
885 889 different GUI toolkits require different thread handling details."""
886 890
887 891 global USE_TK
888 892 # Crude sys.argv hack to extract the threading options.
889 893 argv = sys.argv
890 894 if len(argv) > 1:
891 895 if len(argv) > 2:
892 896 arg2 = argv[2]
893 897 if arg2.endswith('-tk'):
894 898 USE_TK = True
895 899 arg1 = argv[1]
896 900 if arg1.endswith('-gthread'):
897 901 shell = IPShellGTK
898 902 elif arg1.endswith( '-qthread' ):
899 903 shell = IPShellQt
900 904 elif arg1.endswith('-wthread'):
901 905 shell = IPShellWX
902 906 elif arg1.endswith('-pylab'):
903 907 shell = _matplotlib_shell_class()
904 908 else:
905 909 shell = IPShell
906 910 else:
907 911 shell = IPShell
908 912 return shell()
909 913
910 914 # Some aliases for backwards compatibility
911 915 IPythonShell = IPShell
912 916 IPythonShellEmbed = IPShellEmbed
913 917 #************************ End of file <Shell.py> ***************************
@@ -1,1968 +1,1997 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 IPython -- An enhanced Interactive Python
4 4
5 5 Requires Python 2.1 or newer.
6 6
7 7 This file contains all the classes and helper functions specific to IPython.
8 8
9 $Id: iplib.py 964 2005-12-28 21:03:01Z fperez $
9 $Id: iplib.py 965 2005-12-28 23:23:09Z fperez $
10 10 """
11 11
12 12 #*****************************************************************************
13 13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
14 14 # Copyright (C) 2001-2005 Fernando Perez. <fperez@colorado.edu>
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #
19 19 # Note: this code originally subclassed code.InteractiveConsole from the
20 20 # Python standard library. Over time, all of that class has been copied
21 21 # verbatim here for modifications which could not be accomplished by
22 22 # subclassing. At this point, there are no dependencies at all on the code
23 23 # module anymore (it is not even imported). The Python License (sec. 2)
24 24 # allows for this, but it's always nice to acknowledge credit where credit is
25 25 # due.
26 26 #*****************************************************************************
27 27
28 28 #****************************************************************************
29 29 # Modules and globals
30 30
31 31 from __future__ import generators # for 2.2 backwards-compatibility
32 32
33 33 from IPython import Release
34 34 __author__ = '%s <%s>\n%s <%s>' % \
35 35 ( Release.authors['Janko'] + Release.authors['Fernando'] )
36 36 __license__ = Release.license
37 37 __version__ = Release.version
38 38
39 39 # Python standard modules
40 40 import __main__
41 41 import __builtin__
42 42 import StringIO
43 43 import bdb
44 44 import cPickle as pickle
45 45 import codeop
46 46 import exceptions
47 47 import glob
48 48 import inspect
49 49 import keyword
50 50 import new
51 51 import os
52 52 import pdb
53 53 import pydoc
54 54 import re
55 55 import shutil
56 56 import string
57 57 import sys
58 58 import traceback
59 59 import types
60 60
61 61 from pprint import pprint, pformat
62 62
63 63 # IPython's own modules
64 64 import IPython
65 65 from IPython import OInspect,PyColorize,ultraTB
66 66 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
67 67 from IPython.FakeModule import FakeModule
68 68 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
69 69 from IPython.Logger import Logger
70 70 from IPython.Magic import Magic,magic2python
71 71 from IPython.Struct import Struct
72 72 from IPython.background_jobs import BackgroundJobManager
73 73 from IPython.usage import cmd_line_usage,interactive_usage
74 74 from IPython.genutils import *
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 #****************************************************************************
81 81 # Some utility function definitions
82 82
83 83 # This can be replaced with an isspace() call once we drop 2.2 compatibility
84 84 _isspace_match = re.compile(r'^\s+$').match
85 85 def isspace(s):
86 86 return bool(_isspace_match(s))
87 87
88 88 def esc_quotes(strng):
89 89 """Return the input string with single and double quotes escaped out"""
90 90
91 91 return strng.replace('"','\\"').replace("'","\\'")
92 92
93 93 def import_fail_info(mod_name,fns=None):
94 94 """Inform load failure for a module."""
95 95
96 96 if fns == None:
97 97 warn("Loading of %s failed.\n" % (mod_name,))
98 98 else:
99 99 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
100 100
101 101 def qw_lol(indata):
102 102 """qw_lol('a b') -> [['a','b']],
103 103 otherwise it's just a call to qw().
104 104
105 105 We need this to make sure the modules_some keys *always* end up as a
106 106 list of lists."""
107 107
108 108 if type(indata) in StringTypes:
109 109 return [qw(indata)]
110 110 else:
111 111 return qw(indata)
112 112
113 113 def ipmagic(arg_s):
114 114 """Call a magic function by name.
115 115
116 116 Input: a string containing the name of the magic function to call and any
117 117 additional arguments to be passed to the magic.
118 118
119 119 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
120 120 prompt:
121 121
122 122 In[1]: %name -opt foo bar
123 123
124 124 To call a magic without arguments, simply use ipmagic('name').
125 125
126 126 This provides a proper Python function to call IPython's magics in any
127 127 valid Python code you can type at the interpreter, including loops and
128 128 compound statements. It is added by IPython to the Python builtin
129 129 namespace upon initialization."""
130 130
131 131 args = arg_s.split(' ',1)
132 132 magic_name = args[0]
133 133 if magic_name.startswith(__IPYTHON__.ESC_MAGIC):
134 134 magic_name = magic_name[1:]
135 135 try:
136 136 magic_args = args[1]
137 137 except IndexError:
138 138 magic_args = ''
139 139 fn = getattr(__IPYTHON__,'magic_'+magic_name,None)
140 140 if fn is None:
141 141 error("Magic function `%s` not found." % magic_name)
142 142 else:
143 143 magic_args = __IPYTHON__.var_expand(magic_args)
144 144 return fn(magic_args)
145 145
146 146 def ipalias(arg_s):
147 147 """Call an alias by name.
148 148
149 149 Input: a string containing the name of the alias to call and any
150 150 additional arguments to be passed to the magic.
151 151
152 152 ipalias('name -opt foo bar') is equivalent to typing at the ipython
153 153 prompt:
154 154
155 155 In[1]: name -opt foo bar
156 156
157 157 To call an alias without arguments, simply use ipalias('name').
158 158
159 159 This provides a proper Python function to call IPython's aliases in any
160 160 valid Python code you can type at the interpreter, including loops and
161 161 compound statements. It is added by IPython to the Python builtin
162 162 namespace upon initialization."""
163 163
164 164 args = arg_s.split(' ',1)
165 165 alias_name = args[0]
166 166 try:
167 167 alias_args = args[1]
168 168 except IndexError:
169 169 alias_args = ''
170 170 if alias_name in __IPYTHON__.alias_table:
171 171 __IPYTHON__.call_alias(alias_name,alias_args)
172 172 else:
173 173 error("Alias `%s` not found." % alias_name)
174 174
175 175 def softspace(file, newvalue):
176 176 """Copied from code.py, to remove the dependency"""
177 177 oldvalue = 0
178 178 try:
179 179 oldvalue = file.softspace
180 180 except AttributeError:
181 181 pass
182 182 try:
183 183 file.softspace = newvalue
184 184 except (AttributeError, TypeError):
185 185 # "attribute-less object" or "read-only attributes"
186 186 pass
187 187 return oldvalue
188 188
189 189
190 190 #****************************************************************************
191 191 # Local use exceptions
192 192 class SpaceInInput(exceptions.Exception): pass
193 193
194 194 #****************************************************************************
195 195 # Local use classes
196 196 class Bunch: pass
197 197
198 198 class InputList(list):
199 199 """Class to store user input.
200 200
201 201 It's basically a list, but slices return a string instead of a list, thus
202 202 allowing things like (assuming 'In' is an instance):
203 203
204 204 exec In[4:7]
205 205
206 206 or
207 207
208 208 exec In[5:9] + In[14] + In[21:25]"""
209 209
210 210 def __getslice__(self,i,j):
211 211 return ''.join(list.__getslice__(self,i,j))
212 212
213 213 class SyntaxTB(ultraTB.ListTB):
214 214 """Extension which holds some state: the last exception value"""
215 215
216 216 def __init__(self,color_scheme = 'NoColor'):
217 217 ultraTB.ListTB.__init__(self,color_scheme)
218 218 self.last_syntax_error = None
219 219
220 220 def __call__(self, etype, value, elist):
221 221 self.last_syntax_error = value
222 222 ultraTB.ListTB.__call__(self,etype,value,elist)
223 223
224 224 def clear_err_state(self):
225 225 """Return the current error state and clear it"""
226 226 e = self.last_syntax_error
227 227 self.last_syntax_error = None
228 228 return e
229 229
230 230 #****************************************************************************
231 231 # Main IPython class
232 232 class InteractiveShell(Logger, Magic):
233 233 """An enhanced console for Python."""
234 234
235 # class attribute to indicate whether the class supports threads or not.
236 # Subclasses with thread support should override this as needed.
237 isthreaded = False
238
235 239 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
236 240 user_ns = None,user_global_ns=None,banner2='',
237 241 custom_exceptions=((),None),embedded=False):
238 242
239 243 # some minimal strict typechecks. For some core data structures, I
240 244 # want actual basic python types, not just anything that looks like
241 245 # one. This is especially true for namespaces.
242 246 for ns in (user_ns,user_global_ns):
243 247 if ns is not None and type(ns) != types.DictType:
244 248 raise TypeError,'namespace must be a dictionary'
245 249
246 250 # Put a reference to self in builtins so that any form of embedded or
247 251 # imported code can test for being inside IPython.
248 252 __builtin__.__IPYTHON__ = self
249 253
250 254 # And load into builtins ipmagic/ipalias as well
251 255 __builtin__.ipmagic = ipmagic
252 256 __builtin__.ipalias = ipalias
253 257
254 258 # Add to __builtin__ other parts of IPython's public API
255 259 __builtin__.ip_set_hook = self.set_hook
256 260
257 261 # Keep in the builtins a flag for when IPython is active. We set it
258 262 # with setdefault so that multiple nested IPythons don't clobber one
259 263 # another. Each will increase its value by one upon being activated,
260 264 # which also gives us a way to determine the nesting level.
261 265 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
262 266
263 267 # Do the intuitively correct thing for quit/exit: we remove the
264 268 # builtins if they exist, and our own prefilter routine will handle
265 269 # these special cases
266 270 try:
267 271 del __builtin__.exit, __builtin__.quit
268 272 except AttributeError:
269 273 pass
270 274
275 # Store the actual shell's name
276 self.name = name
277
271 278 # We need to know whether the instance is meant for embedding, since
272 279 # global/local namespaces need to be handled differently in that case
273 280 self.embedded = embedded
274 281
275 282 # command compiler
276 283 self.compile = codeop.CommandCompiler()
277 284
278 285 # User input buffer
279 286 self.buffer = []
280 287
281 288 # Default name given in compilation of code
282 289 self.filename = '<ipython console>'
283 290
284 291 # Create the namespace where the user will operate. user_ns is
285 292 # normally the only one used, and it is passed to the exec calls as
286 293 # the locals argument. But we do carry a user_global_ns namespace
287 294 # given as the exec 'globals' argument, This is useful in embedding
288 295 # situations where the ipython shell opens in a context where the
289 296 # distinction between locals and globals is meaningful.
290 297
291 298 # FIXME. For some strange reason, __builtins__ is showing up at user
292 299 # level as a dict instead of a module. This is a manual fix, but I
293 300 # should really track down where the problem is coming from. Alex
294 301 # Schmolck reported this problem first.
295 302
296 303 # A useful post by Alex Martelli on this topic:
297 304 # Re: inconsistent value from __builtins__
298 305 # Von: Alex Martelli <aleaxit@yahoo.com>
299 306 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
300 307 # Gruppen: comp.lang.python
301 308 # Referenzen: 1
302 309
303 310 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
304 311 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
305 312 # > <type 'dict'>
306 313 # > >>> print type(__builtins__)
307 314 # > <type 'module'>
308 315 # > Is this difference in return value intentional?
309 316
310 317 # Well, it's documented that '__builtins__' can be either a dictionary
311 318 # or a module, and it's been that way for a long time. Whether it's
312 319 # intentional (or sensible), I don't know. In any case, the idea is
313 320 # that if you need to access the built-in namespace directly, you
314 321 # should start with "import __builtin__" (note, no 's') which will
315 322 # definitely give you a module. Yeah, it's somewhat confusing:-(.
316 323
317 324 if user_ns is None:
318 325 # Set __name__ to __main__ to better match the behavior of the
319 326 # normal interpreter.
320 327 user_ns = {'__name__' :'__main__',
321 328 '__builtins__' : __builtin__,
322 329 }
323 330
324 331 if user_global_ns is None:
325 332 user_global_ns = {}
326 333
327 334 # Assign namespaces
328 335 # This is the namespace where all normal user variables live
329 336 self.user_ns = user_ns
330 337 # Embedded instances require a separate namespace for globals.
331 338 # Normally this one is unused by non-embedded instances.
332 339 self.user_global_ns = user_global_ns
333 340 # A namespace to keep track of internal data structures to prevent
334 341 # them from cluttering user-visible stuff. Will be updated later
335 342 self.internal_ns = {}
336 343
337 344 # Namespace of system aliases. Each entry in the alias
338 345 # table must be a 2-tuple of the form (N,name), where N is the number
339 346 # of positional arguments of the alias.
340 347 self.alias_table = {}
341 348
342 349 # A table holding all the namespaces IPython deals with, so that
343 350 # introspection facilities can search easily.
344 351 self.ns_table = {'user':user_ns,
345 352 'user_global':user_global_ns,
346 353 'alias':self.alias_table,
347 354 'internal':self.internal_ns,
348 355 'builtin':__builtin__.__dict__
349 356 }
350 357
351 358 # The user namespace MUST have a pointer to the shell itself.
352 359 self.user_ns[name] = self
353 360
354 361 # We need to insert into sys.modules something that looks like a
355 362 # module but which accesses the IPython namespace, for shelve and
356 363 # pickle to work interactively. Normally they rely on getting
357 364 # everything out of __main__, but for embedding purposes each IPython
358 365 # instance has its own private namespace, so we can't go shoving
359 366 # everything into __main__.
360 367
361 368 # note, however, that we should only do this for non-embedded
362 369 # ipythons, which really mimic the __main__.__dict__ with their own
363 370 # namespace. Embedded instances, on the other hand, should not do
364 371 # this because they need to manage the user local/global namespaces
365 372 # only, but they live within a 'normal' __main__ (meaning, they
366 373 # shouldn't overtake the execution environment of the script they're
367 374 # embedded in).
368 375
369 376 if not embedded:
370 377 try:
371 378 main_name = self.user_ns['__name__']
372 379 except KeyError:
373 380 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
374 381 else:
375 382 #print "pickle hack in place" # dbg
376 383 sys.modules[main_name] = FakeModule(self.user_ns)
377 384
378 385 # List of input with multi-line handling.
379 386 # Fill its zero entry, user counter starts at 1
380 387 self.input_hist = InputList(['\n'])
381 388
382 389 # list of visited directories
383 390 try:
384 391 self.dir_hist = [os.getcwd()]
385 392 except IOError, e:
386 393 self.dir_hist = []
387 394
388 395 # dict of output history
389 396 self.output_hist = {}
390 397
391 398 # dict of things NOT to alias (keywords, builtins and some magics)
392 399 no_alias = {}
393 400 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
394 401 for key in keyword.kwlist + no_alias_magics:
395 402 no_alias[key] = 1
396 403 no_alias.update(__builtin__.__dict__)
397 404 self.no_alias = no_alias
398 405
399 406 # make global variables for user access to these
400 407 self.user_ns['_ih'] = self.input_hist
401 408 self.user_ns['_oh'] = self.output_hist
402 409 self.user_ns['_dh'] = self.dir_hist
403 410
404 411 # user aliases to input and output histories
405 412 self.user_ns['In'] = self.input_hist
406 413 self.user_ns['Out'] = self.output_hist
407 414
408 # Store the actual shell's name
409 self.name = name
410
411 415 # Object variable to store code object waiting execution. This is
412 416 # used mainly by the multithreaded shells, but it can come in handy in
413 417 # other situations. No need to use a Queue here, since it's a single
414 418 # item which gets cleared once run.
415 419 self.code_to_run = None
416 420
417 421 # Job manager (for jobs run as background threads)
418 422 self.jobs = BackgroundJobManager()
419 423 # Put the job manager into builtins so it's always there.
420 424 __builtin__.jobs = self.jobs
421 425
422 426 # escapes for automatic behavior on the command line
423 427 self.ESC_SHELL = '!'
424 428 self.ESC_HELP = '?'
425 429 self.ESC_MAGIC = '%'
426 430 self.ESC_QUOTE = ','
427 431 self.ESC_QUOTE2 = ';'
428 432 self.ESC_PAREN = '/'
429 433
430 434 # And their associated handlers
431 435 self.esc_handlers = {self.ESC_PAREN:self.handle_auto,
432 436 self.ESC_QUOTE:self.handle_auto,
433 437 self.ESC_QUOTE2:self.handle_auto,
434 438 self.ESC_MAGIC:self.handle_magic,
435 439 self.ESC_HELP:self.handle_help,
436 440 self.ESC_SHELL:self.handle_shell_escape,
437 441 }
438 442
439 443 # class initializations
440 444 Logger.__init__(self,log_ns = self.user_ns)
441 445 Magic.__init__(self,self)
442 446
443 447 # an ugly hack to get a pointer to the shell, so I can start writing
444 448 # magic code via this pointer instead of the current mixin salad.
445 449 Magic.set_shell(self,self)
446 450
447 451 # Python source parser/formatter for syntax highlighting
448 452 pyformat = PyColorize.Parser().format
449 453 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
450 454
451 455 # hooks holds pointers used for user-side customizations
452 456 self.hooks = Struct()
453 457
454 458 # Set all default hooks, defined in the IPython.hooks module.
455 459 hooks = IPython.hooks
456 460 for hook_name in hooks.__all__:
457 461 self.set_hook(hook_name,getattr(hooks,hook_name))
458 462
459 463 # Flag to mark unconditional exit
460 464 self.exit_now = False
461 465
462 466 self.usage_min = """\
463 467 An enhanced console for Python.
464 468 Some of its features are:
465 469 - Readline support if the readline library is present.
466 470 - Tab completion in the local namespace.
467 471 - Logging of input, see command-line options.
468 472 - System shell escape via ! , eg !ls.
469 473 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
470 474 - Keeps track of locally defined variables via %who, %whos.
471 475 - Show object information with a ? eg ?x or x? (use ?? for more info).
472 476 """
473 477 if usage: self.usage = usage
474 478 else: self.usage = self.usage_min
475 479
476 480 # Storage
477 481 self.rc = rc # This will hold all configuration information
478 482 self.inputcache = []
479 483 self._boundcache = []
480 484 self.pager = 'less'
481 485 # temporary files used for various purposes. Deleted at exit.
482 486 self.tempfiles = []
483 487
484 488 # Keep track of readline usage (later set by init_readline)
485 489 self.has_readline = False
486 490
487 491 # for pushd/popd management
488 492 try:
489 493 self.home_dir = get_home_dir()
490 494 except HomeDirError,msg:
491 495 fatal(msg)
492 496
493 497 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
494 498
495 499 # Functions to call the underlying shell.
496 500
497 501 # utility to expand user variables via Itpl
498 502 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
499 503 self.user_ns))
500 504 # The first is similar to os.system, but it doesn't return a value,
501 505 # and it allows interpolation of variables in the user's namespace.
502 506 self.system = lambda cmd: shell(self.var_expand(cmd),
503 507 header='IPython system call: ',
504 508 verbose=self.rc.system_verbose)
505 509 # These are for getoutput and getoutputerror:
506 510 self.getoutput = lambda cmd: \
507 511 getoutput(self.var_expand(cmd),
508 512 header='IPython system call: ',
509 513 verbose=self.rc.system_verbose)
510 514 self.getoutputerror = lambda cmd: \
511 515 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
512 516 self.user_ns)),
513 517 header='IPython system call: ',
514 518 verbose=self.rc.system_verbose)
515 519
516 520 # RegExp for splitting line contents into pre-char//first
517 521 # word-method//rest. For clarity, each group in on one line.
518 522
519 523 # WARNING: update the regexp if the above escapes are changed, as they
520 524 # are hardwired in.
521 525
522 526 # Don't get carried away with trying to make the autocalling catch too
523 527 # much: it's better to be conservative rather than to trigger hidden
524 528 # evals() somewhere and end up causing side effects.
525 529
526 530 self.line_split = re.compile(r'^([\s*,;/])'
527 531 r'([\?\w\.]+\w*\s*)'
528 532 r'(\(?.*$)')
529 533
530 534 # Original re, keep around for a while in case changes break something
531 535 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
532 536 # r'(\s*[\?\w\.]+\w*\s*)'
533 537 # r'(\(?.*$)')
534 538
535 539 # RegExp to identify potential function names
536 540 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
537 541 # RegExp to exclude strings with this start from autocalling
538 542 self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ')
539 543
540 544 # try to catch also methods for stuff in lists/tuples/dicts: off
541 545 # (experimental). For this to work, the line_split regexp would need
542 546 # to be modified so it wouldn't break things at '['. That line is
543 547 # nasty enough that I shouldn't change it until I can test it _well_.
544 548 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
545 549
546 550 # keep track of where we started running (mainly for crash post-mortem)
547 551 self.starting_dir = os.getcwd()
548 552
549 553 # Attributes for Logger mixin class, make defaults here
550 554 self._dolog = False
551 555 self.LOG = ''
552 556 self.LOGDEF = '.InteractiveShell.log'
553 557 self.LOGMODE = 'over'
554 558 self.LOGHEAD = Itpl(
555 559 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
556 560 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
557 561 #log# opts = $self.rc.opts
558 562 #log# args = $self.rc.args
559 563 #log# It is safe to make manual edits below here.
560 564 #log#-----------------------------------------------------------------------
561 565 """)
562 566 # Various switches which can be set
563 567 self.CACHELENGTH = 5000 # this is cheap, it's just text
564 568 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
565 569 self.banner2 = banner2
566 570
567 571 # TraceBack handlers:
568 # Need two, one for syntax errors and one for other exceptions.
572
573 # Syntax error handler.
569 574 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
575
570 576 # The interactive one is initialized with an offset, meaning we always
571 577 # want to remove the topmost item in the traceback, which is our own
572 578 # internal code. Valid modes: ['Plain','Context','Verbose']
573 579 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
574 580 color_scheme='NoColor',
575 581 tb_offset = 1)
582
583 # IPython itself shouldn't crash. This will produce a detailed
584 # post-mortem if it does. But we only install the crash handler for
585 # non-threaded shells, the threaded ones use a normal verbose reporter
586 # and lose the crash handler. This is because exceptions in the main
587 # thread (such as in GUI code) propagate directly to sys.excepthook,
588 # and there's no point in printing crash dumps for every user exception.
589 if self.isthreaded:
590 sys.excepthook = ultraTB.FormattedTB()
591 else:
592 from IPython import CrashHandler
593 sys.excepthook = CrashHandler.CrashHandler(self)
594
595 # The instance will store a pointer to this, so that runtime code
596 # (such as magics) can access it. This is because during the
597 # read-eval loop, it gets temporarily overwritten (to deal with GUI
598 # frameworks).
599 self.sys_excepthook = sys.excepthook
600
576 601 # and add any custom exception handlers the user may have specified
577 602 self.set_custom_exc(*custom_exceptions)
578 603
579 604 # Object inspector
580 605 self.inspector = OInspect.Inspector(OInspect.InspectColors,
581 606 PyColorize.ANSICodeColors,
582 607 'NoColor')
583 608 # indentation management
584 609 self.autoindent = False
585 610 self.indent_current_nsp = 0
586 611 self.indent_current = '' # actual indent string
587 612
588 613 # Make some aliases automatically
589 614 # Prepare list of shell aliases to auto-define
590 615 if os.name == 'posix':
591 616 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
592 617 'mv mv -i','rm rm -i','cp cp -i',
593 618 'cat cat','less less','clear clear',
594 619 # a better ls
595 620 'ls ls -F',
596 621 # long ls
597 622 'll ls -lF',
598 623 # color ls
599 624 'lc ls -F -o --color',
600 625 # ls normal files only
601 626 'lf ls -F -o --color %l | grep ^-',
602 627 # ls symbolic links
603 628 'lk ls -F -o --color %l | grep ^l',
604 629 # directories or links to directories,
605 630 'ldir ls -F -o --color %l | grep /$',
606 631 # things which are executable
607 632 'lx ls -F -o --color %l | grep ^-..x',
608 633 )
609 634 elif os.name in ['nt','dos']:
610 635 auto_alias = ('dir dir /on', 'ls dir /on',
611 636 'ddir dir /ad /on', 'ldir dir /ad /on',
612 637 'mkdir mkdir','rmdir rmdir','echo echo',
613 638 'ren ren','cls cls','copy copy')
614 639 else:
615 640 auto_alias = ()
616 641 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
617 642 # Call the actual (public) initializer
618 643 self.init_auto_alias()
619 644 # end __init__
620 645
646 def post_config_initialization(self):
647 """Post configuration init method
648
649 This is called after the configuration files have been processed to
650 'finalize' the initialization."""
651
652 rc = self.rc
653
654 # Load readline proper
655 if rc.readline:
656 self.init_readline()
657
658 # Set user colors (don't do it in the constructor above so that it
659 # doesn't crash if colors option is invalid)
660 self.magic_colors(rc.colors)
661
662 # Load user aliases
663 for alias in rc.alias:
664 self.magic_alias(alias)
665
666 # dynamic data that survives through sessions
667 # XXX make the filename a config option?
668 persist_base = 'persist'
669 if rc.profile:
670 persist_base += '_%s' % rc.profile
671 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
672
673 try:
674 self.persist = pickle.load(file(self.persist_fname))
675 except:
676 self.persist = {}
677
621 678 def set_hook(self,name,hook):
622 679 """set_hook(name,hook) -> sets an internal IPython hook.
623 680
624 681 IPython exposes some of its internal API as user-modifiable hooks. By
625 682 resetting one of these hooks, you can modify IPython's behavior to
626 683 call at runtime your own routines."""
627 684
628 685 # At some point in the future, this should validate the hook before it
629 686 # accepts it. Probably at least check that the hook takes the number
630 687 # of args it's supposed to.
631 688 setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
632 689
633 690 def set_custom_exc(self,exc_tuple,handler):
634 691 """set_custom_exc(exc_tuple,handler)
635 692
636 693 Set a custom exception handler, which will be called if any of the
637 694 exceptions in exc_tuple occur in the mainloop (specifically, in the
638 695 runcode() method.
639 696
640 697 Inputs:
641 698
642 699 - exc_tuple: a *tuple* of valid exceptions to call the defined
643 700 handler for. It is very important that you use a tuple, and NOT A
644 701 LIST here, because of the way Python's except statement works. If
645 702 you only want to trap a single exception, use a singleton tuple:
646 703
647 704 exc_tuple == (MyCustomException,)
648 705
649 706 - handler: this must be defined as a function with the following
650 707 basic interface: def my_handler(self,etype,value,tb).
651 708
652 709 This will be made into an instance method (via new.instancemethod)
653 710 of IPython itself, and it will be called if any of the exceptions
654 711 listed in the exc_tuple are caught. If the handler is None, an
655 712 internal basic one is used, which just prints basic info.
656 713
657 714 WARNING: by putting in your own exception handler into IPython's main
658 715 execution loop, you run a very good chance of nasty crashes. This
659 716 facility should only be used if you really know what you are doing."""
660 717
661 718 assert type(exc_tuple)==type(()) , \
662 719 "The custom exceptions must be given AS A TUPLE."
663 720
664 721 def dummy_handler(self,etype,value,tb):
665 722 print '*** Simple custom exception handler ***'
666 723 print 'Exception type :',etype
667 724 print 'Exception value:',value
668 725 print 'Traceback :',tb
669 726 print 'Source code :','\n'.join(self.buffer)
670 727
671 728 if handler is None: handler = dummy_handler
672 729
673 730 self.CustomTB = new.instancemethod(handler,self,self.__class__)
674 731 self.custom_exceptions = exc_tuple
675 732
676 733 def set_custom_completer(self,completer,pos=0):
677 734 """set_custom_completer(completer,pos=0)
678 735
679 736 Adds a new custom completer function.
680 737
681 738 The position argument (defaults to 0) is the index in the completers
682 739 list where you want the completer to be inserted."""
683 740
684 741 newcomp = new.instancemethod(completer,self.Completer,
685 742 self.Completer.__class__)
686 743 self.Completer.matchers.insert(pos,newcomp)
687 744
688 745 def complete(self,text):
689 746 """Return a sorted list of all possible completions on text.
690 747
691 748 Inputs:
692 749
693 750 - text: a string of text to be completed on.
694 751
695 752 This is a wrapper around the completion mechanism, similar to what
696 753 readline does at the command line when the TAB key is hit. By
697 754 exposing it as a method, it can be used by other non-readline
698 755 environments (such as GUIs) for text completion.
699 756
700 757 Simple usage example:
701 758
702 759 In [1]: x = 'hello'
703 760
704 761 In [2]: __IP.complete('x.l')
705 762 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
706 763
707 764 complete = self.Completer.complete
708 765 state = 0
709 766 # use a dict so we get unique keys, since ipyhton's multiple
710 767 # completers can return duplicates.
711 768 comps = {}
712 769 while True:
713 770 newcomp = complete(text,state)
714 771 if newcomp is None:
715 772 break
716 773 comps[newcomp] = 1
717 774 state += 1
718 775 outcomps = comps.keys()
719 776 outcomps.sort()
720 777 return outcomps
721 778
722 779 def set_completer_frame(self, frame):
723 780 if frame:
724 781 self.Completer.namespace = frame.f_locals
725 782 self.Completer.global_namespace = frame.f_globals
726 783 else:
727 784 self.Completer.namespace = self.user_ns
728 785 self.Completer.global_namespace = self.user_global_ns
729 786
730 def post_config_initialization(self):
731 """Post configuration init method
732
733 This is called after the configuration files have been processed to
734 'finalize' the initialization."""
735
736 rc = self.rc
737
738 # Load readline proper
739 if rc.readline:
740 self.init_readline()
741
742 # Set user colors (don't do it in the constructor above so that it
743 # doesn't crash if colors option is invalid)
744 self.magic_colors(rc.colors)
745
746 # Load user aliases
747 for alias in rc.alias:
748 self.magic_alias(alias)
749
750 # dynamic data that survives through sessions
751 # XXX make the filename a config option?
752 persist_base = 'persist'
753 if rc.profile:
754 persist_base += '_%s' % rc.profile
755 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
756
757 try:
758 self.persist = pickle.load(file(self.persist_fname))
759 except:
760 self.persist = {}
761
762 787 def init_auto_alias(self):
763 788 """Define some aliases automatically.
764 789
765 790 These are ALL parameter-less aliases"""
766 791 for alias,cmd in self.auto_alias:
767 792 self.alias_table[alias] = (0,cmd)
768 793
769 794 def alias_table_validate(self,verbose=0):
770 795 """Update information about the alias table.
771 796
772 797 In particular, make sure no Python keywords/builtins are in it."""
773 798
774 799 no_alias = self.no_alias
775 800 for k in self.alias_table.keys():
776 801 if k in no_alias:
777 802 del self.alias_table[k]
778 803 if verbose:
779 804 print ("Deleting alias <%s>, it's a Python "
780 805 "keyword or builtin." % k)
781 806
782 807 def set_autoindent(self,value=None):
783 808 """Set the autoindent flag, checking for readline support.
784 809
785 810 If called with no arguments, it acts as a toggle."""
786 811
787 812 if not self.has_readline:
788 813 if os.name == 'posix':
789 814 warn("The auto-indent feature requires the readline library")
790 815 self.autoindent = 0
791 816 return
792 817 if value is None:
793 818 self.autoindent = not self.autoindent
794 819 else:
795 820 self.autoindent = value
796 821
797 822 def rc_set_toggle(self,rc_field,value=None):
798 823 """Set or toggle a field in IPython's rc config. structure.
799 824
800 825 If called with no arguments, it acts as a toggle.
801 826
802 827 If called with a non-existent field, the resulting AttributeError
803 828 exception will propagate out."""
804 829
805 830 rc_val = getattr(self.rc,rc_field)
806 831 if value is None:
807 832 value = not rc_val
808 833 setattr(self.rc,rc_field,value)
809 834
810 835 def user_setup(self,ipythondir,rc_suffix,mode='install'):
811 836 """Install the user configuration directory.
812 837
813 838 Can be called when running for the first time or to upgrade the user's
814 839 .ipython/ directory with the mode parameter. Valid modes are 'install'
815 840 and 'upgrade'."""
816 841
817 842 def wait():
818 843 try:
819 844 raw_input("Please press <RETURN> to start IPython.")
820 845 except EOFError:
821 846 print >> Term.cout
822 847 print '*'*70
823 848
824 849 cwd = os.getcwd() # remember where we started
825 850 glb = glob.glob
826 851 print '*'*70
827 852 if mode == 'install':
828 853 print \
829 854 """Welcome to IPython. I will try to create a personal configuration directory
830 855 where you can customize many aspects of IPython's functionality in:\n"""
831 856 else:
832 857 print 'I am going to upgrade your configuration in:'
833 858
834 859 print ipythondir
835 860
836 861 rcdirend = os.path.join('IPython','UserConfig')
837 862 cfg = lambda d: os.path.join(d,rcdirend)
838 863 try:
839 864 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
840 865 except IOError:
841 866 warning = """
842 867 Installation error. IPython's directory was not found.
843 868
844 869 Check the following:
845 870
846 871 The ipython/IPython directory should be in a directory belonging to your
847 872 PYTHONPATH environment variable (that is, it should be in a directory
848 873 belonging to sys.path). You can copy it explicitly there or just link to it.
849 874
850 875 IPython will proceed with builtin defaults.
851 876 """
852 877 warn(warning)
853 878 wait()
854 879 return
855 880
856 881 if mode == 'install':
857 882 try:
858 883 shutil.copytree(rcdir,ipythondir)
859 884 os.chdir(ipythondir)
860 885 rc_files = glb("ipythonrc*")
861 886 for rc_file in rc_files:
862 887 os.rename(rc_file,rc_file+rc_suffix)
863 888 except:
864 889 warning = """
865 890
866 891 There was a problem with the installation:
867 892 %s
868 893 Try to correct it or contact the developers if you think it's a bug.
869 894 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
870 895 warn(warning)
871 896 wait()
872 897 return
873 898
874 899 elif mode == 'upgrade':
875 900 try:
876 901 os.chdir(ipythondir)
877 902 except:
878 903 print """
879 904 Can not upgrade: changing to directory %s failed. Details:
880 905 %s
881 906 """ % (ipythondir,sys.exc_info()[1])
882 907 wait()
883 908 return
884 909 else:
885 910 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
886 911 for new_full_path in sources:
887 912 new_filename = os.path.basename(new_full_path)
888 913 if new_filename.startswith('ipythonrc'):
889 914 new_filename = new_filename + rc_suffix
890 915 # The config directory should only contain files, skip any
891 916 # directories which may be there (like CVS)
892 917 if os.path.isdir(new_full_path):
893 918 continue
894 919 if os.path.exists(new_filename):
895 920 old_file = new_filename+'.old'
896 921 if os.path.exists(old_file):
897 922 os.remove(old_file)
898 923 os.rename(new_filename,old_file)
899 924 shutil.copy(new_full_path,new_filename)
900 925 else:
901 926 raise ValueError,'unrecognized mode for install:',`mode`
902 927
903 928 # Fix line-endings to those native to each platform in the config
904 929 # directory.
905 930 try:
906 931 os.chdir(ipythondir)
907 932 except:
908 933 print """
909 934 Problem: changing to directory %s failed.
910 935 Details:
911 936 %s
912 937
913 938 Some configuration files may have incorrect line endings. This should not
914 939 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
915 940 wait()
916 941 else:
917 942 for fname in glb('ipythonrc*'):
918 943 try:
919 944 native_line_ends(fname,backup=0)
920 945 except IOError:
921 946 pass
922 947
923 948 if mode == 'install':
924 949 print """
925 950 Successful installation!
926 951
927 952 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
928 953 IPython manual (there are both HTML and PDF versions supplied with the
929 954 distribution) to make sure that your system environment is properly configured
930 955 to take advantage of IPython's features."""
931 956 else:
932 957 print """
933 958 Successful upgrade!
934 959
935 960 All files in your directory:
936 961 %(ipythondir)s
937 962 which would have been overwritten by the upgrade were backed up with a .old
938 963 extension. If you had made particular customizations in those files you may
939 964 want to merge them back into the new files.""" % locals()
940 965 wait()
941 966 os.chdir(cwd)
942 967 # end user_setup()
943 968
944 969 def atexit_operations(self):
945 970 """This will be executed at the time of exit.
946 971
947 972 Saving of persistent data should be performed here. """
948 973
949 974 # input history
950 975 self.savehist()
951 976
952 977 # Cleanup all tempfiles left around
953 978 for tfile in self.tempfiles:
954 979 try:
955 980 os.unlink(tfile)
956 981 except OSError:
957 982 pass
958 983
959 984 # save the "persistent data" catch-all dictionary
960 985 try:
961 986 pickle.dump(self.persist, open(self.persist_fname,"w"))
962 987 except:
963 988 print "*** ERROR *** persistent data saving failed."
964 989
965 990 def savehist(self):
966 991 """Save input history to a file (via readline library)."""
967 992 try:
968 993 self.readline.write_history_file(self.histfile)
969 994 except:
970 995 print 'Unable to save IPython command history to file: ' + \
971 996 `self.histfile`
972 997
973 998 def pre_readline(self):
974 999 """readline hook to be used at the start of each line.
975 1000
976 1001 Currently it handles auto-indent only."""
977 1002
978 1003 self.readline.insert_text(self.indent_current)
979 1004
980 1005 def init_readline(self):
981 1006 """Command history completion/saving/reloading."""
982 1007 try:
983 1008 import readline
984 1009 except ImportError:
985 1010 self.has_readline = 0
986 1011 self.readline = None
987 1012 # no point in bugging windows users with this every time:
988 1013 if os.name == 'posix':
989 1014 warn('Readline services not available on this platform.')
990 1015 else:
991 1016 import atexit
992 1017 from IPython.completer import IPCompleter
993 1018 self.Completer = IPCompleter(self,
994 1019 self.user_ns,
995 1020 self.user_global_ns,
996 1021 self.rc.readline_omit__names,
997 1022 self.alias_table)
998 1023
999 1024 # Platform-specific configuration
1000 1025 if os.name == 'nt':
1001 1026 self.readline_startup_hook = readline.set_pre_input_hook
1002 1027 else:
1003 1028 self.readline_startup_hook = readline.set_startup_hook
1004 1029
1005 1030 # Load user's initrc file (readline config)
1006 1031 inputrc_name = os.environ.get('INPUTRC')
1007 1032 if inputrc_name is None:
1008 1033 home_dir = get_home_dir()
1009 1034 if home_dir is not None:
1010 1035 inputrc_name = os.path.join(home_dir,'.inputrc')
1011 1036 if os.path.isfile(inputrc_name):
1012 1037 try:
1013 1038 readline.read_init_file(inputrc_name)
1014 1039 except:
1015 1040 warn('Problems reading readline initialization file <%s>'
1016 1041 % inputrc_name)
1017 1042
1018 1043 self.has_readline = 1
1019 1044 self.readline = readline
1020 1045 # save this in sys so embedded copies can restore it properly
1021 1046 sys.ipcompleter = self.Completer.complete
1022 1047 readline.set_completer(self.Completer.complete)
1023 1048
1024 1049 # Configure readline according to user's prefs
1025 1050 for rlcommand in self.rc.readline_parse_and_bind:
1026 1051 readline.parse_and_bind(rlcommand)
1027 1052
1028 1053 # remove some chars from the delimiters list
1029 1054 delims = readline.get_completer_delims()
1030 1055 delims = delims.translate(string._idmap,
1031 1056 self.rc.readline_remove_delims)
1032 1057 readline.set_completer_delims(delims)
1033 1058 # otherwise we end up with a monster history after a while:
1034 1059 readline.set_history_length(1000)
1035 1060 try:
1036 1061 #print '*** Reading readline history' # dbg
1037 1062 readline.read_history_file(self.histfile)
1038 1063 except IOError:
1039 1064 pass # It doesn't exist yet.
1040 1065
1041 1066 atexit.register(self.atexit_operations)
1042 1067 del atexit
1043 1068
1044 1069 # Configure auto-indent for all platforms
1045 1070 self.set_autoindent(self.rc.autoindent)
1046 1071
1047 1072 def _should_recompile(self,e):
1048 1073 """Utility routine for edit_syntax_error"""
1049 1074
1050 1075 if e.filename in ('<ipython console>','<input>','<string>',
1051 1076 '<console>'):
1052 1077 return False
1053 1078 try:
1054 1079 if not ask_yes_no('Return to editor to correct syntax error? '
1055 1080 '[Y/n] ','y'):
1056 1081 return False
1057 1082 except EOFError:
1058 1083 return False
1059 1084 self.hooks.fix_error_editor(e.filename,e.lineno,e.offset,e.msg)
1060 1085 return True
1061 1086
1062 1087 def edit_syntax_error(self):
1063 1088 """The bottom half of the syntax error handler called in the main loop.
1064 1089
1065 1090 Loop until syntax error is fixed or user cancels.
1066 1091 """
1067 1092
1068 1093 while self.SyntaxTB.last_syntax_error:
1069 1094 # copy and clear last_syntax_error
1070 1095 err = self.SyntaxTB.clear_err_state()
1071 1096 if not self._should_recompile(err):
1072 1097 return
1073 1098 try:
1074 1099 # may set last_syntax_error again if a SyntaxError is raised
1075 1100 self.safe_execfile(err.filename,self.shell.user_ns)
1076 1101 except:
1077 1102 self.showtraceback()
1078 1103 else:
1079 1104 f = file(err.filename)
1080 1105 try:
1081 1106 sys.displayhook(f.read())
1082 1107 finally:
1083 1108 f.close()
1084 1109
1085 1110 def showsyntaxerror(self, filename=None):
1086 1111 """Display the syntax error that just occurred.
1087 1112
1088 1113 This doesn't display a stack trace because there isn't one.
1089 1114
1090 1115 If a filename is given, it is stuffed in the exception instead
1091 1116 of what was there before (because Python's parser always uses
1092 1117 "<string>" when reading from a string).
1093 1118 """
1094 1119 type, value, sys.last_traceback = sys.exc_info()
1095 1120 sys.last_type = type
1096 1121 sys.last_value = value
1097 1122 if filename and type is SyntaxError:
1098 1123 # Work hard to stuff the correct filename in the exception
1099 1124 try:
1100 1125 msg, (dummy_filename, lineno, offset, line) = value
1101 1126 except:
1102 1127 # Not the format we expect; leave it alone
1103 1128 pass
1104 1129 else:
1105 1130 # Stuff in the right filename
1106 1131 try:
1107 1132 # Assume SyntaxError is a class exception
1108 1133 value = SyntaxError(msg, (filename, lineno, offset, line))
1109 1134 except:
1110 1135 # If that failed, assume SyntaxError is a string
1111 1136 value = msg, (filename, lineno, offset, line)
1112 1137 self.SyntaxTB(type,value,[])
1113 1138
1114 1139 def debugger(self):
1115 1140 """Call the pdb debugger."""
1116 1141
1117 1142 if not self.rc.pdb:
1118 1143 return
1119 1144 pdb.pm()
1120 1145
1121 1146 def showtraceback(self,exc_tuple = None,filename=None):
1122 1147 """Display the exception that just occurred."""
1123 1148
1124 1149 # Though this won't be called by syntax errors in the input line,
1125 1150 # there may be SyntaxError cases whith imported code.
1126 1151 if exc_tuple is None:
1127 1152 type, value, tb = sys.exc_info()
1128 1153 else:
1129 1154 type, value, tb = exc_tuple
1130 1155 if type is SyntaxError:
1131 1156 self.showsyntaxerror(filename)
1132 1157 else:
1133 1158 sys.last_type = type
1134 1159 sys.last_value = value
1135 1160 sys.last_traceback = tb
1136 1161 self.InteractiveTB()
1137 1162 if self.InteractiveTB.call_pdb and self.has_readline:
1138 1163 # pdb mucks up readline, fix it back
1139 1164 self.readline.set_completer(self.Completer.complete)
1140 1165
1141 1166 def update_cache(self, line):
1142 1167 """puts line into cache"""
1143 1168 self.inputcache.insert(0, line) # This copies the cache every time ... :-(
1144 1169 if len(self.inputcache) >= self.CACHELENGTH:
1145 1170 self.inputcache.pop() # This doesn't :-)
1146 1171
1147 1172 def mainloop(self,banner=None):
1148 1173 """Creates the local namespace and starts the mainloop.
1149 1174
1150 1175 If an optional banner argument is given, it will override the
1151 1176 internally created default banner."""
1152 1177
1153 1178 if self.rc.c: # Emulate Python's -c option
1154 1179 self.exec_init_cmd()
1155 1180 if banner is None:
1156 1181 if self.rc.banner:
1157 1182 banner = self.BANNER+self.banner2
1158 1183 else:
1159 1184 banner = ''
1160 1185 self.interact(banner)
1161 1186
1162 1187 def exec_init_cmd(self):
1163 1188 """Execute a command given at the command line.
1164 1189
1165 1190 This emulates Python's -c option."""
1166 1191
1167 1192 sys.argv = ['-c']
1168 1193 self.push(self.rc.c)
1169 1194
1170 1195 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1171 1196 """Embeds IPython into a running python program.
1172 1197
1173 1198 Input:
1174 1199
1175 1200 - header: An optional header message can be specified.
1176 1201
1177 1202 - local_ns, global_ns: working namespaces. If given as None, the
1178 1203 IPython-initialized one is updated with __main__.__dict__, so that
1179 1204 program variables become visible but user-specific configuration
1180 1205 remains possible.
1181 1206
1182 1207 - stack_depth: specifies how many levels in the stack to go to
1183 1208 looking for namespaces (when local_ns and global_ns are None). This
1184 1209 allows an intermediate caller to make sure that this function gets
1185 1210 the namespace from the intended level in the stack. By default (0)
1186 1211 it will get its locals and globals from the immediate caller.
1187 1212
1188 1213 Warning: it's possible to use this in a program which is being run by
1189 1214 IPython itself (via %run), but some funny things will happen (a few
1190 1215 globals get overwritten). In the future this will be cleaned up, as
1191 1216 there is no fundamental reason why it can't work perfectly."""
1192 1217
1193 1218 # Get locals and globals from caller
1194 1219 if local_ns is None or global_ns is None:
1195 1220 call_frame = sys._getframe(stack_depth).f_back
1196 1221
1197 1222 if local_ns is None:
1198 1223 local_ns = call_frame.f_locals
1199 1224 if global_ns is None:
1200 1225 global_ns = call_frame.f_globals
1201 1226
1202 1227 # Update namespaces and fire up interpreter
1203 1228 self.user_ns = local_ns
1204 1229 self.user_global_ns = global_ns
1205 1230
1206 1231 # Patch for global embedding to make sure that things don't overwrite
1207 1232 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1208 1233 # FIXME. Test this a bit more carefully (the if.. is new)
1209 1234 if local_ns is None and global_ns is None:
1210 1235 self.user_global_ns.update(__main__.__dict__)
1211 1236
1212 1237 # make sure the tab-completer has the correct frame information, so it
1213 1238 # actually completes using the frame's locals/globals
1214 1239 self.set_completer_frame(call_frame)
1215 1240
1216 1241 self.interact(header)
1217 1242
1218 1243 def interact(self, banner=None):
1219 1244 """Closely emulate the interactive Python console.
1220 1245
1221 1246 The optional banner argument specify the banner to print
1222 1247 before the first interaction; by default it prints a banner
1223 1248 similar to the one printed by the real Python interpreter,
1224 1249 followed by the current class name in parentheses (so as not
1225 1250 to confuse this with the real interpreter -- since it's so
1226 1251 close!).
1227 1252
1228 1253 """
1229 1254 cprt = 'Type "copyright", "credits" or "license" for more information.'
1230 1255 if banner is None:
1231 1256 self.write("Python %s on %s\n%s\n(%s)\n" %
1232 1257 (sys.version, sys.platform, cprt,
1233 1258 self.__class__.__name__))
1234 1259 else:
1235 1260 self.write(banner)
1236 1261
1237 1262 more = 0
1238 1263
1239 1264 # Mark activity in the builtins
1240 1265 __builtin__.__dict__['__IPYTHON__active'] += 1
1241 1266
1242 1267 # compiled regexps for autoindent management
1243 1268 ini_spaces_re = re.compile(r'^(\s+)')
1244 1269 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
1245 1270
1246 1271 # exit_now is set by a call to %Exit or %Quit
1247 1272 while not self.exit_now:
1248 1273 try:
1249 1274 if more:
1250 1275 prompt = self.outputcache.prompt2
1251 1276 if self.autoindent:
1252 1277 self.readline_startup_hook(self.pre_readline)
1253 1278 else:
1254 1279 prompt = self.outputcache.prompt1
1255 1280 try:
1256 1281 line = self.raw_input(prompt,more)
1257 1282 if self.autoindent:
1258 1283 self.readline_startup_hook(None)
1259 1284 except EOFError:
1260 1285 if self.autoindent:
1261 1286 self.readline_startup_hook(None)
1262 1287 self.write("\n")
1263 1288 self.exit()
1264 1289 else:
1265 1290 more = self.push(line)
1266 1291 # Auto-indent management
1267 1292 if self.autoindent:
1268 1293 if line:
1269 1294 ini_spaces = ini_spaces_re.match(line)
1270 1295 if ini_spaces:
1271 1296 nspaces = ini_spaces.end()
1272 1297 else:
1273 1298 nspaces = 0
1274 1299 self.indent_current_nsp = nspaces
1275 1300
1276 1301 if line[-1] == ':':
1277 1302 self.indent_current_nsp += 4
1278 1303 elif dedent_re.match(line):
1279 1304 self.indent_current_nsp -= 4
1280 1305 else:
1281 1306 self.indent_current_nsp = 0
1282 1307
1283 1308 # indent_current is the actual string to be inserted
1284 1309 # by the readline hooks for indentation
1285 1310 self.indent_current = ' '* self.indent_current_nsp
1286 1311
1287 1312 if (self.SyntaxTB.last_syntax_error and
1288 1313 self.rc.autoedit_syntax):
1289 1314 self.edit_syntax_error()
1290 1315
1291 1316 except KeyboardInterrupt:
1292 1317 self.write("\nKeyboardInterrupt\n")
1293 1318 self.resetbuffer()
1294 1319 more = 0
1295 1320 # keep cache in sync with the prompt counter:
1296 1321 self.outputcache.prompt_count -= 1
1297 1322
1298 1323 if self.autoindent:
1299 1324 self.indent_current_nsp = 0
1300 1325 self.indent_current = ' '* self.indent_current_nsp
1301 1326
1302 1327 except bdb.BdbQuit:
1303 1328 warn("The Python debugger has exited with a BdbQuit exception.\n"
1304 1329 "Because of how pdb handles the stack, it is impossible\n"
1305 1330 "for IPython to properly format this particular exception.\n"
1306 1331 "IPython will resume normal operation.")
1307 1332
1308 1333 # We are off again...
1309 1334 __builtin__.__dict__['__IPYTHON__active'] -= 1
1310 1335
1311 1336 def excepthook(self, type, value, tb):
1312 1337 """One more defense for GUI apps that call sys.excepthook.
1313 1338
1314 1339 GUI frameworks like wxPython trap exceptions and call
1315 1340 sys.excepthook themselves. I guess this is a feature that
1316 1341 enables them to keep running after exceptions that would
1317 1342 otherwise kill their mainloop. This is a bother for IPython
1318 1343 which excepts to catch all of the program exceptions with a try:
1319 1344 except: statement.
1320 1345
1321 1346 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1322 1347 any app directly invokes sys.excepthook, it will look to the user like
1323 1348 IPython crashed. In order to work around this, we can disable the
1324 1349 CrashHandler and replace it with this excepthook instead, which prints a
1325 1350 regular traceback using our InteractiveTB. In this fashion, apps which
1326 1351 call sys.excepthook will generate a regular-looking exception from
1327 1352 IPython, and the CrashHandler will only be triggered by real IPython
1328 1353 crashes.
1329 1354
1330 1355 This hook should be used sparingly, only in places which are not likely
1331 1356 to be true IPython errors.
1332 1357 """
1333 1358
1334 1359 self.InteractiveTB(type, value, tb, tb_offset=0)
1335 1360 if self.InteractiveTB.call_pdb and self.has_readline:
1336 1361 self.readline.set_completer(self.Completer.complete)
1337 1362
1338 1363 def call_alias(self,alias,rest=''):
1339 1364 """Call an alias given its name and the rest of the line.
1340 1365
1341 1366 This function MUST be given a proper alias, because it doesn't make
1342 1367 any checks when looking up into the alias table. The caller is
1343 1368 responsible for invoking it only with a valid alias."""
1344 1369
1345 1370 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1346 1371 nargs,cmd = self.alias_table[alias]
1347 1372 # Expand the %l special to be the user's input line
1348 1373 if cmd.find('%l') >= 0:
1349 1374 cmd = cmd.replace('%l',rest)
1350 1375 rest = ''
1351 1376 if nargs==0:
1352 1377 # Simple, argument-less aliases
1353 1378 cmd = '%s %s' % (cmd,rest)
1354 1379 else:
1355 1380 # Handle aliases with positional arguments
1356 1381 args = rest.split(None,nargs)
1357 1382 if len(args)< nargs:
1358 1383 error('Alias <%s> requires %s arguments, %s given.' %
1359 1384 (alias,nargs,len(args)))
1360 1385 return
1361 1386 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1362 1387 # Now call the macro, evaluating in the user's namespace
1363 1388 try:
1364 1389 self.system(cmd)
1365 1390 except:
1366 1391 self.showtraceback()
1367 1392
1368 1393 def runlines(self,lines):
1369 1394 """Run a string of one or more lines of source.
1370 1395
1371 1396 This method is capable of running a string containing multiple source
1372 1397 lines, as if they had been entered at the IPython prompt. Since it
1373 1398 exposes IPython's processing machinery, the given strings can contain
1374 1399 magic calls (%magic), special shell access (!cmd), etc."""
1375 1400
1376 1401 # We must start with a clean buffer, in case this is run from an
1377 1402 # interactive IPython session (via a magic, for example).
1378 1403 self.resetbuffer()
1379 1404 lines = lines.split('\n')
1380 1405 more = 0
1381 1406 for line in lines:
1382 1407 # skip blank lines so we don't mess up the prompt counter, but do
1383 1408 # NOT skip even a blank line if we are in a code block (more is
1384 1409 # true)
1385 1410 if line or more:
1386 1411 more = self.push((self.prefilter(line,more)))
1387 1412 # IPython's runsource returns None if there was an error
1388 1413 # compiling the code. This allows us to stop processing right
1389 1414 # away, so the user gets the error message at the right place.
1390 1415 if more is None:
1391 1416 break
1392 1417 # final newline in case the input didn't have it, so that the code
1393 1418 # actually does get executed
1394 1419 if more:
1395 1420 self.push('\n')
1396 1421
1397 1422 def runsource(self, source, filename='<input>', symbol='single'):
1398 1423 """Compile and run some source in the interpreter.
1399 1424
1400 1425 Arguments are as for compile_command().
1401 1426
1402 1427 One several things can happen:
1403 1428
1404 1429 1) The input is incorrect; compile_command() raised an
1405 1430 exception (SyntaxError or OverflowError). A syntax traceback
1406 1431 will be printed by calling the showsyntaxerror() method.
1407 1432
1408 1433 2) The input is incomplete, and more input is required;
1409 1434 compile_command() returned None. Nothing happens.
1410 1435
1411 1436 3) The input is complete; compile_command() returned a code
1412 1437 object. The code is executed by calling self.runcode() (which
1413 1438 also handles run-time exceptions, except for SystemExit).
1414 1439
1415 1440 The return value is:
1416 1441
1417 1442 - True in case 2
1418 1443
1419 1444 - False in the other cases, unless an exception is raised, where
1420 1445 None is returned instead. This can be used by external callers to
1421 1446 know whether to continue feeding input or not.
1422 1447
1423 1448 The return value can be used to decide whether to use sys.ps1 or
1424 1449 sys.ps2 to prompt the next line."""
1425 1450
1426 1451 try:
1427 1452 code = self.compile(source,filename,symbol)
1428 1453 except (OverflowError, SyntaxError, ValueError):
1429 1454 # Case 1
1430 1455 self.showsyntaxerror(filename)
1431 1456 return None
1432 1457
1433 1458 if code is None:
1434 1459 # Case 2
1435 1460 return True
1436 1461
1437 1462 # Case 3
1438 1463 # We store the code object so that threaded shells and
1439 1464 # custom exception handlers can access all this info if needed.
1440 1465 # The source corresponding to this can be obtained from the
1441 1466 # buffer attribute as '\n'.join(self.buffer).
1442 1467 self.code_to_run = code
1443 1468 # now actually execute the code object
1444 1469 if self.runcode(code) == 0:
1445 1470 return False
1446 1471 else:
1447 1472 return None
1448 1473
1449 1474 def runcode(self,code_obj):
1450 1475 """Execute a code object.
1451 1476
1452 1477 When an exception occurs, self.showtraceback() is called to display a
1453 1478 traceback.
1454 1479
1455 1480 Return value: a flag indicating whether the code to be run completed
1456 1481 successfully:
1457 1482
1458 1483 - 0: successful execution.
1459 1484 - 1: an error occurred.
1460 1485 """
1461 1486
1462 1487 # Set our own excepthook in case the user code tries to call it
1463 1488 # directly, so that the IPython crash handler doesn't get triggered
1464 1489 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1490
1491 # we save the original sys.excepthook in the instance, in case config
1492 # code (such as magics) needs access to it.
1493 self.sys_excepthook = old_excepthook
1465 1494 outflag = 1 # happens in more places, so it's easier as default
1466 1495 try:
1467 1496 try:
1468 1497 # Embedded instances require separate global/local namespaces
1469 1498 # so they can see both the surrounding (local) namespace and
1470 1499 # the module-level globals when called inside another function.
1471 1500 if self.embedded:
1472 1501 exec code_obj in self.user_global_ns, self.user_ns
1473 1502 # Normal (non-embedded) instances should only have a single
1474 1503 # namespace for user code execution, otherwise functions won't
1475 1504 # see interactive top-level globals.
1476 1505 else:
1477 1506 exec code_obj in self.user_ns
1478 1507 finally:
1479 1508 # Reset our crash handler in place
1480 1509 sys.excepthook = old_excepthook
1481 1510 except SystemExit:
1482 1511 self.resetbuffer()
1483 1512 self.showtraceback()
1484 1513 warn("Type exit or quit to exit IPython "
1485 1514 "(%Exit or %Quit do so unconditionally).",level=1)
1486 1515 except self.custom_exceptions:
1487 1516 etype,value,tb = sys.exc_info()
1488 1517 self.CustomTB(etype,value,tb)
1489 1518 except:
1490 1519 self.showtraceback()
1491 1520 else:
1492 1521 outflag = 0
1493 1522 if softspace(sys.stdout, 0):
1494 1523 print
1495 1524 # Flush out code object which has been run (and source)
1496 1525 self.code_to_run = None
1497 1526 return outflag
1498 1527
1499 1528 def push(self, line):
1500 1529 """Push a line to the interpreter.
1501 1530
1502 1531 The line should not have a trailing newline; it may have
1503 1532 internal newlines. The line is appended to a buffer and the
1504 1533 interpreter's runsource() method is called with the
1505 1534 concatenated contents of the buffer as source. If this
1506 1535 indicates that the command was executed or invalid, the buffer
1507 1536 is reset; otherwise, the command is incomplete, and the buffer
1508 1537 is left as it was after the line was appended. The return
1509 1538 value is 1 if more input is required, 0 if the line was dealt
1510 1539 with in some way (this is the same as runsource()).
1511 1540
1512 1541 """
1513 1542 self.buffer.append(line)
1514 1543 more = self.runsource('\n'.join(self.buffer), self.filename)
1515 1544 if not more:
1516 1545 self.resetbuffer()
1517 1546 return more
1518 1547
1519 1548 def resetbuffer(self):
1520 1549 """Reset the input buffer."""
1521 1550 self.buffer[:] = []
1522 1551
1523 1552 def raw_input(self,prompt='',continue_prompt=False):
1524 1553 """Write a prompt and read a line.
1525 1554
1526 1555 The returned line does not include the trailing newline.
1527 1556 When the user enters the EOF key sequence, EOFError is raised.
1528 1557
1529 1558 Optional inputs:
1530 1559
1531 1560 - prompt(''): a string to be printed to prompt the user.
1532 1561
1533 1562 - continue_prompt(False): whether this line is the first one or a
1534 1563 continuation in a sequence of inputs.
1535 1564 """
1536 1565
1537 1566 line = raw_input_original(prompt)
1538 1567 # Try to be reasonably smart about not re-indenting pasted input more
1539 1568 # than necessary. We do this by trimming out the auto-indent initial
1540 1569 # spaces, if the user's actual input started itself with whitespace.
1541 1570 if self.autoindent:
1542 1571 line2 = line[self.indent_current_nsp:]
1543 1572 if line2[0:1] in (' ','\t'):
1544 1573 line = line2
1545 1574 return self.prefilter(line,continue_prompt)
1546 1575
1547 1576 def split_user_input(self,line):
1548 1577 """Split user input into pre-char, function part and rest."""
1549 1578
1550 1579 lsplit = self.line_split.match(line)
1551 1580 if lsplit is None: # no regexp match returns None
1552 1581 try:
1553 1582 iFun,theRest = line.split(None,1)
1554 1583 except ValueError:
1555 1584 iFun,theRest = line,''
1556 1585 pre = re.match('^(\s*)(.*)',line).groups()[0]
1557 1586 else:
1558 1587 pre,iFun,theRest = lsplit.groups()
1559 1588
1560 1589 #print 'line:<%s>' % line # dbg
1561 1590 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1562 1591 return pre,iFun.strip(),theRest
1563 1592
1564 1593 def _prefilter(self, line, continue_prompt):
1565 1594 """Calls different preprocessors, depending on the form of line."""
1566 1595
1567 1596 # All handlers *must* return a value, even if it's blank ('').
1568 1597
1569 1598 # Lines are NOT logged here. Handlers should process the line as
1570 1599 # needed, update the cache AND log it (so that the input cache array
1571 1600 # stays synced).
1572 1601
1573 1602 # This function is _very_ delicate, and since it's also the one which
1574 1603 # determines IPython's response to user input, it must be as efficient
1575 1604 # as possible. For this reason it has _many_ returns in it, trying
1576 1605 # always to exit as quickly as it can figure out what it needs to do.
1577 1606
1578 1607 # This function is the main responsible for maintaining IPython's
1579 1608 # behavior respectful of Python's semantics. So be _very_ careful if
1580 1609 # making changes to anything here.
1581 1610
1582 1611 #.....................................................................
1583 1612 # Code begins
1584 1613
1585 1614 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1586 1615
1587 1616 # save the line away in case we crash, so the post-mortem handler can
1588 1617 # record it
1589 1618 self._last_input_line = line
1590 1619
1591 1620 #print '***line: <%s>' % line # dbg
1592 1621
1593 1622 # the input history needs to track even empty lines
1594 1623 if not line.strip():
1595 1624 if not continue_prompt:
1596 1625 self.outputcache.prompt_count -= 1
1597 1626 return self.handle_normal(line,continue_prompt)
1598 1627 #return self.handle_normal('',continue_prompt)
1599 1628
1600 1629 # print '***cont',continue_prompt # dbg
1601 1630 # special handlers are only allowed for single line statements
1602 1631 if continue_prompt and not self.rc.multi_line_specials:
1603 1632 return self.handle_normal(line,continue_prompt)
1604 1633
1605 1634 # For the rest, we need the structure of the input
1606 1635 pre,iFun,theRest = self.split_user_input(line)
1607 1636 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1608 1637
1609 1638 # First check for explicit escapes in the last/first character
1610 1639 handler = None
1611 1640 if line[-1] == self.ESC_HELP:
1612 1641 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1613 1642 if handler is None:
1614 1643 # look at the first character of iFun, NOT of line, so we skip
1615 1644 # leading whitespace in multiline input
1616 1645 handler = self.esc_handlers.get(iFun[0:1])
1617 1646 if handler is not None:
1618 1647 return handler(line,continue_prompt,pre,iFun,theRest)
1619 1648 # Emacs ipython-mode tags certain input lines
1620 1649 if line.endswith('# PYTHON-MODE'):
1621 1650 return self.handle_emacs(line,continue_prompt)
1622 1651
1623 1652 # Next, check if we can automatically execute this thing
1624 1653
1625 1654 # Allow ! in multi-line statements if multi_line_specials is on:
1626 1655 if continue_prompt and self.rc.multi_line_specials and \
1627 1656 iFun.startswith(self.ESC_SHELL):
1628 1657 return self.handle_shell_escape(line,continue_prompt,
1629 1658 pre=pre,iFun=iFun,
1630 1659 theRest=theRest)
1631 1660
1632 1661 # Let's try to find if the input line is a magic fn
1633 1662 oinfo = None
1634 1663 if hasattr(self,'magic_'+iFun):
1635 1664 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1636 1665 if oinfo['ismagic']:
1637 1666 # Be careful not to call magics when a variable assignment is
1638 1667 # being made (ls='hi', for example)
1639 1668 if self.rc.automagic and \
1640 1669 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1641 1670 (self.rc.multi_line_specials or not continue_prompt):
1642 1671 return self.handle_magic(line,continue_prompt,
1643 1672 pre,iFun,theRest)
1644 1673 else:
1645 1674 return self.handle_normal(line,continue_prompt)
1646 1675
1647 1676 # If the rest of the line begins with an (in)equality, assginment or
1648 1677 # function call, we should not call _ofind but simply execute it.
1649 1678 # This avoids spurious geattr() accesses on objects upon assignment.
1650 1679 #
1651 1680 # It also allows users to assign to either alias or magic names true
1652 1681 # python variables (the magic/alias systems always take second seat to
1653 1682 # true python code).
1654 1683 if theRest and theRest[0] in '!=()':
1655 1684 return self.handle_normal(line,continue_prompt)
1656 1685
1657 1686 if oinfo is None:
1658 1687 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1659 1688
1660 1689 if not oinfo['found']:
1661 1690 return self.handle_normal(line,continue_prompt)
1662 1691 else:
1663 1692 #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg
1664 1693 if oinfo['isalias']:
1665 1694 return self.handle_alias(line,continue_prompt,
1666 1695 pre,iFun,theRest)
1667 1696
1668 1697 if self.rc.autocall and \
1669 1698 not self.re_exclude_auto.match(theRest) and \
1670 1699 self.re_fun_name.match(iFun) and \
1671 1700 callable(oinfo['obj']) :
1672 1701 #print 'going auto' # dbg
1673 1702 return self.handle_auto(line,continue_prompt,pre,iFun,theRest)
1674 1703 else:
1675 1704 #print 'was callable?', callable(oinfo['obj']) # dbg
1676 1705 return self.handle_normal(line,continue_prompt)
1677 1706
1678 1707 # If we get here, we have a normal Python line. Log and return.
1679 1708 return self.handle_normal(line,continue_prompt)
1680 1709
1681 1710 def _prefilter_dumb(self, line, continue_prompt):
1682 1711 """simple prefilter function, for debugging"""
1683 1712 return self.handle_normal(line,continue_prompt)
1684 1713
1685 1714 # Set the default prefilter() function (this can be user-overridden)
1686 1715 prefilter = _prefilter
1687 1716
1688 1717 def handle_normal(self,line,continue_prompt=None,
1689 1718 pre=None,iFun=None,theRest=None):
1690 1719 """Handle normal input lines. Use as a template for handlers."""
1691 1720
1692 1721 # With autoindent on, we need some way to exit the input loop, and I
1693 1722 # don't want to force the user to have to backspace all the way to
1694 1723 # clear the line. The rule will be in this case, that either two
1695 1724 # lines of pure whitespace in a row, or a line of pure whitespace but
1696 1725 # of a size different to the indent level, will exit the input loop.
1697 1726 if (continue_prompt and self.autoindent and isspace(line) and
1698 1727 (line != self.indent_current or isspace(self.buffer[-1]))):
1699 1728 line = ''
1700 1729
1701 1730 self.log(line,continue_prompt)
1702 1731 self.update_cache(line)
1703 1732 return line
1704 1733
1705 1734 def handle_alias(self,line,continue_prompt=None,
1706 1735 pre=None,iFun=None,theRest=None):
1707 1736 """Handle alias input lines. """
1708 1737
1709 1738 theRest = esc_quotes(theRest)
1710 1739 line_out = "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest)
1711 1740 self.log(line_out,continue_prompt)
1712 1741 self.update_cache(line_out)
1713 1742 return line_out
1714 1743
1715 1744 def handle_shell_escape(self, line, continue_prompt=None,
1716 1745 pre=None,iFun=None,theRest=None):
1717 1746 """Execute the line in a shell, empty return value"""
1718 1747
1719 1748 #print 'line in :', `line` # dbg
1720 1749 # Example of a special handler. Others follow a similar pattern.
1721 1750 if continue_prompt: # multi-line statements
1722 1751 if iFun.startswith('!!'):
1723 1752 print 'SyntaxError: !! is not allowed in multiline statements'
1724 1753 return pre
1725 1754 else:
1726 1755 cmd = ("%s %s" % (iFun[1:],theRest)) #.replace('"','\\"')
1727 1756 #line_out = '%s%s.system("%s")' % (pre,self.name,cmd)
1728 1757 line_out = '%s%s.system(r"""%s"""[:-1])' % (pre,self.name,cmd + "_")
1729 1758 #line_out = ('%s%s.system(' % (pre,self.name)) + repr(cmd) + ')'
1730 1759 else: # single-line input
1731 1760 if line.startswith('!!'):
1732 1761 # rewrite iFun/theRest to properly hold the call to %sx and
1733 1762 # the actual command to be executed, so handle_magic can work
1734 1763 # correctly
1735 1764 theRest = '%s %s' % (iFun[2:],theRest)
1736 1765 iFun = 'sx'
1737 1766 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]),
1738 1767 continue_prompt,pre,iFun,theRest)
1739 1768 else:
1740 1769 #cmd = esc_quotes(line[1:])
1741 1770 cmd=line[1:]
1742 1771 #line_out = '%s.system("%s")' % (self.name,cmd)
1743 1772 line_out = '%s.system(r"""%s"""[:-1])' % (self.name,cmd +"_")
1744 1773 #line_out = ('%s.system(' % self.name) + repr(cmd)+ ')'
1745 1774 # update cache/log and return
1746 1775 self.log(line_out,continue_prompt)
1747 1776 self.update_cache(line_out) # readline cache gets normal line
1748 1777 #print 'line out r:', `line_out` # dbg
1749 1778 #print 'line out s:', line_out # dbg
1750 1779 return line_out
1751 1780
1752 1781 def handle_magic(self, line, continue_prompt=None,
1753 1782 pre=None,iFun=None,theRest=None):
1754 1783 """Execute magic functions.
1755 1784
1756 1785 Also log them with a prepended # so the log is clean Python."""
1757 1786
1758 1787 cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest)))
1759 1788 self.log(cmd,continue_prompt)
1760 1789 self.update_cache(line)
1761 1790 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
1762 1791 return cmd
1763 1792
1764 1793 def handle_auto(self, line, continue_prompt=None,
1765 1794 pre=None,iFun=None,theRest=None):
1766 1795 """Hande lines which can be auto-executed, quoting if requested."""
1767 1796
1768 1797 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1769 1798
1770 1799 # This should only be active for single-line input!
1771 1800 if continue_prompt:
1772 1801 return line
1773 1802
1774 1803 if pre == self.ESC_QUOTE:
1775 1804 # Auto-quote splitting on whitespace
1776 1805 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
1777 1806 elif pre == self.ESC_QUOTE2:
1778 1807 # Auto-quote whole string
1779 1808 newcmd = '%s("%s")' % (iFun,theRest)
1780 1809 else:
1781 1810 # Auto-paren
1782 1811 if theRest[0:1] in ('=','['):
1783 1812 # Don't autocall in these cases. They can be either
1784 1813 # rebindings of an existing callable's name, or item access
1785 1814 # for an object which is BOTH callable and implements
1786 1815 # __getitem__.
1787 1816 return '%s %s' % (iFun,theRest)
1788 1817 if theRest.endswith(';'):
1789 1818 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
1790 1819 else:
1791 1820 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1792 1821
1793 1822 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
1794 1823 # log what is now valid Python, not the actual user input (without the
1795 1824 # final newline)
1796 1825 self.log(newcmd,continue_prompt)
1797 1826 return newcmd
1798 1827
1799 1828 def handle_help(self, line, continue_prompt=None,
1800 1829 pre=None,iFun=None,theRest=None):
1801 1830 """Try to get some help for the object.
1802 1831
1803 1832 obj? or ?obj -> basic information.
1804 1833 obj?? or ??obj -> more details.
1805 1834 """
1806 1835
1807 1836 # We need to make sure that we don't process lines which would be
1808 1837 # otherwise valid python, such as "x=1 # what?"
1809 1838 try:
1810 1839 codeop.compile_command(line)
1811 1840 except SyntaxError:
1812 1841 # We should only handle as help stuff which is NOT valid syntax
1813 1842 if line[0]==self.ESC_HELP:
1814 1843 line = line[1:]
1815 1844 elif line[-1]==self.ESC_HELP:
1816 1845 line = line[:-1]
1817 1846 self.log('#?'+line)
1818 1847 self.update_cache(line)
1819 1848 if line:
1820 1849 self.magic_pinfo(line)
1821 1850 else:
1822 1851 page(self.usage,screen_lines=self.rc.screen_length)
1823 1852 return '' # Empty string is needed here!
1824 1853 except:
1825 1854 # Pass any other exceptions through to the normal handler
1826 1855 return self.handle_normal(line,continue_prompt)
1827 1856 else:
1828 1857 # If the code compiles ok, we should handle it normally
1829 1858 return self.handle_normal(line,continue_prompt)
1830 1859
1831 1860 def handle_emacs(self,line,continue_prompt=None,
1832 1861 pre=None,iFun=None,theRest=None):
1833 1862 """Handle input lines marked by python-mode."""
1834 1863
1835 1864 # Currently, nothing is done. Later more functionality can be added
1836 1865 # here if needed.
1837 1866
1838 1867 # The input cache shouldn't be updated
1839 1868
1840 1869 return line
1841 1870
1842 1871 def write(self,data):
1843 1872 """Write a string to the default output"""
1844 1873 Term.cout.write(data)
1845 1874
1846 1875 def write_err(self,data):
1847 1876 """Write a string to the default error output"""
1848 1877 Term.cerr.write(data)
1849 1878
1850 1879 def exit(self):
1851 1880 """Handle interactive exit.
1852 1881
1853 1882 This method sets the exit_now attribute."""
1854 1883
1855 1884 if self.rc.confirm_exit:
1856 1885 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
1857 1886 self.exit_now = True
1858 1887 else:
1859 1888 self.exit_now = True
1860 1889 return self.exit_now
1861 1890
1862 1891 def safe_execfile(self,fname,*where,**kw):
1863 1892 fname = os.path.expanduser(fname)
1864 1893
1865 1894 # find things also in current directory
1866 1895 dname = os.path.dirname(fname)
1867 1896 if not sys.path.count(dname):
1868 1897 sys.path.append(dname)
1869 1898
1870 1899 try:
1871 1900 xfile = open(fname)
1872 1901 except:
1873 1902 print >> Term.cerr, \
1874 1903 'Could not open file <%s> for safe execution.' % fname
1875 1904 return None
1876 1905
1877 1906 kw.setdefault('islog',0)
1878 1907 kw.setdefault('quiet',1)
1879 1908 kw.setdefault('exit_ignore',0)
1880 1909 first = xfile.readline()
1881 1910 _LOGHEAD = str(self.LOGHEAD).split('\n',1)[0].strip()
1882 1911 xfile.close()
1883 1912 # line by line execution
1884 1913 if first.startswith(_LOGHEAD) or kw['islog']:
1885 1914 print 'Loading log file <%s> one line at a time...' % fname
1886 1915 if kw['quiet']:
1887 1916 stdout_save = sys.stdout
1888 1917 sys.stdout = StringIO.StringIO()
1889 1918 try:
1890 1919 globs,locs = where[0:2]
1891 1920 except:
1892 1921 try:
1893 1922 globs = locs = where[0]
1894 1923 except:
1895 1924 globs = locs = globals()
1896 1925 badblocks = []
1897 1926
1898 1927 # we also need to identify indented blocks of code when replaying
1899 1928 # logs and put them together before passing them to an exec
1900 1929 # statement. This takes a bit of regexp and look-ahead work in the
1901 1930 # file. It's easiest if we swallow the whole thing in memory
1902 1931 # first, and manually walk through the lines list moving the
1903 1932 # counter ourselves.
1904 1933 indent_re = re.compile('\s+\S')
1905 1934 xfile = open(fname)
1906 1935 filelines = xfile.readlines()
1907 1936 xfile.close()
1908 1937 nlines = len(filelines)
1909 1938 lnum = 0
1910 1939 while lnum < nlines:
1911 1940 line = filelines[lnum]
1912 1941 lnum += 1
1913 1942 # don't re-insert logger status info into cache
1914 1943 if line.startswith('#log#'):
1915 1944 continue
1916 1945 elif line.startswith('#%s'% self.ESC_MAGIC):
1917 1946 self.update_cache(line[1:])
1918 1947 line = magic2python(line)
1919 1948 elif line.startswith('#!'):
1920 1949 self.update_cache(line[1:])
1921 1950 else:
1922 1951 # build a block of code (maybe a single line) for execution
1923 1952 block = line
1924 1953 try:
1925 1954 next = filelines[lnum] # lnum has already incremented
1926 1955 except:
1927 1956 next = None
1928 1957 while next and indent_re.match(next):
1929 1958 block += next
1930 1959 lnum += 1
1931 1960 try:
1932 1961 next = filelines[lnum]
1933 1962 except:
1934 1963 next = None
1935 1964 # now execute the block of one or more lines
1936 1965 try:
1937 1966 exec block in globs,locs
1938 1967 self.update_cache(block.rstrip())
1939 1968 except SystemExit:
1940 1969 pass
1941 1970 except:
1942 1971 badblocks.append(block.rstrip())
1943 1972 if kw['quiet']: # restore stdout
1944 1973 sys.stdout.close()
1945 1974 sys.stdout = stdout_save
1946 1975 print 'Finished replaying log file <%s>' % fname
1947 1976 if badblocks:
1948 1977 print >> sys.stderr, ('\nThe following lines/blocks in file '
1949 1978 '<%s> reported errors:' % fname)
1950 1979
1951 1980 for badline in badblocks:
1952 1981 print >> sys.stderr, badline
1953 1982 else: # regular file execution
1954 1983 try:
1955 1984 execfile(fname,*where)
1956 1985 except SyntaxError:
1957 etype, evalue = sys.exc_info()[0:2]
1986 etype,evalue = sys.exc_info()[:2]
1958 1987 self.SyntaxTB(etype,evalue,[])
1959 1988 warn('Failure executing file: <%s>' % fname)
1960 1989 except SystemExit,status:
1961 1990 if not kw['exit_ignore']:
1962 1991 self.InteractiveTB()
1963 1992 warn('Failure executing file: <%s>' % fname)
1964 1993 except:
1965 1994 self.InteractiveTB()
1966 1995 warn('Failure executing file: <%s>' % fname)
1967 1996
1968 1997 #************************* end of file <iplib.py> *****************************
@@ -1,739 +1,735 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 IPython -- An enhanced Interactive Python
4 4
5 5 Requires Python 2.1 or better.
6 6
7 7 This file contains the main make_IPython() starter function.
8 8
9 $Id: ipmaker.py 964 2005-12-28 21:03:01Z fperez $"""
9 $Id: ipmaker.py 965 2005-12-28 23:23:09Z fperez $"""
10 10
11 11 #*****************************************************************************
12 12 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #*****************************************************************************
17 17
18 18 from IPython import Release
19 19 __author__ = '%s <%s>' % Release.authors['Fernando']
20 20 __license__ = Release.license
21 21 __version__ = Release.version
22 22
23 23 credits._Printer__data = """
24 24 Python: %s
25 25
26 26 IPython: Fernando Perez, Janko Hauser, Nathan Gray, and many users.
27 27 See http://ipython.scipy.org for more information.""" \
28 28 % credits._Printer__data
29 29
30 30 copyright._Printer__data += """
31 31
32 32 Copyright (c) 2001-2004 Fernando Perez, Janko Hauser, Nathan Gray.
33 33 All Rights Reserved."""
34 34
35 35 #****************************************************************************
36 36 # Required modules
37 37
38 38 # From the standard library
39 39 import __main__
40 40 import __builtin__
41 41 import os
42 42 import re
43 43 import sys
44 44 import types
45 45 from pprint import pprint,pformat
46 46
47 47 # Our own
48 48 from IPython import DPyGetOpt
49 49 from IPython.Struct import Struct
50 50 from IPython.OutputTrap import OutputTrap
51 51 from IPython.ConfigLoader import ConfigLoader
52 52 from IPython.iplib import InteractiveShell,qw_lol,import_fail_info
53 53 from IPython.usage import cmd_line_usage,interactive_usage
54 54 from IPython.Prompts import CachedOutput
55 55 from IPython.genutils import *
56 56
57 57 #-----------------------------------------------------------------------------
58 58 def make_IPython(argv=None,user_ns=None,user_global_ns=None,debug=1,
59 59 rc_override=None,shell_class=InteractiveShell,
60 60 embedded=False,**kw):
61 61 """This is a dump of IPython into a single function.
62 62
63 63 Later it will have to be broken up in a sensible manner.
64 64
65 65 Arguments:
66 66
67 67 - argv: a list similar to sys.argv[1:]. It should NOT contain the desired
68 68 script name, b/c DPyGetOpt strips the first argument only for the real
69 69 sys.argv.
70 70
71 71 - user_ns: a dict to be used as the user's namespace."""
72 72
73 73 #----------------------------------------------------------------------
74 74 # Defaults and initialization
75 75
76 76 # For developer debugging, deactivates crash handler and uses pdb.
77 77 DEVDEBUG = False
78 78
79 79 if argv is None:
80 80 argv = sys.argv
81 81
82 82 # __IP is the main global that lives throughout and represents the whole
83 83 # application. If the user redefines it, all bets are off as to what
84 84 # happens.
85 85
86 86 # __IP is the name of he global which the caller will have accessible as
87 87 # __IP.name. We set its name via the first parameter passed to
88 88 # InteractiveShell:
89 89
90 90 IP = shell_class('__IP',user_ns=user_ns,user_global_ns=user_global_ns,
91 91 embedded=embedded,**kw)
92 92
93 93 # Put 'help' in the user namespace
94 94 from site import _Helper
95 95 IP.user_ns['help'] = _Helper()
96 96
97
97 98 if DEVDEBUG:
98 99 # For developer debugging only (global flag)
99 100 from IPython import ultraTB
100 101 sys.excepthook = ultraTB.VerboseTB(call_pdb=1)
101 else:
102 # IPython itself shouldn't crash. This will produce a detailed
103 # post-mortem if it does
104 from IPython import CrashHandler
105 sys.excepthook = CrashHandler.CrashHandler(IP)
106 102
107 103 IP.BANNER_PARTS = ['Python %s\n'
108 104 'Type "copyright", "credits" or "license" '
109 105 'for more information.\n'
110 106 % (sys.version.split('\n')[0],),
111 107 "IPython %s -- An enhanced Interactive Python."
112 108 % (__version__,),
113 109 """? -> Introduction to IPython's features.
114 110 %magic -> Information about IPython's 'magic' % functions.
115 111 help -> Python's own help system.
116 112 object? -> Details about 'object'. ?object also works, ?? prints more.
117 113 """ ]
118 114
119 115 IP.usage = interactive_usage
120 116
121 117 # Platform-dependent suffix and directory names. We use _ipython instead
122 118 # of .ipython under win32 b/c there's software that breaks with .named
123 119 # directories on that platform.
124 120 if os.name == 'posix':
125 121 rc_suffix = ''
126 122 ipdir_def = '.ipython'
127 123 else:
128 124 rc_suffix = '.ini'
129 125 ipdir_def = '_ipython'
130 126
131 127 # default directory for configuration
132 128 ipythondir = os.path.abspath(os.environ.get('IPYTHONDIR',
133 129 os.path.join(IP.home_dir,ipdir_def)))
134 130
135 131 # we need the directory where IPython itself is installed
136 132 import IPython
137 133 IPython_dir = os.path.dirname(IPython.__file__)
138 134 del IPython
139 135
140 136 #-------------------------------------------------------------------------
141 137 # Command line handling
142 138
143 139 # Valid command line options (uses DPyGetOpt syntax, like Perl's
144 140 # GetOpt::Long)
145 141
146 142 # Any key not listed here gets deleted even if in the file (like session
147 143 # or profile). That's deliberate, to maintain the rc namespace clean.
148 144
149 145 # Each set of options appears twice: under _conv only the names are
150 146 # listed, indicating which type they must be converted to when reading the
151 147 # ipythonrc file. And under DPyGetOpt they are listed with the regular
152 148 # DPyGetOpt syntax (=s,=i,:f,etc).
153 149
154 150 # Make sure there's a space before each end of line (they get auto-joined!)
155 151 cmdline_opts = ('autocall! autoindent! automagic! banner! cache_size|cs=i '
156 152 'c=s classic|cl color_info! colors=s confirm_exit! '
157 153 'debug! deep_reload! editor=s log|l messages! nosep pdb! '
158 154 'pprint! prompt_in1|pi1=s prompt_in2|pi2=s prompt_out|po=s '
159 155 'quick screen_length|sl=i prompts_pad_left=i '
160 156 'logfile|lf=s logplay|lp=s profile|p=s '
161 157 'readline! readline_merge_completions! '
162 158 'readline_omit__names! '
163 159 'rcfile=s separate_in|si=s separate_out|so=s '
164 160 'separate_out2|so2=s xmode=s wildcards_case_sensitive! '
165 161 'magic_docstrings system_verbose! '
166 162 'multi_line_specials! '
167 163 'autoedit_syntax!')
168 164
169 165 # Options that can *only* appear at the cmd line (not in rcfiles).
170 166
171 167 # The "ignore" option is a kludge so that Emacs buffers don't crash, since
172 168 # the 'C-c !' command in emacs automatically appends a -i option at the end.
173 169 cmdline_only = ('help ignore|i ipythondir=s Version upgrade '
174 170 'gthread! qthread! wthread! pylab! tk!')
175 171
176 172 # Build the actual name list to be used by DPyGetOpt
177 173 opts_names = qw(cmdline_opts) + qw(cmdline_only)
178 174
179 175 # Set sensible command line defaults.
180 176 # This should have everything from cmdline_opts and cmdline_only
181 177 opts_def = Struct(autocall = 1,
182 178 autoedit_syntax = 1,
183 179 autoindent=0,
184 180 automagic = 1,
185 181 banner = 1,
186 182 cache_size = 1000,
187 183 c = '',
188 184 classic = 0,
189 185 colors = 'NoColor',
190 186 color_info = 0,
191 187 confirm_exit = 1,
192 188 debug = 0,
193 189 deep_reload = 0,
194 190 editor = '0',
195 191 help = 0,
196 192 ignore = 0,
197 193 ipythondir = ipythondir,
198 194 log = 0,
199 195 logfile = '',
200 196 logplay = '',
201 197 multi_line_specials = 1,
202 198 messages = 1,
203 199 nosep = 0,
204 200 pdb = 0,
205 201 pprint = 0,
206 202 profile = '',
207 203 prompt_in1 = 'In [\\#]: ',
208 204 prompt_in2 = ' .\\D.: ',
209 205 prompt_out = 'Out[\\#]: ',
210 206 prompts_pad_left = 1,
211 207 quick = 0,
212 208 readline = 1,
213 209 readline_merge_completions = 1,
214 210 readline_omit__names = 0,
215 211 rcfile = 'ipythonrc' + rc_suffix,
216 212 screen_length = 0,
217 213 separate_in = '\n',
218 214 separate_out = '\n',
219 215 separate_out2 = '',
220 216 system_verbose = 0,
221 217 gthread = 0,
222 218 qthread = 0,
223 219 wthread = 0,
224 220 pylab = 0,
225 221 tk = 0,
226 222 upgrade = 0,
227 223 Version = 0,
228 224 xmode = 'Verbose',
229 225 wildcards_case_sensitive = 1,
230 226 magic_docstrings = 0, # undocumented, for doc generation
231 227 )
232 228
233 229 # Things that will *only* appear in rcfiles (not at the command line).
234 230 # Make sure there's a space before each end of line (they get auto-joined!)
235 231 rcfile_opts = { qwflat: 'include import_mod import_all execfile ',
236 232 qw_lol: 'import_some ',
237 233 # for things with embedded whitespace:
238 234 list_strings:'execute alias readline_parse_and_bind ',
239 235 # Regular strings need no conversion:
240 236 None:'readline_remove_delims ',
241 237 }
242 238 # Default values for these
243 239 rc_def = Struct(include = [],
244 240 import_mod = [],
245 241 import_all = [],
246 242 import_some = [[]],
247 243 execute = [],
248 244 execfile = [],
249 245 alias = [],
250 246 readline_parse_and_bind = [],
251 247 readline_remove_delims = '',
252 248 )
253 249
254 250 # Build the type conversion dictionary from the above tables:
255 251 typeconv = rcfile_opts.copy()
256 252 typeconv.update(optstr2types(cmdline_opts))
257 253
258 254 # FIXME: the None key appears in both, put that back together by hand. Ugly!
259 255 typeconv[None] += ' ' + rcfile_opts[None]
260 256
261 257 # Remove quotes at ends of all strings (used to protect spaces)
262 258 typeconv[unquote_ends] = typeconv[None]
263 259 del typeconv[None]
264 260
265 261 # Build the list we'll use to make all config decisions with defaults:
266 262 opts_all = opts_def.copy()
267 263 opts_all.update(rc_def)
268 264
269 265 # Build conflict resolver for recursive loading of config files:
270 266 # - preserve means the outermost file maintains the value, it is not
271 267 # overwritten if an included file has the same key.
272 268 # - add_flip applies + to the two values, so it better make sense to add
273 269 # those types of keys. But it flips them first so that things loaded
274 270 # deeper in the inclusion chain have lower precedence.
275 271 conflict = {'preserve': ' '.join([ typeconv[int],
276 272 typeconv[unquote_ends] ]),
277 273 'add_flip': ' '.join([ typeconv[qwflat],
278 274 typeconv[qw_lol],
279 275 typeconv[list_strings] ])
280 276 }
281 277
282 278 # Now actually process the command line
283 279 getopt = DPyGetOpt.DPyGetOpt()
284 280 getopt.setIgnoreCase(0)
285 281
286 282 getopt.parseConfiguration(opts_names)
287 283
288 284 try:
289 285 getopt.processArguments(argv)
290 286 except:
291 287 print cmd_line_usage
292 288 warn('\nError in Arguments: ' + `sys.exc_value`)
293 289 sys.exit(1)
294 290
295 291 # convert the options dict to a struct for much lighter syntax later
296 292 opts = Struct(getopt.optionValues)
297 293 args = getopt.freeValues
298 294
299 295 # this is the struct (which has default values at this point) with which
300 296 # we make all decisions:
301 297 opts_all.update(opts)
302 298
303 299 # Options that force an immediate exit
304 300 if opts_all.help:
305 301 page(cmd_line_usage)
306 302 sys.exit()
307 303
308 304 if opts_all.Version:
309 305 print __version__
310 306 sys.exit()
311 307
312 308 if opts_all.magic_docstrings:
313 309 IP.magic_magic('-latex')
314 310 sys.exit()
315 311
316 312 # Create user config directory if it doesn't exist. This must be done
317 313 # *after* getting the cmd line options.
318 314 if not os.path.isdir(opts_all.ipythondir):
319 315 IP.user_setup(opts_all.ipythondir,rc_suffix,'install')
320 316
321 317 # upgrade user config files while preserving a copy of the originals
322 318 if opts_all.upgrade:
323 319 IP.user_setup(opts_all.ipythondir,rc_suffix,'upgrade')
324 320
325 321 # check mutually exclusive options in the *original* command line
326 322 mutex_opts(opts,[qw('log logfile'),qw('rcfile profile'),
327 323 qw('classic profile'),qw('classic rcfile')])
328 324
329 325 # default logfilename used when -log is called.
330 326 IP.LOGDEF = 'ipython.log'
331 327
332 328 #---------------------------------------------------------------------------
333 329 # Log replay
334 330
335 331 # if -logplay, we need to 'become' the other session. That basically means
336 332 # replacing the current command line environment with that of the old
337 333 # session and moving on.
338 334
339 335 # this is needed so that later we know we're in session reload mode, as
340 336 # opts_all will get overwritten:
341 337 load_logplay = 0
342 338
343 339 if opts_all.logplay:
344 340 load_logplay = opts_all.logplay
345 341 opts_debug_save = opts_all.debug
346 342 try:
347 343 logplay = open(opts_all.logplay)
348 344 except IOError:
349 345 if opts_all.debug: IP.InteractiveTB()
350 346 warn('Could not open logplay file '+`opts_all.logplay`)
351 347 # restore state as if nothing had happened and move on, but make
352 348 # sure that later we don't try to actually load the session file
353 349 logplay = None
354 350 load_logplay = 0
355 351 del opts_all.logplay
356 352 else:
357 353 try:
358 354 logplay.readline()
359 355 logplay.readline();
360 356 # this reloads that session's command line
361 357 cmd = logplay.readline()[6:]
362 358 exec cmd
363 359 # restore the true debug flag given so that the process of
364 360 # session loading itself can be monitored.
365 361 opts.debug = opts_debug_save
366 362 # save the logplay flag so later we don't overwrite the log
367 363 opts.logplay = load_logplay
368 364 # now we must update our own structure with defaults
369 365 opts_all.update(opts)
370 366 # now load args
371 367 cmd = logplay.readline()[6:]
372 368 exec cmd
373 369 logplay.close()
374 370 except:
375 371 logplay.close()
376 372 if opts_all.debug: IP.InteractiveTB()
377 373 warn("Logplay file lacking full configuration information.\n"
378 374 "I'll try to read it, but some things may not work.")
379 375
380 376 #-------------------------------------------------------------------------
381 377 # set up output traps: catch all output from files, being run, modules
382 378 # loaded, etc. Then give it to the user in a clean form at the end.
383 379
384 380 msg_out = 'Output messages. '
385 381 msg_err = 'Error messages. '
386 382 msg_sep = '\n'
387 383 msg = Struct(config = OutputTrap('Configuration Loader',msg_out,
388 384 msg_err,msg_sep,debug,
389 385 quiet_out=1),
390 386 user_exec = OutputTrap('User File Execution',msg_out,
391 387 msg_err,msg_sep,debug),
392 388 logplay = OutputTrap('Log Loader',msg_out,
393 389 msg_err,msg_sep,debug),
394 390 summary = ''
395 391 )
396 392
397 393 #-------------------------------------------------------------------------
398 394 # Process user ipythonrc-type configuration files
399 395
400 396 # turn on output trapping and log to msg.config
401 397 # remember that with debug on, trapping is actually disabled
402 398 msg.config.trap_all()
403 399
404 400 # look for rcfile in current or default directory
405 401 try:
406 402 opts_all.rcfile = filefind(opts_all.rcfile,opts_all.ipythondir)
407 403 except IOError:
408 404 if opts_all.debug: IP.InteractiveTB()
409 405 warn('Configuration file %s not found. Ignoring request.'
410 406 % (opts_all.rcfile) )
411 407
412 408 # 'profiles' are a shorthand notation for config filenames
413 409 if opts_all.profile:
414 410 try:
415 411 opts_all.rcfile = filefind('ipythonrc-' + opts_all.profile
416 412 + rc_suffix,
417 413 opts_all.ipythondir)
418 414 except IOError:
419 415 if opts_all.debug: IP.InteractiveTB()
420 416 opts.profile = '' # remove profile from options if invalid
421 417 warn('Profile configuration file %s not found. Ignoring request.'
422 418 % (opts_all.profile) )
423 419
424 420 # load the config file
425 421 rcfiledata = None
426 422 if opts_all.quick:
427 423 print 'Launching IPython in quick mode. No config file read.'
428 424 elif opts_all.classic:
429 425 print 'Launching IPython in classic mode. No config file read.'
430 426 elif opts_all.rcfile:
431 427 try:
432 428 cfg_loader = ConfigLoader(conflict)
433 429 rcfiledata = cfg_loader.load(opts_all.rcfile,typeconv,
434 430 'include',opts_all.ipythondir,
435 431 purge = 1,
436 432 unique = conflict['preserve'])
437 433 except:
438 434 IP.InteractiveTB()
439 435 warn('Problems loading configuration file '+
440 436 `opts_all.rcfile`+
441 437 '\nStarting with default -bare bones- configuration.')
442 438 else:
443 439 warn('No valid configuration file found in either currrent directory\n'+
444 440 'or in the IPython config. directory: '+`opts_all.ipythondir`+
445 441 '\nProceeding with internal defaults.')
446 442
447 443 #------------------------------------------------------------------------
448 444 # Set exception handlers in mode requested by user.
449 445 otrap = OutputTrap(trap_out=1) # trap messages from magic_xmode
450 446 IP.magic_xmode(opts_all.xmode)
451 447 otrap.release_out()
452 448
453 449 #------------------------------------------------------------------------
454 450 # Execute user config
455 451
456 452 # Create a valid config structure with the right precedence order:
457 453 # defaults < rcfile < command line. This needs to be in the instance, so
458 454 # that method calls below that rely on it find it.
459 455 IP.rc = rc_def.copy()
460 456
461 457 # Work with a local alias inside this routine to avoid unnecessary
462 458 # attribute lookups.
463 459 IP_rc = IP.rc
464 460
465 461 IP_rc.update(opts_def)
466 462 if rcfiledata:
467 463 # now we can update
468 464 IP_rc.update(rcfiledata)
469 465 IP_rc.update(opts)
470 466 IP_rc.update(rc_override)
471 467
472 468 # Store the original cmd line for reference:
473 469 IP_rc.opts = opts
474 470 IP_rc.args = args
475 471
476 472 # create a *runtime* Struct like rc for holding parameters which may be
477 473 # created and/or modified by runtime user extensions.
478 474 IP.runtime_rc = Struct()
479 475
480 476 # from this point on, all config should be handled through IP_rc,
481 477 # opts* shouldn't be used anymore.
482 478
483 479 # add personal .ipython dir to sys.path so that users can put things in
484 480 # there for customization
485 481 sys.path.append(IP_rc.ipythondir)
486 482 sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran
487 483
488 484 # update IP_rc with some special things that need manual
489 485 # tweaks. Basically options which affect other options. I guess this
490 486 # should just be written so that options are fully orthogonal and we
491 487 # wouldn't worry about this stuff!
492 488
493 489 if IP_rc.classic:
494 490 IP_rc.quick = 1
495 491 IP_rc.cache_size = 0
496 492 IP_rc.pprint = 0
497 493 IP_rc.prompt_in1 = '>>> '
498 494 IP_rc.prompt_in2 = '... '
499 495 IP_rc.prompt_out = ''
500 496 IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0'
501 497 IP_rc.colors = 'NoColor'
502 498 IP_rc.xmode = 'Plain'
503 499
504 500 # configure readline
505 501 # Define the history file for saving commands in between sessions
506 502 if IP_rc.profile:
507 503 histfname = 'history-%s' % IP_rc.profile
508 504 else:
509 505 histfname = 'history'
510 506 IP.histfile = os.path.join(opts_all.ipythondir,histfname)
511 507
512 508 # update exception handlers with rc file status
513 509 otrap.trap_out() # I don't want these messages ever.
514 510 IP.magic_xmode(IP_rc.xmode)
515 511 otrap.release_out()
516 512
517 513 # activate logging if requested and not reloading a log
518 514 if IP_rc.logplay:
519 515 IP.magic_logstart(IP_rc.logplay + ' append')
520 516 elif IP_rc.logfile:
521 517 IP.magic_logstart(IP_rc.logfile)
522 518 elif IP_rc.log:
523 519 IP.magic_logstart()
524 520
525 521 # find user editor so that it we don't have to look it up constantly
526 522 if IP_rc.editor.strip()=='0':
527 523 try:
528 524 ed = os.environ['EDITOR']
529 525 except KeyError:
530 526 if os.name == 'posix':
531 527 ed = 'vi' # the only one guaranteed to be there!
532 528 else:
533 529 ed = 'notepad' # same in Windows!
534 530 IP_rc.editor = ed
535 531
536 532 # Keep track of whether this is an embedded instance or not (useful for
537 533 # post-mortems).
538 534 IP_rc.embedded = IP.embedded
539 535
540 536 # Recursive reload
541 537 try:
542 538 from IPython import deep_reload
543 539 if IP_rc.deep_reload:
544 540 __builtin__.reload = deep_reload.reload
545 541 else:
546 542 __builtin__.dreload = deep_reload.reload
547 543 del deep_reload
548 544 except ImportError:
549 545 pass
550 546
551 547 # Save the current state of our namespace so that the interactive shell
552 548 # can later know which variables have been created by us from config files
553 549 # and loading. This way, loading a file (in any way) is treated just like
554 550 # defining things on the command line, and %who works as expected.
555 551
556 552 # DON'T do anything that affects the namespace beyond this point!
557 553 IP.internal_ns.update(__main__.__dict__)
558 554
559 555 #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who
560 556
561 557 # Now run through the different sections of the users's config
562 558 if IP_rc.debug:
563 559 print 'Trying to execute the following configuration structure:'
564 560 print '(Things listed first are deeper in the inclusion tree and get'
565 561 print 'loaded first).\n'
566 562 pprint(IP_rc.__dict__)
567 563
568 564 for mod in IP_rc.import_mod:
569 565 try:
570 566 exec 'import '+mod in IP.user_ns
571 567 except :
572 568 IP.InteractiveTB()
573 569 import_fail_info(mod)
574 570
575 571 for mod_fn in IP_rc.import_some:
576 572 if mod_fn == []: break
577 573 mod,fn = mod_fn[0],','.join(mod_fn[1:])
578 574 try:
579 575 exec 'from '+mod+' import '+fn in IP.user_ns
580 576 except :
581 577 IP.InteractiveTB()
582 578 import_fail_info(mod,fn)
583 579
584 580 for mod in IP_rc.import_all:
585 581 try:
586 582 exec 'from '+mod+' import *' in IP.user_ns
587 583 except :
588 584 IP.InteractiveTB()
589 585 import_fail_info(mod)
590 586
591 587 for code in IP_rc.execute:
592 588 try:
593 589 exec code in IP.user_ns
594 590 except:
595 591 IP.InteractiveTB()
596 592 warn('Failure executing code: ' + `code`)
597 593
598 594 # Execute the files the user wants in ipythonrc
599 595 for file in IP_rc.execfile:
600 596 try:
601 597 file = filefind(file,sys.path+[IPython_dir])
602 598 except IOError:
603 599 warn(itpl('File $file not found. Skipping it.'))
604 600 else:
605 601 IP.safe_execfile(os.path.expanduser(file),IP.user_ns)
606 602
607 603 # release stdout and stderr and save config log into a global summary
608 604 msg.config.release_all()
609 605 if IP_rc.messages:
610 606 msg.summary += msg.config.summary_all()
611 607
612 608 #------------------------------------------------------------------------
613 609 # Setup interactive session
614 610
615 611 # Now we should be fully configured. We can then execute files or load
616 612 # things only needed for interactive use. Then we'll open the shell.
617 613
618 614 # Take a snapshot of the user namespace before opening the shell. That way
619 615 # we'll be able to identify which things were interactively defined and
620 616 # which were defined through config files.
621 617 IP.user_config_ns = IP.user_ns.copy()
622 618
623 619 # Force reading a file as if it were a session log. Slower but safer.
624 620 if load_logplay:
625 621 print 'Replaying log...'
626 622 try:
627 623 if IP_rc.debug:
628 624 logplay_quiet = 0
629 625 else:
630 626 logplay_quiet = 1
631 627
632 628 msg.logplay.trap_all()
633 629 IP.safe_execfile(load_logplay,IP.user_ns,
634 630 islog = 1, quiet = logplay_quiet)
635 631 msg.logplay.release_all()
636 632 if IP_rc.messages:
637 633 msg.summary += msg.logplay.summary_all()
638 634 except:
639 635 warn('Problems replaying logfile %s.' % load_logplay)
640 636 IP.InteractiveTB()
641 637
642 638 # Load remaining files in command line
643 639 msg.user_exec.trap_all()
644 640
645 641 # Do NOT execute files named in the command line as scripts to be loaded
646 642 # by embedded instances. Doing so has the potential for an infinite
647 643 # recursion if there are exceptions thrown in the process.
648 644
649 645 # XXX FIXME: the execution of user files should be moved out to after
650 646 # ipython is fully initialized, just as if they were run via %run at the
651 647 # ipython prompt. This would also give them the benefit of ipython's
652 648 # nice tracebacks.
653 649
654 650 if not embedded and IP_rc.args:
655 651 name_save = IP.user_ns['__name__']
656 652 IP.user_ns['__name__'] = '__main__'
657 653 try:
658 654 # Set our own excepthook in case the user code tries to call it
659 655 # directly. This prevents triggering the IPython crash handler.
660 656 old_excepthook,sys.excepthook = sys.excepthook, IP.excepthook
661 657 for run in args:
662 658 IP.safe_execfile(run,IP.user_ns)
663 659 finally:
664 660 # Reset our crash handler in place
665 661 sys.excepthook = old_excepthook
666 662
667 663 IP.user_ns['__name__'] = name_save
668 664
669 665 msg.user_exec.release_all()
670 666 if IP_rc.messages:
671 667 msg.summary += msg.user_exec.summary_all()
672 668
673 669 # since we can't specify a null string on the cmd line, 0 is the equivalent:
674 670 if IP_rc.nosep:
675 671 IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0'
676 672 if IP_rc.separate_in == '0': IP_rc.separate_in = ''
677 673 if IP_rc.separate_out == '0': IP_rc.separate_out = ''
678 674 if IP_rc.separate_out2 == '0': IP_rc.separate_out2 = ''
679 675 IP_rc.separate_in = IP_rc.separate_in.replace('\\n','\n')
680 676 IP_rc.separate_out = IP_rc.separate_out.replace('\\n','\n')
681 677 IP_rc.separate_out2 = IP_rc.separate_out2.replace('\\n','\n')
682 678
683 679 # Determine how many lines at the bottom of the screen are needed for
684 680 # showing prompts, so we can know wheter long strings are to be printed or
685 681 # paged:
686 682 num_lines_bot = IP_rc.separate_in.count('\n')+1
687 683 IP_rc.screen_length = IP_rc.screen_length - num_lines_bot
688 684 # Initialize cache, set in/out prompts and printing system
689 685 IP.outputcache = CachedOutput(IP_rc.cache_size,
690 686 IP_rc.pprint,
691 687 input_sep = IP_rc.separate_in,
692 688 output_sep = IP_rc.separate_out,
693 689 output_sep2 = IP_rc.separate_out2,
694 690 ps1 = IP_rc.prompt_in1,
695 691 ps2 = IP_rc.prompt_in2,
696 692 ps_out = IP_rc.prompt_out,
697 693 user_ns = IP.user_ns,
698 694 input_hist = IP.input_hist,
699 695 pad_left = IP_rc.prompts_pad_left)
700 696
701 697 # user may have over-ridden the default print hook:
702 698 try:
703 699 IP.outputcache.__class__.display = IP.hooks.display
704 700 except AttributeError:
705 701 pass
706 702
707 703 # Set calling of pdb on exceptions
708 704 IP.InteractiveTB.call_pdb = IP_rc.pdb
709 705
710 706 # I don't like assigning globally to sys, because it means when embedding
711 707 # instances, each embedded instance overrides the previous choice. But
712 708 # sys.displayhook seems to be called internally by exec, so I don't see a
713 709 # way around it.
714 710 sys.displayhook = IP.outputcache
715 711
716 712 # we need to know globally if we're caching i/o or not
717 713 IP.do_full_cache = IP.outputcache.do_full_cache
718 714
719 715 # configure startup banner
720 716 if IP_rc.c: # regular python doesn't print the banner with -c
721 717 IP_rc.banner = 0
722 718 if IP_rc.banner:
723 719 BANN_P = IP.BANNER_PARTS
724 720 else:
725 721 BANN_P = []
726 722
727 723 if IP_rc.profile: BANN_P.append('IPython profile: %s\n' % IP_rc.profile)
728 724
729 725 # add message log (possibly empty)
730 726 if msg.summary: BANN_P.append(msg.summary)
731 727 # Final banner is a string
732 728 IP.BANNER = '\n'.join(BANN_P)
733 729
734 730 # Finalize the IPython instance. This assumes the rc structure is fully
735 731 # in place.
736 732 IP.post_config_initialization()
737 733
738 734 return IP
739 735 #************************ end of file <ipmaker.py> **************************
@@ -1,796 +1,796 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 ultraTB.py -- Spice up your tracebacks!
4 4
5 5 * ColorTB
6 6 I've always found it a bit hard to visually parse tracebacks in Python. The
7 7 ColorTB class is a solution to that problem. It colors the different parts of a
8 8 traceback in a manner similar to what you would expect from a syntax-highlighting
9 9 text editor.
10 10
11 11 Installation instructions for ColorTB:
12 12 import sys,ultraTB
13 13 sys.excepthook = ultraTB.ColorTB()
14 14
15 15 * VerboseTB
16 16 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
17 17 of useful info when a traceback occurs. Ping originally had it spit out HTML
18 18 and intended it for CGI programmers, but why should they have all the fun? I
19 19 altered it to spit out colored text to the terminal. It's a bit overwhelming,
20 20 but kind of neat, and maybe useful for long-running programs that you believe
21 21 are bug-free. If a crash *does* occur in that type of program you want details.
22 22 Give it a shot--you'll love it or you'll hate it.
23 23
24 24 Note:
25 25
26 26 The Verbose mode prints the variables currently visible where the exception
27 27 happened (shortening their strings if too long). This can potentially be
28 28 very slow, if you happen to have a huge data structure whose string
29 29 representation is complex to compute. Your computer may appear to freeze for
30 30 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
31 31 with Ctrl-C (maybe hitting it more than once).
32 32
33 33 If you encounter this kind of situation often, you may want to use the
34 34 Verbose_novars mode instead of the regular Verbose, which avoids formatting
35 35 variables (but otherwise includes the information and context given by
36 36 Verbose).
37 37
38 38
39 39 Installation instructions for ColorTB:
40 40 import sys,ultraTB
41 41 sys.excepthook = ultraTB.VerboseTB()
42 42
43 43 Note: Much of the code in this module was lifted verbatim from the standard
44 44 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
45 45
46 46 * Color schemes
47 47 The colors are defined in the class TBTools through the use of the
48 48 ColorSchemeTable class. Currently the following exist:
49 49
50 50 - NoColor: allows all of this module to be used in any terminal (the color
51 51 escapes are just dummy blank strings).
52 52
53 53 - Linux: is meant to look good in a terminal like the Linux console (black
54 54 or very dark background).
55 55
56 56 - LightBG: similar to Linux but swaps dark/light colors to be more readable
57 57 in light background terminals.
58 58
59 59 You can implement other color schemes easily, the syntax is fairly
60 60 self-explanatory. Please send back new schemes you develop to the author for
61 61 possible inclusion in future releases.
62 62
63 $Id: ultraTB.py 958 2005-12-27 23:17:51Z fperez $"""
63 $Id: ultraTB.py 965 2005-12-28 23:23:09Z fperez $"""
64 64
65 65 #*****************************************************************************
66 66 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
67 67 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
68 68 #
69 69 # Distributed under the terms of the BSD License. The full license is in
70 70 # the file COPYING, distributed as part of this software.
71 71 #*****************************************************************************
72 72
73 73 from IPython import Release
74 74 __author__ = '%s <%s>\n%s <%s>' % (Release.authors['Nathan']+
75 75 Release.authors['Fernando'])
76 76 __license__ = Release.license
77 77
78 78 # Required modules
79 79 import inspect
80 80 import keyword
81 81 import linecache
82 82 import os
83 83 import pydoc
84 84 import string
85 85 import sys
86 86 import time
87 87 import tokenize
88 88 import traceback
89 89 import types
90 90
91 91 # IPython's own modules
92 92 # Modified pdb which doesn't damage IPython's readline handling
93 93 from IPython import Debugger
94 94 from IPython.Struct import Struct
95 95 from IPython.excolors import ExceptionColors
96 96 from IPython.genutils import Term,uniq_stable,error,info
97 97
98 98 #---------------------------------------------------------------------------
99 99 # Code begins
100 100
101 101 def inspect_error():
102 102 """Print a message about internal inspect errors.
103 103
104 104 These are unfortunately quite common."""
105 105
106 106 error('Internal Python error in the inspect module.\n'
107 107 'Below is the traceback from this internal error.\n')
108 108
109 109 class TBTools:
110 110 """Basic tools used by all traceback printer classes."""
111 111
112 112 def __init__(self,color_scheme = 'NoColor',call_pdb=False):
113 113 # Whether to call the interactive pdb debugger after printing
114 114 # tracebacks or not
115 115 self.call_pdb = call_pdb
116 116
117 117 # Create color table
118 118 self.color_scheme_table = ExceptionColors
119 119
120 120 self.set_colors(color_scheme)
121 121 self.old_scheme = color_scheme # save initial value for toggles
122 122
123 123 if call_pdb:
124 124 self.pdb = Debugger.Pdb(self.color_scheme_table.active_scheme_name)
125 125 else:
126 126 self.pdb = None
127 127
128 128 def set_colors(self,*args,**kw):
129 129 """Shorthand access to the color table scheme selector method."""
130 130
131 131 self.color_scheme_table.set_active_scheme(*args,**kw)
132 132 # for convenience, set Colors to the active scheme
133 133 self.Colors = self.color_scheme_table.active_colors
134 134
135 135 def color_toggle(self):
136 136 """Toggle between the currently active color scheme and NoColor."""
137 137
138 138 if self.color_scheme_table.active_scheme_name == 'NoColor':
139 139 self.color_scheme_table.set_active_scheme(self.old_scheme)
140 140 self.Colors = self.color_scheme_table.active_colors
141 141 else:
142 142 self.old_scheme = self.color_scheme_table.active_scheme_name
143 143 self.color_scheme_table.set_active_scheme('NoColor')
144 144 self.Colors = self.color_scheme_table.active_colors
145 145
146 146 #---------------------------------------------------------------------------
147 147 class ListTB(TBTools):
148 148 """Print traceback information from a traceback list, with optional color.
149 149
150 150 Calling: requires 3 arguments:
151 151 (etype, evalue, elist)
152 152 as would be obtained by:
153 153 etype, evalue, tb = sys.exc_info()
154 154 if tb:
155 155 elist = traceback.extract_tb(tb)
156 156 else:
157 157 elist = None
158 158
159 159 It can thus be used by programs which need to process the traceback before
160 160 printing (such as console replacements based on the code module from the
161 161 standard library).
162 162
163 163 Because they are meant to be called without a full traceback (only a
164 164 list), instances of this class can't call the interactive pdb debugger."""
165 165
166 166 def __init__(self,color_scheme = 'NoColor'):
167 167 TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0)
168 168
169 169 def __call__(self, etype, value, elist):
170 170 print >> Term.cerr, self.text(etype,value,elist)
171 171
172 172 def text(self,etype, value, elist,context=5):
173 173 """Return a color formatted string with the traceback info."""
174 174
175 175 Colors = self.Colors
176 176 out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)]
177 177 if elist:
178 178 out_string.append('Traceback %s(most recent call last)%s:' % \
179 179 (Colors.normalEm, Colors.Normal) + '\n')
180 180 out_string.extend(self._format_list(elist))
181 181 lines = self._format_exception_only(etype, value)
182 182 for line in lines[:-1]:
183 183 out_string.append(" "+line)
184 184 out_string.append(lines[-1])
185 185 return ''.join(out_string)
186 186
187 187 def _format_list(self, extracted_list):
188 188 """Format a list of traceback entry tuples for printing.
189 189
190 190 Given a list of tuples as returned by extract_tb() or
191 191 extract_stack(), return a list of strings ready for printing.
192 192 Each string in the resulting list corresponds to the item with the
193 193 same index in the argument list. Each string ends in a newline;
194 194 the strings may contain internal newlines as well, for those items
195 195 whose source text line is not None.
196 196
197 197 Lifted almost verbatim from traceback.py
198 198 """
199 199
200 200 Colors = self.Colors
201 201 list = []
202 202 for filename, lineno, name, line in extracted_list[:-1]:
203 203 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
204 204 (Colors.filename, filename, Colors.Normal,
205 205 Colors.lineno, lineno, Colors.Normal,
206 206 Colors.name, name, Colors.Normal)
207 207 if line:
208 208 item = item + ' %s\n' % line.strip()
209 209 list.append(item)
210 210 # Emphasize the last entry
211 211 filename, lineno, name, line = extracted_list[-1]
212 212 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
213 213 (Colors.normalEm,
214 214 Colors.filenameEm, filename, Colors.normalEm,
215 215 Colors.linenoEm, lineno, Colors.normalEm,
216 216 Colors.nameEm, name, Colors.normalEm,
217 217 Colors.Normal)
218 218 if line:
219 219 item = item + '%s %s%s\n' % (Colors.line, line.strip(),
220 220 Colors.Normal)
221 221 list.append(item)
222 222 return list
223 223
224 224 def _format_exception_only(self, etype, value):
225 225 """Format the exception part of a traceback.
226 226
227 227 The arguments are the exception type and value such as given by
228 228 sys.last_type and sys.last_value. The return value is a list of
229 229 strings, each ending in a newline. Normally, the list contains a
230 230 single string; however, for SyntaxError exceptions, it contains
231 231 several lines that (when printed) display detailed information
232 232 about where the syntax error occurred. The message indicating
233 233 which exception occurred is the always last string in the list.
234 234
235 235 Also lifted nearly verbatim from traceback.py
236 236 """
237 237
238 238 Colors = self.Colors
239 239 list = []
240 240 if type(etype) == types.ClassType:
241 241 stype = Colors.excName + etype.__name__ + Colors.Normal
242 242 else:
243 243 stype = etype # String exceptions don't get special coloring
244 244 if value is None:
245 245 list.append( str(stype) + '\n')
246 246 else:
247 247 if etype is SyntaxError:
248 248 try:
249 249 msg, (filename, lineno, offset, line) = value
250 250 except:
251 251 pass
252 252 else:
253 253 #print 'filename is',filename # dbg
254 254 if not filename: filename = "<string>"
255 255 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
256 256 (Colors.normalEm,
257 257 Colors.filenameEm, filename, Colors.normalEm,
258 258 Colors.linenoEm, lineno, Colors.Normal ))
259 259 if line is not None:
260 260 i = 0
261 261 while i < len(line) and line[i].isspace():
262 262 i = i+1
263 263 list.append('%s %s%s\n' % (Colors.line,
264 264 line.strip(),
265 265 Colors.Normal))
266 266 if offset is not None:
267 267 s = ' '
268 268 for c in line[i:offset-1]:
269 269 if c.isspace():
270 270 s = s + c
271 271 else:
272 272 s = s + ' '
273 273 list.append('%s%s^%s\n' % (Colors.caret, s,
274 274 Colors.Normal) )
275 275 value = msg
276 276 s = self._some_str(value)
277 277 if s:
278 278 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
279 279 Colors.Normal, s))
280 280 else:
281 281 list.append('%s\n' % str(stype))
282 282 return list
283 283
284 284 def _some_str(self, value):
285 285 # Lifted from traceback.py
286 286 try:
287 287 return str(value)
288 288 except:
289 289 return '<unprintable %s object>' % type(value).__name__
290 290
291 291 #----------------------------------------------------------------------------
292 292 class VerboseTB(TBTools):
293 293 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
294 294 of HTML. Requires inspect and pydoc. Crazy, man.
295 295
296 296 Modified version which optionally strips the topmost entries from the
297 297 traceback, to be used with alternate interpreters (because their own code
298 298 would appear in the traceback)."""
299 299
300 300 def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0,
301 301 call_pdb = 0, include_vars=1):
302 302 """Specify traceback offset, headers and color scheme.
303 303
304 304 Define how many frames to drop from the tracebacks. Calling it with
305 305 tb_offset=1 allows use of this handler in interpreters which will have
306 306 their own code at the top of the traceback (VerboseTB will first
307 307 remove that frame before printing the traceback info)."""
308 308 TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb)
309 309 self.tb_offset = tb_offset
310 310 self.long_header = long_header
311 311 self.include_vars = include_vars
312 312
313 313 def text(self, etype, evalue, etb, context=5):
314 314 """Return a nice text document describing the traceback."""
315 315
316 316 # some locals
317 317 Colors = self.Colors # just a shorthand + quicker name lookup
318 318 ColorsNormal = Colors.Normal # used a lot
319 319 indent_size = 8 # we need some space to put line numbers before
320 320 indent = ' '*indent_size
321 321 numbers_width = indent_size - 1 # leave space between numbers & code
322 322 text_repr = pydoc.text.repr
323 323 exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal)
324 324 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
325 325 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
326 326
327 327 # some internal-use functions
328 328 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
329 329 def nullrepr(value, repr=text_repr): return ''
330 330
331 331 # meat of the code begins
332 332 if type(etype) is types.ClassType:
333 333 etype = etype.__name__
334 334
335 335 if self.long_header:
336 336 # Header with the exception type, python version, and date
337 337 pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable
338 338 date = time.ctime(time.time())
339 339
340 340 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
341 341 exc, ' '*(75-len(str(etype))-len(pyver)),
342 342 pyver, string.rjust(date, 75) )
343 343 head += "\nA problem occured executing Python code. Here is the sequence of function"\
344 344 "\ncalls leading up to the error, with the most recent (innermost) call last."
345 345 else:
346 346 # Simplified header
347 347 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
348 348 string.rjust('Traceback (most recent call last)',
349 349 75 - len(str(etype)) ) )
350 350 frames = []
351 351 # Flush cache before calling inspect. This helps alleviate some of the
352 352 # problems with python 2.3's inspect.py.
353 353 linecache.checkcache()
354 354 # Drop topmost frames if requested
355 355 try:
356 356 records = inspect.getinnerframes(etb, context)[self.tb_offset:]
357 357 except:
358 358
359 359 # FIXME: I've been getting many crash reports from python 2.3
360 360 # users, traceable to inspect.py. If I can find a small test-case
361 361 # to reproduce this, I should either write a better workaround or
362 362 # file a bug report against inspect (if that's the real problem).
363 363 # So far, I haven't been able to find an isolated example to
364 364 # reproduce the problem.
365 365 inspect_error()
366 366 traceback.print_exc(file=Term.cerr)
367 367 info('\nUnfortunately, your original traceback can not be constructed.\n')
368 368 return ''
369 369
370 370 # build some color string templates outside these nested loops
371 371 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
372 372 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
373 373 ColorsNormal)
374 374 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
375 375 (Colors.vName, Colors.valEm, ColorsNormal)
376 376 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
377 377 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
378 378 Colors.vName, ColorsNormal)
379 379 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
380 380 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
381 381 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
382 382 ColorsNormal)
383 383
384 384 # now, loop over all records printing context and info
385 385 abspath = os.path.abspath
386 386 for frame, file, lnum, func, lines, index in records:
387 387 #print '*** record:',file,lnum,func,lines,index # dbg
388 388 try:
389 389 file = file and abspath(file) or '?'
390 390 except OSError:
391 391 # if file is '<console>' or something not in the filesystem,
392 392 # the abspath call will throw an OSError. Just ignore it and
393 393 # keep the original file string.
394 394 pass
395 395 link = tpl_link % file
396 396 try:
397 397 args, varargs, varkw, locals = inspect.getargvalues(frame)
398 398 except:
399 399 # This can happen due to a bug in python2.3. We should be
400 400 # able to remove this try/except when 2.4 becomes a
401 401 # requirement. Bug details at http://python.org/sf/1005466
402 402 inspect_error()
403 403 traceback.print_exc(file=Term.cerr)
404 404 info("\nIPython's exception reporting continues...\n")
405 405
406 406 if func == '?':
407 407 call = ''
408 408 else:
409 409 # Decide whether to include variable details or not
410 410 var_repr = self.include_vars and eqrepr or nullrepr
411 411 try:
412 412 call = tpl_call % (func,inspect.formatargvalues(args,
413 413 varargs, varkw,
414 414 locals,formatvalue=var_repr))
415 415 except KeyError:
416 416 # Very odd crash from inspect.formatargvalues(). The
417 417 # scenario under which it appeared was a call to
418 418 # view(array,scale) in NumTut.view.view(), where scale had
419 419 # been defined as a scalar (it should be a tuple). Somehow
420 420 # inspect messes up resolving the argument list of view()
421 421 # and barfs out. At some point I should dig into this one
422 422 # and file a bug report about it.
423 423 inspect_error()
424 424 traceback.print_exc(file=Term.cerr)
425 425 info("\nIPython's exception reporting continues...\n")
426 426 call = tpl_call_fail % func
427 427
428 428 # Initialize a list of names on the current line, which the
429 429 # tokenizer below will populate.
430 430 names = []
431 431
432 432 def tokeneater(token_type, token, start, end, line):
433 433 """Stateful tokeneater which builds dotted names.
434 434
435 435 The list of names it appends to (from the enclosing scope) can
436 436 contain repeated composite names. This is unavoidable, since
437 437 there is no way to disambguate partial dotted structures until
438 438 the full list is known. The caller is responsible for pruning
439 439 the final list of duplicates before using it."""
440 440
441 441 # build composite names
442 442 if token == '.':
443 443 try:
444 444 names[-1] += '.'
445 445 # store state so the next token is added for x.y.z names
446 446 tokeneater.name_cont = True
447 447 return
448 448 except IndexError:
449 449 pass
450 450 if token_type == tokenize.NAME and token not in keyword.kwlist:
451 451 if tokeneater.name_cont:
452 452 # Dotted names
453 453 names[-1] += token
454 454 tokeneater.name_cont = False
455 455 else:
456 456 # Regular new names. We append everything, the caller
457 457 # will be responsible for pruning the list later. It's
458 458 # very tricky to try to prune as we go, b/c composite
459 459 # names can fool us. The pruning at the end is easy
460 460 # to do (or the caller can print a list with repeated
461 461 # names if so desired.
462 462 names.append(token)
463 463 elif token_type == tokenize.NEWLINE:
464 464 raise IndexError
465 465 # we need to store a bit of state in the tokenizer to build
466 466 # dotted names
467 467 tokeneater.name_cont = False
468 468
469 469 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
470 470 line = getline(file, lnum[0])
471 471 lnum[0] += 1
472 472 return line
473 473
474 474 # Build the list of names on this line of code where the exception
475 475 # occurred.
476 476 try:
477 477 # This builds the names list in-place by capturing it from the
478 478 # enclosing scope.
479 479 tokenize.tokenize(linereader, tokeneater)
480 480 except IndexError:
481 481 # signals exit of tokenizer
482 482 pass
483 483 except tokenize.TokenError,msg:
484 484 _m = ("An unexpected error occurred while tokenizing input\n"
485 485 "The following traceback may be corrupted or invalid\n"
486 486 "The error message is: %s\n" % msg)
487 487 error(_m)
488 488
489 489 # prune names list of duplicates, but keep the right order
490 490 unique_names = uniq_stable(names)
491 491
492 492 # Start loop over vars
493 493 lvals = []
494 494 if self.include_vars:
495 495 for name_full in unique_names:
496 496 name_base = name_full.split('.',1)[0]
497 497 if name_base in frame.f_code.co_varnames:
498 498 if locals.has_key(name_base):
499 499 try:
500 500 value = repr(eval(name_full,locals))
501 501 except:
502 502 value = undefined
503 503 else:
504 504 value = undefined
505 505 name = tpl_local_var % name_full
506 506 else:
507 507 if frame.f_globals.has_key(name_base):
508 508 try:
509 509 value = repr(eval(name_full,frame.f_globals))
510 510 except:
511 511 value = undefined
512 512 else:
513 513 value = undefined
514 514 name = tpl_global_var % name_full
515 515 lvals.append(tpl_name_val % (name,value))
516 516 if lvals:
517 517 lvals = '%s%s' % (indent,em_normal.join(lvals))
518 518 else:
519 519 lvals = ''
520 520
521 521 level = '%s %s\n' % (link,call)
522 522 excerpt = []
523 523 if index is not None:
524 524 i = lnum - index
525 525 for line in lines:
526 526 if i == lnum:
527 527 # This is the line with the error
528 528 pad = numbers_width - len(str(i))
529 529 if pad >= 3:
530 530 marker = '-'*(pad-3) + '-> '
531 531 elif pad == 2:
532 532 marker = '> '
533 533 elif pad == 1:
534 534 marker = '>'
535 535 else:
536 536 marker = ''
537 537 num = '%s%s' % (marker,i)
538 538 line = tpl_line_em % (num,line)
539 539 else:
540 540 num = '%*s' % (numbers_width,i)
541 541 line = tpl_line % (num,line)
542 542
543 543 excerpt.append(line)
544 544 if self.include_vars and i == lnum:
545 545 excerpt.append('%s\n' % lvals)
546 546 i += 1
547 547 frames.append('%s%s' % (level,''.join(excerpt)) )
548 548
549 549 # Get (safely) a string form of the exception info
550 550 try:
551 551 etype_str,evalue_str = map(str,(etype,evalue))
552 552 except:
553 553 # User exception is improperly defined.
554 554 etype,evalue = str,sys.exc_info()[:2]
555 555 etype_str,evalue_str = map(str,(etype,evalue))
556 556 # ... and format it
557 557 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
558 558 ColorsNormal, evalue_str)]
559 559 if type(evalue) is types.InstanceType:
560 560 try:
561 561 names = [w for w in dir(evalue) if isinstance(w, basestring)]
562 562 except:
563 563 # Every now and then, an object with funny inernals blows up
564 564 # when dir() is called on it. We do the best we can to report
565 565 # the problem and continue
566 566 _m = '%sException reporting error (object with broken dir())%s:'
567 567 exception.append(_m % (Colors.excName,ColorsNormal))
568 568 etype_str,evalue_str = map(str,sys.exc_info()[:2])
569 569 exception.append('%s%s%s: %s' % (Colors.excName,etype_str,
570 570 ColorsNormal, evalue_str))
571 571 names = []
572 572 for name in names:
573 573 value = text_repr(getattr(evalue, name))
574 574 exception.append('\n%s%s = %s' % (indent, name, value))
575 575 # return all our info assembled as a single string
576 576 return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
577 577
578 578 def debugger(self):
579 579 """Call up the pdb debugger if desired, always clean up the tb reference.
580 580
581 581 If the call_pdb flag is set, the pdb interactive debugger is
582 582 invoked. In all cases, the self.tb reference to the current traceback
583 583 is deleted to prevent lingering references which hamper memory
584 584 management.
585 585
586 586 Note that each call to pdb() does an 'import readline', so if your app
587 587 requires a special setup for the readline completers, you'll have to
588 588 fix that by hand after invoking the exception handler."""
589 589
590 590 if self.call_pdb:
591 591 if self.pdb is None:
592 592 self.pdb = Debugger.Pdb(
593 593 self.color_scheme_table.active_scheme_name)
594 594 # the system displayhook may have changed, restore the original
595 595 # for pdb
596 596 dhook = sys.displayhook
597 597 sys.displayhook = sys.__displayhook__
598 598 self.pdb.reset()
599 599 # Find the right frame so we don't pop up inside ipython itself
600 600 etb = self.tb
601 601 while self.tb.tb_next is not None:
602 602 self.tb = self.tb.tb_next
603 603 try:
604 604 if etb and etb.tb_next:
605 605 etb = etb.tb_next
606 606 self.pdb.botframe = etb.tb_frame
607 607 self.pdb.interaction(self.tb.tb_frame, self.tb)
608 608 except:
609 609 print '*** ERROR ***'
610 610 print 'This version of pdb has a bug and crashed.'
611 611 print 'Returning to IPython...'
612 612 sys.displayhook = dhook
613 613 del self.tb
614 614
615 615 def handler(self, info=None):
616 616 (etype, evalue, etb) = info or sys.exc_info()
617 617 self.tb = etb
618 618 print >> Term.cerr, self.text(etype, evalue, etb)
619 619
620 620 # Changed so an instance can just be called as VerboseTB_inst() and print
621 621 # out the right info on its own.
622 622 def __call__(self, etype=None, evalue=None, etb=None):
623 623 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
624 if etb is not None:
625 self.handler((etype, evalue, etb))
626 else:
624 if etb is None:
627 625 self.handler()
626 else:
627 self.handler((etype, evalue, etb))
628 628 self.debugger()
629 629
630 630 #----------------------------------------------------------------------------
631 631 class FormattedTB(VerboseTB,ListTB):
632 632 """Subclass ListTB but allow calling with a traceback.
633 633
634 634 It can thus be used as a sys.excepthook for Python > 2.1.
635 635
636 636 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
637 637
638 638 Allows a tb_offset to be specified. This is useful for situations where
639 639 one needs to remove a number of topmost frames from the traceback (such as
640 640 occurs with python programs that themselves execute other python code,
641 641 like Python shells). """
642 642
643 643 def __init__(self, mode = 'Plain', color_scheme='Linux',
644 644 tb_offset = 0,long_header=0,call_pdb=0,include_vars=0):
645 645
646 646 # NEVER change the order of this list. Put new modes at the end:
647 647 self.valid_modes = ['Plain','Context','Verbose']
648 648 self.verbose_modes = self.valid_modes[1:3]
649 649
650 650 VerboseTB.__init__(self,color_scheme,tb_offset,long_header,
651 651 call_pdb=call_pdb,include_vars=include_vars)
652 652 self.set_mode(mode)
653 653
654 654 def _extract_tb(self,tb):
655 655 if tb:
656 656 return traceback.extract_tb(tb)
657 657 else:
658 658 return None
659 659
660 660 def text(self, etype, value, tb,context=5,mode=None):
661 661 """Return formatted traceback.
662 662
663 663 If the optional mode parameter is given, it overrides the current
664 664 mode."""
665 665
666 666 if mode is None:
667 667 mode = self.mode
668 668 if mode in self.verbose_modes:
669 669 # verbose modes need a full traceback
670 670 return VerboseTB.text(self,etype, value, tb,context=5)
671 671 else:
672 672 # We must check the source cache because otherwise we can print
673 673 # out-of-date source code.
674 674 linecache.checkcache()
675 675 # Now we can extract and format the exception
676 676 elist = self._extract_tb(tb)
677 677 if len(elist) > self.tb_offset:
678 678 del elist[:self.tb_offset]
679 679 return ListTB.text(self,etype,value,elist)
680 680
681 681 def set_mode(self,mode=None):
682 682 """Switch to the desired mode.
683 683
684 684 If mode is not specified, cycles through the available modes."""
685 685
686 686 if not mode:
687 687 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
688 688 len(self.valid_modes)
689 689 self.mode = self.valid_modes[new_idx]
690 690 elif mode not in self.valid_modes:
691 691 raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
692 692 'Valid modes: '+str(self.valid_modes)
693 693 else:
694 694 self.mode = mode
695 695 # include variable details only in 'Verbose' mode
696 696 self.include_vars = (self.mode == self.valid_modes[2])
697 697
698 698 # some convenient shorcuts
699 699 def plain(self):
700 700 self.set_mode(self.valid_modes[0])
701 701
702 702 def context(self):
703 703 self.set_mode(self.valid_modes[1])
704 704
705 705 def verbose(self):
706 706 self.set_mode(self.valid_modes[2])
707 707
708 708 #----------------------------------------------------------------------------
709 709 class AutoFormattedTB(FormattedTB):
710 710 """A traceback printer which can be called on the fly.
711 711
712 712 It will find out about exceptions by itself.
713 713
714 714 A brief example:
715 715
716 716 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
717 717 try:
718 718 ...
719 719 except:
720 720 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
721 721 """
722 722 def __call__(self,etype=None,evalue=None,etb=None,
723 723 out=None,tb_offset=None):
724 724 """Print out a formatted exception traceback.
725 725
726 726 Optional arguments:
727 727 - out: an open file-like object to direct output to.
728 728
729 729 - tb_offset: the number of frames to skip over in the stack, on a
730 730 per-call basis (this overrides temporarily the instance's tb_offset
731 731 given at initialization time. """
732 732
733 733 if out is None:
734 734 out = Term.cerr
735 735 if tb_offset is not None:
736 736 tb_offset, self.tb_offset = self.tb_offset, tb_offset
737 737 print >> out, self.text(etype, evalue, etb)
738 738 self.tb_offset = tb_offset
739 739 else:
740 740 print >> out, self.text(etype, evalue, etb)
741 741 self.debugger()
742 742
743 743 def text(self,etype=None,value=None,tb=None,context=5,mode=None):
744 744 if etype is None:
745 745 etype,value,tb = sys.exc_info()
746 746 self.tb = tb
747 747 return FormattedTB.text(self,etype,value,tb,context=5,mode=mode)
748 748
749 749 #---------------------------------------------------------------------------
750 750 # A simple class to preserve Nathan's original functionality.
751 751 class ColorTB(FormattedTB):
752 752 """Shorthand to initialize a FormattedTB in Linux colors mode."""
753 753 def __init__(self,color_scheme='Linux',call_pdb=0):
754 754 FormattedTB.__init__(self,color_scheme=color_scheme,
755 755 call_pdb=call_pdb)
756 756
757 757 #----------------------------------------------------------------------------
758 758 # module testing (minimal)
759 759 if __name__ == "__main__":
760 760 def spam(c, (d, e)):
761 761 x = c + d
762 762 y = c * d
763 763 foo(x, y)
764 764
765 765 def foo(a, b, bar=1):
766 766 eggs(a, b + bar)
767 767
768 768 def eggs(f, g, z=globals()):
769 769 h = f + g
770 770 i = f - g
771 771 return h / i
772 772
773 773 print ''
774 774 print '*** Before ***'
775 775 try:
776 776 print spam(1, (2, 3))
777 777 except:
778 778 traceback.print_exc()
779 779 print ''
780 780
781 781 handler = ColorTB()
782 782 print '*** ColorTB ***'
783 783 try:
784 784 print spam(1, (2, 3))
785 785 except:
786 786 apply(handler, sys.exc_info() )
787 787 print ''
788 788
789 789 handler = VerboseTB()
790 790 print '*** VerboseTB ***'
791 791 try:
792 792 print spam(1, (2, 3))
793 793 except:
794 794 apply(handler, sys.exc_info() )
795 795 print ''
796 796
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now