##// END OF EJS Templates
Fixes for handling of global variables in embedded ipython instances (I ran...
fperez -
Show More

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

@@ -1,881 +1,880 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 882 2005-09-20 23:17:41Z fperez $"""
7 $Id: Shell.py 921 2005-11-13 06:51:34Z 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 sys
24 24 import os
25 25 import code
26 26 import threading
27 27 import signal
28 28
29 29 import IPython
30 30 from IPython.iplib import InteractiveShell
31 31 from IPython.ipmaker import make_IPython
32 32 from IPython.genutils import Term,warn,error,flag_calls
33 33 from IPython.Struct import Struct
34 34 from IPython.Magic import Magic
35 35 from IPython import ultraTB
36 36
37 37 # global flag to pass around information about Ctrl-C without exceptions
38 38 KBINT = False
39 39
40 40 # global flag to turn on/off Tk support.
41 41 USE_TK = False
42 42
43 43 #-----------------------------------------------------------------------------
44 44 # This class is trivial now, but I want to have it in to publish a clean
45 45 # interface. Later when the internals are reorganized, code that uses this
46 46 # shouldn't have to change.
47 47
48 48 class IPShell:
49 49 """Create an IPython instance."""
50 50
51 51 def __init__(self,argv=None,user_ns=None,debug=1,
52 52 shell_class=InteractiveShell):
53 53 self.IP = make_IPython(argv,user_ns=user_ns,debug=debug,
54 54 shell_class=shell_class)
55 55
56 56 def mainloop(self,sys_exit=0,banner=None):
57 57 self.IP.mainloop(banner)
58 58 if sys_exit:
59 59 sys.exit()
60 60
61 61 #-----------------------------------------------------------------------------
62 62 class IPShellEmbed:
63 63 """Allow embedding an IPython shell into a running program.
64 64
65 65 Instances of this class are callable, with the __call__ method being an
66 66 alias to the embed() method of an InteractiveShell instance.
67 67
68 68 Usage (see also the example-embed.py file for a running example):
69 69
70 70 ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override])
71 71
72 72 - argv: list containing valid command-line options for IPython, as they
73 73 would appear in sys.argv[1:].
74 74
75 75 For example, the following command-line options:
76 76
77 77 $ ipython -prompt_in1 'Input <\\#>' -colors LightBG
78 78
79 79 would be passed in the argv list as:
80 80
81 81 ['-prompt_in1','Input <\\#>','-colors','LightBG']
82 82
83 83 - banner: string which gets printed every time the interpreter starts.
84 84
85 85 - exit_msg: string which gets printed every time the interpreter exits.
86 86
87 87 - rc_override: a dict or Struct of configuration options such as those
88 88 used by IPython. These options are read from your ~/.ipython/ipythonrc
89 89 file when the Shell object is created. Passing an explicit rc_override
90 90 dict with any options you want allows you to override those values at
91 91 creation time without having to modify the file. This way you can create
92 92 embeddable instances configured in any way you want without editing any
93 93 global files (thus keeping your interactive IPython configuration
94 94 unchanged).
95 95
96 96 Then the ipshell instance can be called anywhere inside your code:
97 97
98 98 ipshell(header='') -> Opens up an IPython shell.
99 99
100 100 - header: string printed by the IPython shell upon startup. This can let
101 101 you know where in your code you are when dropping into the shell. Note
102 102 that 'banner' gets prepended to all calls, so header is used for
103 103 location-specific information.
104 104
105 105 For more details, see the __call__ method below.
106 106
107 107 When the IPython shell is exited with Ctrl-D, normal program execution
108 108 resumes.
109 109
110 110 This functionality was inspired by a posting on comp.lang.python by cmkl
111 111 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
112 112 by the IDL stop/continue commands."""
113 113
114 114 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None):
115 115 """Note that argv here is a string, NOT a list."""
116 116 self.set_banner(banner)
117 117 self.set_exit_msg(exit_msg)
118 118 self.set_dummy_mode(0)
119 119
120 120 # sys.displayhook is a global, we need to save the user's original
121 121 # Don't rely on __displayhook__, as the user may have changed that.
122 122 self.sys_displayhook_ori = sys.displayhook
123 123
124 124 # save readline completer status
125 125 try:
126 126 #print 'Save completer',sys.ipcompleter # dbg
127 127 self.sys_ipcompleter_ori = sys.ipcompleter
128 128 except:
129 129 pass # not nested with IPython
130 130
131 131 # FIXME. Passing user_ns breaks namespace handling.
132 132 #self.IP = make_IPython(argv,user_ns=__main__.__dict__)
133 133 self.IP = make_IPython(argv,rc_override=rc_override,embedded=True)
134 134
135 self.IP.name_space_init()
136 135 # mark this as an embedded instance so we know if we get a crash
137 136 # post-mortem
138 137 self.IP.rc.embedded = 1
139 138 # copy our own displayhook also
140 139 self.sys_displayhook_embed = sys.displayhook
141 140 # and leave the system's display hook clean
142 141 sys.displayhook = self.sys_displayhook_ori
143 142 # don't use the ipython crash handler so that user exceptions aren't
144 143 # trapped
145 144 sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors,
146 145 mode = self.IP.rc.xmode,
147 146 call_pdb = self.IP.rc.pdb)
148 147 self.restore_system_completer()
149 148
150 149 def restore_system_completer(self):
151 150 """Restores the readline completer which was in place.
152 151
153 152 This allows embedded IPython within IPython not to disrupt the
154 153 parent's completion.
155 154 """
156 155
157 156 try:
158 157 self.IP.readline.set_completer(self.sys_ipcompleter_ori)
159 158 sys.ipcompleter = self.sys_ipcompleter_ori
160 159 except:
161 160 pass
162 161
163 162 def __call__(self,header='',local_ns=None,global_ns=None,dummy=None):
164 163 """Activate the interactive interpreter.
165 164
166 165 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
167 166 the interpreter shell with the given local and global namespaces, and
168 167 optionally print a header string at startup.
169 168
170 169 The shell can be globally activated/deactivated using the
171 170 set/get_dummy_mode methods. This allows you to turn off a shell used
172 171 for debugging globally.
173 172
174 173 However, *each* time you call the shell you can override the current
175 174 state of dummy_mode with the optional keyword parameter 'dummy'. For
176 175 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
177 176 can still have a specific call work by making it as IPShell(dummy=0).
178 177
179 178 The optional keyword parameter dummy controls whether the call
180 179 actually does anything. """
181 180
182 181 # Allow the dummy parameter to override the global __dummy_mode
183 182 if dummy or (dummy != 0 and self.__dummy_mode):
184 183 return
185 184
186 185 # Set global subsystems (display,completions) to our values
187 186 sys.displayhook = self.sys_displayhook_embed
188 187 if self.IP.has_readline:
189 188 self.IP.readline.set_completer(self.IP.Completer.complete)
190 189
191 190 if self.banner and header:
192 191 format = '%s\n%s\n'
193 192 else:
194 193 format = '%s%s\n'
195 194 banner = format % (self.banner,header)
196 195
197 196 # Call the embedding code with a stack depth of 1 so it can skip over
198 197 # our call and get the original caller's namespaces.
199 198 self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1)
200 199
201 200 if self.exit_msg:
202 201 print self.exit_msg
203 202
204 203 # Restore global systems (display, completion)
205 204 sys.displayhook = self.sys_displayhook_ori
206 205 self.restore_system_completer()
207 206
208 207 def set_dummy_mode(self,dummy):
209 208 """Sets the embeddable shell's dummy mode parameter.
210 209
211 210 set_dummy_mode(dummy): dummy = 0 or 1.
212 211
213 212 This parameter is persistent and makes calls to the embeddable shell
214 213 silently return without performing any action. This allows you to
215 214 globally activate or deactivate a shell you're using with a single call.
216 215
217 216 If you need to manually"""
218 217
219 218 if dummy not in [0,1]:
220 219 raise ValueError,'dummy parameter must be 0 or 1'
221 220 self.__dummy_mode = dummy
222 221
223 222 def get_dummy_mode(self):
224 223 """Return the current value of the dummy mode parameter.
225 224 """
226 225 return self.__dummy_mode
227 226
228 227 def set_banner(self,banner):
229 228 """Sets the global banner.
230 229
231 230 This banner gets prepended to every header printed when the shell
232 231 instance is called."""
233 232
234 233 self.banner = banner
235 234
236 235 def set_exit_msg(self,exit_msg):
237 236 """Sets the global exit_msg.
238 237
239 238 This exit message gets printed upon exiting every time the embedded
240 239 shell is called. It is None by default. """
241 240
242 241 self.exit_msg = exit_msg
243 242
244 243 #-----------------------------------------------------------------------------
245 244 def sigint_handler (signum,stack_frame):
246 245 """Sigint handler for threaded apps.
247 246
248 247 This is a horrible hack to pass information about SIGINT _without_ using
249 248 exceptions, since I haven't been able to properly manage cross-thread
250 249 exceptions in GTK/WX. In fact, I don't think it can be done (or at least
251 250 that's my understanding from a c.l.py thread where this was discussed)."""
252 251
253 252 global KBINT
254 253
255 254 print '\nKeyboardInterrupt - Press <Enter> to continue.',
256 255 Term.cout.flush()
257 256 # Set global flag so that runsource can know that Ctrl-C was hit
258 257 KBINT = True
259 258
260 259 class MTInteractiveShell(InteractiveShell):
261 260 """Simple multi-threaded shell."""
262 261
263 262 # Threading strategy taken from:
264 263 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian
265 264 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
266 265 # from the pygtk mailing list, to avoid lockups with system calls.
267 266
268 267 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
269 268 user_ns = None, banner2='',**kw):
270 269 """Similar to the normal InteractiveShell, but with threading control"""
271 270
272 271 IPython.iplib.InteractiveShell.__init__(self,name,usage,rc,user_ns,banner2)
273 272
274 273 # Locking control variable
275 274 self.thread_ready = threading.Condition()
276 275
277 276 # Stuff to do at closing time
278 277 self._kill = False
279 278 on_kill = kw.get('on_kill')
280 279 if on_kill is None:
281 280 on_kill = []
282 281 # Check that all things to kill are callable:
283 282 for t in on_kill:
284 283 if not callable(t):
285 284 raise TypeError,'on_kill must be a list of callables'
286 285 self.on_kill = on_kill
287 286
288 287 def runsource(self, source, filename="<input>", symbol="single"):
289 288 """Compile and run some source in the interpreter.
290 289
291 290 Modified version of code.py's runsource(), to handle threading issues.
292 291 See the original for full docstring details."""
293 292
294 293 global KBINT
295 294
296 295 # If Ctrl-C was typed, we reset the flag and return right away
297 296 if KBINT:
298 297 KBINT = False
299 298 return False
300 299
301 300 try:
302 301 code = self.compile(source, filename, symbol)
303 302 except (OverflowError, SyntaxError, ValueError):
304 303 # Case 1
305 304 self.showsyntaxerror(filename)
306 305 return False
307 306
308 307 if code is None:
309 308 # Case 2
310 309 return True
311 310
312 311 # Case 3
313 312 # Store code in self, so the execution thread can handle it
314 313 self.thread_ready.acquire()
315 314 self.code_to_run = code
316 315 self.thread_ready.wait() # Wait until processed in timeout interval
317 316 self.thread_ready.release()
318 317
319 318 return False
320 319
321 320 def runcode(self):
322 321 """Execute a code object.
323 322
324 323 Multithreaded wrapper around IPython's runcode()."""
325 324
326 325 # lock thread-protected stuff
327 326 self.thread_ready.acquire()
328 327
329 328 # Install sigint handler
330 329 try:
331 330 signal.signal(signal.SIGINT, sigint_handler)
332 331 except SystemError:
333 332 # This happens under Windows, which seems to have all sorts
334 333 # of problems with signal handling. Oh well...
335 334 pass
336 335
337 336 if self._kill:
338 337 print >>Term.cout, 'Closing threads...',
339 338 Term.cout.flush()
340 339 for tokill in self.on_kill:
341 340 tokill()
342 341 print >>Term.cout, 'Done.'
343 342
344 343 # Run pending code by calling parent class
345 344 if self.code_to_run is not None:
346 345 self.thread_ready.notify()
347 346 InteractiveShell.runcode(self,self.code_to_run)
348 347
349 348 # We're done with thread-protected variables
350 349 self.thread_ready.release()
351 350 # This MUST return true for gtk threading to work
352 351 return True
353 352
354 353 def kill (self):
355 354 """Kill the thread, returning when it has been shut down."""
356 355 self.thread_ready.acquire()
357 356 self._kill = True
358 357 self.thread_ready.release()
359 358
360 359 class MatplotlibShellBase:
361 360 """Mixin class to provide the necessary modifications to regular IPython
362 361 shell classes for matplotlib support.
363 362
364 363 Given Python's MRO, this should be used as the FIRST class in the
365 364 inheritance hierarchy, so that it overrides the relevant methods."""
366 365
367 366 def _matplotlib_config(self,name):
368 367 """Return various items needed to setup the user's shell with matplotlib"""
369 368
370 369 # Initialize matplotlib to interactive mode always
371 370 import matplotlib
372 371 from matplotlib import backends
373 372 matplotlib.interactive(True)
374 373
375 374 def use(arg):
376 375 """IPython wrapper for matplotlib's backend switcher.
377 376
378 377 In interactive use, we can not allow switching to a different
379 378 interactive backend, since thread conflicts will most likely crash
380 379 the python interpreter. This routine does a safety check first,
381 380 and refuses to perform a dangerous switch. It still allows
382 381 switching to non-interactive backends."""
383 382
384 383 if arg in backends.interactive_bk and arg != self.mpl_backend:
385 384 m=('invalid matplotlib backend switch.\n'
386 385 'This script attempted to switch to the interactive '
387 386 'backend: `%s`\n'
388 387 'Your current choice of interactive backend is: `%s`\n\n'
389 388 'Switching interactive matplotlib backends at runtime\n'
390 389 'would crash the python interpreter, '
391 390 'and IPython has blocked it.\n\n'
392 391 'You need to either change your choice of matplotlib backend\n'
393 392 'by editing your .matplotlibrc file, or run this script as a \n'
394 393 'standalone file from the command line, not using IPython.\n' %
395 394 (arg,self.mpl_backend) )
396 395 raise RuntimeError, m
397 396 else:
398 397 self.mpl_use(arg)
399 398 self.mpl_use._called = True
400 399
401 400 self.matplotlib = matplotlib
402 401 self.mpl_backend = matplotlib.rcParams['backend']
403 402
404 403 # we also need to block switching of interactive backends by use()
405 404 self.mpl_use = matplotlib.use
406 405 self.mpl_use._called = False
407 406 # overwrite the original matplotlib.use with our wrapper
408 407 matplotlib.use = use
409 408
410 409
411 410 # This must be imported last in the matplotlib series, after
412 411 # backend/interactivity choices have been made
413 412 try:
414 413 import matplotlib.pylab as pylab
415 414 self.pylab = pylab
416 415 self.pylab_name = 'pylab'
417 416 except ImportError:
418 417 import matplotlib.matlab as matlab
419 418 self.pylab = matlab
420 419 self.pylab_name = 'matlab'
421 420
422 421 self.pylab.show._needmain = False
423 422 # We need to detect at runtime whether show() is called by the user.
424 423 # For this, we wrap it into a decorator which adds a 'called' flag.
425 424 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
426 425
427 426 # Build a user namespace initialized with matplotlib/matlab features.
428 427 user_ns = {'__name__':'__main__',
429 428 '__builtins__' : __builtin__ }
430 429
431 430 # Be careful not to remove the final \n in the code string below, or
432 431 # things will break badly with py22 (I think it's a python bug, 2.3 is
433 432 # OK).
434 433 pname = self.pylab_name # Python can't interpolate dotted var names
435 434 exec ("import matplotlib\n"
436 435 "import matplotlib.%(pname)s as %(pname)s\n"
437 436 "from matplotlib.%(pname)s import *\n" % locals()) in user_ns
438 437
439 438 # Build matplotlib info banner
440 439 b="""
441 440 Welcome to pylab, a matplotlib-based Python environment.
442 441 For more information, type 'help(pylab)'.
443 442 """
444 443 return user_ns,b
445 444
446 445 def mplot_exec(self,fname,*where,**kw):
447 446 """Execute a matplotlib script.
448 447
449 448 This is a call to execfile(), but wrapped in safeties to properly
450 449 handle interactive rendering and backend switching."""
451 450
452 451 #print '*** Matplotlib runner ***' # dbg
453 452 # turn off rendering until end of script
454 453 isInteractive = self.matplotlib.rcParams['interactive']
455 454 self.matplotlib.interactive(False)
456 455 self.safe_execfile(fname,*where,**kw)
457 456 self.matplotlib.interactive(isInteractive)
458 457 # make rendering call now, if the user tried to do it
459 458 if self.pylab.draw_if_interactive.called:
460 459 self.pylab.draw()
461 460 self.pylab.draw_if_interactive.called = False
462 461
463 462 # if a backend switch was performed, reverse it now
464 463 if self.mpl_use._called:
465 464 self.matplotlib.rcParams['backend'] = self.mpl_backend
466 465
467 466 def magic_run(self,parameter_s=''):
468 467 Magic.magic_run(self,parameter_s,runner=self.mplot_exec)
469 468
470 469 # Fix the docstring so users see the original as well
471 470 magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__,
472 471 "\n *** Modified %run for Matplotlib,"
473 472 " with proper interactive handling ***")
474 473
475 474 # Now we provide 2 versions of a matplotlib-aware IPython base shells, single
476 475 # and multithreaded. Note that these are meant for internal use, the IPShell*
477 476 # classes below are the ones meant for public consumption.
478 477
479 478 class MatplotlibShell(MatplotlibShellBase,InteractiveShell):
480 479 """Single-threaded shell with matplotlib support."""
481 480
482 481 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
483 482 user_ns = None, **kw):
484 483 user_ns,b2 = self._matplotlib_config(name)
485 484 InteractiveShell.__init__(self,name,usage,rc,user_ns,banner2=b2,**kw)
486 485
487 486 class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell):
488 487 """Multi-threaded shell with matplotlib support."""
489 488
490 489 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
491 490 user_ns = None, **kw):
492 491 user_ns,b2 = self._matplotlib_config(name)
493 492 MTInteractiveShell.__init__(self,name,usage,rc,user_ns,banner2=b2,**kw)
494 493
495 494 #-----------------------------------------------------------------------------
496 495 # Utility functions for the different GUI enabled IPShell* classes.
497 496
498 497 def get_tk():
499 498 """Tries to import Tkinter and returns a withdrawn Tkinter root
500 499 window. If Tkinter is already imported or not available, this
501 500 returns None. This function calls `hijack_tk` underneath.
502 501 """
503 502 if not USE_TK or sys.modules.has_key('Tkinter'):
504 503 return None
505 504 else:
506 505 try:
507 506 import Tkinter
508 507 except ImportError:
509 508 return None
510 509 else:
511 510 hijack_tk()
512 511 r = Tkinter.Tk()
513 512 r.withdraw()
514 513 return r
515 514
516 515 def hijack_tk():
517 516 """Modifies Tkinter's mainloop with a dummy so when a module calls
518 517 mainloop, it does not block.
519 518
520 519 """
521 520 def misc_mainloop(self, n=0):
522 521 pass
523 522 def tkinter_mainloop(n=0):
524 523 pass
525 524
526 525 import Tkinter
527 526 Tkinter.Misc.mainloop = misc_mainloop
528 527 Tkinter.mainloop = tkinter_mainloop
529 528
530 529 def update_tk(tk):
531 530 """Updates the Tkinter event loop. This is typically called from
532 531 the respective WX or GTK mainloops.
533 532 """
534 533 if tk:
535 534 tk.update()
536 535
537 536 def hijack_wx():
538 537 """Modifies wxPython's MainLoop with a dummy so user code does not
539 538 block IPython. The hijacked mainloop function is returned.
540 539 """
541 540 def dummy_mainloop(*args, **kw):
542 541 pass
543 542 import wxPython
544 543 ver = wxPython.__version__
545 544 orig_mainloop = None
546 545 if ver[:3] >= '2.5':
547 546 import wx
548 547 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
549 548 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
550 549 else: raise AttributeError('Could not find wx core module')
551 550 orig_mainloop = core.PyApp_MainLoop
552 551 core.PyApp_MainLoop = dummy_mainloop
553 552 elif ver[:3] == '2.4':
554 553 orig_mainloop = wxPython.wxc.wxPyApp_MainLoop
555 554 wxPython.wxc.wxPyApp_MainLoop = dummy_mainloop
556 555 else:
557 556 warn("Unable to find either wxPython version 2.4 or >= 2.5.")
558 557 return orig_mainloop
559 558
560 559 def hijack_gtk():
561 560 """Modifies pyGTK's mainloop with a dummy so user code does not
562 561 block IPython. This function returns the original `gtk.mainloop`
563 562 function that has been hijacked.
564 563 """
565 564 def dummy_mainloop(*args, **kw):
566 565 pass
567 566 import gtk
568 567 if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main
569 568 else: orig_mainloop = gtk.mainloop
570 569 gtk.mainloop = dummy_mainloop
571 570 gtk.main = dummy_mainloop
572 571 return orig_mainloop
573 572
574 573 #-----------------------------------------------------------------------------
575 574 # The IPShell* classes below are the ones meant to be run by external code as
576 575 # IPython instances. Note that unless a specific threading strategy is
577 576 # desired, the factory function start() below should be used instead (it
578 577 # selects the proper threaded class).
579 578
580 579 class IPShellGTK(threading.Thread):
581 580 """Run a gtk mainloop() in a separate thread.
582 581
583 582 Python commands can be passed to the thread where they will be executed.
584 583 This is implemented by periodically checking for passed code using a
585 584 GTK timeout callback."""
586 585
587 586 TIMEOUT = 100 # Millisecond interval between timeouts.
588 587
589 588 def __init__(self,argv=None,user_ns=None,debug=1,
590 589 shell_class=MTInteractiveShell):
591 590
592 591 import gtk
593 592
594 593 self.gtk = gtk
595 594 self.gtk_mainloop = hijack_gtk()
596 595
597 596 # Allows us to use both Tk and GTK.
598 597 self.tk = get_tk()
599 598
600 599 if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit
601 600 else: mainquit = self.gtk.mainquit
602 601
603 602 self.IP = make_IPython(argv,user_ns=user_ns,debug=debug,
604 603 shell_class=shell_class,
605 604 on_kill=[mainquit])
606 605 threading.Thread.__init__(self)
607 606
608 607 def run(self):
609 608 self.IP.mainloop()
610 609 self.IP.kill()
611 610
612 611 def mainloop(self):
613 612
614 613 if self.gtk.pygtk_version >= (2,4,0):
615 614 import gobject
616 gobject.timeout_add(self.TIMEOUT, self.on_timer)
615 gobject.idle_add(self.on_timer)
617 616 else:
618 self.gtk.timeout_add(self.TIMEOUT, self.on_timer)
617 self.gtk.idle_add(self.on_timer)
619 618
620 619 if sys.platform != 'win32':
621 620 try:
622 621 if self.gtk.gtk_version[0] >= 2:
623 622 self.gtk.threads_init()
624 623 except AttributeError:
625 624 pass
626 625 except RuntimeError:
627 626 error('Your pyGTK likely has not been compiled with '
628 627 'threading support.\n'
629 628 'The exception printout is below.\n'
630 629 'You can either rebuild pyGTK with threads, or '
631 630 'try using \n'
632 631 'matplotlib with a different backend (like Tk or WX).\n'
633 632 'Note that matplotlib will most likely not work in its '
634 633 'current state!')
635 634 self.IP.InteractiveTB()
636 635 self.start()
637 636 self.gtk.threads_enter()
638 637 self.gtk_mainloop()
639 638 self.gtk.threads_leave()
640 639 self.join()
641 640
642 641 def on_timer(self):
643 642 update_tk(self.tk)
644 643 return self.IP.runcode()
645 644
646 645
647 646 class IPShellWX(threading.Thread):
648 647 """Run a wx mainloop() in a separate thread.
649 648
650 649 Python commands can be passed to the thread where they will be executed.
651 650 This is implemented by periodically checking for passed code using a
652 651 GTK timeout callback."""
653 652
654 653 TIMEOUT = 100 # Millisecond interval between timeouts.
655 654
656 655 def __init__(self,argv=None,user_ns=None,debug=1,
657 656 shell_class=MTInteractiveShell):
658 657
659 658 import wxPython.wx as wx
660 659
661 660 threading.Thread.__init__(self)
662 661 self.wx = wx
663 662 self.wx_mainloop = hijack_wx()
664 663
665 664 # Allows us to use both Tk and GTK.
666 665 self.tk = get_tk()
667 666
668 667 self.IP = make_IPython(argv,user_ns=user_ns,debug=debug,
669 668 shell_class=shell_class,
670 669 on_kill=[self.wxexit])
671 670 self.app = None
672 671
673 672 def wxexit(self, *args):
674 673 if self.app is not None:
675 674 self.app.agent.timer.Stop()
676 675 self.app.ExitMainLoop()
677 676
678 677 def run(self):
679 678 self.IP.mainloop()
680 679 self.IP.kill()
681 680
682 681 def mainloop(self):
683 682
684 683 self.start()
685 684
686 685 class TimerAgent(self.wx.wxMiniFrame):
687 686 wx = self.wx
688 687 IP = self.IP
689 688 tk = self.tk
690 689 def __init__(self, parent, interval):
691 690 style = self.wx.wxDEFAULT_FRAME_STYLE | self.wx.wxTINY_CAPTION_HORIZ
692 691 self.wx.wxMiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200),
693 692 size=(100, 100),style=style)
694 693 self.Show(False)
695 694 self.interval = interval
696 695 self.timerId = self.wx.wxNewId()
697 696
698 697 def StartWork(self):
699 698 self.timer = self.wx.wxTimer(self, self.timerId)
700 699 self.wx.EVT_TIMER(self, self.timerId, self.OnTimer)
701 700 self.timer.Start(self.interval)
702 701
703 702 def OnTimer(self, event):
704 703 update_tk(self.tk)
705 704 self.IP.runcode()
706 705
707 706 class App(self.wx.wxApp):
708 707 wx = self.wx
709 708 TIMEOUT = self.TIMEOUT
710 709 def OnInit(self):
711 710 'Create the main window and insert the custom frame'
712 711 self.agent = TimerAgent(None, self.TIMEOUT)
713 712 self.agent.Show(self.wx.false)
714 713 self.agent.StartWork()
715 714 return self.wx.true
716 715
717 716 self.app = App(redirect=False)
718 717 self.wx_mainloop(self.app)
719 718 self.join()
720 719
721 720
722 721 class IPShellQt(threading.Thread):
723 722 """Run a Qt event loop in a separate thread.
724 723
725 724 Python commands can be passed to the thread where they will be executed.
726 725 This is implemented by periodically checking for passed code using a
727 726 Qt timer / slot."""
728 727
729 728 TIMEOUT = 100 # Millisecond interval between timeouts.
730 729
731 730 def __init__(self,argv=None,user_ns=None,debug=0,
732 731 shell_class=MTInteractiveShell):
733 732
734 733 import qt
735 734
736 735 class newQApplication:
737 736 def __init__( self ):
738 737 self.QApplication = qt.QApplication
739 738
740 739 def __call__( *args, **kwargs ):
741 740 return qt.qApp
742 741
743 742 def exec_loop( *args, **kwargs ):
744 743 pass
745 744
746 745 def __getattr__( self, name ):
747 746 return getattr( self.QApplication, name )
748 747
749 748 qt.QApplication = newQApplication()
750 749
751 750 # Allows us to use both Tk and QT.
752 751 self.tk = get_tk()
753 752
754 753 self.IP = make_IPython(argv,user_ns=user_ns,debug=debug,
755 754 shell_class=shell_class,
756 755 on_kill=[qt.qApp.exit])
757 756
758 757 threading.Thread.__init__(self)
759 758
760 759 def run(self):
761 760 #sys.excepthook = self.IP.excepthook # dbg
762 761 self.IP.mainloop()
763 762 self.IP.kill()
764 763
765 764 def mainloop(self):
766 765 import qt, sys
767 766 if qt.QApplication.startingUp():
768 767 a = qt.QApplication.QApplication( sys.argv )
769 768 self.timer = qt.QTimer()
770 769 qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer )
771 770
772 771 self.start()
773 772 self.timer.start( self.TIMEOUT, True )
774 773 while True:
775 774 if self.IP._kill: break
776 775 qt.qApp.exec_loop()
777 776 self.join()
778 777
779 778 def on_timer(self):
780 779 update_tk(self.tk)
781 780 result = self.IP.runcode()
782 781 self.timer.start( self.TIMEOUT, True )
783 782 return result
784 783
785 784 # A set of matplotlib public IPython shell classes, for single-threaded
786 785 # (Tk* and FLTK* backends) and multithreaded (GTK* and WX* backends) use.
787 786 class IPShellMatplotlib(IPShell):
788 787 """Subclass IPShell with MatplotlibShell as the internal shell.
789 788
790 789 Single-threaded class, meant for the Tk* and FLTK* backends.
791 790
792 791 Having this on a separate class simplifies the external driver code."""
793 792
794 793 def __init__(self,argv=None,user_ns=None,debug=1):
795 794 IPShell.__init__(self,argv,user_ns,debug,shell_class=MatplotlibShell)
796 795
797 796 class IPShellMatplotlibGTK(IPShellGTK):
798 797 """Subclass IPShellGTK with MatplotlibMTShell as the internal shell.
799 798
800 799 Multi-threaded class, meant for the GTK* backends."""
801 800
802 801 def __init__(self,argv=None,user_ns=None,debug=1):
803 802 IPShellGTK.__init__(self,argv,user_ns,debug,shell_class=MatplotlibMTShell)
804 803
805 804 class IPShellMatplotlibWX(IPShellWX):
806 805 """Subclass IPShellWX with MatplotlibMTShell as the internal shell.
807 806
808 807 Multi-threaded class, meant for the WX* backends."""
809 808
810 809 def __init__(self,argv=None,user_ns=None,debug=1):
811 810 IPShellWX.__init__(self,argv,user_ns,debug,shell_class=MatplotlibMTShell)
812 811
813 812 class IPShellMatplotlibQt(IPShellQt):
814 813 """Subclass IPShellQt with MatplotlibMTShell as the internal shell.
815 814
816 815 Multi-threaded class, meant for the Qt* backends."""
817 816
818 817 def __init__(self,argv=None,user_ns=None,debug=1):
819 818 IPShellQt.__init__(self,argv,user_ns,debug,shell_class=MatplotlibMTShell)
820 819
821 820 #-----------------------------------------------------------------------------
822 821 # Factory functions to actually start the proper thread-aware shell
823 822
824 823 def _matplotlib_shell_class():
825 824 """Factory function to handle shell class selection for matplotlib.
826 825
827 826 The proper shell class to use depends on the matplotlib backend, since
828 827 each backend requires a different threading strategy."""
829 828
830 829 try:
831 830 import matplotlib
832 831 except ImportError:
833 832 error('matplotlib could NOT be imported! Starting normal IPython.')
834 833 sh_class = IPShell
835 834 else:
836 835 backend = matplotlib.rcParams['backend']
837 836 if backend.startswith('GTK'):
838 837 sh_class = IPShellMatplotlibGTK
839 838 elif backend.startswith('WX'):
840 839 sh_class = IPShellMatplotlibWX
841 840 elif backend.startswith('Qt'):
842 841 sh_class = IPShellMatplotlibQt
843 842 else:
844 843 sh_class = IPShellMatplotlib
845 844 #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg
846 845 return sh_class
847 846
848 847 # This is the one which should be called by external code.
849 848 def start():
850 849 """Return a running shell instance, dealing with threading options.
851 850
852 851 This is a factory function which will instantiate the proper IPython shell
853 852 based on the user's threading choice. Such a selector is needed because
854 853 different GUI toolkits require different thread handling details."""
855 854
856 855 global USE_TK
857 856 # Crude sys.argv hack to extract the threading options.
858 857 if len(sys.argv) > 1:
859 858 if len(sys.argv) > 2:
860 859 arg2 = sys.argv[2]
861 860 if arg2.endswith('-tk'):
862 861 USE_TK = True
863 862 arg1 = sys.argv[1]
864 863 if arg1.endswith('-gthread'):
865 864 shell = IPShellGTK
866 865 elif arg1.endswith( '-qthread' ):
867 866 shell = IPShellQt
868 867 elif arg1.endswith('-wthread'):
869 868 shell = IPShellWX
870 869 elif arg1.endswith('-pylab'):
871 870 shell = _matplotlib_shell_class()
872 871 else:
873 872 shell = IPShell
874 873 else:
875 874 shell = IPShell
876 875 return shell()
877 876
878 877 # Some aliases for backwards compatibility
879 878 IPythonShell = IPShell
880 879 IPythonShellEmbed = IPShellEmbed
881 880 #************************ End of file <Shell.py> ***************************
@@ -1,2052 +1,2062 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 908 2005-09-26 16:05:48Z fperez $
9 $Id: iplib.py 921 2005-11-13 06:51:34Z fperez $
10 10 """
11 11
12 12 #*****************************************************************************
13 13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
14 14 # Copyright (C) 2001-2004 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, much of that class has been copied
21 21 # verbatim here for modifications which could not be accomplished by
22 22 # subclassing. The Python License (sec. 2) allows for this, but it's always
23 23 # nice to acknowledge credit where credit is due.
24 24 #*****************************************************************************
25 25
26 26 #****************************************************************************
27 27 # Modules and globals
28 28
29 29 from __future__ import generators # for 2.2 backwards-compatibility
30 30
31 31 from IPython import Release
32 32 __author__ = '%s <%s>\n%s <%s>' % \
33 33 ( Release.authors['Janko'] + Release.authors['Fernando'] )
34 34 __license__ = Release.license
35 35 __version__ = Release.version
36 36
37 37 # Python standard modules
38 38 import __main__
39 39 import __builtin__
40 40 import exceptions
41 41 import keyword
42 42 import new
43 43 import os, sys, shutil
44 44 import code, glob, types, re
45 45 import string, StringIO
46 46 import inspect, pydoc
47 47 import bdb, pdb
48 48 import UserList # don't subclass list so this works with Python2.1
49 49 from pprint import pprint, pformat
50 50 import cPickle as pickle
51 51 import traceback
52 from codeop import CommandCompiler
52 53
53 54 # IPython's own modules
54 55 import IPython
55 56 from IPython import OInspect,PyColorize,ultraTB
56 57 from IPython.ultraTB import ColorScheme,ColorSchemeTable # too long names
57 58 from IPython.Logger import Logger
58 59 from IPython.Magic import Magic,magic2python,shlex_split
59 60 from IPython.usage import cmd_line_usage,interactive_usage
60 61 from IPython.Struct import Struct
61 62 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
62 63 from IPython.FakeModule import FakeModule
63 64 from IPython.background_jobs import BackgroundJobManager
64 65 from IPython.PyColorize import Parser
65 66 from IPython.genutils import *
66 67
67 68 # Global pointer to the running
68 69
69 70 # store the builtin raw_input globally, and use this always, in case user code
70 71 # overwrites it (like wx.py.PyShell does)
71 72 raw_input_original = raw_input
72 73
73 74 #****************************************************************************
74 75 # Some utility function definitions
75 76
76 77 class Bunch: pass
77 78
78 79 def esc_quotes(strng):
79 80 """Return the input string with single and double quotes escaped out"""
80 81
81 82 return strng.replace('"','\\"').replace("'","\\'")
82 83
83 84 def import_fail_info(mod_name,fns=None):
84 85 """Inform load failure for a module."""
85 86
86 87 if fns == None:
87 88 warn("Loading of %s failed.\n" % (mod_name,))
88 89 else:
89 90 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
90 91
91 92 def qw_lol(indata):
92 93 """qw_lol('a b') -> [['a','b']],
93 94 otherwise it's just a call to qw().
94 95
95 96 We need this to make sure the modules_some keys *always* end up as a
96 97 list of lists."""
97 98
98 99 if type(indata) in StringTypes:
99 100 return [qw(indata)]
100 101 else:
101 102 return qw(indata)
102 103
103 104 def ipmagic(arg_s):
104 105 """Call a magic function by name.
105 106
106 107 Input: a string containing the name of the magic function to call and any
107 108 additional arguments to be passed to the magic.
108 109
109 110 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
110 111 prompt:
111 112
112 113 In[1]: %name -opt foo bar
113 114
114 115 To call a magic without arguments, simply use ipmagic('name').
115 116
116 117 This provides a proper Python function to call IPython's magics in any
117 118 valid Python code you can type at the interpreter, including loops and
118 119 compound statements. It is added by IPython to the Python builtin
119 120 namespace upon initialization."""
120 121
121 122 args = arg_s.split(' ',1)
122 123 magic_name = args[0]
123 124 if magic_name.startswith(__IPYTHON__.ESC_MAGIC):
124 125 magic_name = magic_name[1:]
125 126 try:
126 127 magic_args = args[1]
127 128 except IndexError:
128 129 magic_args = ''
129 130 fn = getattr(__IPYTHON__,'magic_'+magic_name,None)
130 131 if fn is None:
131 132 error("Magic function `%s` not found." % magic_name)
132 133 else:
133 134 magic_args = __IPYTHON__.var_expand(magic_args)
134 135 return fn(magic_args)
135 136
136 137 def ipalias(arg_s):
137 138 """Call an alias by name.
138 139
139 140 Input: a string containing the name of the alias to call and any
140 141 additional arguments to be passed to the magic.
141 142
142 143 ipalias('name -opt foo bar') is equivalent to typing at the ipython
143 144 prompt:
144 145
145 146 In[1]: name -opt foo bar
146 147
147 148 To call an alias without arguments, simply use ipalias('name').
148 149
149 150 This provides a proper Python function to call IPython's aliases in any
150 151 valid Python code you can type at the interpreter, including loops and
151 152 compound statements. It is added by IPython to the Python builtin
152 153 namespace upon initialization."""
153 154
154 155 args = arg_s.split(' ',1)
155 156 alias_name = args[0]
156 157 try:
157 158 alias_args = args[1]
158 159 except IndexError:
159 160 alias_args = ''
160 161 if alias_name in __IPYTHON__.alias_table:
161 162 __IPYTHON__.call_alias(alias_name,alias_args)
162 163 else:
163 164 error("Alias `%s` not found." % alias_name)
164 165
165 166 #-----------------------------------------------------------------------------
166 167 # Local use classes
167 168 try:
168 169 from IPython import FlexCompleter
169 170
170 171 class MagicCompleter(FlexCompleter.Completer):
171 172 """Extension of the completer class to work on %-prefixed lines."""
172 173
173 174 def __init__(self,shell,namespace=None,omit__names=0,alias_table=None):
174 175 """MagicCompleter() -> completer
175 176
176 177 Return a completer object suitable for use by the readline library
177 178 via readline.set_completer().
178 179
179 180 Inputs:
180 181
181 182 - shell: a pointer to the ipython shell itself. This is needed
182 183 because this completer knows about magic functions, and those can
183 184 only be accessed via the ipython instance.
184 185
185 186 - namespace: an optional dict where completions are performed.
186 187
187 188 - The optional omit__names parameter sets the completer to omit the
188 189 'magic' names (__magicname__) for python objects unless the text
189 190 to be completed explicitly starts with one or more underscores.
190 191
191 192 - If alias_table is supplied, it should be a dictionary of aliases
192 193 to complete. """
193 194
194 195 FlexCompleter.Completer.__init__(self,namespace)
195 196 self.magic_prefix = shell.name+'.magic_'
196 197 self.magic_escape = shell.ESC_MAGIC
197 198 self.readline = FlexCompleter.readline
198 199 delims = self.readline.get_completer_delims()
199 200 delims = delims.replace(self.magic_escape,'')
200 201 self.readline.set_completer_delims(delims)
201 202 self.get_line_buffer = self.readline.get_line_buffer
202 203 self.omit__names = omit__names
203 204 self.merge_completions = shell.rc.readline_merge_completions
204 205
205 206 if alias_table is None:
206 207 alias_table = {}
207 208 self.alias_table = alias_table
208 209 # Regexp to split filenames with spaces in them
209 210 self.space_name_re = re.compile(r'([^\\] )')
210 211 # Hold a local ref. to glob.glob for speed
211 212 self.glob = glob.glob
212 213 # Special handling of backslashes needed in win32 platforms
213 214 if sys.platform == "win32":
214 215 self.clean_glob = self._clean_glob_win32
215 216 else:
216 217 self.clean_glob = self._clean_glob
217 218 self.matchers = [self.python_matches,
218 219 self.file_matches,
219 220 self.alias_matches,
220 221 self.python_func_kw_matches]
221 222
222 223 # Code contributed by Alex Schmolck, for ipython/emacs integration
223 224 def all_completions(self, text):
224 225 """Return all possible completions for the benefit of emacs."""
225 226
226 227 completions = []
227 228 try:
228 229 for i in xrange(sys.maxint):
229 230 res = self.complete(text, i)
230 231
231 232 if not res: break
232 233
233 234 completions.append(res)
234 235 #XXX workaround for ``notDefined.<tab>``
235 236 except NameError:
236 237 pass
237 238 return completions
238 239 # /end Alex Schmolck code.
239 240
240 241 def _clean_glob(self,text):
241 242 return self.glob("%s*" % text)
242 243
243 244 def _clean_glob_win32(self,text):
244 245 return [f.replace("\\","/")
245 246 for f in self.glob("%s*" % text)]
246 247
247 248 def file_matches(self, text):
248 249 """Match filneames, expanding ~USER type strings.
249 250
250 251 Most of the seemingly convoluted logic in this completer is an
251 252 attempt to handle filenames with spaces in them. And yet it's not
252 253 quite perfect, because Python's readline doesn't expose all of the
253 254 GNU readline details needed for this to be done correctly.
254 255
255 256 For a filename with a space in it, the printed completions will be
256 257 only the parts after what's already been typed (instead of the
257 258 full completions, as is normally done). I don't think with the
258 259 current (as of Python 2.3) Python readline it's possible to do
259 260 better."""
260 261
261 262 #print 'Completer->file_matches: <%s>' % text # dbg
262 263
263 264 # chars that require escaping with backslash - i.e. chars
264 265 # that readline treats incorrectly as delimiters, but we
265 266 # don't want to treat as delimiters in filename matching
266 267 # when escaped with backslash
267 268
268 269 protectables = ' ()[]{}'
269 270
270 271 def protect_filename(s):
271 272 return "".join([(ch in protectables and '\\' + ch or ch)
272 273 for ch in s])
273 274
274 275 lbuf = self.get_line_buffer()[:self.readline.get_endidx()]
275 276 open_quotes = 0 # track strings with open quotes
276 277 try:
277 278 lsplit = shlex_split(lbuf)[-1]
278 279 except ValueError:
279 280 # typically an unmatched ", or backslash without escaped char.
280 281 if lbuf.count('"')==1:
281 282 open_quotes = 1
282 283 lsplit = lbuf.split('"')[-1]
283 284 elif lbuf.count("'")==1:
284 285 open_quotes = 1
285 286 lsplit = lbuf.split("'")[-1]
286 287 else:
287 288 return None
288 289 except IndexError:
289 290 # tab pressed on empty line
290 291 lsplit = ""
291 292
292 293 if lsplit != protect_filename(lsplit):
293 294 # if protectables are found, do matching on the whole escaped
294 295 # name
295 296 has_protectables = 1
296 297 text0,text = text,lsplit
297 298 else:
298 299 has_protectables = 0
299 300 text = os.path.expanduser(text)
300 301
301 302 if text == "":
302 303 return [protect_filename(f) for f in self.glob("*")]
303 304
304 305 m0 = self.clean_glob(text.replace('\\',''))
305 306 if has_protectables:
306 307 # If we had protectables, we need to revert our changes to the
307 308 # beginning of filename so that we don't double-write the part
308 309 # of the filename we have so far
309 310 len_lsplit = len(lsplit)
310 311 matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0]
311 312 else:
312 313 if open_quotes:
313 314 # if we have a string with an open quote, we don't need to
314 315 # protect the names at all (and we _shouldn't_, as it
315 316 # would cause bugs when the filesystem call is made).
316 317 matches = m0
317 318 else:
318 319 matches = [protect_filename(f) for f in m0]
319 320 if len(matches) == 1 and os.path.isdir(matches[0]):
320 321 # Takes care of links to directories also. Use '/'
321 322 # explicitly, even under Windows, so that name completions
322 323 # don't end up escaped.
323 324 matches[0] += '/'
324 325 return matches
325 326
326 327 def alias_matches(self, text):
327 328 """Match internal system aliases"""
328 329 #print 'Completer->alias_matches:',text # dbg
329 330 text = os.path.expanduser(text)
330 331 aliases = self.alias_table.keys()
331 332 if text == "":
332 333 return aliases
333 334 else:
334 335 return [alias for alias in aliases if alias.startswith(text)]
335 336
336 337 def python_matches(self,text):
337 338 """Match attributes or global python names"""
338 339 #print 'Completer->python_matches' # dbg
339 340 if "." in text:
340 341 try:
341 342 matches = self.attr_matches(text)
342 343 if text.endswith('.') and self.omit__names:
343 344 if self.omit__names == 1:
344 345 # true if txt is _not_ a __ name, false otherwise:
345 346 no__name = (lambda txt:
346 347 re.match(r'.*\.__.*?__',txt) is None)
347 348 else:
348 349 # true if txt is _not_ a _ name, false otherwise:
349 350 no__name = (lambda txt:
350 351 re.match(r'.*\._.*?',txt) is None)
351 352 matches = filter(no__name, matches)
352 353 except NameError:
353 354 # catches <undefined attributes>.<tab>
354 355 matches = []
355 356 else:
356 357 matches = self.global_matches(text)
357 358 # this is so completion finds magics when automagic is on:
358 359 if matches == [] and not text.startswith(os.sep):
359 360 matches = self.attr_matches(self.magic_prefix+text)
360 361 return matches
361 362
362 363 def _default_arguments(self, obj):
363 364 """Return the list of default arguments of obj if it is callable,
364 365 or empty list otherwise."""
365 366
366 367 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
367 368 # for classes, check for __init__,__new__
368 369 if inspect.isclass(obj):
369 370 obj = (getattr(obj,'__init__',None) or
370 371 getattr(obj,'__new__',None))
371 372 # for all others, check if they are __call__able
372 373 elif hasattr(obj, '__call__'):
373 374 obj = obj.__call__
374 375 # XXX: is there a way to handle the builtins ?
375 376 try:
376 377 args,_,_1,defaults = inspect.getargspec(obj)
377 378 if defaults:
378 379 return args[-len(defaults):]
379 380 except TypeError: pass
380 381 return []
381 382
382 383 def python_func_kw_matches(self,text):
383 384 """Match named parameters (kwargs) of the last open function"""
384 385
385 386 if "." in text: # a parameter cannot be dotted
386 387 return []
387 388 try: regexp = self.__funcParamsRegex
388 389 except AttributeError:
389 390 regexp = self.__funcParamsRegex = re.compile(r'''
390 391 '.*?' | # single quoted strings or
391 392 ".*?" | # double quoted strings or
392 393 \w+ | # identifier
393 394 \S # other characters
394 395 ''', re.VERBOSE | re.DOTALL)
395 396 # 1. find the nearest identifier that comes before an unclosed
396 397 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
397 398 tokens = regexp.findall(self.get_line_buffer())
398 399 tokens.reverse()
399 400 iterTokens = iter(tokens); openPar = 0
400 401 for token in iterTokens:
401 402 if token == ')':
402 403 openPar -= 1
403 404 elif token == '(':
404 405 openPar += 1
405 406 if openPar > 0:
406 407 # found the last unclosed parenthesis
407 408 break
408 409 else:
409 410 return []
410 411 # 2. Concatenate any dotted names (e.g. "foo.bar" for "foo.bar(x, pa" )
411 412 ids = []
412 413 isId = re.compile(r'\w+$').match
413 414 while True:
414 415 try:
415 416 ids.append(iterTokens.next())
416 417 if not isId(ids[-1]):
417 418 ids.pop(); break
418 419 if not iterTokens.next() == '.':
419 420 break
420 421 except StopIteration:
421 422 break
422 423 # lookup the candidate callable matches either using global_matches
423 424 # or attr_matches for dotted names
424 425 if len(ids) == 1:
425 426 callableMatches = self.global_matches(ids[0])
426 427 else:
427 428 callableMatches = self.attr_matches('.'.join(ids[::-1]))
428 429 argMatches = []
429 430 for callableMatch in callableMatches:
430 431 try: namedArgs = self._default_arguments(eval(callableMatch,
431 432 self.namespace))
432 433 except: continue
433 434 for namedArg in namedArgs:
434 435 if namedArg.startswith(text):
435 436 argMatches.append("%s=" %namedArg)
436 437 return argMatches
437 438
438 439 def complete(self, text, state):
439 440 """Return the next possible completion for 'text'.
440 441
441 442 This is called successively with state == 0, 1, 2, ... until it
442 443 returns None. The completion should begin with 'text'. """
443 444
444 445 #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg
445 446 magic_escape = self.magic_escape
446 447 magic_prefix = self.magic_prefix
447 448
448 449 try:
449 450 if text.startswith(magic_escape):
450 451 text = text.replace(magic_escape,magic_prefix)
451 452 elif text.startswith('~'):
452 453 text = os.path.expanduser(text)
453 454 if state == 0:
454 455 # Extend the list of completions with the results of each
455 456 # matcher, so we return results to the user from all
456 457 # namespaces.
457 458 if self.merge_completions:
458 459 self.matches = []
459 460 for matcher in self.matchers:
460 461 self.matches.extend(matcher(text))
461 462 else:
462 463 for matcher in self.matchers:
463 464 self.matches = matcher(text)
464 465 if self.matches:
465 466 break
466 467
467 468 try:
468 469 return self.matches[state].replace(magic_prefix,magic_escape)
469 470 except IndexError:
470 471 return None
471 472 except:
472 473 # If completion fails, don't annoy the user.
473 474 pass
474 475
475 476 except ImportError:
476 477 pass # no readline support
477 478
478 479 except KeyError:
479 480 pass # Windows doesn't set TERM, it doesn't matter
480 481
481 482
482 483 class InputList(UserList.UserList):
483 484 """Class to store user input.
484 485
485 486 It's basically a list, but slices return a string instead of a list, thus
486 487 allowing things like (assuming 'In' is an instance):
487 488
488 489 exec In[4:7]
489 490
490 491 or
491 492
492 493 exec In[5:9] + In[14] + In[21:25]"""
493 494
494 495 def __getslice__(self,i,j):
495 496 return ''.join(UserList.UserList.__getslice__(self,i,j))
496 497
497 498 #****************************************************************************
498 499 # Local use exceptions
499 500 class SpaceInInput(exceptions.Exception):
500 501 pass
501 502
502 503 #****************************************************************************
503 504 # Main IPython class
504 505
505 506 class InteractiveShell(code.InteractiveConsole, Logger, Magic):
506 507 """An enhanced console for Python."""
507 508
508 509 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
509 user_ns = None,banner2='',
510 user_ns = None,user_global_ns=None,banner2='',
510 511 custom_exceptions=((),None)):
511 512
512 513 # Put a reference to self in builtins so that any form of embedded or
513 514 # imported code can test for being inside IPython.
514 515 __builtin__.__IPYTHON__ = self
515 516
516 517 # And load into builtins ipmagic/ipalias as well
517 518 __builtin__.ipmagic = ipmagic
518 519 __builtin__.ipalias = ipalias
519 520
520 521 # Add to __builtin__ other parts of IPython's public API
521 522 __builtin__.ip_set_hook = self.set_hook
522 523
523 524 # Keep in the builtins a flag for when IPython is active. We set it
524 525 # with setdefault so that multiple nested IPythons don't clobber one
525 526 # another. Each will increase its value by one upon being activated,
526 527 # which also gives us a way to determine the nesting level.
527 528 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
528 529
529 530 # Inform the user of ipython's fast exit magics.
530 531 _exit = ' Use %Exit or %Quit to exit without confirmation.'
531 532 __builtin__.exit += _exit
532 533 __builtin__.quit += _exit
533 534
534 # Create the namespace where the user will operate:
535 # compiler command
536 self.compile = CommandCompiler()
537
538 # User input buffer
539 self.buffer = []
540
541 # Default name given in compilation of code
542 self.filename = '<ipython console>'
543
544 # Create the namespace where the user will operate. user_ns is
545 # normally the only one used, and it is passed to the exec calls as
546 # the locals argument. But we do carry a user_global_ns namespace
547 # given as the exec 'globals' argument, This is useful in embedding
548 # situations where the ipython shell opens in a context where the
549 # distinction between locals and globals is meaningful.
535 550
536 551 # FIXME. For some strange reason, __builtins__ is showing up at user
537 552 # level as a dict instead of a module. This is a manual fix, but I
538 553 # should really track down where the problem is coming from. Alex
539 554 # Schmolck reported this problem first.
540 555
541 556 # A useful post by Alex Martelli on this topic:
542 557 # Re: inconsistent value from __builtins__
543 558 # Von: Alex Martelli <aleaxit@yahoo.com>
544 559 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
545 560 # Gruppen: comp.lang.python
546 561 # Referenzen: 1
547 562
548 563 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
549 564 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
550 565 # > <type 'dict'>
551 566 # > >>> print type(__builtins__)
552 567 # > <type 'module'>
553 568 # > Is this difference in return value intentional?
554 569
555 570 # Well, it's documented that '__builtins__' can be either a dictionary
556 571 # or a module, and it's been that way for a long time. Whether it's
557 572 # intentional (or sensible), I don't know. In any case, the idea is that
558 573 # if you need to access the built-in namespace directly, you should start
559 574 # with "import __builtin__" (note, no 's') which will definitely give you
560 575 # a module. Yeah, it's somewhat confusing:-(.
561 576
562 577 if user_ns is None:
563 578 # Set __name__ to __main__ to better match the behavior of the
564 579 # normal interpreter.
565 self.user_ns = {'__name__' :'__main__',
566 '__builtins__' : __builtin__,
567 }
568 else:
569 self.user_ns = user_ns
580 user_ns = {'__name__' :'__main__',
581 '__builtins__' : __builtin__,
582 }
583
584 if user_global_ns is None:
585 user_global_ns = {}
586
587 # assign namespaces
588 self.user_ns = user_ns
589 self.user_global_ns = user_global_ns
570 590
571 591 # The user namespace MUST have a pointer to the shell itself.
572 592 self.user_ns[name] = self
573 593
574 594 # We need to insert into sys.modules something that looks like a
575 595 # module but which accesses the IPython namespace, for shelve and
576 596 # pickle to work interactively. Normally they rely on getting
577 597 # everything out of __main__, but for embedding purposes each IPython
578 598 # instance has its own private namespace, so we can't go shoving
579 599 # everything into __main__.
580 600
581 601 try:
582 602 main_name = self.user_ns['__name__']
583 603 except KeyError:
584 604 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
585 605 else:
586 606 #print "pickle hack in place" # dbg
587 607 sys.modules[main_name] = FakeModule(self.user_ns)
588 608
589 609 # List of input with multi-line handling.
590 610 # Fill its zero entry, user counter starts at 1
591 611 self.input_hist = InputList(['\n'])
592 612
593 613 # list of visited directories
594 614 try:
595 615 self.dir_hist = [os.getcwd()]
596 616 except IOError, e:
597 617 self.dir_hist = []
598 618
599 619 # dict of output history
600 620 self.output_hist = {}
601 621
602 622 # dict of names to be treated as system aliases. Each entry in the
603 623 # alias table must be a 2-tuple of the form (N,name), where N is the
604 624 # number of positional arguments of the alias.
605 625 self.alias_table = {}
606 626
607 627 # dict of things NOT to alias (keywords, builtins and some special magics)
608 628 no_alias = {}
609 629 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
610 630 for key in keyword.kwlist + no_alias_magics:
611 631 no_alias[key] = 1
612 632 no_alias.update(__builtin__.__dict__)
613 633 self.no_alias = no_alias
614
615
634
616 635 # make global variables for user access to these
617 636 self.user_ns['_ih'] = self.input_hist
618 637 self.user_ns['_oh'] = self.output_hist
619 638 self.user_ns['_dh'] = self.dir_hist
620 639
621 640 # user aliases to input and output histories
622 641 self.user_ns['In'] = self.input_hist
623 642 self.user_ns['Out'] = self.output_hist
624 643
625 644 # Store the actual shell's name
626 645 self.name = name
627 646
628 647 # Object variable to store code object waiting execution. This is
629 648 # used mainly by the multithreaded shells, but it can come in handy in
630 649 # other situations. No need to use a Queue here, since it's a single
631 650 # item which gets cleared once run.
632 651 self.code_to_run = None
633 652
634 653 # Job manager (for jobs run as background threads)
635 654 self.jobs = BackgroundJobManager()
636 655 # Put the job manager into builtins so it's always there.
637 656 __builtin__.jobs = self.jobs
638 657
639 658 # escapes for automatic behavior on the command line
640 659 self.ESC_SHELL = '!'
641 660 self.ESC_HELP = '?'
642 661 self.ESC_MAGIC = '%'
643 662 self.ESC_QUOTE = ','
644 663 self.ESC_QUOTE2 = ';'
645 664 self.ESC_PAREN = '/'
646 665
647 666 # And their associated handlers
648 667 self.esc_handlers = {self.ESC_PAREN:self.handle_auto,
649 668 self.ESC_QUOTE:self.handle_auto,
650 669 self.ESC_QUOTE2:self.handle_auto,
651 670 self.ESC_MAGIC:self.handle_magic,
652 671 self.ESC_HELP:self.handle_help,
653 672 self.ESC_SHELL:self.handle_shell_escape,
654 673 }
655 674
656 675 # class initializations
657 code.InteractiveConsole.__init__(self,locals = self.user_ns)
658 676 Logger.__init__(self,log_ns = self.user_ns)
659 677 Magic.__init__(self,self)
660 678
661 679 # an ugly hack to get a pointer to the shell, so I can start writing
662 680 # magic code via this pointer instead of the current mixin salad.
663 681 Magic.set_shell(self,self)
664 682
665 683 # Python source parser/formatter for syntax highlighting
666 684 pyformat = Parser().format
667 685 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
668 686
669 687 # hooks holds pointers used for user-side customizations
670 688 self.hooks = Struct()
671 689
672 690 # Set all default hooks, defined in the IPython.hooks module.
673 691 hooks = IPython.hooks
674 692 for hook_name in hooks.__all__:
675 693 self.set_hook(hook_name,getattr(hooks,hook_name))
676 694
677 695 # Flag to mark unconditional exit
678 696 self.exit_now = False
679 697
680 698 self.usage_min = """\
681 699 An enhanced console for Python.
682 700 Some of its features are:
683 701 - Readline support if the readline library is present.
684 702 - Tab completion in the local namespace.
685 703 - Logging of input, see command-line options.
686 704 - System shell escape via ! , eg !ls.
687 705 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
688 706 - Keeps track of locally defined variables via %who, %whos.
689 707 - Show object information with a ? eg ?x or x? (use ?? for more info).
690 708 """
691 709 if usage: self.usage = usage
692 710 else: self.usage = self.usage_min
693 711
694 712 # Storage
695 713 self.rc = rc # This will hold all configuration information
696 714 self.inputcache = []
697 715 self._boundcache = []
698 716 self.pager = 'less'
699 717 # temporary files used for various purposes. Deleted at exit.
700 718 self.tempfiles = []
701 719
702 720 # Keep track of readline usage (later set by init_readline)
703 721 self.has_readline = 0
704 722
705 723 # for pushd/popd management
706 724 try:
707 725 self.home_dir = get_home_dir()
708 726 except HomeDirError,msg:
709 727 fatal(msg)
710 728
711 729 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
712 730
713 731 # Functions to call the underlying shell.
714 732
715 733 # utility to expand user variables via Itpl
716 734 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
717 735 self.user_ns))
718 736 # The first is similar to os.system, but it doesn't return a value,
719 737 # and it allows interpolation of variables in the user's namespace.
720 738 self.system = lambda cmd: shell(self.var_expand(cmd),
721 739 header='IPython system call: ',
722 740 verbose=self.rc.system_verbose)
723 741 # These are for getoutput and getoutputerror:
724 742 self.getoutput = lambda cmd: \
725 743 getoutput(self.var_expand(cmd),
726 744 header='IPython system call: ',
727 745 verbose=self.rc.system_verbose)
728 746 self.getoutputerror = lambda cmd: \
729 747 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
730 748 self.user_ns)),
731 749 header='IPython system call: ',
732 750 verbose=self.rc.system_verbose)
733 751
734 752 # RegExp for splitting line contents into pre-char//first
735 753 # word-method//rest. For clarity, each group in on one line.
736 754
737 755 # WARNING: update the regexp if the above escapes are changed, as they
738 756 # are hardwired in.
739 757
740 758 # Don't get carried away with trying to make the autocalling catch too
741 759 # much: it's better to be conservative rather than to trigger hidden
742 760 # evals() somewhere and end up causing side effects.
743 761
744 762 self.line_split = re.compile(r'^([\s*,;/])'
745 763 r'([\?\w\.]+\w*\s*)'
746 764 r'(\(?.*$)')
747 765
748 766 # Original re, keep around for a while in case changes break something
749 767 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
750 768 # r'(\s*[\?\w\.]+\w*\s*)'
751 769 # r'(\(?.*$)')
752 770
753 771 # RegExp to identify potential function names
754 772 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
755 773 # RegExp to exclude strings with this start from autocalling
756 774 self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ')
757 775 # try to catch also methods for stuff in lists/tuples/dicts: off
758 776 # (experimental). For this to work, the line_split regexp would need
759 777 # to be modified so it wouldn't break things at '['. That line is
760 778 # nasty enough that I shouldn't change it until I can test it _well_.
761 779 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
762 780
763 781 # keep track of where we started running (mainly for crash post-mortem)
764 782 self.starting_dir = os.getcwd()
765 783
766 784 # Attributes for Logger mixin class, make defaults here
767 785 self._dolog = 0
768 786 self.LOG = ''
769 787 self.LOGDEF = '.InteractiveShell.log'
770 788 self.LOGMODE = 'over'
771 789 self.LOGHEAD = Itpl(
772 790 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
773 791 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
774 792 #log# opts = $self.rc.opts
775 793 #log# args = $self.rc.args
776 794 #log# It is safe to make manual edits below here.
777 795 #log#-----------------------------------------------------------------------
778 796 """)
779 797 # Various switches which can be set
780 798 self.CACHELENGTH = 5000 # this is cheap, it's just text
781 799 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
782 800 self.banner2 = banner2
783 801
784 802 # TraceBack handlers:
785 803 # Need two, one for syntax errors and one for other exceptions.
786 804 self.SyntaxTB = ultraTB.ListTB(color_scheme='NoColor')
787 805 # This one is initialized with an offset, meaning we always want to
788 806 # remove the topmost item in the traceback, which is our own internal
789 807 # code. Valid modes: ['Plain','Context','Verbose']
790 808 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
791 809 color_scheme='NoColor',
792 810 tb_offset = 1)
793 811 # and add any custom exception handlers the user may have specified
794 812 self.set_custom_exc(*custom_exceptions)
795 813
796 814 # Object inspector
797 815 ins_colors = OInspect.InspectColors
798 816 code_colors = PyColorize.ANSICodeColors
799 817 self.inspector = OInspect.Inspector(ins_colors,code_colors,'NoColor')
800 818 self.autoindent = 0
801 819
802 820 # Make some aliases automatically
803 821 # Prepare list of shell aliases to auto-define
804 822 if os.name == 'posix':
805 823 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
806 824 'mv mv -i','rm rm -i','cp cp -i',
807 825 'cat cat','less less','clear clear',
808 826 # a better ls
809 827 'ls ls -F',
810 828 # long ls
811 829 'll ls -lF',
812 830 # color ls
813 831 'lc ls -F -o --color',
814 832 # ls normal files only
815 833 'lf ls -F -o --color %l | grep ^-',
816 834 # ls symbolic links
817 835 'lk ls -F -o --color %l | grep ^l',
818 836 # directories or links to directories,
819 837 'ldir ls -F -o --color %l | grep /$',
820 838 # things which are executable
821 839 'lx ls -F -o --color %l | grep ^-..x',
822 840 )
823 841 elif os.name in ['nt','dos']:
824 842 auto_alias = ('dir dir /on', 'ls dir /on',
825 843 'ddir dir /ad /on', 'ldir dir /ad /on',
826 844 'mkdir mkdir','rmdir rmdir','echo echo',
827 845 'ren ren','cls cls','copy copy')
828 846 else:
829 847 auto_alias = ()
830 848 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
831 849 # Call the actual (public) initializer
832 850 self.init_auto_alias()
833 851 # end __init__
834 852
835 853 def set_hook(self,name,hook):
836 854 """set_hook(name,hook) -> sets an internal IPython hook.
837 855
838 856 IPython exposes some of its internal API as user-modifiable hooks. By
839 857 resetting one of these hooks, you can modify IPython's behavior to
840 858 call at runtime your own routines."""
841 859
842 860 # At some point in the future, this should validate the hook before it
843 861 # accepts it. Probably at least check that the hook takes the number
844 862 # of args it's supposed to.
845 863 setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
846 864
847 865 def set_custom_exc(self,exc_tuple,handler):
848 866 """set_custom_exc(exc_tuple,handler)
849 867
850 868 Set a custom exception handler, which will be called if any of the
851 869 exceptions in exc_tuple occur in the mainloop (specifically, in the
852 870 runcode() method.
853 871
854 872 Inputs:
855 873
856 874 - exc_tuple: a *tuple* of valid exceptions to call the defined
857 875 handler for. It is very important that you use a tuple, and NOT A
858 876 LIST here, because of the way Python's except statement works. If
859 877 you only want to trap a single exception, use a singleton tuple:
860 878
861 879 exc_tuple == (MyCustomException,)
862 880
863 881 - handler: this must be defined as a function with the following
864 882 basic interface: def my_handler(self,etype,value,tb).
865 883
866 884 This will be made into an instance method (via new.instancemethod)
867 885 of IPython itself, and it will be called if any of the exceptions
868 886 listed in the exc_tuple are caught. If the handler is None, an
869 887 internal basic one is used, which just prints basic info.
870 888
871 889 WARNING: by putting in your own exception handler into IPython's main
872 890 execution loop, you run a very good chance of nasty crashes. This
873 891 facility should only be used if you really know what you are doing."""
874 892
875 893 assert type(exc_tuple)==type(()) , \
876 894 "The custom exceptions must be given AS A TUPLE."
877 895
878 896 def dummy_handler(self,etype,value,tb):
879 897 print '*** Simple custom exception handler ***'
880 898 print 'Exception type :',etype
881 899 print 'Exception value:',value
882 900 print 'Traceback :',tb
883 901 print 'Source code :','\n'.join(self.buffer)
884 902
885 903 if handler is None: handler = dummy_handler
886 904
887 905 self.CustomTB = new.instancemethod(handler,self,self.__class__)
888 906 self.custom_exceptions = exc_tuple
889 907
890 908 def set_custom_completer(self,completer,pos=0):
891 909 """set_custom_completer(completer,pos=0)
892 910
893 911 Adds a new custom completer function.
894 912
895 913 The position argument (defaults to 0) is the index in the completers
896 914 list where you want the completer to be inserted."""
897 915
898 916 newcomp = new.instancemethod(completer,self.Completer,
899 917 self.Completer.__class__)
900 918 self.Completer.matchers.insert(pos,newcomp)
901 919
902 920 def complete(self,text):
903 921 """Return a sorted list of all possible completions on text.
904 922
905 923 Inputs:
906 924
907 925 - text: a string of text to be completed on.
908 926
909 927 This is a wrapper around the completion mechanism, similar to what
910 928 readline does at the command line when the TAB key is hit. By
911 929 exposing it as a method, it can be used by other non-readline
912 930 environments (such as GUIs) for text completion.
913 931
914 932 Simple usage example:
915 933
916 934 In [1]: x = 'hello'
917 935
918 936 In [2]: __IP.complete('x.l')
919 937 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
920 938
921 939 complete = self.Completer.complete
922 940 state = 0
923 941 # use a dict so we get unique keys, since ipyhton's multiple
924 942 # completers can return duplicates.
925 943 comps = {}
926 944 while True:
927 945 newcomp = complete(text,state)
928 946 if newcomp is None:
929 947 break
930 948 comps[newcomp] = 1
931 949 state += 1
932 950 outcomps = comps.keys()
933 951 outcomps.sort()
934 952 return outcomps
935 953
936 954 def post_config_initialization(self):
937 955 """Post configuration init method
938 956
939 957 This is called after the configuration files have been processed to
940 958 'finalize' the initialization."""
941 959
942 960 # dynamic data that survives through sessions
943 961 # XXX make the filename a config option?
944 962 persist_base = 'persist'
945 963 if self.rc.profile:
946 964 persist_base += '_%s' % self.rc.profile
947 965 self.persist_fname = os.path.join(self.rc.ipythondir,persist_base)
948 966
949 967 try:
950 968 self.persist = pickle.load(file(self.persist_fname))
951 969 except:
952 970 self.persist = {}
953 971
954 972 def init_auto_alias(self):
955 973 """Define some aliases automatically.
956 974
957 975 These are ALL parameter-less aliases"""
958 976 for alias,cmd in self.auto_alias:
959 977 self.alias_table[alias] = (0,cmd)
960 978
961 979 def alias_table_validate(self,verbose=0):
962 980 """Update information about the alias table.
963 981
964 982 In particular, make sure no Python keywords/builtins are in it."""
965 983
966 984 no_alias = self.no_alias
967 985 for k in self.alias_table.keys():
968 986 if k in no_alias:
969 987 del self.alias_table[k]
970 988 if verbose:
971 989 print ("Deleting alias <%s>, it's a Python "
972 990 "keyword or builtin." % k)
973 991
974 992 def set_autoindent(self,value=None):
975 993 """Set the autoindent flag, checking for readline support.
976 994
977 995 If called with no arguments, it acts as a toggle."""
978 996
979 997 if not self.has_readline:
980 998 if os.name == 'posix':
981 999 warn("The auto-indent feature requires the readline library")
982 1000 self.autoindent = 0
983 1001 return
984 1002 if value is None:
985 1003 self.autoindent = not self.autoindent
986 1004 else:
987 1005 self.autoindent = value
988 1006
989 1007 def rc_set_toggle(self,rc_field,value=None):
990 1008 """Set or toggle a field in IPython's rc config. structure.
991 1009
992 1010 If called with no arguments, it acts as a toggle.
993 1011
994 1012 If called with a non-existent field, the resulting AttributeError
995 1013 exception will propagate out."""
996 1014
997 1015 rc_val = getattr(self.rc,rc_field)
998 1016 if value is None:
999 1017 value = not rc_val
1000 1018 setattr(self.rc,rc_field,value)
1001 1019
1002 1020 def user_setup(self,ipythondir,rc_suffix,mode='install'):
1003 1021 """Install the user configuration directory.
1004 1022
1005 1023 Can be called when running for the first time or to upgrade the user's
1006 1024 .ipython/ directory with the mode parameter. Valid modes are 'install'
1007 1025 and 'upgrade'."""
1008 1026
1009 1027 def wait():
1010 1028 try:
1011 1029 raw_input("Please press <RETURN> to start IPython.")
1012 1030 except EOFError:
1013 1031 print >> Term.cout
1014 1032 print '*'*70
1015 1033
1016 1034 cwd = os.getcwd() # remember where we started
1017 1035 glb = glob.glob
1018 1036 print '*'*70
1019 1037 if mode == 'install':
1020 1038 print \
1021 1039 """Welcome to IPython. I will try to create a personal configuration directory
1022 1040 where you can customize many aspects of IPython's functionality in:\n"""
1023 1041 else:
1024 1042 print 'I am going to upgrade your configuration in:'
1025 1043
1026 1044 print ipythondir
1027 1045
1028 1046 rcdirend = os.path.join('IPython','UserConfig')
1029 1047 cfg = lambda d: os.path.join(d,rcdirend)
1030 1048 try:
1031 1049 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1032 1050 except IOError:
1033 1051 warning = """
1034 1052 Installation error. IPython's directory was not found.
1035 1053
1036 1054 Check the following:
1037 1055
1038 1056 The ipython/IPython directory should be in a directory belonging to your
1039 1057 PYTHONPATH environment variable (that is, it should be in a directory
1040 1058 belonging to sys.path). You can copy it explicitly there or just link to it.
1041 1059
1042 1060 IPython will proceed with builtin defaults.
1043 1061 """
1044 1062 warn(warning)
1045 1063 wait()
1046 1064 return
1047 1065
1048 1066 if mode == 'install':
1049 1067 try:
1050 1068 shutil.copytree(rcdir,ipythondir)
1051 1069 os.chdir(ipythondir)
1052 1070 rc_files = glb("ipythonrc*")
1053 1071 for rc_file in rc_files:
1054 1072 os.rename(rc_file,rc_file+rc_suffix)
1055 1073 except:
1056 1074 warning = """
1057 1075
1058 1076 There was a problem with the installation:
1059 1077 %s
1060 1078 Try to correct it or contact the developers if you think it's a bug.
1061 1079 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1062 1080 warn(warning)
1063 1081 wait()
1064 1082 return
1065 1083
1066 1084 elif mode == 'upgrade':
1067 1085 try:
1068 1086 os.chdir(ipythondir)
1069 1087 except:
1070 1088 print """
1071 1089 Can not upgrade: changing to directory %s failed. Details:
1072 1090 %s
1073 1091 """ % (ipythondir,sys.exc_info()[1])
1074 1092 wait()
1075 1093 return
1076 1094 else:
1077 1095 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1078 1096 for new_full_path in sources:
1079 1097 new_filename = os.path.basename(new_full_path)
1080 1098 if new_filename.startswith('ipythonrc'):
1081 1099 new_filename = new_filename + rc_suffix
1082 1100 # The config directory should only contain files, skip any
1083 1101 # directories which may be there (like CVS)
1084 1102 if os.path.isdir(new_full_path):
1085 1103 continue
1086 1104 if os.path.exists(new_filename):
1087 1105 old_file = new_filename+'.old'
1088 1106 if os.path.exists(old_file):
1089 1107 os.remove(old_file)
1090 1108 os.rename(new_filename,old_file)
1091 1109 shutil.copy(new_full_path,new_filename)
1092 1110 else:
1093 1111 raise ValueError,'unrecognized mode for install:',`mode`
1094 1112
1095 1113 # Fix line-endings to those native to each platform in the config
1096 1114 # directory.
1097 1115 try:
1098 1116 os.chdir(ipythondir)
1099 1117 except:
1100 1118 print """
1101 1119 Problem: changing to directory %s failed.
1102 1120 Details:
1103 1121 %s
1104 1122
1105 1123 Some configuration files may have incorrect line endings. This should not
1106 1124 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1107 1125 wait()
1108 1126 else:
1109 1127 for fname in glb('ipythonrc*'):
1110 1128 try:
1111 1129 native_line_ends(fname,backup=0)
1112 1130 except IOError:
1113 1131 pass
1114 1132
1115 1133 if mode == 'install':
1116 1134 print """
1117 1135 Successful installation!
1118 1136
1119 1137 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1120 1138 IPython manual (there are both HTML and PDF versions supplied with the
1121 1139 distribution) to make sure that your system environment is properly configured
1122 1140 to take advantage of IPython's features."""
1123 1141 else:
1124 1142 print """
1125 1143 Successful upgrade!
1126 1144
1127 1145 All files in your directory:
1128 1146 %(ipythondir)s
1129 1147 which would have been overwritten by the upgrade were backed up with a .old
1130 1148 extension. If you had made particular customizations in those files you may
1131 1149 want to merge them back into the new files.""" % locals()
1132 1150 wait()
1133 1151 os.chdir(cwd)
1134 1152 # end user_setup()
1135 1153
1136 1154 def atexit_operations(self):
1137 1155 """This will be executed at the time of exit.
1138 1156
1139 1157 Saving of persistent data should be performed here. """
1140 1158
1141 1159 # input history
1142 1160 self.savehist()
1143 1161
1144 1162 # Cleanup all tempfiles left around
1145 1163 for tfile in self.tempfiles:
1146 1164 try:
1147 1165 os.unlink(tfile)
1148 1166 except OSError:
1149 1167 pass
1150 1168
1151 1169 # save the "persistent data" catch-all dictionary
1152 1170 try:
1153 1171 pickle.dump(self.persist, open(self.persist_fname,"w"))
1154 1172 except:
1155 1173 print "*** ERROR *** persistent data saving failed."
1156 1174
1157 1175 def savehist(self):
1158 1176 """Save input history to a file (via readline library)."""
1159 1177 try:
1160 1178 self.readline.write_history_file(self.histfile)
1161 1179 except:
1162 1180 print 'Unable to save IPython command history to file: ' + \
1163 1181 `self.histfile`
1164 1182
1165 1183 def pre_readline(self):
1166 1184 """readline hook to be used at the start of each line.
1167 1185
1168 1186 Currently it handles auto-indent only."""
1169 1187
1170 1188 self.readline.insert_text(' '* self.readline_indent)
1171 1189
1172 1190 def init_readline(self):
1173 1191 """Command history completion/saving/reloading."""
1174 1192 try:
1175 1193 import readline
1176 1194 self.Completer = MagicCompleter(self,
1177 1195 self.user_ns,
1178 1196 self.rc.readline_omit__names,
1179 1197 self.alias_table)
1180 1198 except ImportError,NameError:
1181 1199 # If FlexCompleter failed to import, MagicCompleter won't be
1182 1200 # defined. This can happen because of a problem with readline
1183 1201 self.has_readline = 0
1184 1202 # no point in bugging windows users with this every time:
1185 1203 if os.name == 'posix':
1186 1204 warn('Readline services not available on this platform.')
1187 1205 else:
1188 1206 import atexit
1189 1207
1190 1208 # Platform-specific configuration
1191 1209 if os.name == 'nt':
1192 1210 # readline under Windows modifies the default exit behavior
1193 1211 # from being Ctrl-Z/Return to the Unix Ctrl-D one.
1194 1212 __builtin__.exit = __builtin__.quit = \
1195 1213 ('Use Ctrl-D (i.e. EOF) to exit. '
1196 1214 'Use %Exit or %Quit to exit without confirmation.')
1197 1215 self.readline_startup_hook = readline.set_pre_input_hook
1198 1216 else:
1199 1217 self.readline_startup_hook = readline.set_startup_hook
1200 1218
1201 1219 # Load user's initrc file (readline config)
1202 1220 inputrc_name = os.environ.get('INPUTRC')
1203 1221 if inputrc_name is None:
1204 1222 home_dir = get_home_dir()
1205 1223 if home_dir is not None:
1206 1224 inputrc_name = os.path.join(home_dir,'.inputrc')
1207 1225 if os.path.isfile(inputrc_name):
1208 1226 try:
1209 1227 readline.read_init_file(inputrc_name)
1210 1228 except:
1211 1229 warn('Problems reading readline initialization file <%s>'
1212 1230 % inputrc_name)
1213 1231
1214 1232 self.has_readline = 1
1215 1233 self.readline = readline
1216 1234 self.readline_indent = 0 # for auto-indenting via readline
1217 1235 # save this in sys so embedded copies can restore it properly
1218 1236 sys.ipcompleter = self.Completer.complete
1219 1237 readline.set_completer(self.Completer.complete)
1220 1238
1221 1239 # Configure readline according to user's prefs
1222 1240 for rlcommand in self.rc.readline_parse_and_bind:
1223 1241 readline.parse_and_bind(rlcommand)
1224 1242
1225 1243 # remove some chars from the delimiters list
1226 1244 delims = readline.get_completer_delims()
1227 1245 delims = delims.translate(string._idmap,
1228 1246 self.rc.readline_remove_delims)
1229 1247 readline.set_completer_delims(delims)
1230 1248 # otherwise we end up with a monster history after a while:
1231 1249 readline.set_history_length(1000)
1232 1250 try:
1233 1251 #print '*** Reading readline history' # dbg
1234 1252 readline.read_history_file(self.histfile)
1235 1253 except IOError:
1236 1254 pass # It doesn't exist yet.
1237 1255
1238 1256 atexit.register(self.atexit_operations)
1239 1257 del atexit
1240 1258
1241 1259 # Configure auto-indent for all platforms
1242 1260 self.set_autoindent(self.rc.autoindent)
1243 1261
1244 1262 def showsyntaxerror(self, filename=None):
1245 1263 """Display the syntax error that just occurred.
1246 1264
1247 1265 This doesn't display a stack trace because there isn't one.
1248 1266
1249 1267 If a filename is given, it is stuffed in the exception instead
1250 1268 of what was there before (because Python's parser always uses
1251 1269 "<string>" when reading from a string).
1252 1270 """
1253 1271 type, value, sys.last_traceback = sys.exc_info()
1254 1272 sys.last_type = type
1255 1273 sys.last_value = value
1256 1274 if filename and type is SyntaxError:
1257 1275 # Work hard to stuff the correct filename in the exception
1258 1276 try:
1259 1277 msg, (dummy_filename, lineno, offset, line) = value
1260 1278 except:
1261 1279 # Not the format we expect; leave it alone
1262 1280 pass
1263 1281 else:
1264 1282 # Stuff in the right filename
1265 1283 try:
1266 1284 # Assume SyntaxError is a class exception
1267 1285 value = SyntaxError(msg, (filename, lineno, offset, line))
1268 1286 except:
1269 1287 # If that failed, assume SyntaxError is a string
1270 1288 value = msg, (filename, lineno, offset, line)
1271 1289 self.SyntaxTB(type,value,[])
1272 1290
1273 1291 def debugger(self):
1274 1292 """Call the pdb debugger."""
1275 1293
1276 1294 if not self.rc.pdb:
1277 1295 return
1278 1296 pdb.pm()
1279 1297
1280 1298 def showtraceback(self,exc_tuple = None,filename=None):
1281 1299 """Display the exception that just occurred."""
1282 1300
1283 1301 # Though this won't be called by syntax errors in the input line,
1284 1302 # there may be SyntaxError cases whith imported code.
1285 1303 if exc_tuple is None:
1286 1304 type, value, tb = sys.exc_info()
1287 1305 else:
1288 1306 type, value, tb = exc_tuple
1289 1307 if type is SyntaxError:
1290 1308 self.showsyntaxerror(filename)
1291 1309 else:
1292 1310 sys.last_type = type
1293 1311 sys.last_value = value
1294 1312 sys.last_traceback = tb
1295 1313 self.InteractiveTB()
1296 1314 if self.InteractiveTB.call_pdb and self.has_readline:
1297 1315 # pdb mucks up readline, fix it back
1298 1316 self.readline.set_completer(self.Completer.complete)
1299 1317
1300 1318 def update_cache(self, line):
1301 1319 """puts line into cache"""
1302 1320 self.inputcache.insert(0, line) # This copies the cache every time ... :-(
1303 1321 if len(self.inputcache) >= self.CACHELENGTH:
1304 1322 self.inputcache.pop() # This not :-)
1305 1323
1306 def name_space_init(self):
1307 """Create local namespace."""
1308 # We want this to be a method to facilitate embedded initialization.
1309 code.InteractiveConsole.__init__(self,self.user_ns)
1310
1311 1324 def mainloop(self,banner=None):
1312 1325 """Creates the local namespace and starts the mainloop.
1313 1326
1314 1327 If an optional banner argument is given, it will override the
1315 1328 internally created default banner."""
1316 1329
1317 self.name_space_init()
1318 1330 if self.rc.c: # Emulate Python's -c option
1319 1331 self.exec_init_cmd()
1320 1332 if banner is None:
1321 1333 if self.rc.banner:
1322 1334 banner = self.BANNER+self.banner2
1323 1335 else:
1324 1336 banner = ''
1325 1337 self.interact(banner)
1326 1338
1327 1339 def exec_init_cmd(self):
1328 1340 """Execute a command given at the command line.
1329 1341
1330 1342 This emulates Python's -c option."""
1331 1343
1332 1344 sys.argv = ['-c']
1333 1345 self.push(self.rc.c)
1334 1346
1335 1347 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1336 1348 """Embeds IPython into a running python program.
1337 1349
1338 1350 Input:
1339 1351
1340 1352 - header: An optional header message can be specified.
1341 1353
1342 1354 - local_ns, global_ns: working namespaces. If given as None, the
1343 1355 IPython-initialized one is updated with __main__.__dict__, so that
1344 1356 program variables become visible but user-specific configuration
1345 1357 remains possible.
1346 1358
1347 1359 - stack_depth: specifies how many levels in the stack to go to
1348 1360 looking for namespaces (when local_ns and global_ns are None). This
1349 1361 allows an intermediate caller to make sure that this function gets
1350 1362 the namespace from the intended level in the stack. By default (0)
1351 1363 it will get its locals and globals from the immediate caller.
1352 1364
1353 1365 Warning: it's possible to use this in a program which is being run by
1354 1366 IPython itself (via %run), but some funny things will happen (a few
1355 1367 globals get overwritten). In the future this will be cleaned up, as
1356 1368 there is no fundamental reason why it can't work perfectly."""
1357 1369
1358 # Patch for global embedding to make sure that things don't overwrite
1359 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1360 # FIXME. Test this a bit more carefully (the if.. is new)
1361 if local_ns is None and global_ns is None:
1362 self.user_ns.update(__main__.__dict__)
1363
1364 1370 # Get locals and globals from caller
1365 1371 if local_ns is None or global_ns is None:
1366 1372 call_frame = sys._getframe(stack_depth).f_back
1367 1373
1368 1374 if local_ns is None:
1369 1375 local_ns = call_frame.f_locals
1370 1376 if global_ns is None:
1371 1377 global_ns = call_frame.f_globals
1372 1378
1373 1379 # Update namespaces and fire up interpreter
1374 self.user_ns.update(local_ns)
1375 self.interact(header)
1380 self.user_ns = local_ns
1381 self.user_global_ns = global_ns
1382
1383 # Patch for global embedding to make sure that things don't overwrite
1384 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1385 # FIXME. Test this a bit more carefully (the if.. is new)
1386 if local_ns is None and global_ns is None:
1387 self.user_global_ns.update(__main__.__dict__)
1376 1388
1377 # Remove locals from namespace
1378 for k in local_ns:
1379 del self.user_ns[k]
1389 self.interact(header)
1380 1390
1381 1391 def interact(self, banner=None):
1382 1392 """Closely emulate the interactive Python console.
1383 1393
1384 1394 The optional banner argument specify the banner to print
1385 1395 before the first interaction; by default it prints a banner
1386 1396 similar to the one printed by the real Python interpreter,
1387 1397 followed by the current class name in parentheses (so as not
1388 1398 to confuse this with the real interpreter -- since it's so
1389 1399 close!).
1390 1400
1391 1401 """
1392 1402 cprt = 'Type "copyright", "credits" or "license" for more information.'
1393 1403 if banner is None:
1394 1404 self.write("Python %s on %s\n%s\n(%s)\n" %
1395 1405 (sys.version, sys.platform, cprt,
1396 1406 self.__class__.__name__))
1397 1407 else:
1398 1408 self.write(banner)
1399 1409
1400 1410 more = 0
1401 1411
1402 1412 # Mark activity in the builtins
1403 1413 __builtin__.__dict__['__IPYTHON__active'] += 1
1404 1414
1405 1415 # exit_now is set by a call to %Exit or %Quit
1406 1416 while not self.exit_now:
1407 1417 try:
1408 1418 if more:
1409 1419 prompt = self.outputcache.prompt2
1410 1420 if self.autoindent:
1411 1421 self.readline_startup_hook(self.pre_readline)
1412 1422 else:
1413 1423 prompt = self.outputcache.prompt1
1414 1424 try:
1415 1425 line = self.raw_input(prompt)
1416 1426 if self.autoindent:
1417 1427 self.readline_startup_hook(None)
1418 1428 except EOFError:
1419 1429 if self.autoindent:
1420 1430 self.readline_startup_hook(None)
1421 1431 self.write("\n")
1422 1432 if self.rc.confirm_exit:
1423 1433 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
1424 1434 break
1425 1435 else:
1426 1436 break
1427 1437 else:
1428 1438 more = self.push(line)
1429 1439 # Auto-indent management
1430 1440 if self.autoindent:
1431 1441 if line:
1432 1442 ini_spaces = re.match('^(\s+)',line)
1433 1443 if ini_spaces:
1434 1444 nspaces = ini_spaces.end()
1435 1445 else:
1436 1446 nspaces = 0
1437 1447 self.readline_indent = nspaces
1438 1448
1439 1449 if line[-1] == ':':
1440 1450 self.readline_indent += 4
1441 1451 elif re.match(r'^\s+raise|^\s+return',line):
1442 1452 self.readline_indent -= 4
1443 1453 else:
1444 1454 self.readline_indent = 0
1445 1455
1446 1456 except KeyboardInterrupt:
1447 1457 self.write("\nKeyboardInterrupt\n")
1448 1458 self.resetbuffer()
1449 1459 more = 0
1450 1460 # keep cache in sync with the prompt counter:
1451 1461 self.outputcache.prompt_count -= 1
1452 1462
1453 1463 if self.autoindent:
1454 1464 self.readline_indent = 0
1455 1465
1456 1466 except bdb.BdbQuit:
1457 1467 warn("The Python debugger has exited with a BdbQuit exception.\n"
1458 1468 "Because of how pdb handles the stack, it is impossible\n"
1459 1469 "for IPython to properly format this particular exception.\n"
1460 1470 "IPython will resume normal operation.")
1461 1471
1462 1472 # We are off again...
1463 1473 __builtin__.__dict__['__IPYTHON__active'] -= 1
1464 1474
1465 1475 def excepthook(self, type, value, tb):
1466 1476 """One more defense for GUI apps that call sys.excepthook.
1467 1477
1468 1478 GUI frameworks like wxPython trap exceptions and call
1469 1479 sys.excepthook themselves. I guess this is a feature that
1470 1480 enables them to keep running after exceptions that would
1471 1481 otherwise kill their mainloop. This is a bother for IPython
1472 1482 which excepts to catch all of the program exceptions with a try:
1473 1483 except: statement.
1474 1484
1475 1485 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1476 1486 any app directly invokes sys.excepthook, it will look to the user like
1477 1487 IPython crashed. In order to work around this, we can disable the
1478 1488 CrashHandler and replace it with this excepthook instead, which prints a
1479 1489 regular traceback using our InteractiveTB. In this fashion, apps which
1480 1490 call sys.excepthook will generate a regular-looking exception from
1481 1491 IPython, and the CrashHandler will only be triggered by real IPython
1482 1492 crashes.
1483 1493
1484 1494 This hook should be used sparingly, only in places which are not likely
1485 1495 to be true IPython errors.
1486 1496 """
1487 1497
1488 1498 self.InteractiveTB(type, value, tb, tb_offset=0)
1489 1499 if self.InteractiveTB.call_pdb and self.has_readline:
1490 1500 self.readline.set_completer(self.Completer.complete)
1491 1501
1492 1502 def call_alias(self,alias,rest=''):
1493 1503 """Call an alias given its name and the rest of the line.
1494 1504
1495 1505 This function MUST be given a proper alias, because it doesn't make
1496 1506 any checks when looking up into the alias table. The caller is
1497 1507 responsible for invoking it only with a valid alias."""
1498 1508
1499 1509 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1500 1510 nargs,cmd = self.alias_table[alias]
1501 1511 # Expand the %l special to be the user's input line
1502 1512 if cmd.find('%l') >= 0:
1503 1513 cmd = cmd.replace('%l',rest)
1504 1514 rest = ''
1505 1515 if nargs==0:
1506 1516 # Simple, argument-less aliases
1507 1517 cmd = '%s %s' % (cmd,rest)
1508 1518 else:
1509 1519 # Handle aliases with positional arguments
1510 1520 args = rest.split(None,nargs)
1511 1521 if len(args)< nargs:
1512 1522 error('Alias <%s> requires %s arguments, %s given.' %
1513 1523 (alias,nargs,len(args)))
1514 1524 return
1515 1525 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1516 1526 # Now call the macro, evaluating in the user's namespace
1517 1527 try:
1518 1528 self.system(cmd)
1519 1529 except:
1520 1530 self.showtraceback()
1521 1531
1522 1532 def runlines(self,lines):
1523 1533 """Run a string of one or more lines of source.
1524 1534
1525 1535 This method is capable of running a string containing multiple source
1526 1536 lines, as if they had been entered at the IPython prompt. Since it
1527 1537 exposes IPython's processing machinery, the given strings can contain
1528 1538 magic calls (%magic), special shell access (!cmd), etc."""
1529 1539
1530 1540 # We must start with a clean buffer, in case this is run from an
1531 1541 # interactive IPython session (via a magic, for example).
1532 1542 self.resetbuffer()
1533 1543 lines = lines.split('\n')
1534 1544 more = 0
1535 1545 for line in lines:
1536 1546 # skip blank lines so we don't mess up the prompt counter, but do
1537 1547 # NOT skip even a blank line if we are in a code block (more is
1538 1548 # true)
1539 1549 if line or more:
1540 1550 more = self.push((self.prefilter(line,more)))
1541 1551 # IPython's runsource returns None if there was an error
1542 1552 # compiling the code. This allows us to stop processing right
1543 1553 # away, so the user gets the error message at the right place.
1544 1554 if more is None:
1545 1555 break
1546 1556 # final newline in case the input didn't have it, so that the code
1547 1557 # actually does get executed
1548 1558 if more:
1549 1559 self.push('\n')
1550 1560
1551 1561 def runsource(self, source, filename="<input>", symbol="single"):
1552 1562 """Compile and run some source in the interpreter.
1553 1563
1554 1564 Arguments are as for compile_command().
1555 1565
1556 1566 One several things can happen:
1557 1567
1558 1568 1) The input is incorrect; compile_command() raised an
1559 1569 exception (SyntaxError or OverflowError). A syntax traceback
1560 1570 will be printed by calling the showsyntaxerror() method.
1561 1571
1562 1572 2) The input is incomplete, and more input is required;
1563 1573 compile_command() returned None. Nothing happens.
1564 1574
1565 1575 3) The input is complete; compile_command() returned a code
1566 1576 object. The code is executed by calling self.runcode() (which
1567 1577 also handles run-time exceptions, except for SystemExit).
1568 1578
1569 1579 The return value is:
1570 1580
1571 1581 - True in case 2
1572 1582
1573 1583 - False in the other cases, unless an exception is raised, where
1574 1584 None is returned instead. This can be used by external callers to
1575 1585 know whether to continue feeding input or not.
1576 1586
1577 1587 The return value can be used to decide whether to use sys.ps1 or
1578 1588 sys.ps2 to prompt the next line."""
1579 1589
1580 1590 try:
1581 1591 code = self.compile(source, filename, symbol)
1582 1592 except (OverflowError, SyntaxError, ValueError):
1583 1593 # Case 1
1584 1594 self.showsyntaxerror(filename)
1585 1595 return None
1586 1596
1587 1597 if code is None:
1588 1598 # Case 2
1589 1599 return True
1590 1600
1591 1601 # Case 3
1592 1602 # We store the code object so that threaded shells and
1593 1603 # custom exception handlers can access all this info if needed.
1594 1604 # The source corresponding to this can be obtained from the
1595 1605 # buffer attribute as '\n'.join(self.buffer).
1596 1606 self.code_to_run = code
1597 1607 # now actually execute the code object
1598 1608 if self.runcode(code) == 0:
1599 1609 return False
1600 1610 else:
1601 1611 return None
1602 1612
1603 1613 def runcode(self,code_obj):
1604 1614 """Execute a code object.
1605 1615
1606 1616 When an exception occurs, self.showtraceback() is called to display a
1607 1617 traceback.
1608 1618
1609 1619 Return value: a flag indicating whether the code to be run completed
1610 1620 successfully:
1611 1621
1612 1622 - 0: successful execution.
1613 1623 - 1: an error occurred.
1614 1624 """
1615 1625
1616 1626 # Set our own excepthook in case the user code tries to call it
1617 1627 # directly, so that the IPython crash handler doesn't get triggered
1618 1628 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1619 1629 outflag = 1 # happens in more places, so it's easier as default
1620 1630 try:
1621 1631 try:
1622 exec code_obj in self.locals
1632 exec code_obj in self.user_global_ns, self.user_ns
1623 1633 finally:
1624 1634 # Reset our crash handler in place
1625 1635 sys.excepthook = old_excepthook
1626 1636 except SystemExit:
1627 1637 self.resetbuffer()
1628 1638 self.showtraceback()
1629 1639 warn( __builtin__.exit,level=1)
1630 1640 except self.custom_exceptions:
1631 1641 etype,value,tb = sys.exc_info()
1632 1642 self.CustomTB(etype,value,tb)
1633 1643 except:
1634 1644 self.showtraceback()
1635 1645 else:
1636 1646 outflag = 0
1637 1647 if code.softspace(sys.stdout, 0):
1638 1648 print
1639 1649 # Flush out code object which has been run (and source)
1640 1650 self.code_to_run = None
1641 1651 return outflag
1642 1652
1643 1653 def raw_input(self, prompt=""):
1644 1654 """Write a prompt and read a line.
1645 1655
1646 1656 The returned line does not include the trailing newline.
1647 1657 When the user enters the EOF key sequence, EOFError is raised.
1648 1658
1649 1659 The base implementation uses the built-in function
1650 1660 raw_input(); a subclass may replace this with a different
1651 1661 implementation.
1652 1662 """
1653 1663 return self.prefilter(raw_input_original(prompt),
1654 1664 prompt==self.outputcache.prompt2)
1655 1665
1656 1666 def split_user_input(self,line):
1657 1667 """Split user input into pre-char, function part and rest."""
1658 1668
1659 1669 lsplit = self.line_split.match(line)
1660 1670 if lsplit is None: # no regexp match returns None
1661 1671 try:
1662 1672 iFun,theRest = line.split(None,1)
1663 1673 except ValueError:
1664 1674 iFun,theRest = line,''
1665 1675 pre = re.match('^(\s*)(.*)',line).groups()[0]
1666 1676 else:
1667 1677 pre,iFun,theRest = lsplit.groups()
1668 1678
1669 1679 #print 'line:<%s>' % line # dbg
1670 1680 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1671 1681 return pre,iFun.strip(),theRest
1672 1682
1673 1683 def _prefilter(self, line, continue_prompt):
1674 1684 """Calls different preprocessors, depending on the form of line."""
1675 1685
1676 1686 # All handlers *must* return a value, even if it's blank ('').
1677 1687
1678 1688 # Lines are NOT logged here. Handlers should process the line as
1679 1689 # needed, update the cache AND log it (so that the input cache array
1680 1690 # stays synced).
1681 1691
1682 1692 # This function is _very_ delicate, and since it's also the one which
1683 1693 # determines IPython's response to user input, it must be as efficient
1684 1694 # as possible. For this reason it has _many_ returns in it, trying
1685 1695 # always to exit as quickly as it can figure out what it needs to do.
1686 1696
1687 1697 # This function is the main responsible for maintaining IPython's
1688 1698 # behavior respectful of Python's semantics. So be _very_ careful if
1689 1699 # making changes to anything here.
1690 1700
1691 1701 #.....................................................................
1692 1702 # Code begins
1693 1703
1694 1704 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1695 1705
1696 1706 # save the line away in case we crash, so the post-mortem handler can
1697 1707 # record it
1698 1708 self._last_input_line = line
1699 1709
1700 1710 #print '***line: <%s>' % line # dbg
1701 1711
1702 1712 # the input history needs to track even empty lines
1703 1713 if not line.strip():
1704 1714 if not continue_prompt:
1705 1715 self.outputcache.prompt_count -= 1
1706 1716 return self.handle_normal('',continue_prompt)
1707 1717
1708 1718 # print '***cont',continue_prompt # dbg
1709 1719 # special handlers are only allowed for single line statements
1710 1720 if continue_prompt and not self.rc.multi_line_specials:
1711 1721 return self.handle_normal(line,continue_prompt)
1712 1722
1713 1723 # For the rest, we need the structure of the input
1714 1724 pre,iFun,theRest = self.split_user_input(line)
1715 1725 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1716 1726
1717 1727 # First check for explicit escapes in the last/first character
1718 1728 handler = None
1719 1729 if line[-1] == self.ESC_HELP:
1720 1730 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1721 1731 if handler is None:
1722 1732 # look at the first character of iFun, NOT of line, so we skip
1723 1733 # leading whitespace in multiline input
1724 1734 handler = self.esc_handlers.get(iFun[0:1])
1725 1735 if handler is not None:
1726 1736 return handler(line,continue_prompt,pre,iFun,theRest)
1727 1737 # Emacs ipython-mode tags certain input lines
1728 1738 if line.endswith('# PYTHON-MODE'):
1729 1739 return self.handle_emacs(line,continue_prompt)
1730 1740
1731 1741 # Next, check if we can automatically execute this thing
1732 1742
1733 1743 # Allow ! in multi-line statements if multi_line_specials is on:
1734 1744 if continue_prompt and self.rc.multi_line_specials and \
1735 1745 iFun.startswith(self.ESC_SHELL):
1736 1746 return self.handle_shell_escape(line,continue_prompt,
1737 1747 pre=pre,iFun=iFun,
1738 1748 theRest=theRest)
1739 1749
1740 1750 # Let's try to find if the input line is a magic fn
1741 1751 oinfo = None
1742 1752 if hasattr(self,'magic_'+iFun):
1743 1753 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1744 1754 if oinfo['ismagic']:
1745 1755 # Be careful not to call magics when a variable assignment is
1746 1756 # being made (ls='hi', for example)
1747 1757 if self.rc.automagic and \
1748 1758 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1749 1759 (self.rc.multi_line_specials or not continue_prompt):
1750 1760 return self.handle_magic(line,continue_prompt,
1751 1761 pre,iFun,theRest)
1752 1762 else:
1753 1763 return self.handle_normal(line,continue_prompt)
1754 1764
1755 1765 # If the rest of the line begins with an (in)equality, assginment or
1756 1766 # function call, we should not call _ofind but simply execute it.
1757 1767 # This avoids spurious geattr() accesses on objects upon assignment.
1758 1768 #
1759 1769 # It also allows users to assign to either alias or magic names true
1760 1770 # python variables (the magic/alias systems always take second seat to
1761 1771 # true python code).
1762 1772 if theRest and theRest[0] in '!=()':
1763 1773 return self.handle_normal(line,continue_prompt)
1764 1774
1765 1775 if oinfo is None:
1766 1776 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1767 1777
1768 1778 if not oinfo['found']:
1769 1779 return self.handle_normal(line,continue_prompt)
1770 1780 else:
1771 1781 #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg
1772 1782 if oinfo['isalias']:
1773 1783 return self.handle_alias(line,continue_prompt,
1774 1784 pre,iFun,theRest)
1775 1785
1776 1786 if self.rc.autocall and \
1777 1787 not self.re_exclude_auto.match(theRest) and \
1778 1788 self.re_fun_name.match(iFun) and \
1779 1789 callable(oinfo['obj']) :
1780 1790 #print 'going auto' # dbg
1781 1791 return self.handle_auto(line,continue_prompt,pre,iFun,theRest)
1782 1792 else:
1783 1793 #print 'was callable?', callable(oinfo['obj']) # dbg
1784 1794 return self.handle_normal(line,continue_prompt)
1785 1795
1786 1796 # If we get here, we have a normal Python line. Log and return.
1787 1797 return self.handle_normal(line,continue_prompt)
1788 1798
1789 1799 def _prefilter_dumb(self, line, continue_prompt):
1790 1800 """simple prefilter function, for debugging"""
1791 1801 return self.handle_normal(line,continue_prompt)
1792 1802
1793 1803 # Set the default prefilter() function (this can be user-overridden)
1794 1804 prefilter = _prefilter
1795 1805
1796 1806 def handle_normal(self,line,continue_prompt=None,
1797 1807 pre=None,iFun=None,theRest=None):
1798 1808 """Handle normal input lines. Use as a template for handlers."""
1799 1809
1800 1810 self.log(line,continue_prompt)
1801 1811 self.update_cache(line)
1802 1812 return line
1803 1813
1804 1814 def handle_alias(self,line,continue_prompt=None,
1805 1815 pre=None,iFun=None,theRest=None):
1806 1816 """Handle alias input lines. """
1807 1817
1808 1818 theRest = esc_quotes(theRest)
1809 1819 line_out = "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest)
1810 1820 self.log(line_out,continue_prompt)
1811 1821 self.update_cache(line_out)
1812 1822 return line_out
1813 1823
1814 1824 def handle_shell_escape(self, line, continue_prompt=None,
1815 1825 pre=None,iFun=None,theRest=None):
1816 1826 """Execute the line in a shell, empty return value"""
1817 1827
1818 1828 #print 'line in :', `line` # dbg
1819 1829 # Example of a special handler. Others follow a similar pattern.
1820 1830 if continue_prompt: # multi-line statements
1821 1831 if iFun.startswith('!!'):
1822 1832 print 'SyntaxError: !! is not allowed in multiline statements'
1823 1833 return pre
1824 1834 else:
1825 1835 cmd = ("%s %s" % (iFun[1:],theRest)).replace('"','\\"')
1826 1836 line_out = '%s%s.system("%s")' % (pre,self.name,cmd)
1827 1837 #line_out = ('%s%s.system(' % (pre,self.name)) + repr(cmd) + ')'
1828 1838 else: # single-line input
1829 1839 if line.startswith('!!'):
1830 1840 # rewrite iFun/theRest to properly hold the call to %sx and
1831 1841 # the actual command to be executed, so handle_magic can work
1832 1842 # correctly
1833 1843 theRest = '%s %s' % (iFun[2:],theRest)
1834 1844 iFun = 'sx'
1835 1845 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]),
1836 1846 continue_prompt,pre,iFun,theRest)
1837 1847 else:
1838 1848 cmd = esc_quotes(line[1:])
1839 1849 line_out = '%s.system("%s")' % (self.name,cmd)
1840 1850 #line_out = ('%s.system(' % self.name) + repr(cmd)+ ')'
1841 1851 # update cache/log and return
1842 1852 self.log(line_out,continue_prompt)
1843 1853 self.update_cache(line_out) # readline cache gets normal line
1844 1854 #print 'line out r:', `line_out` # dbg
1845 1855 #print 'line out s:', line_out # dbg
1846 1856 return line_out
1847 1857
1848 1858 def handle_magic(self, line, continue_prompt=None,
1849 1859 pre=None,iFun=None,theRest=None):
1850 1860 """Execute magic functions.
1851 1861
1852 1862 Also log them with a prepended # so the log is clean Python."""
1853 1863
1854 1864 cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest)))
1855 1865 self.log(cmd,continue_prompt)
1856 1866 self.update_cache(line)
1857 1867 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
1858 1868 return cmd
1859 1869
1860 1870 def handle_auto(self, line, continue_prompt=None,
1861 1871 pre=None,iFun=None,theRest=None):
1862 1872 """Hande lines which can be auto-executed, quoting if requested."""
1863 1873
1864 1874 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1865 1875
1866 1876 # This should only be active for single-line input!
1867 1877 if continue_prompt:
1868 1878 return line
1869 1879
1870 1880 if pre == self.ESC_QUOTE:
1871 1881 # Auto-quote splitting on whitespace
1872 1882 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
1873 1883 elif pre == self.ESC_QUOTE2:
1874 1884 # Auto-quote whole string
1875 1885 newcmd = '%s("%s")' % (iFun,theRest)
1876 1886 else:
1877 1887 # Auto-paren
1878 1888 if theRest[0:1] in ('=','['):
1879 1889 # Don't autocall in these cases. They can be either
1880 1890 # rebindings of an existing callable's name, or item access
1881 1891 # for an object which is BOTH callable and implements
1882 1892 # __getitem__.
1883 1893 return '%s %s' % (iFun,theRest)
1884 1894 if theRest.endswith(';'):
1885 1895 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
1886 1896 else:
1887 1897 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1888 1898
1889 1899 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
1890 1900 # log what is now valid Python, not the actual user input (without the
1891 1901 # final newline)
1892 1902 self.log(newcmd,continue_prompt)
1893 1903 return newcmd
1894 1904
1895 1905 def handle_help(self, line, continue_prompt=None,
1896 1906 pre=None,iFun=None,theRest=None):
1897 1907 """Try to get some help for the object.
1898 1908
1899 1909 obj? or ?obj -> basic information.
1900 1910 obj?? or ??obj -> more details.
1901 1911 """
1902 1912
1903 1913 # We need to make sure that we don't process lines which would be
1904 1914 # otherwise valid python, such as "x=1 # what?"
1905 1915 try:
1906 1916 code.compile_command(line)
1907 1917 except SyntaxError:
1908 1918 # We should only handle as help stuff which is NOT valid syntax
1909 1919 if line[0]==self.ESC_HELP:
1910 1920 line = line[1:]
1911 1921 elif line[-1]==self.ESC_HELP:
1912 1922 line = line[:-1]
1913 1923 self.log('#?'+line)
1914 1924 self.update_cache(line)
1915 1925 if line:
1916 1926 self.magic_pinfo(line)
1917 1927 else:
1918 1928 page(self.usage,screen_lines=self.rc.screen_length)
1919 1929 return '' # Empty string is needed here!
1920 1930 except:
1921 1931 # Pass any other exceptions through to the normal handler
1922 1932 return self.handle_normal(line,continue_prompt)
1923 1933 else:
1924 1934 # If the code compiles ok, we should handle it normally
1925 1935 return self.handle_normal(line,continue_prompt)
1926 1936
1927 1937 def handle_emacs(self,line,continue_prompt=None,
1928 1938 pre=None,iFun=None,theRest=None):
1929 1939 """Handle input lines marked by python-mode."""
1930 1940
1931 1941 # Currently, nothing is done. Later more functionality can be added
1932 1942 # here if needed.
1933 1943
1934 1944 # The input cache shouldn't be updated
1935 1945
1936 1946 return line
1937 1947
1938 1948 def write(self,data):
1939 1949 """Write a string to the default output"""
1940 1950 Term.cout.write(data)
1941 1951
1942 1952 def write_err(self,data):
1943 1953 """Write a string to the default error output"""
1944 1954 Term.cerr.write(data)
1945 1955
1946 1956 def safe_execfile(self,fname,*where,**kw):
1947 1957 fname = os.path.expanduser(fname)
1948 1958
1949 1959 # find things also in current directory
1950 1960 dname = os.path.dirname(fname)
1951 1961 if not sys.path.count(dname):
1952 1962 sys.path.append(dname)
1953 1963
1954 1964 try:
1955 1965 xfile = open(fname)
1956 1966 except:
1957 1967 print >> Term.cerr, \
1958 1968 'Could not open file <%s> for safe execution.' % fname
1959 1969 return None
1960 1970
1961 1971 kw.setdefault('islog',0)
1962 1972 kw.setdefault('quiet',1)
1963 1973 kw.setdefault('exit_ignore',0)
1964 1974 first = xfile.readline()
1965 1975 _LOGHEAD = str(self.LOGHEAD).split('\n',1)[0].strip()
1966 1976 xfile.close()
1967 1977 # line by line execution
1968 1978 if first.startswith(_LOGHEAD) or kw['islog']:
1969 1979 print 'Loading log file <%s> one line at a time...' % fname
1970 1980 if kw['quiet']:
1971 1981 stdout_save = sys.stdout
1972 1982 sys.stdout = StringIO.StringIO()
1973 1983 try:
1974 1984 globs,locs = where[0:2]
1975 1985 except:
1976 1986 try:
1977 1987 globs = locs = where[0]
1978 1988 except:
1979 1989 globs = locs = globals()
1980 1990 badblocks = []
1981 1991
1982 1992 # we also need to identify indented blocks of code when replaying
1983 1993 # logs and put them together before passing them to an exec
1984 1994 # statement. This takes a bit of regexp and look-ahead work in the
1985 1995 # file. It's easiest if we swallow the whole thing in memory
1986 1996 # first, and manually walk through the lines list moving the
1987 1997 # counter ourselves.
1988 1998 indent_re = re.compile('\s+\S')
1989 1999 xfile = open(fname)
1990 2000 filelines = xfile.readlines()
1991 2001 xfile.close()
1992 2002 nlines = len(filelines)
1993 2003 lnum = 0
1994 2004 while lnum < nlines:
1995 2005 line = filelines[lnum]
1996 2006 lnum += 1
1997 2007 # don't re-insert logger status info into cache
1998 2008 if line.startswith('#log#'):
1999 2009 continue
2000 2010 elif line.startswith('#%s'% self.ESC_MAGIC):
2001 2011 self.update_cache(line[1:])
2002 2012 line = magic2python(line)
2003 2013 elif line.startswith('#!'):
2004 2014 self.update_cache(line[1:])
2005 2015 else:
2006 2016 # build a block of code (maybe a single line) for execution
2007 2017 block = line
2008 2018 try:
2009 2019 next = filelines[lnum] # lnum has already incremented
2010 2020 except:
2011 2021 next = None
2012 2022 while next and indent_re.match(next):
2013 2023 block += next
2014 2024 lnum += 1
2015 2025 try:
2016 2026 next = filelines[lnum]
2017 2027 except:
2018 2028 next = None
2019 2029 # now execute the block of one or more lines
2020 2030 try:
2021 2031 exec block in globs,locs
2022 2032 self.update_cache(block.rstrip())
2023 2033 except SystemExit:
2024 2034 pass
2025 2035 except:
2026 2036 badblocks.append(block.rstrip())
2027 2037 if kw['quiet']: # restore stdout
2028 2038 sys.stdout.close()
2029 2039 sys.stdout = stdout_save
2030 2040 print 'Finished replaying log file <%s>' % fname
2031 2041 if badblocks:
2032 2042 print >> sys.stderr, \
2033 2043 '\nThe following lines/blocks in file <%s> reported errors:' \
2034 2044 % fname
2035 2045 for badline in badblocks:
2036 2046 print >> sys.stderr, badline
2037 2047 else: # regular file execution
2038 2048 try:
2039 2049 execfile(fname,*where)
2040 2050 except SyntaxError:
2041 2051 etype, evalue = sys.exc_info()[0:2]
2042 2052 self.SyntaxTB(etype,evalue,[])
2043 2053 warn('Failure executing file: <%s>' % fname)
2044 2054 except SystemExit,status:
2045 2055 if not kw['exit_ignore']:
2046 2056 self.InteractiveTB()
2047 2057 warn('Failure executing file: <%s>' % fname)
2048 2058 except:
2049 2059 self.InteractiveTB()
2050 2060 warn('Failure executing file: <%s>' % fname)
2051 2061
2052 2062 #************************* end of file <iplib.py> *****************************
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