##// END OF EJS Templates
- Fix a bug introduced by r1357 which broke embedding....
fperez -
Show More

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

@@ -1,1116 +1,1143 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 2222 2007-04-06 17:11:27Z fperez $"""
7 $Id: Shell.py 2577 2007-08-02 23:50:02Z fperez $"""
8 8
9 9 #*****************************************************************************
10 10 # Copyright (C) 2001-2006 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 # Stdlib imports
22 22 import __builtin__
23 23 import __main__
24 24 import Queue
25 25 import inspect
26 26 import os
27 27 import sys
28 28 import thread
29 29 import threading
30 30 import time
31 31
32 32 from signal import signal, SIGINT
33 33
34 34 try:
35 35 import ctypes
36 36 HAS_CTYPES = True
37 37 except ImportError:
38 38 HAS_CTYPES = False
39 39
40 40 # IPython imports
41 41 import IPython
42 from IPython import ultraTB
43 from IPython.genutils import Term,warn,error,flag_calls
42 from IPython import ultraTB, ipapi
43 from IPython.genutils import Term,warn,error,flag_calls, ask_yes_no
44 44 from IPython.iplib import InteractiveShell
45 45 from IPython.ipmaker import make_IPython
46 46 from IPython.Magic import Magic
47 47 from IPython.ipstruct import Struct
48 48
49 49 # Globals
50 50 # global flag to pass around information about Ctrl-C without exceptions
51 51 KBINT = False
52 52
53 53 # global flag to turn on/off Tk support.
54 54 USE_TK = False
55 55
56 56 # ID for the main thread, used for cross-thread exceptions
57 57 MAIN_THREAD_ID = thread.get_ident()
58 58
59 59 # Tag when runcode() is active, for exception handling
60 60 CODE_RUN = None
61 61
62 62 #-----------------------------------------------------------------------------
63 63 # This class is trivial now, but I want to have it in to publish a clean
64 64 # interface. Later when the internals are reorganized, code that uses this
65 65 # shouldn't have to change.
66 66
67 67 class IPShell:
68 68 """Create an IPython instance."""
69 69
70 70 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
71 71 debug=1,shell_class=InteractiveShell):
72 72 self.IP = make_IPython(argv,user_ns=user_ns,
73 73 user_global_ns=user_global_ns,
74 74 debug=debug,shell_class=shell_class)
75 75
76 76 def mainloop(self,sys_exit=0,banner=None):
77 77 self.IP.mainloop(banner)
78 78 if sys_exit:
79 79 sys.exit()
80 80
81 81 #-----------------------------------------------------------------------------
82 def kill_embedded(self,parameter_s=''):
83 """%kill_embedded : deactivate for good the current embedded IPython.
84
85 This function (after asking for confirmation) sets an internal flag so that
86 an embedded IPython will never activate again. This is useful to
87 permanently disable a shell that is being called inside a loop: once you've
88 figured out what you needed from it, you may then kill it and the program
89 will then continue to run without the interactive shell interfering again.
90 """
91
92 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
93 "(y/n)? [y/N] ",'n')
94 if kill:
95 self.shell.embedded_active = False
96 print "This embedded IPython will not reactivate anymore once you exit."
97
82 98 class IPShellEmbed:
83 99 """Allow embedding an IPython shell into a running program.
84 100
85 101 Instances of this class are callable, with the __call__ method being an
86 102 alias to the embed() method of an InteractiveShell instance.
87 103
88 104 Usage (see also the example-embed.py file for a running example):
89 105
90 106 ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override])
91 107
92 108 - argv: list containing valid command-line options for IPython, as they
93 109 would appear in sys.argv[1:].
94 110
95 111 For example, the following command-line options:
96 112
97 113 $ ipython -prompt_in1 'Input <\\#>' -colors LightBG
98 114
99 115 would be passed in the argv list as:
100 116
101 117 ['-prompt_in1','Input <\\#>','-colors','LightBG']
102 118
103 119 - banner: string which gets printed every time the interpreter starts.
104 120
105 121 - exit_msg: string which gets printed every time the interpreter exits.
106 122
107 123 - rc_override: a dict or Struct of configuration options such as those
108 124 used by IPython. These options are read from your ~/.ipython/ipythonrc
109 125 file when the Shell object is created. Passing an explicit rc_override
110 126 dict with any options you want allows you to override those values at
111 127 creation time without having to modify the file. This way you can create
112 128 embeddable instances configured in any way you want without editing any
113 129 global files (thus keeping your interactive IPython configuration
114 130 unchanged).
115 131
116 132 Then the ipshell instance can be called anywhere inside your code:
117 133
118 134 ipshell(header='') -> Opens up an IPython shell.
119 135
120 136 - header: string printed by the IPython shell upon startup. This can let
121 137 you know where in your code you are when dropping into the shell. Note
122 138 that 'banner' gets prepended to all calls, so header is used for
123 139 location-specific information.
124 140
125 141 For more details, see the __call__ method below.
126 142
127 143 When the IPython shell is exited with Ctrl-D, normal program execution
128 144 resumes.
129 145
130 146 This functionality was inspired by a posting on comp.lang.python by cmkl
131 147 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
132 148 by the IDL stop/continue commands."""
133 149
134 150 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None,
135 151 user_ns=None):
136 152 """Note that argv here is a string, NOT a list."""
137 153 self.set_banner(banner)
138 154 self.set_exit_msg(exit_msg)
139 155 self.set_dummy_mode(0)
140 156
141 157 # sys.displayhook is a global, we need to save the user's original
142 158 # Don't rely on __displayhook__, as the user may have changed that.
143 159 self.sys_displayhook_ori = sys.displayhook
144 160
145 161 # save readline completer status
146 162 try:
147 163 #print 'Save completer',sys.ipcompleter # dbg
148 164 self.sys_ipcompleter_ori = sys.ipcompleter
149 165 except:
150 166 pass # not nested with IPython
151 167
152 168 self.IP = make_IPython(argv,rc_override=rc_override,
153 169 embedded=True,
154 170 user_ns=user_ns)
155 171
172 ip = ipapi.IPApi(self.IP)
173 ip.expose_magic("kill_embedded",kill_embedded)
174
156 175 # copy our own displayhook also
157 176 self.sys_displayhook_embed = sys.displayhook
158 177 # and leave the system's display hook clean
159 178 sys.displayhook = self.sys_displayhook_ori
160 179 # don't use the ipython crash handler so that user exceptions aren't
161 180 # trapped
162 181 sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors,
163 182 mode = self.IP.rc.xmode,
164 183 call_pdb = self.IP.rc.pdb)
165 184 self.restore_system_completer()
166 185
167 186 def restore_system_completer(self):
168 187 """Restores the readline completer which was in place.
169 188
170 189 This allows embedded IPython within IPython not to disrupt the
171 190 parent's completion.
172 191 """
173 192
174 193 try:
175 194 self.IP.readline.set_completer(self.sys_ipcompleter_ori)
176 195 sys.ipcompleter = self.sys_ipcompleter_ori
177 196 except:
178 197 pass
179 198
180 199 def __call__(self,header='',local_ns=None,global_ns=None,dummy=None):
181 200 """Activate the interactive interpreter.
182 201
183 202 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
184 203 the interpreter shell with the given local and global namespaces, and
185 204 optionally print a header string at startup.
186 205
187 206 The shell can be globally activated/deactivated using the
188 207 set/get_dummy_mode methods. This allows you to turn off a shell used
189 208 for debugging globally.
190 209
191 210 However, *each* time you call the shell you can override the current
192 211 state of dummy_mode with the optional keyword parameter 'dummy'. For
193 212 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
194 213 can still have a specific call work by making it as IPShell(dummy=0).
195 214
196 215 The optional keyword parameter dummy controls whether the call
197 216 actually does anything. """
198 217
218 # If the user has turned it off, go away
219 if not self.IP.embedded_active:
220 return
221
222 # Normal exits from interactive mode set this flag, so the shell can't
223 # re-enter (it checks this variable at the start of interactive mode).
224 self.IP.exit_now = False
225
199 226 # Allow the dummy parameter to override the global __dummy_mode
200 227 if dummy or (dummy != 0 and self.__dummy_mode):
201 228 return
202 229
203 230 # Set global subsystems (display,completions) to our values
204 231 sys.displayhook = self.sys_displayhook_embed
205 232 if self.IP.has_readline:
206 233 self.IP.set_completer()
207 234
208 235 if self.banner and header:
209 236 format = '%s\n%s\n'
210 237 else:
211 238 format = '%s%s\n'
212 239 banner = format % (self.banner,header)
213 240
214 241 # Call the embedding code with a stack depth of 1 so it can skip over
215 242 # our call and get the original caller's namespaces.
216 243 self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1)
217 244
218 245 if self.exit_msg:
219 246 print self.exit_msg
220 247
221 248 # Restore global systems (display, completion)
222 249 sys.displayhook = self.sys_displayhook_ori
223 250 self.restore_system_completer()
224 251
225 252 def set_dummy_mode(self,dummy):
226 253 """Sets the embeddable shell's dummy mode parameter.
227 254
228 255 set_dummy_mode(dummy): dummy = 0 or 1.
229 256
230 257 This parameter is persistent and makes calls to the embeddable shell
231 258 silently return without performing any action. This allows you to
232 259 globally activate or deactivate a shell you're using with a single call.
233 260
234 261 If you need to manually"""
235 262
236 263 if dummy not in [0,1,False,True]:
237 264 raise ValueError,'dummy parameter must be boolean'
238 265 self.__dummy_mode = dummy
239 266
240 267 def get_dummy_mode(self):
241 268 """Return the current value of the dummy mode parameter.
242 269 """
243 270 return self.__dummy_mode
244 271
245 272 def set_banner(self,banner):
246 273 """Sets the global banner.
247 274
248 275 This banner gets prepended to every header printed when the shell
249 276 instance is called."""
250 277
251 278 self.banner = banner
252 279
253 280 def set_exit_msg(self,exit_msg):
254 281 """Sets the global exit_msg.
255 282
256 283 This exit message gets printed upon exiting every time the embedded
257 284 shell is called. It is None by default. """
258 285
259 286 self.exit_msg = exit_msg
260 287
261 288 #-----------------------------------------------------------------------------
262 289 if HAS_CTYPES:
263 290 # Add async exception support. Trick taken from:
264 291 # http://sebulba.wikispaces.com/recipe+thread2
265 292 def _async_raise(tid, exctype):
266 293 """raises the exception, performs cleanup if needed"""
267 294 if not inspect.isclass(exctype):
268 295 raise TypeError("Only types can be raised (not instances)")
269 296 res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid,
270 297 ctypes.py_object(exctype))
271 298 if res == 0:
272 299 raise ValueError("invalid thread id")
273 300 elif res != 1:
274 301 # """if it returns a number greater than one, you're in trouble,
275 302 # and you should call it again with exc=NULL to revert the effect"""
276 303 ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
277 304 raise SystemError("PyThreadState_SetAsyncExc failed")
278 305
279 306 def sigint_handler (signum,stack_frame):
280 307 """Sigint handler for threaded apps.
281 308
282 309 This is a horrible hack to pass information about SIGINT _without_
283 310 using exceptions, since I haven't been able to properly manage
284 311 cross-thread exceptions in GTK/WX. In fact, I don't think it can be
285 312 done (or at least that's my understanding from a c.l.py thread where
286 313 this was discussed)."""
287 314
288 315 global KBINT
289 316
290 317 if CODE_RUN:
291 318 _async_raise(MAIN_THREAD_ID,KeyboardInterrupt)
292 319 else:
293 320 KBINT = True
294 321 print '\nKeyboardInterrupt - Press <Enter> to continue.',
295 322 Term.cout.flush()
296 323
297 324 else:
298 325 def sigint_handler (signum,stack_frame):
299 326 """Sigint handler for threaded apps.
300 327
301 328 This is a horrible hack to pass information about SIGINT _without_
302 329 using exceptions, since I haven't been able to properly manage
303 330 cross-thread exceptions in GTK/WX. In fact, I don't think it can be
304 331 done (or at least that's my understanding from a c.l.py thread where
305 332 this was discussed)."""
306 333
307 334 global KBINT
308 335
309 336 print '\nKeyboardInterrupt - Press <Enter> to continue.',
310 337 Term.cout.flush()
311 338 # Set global flag so that runsource can know that Ctrl-C was hit
312 339 KBINT = True
313 340
314 341
315 342 class MTInteractiveShell(InteractiveShell):
316 343 """Simple multi-threaded shell."""
317 344
318 345 # Threading strategy taken from:
319 346 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian
320 347 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
321 348 # from the pygtk mailing list, to avoid lockups with system calls.
322 349
323 350 # class attribute to indicate whether the class supports threads or not.
324 351 # Subclasses with thread support should override this as needed.
325 352 isthreaded = True
326 353
327 354 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
328 355 user_ns=None,user_global_ns=None,banner2='',**kw):
329 356 """Similar to the normal InteractiveShell, but with threading control"""
330 357
331 358 InteractiveShell.__init__(self,name,usage,rc,user_ns,
332 359 user_global_ns,banner2)
333 360
334 361 # Locking control variable. We need to use a norma lock, not an RLock
335 362 # here. I'm not exactly sure why, it seems to me like it should be
336 363 # the opposite, but we deadlock with an RLock. Puzzled...
337 364 self.thread_ready = threading.Condition(threading.Lock())
338 365
339 366 # A queue to hold the code to be executed. A scalar variable is NOT
340 367 # enough, because uses like macros cause reentrancy.
341 368 self.code_queue = Queue.Queue()
342 369
343 370 # Stuff to do at closing time
344 371 self._kill = False
345 372 on_kill = kw.get('on_kill')
346 373 if on_kill is None:
347 374 on_kill = []
348 375 # Check that all things to kill are callable:
349 376 for t in on_kill:
350 377 if not callable(t):
351 378 raise TypeError,'on_kill must be a list of callables'
352 379 self.on_kill = on_kill
353 380
354 381 def runsource(self, source, filename="<input>", symbol="single"):
355 382 """Compile and run some source in the interpreter.
356 383
357 384 Modified version of code.py's runsource(), to handle threading issues.
358 385 See the original for full docstring details."""
359 386
360 387 global KBINT
361 388
362 389 # If Ctrl-C was typed, we reset the flag and return right away
363 390 if KBINT:
364 391 KBINT = False
365 392 return False
366 393
367 394 try:
368 395 code = self.compile(source, filename, symbol)
369 396 except (OverflowError, SyntaxError, ValueError):
370 397 # Case 1
371 398 self.showsyntaxerror(filename)
372 399 return False
373 400
374 401 if code is None:
375 402 # Case 2
376 403 return True
377 404
378 405 # Case 3
379 406 # Store code in queue, so the execution thread can handle it.
380 407
381 408 # Note that with macros and other applications, we MAY re-enter this
382 409 # section, so we have to acquire the lock with non-blocking semantics,
383 410 # else we deadlock.
384 411 got_lock = self.thread_ready.acquire(False)
385 412 self.code_queue.put(code)
386 413 if got_lock:
387 414 self.thread_ready.wait() # Wait until processed in timeout interval
388 415 self.thread_ready.release()
389 416
390 417 return False
391 418
392 419 def runcode(self):
393 420 """Execute a code object.
394 421
395 422 Multithreaded wrapper around IPython's runcode()."""
396 423
397 424 global CODE_RUN
398 425
399 426 # Exceptions need to be raised differently depending on which thread is
400 427 # active
401 428 CODE_RUN = True
402 429
403 430 # lock thread-protected stuff
404 431 got_lock = self.thread_ready.acquire(False)
405 432
406 433 if self._kill:
407 434 print >>Term.cout, 'Closing threads...',
408 435 Term.cout.flush()
409 436 for tokill in self.on_kill:
410 437 tokill()
411 438 print >>Term.cout, 'Done.'
412 439
413 440 # Install sigint handler. We do it every time to ensure that if user
414 441 # code modifies it, we restore our own handling.
415 442 try:
416 443 signal(SIGINT,sigint_handler)
417 444 except SystemError:
418 445 # This happens under Windows, which seems to have all sorts
419 446 # of problems with signal handling. Oh well...
420 447 pass
421 448
422 449 # Flush queue of pending code by calling the run methood of the parent
423 450 # class with all items which may be in the queue.
424 451 while 1:
425 452 try:
426 453 code_to_run = self.code_queue.get_nowait()
427 454 except Queue.Empty:
428 455 break
429 456 if got_lock:
430 457 self.thread_ready.notify()
431 458 InteractiveShell.runcode(self,code_to_run)
432 459 else:
433 460 break
434 461
435 462 # We're done with thread-protected variables
436 463 if got_lock:
437 464 self.thread_ready.release()
438 465
439 466 # We're done...
440 467 CODE_RUN = False
441 468 # This MUST return true for gtk threading to work
442 469 return True
443 470
444 471 def kill(self):
445 472 """Kill the thread, returning when it has been shut down."""
446 473 got_lock = self.thread_ready.acquire(False)
447 474 self._kill = True
448 475 if got_lock:
449 476 self.thread_ready.release()
450 477
451 478 class MatplotlibShellBase:
452 479 """Mixin class to provide the necessary modifications to regular IPython
453 480 shell classes for matplotlib support.
454 481
455 482 Given Python's MRO, this should be used as the FIRST class in the
456 483 inheritance hierarchy, so that it overrides the relevant methods."""
457 484
458 485 def _matplotlib_config(self,name,user_ns):
459 486 """Return items needed to setup the user's shell with matplotlib"""
460 487
461 488 # Initialize matplotlib to interactive mode always
462 489 import matplotlib
463 490 from matplotlib import backends
464 491 matplotlib.interactive(True)
465 492
466 493 def use(arg):
467 494 """IPython wrapper for matplotlib's backend switcher.
468 495
469 496 In interactive use, we can not allow switching to a different
470 497 interactive backend, since thread conflicts will most likely crash
471 498 the python interpreter. This routine does a safety check first,
472 499 and refuses to perform a dangerous switch. It still allows
473 500 switching to non-interactive backends."""
474 501
475 502 if arg in backends.interactive_bk and arg != self.mpl_backend:
476 503 m=('invalid matplotlib backend switch.\n'
477 504 'This script attempted to switch to the interactive '
478 505 'backend: `%s`\n'
479 506 'Your current choice of interactive backend is: `%s`\n\n'
480 507 'Switching interactive matplotlib backends at runtime\n'
481 508 'would crash the python interpreter, '
482 509 'and IPython has blocked it.\n\n'
483 510 'You need to either change your choice of matplotlib backend\n'
484 511 'by editing your .matplotlibrc file, or run this script as a \n'
485 512 'standalone file from the command line, not using IPython.\n' %
486 513 (arg,self.mpl_backend) )
487 514 raise RuntimeError, m
488 515 else:
489 516 self.mpl_use(arg)
490 517 self.mpl_use._called = True
491 518
492 519 self.matplotlib = matplotlib
493 520 self.mpl_backend = matplotlib.rcParams['backend']
494 521
495 522 # we also need to block switching of interactive backends by use()
496 523 self.mpl_use = matplotlib.use
497 524 self.mpl_use._called = False
498 525 # overwrite the original matplotlib.use with our wrapper
499 526 matplotlib.use = use
500 527
501 528 # This must be imported last in the matplotlib series, after
502 529 # backend/interactivity choices have been made
503 530 import matplotlib.pylab as pylab
504 531 self.pylab = pylab
505 532
506 533 self.pylab.show._needmain = False
507 534 # We need to detect at runtime whether show() is called by the user.
508 535 # For this, we wrap it into a decorator which adds a 'called' flag.
509 536 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
510 537
511 538 # Build a user namespace initialized with matplotlib/matlab features.
512 539 user_ns = IPython.ipapi.make_user_ns(user_ns)
513 540
514 541 exec ("import matplotlib\n"
515 542 "import matplotlib.pylab as pylab\n") in user_ns
516 543
517 544 # Build matplotlib info banner
518 545 b="""
519 546 Welcome to pylab, a matplotlib-based Python environment.
520 547 For more information, type 'help(pylab)'.
521 548 """
522 549 return user_ns,b
523 550
524 551 def mplot_exec(self,fname,*where,**kw):
525 552 """Execute a matplotlib script.
526 553
527 554 This is a call to execfile(), but wrapped in safeties to properly
528 555 handle interactive rendering and backend switching."""
529 556
530 557 #print '*** Matplotlib runner ***' # dbg
531 558 # turn off rendering until end of script
532 559 isInteractive = self.matplotlib.rcParams['interactive']
533 560 self.matplotlib.interactive(False)
534 561 self.safe_execfile(fname,*where,**kw)
535 562 self.matplotlib.interactive(isInteractive)
536 563 # make rendering call now, if the user tried to do it
537 564 if self.pylab.draw_if_interactive.called:
538 565 self.pylab.draw()
539 566 self.pylab.draw_if_interactive.called = False
540 567
541 568 # if a backend switch was performed, reverse it now
542 569 if self.mpl_use._called:
543 570 self.matplotlib.rcParams['backend'] = self.mpl_backend
544 571
545 572 def magic_run(self,parameter_s=''):
546 573 Magic.magic_run(self,parameter_s,runner=self.mplot_exec)
547 574
548 575 # Fix the docstring so users see the original as well
549 576 magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__,
550 577 "\n *** Modified %run for Matplotlib,"
551 578 " with proper interactive handling ***")
552 579
553 580 # Now we provide 2 versions of a matplotlib-aware IPython base shells, single
554 581 # and multithreaded. Note that these are meant for internal use, the IPShell*
555 582 # classes below are the ones meant for public consumption.
556 583
557 584 class MatplotlibShell(MatplotlibShellBase,InteractiveShell):
558 585 """Single-threaded shell with matplotlib support."""
559 586
560 587 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
561 588 user_ns=None,user_global_ns=None,**kw):
562 589 user_ns,b2 = self._matplotlib_config(name,user_ns)
563 590 InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
564 591 banner2=b2,**kw)
565 592
566 593 class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell):
567 594 """Multi-threaded shell with matplotlib support."""
568 595
569 596 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
570 597 user_ns=None,user_global_ns=None, **kw):
571 598 user_ns,b2 = self._matplotlib_config(name,user_ns)
572 599 MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
573 600 banner2=b2,**kw)
574 601
575 602 #-----------------------------------------------------------------------------
576 603 # Utility functions for the different GUI enabled IPShell* classes.
577 604
578 605 def get_tk():
579 606 """Tries to import Tkinter and returns a withdrawn Tkinter root
580 607 window. If Tkinter is already imported or not available, this
581 608 returns None. This function calls `hijack_tk` underneath.
582 609 """
583 610 if not USE_TK or sys.modules.has_key('Tkinter'):
584 611 return None
585 612 else:
586 613 try:
587 614 import Tkinter
588 615 except ImportError:
589 616 return None
590 617 else:
591 618 hijack_tk()
592 619 r = Tkinter.Tk()
593 620 r.withdraw()
594 621 return r
595 622
596 623 def hijack_tk():
597 624 """Modifies Tkinter's mainloop with a dummy so when a module calls
598 625 mainloop, it does not block.
599 626
600 627 """
601 628 def misc_mainloop(self, n=0):
602 629 pass
603 630 def tkinter_mainloop(n=0):
604 631 pass
605 632
606 633 import Tkinter
607 634 Tkinter.Misc.mainloop = misc_mainloop
608 635 Tkinter.mainloop = tkinter_mainloop
609 636
610 637 def update_tk(tk):
611 638 """Updates the Tkinter event loop. This is typically called from
612 639 the respective WX or GTK mainloops.
613 640 """
614 641 if tk:
615 642 tk.update()
616 643
617 644 def hijack_wx():
618 645 """Modifies wxPython's MainLoop with a dummy so user code does not
619 646 block IPython. The hijacked mainloop function is returned.
620 647 """
621 648 def dummy_mainloop(*args, **kw):
622 649 pass
623 650
624 651 try:
625 652 import wx
626 653 except ImportError:
627 654 # For very old versions of WX
628 655 import wxPython as wx
629 656
630 657 ver = wx.__version__
631 658 orig_mainloop = None
632 659 if ver[:3] >= '2.5':
633 660 import wx
634 661 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
635 662 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
636 663 else: raise AttributeError('Could not find wx core module')
637 664 orig_mainloop = core.PyApp_MainLoop
638 665 core.PyApp_MainLoop = dummy_mainloop
639 666 elif ver[:3] == '2.4':
640 667 orig_mainloop = wx.wxc.wxPyApp_MainLoop
641 668 wx.wxc.wxPyApp_MainLoop = dummy_mainloop
642 669 else:
643 670 warn("Unable to find either wxPython version 2.4 or >= 2.5.")
644 671 return orig_mainloop
645 672
646 673 def hijack_gtk():
647 674 """Modifies pyGTK's mainloop with a dummy so user code does not
648 675 block IPython. This function returns the original `gtk.mainloop`
649 676 function that has been hijacked.
650 677 """
651 678 def dummy_mainloop(*args, **kw):
652 679 pass
653 680 import gtk
654 681 if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main
655 682 else: orig_mainloop = gtk.mainloop
656 683 gtk.mainloop = dummy_mainloop
657 684 gtk.main = dummy_mainloop
658 685 return orig_mainloop
659 686
660 687 #-----------------------------------------------------------------------------
661 688 # The IPShell* classes below are the ones meant to be run by external code as
662 689 # IPython instances. Note that unless a specific threading strategy is
663 690 # desired, the factory function start() below should be used instead (it
664 691 # selects the proper threaded class).
665 692
666 693 class IPThread(threading.Thread):
667 694 def run(self):
668 695 self.IP.mainloop(self._banner)
669 696 self.IP.kill()
670 697
671 698 class IPShellGTK(IPThread):
672 699 """Run a gtk mainloop() in a separate thread.
673 700
674 701 Python commands can be passed to the thread where they will be executed.
675 702 This is implemented by periodically checking for passed code using a
676 703 GTK timeout callback."""
677 704
678 705 TIMEOUT = 100 # Millisecond interval between timeouts.
679 706
680 707 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
681 708 debug=1,shell_class=MTInteractiveShell):
682 709
683 710 import gtk
684 711
685 712 self.gtk = gtk
686 713 self.gtk_mainloop = hijack_gtk()
687 714
688 715 # Allows us to use both Tk and GTK.
689 716 self.tk = get_tk()
690 717
691 718 if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit
692 719 else: mainquit = self.gtk.mainquit
693 720
694 721 self.IP = make_IPython(argv,user_ns=user_ns,
695 722 user_global_ns=user_global_ns,
696 723 debug=debug,
697 724 shell_class=shell_class,
698 725 on_kill=[mainquit])
699 726
700 727 # HACK: slot for banner in self; it will be passed to the mainloop
701 728 # method only and .run() needs it. The actual value will be set by
702 729 # .mainloop().
703 730 self._banner = None
704 731
705 732 threading.Thread.__init__(self)
706 733
707 734 def mainloop(self,sys_exit=0,banner=None):
708 735
709 736 self._banner = banner
710 737
711 738 if self.gtk.pygtk_version >= (2,4,0):
712 739 import gobject
713 740 gobject.idle_add(self.on_timer)
714 741 else:
715 742 self.gtk.idle_add(self.on_timer)
716 743
717 744 if sys.platform != 'win32':
718 745 try:
719 746 if self.gtk.gtk_version[0] >= 2:
720 747 self.gtk.gdk.threads_init()
721 748 except AttributeError:
722 749 pass
723 750 except RuntimeError:
724 751 error('Your pyGTK likely has not been compiled with '
725 752 'threading support.\n'
726 753 'The exception printout is below.\n'
727 754 'You can either rebuild pyGTK with threads, or '
728 755 'try using \n'
729 756 'matplotlib with a different backend (like Tk or WX).\n'
730 757 'Note that matplotlib will most likely not work in its '
731 758 'current state!')
732 759 self.IP.InteractiveTB()
733 760
734 761 self.start()
735 762 self.gtk.gdk.threads_enter()
736 763 self.gtk_mainloop()
737 764 self.gtk.gdk.threads_leave()
738 765 self.join()
739 766
740 767 def on_timer(self):
741 768 """Called when GTK is idle.
742 769
743 770 Must return True always, otherwise GTK stops calling it"""
744 771
745 772 update_tk(self.tk)
746 773 self.IP.runcode()
747 774 time.sleep(0.01)
748 775 return True
749 776
750 777
751 778 class IPShellWX(IPThread):
752 779 """Run a wx mainloop() in a separate thread.
753 780
754 781 Python commands can be passed to the thread where they will be executed.
755 782 This is implemented by periodically checking for passed code using a
756 783 GTK timeout callback."""
757 784
758 785 TIMEOUT = 100 # Millisecond interval between timeouts.
759 786
760 787 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
761 788 debug=1,shell_class=MTInteractiveShell):
762 789
763 790 self.IP = make_IPython(argv,user_ns=user_ns,
764 791 user_global_ns=user_global_ns,
765 792 debug=debug,
766 793 shell_class=shell_class,
767 794 on_kill=[self.wxexit])
768 795
769 796 wantedwxversion=self.IP.rc.wxversion
770 797 if wantedwxversion!="0":
771 798 try:
772 799 import wxversion
773 800 except ImportError:
774 801 error('The wxversion module is needed for WX version selection')
775 802 else:
776 803 try:
777 804 wxversion.select(wantedwxversion)
778 805 except:
779 806 self.IP.InteractiveTB()
780 807 error('Requested wxPython version %s could not be loaded' %
781 808 wantedwxversion)
782 809
783 810 import wx
784 811
785 812 threading.Thread.__init__(self)
786 813 self.wx = wx
787 814 self.wx_mainloop = hijack_wx()
788 815
789 816 # Allows us to use both Tk and GTK.
790 817 self.tk = get_tk()
791 818
792 819 # HACK: slot for banner in self; it will be passed to the mainloop
793 820 # method only and .run() needs it. The actual value will be set by
794 821 # .mainloop().
795 822 self._banner = None
796 823
797 824 self.app = None
798 825
799 826 def wxexit(self, *args):
800 827 if self.app is not None:
801 828 self.app.agent.timer.Stop()
802 829 self.app.ExitMainLoop()
803 830
804 831 def mainloop(self,sys_exit=0,banner=None):
805 832
806 833 self._banner = banner
807 834
808 835 self.start()
809 836
810 837 class TimerAgent(self.wx.MiniFrame):
811 838 wx = self.wx
812 839 IP = self.IP
813 840 tk = self.tk
814 841 def __init__(self, parent, interval):
815 842 style = self.wx.DEFAULT_FRAME_STYLE | self.wx.TINY_CAPTION_HORIZ
816 843 self.wx.MiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200),
817 844 size=(100, 100),style=style)
818 845 self.Show(False)
819 846 self.interval = interval
820 847 self.timerId = self.wx.NewId()
821 848
822 849 def StartWork(self):
823 850 self.timer = self.wx.Timer(self, self.timerId)
824 851 self.wx.EVT_TIMER(self, self.timerId, self.OnTimer)
825 852 self.timer.Start(self.interval)
826 853
827 854 def OnTimer(self, event):
828 855 update_tk(self.tk)
829 856 self.IP.runcode()
830 857
831 858 class App(self.wx.App):
832 859 wx = self.wx
833 860 TIMEOUT = self.TIMEOUT
834 861 def OnInit(self):
835 862 'Create the main window and insert the custom frame'
836 863 self.agent = TimerAgent(None, self.TIMEOUT)
837 864 self.agent.Show(False)
838 865 self.agent.StartWork()
839 866 return True
840 867
841 868 self.app = App(redirect=False)
842 869 self.wx_mainloop(self.app)
843 870 self.join()
844 871
845 872
846 873 class IPShellQt(IPThread):
847 874 """Run a Qt event loop in a separate thread.
848 875
849 876 Python commands can be passed to the thread where they will be executed.
850 877 This is implemented by periodically checking for passed code using a
851 878 Qt timer / slot."""
852 879
853 880 TIMEOUT = 100 # Millisecond interval between timeouts.
854 881
855 882 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
856 883 debug=0,shell_class=MTInteractiveShell):
857 884
858 885 import qt
859 886
860 887 class newQApplication:
861 888 def __init__( self ):
862 889 self.QApplication = qt.QApplication
863 890
864 891 def __call__( *args, **kwargs ):
865 892 return qt.qApp
866 893
867 894 def exec_loop( *args, **kwargs ):
868 895 pass
869 896
870 897 def __getattr__( self, name ):
871 898 return getattr( self.QApplication, name )
872 899
873 900 qt.QApplication = newQApplication()
874 901
875 902 # Allows us to use both Tk and QT.
876 903 self.tk = get_tk()
877 904
878 905 self.IP = make_IPython(argv,user_ns=user_ns,
879 906 user_global_ns=user_global_ns,
880 907 debug=debug,
881 908 shell_class=shell_class,
882 909 on_kill=[qt.qApp.exit])
883 910
884 911 # HACK: slot for banner in self; it will be passed to the mainloop
885 912 # method only and .run() needs it. The actual value will be set by
886 913 # .mainloop().
887 914 self._banner = None
888 915
889 916 threading.Thread.__init__(self)
890 917
891 918 def mainloop(self,sys_exit=0,banner=None):
892 919
893 920 import qt
894 921
895 922 self._banner = banner
896 923
897 924 if qt.QApplication.startingUp():
898 925 a = qt.QApplication.QApplication(sys.argv)
899 926 self.timer = qt.QTimer()
900 927 qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer )
901 928
902 929 self.start()
903 930 self.timer.start( self.TIMEOUT, True )
904 931 while True:
905 932 if self.IP._kill: break
906 933 qt.qApp.exec_loop()
907 934 self.join()
908 935
909 936 def on_timer(self):
910 937 update_tk(self.tk)
911 938 result = self.IP.runcode()
912 939 self.timer.start( self.TIMEOUT, True )
913 940 return result
914 941
915 942
916 943 class IPShellQt4(IPThread):
917 944 """Run a Qt event loop in a separate thread.
918 945
919 946 Python commands can be passed to the thread where they will be executed.
920 947 This is implemented by periodically checking for passed code using a
921 948 Qt timer / slot."""
922 949
923 950 TIMEOUT = 100 # Millisecond interval between timeouts.
924 951
925 952 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
926 953 debug=0,shell_class=MTInteractiveShell):
927 954
928 955 from PyQt4 import QtCore, QtGui
929 956
930 957 class newQApplication:
931 958 def __init__( self ):
932 959 self.QApplication = QtGui.QApplication
933 960
934 961 def __call__( *args, **kwargs ):
935 962 return QtGui.qApp
936 963
937 964 def exec_loop( *args, **kwargs ):
938 965 pass
939 966
940 967 def __getattr__( self, name ):
941 968 return getattr( self.QApplication, name )
942 969
943 970 QtGui.QApplication = newQApplication()
944 971
945 972 # Allows us to use both Tk and QT.
946 973 self.tk = get_tk()
947 974
948 975 self.IP = make_IPython(argv,user_ns=user_ns,
949 976 user_global_ns=user_global_ns,
950 977 debug=debug,
951 978 shell_class=shell_class,
952 979 on_kill=[QtGui.qApp.exit])
953 980
954 981 # HACK: slot for banner in self; it will be passed to the mainloop
955 982 # method only and .run() needs it. The actual value will be set by
956 983 # .mainloop().
957 984 self._banner = None
958 985
959 986 threading.Thread.__init__(self)
960 987
961 988 def mainloop(self,sys_exit=0,banner=None):
962 989
963 990 from PyQt4 import QtCore, QtGui
964 991
965 992 self._banner = banner
966 993
967 994 if QtGui.QApplication.startingUp():
968 995 a = QtGui.QApplication.QApplication(sys.argv)
969 996 self.timer = QtCore.QTimer()
970 997 QtCore.QObject.connect( self.timer, QtCore.SIGNAL( 'timeout()' ), self.on_timer )
971 998
972 999 self.start()
973 1000 self.timer.start( self.TIMEOUT )
974 1001 while True:
975 1002 if self.IP._kill: break
976 1003 QtGui.qApp.exec_()
977 1004 self.join()
978 1005
979 1006 def on_timer(self):
980 1007 update_tk(self.tk)
981 1008 result = self.IP.runcode()
982 1009 self.timer.start( self.TIMEOUT )
983 1010 return result
984 1011
985 1012
986 1013 # A set of matplotlib public IPython shell classes, for single-threaded (Tk*
987 1014 # and FLTK*) and multithreaded (GTK*, WX* and Qt*) backends to use.
988 1015 def _load_pylab(user_ns):
989 1016 """Allow users to disable pulling all of pylab into the top-level
990 1017 namespace.
991 1018
992 1019 This little utility must be called AFTER the actual ipython instance is
993 1020 running, since only then will the options file have been fully parsed."""
994 1021
995 1022 ip = IPython.ipapi.get()
996 1023 if ip.options.pylab_import_all:
997 1024 exec "from matplotlib.pylab import *" in user_ns
998 1025
999 1026 class IPShellMatplotlib(IPShell):
1000 1027 """Subclass IPShell with MatplotlibShell as the internal shell.
1001 1028
1002 1029 Single-threaded class, meant for the Tk* and FLTK* backends.
1003 1030
1004 1031 Having this on a separate class simplifies the external driver code."""
1005 1032
1006 1033 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1007 1034 IPShell.__init__(self,argv,user_ns,user_global_ns,debug,
1008 1035 shell_class=MatplotlibShell)
1009 1036 _load_pylab(self.IP.user_ns)
1010 1037
1011 1038 class IPShellMatplotlibGTK(IPShellGTK):
1012 1039 """Subclass IPShellGTK with MatplotlibMTShell as the internal shell.
1013 1040
1014 1041 Multi-threaded class, meant for the GTK* backends."""
1015 1042
1016 1043 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1017 1044 IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug,
1018 1045 shell_class=MatplotlibMTShell)
1019 1046 _load_pylab(self.IP.user_ns)
1020 1047
1021 1048 class IPShellMatplotlibWX(IPShellWX):
1022 1049 """Subclass IPShellWX with MatplotlibMTShell as the internal shell.
1023 1050
1024 1051 Multi-threaded class, meant for the WX* backends."""
1025 1052
1026 1053 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1027 1054 IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug,
1028 1055 shell_class=MatplotlibMTShell)
1029 1056 _load_pylab(self.IP.user_ns)
1030 1057
1031 1058 class IPShellMatplotlibQt(IPShellQt):
1032 1059 """Subclass IPShellQt with MatplotlibMTShell as the internal shell.
1033 1060
1034 1061 Multi-threaded class, meant for the Qt* backends."""
1035 1062
1036 1063 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1037 1064 IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug,
1038 1065 shell_class=MatplotlibMTShell)
1039 1066 _load_pylab(self.IP.user_ns)
1040 1067
1041 1068 class IPShellMatplotlibQt4(IPShellQt4):
1042 1069 """Subclass IPShellQt4 with MatplotlibMTShell as the internal shell.
1043 1070
1044 1071 Multi-threaded class, meant for the Qt4* backends."""
1045 1072
1046 1073 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1047 1074 IPShellQt4.__init__(self,argv,user_ns,user_global_ns,debug,
1048 1075 shell_class=MatplotlibMTShell)
1049 1076 _load_pylab(self.IP.user_ns)
1050 1077
1051 1078 #-----------------------------------------------------------------------------
1052 1079 # Factory functions to actually start the proper thread-aware shell
1053 1080
1054 1081 def _matplotlib_shell_class():
1055 1082 """Factory function to handle shell class selection for matplotlib.
1056 1083
1057 1084 The proper shell class to use depends on the matplotlib backend, since
1058 1085 each backend requires a different threading strategy."""
1059 1086
1060 1087 try:
1061 1088 import matplotlib
1062 1089 except ImportError:
1063 1090 error('matplotlib could NOT be imported! Starting normal IPython.')
1064 1091 sh_class = IPShell
1065 1092 else:
1066 1093 backend = matplotlib.rcParams['backend']
1067 1094 if backend.startswith('GTK'):
1068 1095 sh_class = IPShellMatplotlibGTK
1069 1096 elif backend.startswith('WX'):
1070 1097 sh_class = IPShellMatplotlibWX
1071 1098 elif backend.startswith('Qt4'):
1072 1099 sh_class = IPShellMatplotlibQt4
1073 1100 elif backend.startswith('Qt'):
1074 1101 sh_class = IPShellMatplotlibQt
1075 1102 else:
1076 1103 sh_class = IPShellMatplotlib
1077 1104 #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg
1078 1105 return sh_class
1079 1106
1080 1107 # This is the one which should be called by external code.
1081 1108 def start(user_ns = None):
1082 1109 """Return a running shell instance, dealing with threading options.
1083 1110
1084 1111 This is a factory function which will instantiate the proper IPython shell
1085 1112 based on the user's threading choice. Such a selector is needed because
1086 1113 different GUI toolkits require different thread handling details."""
1087 1114
1088 1115 global USE_TK
1089 1116 # Crude sys.argv hack to extract the threading options.
1090 1117 argv = sys.argv
1091 1118 if len(argv) > 1:
1092 1119 if len(argv) > 2:
1093 1120 arg2 = argv[2]
1094 1121 if arg2.endswith('-tk'):
1095 1122 USE_TK = True
1096 1123 arg1 = argv[1]
1097 1124 if arg1.endswith('-gthread'):
1098 1125 shell = IPShellGTK
1099 1126 elif arg1.endswith( '-qthread' ):
1100 1127 shell = IPShellQt
1101 1128 elif arg1.endswith( '-q4thread' ):
1102 1129 shell = IPShellQt4
1103 1130 elif arg1.endswith('-wthread'):
1104 1131 shell = IPShellWX
1105 1132 elif arg1.endswith('-pylab'):
1106 1133 shell = _matplotlib_shell_class()
1107 1134 else:
1108 1135 shell = IPShell
1109 1136 else:
1110 1137 shell = IPShell
1111 1138 return shell(user_ns = user_ns)
1112 1139
1113 1140 # Some aliases for backwards compatibility
1114 1141 IPythonShell = IPShell
1115 1142 IPythonShellEmbed = IPShellEmbed
1116 1143 #************************ End of file <Shell.py> ***************************
@@ -1,1850 +1,1850 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 General purpose utilities.
4 4
5 5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 6 these things are also convenient when working at the command line.
7 7
8 $Id: genutils.py 2568 2007-07-29 21:38:44Z fperez $"""
8 $Id: genutils.py 2577 2007-08-02 23:50:02Z fperez $"""
9 9
10 10 #*****************************************************************************
11 11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
12 12 #
13 13 # Distributed under the terms of the BSD License. The full license is in
14 14 # the file COPYING, distributed as part of this software.
15 15 #*****************************************************************************
16 16
17 17 from IPython import Release
18 18 __author__ = '%s <%s>' % Release.authors['Fernando']
19 19 __license__ = Release.license
20 20
21 21 #****************************************************************************
22 22 # required modules from the Python standard library
23 23 import __main__
24 24 import commands
25 25 import os
26 26 import re
27 27 import shlex
28 28 import shutil
29 29 import sys
30 30 import tempfile
31 31 import time
32 32 import types
33 33 import warnings
34 34
35 35 # Other IPython utilities
36 36 import IPython
37 37 from IPython.Itpl import Itpl,itpl,printpl
38 38 from IPython import DPyGetOpt, platutils
39 39 from IPython.generics import result_display
40 40 from path import path
41 41 if os.name == "nt":
42 42 from IPython.winconsole import get_console_size
43 43
44 44 #****************************************************************************
45 45 # Exceptions
46 46 class Error(Exception):
47 47 """Base class for exceptions in this module."""
48 48 pass
49 49
50 50 #----------------------------------------------------------------------------
51 51 class IOStream:
52 52 def __init__(self,stream,fallback):
53 53 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
54 54 stream = fallback
55 55 self.stream = stream
56 56 self._swrite = stream.write
57 57 self.flush = stream.flush
58 58
59 59 def write(self,data):
60 60 try:
61 61 self._swrite(data)
62 62 except:
63 63 try:
64 64 # print handles some unicode issues which may trip a plain
65 65 # write() call. Attempt to emulate write() by using a
66 66 # trailing comma
67 67 print >> self.stream, data,
68 68 except:
69 69 # if we get here, something is seriously broken.
70 70 print >> sys.stderr, \
71 71 'ERROR - failed to write data to stream:', self.stream
72 72
73 73 def close(self):
74 74 pass
75 75
76 76
77 77 class IOTerm:
78 78 """ Term holds the file or file-like objects for handling I/O operations.
79 79
80 80 These are normally just sys.stdin, sys.stdout and sys.stderr but for
81 81 Windows they can can replaced to allow editing the strings before they are
82 82 displayed."""
83 83
84 84 # In the future, having IPython channel all its I/O operations through
85 85 # this class will make it easier to embed it into other environments which
86 86 # are not a normal terminal (such as a GUI-based shell)
87 87 def __init__(self,cin=None,cout=None,cerr=None):
88 88 self.cin = IOStream(cin,sys.stdin)
89 89 self.cout = IOStream(cout,sys.stdout)
90 90 self.cerr = IOStream(cerr,sys.stderr)
91 91
92 92 # Global variable to be used for all I/O
93 93 Term = IOTerm()
94 94
95 95 import IPython.rlineimpl as readline
96 96 # Remake Term to use the readline i/o facilities
97 97 if sys.platform == 'win32' and readline.have_readline:
98 98
99 99 Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile)
100 100
101 101
102 102 #****************************************************************************
103 103 # Generic warning/error printer, used by everything else
104 104 def warn(msg,level=2,exit_val=1):
105 105 """Standard warning printer. Gives formatting consistency.
106 106
107 107 Output is sent to Term.cerr (sys.stderr by default).
108 108
109 109 Options:
110 110
111 111 -level(2): allows finer control:
112 112 0 -> Do nothing, dummy function.
113 113 1 -> Print message.
114 114 2 -> Print 'WARNING:' + message. (Default level).
115 115 3 -> Print 'ERROR:' + message.
116 116 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
117 117
118 118 -exit_val (1): exit value returned by sys.exit() for a level 4
119 119 warning. Ignored for all other levels."""
120 120
121 121 if level>0:
122 122 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
123 123 print >> Term.cerr, '%s%s' % (header[level],msg)
124 124 if level == 4:
125 125 print >> Term.cerr,'Exiting.\n'
126 126 sys.exit(exit_val)
127 127
128 128 def info(msg):
129 129 """Equivalent to warn(msg,level=1)."""
130 130
131 131 warn(msg,level=1)
132 132
133 133 def error(msg):
134 134 """Equivalent to warn(msg,level=3)."""
135 135
136 136 warn(msg,level=3)
137 137
138 138 def fatal(msg,exit_val=1):
139 139 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
140 140
141 141 warn(msg,exit_val=exit_val,level=4)
142 142
143 143 #---------------------------------------------------------------------------
144 144 # Debugging routines
145 145 #
146 146 def debugx(expr,pre_msg=''):
147 147 """Print the value of an expression from the caller's frame.
148 148
149 149 Takes an expression, evaluates it in the caller's frame and prints both
150 150 the given expression and the resulting value (as well as a debug mark
151 151 indicating the name of the calling function. The input must be of a form
152 152 suitable for eval().
153 153
154 154 An optional message can be passed, which will be prepended to the printed
155 155 expr->value pair."""
156 156
157 157 cf = sys._getframe(1)
158 158 print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr,
159 159 eval(expr,cf.f_globals,cf.f_locals))
160 160
161 161 # deactivate it by uncommenting the following line, which makes it a no-op
162 162 #def debugx(expr,pre_msg=''): pass
163 163
164 164 #----------------------------------------------------------------------------
165 165 StringTypes = types.StringTypes
166 166
167 167 # Basic timing functionality
168 168
169 169 # If possible (Unix), use the resource module instead of time.clock()
170 170 try:
171 171 import resource
172 172 def clocku():
173 173 """clocku() -> floating point number
174 174
175 175 Return the *USER* CPU time in seconds since the start of the process.
176 176 This is done via a call to resource.getrusage, so it avoids the
177 177 wraparound problems in time.clock()."""
178 178
179 179 return resource.getrusage(resource.RUSAGE_SELF)[0]
180 180
181 181 def clocks():
182 182 """clocks() -> floating point number
183 183
184 184 Return the *SYSTEM* CPU time in seconds since the start of the process.
185 185 This is done via a call to resource.getrusage, so it avoids the
186 186 wraparound problems in time.clock()."""
187 187
188 188 return resource.getrusage(resource.RUSAGE_SELF)[1]
189 189
190 190 def clock():
191 191 """clock() -> floating point number
192 192
193 193 Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of
194 194 the process. This is done via a call to resource.getrusage, so it
195 195 avoids the wraparound problems in time.clock()."""
196 196
197 197 u,s = resource.getrusage(resource.RUSAGE_SELF)[:2]
198 198 return u+s
199 199
200 200 def clock2():
201 201 """clock2() -> (t_user,t_system)
202 202
203 203 Similar to clock(), but return a tuple of user/system times."""
204 204 return resource.getrusage(resource.RUSAGE_SELF)[:2]
205 205
206 206 except ImportError:
207 207 # There is no distinction of user/system time under windows, so we just use
208 208 # time.clock() for everything...
209 209 clocku = clocks = clock = time.clock
210 210 def clock2():
211 211 """Under windows, system CPU time can't be measured.
212 212
213 213 This just returns clock() and zero."""
214 214 return time.clock(),0.0
215 215
216 216 def timings_out(reps,func,*args,**kw):
217 217 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
218 218
219 219 Execute a function reps times, return a tuple with the elapsed total
220 220 CPU time in seconds, the time per call and the function's output.
221 221
222 222 Under Unix, the return value is the sum of user+system time consumed by
223 223 the process, computed via the resource module. This prevents problems
224 224 related to the wraparound effect which the time.clock() function has.
225 225
226 226 Under Windows the return value is in wall clock seconds. See the
227 227 documentation for the time module for more details."""
228 228
229 229 reps = int(reps)
230 230 assert reps >=1, 'reps must be >= 1'
231 231 if reps==1:
232 232 start = clock()
233 233 out = func(*args,**kw)
234 234 tot_time = clock()-start
235 235 else:
236 236 rng = xrange(reps-1) # the last time is executed separately to store output
237 237 start = clock()
238 238 for dummy in rng: func(*args,**kw)
239 239 out = func(*args,**kw) # one last time
240 240 tot_time = clock()-start
241 241 av_time = tot_time / reps
242 242 return tot_time,av_time,out
243 243
244 244 def timings(reps,func,*args,**kw):
245 245 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
246 246
247 247 Execute a function reps times, return a tuple with the elapsed total CPU
248 248 time in seconds and the time per call. These are just the first two values
249 249 in timings_out()."""
250 250
251 251 return timings_out(reps,func,*args,**kw)[0:2]
252 252
253 253 def timing(func,*args,**kw):
254 254 """timing(func,*args,**kw) -> t_total
255 255
256 256 Execute a function once, return the elapsed total CPU time in
257 257 seconds. This is just the first value in timings_out()."""
258 258
259 259 return timings_out(1,func,*args,**kw)[0]
260 260
261 261 #****************************************************************************
262 262 # file and system
263 263
264 264 def arg_split(s,posix=False):
265 265 """Split a command line's arguments in a shell-like manner.
266 266
267 267 This is a modified version of the standard library's shlex.split()
268 268 function, but with a default of posix=False for splitting, so that quotes
269 269 in inputs are respected."""
270 270
271 271 # XXX - there may be unicode-related problems here!!! I'm not sure that
272 272 # shlex is truly unicode-safe, so it might be necessary to do
273 273 #
274 274 # s = s.encode(sys.stdin.encoding)
275 275 #
276 276 # first, to ensure that shlex gets a normal string. Input from anyone who
277 277 # knows more about unicode and shlex than I would be good to have here...
278 278 lex = shlex.shlex(s, posix=posix)
279 279 lex.whitespace_split = True
280 280 return list(lex)
281 281
282 282 def system(cmd,verbose=0,debug=0,header=''):
283 283 """Execute a system command, return its exit status.
284 284
285 285 Options:
286 286
287 287 - verbose (0): print the command to be executed.
288 288
289 289 - debug (0): only print, do not actually execute.
290 290
291 291 - header (''): Header to print on screen prior to the executed command (it
292 292 is only prepended to the command, no newlines are added).
293 293
294 294 Note: a stateful version of this function is available through the
295 295 SystemExec class."""
296 296
297 297 stat = 0
298 298 if verbose or debug: print header+cmd
299 299 sys.stdout.flush()
300 300 if not debug: stat = os.system(cmd)
301 301 return stat
302 302
303 303 # This function is used by ipython in a lot of places to make system calls.
304 304 # We need it to be slightly different under win32, due to the vagaries of
305 305 # 'network shares'. A win32 override is below.
306 306
307 307 def shell(cmd,verbose=0,debug=0,header=''):
308 308 """Execute a command in the system shell, always return None.
309 309
310 310 Options:
311 311
312 312 - verbose (0): print the command to be executed.
313 313
314 314 - debug (0): only print, do not actually execute.
315 315
316 316 - header (''): Header to print on screen prior to the executed command (it
317 317 is only prepended to the command, no newlines are added).
318 318
319 319 Note: this is similar to genutils.system(), but it returns None so it can
320 320 be conveniently used in interactive loops without getting the return value
321 321 (typically 0) printed many times."""
322 322
323 323 stat = 0
324 324 if verbose or debug: print header+cmd
325 325 # flush stdout so we don't mangle python's buffering
326 326 sys.stdout.flush()
327 327
328 328 if not debug:
329 329 platutils.set_term_title("IPy:" + cmd)
330 330 os.system(cmd)
331 331 platutils.set_term_title("IPy:" + os.path.basename(os.getcwd()))
332 332
333 333 # override shell() for win32 to deal with network shares
334 334 if os.name in ('nt','dos'):
335 335
336 336 shell_ori = shell
337 337
338 338 def shell(cmd,verbose=0,debug=0,header=''):
339 339 if os.getcwd().startswith(r"\\"):
340 340 path = os.getcwd()
341 341 # change to c drive (cannot be on UNC-share when issuing os.system,
342 342 # as cmd.exe cannot handle UNC addresses)
343 343 os.chdir("c:")
344 344 # issue pushd to the UNC-share and then run the command
345 345 try:
346 346 shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header)
347 347 finally:
348 348 os.chdir(path)
349 349 else:
350 350 shell_ori(cmd,verbose,debug,header)
351 351
352 352 shell.__doc__ = shell_ori.__doc__
353 353
354 354 def getoutput(cmd,verbose=0,debug=0,header='',split=0):
355 355 """Dummy substitute for perl's backquotes.
356 356
357 357 Executes a command and returns the output.
358 358
359 359 Accepts the same arguments as system(), plus:
360 360
361 361 - split(0): if true, the output is returned as a list split on newlines.
362 362
363 363 Note: a stateful version of this function is available through the
364 364 SystemExec class.
365 365
366 366 This is pretty much deprecated and rarely used,
367 367 genutils.getoutputerror may be what you need.
368 368
369 369 """
370 370
371 371 if verbose or debug: print header+cmd
372 372 if not debug:
373 373 output = os.popen(cmd).read()
374 374 # stipping last \n is here for backwards compat.
375 375 if output.endswith('\n'):
376 376 output = output[:-1]
377 377 if split:
378 378 return output.split('\n')
379 379 else:
380 380 return output
381 381
382 382 def getoutputerror(cmd,verbose=0,debug=0,header='',split=0):
383 383 """Return (standard output,standard error) of executing cmd in a shell.
384 384
385 385 Accepts the same arguments as system(), plus:
386 386
387 387 - split(0): if true, each of stdout/err is returned as a list split on
388 388 newlines.
389 389
390 390 Note: a stateful version of this function is available through the
391 391 SystemExec class."""
392 392
393 393 if verbose or debug: print header+cmd
394 394 if not cmd:
395 395 if split:
396 396 return [],[]
397 397 else:
398 398 return '',''
399 399 if not debug:
400 400 pin,pout,perr = os.popen3(cmd)
401 401 tout = pout.read().rstrip()
402 402 terr = perr.read().rstrip()
403 403 pin.close()
404 404 pout.close()
405 405 perr.close()
406 406 if split:
407 407 return tout.split('\n'),terr.split('\n')
408 408 else:
409 409 return tout,terr
410 410
411 411 # for compatibility with older naming conventions
412 412 xsys = system
413 413 bq = getoutput
414 414
415 415 class SystemExec:
416 416 """Access the system and getoutput functions through a stateful interface.
417 417
418 418 Note: here we refer to the system and getoutput functions from this
419 419 library, not the ones from the standard python library.
420 420
421 421 This class offers the system and getoutput functions as methods, but the
422 422 verbose, debug and header parameters can be set for the instance (at
423 423 creation time or later) so that they don't need to be specified on each
424 424 call.
425 425
426 426 For efficiency reasons, there's no way to override the parameters on a
427 427 per-call basis other than by setting instance attributes. If you need
428 428 local overrides, it's best to directly call system() or getoutput().
429 429
430 430 The following names are provided as alternate options:
431 431 - xsys: alias to system
432 432 - bq: alias to getoutput
433 433
434 434 An instance can then be created as:
435 435 >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ')
436 436
437 437 And used as:
438 438 >>> sysexec.xsys('pwd')
439 439 >>> dirlist = sysexec.bq('ls -l')
440 440 """
441 441
442 442 def __init__(self,verbose=0,debug=0,header='',split=0):
443 443 """Specify the instance's values for verbose, debug and header."""
444 444 setattr_list(self,'verbose debug header split')
445 445
446 446 def system(self,cmd):
447 447 """Stateful interface to system(), with the same keyword parameters."""
448 448
449 449 system(cmd,self.verbose,self.debug,self.header)
450 450
451 451 def shell(self,cmd):
452 452 """Stateful interface to shell(), with the same keyword parameters."""
453 453
454 454 shell(cmd,self.verbose,self.debug,self.header)
455 455
456 456 xsys = system # alias
457 457
458 458 def getoutput(self,cmd):
459 459 """Stateful interface to getoutput()."""
460 460
461 461 return getoutput(cmd,self.verbose,self.debug,self.header,self.split)
462 462
463 463 def getoutputerror(self,cmd):
464 464 """Stateful interface to getoutputerror()."""
465 465
466 466 return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split)
467 467
468 468 bq = getoutput # alias
469 469
470 470 #-----------------------------------------------------------------------------
471 471 def mutex_opts(dict,ex_op):
472 472 """Check for presence of mutually exclusive keys in a dict.
473 473
474 474 Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
475 475 for op1,op2 in ex_op:
476 476 if op1 in dict and op2 in dict:
477 477 raise ValueError,'\n*** ERROR in Arguments *** '\
478 478 'Options '+op1+' and '+op2+' are mutually exclusive.'
479 479
480 480 #-----------------------------------------------------------------------------
481 481 def get_py_filename(name):
482 482 """Return a valid python filename in the current directory.
483 483
484 484 If the given name is not a file, it adds '.py' and searches again.
485 485 Raises IOError with an informative message if the file isn't found."""
486 486
487 487 name = os.path.expanduser(name)
488 488 if not os.path.isfile(name) and not name.endswith('.py'):
489 489 name += '.py'
490 490 if os.path.isfile(name):
491 491 return name
492 492 else:
493 493 raise IOError,'File `%s` not found.' % name
494 494
495 495 #-----------------------------------------------------------------------------
496 496 def filefind(fname,alt_dirs = None):
497 497 """Return the given filename either in the current directory, if it
498 498 exists, or in a specified list of directories.
499 499
500 500 ~ expansion is done on all file and directory names.
501 501
502 502 Upon an unsuccessful search, raise an IOError exception."""
503 503
504 504 if alt_dirs is None:
505 505 try:
506 506 alt_dirs = get_home_dir()
507 507 except HomeDirError:
508 508 alt_dirs = os.getcwd()
509 509 search = [fname] + list_strings(alt_dirs)
510 510 search = map(os.path.expanduser,search)
511 511 #print 'search list for',fname,'list:',search # dbg
512 512 fname = search[0]
513 513 if os.path.isfile(fname):
514 514 return fname
515 515 for direc in search[1:]:
516 516 testname = os.path.join(direc,fname)
517 517 #print 'testname',testname # dbg
518 518 if os.path.isfile(testname):
519 519 return testname
520 520 raise IOError,'File' + `fname` + \
521 521 ' not found in current or supplied directories:' + `alt_dirs`
522 522
523 523 #----------------------------------------------------------------------------
524 524 def file_read(filename):
525 525 """Read a file and close it. Returns the file source."""
526 526 fobj = open(filename,'r');
527 527 source = fobj.read();
528 528 fobj.close()
529 529 return source
530 530
531 531 def file_readlines(filename):
532 532 """Read a file and close it. Returns the file source using readlines()."""
533 533 fobj = open(filename,'r');
534 534 lines = fobj.readlines();
535 535 fobj.close()
536 536 return lines
537 537
538 538 #----------------------------------------------------------------------------
539 539 def target_outdated(target,deps):
540 540 """Determine whether a target is out of date.
541 541
542 542 target_outdated(target,deps) -> 1/0
543 543
544 544 deps: list of filenames which MUST exist.
545 545 target: single filename which may or may not exist.
546 546
547 547 If target doesn't exist or is older than any file listed in deps, return
548 548 true, otherwise return false.
549 549 """
550 550 try:
551 551 target_time = os.path.getmtime(target)
552 552 except os.error:
553 553 return 1
554 554 for dep in deps:
555 555 dep_time = os.path.getmtime(dep)
556 556 if dep_time > target_time:
557 557 #print "For target",target,"Dep failed:",dep # dbg
558 558 #print "times (dep,tar):",dep_time,target_time # dbg
559 559 return 1
560 560 return 0
561 561
562 562 #-----------------------------------------------------------------------------
563 563 def target_update(target,deps,cmd):
564 564 """Update a target with a given command given a list of dependencies.
565 565
566 566 target_update(target,deps,cmd) -> runs cmd if target is outdated.
567 567
568 568 This is just a wrapper around target_outdated() which calls the given
569 569 command if target is outdated."""
570 570
571 571 if target_outdated(target,deps):
572 572 xsys(cmd)
573 573
574 574 #----------------------------------------------------------------------------
575 575 def unquote_ends(istr):
576 576 """Remove a single pair of quotes from the endpoints of a string."""
577 577
578 578 if not istr:
579 579 return istr
580 580 if (istr[0]=="'" and istr[-1]=="'") or \
581 581 (istr[0]=='"' and istr[-1]=='"'):
582 582 return istr[1:-1]
583 583 else:
584 584 return istr
585 585
586 586 #----------------------------------------------------------------------------
587 587 def process_cmdline(argv,names=[],defaults={},usage=''):
588 588 """ Process command-line options and arguments.
589 589
590 590 Arguments:
591 591
592 592 - argv: list of arguments, typically sys.argv.
593 593
594 594 - names: list of option names. See DPyGetOpt docs for details on options
595 595 syntax.
596 596
597 597 - defaults: dict of default values.
598 598
599 599 - usage: optional usage notice to print if a wrong argument is passed.
600 600
601 601 Return a dict of options and a list of free arguments."""
602 602
603 603 getopt = DPyGetOpt.DPyGetOpt()
604 604 getopt.setIgnoreCase(0)
605 605 getopt.parseConfiguration(names)
606 606
607 607 try:
608 608 getopt.processArguments(argv)
609 609 except:
610 610 print usage
611 611 warn(`sys.exc_value`,level=4)
612 612
613 613 defaults.update(getopt.optionValues)
614 614 args = getopt.freeValues
615 615
616 616 return defaults,args
617 617
618 618 #----------------------------------------------------------------------------
619 619 def optstr2types(ostr):
620 620 """Convert a string of option names to a dict of type mappings.
621 621
622 622 optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'}
623 623
624 624 This is used to get the types of all the options in a string formatted
625 625 with the conventions of DPyGetOpt. The 'type' None is used for options
626 626 which are strings (they need no further conversion). This function's main
627 627 use is to get a typemap for use with read_dict().
628 628 """
629 629
630 630 typeconv = {None:'',int:'',float:''}
631 631 typemap = {'s':None,'i':int,'f':float}
632 632 opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)')
633 633
634 634 for w in ostr.split():
635 635 oname,alias,otype = opt_re.match(w).groups()
636 636 if otype == '' or alias == '!': # simple switches are integers too
637 637 otype = 'i'
638 638 typeconv[typemap[otype]] += oname + ' '
639 639 return typeconv
640 640
641 641 #----------------------------------------------------------------------------
642 642 def read_dict(filename,type_conv=None,**opt):
643 643
644 644 """Read a dictionary of key=value pairs from an input file, optionally
645 645 performing conversions on the resulting values.
646 646
647 647 read_dict(filename,type_conv,**opt) -> dict
648 648
649 649 Only one value per line is accepted, the format should be
650 650 # optional comments are ignored
651 651 key value\n
652 652
653 653 Args:
654 654
655 655 - type_conv: A dictionary specifying which keys need to be converted to
656 656 which types. By default all keys are read as strings. This dictionary
657 657 should have as its keys valid conversion functions for strings
658 658 (int,long,float,complex, or your own). The value for each key
659 659 (converter) should be a whitespace separated string containing the names
660 660 of all the entries in the file to be converted using that function. For
661 661 keys to be left alone, use None as the conversion function (only needed
662 662 with purge=1, see below).
663 663
664 664 - opt: dictionary with extra options as below (default in parens)
665 665
666 666 purge(0): if set to 1, all keys *not* listed in type_conv are purged out
667 667 of the dictionary to be returned. If purge is going to be used, the
668 668 set of keys to be left as strings also has to be explicitly specified
669 669 using the (non-existent) conversion function None.
670 670
671 671 fs(None): field separator. This is the key/value separator to be used
672 672 when parsing the file. The None default means any whitespace [behavior
673 673 of string.split()].
674 674
675 675 strip(0): if 1, strip string values of leading/trailinig whitespace.
676 676
677 677 warn(1): warning level if requested keys are not found in file.
678 678 - 0: silently ignore.
679 679 - 1: inform but proceed.
680 680 - 2: raise KeyError exception.
681 681
682 682 no_empty(0): if 1, remove keys with whitespace strings as a value.
683 683
684 684 unique([]): list of keys (or space separated string) which can't be
685 685 repeated. If one such key is found in the file, each new instance
686 686 overwrites the previous one. For keys not listed here, the behavior is
687 687 to make a list of all appearances.
688 688
689 689 Example:
690 690 If the input file test.ini has:
691 691 i 3
692 692 x 4.5
693 693 y 5.5
694 694 s hi ho
695 695 Then:
696 696
697 697 >>> type_conv={int:'i',float:'x',None:'s'}
698 698 >>> read_dict('test.ini')
699 699 {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'}
700 700 >>> read_dict('test.ini',type_conv)
701 701 {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'}
702 702 >>> read_dict('test.ini',type_conv,purge=1)
703 703 {'i': 3, 's': 'hi ho', 'x': 4.5}
704 704 """
705 705
706 706 # starting config
707 707 opt.setdefault('purge',0)
708 708 opt.setdefault('fs',None) # field sep defaults to any whitespace
709 709 opt.setdefault('strip',0)
710 710 opt.setdefault('warn',1)
711 711 opt.setdefault('no_empty',0)
712 712 opt.setdefault('unique','')
713 713 if type(opt['unique']) in StringTypes:
714 714 unique_keys = qw(opt['unique'])
715 715 elif type(opt['unique']) in (types.TupleType,types.ListType):
716 716 unique_keys = opt['unique']
717 717 else:
718 718 raise ValueError, 'Unique keys must be given as a string, List or Tuple'
719 719
720 720 dict = {}
721 721 # first read in table of values as strings
722 722 file = open(filename,'r')
723 723 for line in file.readlines():
724 724 line = line.strip()
725 725 if len(line) and line[0]=='#': continue
726 726 if len(line)>0:
727 727 lsplit = line.split(opt['fs'],1)
728 728 try:
729 729 key,val = lsplit
730 730 except ValueError:
731 731 key,val = lsplit[0],''
732 732 key = key.strip()
733 733 if opt['strip']: val = val.strip()
734 734 if val == "''" or val == '""': val = ''
735 735 if opt['no_empty'] and (val=='' or val.isspace()):
736 736 continue
737 737 # if a key is found more than once in the file, build a list
738 738 # unless it's in the 'unique' list. In that case, last found in file
739 739 # takes precedence. User beware.
740 740 try:
741 741 if dict[key] and key in unique_keys:
742 742 dict[key] = val
743 743 elif type(dict[key]) is types.ListType:
744 744 dict[key].append(val)
745 745 else:
746 746 dict[key] = [dict[key],val]
747 747 except KeyError:
748 748 dict[key] = val
749 749 # purge if requested
750 750 if opt['purge']:
751 751 accepted_keys = qwflat(type_conv.values())
752 752 for key in dict.keys():
753 753 if key in accepted_keys: continue
754 754 del(dict[key])
755 755 # now convert if requested
756 756 if type_conv==None: return dict
757 757 conversions = type_conv.keys()
758 758 try: conversions.remove(None)
759 759 except: pass
760 760 for convert in conversions:
761 761 for val in qw(type_conv[convert]):
762 762 try:
763 763 dict[val] = convert(dict[val])
764 764 except KeyError,e:
765 765 if opt['warn'] == 0:
766 766 pass
767 767 elif opt['warn'] == 1:
768 768 print >>sys.stderr, 'Warning: key',val,\
769 769 'not found in file',filename
770 770 elif opt['warn'] == 2:
771 771 raise KeyError,e
772 772 else:
773 773 raise ValueError,'Warning level must be 0,1 or 2'
774 774
775 775 return dict
776 776
777 777 #----------------------------------------------------------------------------
778 778 def flag_calls(func):
779 779 """Wrap a function to detect and flag when it gets called.
780 780
781 781 This is a decorator which takes a function and wraps it in a function with
782 782 a 'called' attribute. wrapper.called is initialized to False.
783 783
784 784 The wrapper.called attribute is set to False right before each call to the
785 785 wrapped function, so if the call fails it remains False. After the call
786 786 completes, wrapper.called is set to True and the output is returned.
787 787
788 788 Testing for truth in wrapper.called allows you to determine if a call to
789 789 func() was attempted and succeeded."""
790 790
791 791 def wrapper(*args,**kw):
792 792 wrapper.called = False
793 793 out = func(*args,**kw)
794 794 wrapper.called = True
795 795 return out
796 796
797 797 wrapper.called = False
798 798 wrapper.__doc__ = func.__doc__
799 799 return wrapper
800 800
801 801 #----------------------------------------------------------------------------
802 802 class HomeDirError(Error):
803 803 pass
804 804
805 805 def get_home_dir():
806 806 """Return the closest possible equivalent to a 'home' directory.
807 807
808 808 We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH.
809 809
810 810 Currently only Posix and NT are implemented, a HomeDirError exception is
811 811 raised for all other OSes. """
812 812
813 813 isdir = os.path.isdir
814 814 env = os.environ
815 815
816 816 # first, check py2exe distribution root directory for _ipython.
817 817 # This overrides all. Normally does not exist.
818 818
819 819 if '\\library.zip\\' in IPython.__file__.lower():
820 820 root, rest = IPython.__file__.lower().split('library.zip')
821 821 if isdir(root + '_ipython'):
822 822 os.environ["IPYKITROOT"] = root.rstrip('\\')
823 823 return root
824 824
825 825 try:
826 826 homedir = env['HOME']
827 827 if not isdir(homedir):
828 828 # in case a user stuck some string which does NOT resolve to a
829 829 # valid path, it's as good as if we hadn't foud it
830 830 raise KeyError
831 831 return homedir
832 832 except KeyError:
833 833 if os.name == 'posix':
834 834 raise HomeDirError,'undefined $HOME, IPython can not proceed.'
835 835 elif os.name == 'nt':
836 836 # For some strange reason, win9x returns 'nt' for os.name.
837 837 try:
838 838 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
839 839 if not isdir(homedir):
840 840 homedir = os.path.join(env['USERPROFILE'])
841 841 if not isdir(homedir):
842 842 raise HomeDirError
843 843 return homedir
844 844 except:
845 845 try:
846 846 # Use the registry to get the 'My Documents' folder.
847 847 import _winreg as wreg
848 848 key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
849 849 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
850 850 homedir = wreg.QueryValueEx(key,'Personal')[0]
851 851 key.Close()
852 852 if not isdir(homedir):
853 853 e = ('Invalid "Personal" folder registry key '
854 854 'typically "My Documents".\n'
855 855 'Value: %s\n'
856 856 'This is not a valid directory on your system.' %
857 857 homedir)
858 858 raise HomeDirError(e)
859 859 return homedir
860 860 except HomeDirError:
861 861 raise
862 862 except:
863 863 return 'C:\\'
864 864 elif os.name == 'dos':
865 865 # Desperate, may do absurd things in classic MacOS. May work under DOS.
866 866 return 'C:\\'
867 867 else:
868 868 raise HomeDirError,'support for your operating system not implemented.'
869 869
870 870 #****************************************************************************
871 871 # strings and text
872 872
873 873 class LSString(str):
874 874 """String derivative with a special access attributes.
875 875
876 876 These are normal strings, but with the special attributes:
877 877
878 878 .l (or .list) : value as list (split on newlines).
879 879 .n (or .nlstr): original value (the string itself).
880 880 .s (or .spstr): value as whitespace-separated string.
881 881 .p (or .paths): list of path objects
882 882
883 883 Any values which require transformations are computed only once and
884 884 cached.
885 885
886 886 Such strings are very useful to efficiently interact with the shell, which
887 887 typically only understands whitespace-separated options for commands."""
888 888
889 889 def get_list(self):
890 890 try:
891 891 return self.__list
892 892 except AttributeError:
893 893 self.__list = self.split('\n')
894 894 return self.__list
895 895
896 896 l = list = property(get_list)
897 897
898 898 def get_spstr(self):
899 899 try:
900 900 return self.__spstr
901 901 except AttributeError:
902 902 self.__spstr = self.replace('\n',' ')
903 903 return self.__spstr
904 904
905 905 s = spstr = property(get_spstr)
906 906
907 907 def get_nlstr(self):
908 908 return self
909 909
910 910 n = nlstr = property(get_nlstr)
911 911
912 912 def get_paths(self):
913 913 try:
914 914 return self.__paths
915 915 except AttributeError:
916 916 self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)]
917 917 return self.__paths
918 918
919 919 p = paths = property(get_paths)
920 920
921 921 def print_lsstring(arg):
922 922 """ Prettier (non-repr-like) and more informative printer for LSString """
923 923 print "LSString (.p, .n, .l, .s available). Value:"
924 924 print arg
925 925
926 926 print_lsstring = result_display.when_type(LSString)(print_lsstring)
927 927
928 928 #----------------------------------------------------------------------------
929 929 class SList(list):
930 930 """List derivative with a special access attributes.
931 931
932 932 These are normal lists, but with the special attributes:
933 933
934 934 .l (or .list) : value as list (the list itself).
935 935 .n (or .nlstr): value as a string, joined on newlines.
936 936 .s (or .spstr): value as a string, joined on spaces.
937 937 .p (or .paths): list of path objects
938 938
939 939 Any values which require transformations are computed only once and
940 940 cached."""
941 941
942 942 def get_list(self):
943 943 return self
944 944
945 945 l = list = property(get_list)
946 946
947 947 def get_spstr(self):
948 948 try:
949 949 return self.__spstr
950 950 except AttributeError:
951 951 self.__spstr = ' '.join(self)
952 952 return self.__spstr
953 953
954 954 s = spstr = property(get_spstr)
955 955
956 956 def get_nlstr(self):
957 957 try:
958 958 return self.__nlstr
959 959 except AttributeError:
960 960 self.__nlstr = '\n'.join(self)
961 961 return self.__nlstr
962 962
963 963 n = nlstr = property(get_nlstr)
964 964
965 965 def get_paths(self):
966 966 try:
967 967 return self.__paths
968 968 except AttributeError:
969 969 self.__paths = [path(p) for p in self if os.path.exists(p)]
970 970 return self.__paths
971 971
972 972 p = paths = property(get_paths)
973 973
974 974 #----------------------------------------------------------------------------
975 975 def esc_quotes(strng):
976 976 """Return the input string with single and double quotes escaped out"""
977 977
978 978 return strng.replace('"','\\"').replace("'","\\'")
979 979
980 980 #----------------------------------------------------------------------------
981 981 def make_quoted_expr(s):
982 982 """Return string s in appropriate quotes, using raw string if possible.
983 983
984 984 Effectively this turns string: cd \ao\ao\
985 985 to: r"cd \ao\ao\_"[:-1]
986 986
987 987 Note the use of raw string and padding at the end to allow trailing backslash.
988 988
989 989 """
990 990
991 991 tail = ''
992 992 tailpadding = ''
993 993 raw = ''
994 994 if "\\" in s:
995 995 raw = 'r'
996 996 if s.endswith('\\'):
997 997 tail = '[:-1]'
998 998 tailpadding = '_'
999 999 if '"' not in s:
1000 1000 quote = '"'
1001 1001 elif "'" not in s:
1002 1002 quote = "'"
1003 1003 elif '"""' not in s and not s.endswith('"'):
1004 1004 quote = '"""'
1005 1005 elif "'''" not in s and not s.endswith("'"):
1006 1006 quote = "'''"
1007 1007 else:
1008 1008 # give up, backslash-escaped string will do
1009 1009 return '"%s"' % esc_quotes(s)
1010 1010 res = itpl("$raw$quote$s$tailpadding$quote$tail")
1011 1011 return res
1012 1012
1013 1013
1014 1014 #----------------------------------------------------------------------------
1015 1015 def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'):
1016 1016 """Take multiple lines of input.
1017 1017
1018 1018 A list with each line of input as a separate element is returned when a
1019 1019 termination string is entered (defaults to a single '.'). Input can also
1020 1020 terminate via EOF (^D in Unix, ^Z-RET in Windows).
1021 1021
1022 1022 Lines of input which end in \\ are joined into single entries (and a
1023 1023 secondary continuation prompt is issued as long as the user terminates
1024 1024 lines with \\). This allows entering very long strings which are still
1025 1025 meant to be treated as single entities.
1026 1026 """
1027 1027
1028 1028 try:
1029 1029 if header:
1030 1030 header += '\n'
1031 1031 lines = [raw_input(header + ps1)]
1032 1032 except EOFError:
1033 1033 return []
1034 1034 terminate = [terminate_str]
1035 1035 try:
1036 1036 while lines[-1:] != terminate:
1037 1037 new_line = raw_input(ps1)
1038 1038 while new_line.endswith('\\'):
1039 1039 new_line = new_line[:-1] + raw_input(ps2)
1040 1040 lines.append(new_line)
1041 1041
1042 1042 return lines[:-1] # don't return the termination command
1043 1043 except EOFError:
1044 1044 print
1045 1045 return lines
1046 1046
1047 1047 #----------------------------------------------------------------------------
1048 1048 def raw_input_ext(prompt='', ps2='... '):
1049 1049 """Similar to raw_input(), but accepts extended lines if input ends with \\."""
1050 1050
1051 1051 line = raw_input(prompt)
1052 1052 while line.endswith('\\'):
1053 1053 line = line[:-1] + raw_input(ps2)
1054 1054 return line
1055 1055
1056 1056 #----------------------------------------------------------------------------
1057 1057 def ask_yes_no(prompt,default=None):
1058 """Asks a question and returns an integer 1/0 (y/n) answer.
1058 """Asks a question and returns a boolean (y/n) answer.
1059 1059
1060 1060 If default is given (one of 'y','n'), it is used if the user input is
1061 1061 empty. Otherwise the question is repeated until an answer is given.
1062 1062
1063 1063 An EOF is treated as the default answer. If there is no default, an
1064 1064 exception is raised to prevent infinite loops.
1065 1065
1066 1066 Valid answers are: y/yes/n/no (match is not case sensitive)."""
1067 1067
1068 1068 answers = {'y':True,'n':False,'yes':True,'no':False}
1069 1069 ans = None
1070 1070 while ans not in answers.keys():
1071 1071 try:
1072 1072 ans = raw_input(prompt+' ').lower()
1073 1073 if not ans: # response was an empty string
1074 1074 ans = default
1075 1075 except KeyboardInterrupt:
1076 1076 pass
1077 1077 except EOFError:
1078 1078 if default in answers.keys():
1079 1079 ans = default
1080 1080 print
1081 1081 else:
1082 1082 raise
1083 1083
1084 1084 return answers[ans]
1085 1085
1086 1086 #----------------------------------------------------------------------------
1087 1087 def marquee(txt='',width=78,mark='*'):
1088 1088 """Return the input string centered in a 'marquee'."""
1089 1089 if not txt:
1090 1090 return (mark*width)[:width]
1091 1091 nmark = (width-len(txt)-2)/len(mark)/2
1092 1092 if nmark < 0: nmark =0
1093 1093 marks = mark*nmark
1094 1094 return '%s %s %s' % (marks,txt,marks)
1095 1095
1096 1096 #----------------------------------------------------------------------------
1097 1097 class EvalDict:
1098 1098 """
1099 1099 Emulate a dict which evaluates its contents in the caller's frame.
1100 1100
1101 1101 Usage:
1102 1102 >>>number = 19
1103 1103 >>>text = "python"
1104 1104 >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
1105 1105 """
1106 1106
1107 1107 # This version is due to sismex01@hebmex.com on c.l.py, and is basically a
1108 1108 # modified (shorter) version of:
1109 1109 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
1110 1110 # Skip Montanaro (skip@pobox.com).
1111 1111
1112 1112 def __getitem__(self, name):
1113 1113 frame = sys._getframe(1)
1114 1114 return eval(name, frame.f_globals, frame.f_locals)
1115 1115
1116 1116 EvalString = EvalDict # for backwards compatibility
1117 1117 #----------------------------------------------------------------------------
1118 1118 def qw(words,flat=0,sep=None,maxsplit=-1):
1119 1119 """Similar to Perl's qw() operator, but with some more options.
1120 1120
1121 1121 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
1122 1122
1123 1123 words can also be a list itself, and with flat=1, the output will be
1124 1124 recursively flattened. Examples:
1125 1125
1126 1126 >>> qw('1 2')
1127 1127 ['1', '2']
1128 1128 >>> qw(['a b','1 2',['m n','p q']])
1129 1129 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
1130 1130 >>> qw(['a b','1 2',['m n','p q']],flat=1)
1131 1131 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """
1132 1132
1133 1133 if type(words) in StringTypes:
1134 1134 return [word.strip() for word in words.split(sep,maxsplit)
1135 1135 if word and not word.isspace() ]
1136 1136 if flat:
1137 1137 return flatten(map(qw,words,[1]*len(words)))
1138 1138 return map(qw,words)
1139 1139
1140 1140 #----------------------------------------------------------------------------
1141 1141 def qwflat(words,sep=None,maxsplit=-1):
1142 1142 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
1143 1143 return qw(words,1,sep,maxsplit)
1144 1144
1145 1145 #----------------------------------------------------------------------------
1146 1146 def qw_lol(indata):
1147 1147 """qw_lol('a b') -> [['a','b']],
1148 1148 otherwise it's just a call to qw().
1149 1149
1150 1150 We need this to make sure the modules_some keys *always* end up as a
1151 1151 list of lists."""
1152 1152
1153 1153 if type(indata) in StringTypes:
1154 1154 return [qw(indata)]
1155 1155 else:
1156 1156 return qw(indata)
1157 1157
1158 1158 #-----------------------------------------------------------------------------
1159 1159 def list_strings(arg):
1160 1160 """Always return a list of strings, given a string or list of strings
1161 1161 as input."""
1162 1162
1163 1163 if type(arg) in StringTypes: return [arg]
1164 1164 else: return arg
1165 1165
1166 1166 #----------------------------------------------------------------------------
1167 1167 def grep(pat,list,case=1):
1168 1168 """Simple minded grep-like function.
1169 1169 grep(pat,list) returns occurrences of pat in list, None on failure.
1170 1170
1171 1171 It only does simple string matching, with no support for regexps. Use the
1172 1172 option case=0 for case-insensitive matching."""
1173 1173
1174 1174 # This is pretty crude. At least it should implement copying only references
1175 1175 # to the original data in case it's big. Now it copies the data for output.
1176 1176 out=[]
1177 1177 if case:
1178 1178 for term in list:
1179 1179 if term.find(pat)>-1: out.append(term)
1180 1180 else:
1181 1181 lpat=pat.lower()
1182 1182 for term in list:
1183 1183 if term.lower().find(lpat)>-1: out.append(term)
1184 1184
1185 1185 if len(out): return out
1186 1186 else: return None
1187 1187
1188 1188 #----------------------------------------------------------------------------
1189 1189 def dgrep(pat,*opts):
1190 1190 """Return grep() on dir()+dir(__builtins__).
1191 1191
1192 1192 A very common use of grep() when working interactively."""
1193 1193
1194 1194 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
1195 1195
1196 1196 #----------------------------------------------------------------------------
1197 1197 def idgrep(pat):
1198 1198 """Case-insensitive dgrep()"""
1199 1199
1200 1200 return dgrep(pat,0)
1201 1201
1202 1202 #----------------------------------------------------------------------------
1203 1203 def igrep(pat,list):
1204 1204 """Synonym for case-insensitive grep."""
1205 1205
1206 1206 return grep(pat,list,case=0)
1207 1207
1208 1208 #----------------------------------------------------------------------------
1209 1209 def indent(str,nspaces=4,ntabs=0):
1210 1210 """Indent a string a given number of spaces or tabstops.
1211 1211
1212 1212 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
1213 1213 """
1214 1214 if str is None:
1215 1215 return
1216 1216 ind = '\t'*ntabs+' '*nspaces
1217 1217 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
1218 1218 if outstr.endswith(os.linesep+ind):
1219 1219 return outstr[:-len(ind)]
1220 1220 else:
1221 1221 return outstr
1222 1222
1223 1223 #-----------------------------------------------------------------------------
1224 1224 def native_line_ends(filename,backup=1):
1225 1225 """Convert (in-place) a file to line-ends native to the current OS.
1226 1226
1227 1227 If the optional backup argument is given as false, no backup of the
1228 1228 original file is left. """
1229 1229
1230 1230 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
1231 1231
1232 1232 bak_filename = filename + backup_suffixes[os.name]
1233 1233
1234 1234 original = open(filename).read()
1235 1235 shutil.copy2(filename,bak_filename)
1236 1236 try:
1237 1237 new = open(filename,'wb')
1238 1238 new.write(os.linesep.join(original.splitlines()))
1239 1239 new.write(os.linesep) # ALWAYS put an eol at the end of the file
1240 1240 new.close()
1241 1241 except:
1242 1242 os.rename(bak_filename,filename)
1243 1243 if not backup:
1244 1244 try:
1245 1245 os.remove(bak_filename)
1246 1246 except:
1247 1247 pass
1248 1248
1249 1249 #----------------------------------------------------------------------------
1250 1250 def get_pager_cmd(pager_cmd = None):
1251 1251 """Return a pager command.
1252 1252
1253 1253 Makes some attempts at finding an OS-correct one."""
1254 1254
1255 1255 if os.name == 'posix':
1256 1256 default_pager_cmd = 'less -r' # -r for color control sequences
1257 1257 elif os.name in ['nt','dos']:
1258 1258 default_pager_cmd = 'type'
1259 1259
1260 1260 if pager_cmd is None:
1261 1261 try:
1262 1262 pager_cmd = os.environ['PAGER']
1263 1263 except:
1264 1264 pager_cmd = default_pager_cmd
1265 1265 return pager_cmd
1266 1266
1267 1267 #-----------------------------------------------------------------------------
1268 1268 def get_pager_start(pager,start):
1269 1269 """Return the string for paging files with an offset.
1270 1270
1271 1271 This is the '+N' argument which less and more (under Unix) accept.
1272 1272 """
1273 1273
1274 1274 if pager in ['less','more']:
1275 1275 if start:
1276 1276 start_string = '+' + str(start)
1277 1277 else:
1278 1278 start_string = ''
1279 1279 else:
1280 1280 start_string = ''
1281 1281 return start_string
1282 1282
1283 1283 #----------------------------------------------------------------------------
1284 1284 # (X)emacs on W32 doesn't like to be bypassed with msvcrt.getch()
1285 1285 if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs':
1286 1286 import msvcrt
1287 1287 def page_more():
1288 1288 """ Smart pausing between pages
1289 1289
1290 1290 @return: True if need print more lines, False if quit
1291 1291 """
1292 1292 Term.cout.write('---Return to continue, q to quit--- ')
1293 1293 ans = msvcrt.getch()
1294 1294 if ans in ("q", "Q"):
1295 1295 result = False
1296 1296 else:
1297 1297 result = True
1298 1298 Term.cout.write("\b"*37 + " "*37 + "\b"*37)
1299 1299 return result
1300 1300 else:
1301 1301 def page_more():
1302 1302 ans = raw_input('---Return to continue, q to quit--- ')
1303 1303 if ans.lower().startswith('q'):
1304 1304 return False
1305 1305 else:
1306 1306 return True
1307 1307
1308 1308 esc_re = re.compile(r"(\x1b[^m]+m)")
1309 1309
1310 1310 def page_dumb(strng,start=0,screen_lines=25):
1311 1311 """Very dumb 'pager' in Python, for when nothing else works.
1312 1312
1313 1313 Only moves forward, same interface as page(), except for pager_cmd and
1314 1314 mode."""
1315 1315
1316 1316 out_ln = strng.splitlines()[start:]
1317 1317 screens = chop(out_ln,screen_lines-1)
1318 1318 if len(screens) == 1:
1319 1319 print >>Term.cout, os.linesep.join(screens[0])
1320 1320 else:
1321 1321 last_escape = ""
1322 1322 for scr in screens[0:-1]:
1323 1323 hunk = os.linesep.join(scr)
1324 1324 print >>Term.cout, last_escape + hunk
1325 1325 if not page_more():
1326 1326 return
1327 1327 esc_list = esc_re.findall(hunk)
1328 1328 if len(esc_list) > 0:
1329 1329 last_escape = esc_list[-1]
1330 1330 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
1331 1331
1332 1332 #----------------------------------------------------------------------------
1333 1333 def page(strng,start=0,screen_lines=0,pager_cmd = None):
1334 1334 """Print a string, piping through a pager after a certain length.
1335 1335
1336 1336 The screen_lines parameter specifies the number of *usable* lines of your
1337 1337 terminal screen (total lines minus lines you need to reserve to show other
1338 1338 information).
1339 1339
1340 1340 If you set screen_lines to a number <=0, page() will try to auto-determine
1341 1341 your screen size and will only use up to (screen_size+screen_lines) for
1342 1342 printing, paging after that. That is, if you want auto-detection but need
1343 1343 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
1344 1344 auto-detection without any lines reserved simply use screen_lines = 0.
1345 1345
1346 1346 If a string won't fit in the allowed lines, it is sent through the
1347 1347 specified pager command. If none given, look for PAGER in the environment,
1348 1348 and ultimately default to less.
1349 1349
1350 1350 If no system pager works, the string is sent through a 'dumb pager'
1351 1351 written in python, very simplistic.
1352 1352 """
1353 1353
1354 1354 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
1355 1355 TERM = os.environ.get('TERM','dumb')
1356 1356 if TERM in ['dumb','emacs'] and os.name != 'nt':
1357 1357 print strng
1358 1358 return
1359 1359 # chop off the topmost part of the string we don't want to see
1360 1360 str_lines = strng.split(os.linesep)[start:]
1361 1361 str_toprint = os.linesep.join(str_lines)
1362 1362 num_newlines = len(str_lines)
1363 1363 len_str = len(str_toprint)
1364 1364
1365 1365 # Dumb heuristics to guesstimate number of on-screen lines the string
1366 1366 # takes. Very basic, but good enough for docstrings in reasonable
1367 1367 # terminals. If someone later feels like refining it, it's not hard.
1368 1368 numlines = max(num_newlines,int(len_str/80)+1)
1369 1369
1370 1370 if os.name == "nt":
1371 1371 screen_lines_def = get_console_size(defaulty=25)[1]
1372 1372 else:
1373 1373 screen_lines_def = 25 # default value if we can't auto-determine
1374 1374
1375 1375 # auto-determine screen size
1376 1376 if screen_lines <= 0:
1377 1377 if TERM=='xterm':
1378 1378 try:
1379 1379 import curses
1380 1380 if hasattr(curses,'initscr'):
1381 1381 use_curses = 1
1382 1382 else:
1383 1383 use_curses = 0
1384 1384 except ImportError:
1385 1385 use_curses = 0
1386 1386 else:
1387 1387 # curses causes problems on many terminals other than xterm.
1388 1388 use_curses = 0
1389 1389 if use_curses:
1390 1390 scr = curses.initscr()
1391 1391 screen_lines_real,screen_cols = scr.getmaxyx()
1392 1392 curses.endwin()
1393 1393 screen_lines += screen_lines_real
1394 1394 #print '***Screen size:',screen_lines_real,'lines x',\
1395 1395 #screen_cols,'columns.' # dbg
1396 1396 else:
1397 1397 screen_lines += screen_lines_def
1398 1398
1399 1399 #print 'numlines',numlines,'screenlines',screen_lines # dbg
1400 1400 if numlines <= screen_lines :
1401 1401 #print '*** normal print' # dbg
1402 1402 print >>Term.cout, str_toprint
1403 1403 else:
1404 1404 # Try to open pager and default to internal one if that fails.
1405 1405 # All failure modes are tagged as 'retval=1', to match the return
1406 1406 # value of a failed system command. If any intermediate attempt
1407 1407 # sets retval to 1, at the end we resort to our own page_dumb() pager.
1408 1408 pager_cmd = get_pager_cmd(pager_cmd)
1409 1409 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1410 1410 if os.name == 'nt':
1411 1411 if pager_cmd.startswith('type'):
1412 1412 # The default WinXP 'type' command is failing on complex strings.
1413 1413 retval = 1
1414 1414 else:
1415 1415 tmpname = tempfile.mktemp('.txt')
1416 1416 tmpfile = file(tmpname,'wt')
1417 1417 tmpfile.write(strng)
1418 1418 tmpfile.close()
1419 1419 cmd = "%s < %s" % (pager_cmd,tmpname)
1420 1420 if os.system(cmd):
1421 1421 retval = 1
1422 1422 else:
1423 1423 retval = None
1424 1424 os.remove(tmpname)
1425 1425 else:
1426 1426 try:
1427 1427 retval = None
1428 1428 # if I use popen4, things hang. No idea why.
1429 1429 #pager,shell_out = os.popen4(pager_cmd)
1430 1430 pager = os.popen(pager_cmd,'w')
1431 1431 pager.write(strng)
1432 1432 pager.close()
1433 1433 retval = pager.close() # success returns None
1434 1434 except IOError,msg: # broken pipe when user quits
1435 1435 if msg.args == (32,'Broken pipe'):
1436 1436 retval = None
1437 1437 else:
1438 1438 retval = 1
1439 1439 except OSError:
1440 1440 # Other strange problems, sometimes seen in Win2k/cygwin
1441 1441 retval = 1
1442 1442 if retval is not None:
1443 1443 page_dumb(strng,screen_lines=screen_lines)
1444 1444
1445 1445 #----------------------------------------------------------------------------
1446 1446 def page_file(fname,start = 0, pager_cmd = None):
1447 1447 """Page a file, using an optional pager command and starting line.
1448 1448 """
1449 1449
1450 1450 pager_cmd = get_pager_cmd(pager_cmd)
1451 1451 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1452 1452
1453 1453 try:
1454 1454 if os.environ['TERM'] in ['emacs','dumb']:
1455 1455 raise EnvironmentError
1456 1456 xsys(pager_cmd + ' ' + fname)
1457 1457 except:
1458 1458 try:
1459 1459 if start > 0:
1460 1460 start -= 1
1461 1461 page(open(fname).read(),start)
1462 1462 except:
1463 1463 print 'Unable to show file',`fname`
1464 1464
1465 1465 #----------------------------------------------------------------------------
1466 1466 def snip_print(str,width = 75,print_full = 0,header = ''):
1467 1467 """Print a string snipping the midsection to fit in width.
1468 1468
1469 1469 print_full: mode control:
1470 1470 - 0: only snip long strings
1471 1471 - 1: send to page() directly.
1472 1472 - 2: snip long strings and ask for full length viewing with page()
1473 1473 Return 1 if snipping was necessary, 0 otherwise."""
1474 1474
1475 1475 if print_full == 1:
1476 1476 page(header+str)
1477 1477 return 0
1478 1478
1479 1479 print header,
1480 1480 if len(str) < width:
1481 1481 print str
1482 1482 snip = 0
1483 1483 else:
1484 1484 whalf = int((width -5)/2)
1485 1485 print str[:whalf] + ' <...> ' + str[-whalf:]
1486 1486 snip = 1
1487 1487 if snip and print_full == 2:
1488 1488 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
1489 1489 page(str)
1490 1490 return snip
1491 1491
1492 1492 #****************************************************************************
1493 1493 # lists, dicts and structures
1494 1494
1495 1495 def belong(candidates,checklist):
1496 1496 """Check whether a list of items appear in a given list of options.
1497 1497
1498 1498 Returns a list of 1 and 0, one for each candidate given."""
1499 1499
1500 1500 return [x in checklist for x in candidates]
1501 1501
1502 1502 #----------------------------------------------------------------------------
1503 1503 def uniq_stable(elems):
1504 1504 """uniq_stable(elems) -> list
1505 1505
1506 1506 Return from an iterable, a list of all the unique elements in the input,
1507 1507 but maintaining the order in which they first appear.
1508 1508
1509 1509 A naive solution to this problem which just makes a dictionary with the
1510 1510 elements as keys fails to respect the stability condition, since
1511 1511 dictionaries are unsorted by nature.
1512 1512
1513 1513 Note: All elements in the input must be valid dictionary keys for this
1514 1514 routine to work, as it internally uses a dictionary for efficiency
1515 1515 reasons."""
1516 1516
1517 1517 unique = []
1518 1518 unique_dict = {}
1519 1519 for nn in elems:
1520 1520 if nn not in unique_dict:
1521 1521 unique.append(nn)
1522 1522 unique_dict[nn] = None
1523 1523 return unique
1524 1524
1525 1525 #----------------------------------------------------------------------------
1526 1526 class NLprinter:
1527 1527 """Print an arbitrarily nested list, indicating index numbers.
1528 1528
1529 1529 An instance of this class called nlprint is available and callable as a
1530 1530 function.
1531 1531
1532 1532 nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent'
1533 1533 and using 'sep' to separate the index from the value. """
1534 1534
1535 1535 def __init__(self):
1536 1536 self.depth = 0
1537 1537
1538 1538 def __call__(self,lst,pos='',**kw):
1539 1539 """Prints the nested list numbering levels."""
1540 1540 kw.setdefault('indent',' ')
1541 1541 kw.setdefault('sep',': ')
1542 1542 kw.setdefault('start',0)
1543 1543 kw.setdefault('stop',len(lst))
1544 1544 # we need to remove start and stop from kw so they don't propagate
1545 1545 # into a recursive call for a nested list.
1546 1546 start = kw['start']; del kw['start']
1547 1547 stop = kw['stop']; del kw['stop']
1548 1548 if self.depth == 0 and 'header' in kw.keys():
1549 1549 print kw['header']
1550 1550
1551 1551 for idx in range(start,stop):
1552 1552 elem = lst[idx]
1553 1553 if type(elem)==type([]):
1554 1554 self.depth += 1
1555 1555 self.__call__(elem,itpl('$pos$idx,'),**kw)
1556 1556 self.depth -= 1
1557 1557 else:
1558 1558 printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
1559 1559
1560 1560 nlprint = NLprinter()
1561 1561 #----------------------------------------------------------------------------
1562 1562 def all_belong(candidates,checklist):
1563 1563 """Check whether a list of items ALL appear in a given list of options.
1564 1564
1565 1565 Returns a single 1 or 0 value."""
1566 1566
1567 1567 return 1-(0 in [x in checklist for x in candidates])
1568 1568
1569 1569 #----------------------------------------------------------------------------
1570 1570 def sort_compare(lst1,lst2,inplace = 1):
1571 1571 """Sort and compare two lists.
1572 1572
1573 1573 By default it does it in place, thus modifying the lists. Use inplace = 0
1574 1574 to avoid that (at the cost of temporary copy creation)."""
1575 1575 if not inplace:
1576 1576 lst1 = lst1[:]
1577 1577 lst2 = lst2[:]
1578 1578 lst1.sort(); lst2.sort()
1579 1579 return lst1 == lst2
1580 1580
1581 1581 #----------------------------------------------------------------------------
1582 1582 def mkdict(**kwargs):
1583 1583 """Return a dict from a keyword list.
1584 1584
1585 1585 It's just syntactic sugar for making ditcionary creation more convenient:
1586 1586 # the standard way
1587 1587 >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 }
1588 1588 # a cleaner way
1589 1589 >>>data = dict(red=1, green=2, blue=3)
1590 1590
1591 1591 If you need more than this, look at the Struct() class."""
1592 1592
1593 1593 return kwargs
1594 1594
1595 1595 #----------------------------------------------------------------------------
1596 1596 def list2dict(lst):
1597 1597 """Takes a list of (key,value) pairs and turns it into a dict."""
1598 1598
1599 1599 dic = {}
1600 1600 for k,v in lst: dic[k] = v
1601 1601 return dic
1602 1602
1603 1603 #----------------------------------------------------------------------------
1604 1604 def list2dict2(lst,default=''):
1605 1605 """Takes a list and turns it into a dict.
1606 1606 Much slower than list2dict, but more versatile. This version can take
1607 1607 lists with sublists of arbitrary length (including sclars)."""
1608 1608
1609 1609 dic = {}
1610 1610 for elem in lst:
1611 1611 if type(elem) in (types.ListType,types.TupleType):
1612 1612 size = len(elem)
1613 1613 if size == 0:
1614 1614 pass
1615 1615 elif size == 1:
1616 1616 dic[elem] = default
1617 1617 else:
1618 1618 k,v = elem[0], elem[1:]
1619 1619 if len(v) == 1: v = v[0]
1620 1620 dic[k] = v
1621 1621 else:
1622 1622 dic[elem] = default
1623 1623 return dic
1624 1624
1625 1625 #----------------------------------------------------------------------------
1626 1626 def flatten(seq):
1627 1627 """Flatten a list of lists (NOT recursive, only works for 2d lists)."""
1628 1628
1629 1629 return [x for subseq in seq for x in subseq]
1630 1630
1631 1631 #----------------------------------------------------------------------------
1632 1632 def get_slice(seq,start=0,stop=None,step=1):
1633 1633 """Get a slice of a sequence with variable step. Specify start,stop,step."""
1634 1634 if stop == None:
1635 1635 stop = len(seq)
1636 1636 item = lambda i: seq[i]
1637 1637 return map(item,xrange(start,stop,step))
1638 1638
1639 1639 #----------------------------------------------------------------------------
1640 1640 def chop(seq,size):
1641 1641 """Chop a sequence into chunks of the given size."""
1642 1642 chunk = lambda i: seq[i:i+size]
1643 1643 return map(chunk,xrange(0,len(seq),size))
1644 1644
1645 1645 #----------------------------------------------------------------------------
1646 1646 # with is a keyword as of python 2.5, so this function is renamed to withobj
1647 1647 # from its old 'with' name.
1648 1648 def with_obj(object, **args):
1649 1649 """Set multiple attributes for an object, similar to Pascal's with.
1650 1650
1651 1651 Example:
1652 1652 with_obj(jim,
1653 1653 born = 1960,
1654 1654 haircolour = 'Brown',
1655 1655 eyecolour = 'Green')
1656 1656
1657 1657 Credit: Greg Ewing, in
1658 1658 http://mail.python.org/pipermail/python-list/2001-May/040703.html.
1659 1659
1660 1660 NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with'
1661 1661 has become a keyword for Python 2.5, so we had to rename it."""
1662 1662
1663 1663 object.__dict__.update(args)
1664 1664
1665 1665 #----------------------------------------------------------------------------
1666 1666 def setattr_list(obj,alist,nspace = None):
1667 1667 """Set a list of attributes for an object taken from a namespace.
1668 1668
1669 1669 setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in
1670 1670 alist with their values taken from nspace, which must be a dict (something
1671 1671 like locals() will often do) If nspace isn't given, locals() of the
1672 1672 *caller* is used, so in most cases you can omit it.
1673 1673
1674 1674 Note that alist can be given as a string, which will be automatically
1675 1675 split into a list on whitespace. If given as a list, it must be a list of
1676 1676 *strings* (the variable names themselves), not of variables."""
1677 1677
1678 1678 # this grabs the local variables from the *previous* call frame -- that is
1679 1679 # the locals from the function that called setattr_list().
1680 1680 # - snipped from weave.inline()
1681 1681 if nspace is None:
1682 1682 call_frame = sys._getframe().f_back
1683 1683 nspace = call_frame.f_locals
1684 1684
1685 1685 if type(alist) in StringTypes:
1686 1686 alist = alist.split()
1687 1687 for attr in alist:
1688 1688 val = eval(attr,nspace)
1689 1689 setattr(obj,attr,val)
1690 1690
1691 1691 #----------------------------------------------------------------------------
1692 1692 def getattr_list(obj,alist,*args):
1693 1693 """getattr_list(obj,alist[, default]) -> attribute list.
1694 1694
1695 1695 Get a list of named attributes for an object. When a default argument is
1696 1696 given, it is returned when the attribute doesn't exist; without it, an
1697 1697 exception is raised in that case.
1698 1698
1699 1699 Note that alist can be given as a string, which will be automatically
1700 1700 split into a list on whitespace. If given as a list, it must be a list of
1701 1701 *strings* (the variable names themselves), not of variables."""
1702 1702
1703 1703 if type(alist) in StringTypes:
1704 1704 alist = alist.split()
1705 1705 if args:
1706 1706 if len(args)==1:
1707 1707 default = args[0]
1708 1708 return map(lambda attr: getattr(obj,attr,default),alist)
1709 1709 else:
1710 1710 raise ValueError,'getattr_list() takes only one optional argument'
1711 1711 else:
1712 1712 return map(lambda attr: getattr(obj,attr),alist)
1713 1713
1714 1714 #----------------------------------------------------------------------------
1715 1715 def map_method(method,object_list,*argseq,**kw):
1716 1716 """map_method(method,object_list,*args,**kw) -> list
1717 1717
1718 1718 Return a list of the results of applying the methods to the items of the
1719 1719 argument sequence(s). If more than one sequence is given, the method is
1720 1720 called with an argument list consisting of the corresponding item of each
1721 1721 sequence. All sequences must be of the same length.
1722 1722
1723 1723 Keyword arguments are passed verbatim to all objects called.
1724 1724
1725 1725 This is Python code, so it's not nearly as fast as the builtin map()."""
1726 1726
1727 1727 out_list = []
1728 1728 idx = 0
1729 1729 for object in object_list:
1730 1730 try:
1731 1731 handler = getattr(object, method)
1732 1732 except AttributeError:
1733 1733 out_list.append(None)
1734 1734 else:
1735 1735 if argseq:
1736 1736 args = map(lambda lst:lst[idx],argseq)
1737 1737 #print 'ob',object,'hand',handler,'ar',args # dbg
1738 1738 out_list.append(handler(args,**kw))
1739 1739 else:
1740 1740 out_list.append(handler(**kw))
1741 1741 idx += 1
1742 1742 return out_list
1743 1743
1744 1744 #----------------------------------------------------------------------------
1745 1745 def get_class_members(cls):
1746 1746 ret = dir(cls)
1747 1747 if hasattr(cls,'__bases__'):
1748 1748 for base in cls.__bases__:
1749 1749 ret.extend(get_class_members(base))
1750 1750 return ret
1751 1751
1752 1752 #----------------------------------------------------------------------------
1753 1753 def dir2(obj):
1754 1754 """dir2(obj) -> list of strings
1755 1755
1756 1756 Extended version of the Python builtin dir(), which does a few extra
1757 1757 checks, and supports common objects with unusual internals that confuse
1758 1758 dir(), such as Traits and PyCrust.
1759 1759
1760 1760 This version is guaranteed to return only a list of true strings, whereas
1761 1761 dir() returns anything that objects inject into themselves, even if they
1762 1762 are later not really valid for attribute access (many extension libraries
1763 1763 have such bugs).
1764 1764 """
1765 1765
1766 1766 # Start building the attribute list via dir(), and then complete it
1767 1767 # with a few extra special-purpose calls.
1768 1768 words = dir(obj)
1769 1769
1770 1770 if hasattr(obj,'__class__'):
1771 1771 words.append('__class__')
1772 1772 words.extend(get_class_members(obj.__class__))
1773 1773 #if '__base__' in words: 1/0
1774 1774
1775 1775 # Some libraries (such as traits) may introduce duplicates, we want to
1776 1776 # track and clean this up if it happens
1777 1777 may_have_dupes = False
1778 1778
1779 1779 # this is the 'dir' function for objects with Enthought's traits
1780 1780 if hasattr(obj, 'trait_names'):
1781 1781 try:
1782 1782 words.extend(obj.trait_names())
1783 1783 may_have_dupes = True
1784 1784 except TypeError:
1785 1785 # This will happen if `obj` is a class and not an instance.
1786 1786 pass
1787 1787
1788 1788 # Support for PyCrust-style _getAttributeNames magic method.
1789 1789 if hasattr(obj, '_getAttributeNames'):
1790 1790 try:
1791 1791 words.extend(obj._getAttributeNames())
1792 1792 may_have_dupes = True
1793 1793 except TypeError:
1794 1794 # `obj` is a class and not an instance. Ignore
1795 1795 # this error.
1796 1796 pass
1797 1797
1798 1798 if may_have_dupes:
1799 1799 # eliminate possible duplicates, as some traits may also
1800 1800 # appear as normal attributes in the dir() call.
1801 1801 words = list(set(words))
1802 1802 words.sort()
1803 1803
1804 1804 # filter out non-string attributes which may be stuffed by dir() calls
1805 1805 # and poor coding in third-party modules
1806 1806 return [w for w in words if isinstance(w, basestring)]
1807 1807
1808 1808 #----------------------------------------------------------------------------
1809 1809 def import_fail_info(mod_name,fns=None):
1810 1810 """Inform load failure for a module."""
1811 1811
1812 1812 if fns == None:
1813 1813 warn("Loading of %s failed.\n" % (mod_name,))
1814 1814 else:
1815 1815 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
1816 1816
1817 1817 #----------------------------------------------------------------------------
1818 1818 # Proposed popitem() extension, written as a method
1819 1819
1820 1820
1821 1821 class NotGiven: pass
1822 1822
1823 1823 def popkey(dct,key,default=NotGiven):
1824 1824 """Return dct[key] and delete dct[key].
1825 1825
1826 1826 If default is given, return it if dct[key] doesn't exist, otherwise raise
1827 1827 KeyError. """
1828 1828
1829 1829 try:
1830 1830 val = dct[key]
1831 1831 except KeyError:
1832 1832 if default is NotGiven:
1833 1833 raise
1834 1834 else:
1835 1835 return default
1836 1836 else:
1837 1837 del dct[key]
1838 1838 return val
1839 1839
1840 1840 def wrap_deprecated(func, suggest = '<nothing>'):
1841 1841 def newFunc(*args, **kwargs):
1842 1842 warnings.warn("Call to deprecated function %s, use %s instead" %
1843 1843 ( func.__name__, suggest),
1844 1844 category=DeprecationWarning,
1845 1845 stacklevel = 2)
1846 1846 return func(*args, **kwargs)
1847 1847 return newFunc
1848 1848
1849 1849 #*************************** end of file <genutils.py> **********************
1850 1850
@@ -1,2471 +1,2475 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 IPython -- An enhanced Interactive Python
4 4
5 5 Requires Python 2.3 or newer.
6 6
7 7 This file contains all the classes and helper functions specific to IPython.
8 8
9 $Id: iplib.py 2571 2007-08-01 14:48:03Z vivainio $
9 $Id: iplib.py 2577 2007-08-02 23:50:02Z fperez $
10 10 """
11 11
12 12 #*****************************************************************************
13 13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
14 14 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #
19 19 # Note: this code originally subclassed code.InteractiveConsole from the
20 20 # Python standard library. Over time, all of that class has been copied
21 21 # verbatim here for modifications which could not be accomplished by
22 22 # subclassing. At this point, there are no dependencies at all on the code
23 23 # module anymore (it is not even imported). The Python License (sec. 2)
24 24 # allows for this, but it's always nice to acknowledge credit where credit is
25 25 # due.
26 26 #*****************************************************************************
27 27
28 28 #****************************************************************************
29 29 # Modules and globals
30 30
31 31 from 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 StringIO
41 41 import bdb
42 42 import cPickle as pickle
43 43 import codeop
44 44 import exceptions
45 45 import glob
46 46 import inspect
47 47 import keyword
48 48 import new
49 49 import os
50 50 import pydoc
51 51 import re
52 52 import shutil
53 53 import string
54 54 import sys
55 55 import tempfile
56 56 import traceback
57 57 import types
58 58 import pickleshare
59 59 from sets import Set
60 60 from pprint import pprint, pformat
61 61
62 62 # IPython's own modules
63 63 #import IPython
64 64 from IPython import Debugger,OInspect,PyColorize,ultraTB
65 65 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
66 66 from IPython.FakeModule import FakeModule
67 67 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
68 68 from IPython.Logger import Logger
69 69 from IPython.Magic import Magic
70 70 from IPython.Prompts import CachedOutput
71 71 from IPython.ipstruct import Struct
72 72 from IPython.background_jobs import BackgroundJobManager
73 73 from IPython.usage import cmd_line_usage,interactive_usage
74 74 from IPython.genutils import *
75 75 from IPython.strdispatch import StrDispatch
76 76 import IPython.ipapi
77 77 import IPython.history
78 78 import IPython.prefilter as prefilter
79 79 import IPython.shadowns
80 80 # Globals
81 81
82 82 # store the builtin raw_input globally, and use this always, in case user code
83 83 # overwrites it (like wx.py.PyShell does)
84 84 raw_input_original = raw_input
85 85
86 86 # compiled regexps for autoindent management
87 87 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
88 88
89 89
90 90 #****************************************************************************
91 91 # Some utility function definitions
92 92
93 93 ini_spaces_re = re.compile(r'^(\s+)')
94 94
95 95 def num_ini_spaces(strng):
96 96 """Return the number of initial spaces in a string"""
97 97
98 98 ini_spaces = ini_spaces_re.match(strng)
99 99 if ini_spaces:
100 100 return ini_spaces.end()
101 101 else:
102 102 return 0
103 103
104 104 def softspace(file, newvalue):
105 105 """Copied from code.py, to remove the dependency"""
106 106
107 107 oldvalue = 0
108 108 try:
109 109 oldvalue = file.softspace
110 110 except AttributeError:
111 111 pass
112 112 try:
113 113 file.softspace = newvalue
114 114 except (AttributeError, TypeError):
115 115 # "attribute-less object" or "read-only attributes"
116 116 pass
117 117 return oldvalue
118 118
119 119
120 120 #****************************************************************************
121 121 # Local use exceptions
122 122 class SpaceInInput(exceptions.Exception): pass
123 123
124 124
125 125 #****************************************************************************
126 126 # Local use classes
127 127 class Bunch: pass
128 128
129 129 class Undefined: pass
130 130
131 131 class Quitter(object):
132 132 """Simple class to handle exit, similar to Python 2.5's.
133 133
134 134 It handles exiting in an ipython-safe manner, which the one in Python 2.5
135 135 doesn't do (obviously, since it doesn't know about ipython)."""
136 136
137 137 def __init__(self,shell,name):
138 138 self.shell = shell
139 139 self.name = name
140 140
141 141 def __repr__(self):
142 142 return 'Type %s() to exit.' % self.name
143 143 __str__ = __repr__
144 144
145 145 def __call__(self):
146 146 self.shell.exit()
147 147
148 148 class InputList(list):
149 149 """Class to store user input.
150 150
151 151 It's basically a list, but slices return a string instead of a list, thus
152 152 allowing things like (assuming 'In' is an instance):
153 153
154 154 exec In[4:7]
155 155
156 156 or
157 157
158 158 exec In[5:9] + In[14] + In[21:25]"""
159 159
160 160 def __getslice__(self,i,j):
161 161 return ''.join(list.__getslice__(self,i,j))
162 162
163 163 class SyntaxTB(ultraTB.ListTB):
164 164 """Extension which holds some state: the last exception value"""
165 165
166 166 def __init__(self,color_scheme = 'NoColor'):
167 167 ultraTB.ListTB.__init__(self,color_scheme)
168 168 self.last_syntax_error = None
169 169
170 170 def __call__(self, etype, value, elist):
171 171 self.last_syntax_error = value
172 172 ultraTB.ListTB.__call__(self,etype,value,elist)
173 173
174 174 def clear_err_state(self):
175 175 """Return the current error state and clear it"""
176 176 e = self.last_syntax_error
177 177 self.last_syntax_error = None
178 178 return e
179 179
180 180 #****************************************************************************
181 181 # Main IPython class
182 182
183 183 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
184 184 # until a full rewrite is made. I've cleaned all cross-class uses of
185 185 # attributes and methods, but too much user code out there relies on the
186 186 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
187 187 #
188 188 # But at least now, all the pieces have been separated and we could, in
189 189 # principle, stop using the mixin. This will ease the transition to the
190 190 # chainsaw branch.
191 191
192 192 # For reference, the following is the list of 'self.foo' uses in the Magic
193 193 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
194 194 # class, to prevent clashes.
195 195
196 196 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
197 197 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
198 198 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
199 199 # 'self.value']
200 200
201 201 class InteractiveShell(object,Magic):
202 202 """An enhanced console for Python."""
203 203
204 204 # class attribute to indicate whether the class supports threads or not.
205 205 # Subclasses with thread support should override this as needed.
206 206 isthreaded = False
207 207
208 208 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
209 209 user_ns = None,user_global_ns=None,banner2='',
210 210 custom_exceptions=((),None),embedded=False):
211 211
212 212 # log system
213 213 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
214 214
215 215 # some minimal strict typechecks. For some core data structures, I
216 216 # want actual basic python types, not just anything that looks like
217 217 # one. This is especially true for namespaces.
218 218 for ns in (user_ns,user_global_ns):
219 219 if ns is not None and type(ns) != types.DictType:
220 220 raise TypeError,'namespace must be a dictionary'
221 221
222 222 # Job manager (for jobs run as background threads)
223 223 self.jobs = BackgroundJobManager()
224 224
225 225 # Store the actual shell's name
226 226 self.name = name
227 227
228 228 # We need to know whether the instance is meant for embedding, since
229 229 # global/local namespaces need to be handled differently in that case
230 230 self.embedded = embedded
231 if embedded:
232 # Control variable so users can, from within the embedded instance,
233 # permanently deactivate it.
234 self.embedded_active = True
231 235
232 236 # command compiler
233 237 self.compile = codeop.CommandCompiler()
234 238
235 239 # User input buffer
236 240 self.buffer = []
237 241
238 242 # Default name given in compilation of code
239 243 self.filename = '<ipython console>'
240 244
241 245 # Install our own quitter instead of the builtins. For python2.3-2.4,
242 246 # this brings in behavior like 2.5, and for 2.5 it's identical.
243 247 __builtin__.exit = Quitter(self,'exit')
244 248 __builtin__.quit = Quitter(self,'quit')
245 249
246 250 # Make an empty namespace, which extension writers can rely on both
247 251 # existing and NEVER being used by ipython itself. This gives them a
248 252 # convenient location for storing additional information and state
249 253 # their extensions may require, without fear of collisions with other
250 254 # ipython names that may develop later.
251 255 self.meta = Struct()
252 256
253 257 # Create the namespace where the user will operate. user_ns is
254 258 # normally the only one used, and it is passed to the exec calls as
255 259 # the locals argument. But we do carry a user_global_ns namespace
256 260 # given as the exec 'globals' argument, This is useful in embedding
257 261 # situations where the ipython shell opens in a context where the
258 262 # distinction between locals and globals is meaningful.
259 263
260 264 # FIXME. For some strange reason, __builtins__ is showing up at user
261 265 # level as a dict instead of a module. This is a manual fix, but I
262 266 # should really track down where the problem is coming from. Alex
263 267 # Schmolck reported this problem first.
264 268
265 269 # A useful post by Alex Martelli on this topic:
266 270 # Re: inconsistent value from __builtins__
267 271 # Von: Alex Martelli <aleaxit@yahoo.com>
268 272 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
269 273 # Gruppen: comp.lang.python
270 274
271 275 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
272 276 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
273 277 # > <type 'dict'>
274 278 # > >>> print type(__builtins__)
275 279 # > <type 'module'>
276 280 # > Is this difference in return value intentional?
277 281
278 282 # Well, it's documented that '__builtins__' can be either a dictionary
279 283 # or a module, and it's been that way for a long time. Whether it's
280 284 # intentional (or sensible), I don't know. In any case, the idea is
281 285 # that if you need to access the built-in namespace directly, you
282 286 # should start with "import __builtin__" (note, no 's') which will
283 287 # definitely give you a module. Yeah, it's somewhat confusing:-(.
284 288
285 289 # These routines return properly built dicts as needed by the rest of
286 290 # the code, and can also be used by extension writers to generate
287 291 # properly initialized namespaces.
288 292 user_ns = IPython.ipapi.make_user_ns(user_ns)
289 293 user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns)
290 294
291 295 # Assign namespaces
292 296 # This is the namespace where all normal user variables live
293 297 self.user_ns = user_ns
294 298 # Embedded instances require a separate namespace for globals.
295 299 # Normally this one is unused by non-embedded instances.
296 300 self.user_global_ns = user_global_ns
297 301 # A namespace to keep track of internal data structures to prevent
298 302 # them from cluttering user-visible stuff. Will be updated later
299 303 self.internal_ns = {}
300 304
301 305 # Namespace of system aliases. Each entry in the alias
302 306 # table must be a 2-tuple of the form (N,name), where N is the number
303 307 # of positional arguments of the alias.
304 308 self.alias_table = {}
305 309
306 310 # A table holding all the namespaces IPython deals with, so that
307 311 # introspection facilities can search easily.
308 312 self.ns_table = {'user':user_ns,
309 313 'user_global':user_global_ns,
310 314 'alias':self.alias_table,
311 315 'internal':self.internal_ns,
312 316 'builtin':__builtin__.__dict__
313 317 }
314 318
315 319 # The user namespace MUST have a pointer to the shell itself.
316 320 self.user_ns[name] = self
317 321
318 322 # We need to insert into sys.modules something that looks like a
319 323 # module but which accesses the IPython namespace, for shelve and
320 324 # pickle to work interactively. Normally they rely on getting
321 325 # everything out of __main__, but for embedding purposes each IPython
322 326 # instance has its own private namespace, so we can't go shoving
323 327 # everything into __main__.
324 328
325 329 # note, however, that we should only do this for non-embedded
326 330 # ipythons, which really mimic the __main__.__dict__ with their own
327 331 # namespace. Embedded instances, on the other hand, should not do
328 332 # this because they need to manage the user local/global namespaces
329 333 # only, but they live within a 'normal' __main__ (meaning, they
330 334 # shouldn't overtake the execution environment of the script they're
331 335 # embedded in).
332 336
333 337 if not embedded:
334 338 try:
335 339 main_name = self.user_ns['__name__']
336 340 except KeyError:
337 341 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
338 342 else:
339 343 #print "pickle hack in place" # dbg
340 344 #print 'main_name:',main_name # dbg
341 345 sys.modules[main_name] = FakeModule(self.user_ns)
342 346
343 347 # List of input with multi-line handling.
344 348 # Fill its zero entry, user counter starts at 1
345 349 self.input_hist = InputList(['\n'])
346 350 # This one will hold the 'raw' input history, without any
347 351 # pre-processing. This will allow users to retrieve the input just as
348 352 # it was exactly typed in by the user, with %hist -r.
349 353 self.input_hist_raw = InputList(['\n'])
350 354
351 355 # list of visited directories
352 356 try:
353 357 self.dir_hist = [os.getcwd()]
354 358 except OSError:
355 359 self.dir_hist = []
356 360
357 361 # dict of output history
358 362 self.output_hist = {}
359 363
360 364 # Get system encoding at startup time. Certain terminals (like Emacs
361 365 # under Win32 have it set to None, and we need to have a known valid
362 366 # encoding to use in the raw_input() method
363 367 self.stdin_encoding = sys.stdin.encoding or 'ascii'
364 368
365 369 # dict of things NOT to alias (keywords, builtins and some magics)
366 370 no_alias = {}
367 371 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
368 372 for key in keyword.kwlist + no_alias_magics:
369 373 no_alias[key] = 1
370 374 no_alias.update(__builtin__.__dict__)
371 375 self.no_alias = no_alias
372 376
373 377 # make global variables for user access to these
374 378 self.user_ns['_ih'] = self.input_hist
375 379 self.user_ns['_oh'] = self.output_hist
376 380 self.user_ns['_dh'] = self.dir_hist
377 381
378 382 # user aliases to input and output histories
379 383 self.user_ns['In'] = self.input_hist
380 384 self.user_ns['Out'] = self.output_hist
381 385
382 386 self.user_ns['_sh'] = IPython.shadowns
383 387 # Object variable to store code object waiting execution. This is
384 388 # used mainly by the multithreaded shells, but it can come in handy in
385 389 # other situations. No need to use a Queue here, since it's a single
386 390 # item which gets cleared once run.
387 391 self.code_to_run = None
388 392
389 393 # escapes for automatic behavior on the command line
390 394 self.ESC_SHELL = '!'
391 395 self.ESC_SH_CAP = '!!'
392 396 self.ESC_HELP = '?'
393 397 self.ESC_MAGIC = '%'
394 398 self.ESC_QUOTE = ','
395 399 self.ESC_QUOTE2 = ';'
396 400 self.ESC_PAREN = '/'
397 401
398 402 # And their associated handlers
399 403 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
400 404 self.ESC_QUOTE : self.handle_auto,
401 405 self.ESC_QUOTE2 : self.handle_auto,
402 406 self.ESC_MAGIC : self.handle_magic,
403 407 self.ESC_HELP : self.handle_help,
404 408 self.ESC_SHELL : self.handle_shell_escape,
405 409 self.ESC_SH_CAP : self.handle_shell_escape,
406 410 }
407 411
408 412 # class initializations
409 413 Magic.__init__(self,self)
410 414
411 415 # Python source parser/formatter for syntax highlighting
412 416 pyformat = PyColorize.Parser().format
413 417 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
414 418
415 419 # hooks holds pointers used for user-side customizations
416 420 self.hooks = Struct()
417 421
418 422 self.strdispatchers = {}
419 423
420 424 # Set all default hooks, defined in the IPython.hooks module.
421 425 hooks = IPython.hooks
422 426 for hook_name in hooks.__all__:
423 427 # default hooks have priority 100, i.e. low; user hooks should have
424 428 # 0-100 priority
425 429 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
426 430 #print "bound hook",hook_name
427 431
428 432 # Flag to mark unconditional exit
429 433 self.exit_now = False
430 434
431 435 self.usage_min = """\
432 436 An enhanced console for Python.
433 437 Some of its features are:
434 438 - Readline support if the readline library is present.
435 439 - Tab completion in the local namespace.
436 440 - Logging of input, see command-line options.
437 441 - System shell escape via ! , eg !ls.
438 442 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
439 443 - Keeps track of locally defined variables via %who, %whos.
440 444 - Show object information with a ? eg ?x or x? (use ?? for more info).
441 445 """
442 446 if usage: self.usage = usage
443 447 else: self.usage = self.usage_min
444 448
445 449 # Storage
446 450 self.rc = rc # This will hold all configuration information
447 451 self.pager = 'less'
448 452 # temporary files used for various purposes. Deleted at exit.
449 453 self.tempfiles = []
450 454
451 455 # Keep track of readline usage (later set by init_readline)
452 456 self.has_readline = False
453 457
454 458 # template for logfile headers. It gets resolved at runtime by the
455 459 # logstart method.
456 460 self.loghead_tpl = \
457 461 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
458 462 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
459 463 #log# opts = %s
460 464 #log# args = %s
461 465 #log# It is safe to make manual edits below here.
462 466 #log#-----------------------------------------------------------------------
463 467 """
464 468 # for pushd/popd management
465 469 try:
466 470 self.home_dir = get_home_dir()
467 471 except HomeDirError,msg:
468 472 fatal(msg)
469 473
470 474 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
471 475
472 476 # Functions to call the underlying shell.
473 477
474 478 # The first is similar to os.system, but it doesn't return a value,
475 479 # and it allows interpolation of variables in the user's namespace.
476 480 self.system = lambda cmd: \
477 481 shell(self.var_expand(cmd,depth=2),
478 482 header=self.rc.system_header,
479 483 verbose=self.rc.system_verbose)
480 484
481 485 # These are for getoutput and getoutputerror:
482 486 self.getoutput = lambda cmd: \
483 487 getoutput(self.var_expand(cmd,depth=2),
484 488 header=self.rc.system_header,
485 489 verbose=self.rc.system_verbose)
486 490
487 491 self.getoutputerror = lambda cmd: \
488 492 getoutputerror(self.var_expand(cmd,depth=2),
489 493 header=self.rc.system_header,
490 494 verbose=self.rc.system_verbose)
491 495
492 496
493 497 # keep track of where we started running (mainly for crash post-mortem)
494 498 self.starting_dir = os.getcwd()
495 499
496 500 # Various switches which can be set
497 501 self.CACHELENGTH = 5000 # this is cheap, it's just text
498 502 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
499 503 self.banner2 = banner2
500 504
501 505 # TraceBack handlers:
502 506
503 507 # Syntax error handler.
504 508 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
505 509
506 510 # The interactive one is initialized with an offset, meaning we always
507 511 # want to remove the topmost item in the traceback, which is our own
508 512 # internal code. Valid modes: ['Plain','Context','Verbose']
509 513 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
510 514 color_scheme='NoColor',
511 515 tb_offset = 1)
512 516
513 517 # IPython itself shouldn't crash. This will produce a detailed
514 518 # post-mortem if it does. But we only install the crash handler for
515 519 # non-threaded shells, the threaded ones use a normal verbose reporter
516 520 # and lose the crash handler. This is because exceptions in the main
517 521 # thread (such as in GUI code) propagate directly to sys.excepthook,
518 522 # and there's no point in printing crash dumps for every user exception.
519 523 if self.isthreaded:
520 524 ipCrashHandler = ultraTB.FormattedTB()
521 525 else:
522 526 from IPython import CrashHandler
523 527 ipCrashHandler = CrashHandler.IPythonCrashHandler(self)
524 528 self.set_crash_handler(ipCrashHandler)
525 529
526 530 # and add any custom exception handlers the user may have specified
527 531 self.set_custom_exc(*custom_exceptions)
528 532
529 533 # indentation management
530 534 self.autoindent = False
531 535 self.indent_current_nsp = 0
532 536
533 537 # Make some aliases automatically
534 538 # Prepare list of shell aliases to auto-define
535 539 if os.name == 'posix':
536 540 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
537 541 'mv mv -i','rm rm -i','cp cp -i',
538 542 'cat cat','less less','clear clear',
539 543 # a better ls
540 544 'ls ls -F',
541 545 # long ls
542 546 'll ls -lF')
543 547 # Extra ls aliases with color, which need special treatment on BSD
544 548 # variants
545 549 ls_extra = ( # color ls
546 550 'lc ls -F -o --color',
547 551 # ls normal files only
548 552 'lf ls -F -o --color %l | grep ^-',
549 553 # ls symbolic links
550 554 'lk ls -F -o --color %l | grep ^l',
551 555 # directories or links to directories,
552 556 'ldir ls -F -o --color %l | grep /$',
553 557 # things which are executable
554 558 'lx ls -F -o --color %l | grep ^-..x',
555 559 )
556 560 # The BSDs don't ship GNU ls, so they don't understand the
557 561 # --color switch out of the box
558 562 if 'bsd' in sys.platform:
559 563 ls_extra = ( # ls normal files only
560 564 'lf ls -lF | grep ^-',
561 565 # ls symbolic links
562 566 'lk ls -lF | grep ^l',
563 567 # directories or links to directories,
564 568 'ldir ls -lF | grep /$',
565 569 # things which are executable
566 570 'lx ls -lF | grep ^-..x',
567 571 )
568 572 auto_alias = auto_alias + ls_extra
569 573 elif os.name in ['nt','dos']:
570 574 auto_alias = ('dir dir /on', 'ls dir /on',
571 575 'ddir dir /ad /on', 'ldir dir /ad /on',
572 576 'mkdir mkdir','rmdir rmdir','echo echo',
573 577 'ren ren','cls cls','copy copy')
574 578 else:
575 579 auto_alias = ()
576 580 self.auto_alias = [s.split(None,1) for s in auto_alias]
577 581 # Call the actual (public) initializer
578 582 self.init_auto_alias()
579 583
580 584 # Produce a public API instance
581 585 self.api = IPython.ipapi.IPApi(self)
582 586
583 587 # track which builtins we add, so we can clean up later
584 588 self.builtins_added = {}
585 589 # This method will add the necessary builtins for operation, but
586 590 # tracking what it did via the builtins_added dict.
587 591 self.add_builtins()
588 592
589 593 # end __init__
590 594
591 595 def var_expand(self,cmd,depth=0):
592 596 """Expand python variables in a string.
593 597
594 598 The depth argument indicates how many frames above the caller should
595 599 be walked to look for the local namespace where to expand variables.
596 600
597 601 The global namespace for expansion is always the user's interactive
598 602 namespace.
599 603 """
600 604
601 605 return str(ItplNS(cmd.replace('#','\#'),
602 606 self.user_ns, # globals
603 607 # Skip our own frame in searching for locals:
604 608 sys._getframe(depth+1).f_locals # locals
605 609 ))
606 610
607 611 def pre_config_initialization(self):
608 612 """Pre-configuration init method
609 613
610 614 This is called before the configuration files are processed to
611 615 prepare the services the config files might need.
612 616
613 617 self.rc already has reasonable default values at this point.
614 618 """
615 619 rc = self.rc
616 620 try:
617 621 self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db")
618 622 except exceptions.UnicodeDecodeError:
619 623 print "Your ipythondir can't be decoded to unicode!"
620 624 print "Please set HOME environment variable to something that"
621 625 print r"only has ASCII characters, e.g. c:\home"
622 626 print "Now it is",rc.ipythondir
623 627 sys.exit()
624 628 self.shadowhist = IPython.history.ShadowHist(self.db)
625 629
626 630
627 631 def post_config_initialization(self):
628 632 """Post configuration init method
629 633
630 634 This is called after the configuration files have been processed to
631 635 'finalize' the initialization."""
632 636
633 637 rc = self.rc
634 638
635 639 # Object inspector
636 640 self.inspector = OInspect.Inspector(OInspect.InspectColors,
637 641 PyColorize.ANSICodeColors,
638 642 'NoColor',
639 643 rc.object_info_string_level)
640 644
641 645 self.rl_next_input = None
642 646 self.rl_do_indent = False
643 647 # Load readline proper
644 648 if rc.readline:
645 649 self.init_readline()
646 650
647 651
648 652 # local shortcut, this is used a LOT
649 653 self.log = self.logger.log
650 654
651 655 # Initialize cache, set in/out prompts and printing system
652 656 self.outputcache = CachedOutput(self,
653 657 rc.cache_size,
654 658 rc.pprint,
655 659 input_sep = rc.separate_in,
656 660 output_sep = rc.separate_out,
657 661 output_sep2 = rc.separate_out2,
658 662 ps1 = rc.prompt_in1,
659 663 ps2 = rc.prompt_in2,
660 664 ps_out = rc.prompt_out,
661 665 pad_left = rc.prompts_pad_left)
662 666
663 667 # user may have over-ridden the default print hook:
664 668 try:
665 669 self.outputcache.__class__.display = self.hooks.display
666 670 except AttributeError:
667 671 pass
668 672
669 673 # I don't like assigning globally to sys, because it means when
670 674 # embedding instances, each embedded instance overrides the previous
671 675 # choice. But sys.displayhook seems to be called internally by exec,
672 676 # so I don't see a way around it. We first save the original and then
673 677 # overwrite it.
674 678 self.sys_displayhook = sys.displayhook
675 679 sys.displayhook = self.outputcache
676 680
677 681 # Set user colors (don't do it in the constructor above so that it
678 682 # doesn't crash if colors option is invalid)
679 683 self.magic_colors(rc.colors)
680 684
681 685 # Set calling of pdb on exceptions
682 686 self.call_pdb = rc.pdb
683 687
684 688 # Load user aliases
685 689 for alias in rc.alias:
686 690 self.magic_alias(alias)
687 691 self.hooks.late_startup_hook()
688 692
689 693 batchrun = False
690 694 for batchfile in [path(arg) for arg in self.rc.args
691 695 if arg.lower().endswith('.ipy')]:
692 696 if not batchfile.isfile():
693 697 print "No such batch file:", batchfile
694 698 continue
695 699 self.api.runlines(batchfile.text())
696 700 batchrun = True
697 701 if batchrun:
698 702 self.exit_now = True
699 703
700 704 def add_builtins(self):
701 705 """Store ipython references into the builtin namespace.
702 706
703 707 Some parts of ipython operate via builtins injected here, which hold a
704 708 reference to IPython itself."""
705 709
706 710 # TODO: deprecate all except _ip; 'jobs' should be installed
707 711 # by an extension and the rest are under _ip, ipalias is redundant
708 712 builtins_new = dict(__IPYTHON__ = self,
709 713 ip_set_hook = self.set_hook,
710 714 jobs = self.jobs,
711 715 ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'),
712 716 ipalias = wrap_deprecated(self.ipalias),
713 717 ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'),
714 718 _ip = self.api
715 719 )
716 720 for biname,bival in builtins_new.items():
717 721 try:
718 722 # store the orignal value so we can restore it
719 723 self.builtins_added[biname] = __builtin__.__dict__[biname]
720 724 except KeyError:
721 725 # or mark that it wasn't defined, and we'll just delete it at
722 726 # cleanup
723 727 self.builtins_added[biname] = Undefined
724 728 __builtin__.__dict__[biname] = bival
725 729
726 730 # Keep in the builtins a flag for when IPython is active. We set it
727 731 # with setdefault so that multiple nested IPythons don't clobber one
728 732 # another. Each will increase its value by one upon being activated,
729 733 # which also gives us a way to determine the nesting level.
730 734 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
731 735
732 736 def clean_builtins(self):
733 737 """Remove any builtins which might have been added by add_builtins, or
734 738 restore overwritten ones to their previous values."""
735 739 for biname,bival in self.builtins_added.items():
736 740 if bival is Undefined:
737 741 del __builtin__.__dict__[biname]
738 742 else:
739 743 __builtin__.__dict__[biname] = bival
740 744 self.builtins_added.clear()
741 745
742 746 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
743 747 """set_hook(name,hook) -> sets an internal IPython hook.
744 748
745 749 IPython exposes some of its internal API as user-modifiable hooks. By
746 750 adding your function to one of these hooks, you can modify IPython's
747 751 behavior to call at runtime your own routines."""
748 752
749 753 # At some point in the future, this should validate the hook before it
750 754 # accepts it. Probably at least check that the hook takes the number
751 755 # of args it's supposed to.
752 756
753 757 f = new.instancemethod(hook,self,self.__class__)
754 758
755 759 # check if the hook is for strdispatcher first
756 760 if str_key is not None:
757 761 sdp = self.strdispatchers.get(name, StrDispatch())
758 762 sdp.add_s(str_key, f, priority )
759 763 self.strdispatchers[name] = sdp
760 764 return
761 765 if re_key is not None:
762 766 sdp = self.strdispatchers.get(name, StrDispatch())
763 767 sdp.add_re(re.compile(re_key), f, priority )
764 768 self.strdispatchers[name] = sdp
765 769 return
766 770
767 771 dp = getattr(self.hooks, name, None)
768 772 if name not in IPython.hooks.__all__:
769 773 print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ )
770 774 if not dp:
771 775 dp = IPython.hooks.CommandChainDispatcher()
772 776
773 777 try:
774 778 dp.add(f,priority)
775 779 except AttributeError:
776 780 # it was not commandchain, plain old func - replace
777 781 dp = f
778 782
779 783 setattr(self.hooks,name, dp)
780 784
781 785
782 786 #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
783 787
784 788 def set_crash_handler(self,crashHandler):
785 789 """Set the IPython crash handler.
786 790
787 791 This must be a callable with a signature suitable for use as
788 792 sys.excepthook."""
789 793
790 794 # Install the given crash handler as the Python exception hook
791 795 sys.excepthook = crashHandler
792 796
793 797 # The instance will store a pointer to this, so that runtime code
794 798 # (such as magics) can access it. This is because during the
795 799 # read-eval loop, it gets temporarily overwritten (to deal with GUI
796 800 # frameworks).
797 801 self.sys_excepthook = sys.excepthook
798 802
799 803
800 804 def set_custom_exc(self,exc_tuple,handler):
801 805 """set_custom_exc(exc_tuple,handler)
802 806
803 807 Set a custom exception handler, which will be called if any of the
804 808 exceptions in exc_tuple occur in the mainloop (specifically, in the
805 809 runcode() method.
806 810
807 811 Inputs:
808 812
809 813 - exc_tuple: a *tuple* of valid exceptions to call the defined
810 814 handler for. It is very important that you use a tuple, and NOT A
811 815 LIST here, because of the way Python's except statement works. If
812 816 you only want to trap a single exception, use a singleton tuple:
813 817
814 818 exc_tuple == (MyCustomException,)
815 819
816 820 - handler: this must be defined as a function with the following
817 821 basic interface: def my_handler(self,etype,value,tb).
818 822
819 823 This will be made into an instance method (via new.instancemethod)
820 824 of IPython itself, and it will be called if any of the exceptions
821 825 listed in the exc_tuple are caught. If the handler is None, an
822 826 internal basic one is used, which just prints basic info.
823 827
824 828 WARNING: by putting in your own exception handler into IPython's main
825 829 execution loop, you run a very good chance of nasty crashes. This
826 830 facility should only be used if you really know what you are doing."""
827 831
828 832 assert type(exc_tuple)==type(()) , \
829 833 "The custom exceptions must be given AS A TUPLE."
830 834
831 835 def dummy_handler(self,etype,value,tb):
832 836 print '*** Simple custom exception handler ***'
833 837 print 'Exception type :',etype
834 838 print 'Exception value:',value
835 839 print 'Traceback :',tb
836 840 print 'Source code :','\n'.join(self.buffer)
837 841
838 842 if handler is None: handler = dummy_handler
839 843
840 844 self.CustomTB = new.instancemethod(handler,self,self.__class__)
841 845 self.custom_exceptions = exc_tuple
842 846
843 847 def set_custom_completer(self,completer,pos=0):
844 848 """set_custom_completer(completer,pos=0)
845 849
846 850 Adds a new custom completer function.
847 851
848 852 The position argument (defaults to 0) is the index in the completers
849 853 list where you want the completer to be inserted."""
850 854
851 855 newcomp = new.instancemethod(completer,self.Completer,
852 856 self.Completer.__class__)
853 857 self.Completer.matchers.insert(pos,newcomp)
854 858
855 859 def set_completer(self):
856 860 """reset readline's completer to be our own."""
857 861 self.readline.set_completer(self.Completer.complete)
858 862
859 863 def _get_call_pdb(self):
860 864 return self._call_pdb
861 865
862 866 def _set_call_pdb(self,val):
863 867
864 868 if val not in (0,1,False,True):
865 869 raise ValueError,'new call_pdb value must be boolean'
866 870
867 871 # store value in instance
868 872 self._call_pdb = val
869 873
870 874 # notify the actual exception handlers
871 875 self.InteractiveTB.call_pdb = val
872 876 if self.isthreaded:
873 877 try:
874 878 self.sys_excepthook.call_pdb = val
875 879 except:
876 880 warn('Failed to activate pdb for threaded exception handler')
877 881
878 882 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
879 883 'Control auto-activation of pdb at exceptions')
880 884
881 885
882 886 # These special functions get installed in the builtin namespace, to
883 887 # provide programmatic (pure python) access to magics, aliases and system
884 888 # calls. This is important for logging, user scripting, and more.
885 889
886 890 # We are basically exposing, via normal python functions, the three
887 891 # mechanisms in which ipython offers special call modes (magics for
888 892 # internal control, aliases for direct system access via pre-selected
889 893 # names, and !cmd for calling arbitrary system commands).
890 894
891 895 def ipmagic(self,arg_s):
892 896 """Call a magic function by name.
893 897
894 898 Input: a string containing the name of the magic function to call and any
895 899 additional arguments to be passed to the magic.
896 900
897 901 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
898 902 prompt:
899 903
900 904 In[1]: %name -opt foo bar
901 905
902 906 To call a magic without arguments, simply use ipmagic('name').
903 907
904 908 This provides a proper Python function to call IPython's magics in any
905 909 valid Python code you can type at the interpreter, including loops and
906 910 compound statements. It is added by IPython to the Python builtin
907 911 namespace upon initialization."""
908 912
909 913 args = arg_s.split(' ',1)
910 914 magic_name = args[0]
911 915 magic_name = magic_name.lstrip(self.ESC_MAGIC)
912 916
913 917 try:
914 918 magic_args = args[1]
915 919 except IndexError:
916 920 magic_args = ''
917 921 fn = getattr(self,'magic_'+magic_name,None)
918 922 if fn is None:
919 923 error("Magic function `%s` not found." % magic_name)
920 924 else:
921 925 magic_args = self.var_expand(magic_args,1)
922 926 return fn(magic_args)
923 927
924 928 def ipalias(self,arg_s):
925 929 """Call an alias by name.
926 930
927 931 Input: a string containing the name of the alias to call and any
928 932 additional arguments to be passed to the magic.
929 933
930 934 ipalias('name -opt foo bar') is equivalent to typing at the ipython
931 935 prompt:
932 936
933 937 In[1]: name -opt foo bar
934 938
935 939 To call an alias without arguments, simply use ipalias('name').
936 940
937 941 This provides a proper Python function to call IPython's aliases in any
938 942 valid Python code you can type at the interpreter, including loops and
939 943 compound statements. It is added by IPython to the Python builtin
940 944 namespace upon initialization."""
941 945
942 946 args = arg_s.split(' ',1)
943 947 alias_name = args[0]
944 948 try:
945 949 alias_args = args[1]
946 950 except IndexError:
947 951 alias_args = ''
948 952 if alias_name in self.alias_table:
949 953 self.call_alias(alias_name,alias_args)
950 954 else:
951 955 error("Alias `%s` not found." % alias_name)
952 956
953 957 def ipsystem(self,arg_s):
954 958 """Make a system call, using IPython."""
955 959
956 960 self.system(arg_s)
957 961
958 962 def complete(self,text):
959 963 """Return a sorted list of all possible completions on text.
960 964
961 965 Inputs:
962 966
963 967 - text: a string of text to be completed on.
964 968
965 969 This is a wrapper around the completion mechanism, similar to what
966 970 readline does at the command line when the TAB key is hit. By
967 971 exposing it as a method, it can be used by other non-readline
968 972 environments (such as GUIs) for text completion.
969 973
970 974 Simple usage example:
971 975
972 976 In [1]: x = 'hello'
973 977
974 978 In [2]: __IP.complete('x.l')
975 979 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
976 980
977 981 complete = self.Completer.complete
978 982 state = 0
979 983 # use a dict so we get unique keys, since ipyhton's multiple
980 984 # completers can return duplicates. When we make 2.4 a requirement,
981 985 # start using sets instead, which are faster.
982 986 comps = {}
983 987 while True:
984 988 newcomp = complete(text,state,line_buffer=text)
985 989 if newcomp is None:
986 990 break
987 991 comps[newcomp] = 1
988 992 state += 1
989 993 outcomps = comps.keys()
990 994 outcomps.sort()
991 995 return outcomps
992 996
993 997 def set_completer_frame(self, frame=None):
994 998 if frame:
995 999 self.Completer.namespace = frame.f_locals
996 1000 self.Completer.global_namespace = frame.f_globals
997 1001 else:
998 1002 self.Completer.namespace = self.user_ns
999 1003 self.Completer.global_namespace = self.user_global_ns
1000 1004
1001 1005 def init_auto_alias(self):
1002 1006 """Define some aliases automatically.
1003 1007
1004 1008 These are ALL parameter-less aliases"""
1005 1009
1006 1010 for alias,cmd in self.auto_alias:
1007 1011 self.alias_table[alias] = (0,cmd)
1008 1012
1009 1013 def alias_table_validate(self,verbose=0):
1010 1014 """Update information about the alias table.
1011 1015
1012 1016 In particular, make sure no Python keywords/builtins are in it."""
1013 1017
1014 1018 no_alias = self.no_alias
1015 1019 for k in self.alias_table.keys():
1016 1020 if k in no_alias:
1017 1021 del self.alias_table[k]
1018 1022 if verbose:
1019 1023 print ("Deleting alias <%s>, it's a Python "
1020 1024 "keyword or builtin." % k)
1021 1025
1022 1026 def set_autoindent(self,value=None):
1023 1027 """Set the autoindent flag, checking for readline support.
1024 1028
1025 1029 If called with no arguments, it acts as a toggle."""
1026 1030
1027 1031 if not self.has_readline:
1028 1032 if os.name == 'posix':
1029 1033 warn("The auto-indent feature requires the readline library")
1030 1034 self.autoindent = 0
1031 1035 return
1032 1036 if value is None:
1033 1037 self.autoindent = not self.autoindent
1034 1038 else:
1035 1039 self.autoindent = value
1036 1040
1037 1041 def rc_set_toggle(self,rc_field,value=None):
1038 1042 """Set or toggle a field in IPython's rc config. structure.
1039 1043
1040 1044 If called with no arguments, it acts as a toggle.
1041 1045
1042 1046 If called with a non-existent field, the resulting AttributeError
1043 1047 exception will propagate out."""
1044 1048
1045 1049 rc_val = getattr(self.rc,rc_field)
1046 1050 if value is None:
1047 1051 value = not rc_val
1048 1052 setattr(self.rc,rc_field,value)
1049 1053
1050 1054 def user_setup(self,ipythondir,rc_suffix,mode='install'):
1051 1055 """Install the user configuration directory.
1052 1056
1053 1057 Can be called when running for the first time or to upgrade the user's
1054 1058 .ipython/ directory with the mode parameter. Valid modes are 'install'
1055 1059 and 'upgrade'."""
1056 1060
1057 1061 def wait():
1058 1062 try:
1059 1063 raw_input("Please press <RETURN> to start IPython.")
1060 1064 except EOFError:
1061 1065 print >> Term.cout
1062 1066 print '*'*70
1063 1067
1064 1068 cwd = os.getcwd() # remember where we started
1065 1069 glb = glob.glob
1066 1070 print '*'*70
1067 1071 if mode == 'install':
1068 1072 print \
1069 1073 """Welcome to IPython. I will try to create a personal configuration directory
1070 1074 where you can customize many aspects of IPython's functionality in:\n"""
1071 1075 else:
1072 1076 print 'I am going to upgrade your configuration in:'
1073 1077
1074 1078 print ipythondir
1075 1079
1076 1080 rcdirend = os.path.join('IPython','UserConfig')
1077 1081 cfg = lambda d: os.path.join(d,rcdirend)
1078 1082 try:
1079 1083 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1080 1084 except IOError:
1081 1085 warning = """
1082 1086 Installation error. IPython's directory was not found.
1083 1087
1084 1088 Check the following:
1085 1089
1086 1090 The ipython/IPython directory should be in a directory belonging to your
1087 1091 PYTHONPATH environment variable (that is, it should be in a directory
1088 1092 belonging to sys.path). You can copy it explicitly there or just link to it.
1089 1093
1090 1094 IPython will proceed with builtin defaults.
1091 1095 """
1092 1096 warn(warning)
1093 1097 wait()
1094 1098 return
1095 1099
1096 1100 if mode == 'install':
1097 1101 try:
1098 1102 shutil.copytree(rcdir,ipythondir)
1099 1103 os.chdir(ipythondir)
1100 1104 rc_files = glb("ipythonrc*")
1101 1105 for rc_file in rc_files:
1102 1106 os.rename(rc_file,rc_file+rc_suffix)
1103 1107 except:
1104 1108 warning = """
1105 1109
1106 1110 There was a problem with the installation:
1107 1111 %s
1108 1112 Try to correct it or contact the developers if you think it's a bug.
1109 1113 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1110 1114 warn(warning)
1111 1115 wait()
1112 1116 return
1113 1117
1114 1118 elif mode == 'upgrade':
1115 1119 try:
1116 1120 os.chdir(ipythondir)
1117 1121 except:
1118 1122 print """
1119 1123 Can not upgrade: changing to directory %s failed. Details:
1120 1124 %s
1121 1125 """ % (ipythondir,sys.exc_info()[1])
1122 1126 wait()
1123 1127 return
1124 1128 else:
1125 1129 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1126 1130 for new_full_path in sources:
1127 1131 new_filename = os.path.basename(new_full_path)
1128 1132 if new_filename.startswith('ipythonrc'):
1129 1133 new_filename = new_filename + rc_suffix
1130 1134 # The config directory should only contain files, skip any
1131 1135 # directories which may be there (like CVS)
1132 1136 if os.path.isdir(new_full_path):
1133 1137 continue
1134 1138 if os.path.exists(new_filename):
1135 1139 old_file = new_filename+'.old'
1136 1140 if os.path.exists(old_file):
1137 1141 os.remove(old_file)
1138 1142 os.rename(new_filename,old_file)
1139 1143 shutil.copy(new_full_path,new_filename)
1140 1144 else:
1141 1145 raise ValueError,'unrecognized mode for install:',`mode`
1142 1146
1143 1147 # Fix line-endings to those native to each platform in the config
1144 1148 # directory.
1145 1149 try:
1146 1150 os.chdir(ipythondir)
1147 1151 except:
1148 1152 print """
1149 1153 Problem: changing to directory %s failed.
1150 1154 Details:
1151 1155 %s
1152 1156
1153 1157 Some configuration files may have incorrect line endings. This should not
1154 1158 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1155 1159 wait()
1156 1160 else:
1157 1161 for fname in glb('ipythonrc*'):
1158 1162 try:
1159 1163 native_line_ends(fname,backup=0)
1160 1164 except IOError:
1161 1165 pass
1162 1166
1163 1167 if mode == 'install':
1164 1168 print """
1165 1169 Successful installation!
1166 1170
1167 1171 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1168 1172 IPython manual (there are both HTML and PDF versions supplied with the
1169 1173 distribution) to make sure that your system environment is properly configured
1170 1174 to take advantage of IPython's features.
1171 1175
1172 1176 Important note: the configuration system has changed! The old system is
1173 1177 still in place, but its setting may be partly overridden by the settings in
1174 1178 "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
1175 1179 if some of the new settings bother you.
1176 1180
1177 1181 """
1178 1182 else:
1179 1183 print """
1180 1184 Successful upgrade!
1181 1185
1182 1186 All files in your directory:
1183 1187 %(ipythondir)s
1184 1188 which would have been overwritten by the upgrade were backed up with a .old
1185 1189 extension. If you had made particular customizations in those files you may
1186 1190 want to merge them back into the new files.""" % locals()
1187 1191 wait()
1188 1192 os.chdir(cwd)
1189 1193 # end user_setup()
1190 1194
1191 1195 def atexit_operations(self):
1192 1196 """This will be executed at the time of exit.
1193 1197
1194 1198 Saving of persistent data should be performed here. """
1195 1199
1196 1200 #print '*** IPython exit cleanup ***' # dbg
1197 1201 # input history
1198 1202 self.savehist()
1199 1203
1200 1204 # Cleanup all tempfiles left around
1201 1205 for tfile in self.tempfiles:
1202 1206 try:
1203 1207 os.unlink(tfile)
1204 1208 except OSError:
1205 1209 pass
1206 1210
1207 1211 self.hooks.shutdown_hook()
1208 1212
1209 1213 def savehist(self):
1210 1214 """Save input history to a file (via readline library)."""
1211 1215 try:
1212 1216 self.readline.write_history_file(self.histfile)
1213 1217 except:
1214 1218 print 'Unable to save IPython command history to file: ' + \
1215 1219 `self.histfile`
1216 1220
1217 1221 def reloadhist(self):
1218 1222 """Reload the input history from disk file."""
1219 1223
1220 1224 if self.has_readline:
1221 1225 self.readline.clear_history()
1222 1226 self.readline.read_history_file(self.shell.histfile)
1223 1227
1224 1228 def history_saving_wrapper(self, func):
1225 1229 """ Wrap func for readline history saving
1226 1230
1227 1231 Convert func into callable that saves & restores
1228 1232 history around the call """
1229 1233
1230 1234 if not self.has_readline:
1231 1235 return func
1232 1236
1233 1237 def wrapper():
1234 1238 self.savehist()
1235 1239 try:
1236 1240 func()
1237 1241 finally:
1238 1242 readline.read_history_file(self.histfile)
1239 1243 return wrapper
1240 1244
1241 1245
1242 1246 def pre_readline(self):
1243 1247 """readline hook to be used at the start of each line.
1244 1248
1245 1249 Currently it handles auto-indent only."""
1246 1250
1247 1251 #debugx('self.indent_current_nsp','pre_readline:')
1248 1252
1249 1253 if self.rl_do_indent:
1250 1254 self.readline.insert_text(self.indent_current_str())
1251 1255 if self.rl_next_input is not None:
1252 1256 self.readline.insert_text(self.rl_next_input)
1253 1257 self.rl_next_input = None
1254 1258
1255 1259 def init_readline(self):
1256 1260 """Command history completion/saving/reloading."""
1257 1261
1258 1262 import IPython.rlineimpl as readline
1259 1263 if not readline.have_readline:
1260 1264 self.has_readline = 0
1261 1265 self.readline = None
1262 1266 # no point in bugging windows users with this every time:
1263 1267 warn('Readline services not available on this platform.')
1264 1268 else:
1265 1269 sys.modules['readline'] = readline
1266 1270 import atexit
1267 1271 from IPython.completer import IPCompleter
1268 1272 self.Completer = IPCompleter(self,
1269 1273 self.user_ns,
1270 1274 self.user_global_ns,
1271 1275 self.rc.readline_omit__names,
1272 1276 self.alias_table)
1273 1277 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1274 1278 self.strdispatchers['complete_command'] = sdisp
1275 1279 self.Completer.custom_completers = sdisp
1276 1280 # Platform-specific configuration
1277 1281 if os.name == 'nt':
1278 1282 self.readline_startup_hook = readline.set_pre_input_hook
1279 1283 else:
1280 1284 self.readline_startup_hook = readline.set_startup_hook
1281 1285
1282 1286 # Load user's initrc file (readline config)
1283 1287 inputrc_name = os.environ.get('INPUTRC')
1284 1288 if inputrc_name is None:
1285 1289 home_dir = get_home_dir()
1286 1290 if home_dir is not None:
1287 1291 inputrc_name = os.path.join(home_dir,'.inputrc')
1288 1292 if os.path.isfile(inputrc_name):
1289 1293 try:
1290 1294 readline.read_init_file(inputrc_name)
1291 1295 except:
1292 1296 warn('Problems reading readline initialization file <%s>'
1293 1297 % inputrc_name)
1294 1298
1295 1299 self.has_readline = 1
1296 1300 self.readline = readline
1297 1301 # save this in sys so embedded copies can restore it properly
1298 1302 sys.ipcompleter = self.Completer.complete
1299 1303 self.set_completer()
1300 1304
1301 1305 # Configure readline according to user's prefs
1302 1306 for rlcommand in self.rc.readline_parse_and_bind:
1303 1307 readline.parse_and_bind(rlcommand)
1304 1308
1305 1309 # remove some chars from the delimiters list
1306 1310 delims = readline.get_completer_delims()
1307 1311 delims = delims.translate(string._idmap,
1308 1312 self.rc.readline_remove_delims)
1309 1313 readline.set_completer_delims(delims)
1310 1314 # otherwise we end up with a monster history after a while:
1311 1315 readline.set_history_length(1000)
1312 1316 try:
1313 1317 #print '*** Reading readline history' # dbg
1314 1318 readline.read_history_file(self.histfile)
1315 1319 except IOError:
1316 1320 pass # It doesn't exist yet.
1317 1321
1318 1322 atexit.register(self.atexit_operations)
1319 1323 del atexit
1320 1324
1321 1325 # Configure auto-indent for all platforms
1322 1326 self.set_autoindent(self.rc.autoindent)
1323 1327
1324 1328 def ask_yes_no(self,prompt,default=True):
1325 1329 if self.rc.quiet:
1326 1330 return True
1327 1331 return ask_yes_no(prompt,default)
1328 1332
1329 1333 def _should_recompile(self,e):
1330 1334 """Utility routine for edit_syntax_error"""
1331 1335
1332 1336 if e.filename in ('<ipython console>','<input>','<string>',
1333 1337 '<console>','<BackgroundJob compilation>',
1334 1338 None):
1335 1339
1336 1340 return False
1337 1341 try:
1338 1342 if (self.rc.autoedit_syntax and
1339 1343 not self.ask_yes_no('Return to editor to correct syntax error? '
1340 1344 '[Y/n] ','y')):
1341 1345 return False
1342 1346 except EOFError:
1343 1347 return False
1344 1348
1345 1349 def int0(x):
1346 1350 try:
1347 1351 return int(x)
1348 1352 except TypeError:
1349 1353 return 0
1350 1354 # always pass integer line and offset values to editor hook
1351 1355 self.hooks.fix_error_editor(e.filename,
1352 1356 int0(e.lineno),int0(e.offset),e.msg)
1353 1357 return True
1354 1358
1355 1359 def edit_syntax_error(self):
1356 1360 """The bottom half of the syntax error handler called in the main loop.
1357 1361
1358 1362 Loop until syntax error is fixed or user cancels.
1359 1363 """
1360 1364
1361 1365 while self.SyntaxTB.last_syntax_error:
1362 1366 # copy and clear last_syntax_error
1363 1367 err = self.SyntaxTB.clear_err_state()
1364 1368 if not self._should_recompile(err):
1365 1369 return
1366 1370 try:
1367 1371 # may set last_syntax_error again if a SyntaxError is raised
1368 1372 self.safe_execfile(err.filename,self.user_ns)
1369 1373 except:
1370 1374 self.showtraceback()
1371 1375 else:
1372 1376 try:
1373 1377 f = file(err.filename)
1374 1378 try:
1375 1379 sys.displayhook(f.read())
1376 1380 finally:
1377 1381 f.close()
1378 1382 except:
1379 1383 self.showtraceback()
1380 1384
1381 1385 def showsyntaxerror(self, filename=None):
1382 1386 """Display the syntax error that just occurred.
1383 1387
1384 1388 This doesn't display a stack trace because there isn't one.
1385 1389
1386 1390 If a filename is given, it is stuffed in the exception instead
1387 1391 of what was there before (because Python's parser always uses
1388 1392 "<string>" when reading from a string).
1389 1393 """
1390 1394 etype, value, last_traceback = sys.exc_info()
1391 1395
1392 1396 # See note about these variables in showtraceback() below
1393 1397 sys.last_type = etype
1394 1398 sys.last_value = value
1395 1399 sys.last_traceback = last_traceback
1396 1400
1397 1401 if filename and etype is SyntaxError:
1398 1402 # Work hard to stuff the correct filename in the exception
1399 1403 try:
1400 1404 msg, (dummy_filename, lineno, offset, line) = value
1401 1405 except:
1402 1406 # Not the format we expect; leave it alone
1403 1407 pass
1404 1408 else:
1405 1409 # Stuff in the right filename
1406 1410 try:
1407 1411 # Assume SyntaxError is a class exception
1408 1412 value = SyntaxError(msg, (filename, lineno, offset, line))
1409 1413 except:
1410 1414 # If that failed, assume SyntaxError is a string
1411 1415 value = msg, (filename, lineno, offset, line)
1412 1416 self.SyntaxTB(etype,value,[])
1413 1417
1414 1418 def debugger(self,force=False):
1415 1419 """Call the pydb/pdb debugger.
1416 1420
1417 1421 Keywords:
1418 1422
1419 1423 - force(False): by default, this routine checks the instance call_pdb
1420 1424 flag and does not actually invoke the debugger if the flag is false.
1421 1425 The 'force' option forces the debugger to activate even if the flag
1422 1426 is false.
1423 1427 """
1424 1428
1425 1429 if not (force or self.call_pdb):
1426 1430 return
1427 1431
1428 1432 if not hasattr(sys,'last_traceback'):
1429 1433 error('No traceback has been produced, nothing to debug.')
1430 1434 return
1431 1435
1432 1436 # use pydb if available
1433 1437 if Debugger.has_pydb:
1434 1438 from pydb import pm
1435 1439 else:
1436 1440 # fallback to our internal debugger
1437 1441 pm = lambda : self.InteractiveTB.debugger(force=True)
1438 1442 self.history_saving_wrapper(pm)()
1439 1443
1440 1444 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1441 1445 """Display the exception that just occurred.
1442 1446
1443 1447 If nothing is known about the exception, this is the method which
1444 1448 should be used throughout the code for presenting user tracebacks,
1445 1449 rather than directly invoking the InteractiveTB object.
1446 1450
1447 1451 A specific showsyntaxerror() also exists, but this method can take
1448 1452 care of calling it if needed, so unless you are explicitly catching a
1449 1453 SyntaxError exception, don't try to analyze the stack manually and
1450 1454 simply call this method."""
1451 1455
1452 1456
1453 1457 # Though this won't be called by syntax errors in the input line,
1454 1458 # there may be SyntaxError cases whith imported code.
1455 1459
1456 1460
1457 1461 if exc_tuple is None:
1458 1462 etype, value, tb = sys.exc_info()
1459 1463 else:
1460 1464 etype, value, tb = exc_tuple
1461 1465
1462 1466 if etype is SyntaxError:
1463 1467 self.showsyntaxerror(filename)
1464 1468 else:
1465 1469 # WARNING: these variables are somewhat deprecated and not
1466 1470 # necessarily safe to use in a threaded environment, but tools
1467 1471 # like pdb depend on their existence, so let's set them. If we
1468 1472 # find problems in the field, we'll need to revisit their use.
1469 1473 sys.last_type = etype
1470 1474 sys.last_value = value
1471 1475 sys.last_traceback = tb
1472 1476
1473 1477 if etype in self.custom_exceptions:
1474 1478 self.CustomTB(etype,value,tb)
1475 1479 else:
1476 1480 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1477 1481 if self.InteractiveTB.call_pdb and self.has_readline:
1478 1482 # pdb mucks up readline, fix it back
1479 1483 self.set_completer()
1480 1484
1481 1485
1482 1486 def mainloop(self,banner=None):
1483 1487 """Creates the local namespace and starts the mainloop.
1484 1488
1485 1489 If an optional banner argument is given, it will override the
1486 1490 internally created default banner."""
1487 1491
1488 1492 if self.rc.c: # Emulate Python's -c option
1489 1493 self.exec_init_cmd()
1490 1494 if banner is None:
1491 1495 if not self.rc.banner:
1492 1496 banner = ''
1493 1497 # banner is string? Use it directly!
1494 1498 elif isinstance(self.rc.banner,basestring):
1495 1499 banner = self.rc.banner
1496 1500 else:
1497 1501 banner = self.BANNER+self.banner2
1498 1502
1499 1503 self.interact(banner)
1500 1504
1501 1505 def exec_init_cmd(self):
1502 1506 """Execute a command given at the command line.
1503 1507
1504 1508 This emulates Python's -c option."""
1505 1509
1506 1510 #sys.argv = ['-c']
1507 1511 self.push(self.prefilter(self.rc.c, False))
1508 1512 self.exit_now = True
1509 1513
1510 1514 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1511 1515 """Embeds IPython into a running python program.
1512 1516
1513 1517 Input:
1514 1518
1515 1519 - header: An optional header message can be specified.
1516 1520
1517 1521 - local_ns, global_ns: working namespaces. If given as None, the
1518 1522 IPython-initialized one is updated with __main__.__dict__, so that
1519 1523 program variables become visible but user-specific configuration
1520 1524 remains possible.
1521 1525
1522 1526 - stack_depth: specifies how many levels in the stack to go to
1523 1527 looking for namespaces (when local_ns and global_ns are None). This
1524 1528 allows an intermediate caller to make sure that this function gets
1525 1529 the namespace from the intended level in the stack. By default (0)
1526 1530 it will get its locals and globals from the immediate caller.
1527 1531
1528 1532 Warning: it's possible to use this in a program which is being run by
1529 1533 IPython itself (via %run), but some funny things will happen (a few
1530 1534 globals get overwritten). In the future this will be cleaned up, as
1531 1535 there is no fundamental reason why it can't work perfectly."""
1532 1536
1533 1537 # Get locals and globals from caller
1534 1538 if local_ns is None or global_ns is None:
1535 1539 call_frame = sys._getframe(stack_depth).f_back
1536 1540
1537 1541 if local_ns is None:
1538 1542 local_ns = call_frame.f_locals
1539 1543 if global_ns is None:
1540 1544 global_ns = call_frame.f_globals
1541 1545
1542 1546 # Update namespaces and fire up interpreter
1543 1547
1544 1548 # The global one is easy, we can just throw it in
1545 1549 self.user_global_ns = global_ns
1546 1550
1547 1551 # but the user/local one is tricky: ipython needs it to store internal
1548 1552 # data, but we also need the locals. We'll copy locals in the user
1549 1553 # one, but will track what got copied so we can delete them at exit.
1550 1554 # This is so that a later embedded call doesn't see locals from a
1551 1555 # previous call (which most likely existed in a separate scope).
1552 1556 local_varnames = local_ns.keys()
1553 1557 self.user_ns.update(local_ns)
1554 1558
1555 1559 # Patch for global embedding to make sure that things don't overwrite
1556 1560 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1557 1561 # FIXME. Test this a bit more carefully (the if.. is new)
1558 1562 if local_ns is None and global_ns is None:
1559 1563 self.user_global_ns.update(__main__.__dict__)
1560 1564
1561 1565 # make sure the tab-completer has the correct frame information, so it
1562 1566 # actually completes using the frame's locals/globals
1563 1567 self.set_completer_frame()
1564 1568
1565 1569 # before activating the interactive mode, we need to make sure that
1566 1570 # all names in the builtin namespace needed by ipython point to
1567 1571 # ourselves, and not to other instances.
1568 1572 self.add_builtins()
1569 1573
1570 1574 self.interact(header)
1571 1575
1572 1576 # now, purge out the user namespace from anything we might have added
1573 1577 # from the caller's local namespace
1574 1578 delvar = self.user_ns.pop
1575 1579 for var in local_varnames:
1576 1580 delvar(var,None)
1577 1581 # and clean builtins we may have overridden
1578 1582 self.clean_builtins()
1579 1583
1580 1584 def interact(self, banner=None):
1581 1585 """Closely emulate the interactive Python console.
1582 1586
1583 1587 The optional banner argument specify the banner to print
1584 1588 before the first interaction; by default it prints a banner
1585 1589 similar to the one printed by the real Python interpreter,
1586 1590 followed by the current class name in parentheses (so as not
1587 1591 to confuse this with the real interpreter -- since it's so
1588 1592 close!).
1589 1593
1590 1594 """
1591 1595
1592 1596 if self.exit_now:
1593 1597 # batch run -> do not interact
1594 1598 return
1595 1599 cprt = 'Type "copyright", "credits" or "license" for more information.'
1596 1600 if banner is None:
1597 1601 self.write("Python %s on %s\n%s\n(%s)\n" %
1598 1602 (sys.version, sys.platform, cprt,
1599 1603 self.__class__.__name__))
1600 1604 else:
1601 1605 self.write(banner)
1602 1606
1603 1607 more = 0
1604 1608
1605 1609 # Mark activity in the builtins
1606 1610 __builtin__.__dict__['__IPYTHON__active'] += 1
1607 1611
1608 1612 if readline.have_readline:
1609 1613 self.readline_startup_hook(self.pre_readline)
1610 1614 # exit_now is set by a call to %Exit or %Quit
1611 1615
1612 1616 while not self.exit_now:
1613 1617 if more:
1614 1618 prompt = self.hooks.generate_prompt(True)
1615 1619 if self.autoindent:
1616 1620 self.rl_do_indent = True
1617 1621
1618 1622 else:
1619 1623 prompt = self.hooks.generate_prompt(False)
1620 1624 try:
1621 1625 line = self.raw_input(prompt,more)
1622 1626 if self.exit_now:
1623 1627 # quick exit on sys.std[in|out] close
1624 1628 break
1625 1629 if self.autoindent:
1626 1630 self.rl_do_indent = False
1627 1631
1628 1632 except KeyboardInterrupt:
1629 1633 self.write('\nKeyboardInterrupt\n')
1630 1634 self.resetbuffer()
1631 1635 # keep cache in sync with the prompt counter:
1632 1636 self.outputcache.prompt_count -= 1
1633 1637
1634 1638 if self.autoindent:
1635 1639 self.indent_current_nsp = 0
1636 1640 more = 0
1637 1641 except EOFError:
1638 1642 if self.autoindent:
1639 1643 self.rl_do_indent = False
1640 1644 self.readline_startup_hook(None)
1641 1645 self.write('\n')
1642 1646 self.exit()
1643 1647 except bdb.BdbQuit:
1644 1648 warn('The Python debugger has exited with a BdbQuit exception.\n'
1645 1649 'Because of how pdb handles the stack, it is impossible\n'
1646 1650 'for IPython to properly format this particular exception.\n'
1647 1651 'IPython will resume normal operation.')
1648 1652 except:
1649 1653 # exceptions here are VERY RARE, but they can be triggered
1650 1654 # asynchronously by signal handlers, for example.
1651 1655 self.showtraceback()
1652 1656 else:
1653 1657 more = self.push(line)
1654 1658 if (self.SyntaxTB.last_syntax_error and
1655 1659 self.rc.autoedit_syntax):
1656 1660 self.edit_syntax_error()
1657 1661
1658 1662 # We are off again...
1659 1663 __builtin__.__dict__['__IPYTHON__active'] -= 1
1660 1664
1661 1665 def excepthook(self, etype, value, tb):
1662 1666 """One more defense for GUI apps that call sys.excepthook.
1663 1667
1664 1668 GUI frameworks like wxPython trap exceptions and call
1665 1669 sys.excepthook themselves. I guess this is a feature that
1666 1670 enables them to keep running after exceptions that would
1667 1671 otherwise kill their mainloop. This is a bother for IPython
1668 1672 which excepts to catch all of the program exceptions with a try:
1669 1673 except: statement.
1670 1674
1671 1675 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1672 1676 any app directly invokes sys.excepthook, it will look to the user like
1673 1677 IPython crashed. In order to work around this, we can disable the
1674 1678 CrashHandler and replace it with this excepthook instead, which prints a
1675 1679 regular traceback using our InteractiveTB. In this fashion, apps which
1676 1680 call sys.excepthook will generate a regular-looking exception from
1677 1681 IPython, and the CrashHandler will only be triggered by real IPython
1678 1682 crashes.
1679 1683
1680 1684 This hook should be used sparingly, only in places which are not likely
1681 1685 to be true IPython errors.
1682 1686 """
1683 1687 self.showtraceback((etype,value,tb),tb_offset=0)
1684 1688
1685 1689 def expand_aliases(self,fn,rest):
1686 1690 """ Expand multiple levels of aliases:
1687 1691
1688 1692 if:
1689 1693
1690 1694 alias foo bar /tmp
1691 1695 alias baz foo
1692 1696
1693 1697 then:
1694 1698
1695 1699 baz huhhahhei -> bar /tmp huhhahhei
1696 1700
1697 1701 """
1698 1702 line = fn + " " + rest
1699 1703
1700 1704 done = Set()
1701 1705 while 1:
1702 1706 pre,fn,rest = prefilter.splitUserInput(line,
1703 1707 prefilter.shell_line_split)
1704 1708 if fn in self.alias_table:
1705 1709 if fn in done:
1706 1710 warn("Cyclic alias definition, repeated '%s'" % fn)
1707 1711 return ""
1708 1712 done.add(fn)
1709 1713
1710 1714 l2 = self.transform_alias(fn,rest)
1711 1715 # dir -> dir
1712 1716 # print "alias",line, "->",l2 #dbg
1713 1717 if l2 == line:
1714 1718 break
1715 1719 # ls -> ls -F should not recurse forever
1716 1720 if l2.split(None,1)[0] == line.split(None,1)[0]:
1717 1721 line = l2
1718 1722 break
1719 1723
1720 1724 line=l2
1721 1725
1722 1726
1723 1727 # print "al expand to",line #dbg
1724 1728 else:
1725 1729 break
1726 1730
1727 1731 return line
1728 1732
1729 1733 def transform_alias(self, alias,rest=''):
1730 1734 """ Transform alias to system command string.
1731 1735 """
1732 1736 nargs,cmd = self.alias_table[alias]
1733 1737 if ' ' in cmd and os.path.isfile(cmd):
1734 1738 cmd = '"%s"' % cmd
1735 1739
1736 1740 # Expand the %l special to be the user's input line
1737 1741 if cmd.find('%l') >= 0:
1738 1742 cmd = cmd.replace('%l',rest)
1739 1743 rest = ''
1740 1744 if nargs==0:
1741 1745 # Simple, argument-less aliases
1742 1746 cmd = '%s %s' % (cmd,rest)
1743 1747 else:
1744 1748 # Handle aliases with positional arguments
1745 1749 args = rest.split(None,nargs)
1746 1750 if len(args)< nargs:
1747 1751 error('Alias <%s> requires %s arguments, %s given.' %
1748 1752 (alias,nargs,len(args)))
1749 1753 return None
1750 1754 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1751 1755 # Now call the macro, evaluating in the user's namespace
1752 1756 #print 'new command: <%r>' % cmd # dbg
1753 1757 return cmd
1754 1758
1755 1759 def call_alias(self,alias,rest=''):
1756 1760 """Call an alias given its name and the rest of the line.
1757 1761
1758 1762 This is only used to provide backwards compatibility for users of
1759 1763 ipalias(), use of which is not recommended for anymore."""
1760 1764
1761 1765 # Now call the macro, evaluating in the user's namespace
1762 1766 cmd = self.transform_alias(alias, rest)
1763 1767 try:
1764 1768 self.system(cmd)
1765 1769 except:
1766 1770 self.showtraceback()
1767 1771
1768 1772 def indent_current_str(self):
1769 1773 """return the current level of indentation as a string"""
1770 1774 return self.indent_current_nsp * ' '
1771 1775
1772 1776 def autoindent_update(self,line):
1773 1777 """Keep track of the indent level."""
1774 1778
1775 1779 #debugx('line')
1776 1780 #debugx('self.indent_current_nsp')
1777 1781 if self.autoindent:
1778 1782 if line:
1779 1783 inisp = num_ini_spaces(line)
1780 1784 if inisp < self.indent_current_nsp:
1781 1785 self.indent_current_nsp = inisp
1782 1786
1783 1787 if line[-1] == ':':
1784 1788 self.indent_current_nsp += 4
1785 1789 elif dedent_re.match(line):
1786 1790 self.indent_current_nsp -= 4
1787 1791 else:
1788 1792 self.indent_current_nsp = 0
1789 1793 def runlines(self,lines):
1790 1794 """Run a string of one or more lines of source.
1791 1795
1792 1796 This method is capable of running a string containing multiple source
1793 1797 lines, as if they had been entered at the IPython prompt. Since it
1794 1798 exposes IPython's processing machinery, the given strings can contain
1795 1799 magic calls (%magic), special shell access (!cmd), etc."""
1796 1800
1797 1801 # We must start with a clean buffer, in case this is run from an
1798 1802 # interactive IPython session (via a magic, for example).
1799 1803 self.resetbuffer()
1800 1804 lines = lines.split('\n')
1801 1805 more = 0
1802 1806
1803 1807 for line in lines:
1804 1808 # skip blank lines so we don't mess up the prompt counter, but do
1805 1809 # NOT skip even a blank line if we are in a code block (more is
1806 1810 # true)
1807 1811
1808 1812
1809 1813 if line or more:
1810 1814 # push to raw history, so hist line numbers stay in sync
1811 1815 self.input_hist_raw.append("# " + line + "\n")
1812 1816 more = self.push(self.prefilter(line,more))
1813 1817 # IPython's runsource returns None if there was an error
1814 1818 # compiling the code. This allows us to stop processing right
1815 1819 # away, so the user gets the error message at the right place.
1816 1820 if more is None:
1817 1821 break
1818 1822 # final newline in case the input didn't have it, so that the code
1819 1823 # actually does get executed
1820 1824 if more:
1821 1825 self.push('\n')
1822 1826
1823 1827 def runsource(self, source, filename='<input>', symbol='single'):
1824 1828 """Compile and run some source in the interpreter.
1825 1829
1826 1830 Arguments are as for compile_command().
1827 1831
1828 1832 One several things can happen:
1829 1833
1830 1834 1) The input is incorrect; compile_command() raised an
1831 1835 exception (SyntaxError or OverflowError). A syntax traceback
1832 1836 will be printed by calling the showsyntaxerror() method.
1833 1837
1834 1838 2) The input is incomplete, and more input is required;
1835 1839 compile_command() returned None. Nothing happens.
1836 1840
1837 1841 3) The input is complete; compile_command() returned a code
1838 1842 object. The code is executed by calling self.runcode() (which
1839 1843 also handles run-time exceptions, except for SystemExit).
1840 1844
1841 1845 The return value is:
1842 1846
1843 1847 - True in case 2
1844 1848
1845 1849 - False in the other cases, unless an exception is raised, where
1846 1850 None is returned instead. This can be used by external callers to
1847 1851 know whether to continue feeding input or not.
1848 1852
1849 1853 The return value can be used to decide whether to use sys.ps1 or
1850 1854 sys.ps2 to prompt the next line."""
1851 1855
1852 1856 # if the source code has leading blanks, add 'if 1:\n' to it
1853 1857 # this allows execution of indented pasted code. It is tempting
1854 1858 # to add '\n' at the end of source to run commands like ' a=1'
1855 1859 # directly, but this fails for more complicated scenarios
1856 1860 if source[:1] in [' ', '\t']:
1857 1861 source = 'if 1:\n%s' % source
1858 1862
1859 1863 try:
1860 1864 code = self.compile(source,filename,symbol)
1861 1865 except (OverflowError, SyntaxError, ValueError):
1862 1866 # Case 1
1863 1867 self.showsyntaxerror(filename)
1864 1868 return None
1865 1869
1866 1870 if code is None:
1867 1871 # Case 2
1868 1872 return True
1869 1873
1870 1874 # Case 3
1871 1875 # We store the code object so that threaded shells and
1872 1876 # custom exception handlers can access all this info if needed.
1873 1877 # The source corresponding to this can be obtained from the
1874 1878 # buffer attribute as '\n'.join(self.buffer).
1875 1879 self.code_to_run = code
1876 1880 # now actually execute the code object
1877 1881 if self.runcode(code) == 0:
1878 1882 return False
1879 1883 else:
1880 1884 return None
1881 1885
1882 1886 def runcode(self,code_obj):
1883 1887 """Execute a code object.
1884 1888
1885 1889 When an exception occurs, self.showtraceback() is called to display a
1886 1890 traceback.
1887 1891
1888 1892 Return value: a flag indicating whether the code to be run completed
1889 1893 successfully:
1890 1894
1891 1895 - 0: successful execution.
1892 1896 - 1: an error occurred.
1893 1897 """
1894 1898
1895 1899 # Set our own excepthook in case the user code tries to call it
1896 1900 # directly, so that the IPython crash handler doesn't get triggered
1897 1901 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1898 1902
1899 1903 # we save the original sys.excepthook in the instance, in case config
1900 1904 # code (such as magics) needs access to it.
1901 1905 self.sys_excepthook = old_excepthook
1902 1906 outflag = 1 # happens in more places, so it's easier as default
1903 1907 try:
1904 1908 try:
1905 1909 # Embedded instances require separate global/local namespaces
1906 1910 # so they can see both the surrounding (local) namespace and
1907 1911 # the module-level globals when called inside another function.
1908 1912 if self.embedded:
1909 1913 exec code_obj in self.user_global_ns, self.user_ns
1910 1914 # Normal (non-embedded) instances should only have a single
1911 1915 # namespace for user code execution, otherwise functions won't
1912 1916 # see interactive top-level globals.
1913 1917 else:
1914 1918 exec code_obj in self.user_ns
1915 1919 finally:
1916 1920 # Reset our crash handler in place
1917 1921 sys.excepthook = old_excepthook
1918 1922 except SystemExit:
1919 1923 self.resetbuffer()
1920 1924 self.showtraceback()
1921 1925 warn("Type %exit or %quit to exit IPython "
1922 1926 "(%Exit or %Quit do so unconditionally).",level=1)
1923 1927 except self.custom_exceptions:
1924 1928 etype,value,tb = sys.exc_info()
1925 1929 self.CustomTB(etype,value,tb)
1926 1930 except:
1927 1931 self.showtraceback()
1928 1932 else:
1929 1933 outflag = 0
1930 1934 if softspace(sys.stdout, 0):
1931 1935 print
1932 1936 # Flush out code object which has been run (and source)
1933 1937 self.code_to_run = None
1934 1938 return outflag
1935 1939
1936 1940 def push(self, line):
1937 1941 """Push a line to the interpreter.
1938 1942
1939 1943 The line should not have a trailing newline; it may have
1940 1944 internal newlines. The line is appended to a buffer and the
1941 1945 interpreter's runsource() method is called with the
1942 1946 concatenated contents of the buffer as source. If this
1943 1947 indicates that the command was executed or invalid, the buffer
1944 1948 is reset; otherwise, the command is incomplete, and the buffer
1945 1949 is left as it was after the line was appended. The return
1946 1950 value is 1 if more input is required, 0 if the line was dealt
1947 1951 with in some way (this is the same as runsource()).
1948 1952 """
1949 1953
1950 1954 # autoindent management should be done here, and not in the
1951 1955 # interactive loop, since that one is only seen by keyboard input. We
1952 1956 # need this done correctly even for code run via runlines (which uses
1953 1957 # push).
1954 1958
1955 1959 #print 'push line: <%s>' % line # dbg
1956 1960 for subline in line.splitlines():
1957 1961 self.autoindent_update(subline)
1958 1962 self.buffer.append(line)
1959 1963 more = self.runsource('\n'.join(self.buffer), self.filename)
1960 1964 if not more:
1961 1965 self.resetbuffer()
1962 1966 return more
1963 1967
1964 1968 def split_user_input(self, line):
1965 1969 # This is really a hold-over to support ipapi and some extensions
1966 1970 return prefilter.splitUserInput(line)
1967 1971
1968 1972 def resetbuffer(self):
1969 1973 """Reset the input buffer."""
1970 1974 self.buffer[:] = []
1971 1975
1972 1976 def raw_input(self,prompt='',continue_prompt=False):
1973 1977 """Write a prompt and read a line.
1974 1978
1975 1979 The returned line does not include the trailing newline.
1976 1980 When the user enters the EOF key sequence, EOFError is raised.
1977 1981
1978 1982 Optional inputs:
1979 1983
1980 1984 - prompt(''): a string to be printed to prompt the user.
1981 1985
1982 1986 - continue_prompt(False): whether this line is the first one or a
1983 1987 continuation in a sequence of inputs.
1984 1988 """
1985 1989
1986 1990 # Code run by the user may have modified the readline completer state.
1987 1991 # We must ensure that our completer is back in place.
1988 1992 if self.has_readline:
1989 1993 self.set_completer()
1990 1994
1991 1995 try:
1992 1996 line = raw_input_original(prompt).decode(self.stdin_encoding)
1993 1997 except ValueError:
1994 1998 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
1995 1999 " or sys.stdout.close()!\nExiting IPython!")
1996 2000 self.exit_now = True
1997 2001 return ""
1998 2002
1999 2003 # Try to be reasonably smart about not re-indenting pasted input more
2000 2004 # than necessary. We do this by trimming out the auto-indent initial
2001 2005 # spaces, if the user's actual input started itself with whitespace.
2002 2006 #debugx('self.buffer[-1]')
2003 2007
2004 2008 if self.autoindent:
2005 2009 if num_ini_spaces(line) > self.indent_current_nsp:
2006 2010 line = line[self.indent_current_nsp:]
2007 2011 self.indent_current_nsp = 0
2008 2012
2009 2013 # store the unfiltered input before the user has any chance to modify
2010 2014 # it.
2011 2015 if line.strip():
2012 2016 if continue_prompt:
2013 2017 self.input_hist_raw[-1] += '%s\n' % line
2014 2018 if self.has_readline: # and some config option is set?
2015 2019 try:
2016 2020 histlen = self.readline.get_current_history_length()
2017 2021 newhist = self.input_hist_raw[-1].rstrip()
2018 2022 self.readline.remove_history_item(histlen-1)
2019 2023 self.readline.replace_history_item(histlen-2,newhist)
2020 2024 except AttributeError:
2021 2025 pass # re{move,place}_history_item are new in 2.4.
2022 2026 else:
2023 2027 self.input_hist_raw.append('%s\n' % line)
2024 2028
2025 2029 if line.lstrip() == line:
2026 2030 self.shadowhist.add(line.strip())
2027 2031
2028 2032 try:
2029 2033 lineout = self.prefilter(line,continue_prompt)
2030 2034 except:
2031 2035 # blanket except, in case a user-defined prefilter crashes, so it
2032 2036 # can't take all of ipython with it.
2033 2037 self.showtraceback()
2034 2038 return ''
2035 2039 else:
2036 2040 return lineout
2037 2041
2038 2042 def _prefilter(self, line, continue_prompt):
2039 2043 """Calls different preprocessors, depending on the form of line."""
2040 2044
2041 2045 # All handlers *must* return a value, even if it's blank ('').
2042 2046
2043 2047 # Lines are NOT logged here. Handlers should process the line as
2044 2048 # needed, update the cache AND log it (so that the input cache array
2045 2049 # stays synced).
2046 2050
2047 2051 #.....................................................................
2048 2052 # Code begins
2049 2053
2050 2054 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
2051 2055
2052 2056 # save the line away in case we crash, so the post-mortem handler can
2053 2057 # record it
2054 2058 self._last_input_line = line
2055 2059
2056 2060 #print '***line: <%s>' % line # dbg
2057 2061
2058 2062 line_info = prefilter.LineInfo(line, continue_prompt)
2059 2063
2060 2064 # the input history needs to track even empty lines
2061 2065 stripped = line.strip()
2062 2066
2063 2067 if not stripped:
2064 2068 if not continue_prompt:
2065 2069 self.outputcache.prompt_count -= 1
2066 2070 return self.handle_normal(line_info)
2067 2071
2068 2072 # print '***cont',continue_prompt # dbg
2069 2073 # special handlers are only allowed for single line statements
2070 2074 if continue_prompt and not self.rc.multi_line_specials:
2071 2075 return self.handle_normal(line_info)
2072 2076
2073 2077
2074 2078 # See whether any pre-existing handler can take care of it
2075 2079 rewritten = self.hooks.input_prefilter(stripped)
2076 2080 if rewritten != stripped: # ok, some prefilter did something
2077 2081 rewritten = line_info.pre + rewritten # add indentation
2078 2082 return self.handle_normal(prefilter.LineInfo(rewritten,
2079 2083 continue_prompt))
2080 2084
2081 2085 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2082 2086
2083 2087 return prefilter.prefilter(line_info, self)
2084 2088
2085 2089
2086 2090 def _prefilter_dumb(self, line, continue_prompt):
2087 2091 """simple prefilter function, for debugging"""
2088 2092 return self.handle_normal(line,continue_prompt)
2089 2093
2090 2094
2091 2095 def multiline_prefilter(self, line, continue_prompt):
2092 2096 """ Run _prefilter for each line of input
2093 2097
2094 2098 Covers cases where there are multiple lines in the user entry,
2095 2099 which is the case when the user goes back to a multiline history
2096 2100 entry and presses enter.
2097 2101
2098 2102 """
2099 2103 out = []
2100 2104 for l in line.rstrip('\n').split('\n'):
2101 2105 out.append(self._prefilter(l, continue_prompt))
2102 2106 return '\n'.join(out)
2103 2107
2104 2108 # Set the default prefilter() function (this can be user-overridden)
2105 2109 prefilter = multiline_prefilter
2106 2110
2107 2111 def handle_normal(self,line_info):
2108 2112 """Handle normal input lines. Use as a template for handlers."""
2109 2113
2110 2114 # With autoindent on, we need some way to exit the input loop, and I
2111 2115 # don't want to force the user to have to backspace all the way to
2112 2116 # clear the line. The rule will be in this case, that either two
2113 2117 # lines of pure whitespace in a row, or a line of pure whitespace but
2114 2118 # of a size different to the indent level, will exit the input loop.
2115 2119 line = line_info.line
2116 2120 continue_prompt = line_info.continue_prompt
2117 2121
2118 2122 if (continue_prompt and self.autoindent and line.isspace() and
2119 2123 (0 < abs(len(line) - self.indent_current_nsp) <= 2 or
2120 2124 (self.buffer[-1]).isspace() )):
2121 2125 line = ''
2122 2126
2123 2127 self.log(line,line,continue_prompt)
2124 2128 return line
2125 2129
2126 2130 def handle_alias(self,line_info):
2127 2131 """Handle alias input lines. """
2128 2132 tgt = self.alias_table[line_info.iFun]
2129 2133 # print "=>",tgt #dbg
2130 2134 if callable(tgt):
2131 2135 line_out = "_sh." + line_info.iFun + '(r"""' + line_info.theRest + '""")'
2132 2136 else:
2133 2137 transformed = self.expand_aliases(line_info.iFun,line_info.theRest)
2134 2138
2135 2139 # pre is needed, because it carries the leading whitespace. Otherwise
2136 2140 # aliases won't work in indented sections.
2137 2141 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2138 2142 make_quoted_expr( transformed ))
2139 2143
2140 2144 self.log(line_info.line,line_out,line_info.continue_prompt)
2141 2145 #print 'line out:',line_out # dbg
2142 2146 return line_out
2143 2147
2144 2148 def handle_shell_escape(self, line_info):
2145 2149 """Execute the line in a shell, empty return value"""
2146 2150 #print 'line in :', `line` # dbg
2147 2151 line = line_info.line
2148 2152 if line.lstrip().startswith('!!'):
2149 2153 # rewrite LineInfo's line, iFun and theRest to properly hold the
2150 2154 # call to %sx and the actual command to be executed, so
2151 2155 # handle_magic can work correctly. Note that this works even if
2152 2156 # the line is indented, so it handles multi_line_specials
2153 2157 # properly.
2154 2158 new_rest = line.lstrip()[2:]
2155 2159 line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest)
2156 2160 line_info.iFun = 'sx'
2157 2161 line_info.theRest = new_rest
2158 2162 return self.handle_magic(line_info)
2159 2163 else:
2160 2164 cmd = line.lstrip().lstrip('!')
2161 2165 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2162 2166 make_quoted_expr(cmd))
2163 2167 # update cache/log and return
2164 2168 self.log(line,line_out,line_info.continue_prompt)
2165 2169 return line_out
2166 2170
2167 2171 def handle_magic(self, line_info):
2168 2172 """Execute magic functions."""
2169 2173 iFun = line_info.iFun
2170 2174 theRest = line_info.theRest
2171 2175 cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace,
2172 2176 make_quoted_expr(iFun + " " + theRest))
2173 2177 self.log(line_info.line,cmd,line_info.continue_prompt)
2174 2178 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
2175 2179 return cmd
2176 2180
2177 2181 def handle_auto(self, line_info):
2178 2182 """Hande lines which can be auto-executed, quoting if requested."""
2179 2183
2180 2184 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2181 2185 line = line_info.line
2182 2186 iFun = line_info.iFun
2183 2187 theRest = line_info.theRest
2184 2188 pre = line_info.pre
2185 2189 continue_prompt = line_info.continue_prompt
2186 2190 obj = line_info.ofind(self)['obj']
2187 2191
2188 2192 # This should only be active for single-line input!
2189 2193 if continue_prompt:
2190 2194 self.log(line,line,continue_prompt)
2191 2195 return line
2192 2196
2193 2197 force_auto = isinstance(obj, IPython.ipapi.IPyAutocall)
2194 2198 auto_rewrite = True
2195 2199
2196 2200 if pre == self.ESC_QUOTE:
2197 2201 # Auto-quote splitting on whitespace
2198 2202 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2199 2203 elif pre == self.ESC_QUOTE2:
2200 2204 # Auto-quote whole string
2201 2205 newcmd = '%s("%s")' % (iFun,theRest)
2202 2206 elif pre == self.ESC_PAREN:
2203 2207 newcmd = '%s(%s)' % (iFun,",".join(theRest.split()))
2204 2208 else:
2205 2209 # Auto-paren.
2206 2210 # We only apply it to argument-less calls if the autocall
2207 2211 # parameter is set to 2. We only need to check that autocall is <
2208 2212 # 2, since this function isn't called unless it's at least 1.
2209 2213 if not theRest and (self.rc.autocall < 2) and not force_auto:
2210 2214 newcmd = '%s %s' % (iFun,theRest)
2211 2215 auto_rewrite = False
2212 2216 else:
2213 2217 if not force_auto and theRest.startswith('['):
2214 2218 if hasattr(obj,'__getitem__'):
2215 2219 # Don't autocall in this case: item access for an object
2216 2220 # which is BOTH callable and implements __getitem__.
2217 2221 newcmd = '%s %s' % (iFun,theRest)
2218 2222 auto_rewrite = False
2219 2223 else:
2220 2224 # if the object doesn't support [] access, go ahead and
2221 2225 # autocall
2222 2226 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2223 2227 elif theRest.endswith(';'):
2224 2228 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2225 2229 else:
2226 2230 newcmd = '%s(%s)' % (iFun.rstrip(), theRest)
2227 2231
2228 2232 if auto_rewrite:
2229 2233 rw = self.outputcache.prompt1.auto_rewrite() + newcmd
2230 2234
2231 2235 try:
2232 2236 # plain ascii works better w/ pyreadline, on some machines, so
2233 2237 # we use it and only print uncolored rewrite if we have unicode
2234 2238 rw = str(rw)
2235 2239 print >>Term.cout, rw
2236 2240 except UnicodeEncodeError:
2237 2241 print "-------------->" + newcmd
2238 2242
2239 2243 # log what is now valid Python, not the actual user input (without the
2240 2244 # final newline)
2241 2245 self.log(line,newcmd,continue_prompt)
2242 2246 return newcmd
2243 2247
2244 2248 def handle_help(self, line_info):
2245 2249 """Try to get some help for the object.
2246 2250
2247 2251 obj? or ?obj -> basic information.
2248 2252 obj?? or ??obj -> more details.
2249 2253 """
2250 2254
2251 2255 line = line_info.line
2252 2256 # We need to make sure that we don't process lines which would be
2253 2257 # otherwise valid python, such as "x=1 # what?"
2254 2258 try:
2255 2259 codeop.compile_command(line)
2256 2260 except SyntaxError:
2257 2261 # We should only handle as help stuff which is NOT valid syntax
2258 2262 if line[0]==self.ESC_HELP:
2259 2263 line = line[1:]
2260 2264 elif line[-1]==self.ESC_HELP:
2261 2265 line = line[:-1]
2262 2266 self.log(line,'#?'+line,line_info.continue_prompt)
2263 2267 if line:
2264 2268 #print 'line:<%r>' % line # dbg
2265 2269 self.magic_pinfo(line)
2266 2270 else:
2267 2271 page(self.usage,screen_lines=self.rc.screen_length)
2268 2272 return '' # Empty string is needed here!
2269 2273 except:
2270 2274 # Pass any other exceptions through to the normal handler
2271 2275 return self.handle_normal(line_info)
2272 2276 else:
2273 2277 # If the code compiles ok, we should handle it normally
2274 2278 return self.handle_normal(line_info)
2275 2279
2276 2280 def getapi(self):
2277 2281 """ Get an IPApi object for this shell instance
2278 2282
2279 2283 Getting an IPApi object is always preferable to accessing the shell
2280 2284 directly, but this holds true especially for extensions.
2281 2285
2282 2286 It should always be possible to implement an extension with IPApi
2283 2287 alone. If not, contact maintainer to request an addition.
2284 2288
2285 2289 """
2286 2290 return self.api
2287 2291
2288 2292 def handle_emacs(self, line_info):
2289 2293 """Handle input lines marked by python-mode."""
2290 2294
2291 2295 # Currently, nothing is done. Later more functionality can be added
2292 2296 # here if needed.
2293 2297
2294 2298 # The input cache shouldn't be updated
2295 2299 return line_info.line
2296 2300
2297 2301
2298 2302 def mktempfile(self,data=None):
2299 2303 """Make a new tempfile and return its filename.
2300 2304
2301 2305 This makes a call to tempfile.mktemp, but it registers the created
2302 2306 filename internally so ipython cleans it up at exit time.
2303 2307
2304 2308 Optional inputs:
2305 2309
2306 2310 - data(None): if data is given, it gets written out to the temp file
2307 2311 immediately, and the file is closed again."""
2308 2312
2309 2313 filename = tempfile.mktemp('.py','ipython_edit_')
2310 2314 self.tempfiles.append(filename)
2311 2315
2312 2316 if data:
2313 2317 tmp_file = open(filename,'w')
2314 2318 tmp_file.write(data)
2315 2319 tmp_file.close()
2316 2320 return filename
2317 2321
2318 2322 def write(self,data):
2319 2323 """Write a string to the default output"""
2320 2324 Term.cout.write(data)
2321 2325
2322 2326 def write_err(self,data):
2323 2327 """Write a string to the default error output"""
2324 2328 Term.cerr.write(data)
2325 2329
2326 2330 def exit(self):
2327 2331 """Handle interactive exit.
2328 2332
2329 2333 This method sets the exit_now attribute."""
2330 2334
2331 2335 if self.rc.confirm_exit:
2332 2336 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2333 2337 self.exit_now = True
2334 2338 else:
2335 2339 self.exit_now = True
2336 2340
2337 2341 def safe_execfile(self,fname,*where,**kw):
2338 2342 """A safe version of the builtin execfile().
2339 2343
2340 2344 This version will never throw an exception, and knows how to handle
2341 2345 ipython logs as well."""
2342 2346
2343 2347 def syspath_cleanup():
2344 2348 """Internal cleanup routine for sys.path."""
2345 2349 if add_dname:
2346 2350 try:
2347 2351 sys.path.remove(dname)
2348 2352 except ValueError:
2349 2353 # For some reason the user has already removed it, ignore.
2350 2354 pass
2351 2355
2352 2356 fname = os.path.expanduser(fname)
2353 2357
2354 2358 # Find things also in current directory. This is needed to mimic the
2355 2359 # behavior of running a script from the system command line, where
2356 2360 # Python inserts the script's directory into sys.path
2357 2361 dname = os.path.dirname(os.path.abspath(fname))
2358 2362 add_dname = False
2359 2363 if dname not in sys.path:
2360 2364 sys.path.insert(0,dname)
2361 2365 add_dname = True
2362 2366
2363 2367 try:
2364 2368 xfile = open(fname)
2365 2369 except:
2366 2370 print >> Term.cerr, \
2367 2371 'Could not open file <%s> for safe execution.' % fname
2368 2372 syspath_cleanup()
2369 2373 return None
2370 2374
2371 2375 kw.setdefault('islog',0)
2372 2376 kw.setdefault('quiet',1)
2373 2377 kw.setdefault('exit_ignore',0)
2374 2378 first = xfile.readline()
2375 2379 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2376 2380 xfile.close()
2377 2381 # line by line execution
2378 2382 if first.startswith(loghead) or kw['islog']:
2379 2383 print 'Loading log file <%s> one line at a time...' % fname
2380 2384 if kw['quiet']:
2381 2385 stdout_save = sys.stdout
2382 2386 sys.stdout = StringIO.StringIO()
2383 2387 try:
2384 2388 globs,locs = where[0:2]
2385 2389 except:
2386 2390 try:
2387 2391 globs = locs = where[0]
2388 2392 except:
2389 2393 globs = locs = globals()
2390 2394 badblocks = []
2391 2395
2392 2396 # we also need to identify indented blocks of code when replaying
2393 2397 # logs and put them together before passing them to an exec
2394 2398 # statement. This takes a bit of regexp and look-ahead work in the
2395 2399 # file. It's easiest if we swallow the whole thing in memory
2396 2400 # first, and manually walk through the lines list moving the
2397 2401 # counter ourselves.
2398 2402 indent_re = re.compile('\s+\S')
2399 2403 xfile = open(fname)
2400 2404 filelines = xfile.readlines()
2401 2405 xfile.close()
2402 2406 nlines = len(filelines)
2403 2407 lnum = 0
2404 2408 while lnum < nlines:
2405 2409 line = filelines[lnum]
2406 2410 lnum += 1
2407 2411 # don't re-insert logger status info into cache
2408 2412 if line.startswith('#log#'):
2409 2413 continue
2410 2414 else:
2411 2415 # build a block of code (maybe a single line) for execution
2412 2416 block = line
2413 2417 try:
2414 2418 next = filelines[lnum] # lnum has already incremented
2415 2419 except:
2416 2420 next = None
2417 2421 while next and indent_re.match(next):
2418 2422 block += next
2419 2423 lnum += 1
2420 2424 try:
2421 2425 next = filelines[lnum]
2422 2426 except:
2423 2427 next = None
2424 2428 # now execute the block of one or more lines
2425 2429 try:
2426 2430 exec block in globs,locs
2427 2431 except SystemExit:
2428 2432 pass
2429 2433 except:
2430 2434 badblocks.append(block.rstrip())
2431 2435 if kw['quiet']: # restore stdout
2432 2436 sys.stdout.close()
2433 2437 sys.stdout = stdout_save
2434 2438 print 'Finished replaying log file <%s>' % fname
2435 2439 if badblocks:
2436 2440 print >> sys.stderr, ('\nThe following lines/blocks in file '
2437 2441 '<%s> reported errors:' % fname)
2438 2442
2439 2443 for badline in badblocks:
2440 2444 print >> sys.stderr, badline
2441 2445 else: # regular file execution
2442 2446 try:
2443 2447 if sys.platform == 'win32' and sys.version_info < (2,5,1):
2444 2448 # Work around a bug in Python for Windows. The bug was
2445 2449 # fixed in in Python 2.5 r54159 and 54158, but that's still
2446 2450 # SVN Python as of March/07. For details, see:
2447 2451 # http://projects.scipy.org/ipython/ipython/ticket/123
2448 2452 try:
2449 2453 globs,locs = where[0:2]
2450 2454 except:
2451 2455 try:
2452 2456 globs = locs = where[0]
2453 2457 except:
2454 2458 globs = locs = globals()
2455 2459 exec file(fname) in globs,locs
2456 2460 else:
2457 2461 execfile(fname,*where)
2458 2462 except SyntaxError:
2459 2463 self.showsyntaxerror()
2460 2464 warn('Failure executing file: <%s>' % fname)
2461 2465 except SystemExit,status:
2462 2466 if not kw['exit_ignore']:
2463 2467 self.showtraceback()
2464 2468 warn('Failure executing file: <%s>' % fname)
2465 2469 except:
2466 2470 self.showtraceback()
2467 2471 warn('Failure executing file: <%s>' % fname)
2468 2472
2469 2473 syspath_cleanup()
2470 2474
2471 2475 #************************* 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
@@ -1,129 +1,131 b''
1 1 #!/usr/bin/env python
2 2
3 3 """An example of how to embed an IPython shell into a running program.
4 4
5 5 Please see the documentation in the IPython.Shell module for more details.
6 6
7 7 The accompanying file example-embed-short.py has quick code fragments for
8 8 embedding which you can cut and paste in your code once you understand how
9 9 things work.
10 10
11 11 The code in this file is deliberately extra-verbose, meant for learning."""
12 12
13 13 # The basics to get you going:
14 14
15 15 # IPython sets the __IPYTHON__ variable so you can know if you have nested
16 16 # copies running.
17 17
18 18 # Try running this code both at the command line and from inside IPython (with
19 19 # %run example-embed.py)
20 20 try:
21 21 __IPYTHON__
22 22 except NameError:
23 23 nested = 0
24 24 args = ['']
25 25 else:
26 26 print "Running nested copies of IPython."
27 27 print "The prompts for the nested copy have been modified"
28 28 nested = 1
29 29 # what the embedded instance will see as sys.argv:
30 30 args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ',
31 31 '-po','Out<\\#>: ','-nosep']
32 32
33 33 # First import the embeddable shell class
34 34 from IPython.Shell import IPShellEmbed
35 35
36 36 # Now create an instance of the embeddable shell. The first argument is a
37 37 # string with options exactly as you would type them if you were starting
38 38 # IPython at the system command line. Any parameters you want to define for
39 39 # configuration can thus be specified here.
40 40 ipshell = IPShellEmbed(args,
41 41 banner = 'Dropping into IPython',
42 42 exit_msg = 'Leaving Interpreter, back to program.')
43 43
44 44 # Make a second instance, you can have as many as you want.
45 45 if nested:
46 46 args[1] = 'In2<\\#>'
47 47 else:
48 48 args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ',
49 49 '-po','Out<\\#>: ','-nosep']
50 50 ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.')
51 51
52 52 print '\nHello. This is printed from the main controller program.\n'
53 53
54 54 # You can then call ipshell() anywhere you need it (with an optional
55 55 # message):
56 56 ipshell('***Called from top level. '
57 'Hit Ctrl-D to exit interpreter and continue program.')
57 'Hit Ctrl-D to exit interpreter and continue program.\n'
58 'Note that if you use %kill_embedded, you can fully deactivate\n'
59 'This embedded instance so it will never turn on again')
58 60
59 61 print '\nBack in caller program, moving along...\n'
60 62
61 63 #---------------------------------------------------------------------------
62 64 # More details:
63 65
64 66 # IPShellEmbed instances don't print the standard system banner and
65 67 # messages. The IPython banner (which actually may contain initialization
66 68 # messages) is available as <instance>.IP.BANNER in case you want it.
67 69
68 70 # IPShellEmbed instances print the following information everytime they
69 71 # start:
70 72
71 73 # - A global startup banner.
72 74
73 75 # - A call-specific header string, which you can use to indicate where in the
74 76 # execution flow the shell is starting.
75 77
76 78 # They also print an exit message every time they exit.
77 79
78 80 # Both the startup banner and the exit message default to None, and can be set
79 81 # either at the instance constructor or at any other time with the
80 82 # set_banner() and set_exit_msg() methods.
81 83
82 84 # The shell instance can be also put in 'dummy' mode globally or on a per-call
83 85 # basis. This gives you fine control for debugging without having to change
84 86 # code all over the place.
85 87
86 88 # The code below illustrates all this.
87 89
88 90
89 91 # This is how the global banner and exit_msg can be reset at any point
90 92 ipshell.set_banner('Entering interpreter - New Banner')
91 93 ipshell.set_exit_msg('Leaving interpreter - New exit_msg')
92 94
93 95 def foo(m):
94 96 s = 'spam'
95 97 ipshell('***In foo(). Try @whos, or print s or m:')
96 98 print 'foo says m = ',m
97 99
98 100 def bar(n):
99 101 s = 'eggs'
100 102 ipshell('***In bar(). Try @whos, or print s or n:')
101 103 print 'bar says n = ',n
102 104
103 105 # Some calls to the above functions which will trigger IPython:
104 106 print 'Main program calling foo("eggs")\n'
105 107 foo('eggs')
106 108
107 109 # The shell can be put in 'dummy' mode where calls to it silently return. This
108 110 # allows you, for example, to globally turn off debugging for a program with a
109 111 # single call.
110 112 ipshell.set_dummy_mode(1)
111 113 print '\nTrying to call IPython which is now "dummy":'
112 114 ipshell()
113 115 print 'Nothing happened...'
114 116 # The global 'dummy' mode can still be overridden for a single call
115 117 print '\nOverriding dummy mode manually:'
116 118 ipshell(dummy=0)
117 119
118 120 # Reactivate the IPython shell
119 121 ipshell.set_dummy_mode(0)
120 122
121 123 print 'You can even have multiple embedded instances:'
122 124 ipshell2()
123 125
124 126 print '\nMain program calling bar("spam")\n'
125 127 bar('spam')
126 128
127 129 print 'Main program finished. Bye!'
128 130
129 131 #********************** End of file <example-embed.py> ***********************
General Comments 0
You need to be logged in to leave comments. Login now