##// END OF EJS Templates
BUG: Fix syntax error.
Robert Kern -
Show More
@@ -1,1236 +1,1236 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """IPython Shell classes.
2 """IPython Shell classes.
3
3
4 All the matplotlib support code was co-developed with John Hunter,
4 All the matplotlib support code was co-developed with John Hunter,
5 matplotlib's author.
5 matplotlib's author.
6
6
7 $Id: Shell.py 3024 2008-02-07 15:34:42Z darren.dale $"""
7 $Id: Shell.py 3024 2008-02-07 15:34:42Z darren.dale $"""
8
8
9 #*****************************************************************************
9 #*****************************************************************************
10 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
10 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #*****************************************************************************
14 #*****************************************************************************
15
15
16 from IPython import Release
16 from IPython import Release
17 __author__ = '%s <%s>' % Release.authors['Fernando']
17 __author__ = '%s <%s>' % Release.authors['Fernando']
18 __license__ = Release.license
18 __license__ = Release.license
19
19
20 # Code begins
20 # Code begins
21 # Stdlib imports
21 # Stdlib imports
22 import __builtin__
22 import __builtin__
23 import __main__
23 import __main__
24 import Queue
24 import Queue
25 import inspect
25 import inspect
26 import os
26 import os
27 import sys
27 import sys
28 import thread
28 import thread
29 import threading
29 import threading
30 import time
30 import time
31
31
32 from signal import signal, SIGINT
32 from signal import signal, SIGINT
33
33
34 try:
34 try:
35 import ctypes
35 import ctypes
36 HAS_CTYPES = True
36 HAS_CTYPES = True
37 except ImportError:
37 except ImportError:
38 HAS_CTYPES = False
38 HAS_CTYPES = False
39
39
40 # IPython imports
40 # IPython imports
41 import IPython
41 import IPython
42 from IPython import ultraTB, ipapi
42 from IPython import ultraTB, ipapi
43 from IPython.genutils import Term,warn,error,flag_calls, ask_yes_no
43 from IPython.genutils import Term,warn,error,flag_calls, ask_yes_no
44 from IPython.iplib import InteractiveShell
44 from IPython.iplib import InteractiveShell
45 from IPython.ipmaker import make_IPython
45 from IPython.ipmaker import make_IPython
46 from IPython.Magic import Magic
46 from IPython.Magic import Magic
47 from IPython.ipstruct import Struct
47 from IPython.ipstruct import Struct
48
48
49 try: # Python 2.3 compatibility
49 try: # Python 2.3 compatibility
50 set
50 set
51 except NameError:
51 except NameError:
52 import sets
52 import sets
53 set = sets.Set
53 set = sets.Set
54
54
55
55
56 # Globals
56 # Globals
57 # global flag to pass around information about Ctrl-C without exceptions
57 # global flag to pass around information about Ctrl-C without exceptions
58 KBINT = False
58 KBINT = False
59
59
60 # global flag to turn on/off Tk support.
60 # global flag to turn on/off Tk support.
61 USE_TK = False
61 USE_TK = False
62
62
63 # ID for the main thread, used for cross-thread exceptions
63 # ID for the main thread, used for cross-thread exceptions
64 MAIN_THREAD_ID = thread.get_ident()
64 MAIN_THREAD_ID = thread.get_ident()
65
65
66 # Tag when runcode() is active, for exception handling
66 # Tag when runcode() is active, for exception handling
67 CODE_RUN = None
67 CODE_RUN = None
68
68
69 #-----------------------------------------------------------------------------
69 #-----------------------------------------------------------------------------
70 # This class is trivial now, but I want to have it in to publish a clean
70 # This class is trivial now, but I want to have it in to publish a clean
71 # interface. Later when the internals are reorganized, code that uses this
71 # interface. Later when the internals are reorganized, code that uses this
72 # shouldn't have to change.
72 # shouldn't have to change.
73
73
74 class IPShell:
74 class IPShell:
75 """Create an IPython instance."""
75 """Create an IPython instance."""
76
76
77 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
77 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
78 debug=1,shell_class=InteractiveShell):
78 debug=1,shell_class=InteractiveShell):
79 self.IP = make_IPython(argv,user_ns=user_ns,
79 self.IP = make_IPython(argv,user_ns=user_ns,
80 user_global_ns=user_global_ns,
80 user_global_ns=user_global_ns,
81 debug=debug,shell_class=shell_class)
81 debug=debug,shell_class=shell_class)
82
82
83 def mainloop(self,sys_exit=0,banner=None):
83 def mainloop(self,sys_exit=0,banner=None):
84 self.IP.mainloop(banner)
84 self.IP.mainloop(banner)
85 if sys_exit:
85 if sys_exit:
86 sys.exit()
86 sys.exit()
87
87
88 #-----------------------------------------------------------------------------
88 #-----------------------------------------------------------------------------
89 def kill_embedded(self,parameter_s=''):
89 def kill_embedded(self,parameter_s=''):
90 """%kill_embedded : deactivate for good the current embedded IPython.
90 """%kill_embedded : deactivate for good the current embedded IPython.
91
91
92 This function (after asking for confirmation) sets an internal flag so that
92 This function (after asking for confirmation) sets an internal flag so that
93 an embedded IPython will never activate again. This is useful to
93 an embedded IPython will never activate again. This is useful to
94 permanently disable a shell that is being called inside a loop: once you've
94 permanently disable a shell that is being called inside a loop: once you've
95 figured out what you needed from it, you may then kill it and the program
95 figured out what you needed from it, you may then kill it and the program
96 will then continue to run without the interactive shell interfering again.
96 will then continue to run without the interactive shell interfering again.
97 """
97 """
98
98
99 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
99 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
100 "(y/n)? [y/N] ",'n')
100 "(y/n)? [y/N] ",'n')
101 if kill:
101 if kill:
102 self.shell.embedded_active = False
102 self.shell.embedded_active = False
103 print "This embedded IPython will not reactivate anymore once you exit."
103 print "This embedded IPython will not reactivate anymore once you exit."
104
104
105 class IPShellEmbed:
105 class IPShellEmbed:
106 """Allow embedding an IPython shell into a running program.
106 """Allow embedding an IPython shell into a running program.
107
107
108 Instances of this class are callable, with the __call__ method being an
108 Instances of this class are callable, with the __call__ method being an
109 alias to the embed() method of an InteractiveShell instance.
109 alias to the embed() method of an InteractiveShell instance.
110
110
111 Usage (see also the example-embed.py file for a running example):
111 Usage (see also the example-embed.py file for a running example):
112
112
113 ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override])
113 ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override])
114
114
115 - argv: list containing valid command-line options for IPython, as they
115 - argv: list containing valid command-line options for IPython, as they
116 would appear in sys.argv[1:].
116 would appear in sys.argv[1:].
117
117
118 For example, the following command-line options:
118 For example, the following command-line options:
119
119
120 $ ipython -prompt_in1 'Input <\\#>' -colors LightBG
120 $ ipython -prompt_in1 'Input <\\#>' -colors LightBG
121
121
122 would be passed in the argv list as:
122 would be passed in the argv list as:
123
123
124 ['-prompt_in1','Input <\\#>','-colors','LightBG']
124 ['-prompt_in1','Input <\\#>','-colors','LightBG']
125
125
126 - banner: string which gets printed every time the interpreter starts.
126 - banner: string which gets printed every time the interpreter starts.
127
127
128 - exit_msg: string which gets printed every time the interpreter exits.
128 - exit_msg: string which gets printed every time the interpreter exits.
129
129
130 - rc_override: a dict or Struct of configuration options such as those
130 - rc_override: a dict or Struct of configuration options such as those
131 used by IPython. These options are read from your ~/.ipython/ipythonrc
131 used by IPython. These options are read from your ~/.ipython/ipythonrc
132 file when the Shell object is created. Passing an explicit rc_override
132 file when the Shell object is created. Passing an explicit rc_override
133 dict with any options you want allows you to override those values at
133 dict with any options you want allows you to override those values at
134 creation time without having to modify the file. This way you can create
134 creation time without having to modify the file. This way you can create
135 embeddable instances configured in any way you want without editing any
135 embeddable instances configured in any way you want without editing any
136 global files (thus keeping your interactive IPython configuration
136 global files (thus keeping your interactive IPython configuration
137 unchanged).
137 unchanged).
138
138
139 Then the ipshell instance can be called anywhere inside your code:
139 Then the ipshell instance can be called anywhere inside your code:
140
140
141 ipshell(header='') -> Opens up an IPython shell.
141 ipshell(header='') -> Opens up an IPython shell.
142
142
143 - header: string printed by the IPython shell upon startup. This can let
143 - header: string printed by the IPython shell upon startup. This can let
144 you know where in your code you are when dropping into the shell. Note
144 you know where in your code you are when dropping into the shell. Note
145 that 'banner' gets prepended to all calls, so header is used for
145 that 'banner' gets prepended to all calls, so header is used for
146 location-specific information.
146 location-specific information.
147
147
148 For more details, see the __call__ method below.
148 For more details, see the __call__ method below.
149
149
150 When the IPython shell is exited with Ctrl-D, normal program execution
150 When the IPython shell is exited with Ctrl-D, normal program execution
151 resumes.
151 resumes.
152
152
153 This functionality was inspired by a posting on comp.lang.python by cmkl
153 This functionality was inspired by a posting on comp.lang.python by cmkl
154 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
154 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
155 by the IDL stop/continue commands."""
155 by the IDL stop/continue commands."""
156
156
157 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None,
157 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None,
158 user_ns=None):
158 user_ns=None):
159 """Note that argv here is a string, NOT a list."""
159 """Note that argv here is a string, NOT a list."""
160 self.set_banner(banner)
160 self.set_banner(banner)
161 self.set_exit_msg(exit_msg)
161 self.set_exit_msg(exit_msg)
162 self.set_dummy_mode(0)
162 self.set_dummy_mode(0)
163
163
164 # sys.displayhook is a global, we need to save the user's original
164 # sys.displayhook is a global, we need to save the user's original
165 # Don't rely on __displayhook__, as the user may have changed that.
165 # Don't rely on __displayhook__, as the user may have changed that.
166 self.sys_displayhook_ori = sys.displayhook
166 self.sys_displayhook_ori = sys.displayhook
167
167
168 # save readline completer status
168 # save readline completer status
169 try:
169 try:
170 #print 'Save completer',sys.ipcompleter # dbg
170 #print 'Save completer',sys.ipcompleter # dbg
171 self.sys_ipcompleter_ori = sys.ipcompleter
171 self.sys_ipcompleter_ori = sys.ipcompleter
172 except:
172 except:
173 pass # not nested with IPython
173 pass # not nested with IPython
174
174
175 self.IP = make_IPython(argv,rc_override=rc_override,
175 self.IP = make_IPython(argv,rc_override=rc_override,
176 embedded=True,
176 embedded=True,
177 user_ns=user_ns)
177 user_ns=user_ns)
178
178
179 ip = ipapi.IPApi(self.IP)
179 ip = ipapi.IPApi(self.IP)
180 ip.expose_magic("kill_embedded",kill_embedded)
180 ip.expose_magic("kill_embedded",kill_embedded)
181
181
182 # copy our own displayhook also
182 # copy our own displayhook also
183 self.sys_displayhook_embed = sys.displayhook
183 self.sys_displayhook_embed = sys.displayhook
184 # and leave the system's display hook clean
184 # and leave the system's display hook clean
185 sys.displayhook = self.sys_displayhook_ori
185 sys.displayhook = self.sys_displayhook_ori
186 # don't use the ipython crash handler so that user exceptions aren't
186 # don't use the ipython crash handler so that user exceptions aren't
187 # trapped
187 # trapped
188 sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors,
188 sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors,
189 mode = self.IP.rc.xmode,
189 mode = self.IP.rc.xmode,
190 call_pdb = self.IP.rc.pdb)
190 call_pdb = self.IP.rc.pdb)
191 self.restore_system_completer()
191 self.restore_system_completer()
192
192
193 def restore_system_completer(self):
193 def restore_system_completer(self):
194 """Restores the readline completer which was in place.
194 """Restores the readline completer which was in place.
195
195
196 This allows embedded IPython within IPython not to disrupt the
196 This allows embedded IPython within IPython not to disrupt the
197 parent's completion.
197 parent's completion.
198 """
198 """
199
199
200 try:
200 try:
201 self.IP.readline.set_completer(self.sys_ipcompleter_ori)
201 self.IP.readline.set_completer(self.sys_ipcompleter_ori)
202 sys.ipcompleter = self.sys_ipcompleter_ori
202 sys.ipcompleter = self.sys_ipcompleter_ori
203 except:
203 except:
204 pass
204 pass
205
205
206 def __call__(self,header='',local_ns=None,global_ns=None,dummy=None):
206 def __call__(self,header='',local_ns=None,global_ns=None,dummy=None):
207 """Activate the interactive interpreter.
207 """Activate the interactive interpreter.
208
208
209 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
209 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
210 the interpreter shell with the given local and global namespaces, and
210 the interpreter shell with the given local and global namespaces, and
211 optionally print a header string at startup.
211 optionally print a header string at startup.
212
212
213 The shell can be globally activated/deactivated using the
213 The shell can be globally activated/deactivated using the
214 set/get_dummy_mode methods. This allows you to turn off a shell used
214 set/get_dummy_mode methods. This allows you to turn off a shell used
215 for debugging globally.
215 for debugging globally.
216
216
217 However, *each* time you call the shell you can override the current
217 However, *each* time you call the shell you can override the current
218 state of dummy_mode with the optional keyword parameter 'dummy'. For
218 state of dummy_mode with the optional keyword parameter 'dummy'. For
219 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
219 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
220 can still have a specific call work by making it as IPShell(dummy=0).
220 can still have a specific call work by making it as IPShell(dummy=0).
221
221
222 The optional keyword parameter dummy controls whether the call
222 The optional keyword parameter dummy controls whether the call
223 actually does anything. """
223 actually does anything. """
224
224
225 # If the user has turned it off, go away
225 # If the user has turned it off, go away
226 if not self.IP.embedded_active:
226 if not self.IP.embedded_active:
227 return
227 return
228
228
229 # Normal exits from interactive mode set this flag, so the shell can't
229 # Normal exits from interactive mode set this flag, so the shell can't
230 # re-enter (it checks this variable at the start of interactive mode).
230 # re-enter (it checks this variable at the start of interactive mode).
231 self.IP.exit_now = False
231 self.IP.exit_now = False
232
232
233 # Allow the dummy parameter to override the global __dummy_mode
233 # Allow the dummy parameter to override the global __dummy_mode
234 if dummy or (dummy != 0 and self.__dummy_mode):
234 if dummy or (dummy != 0 and self.__dummy_mode):
235 return
235 return
236
236
237 # Set global subsystems (display,completions) to our values
237 # Set global subsystems (display,completions) to our values
238 sys.displayhook = self.sys_displayhook_embed
238 sys.displayhook = self.sys_displayhook_embed
239 if self.IP.has_readline:
239 if self.IP.has_readline:
240 self.IP.set_completer()
240 self.IP.set_completer()
241
241
242 if self.banner and header:
242 if self.banner and header:
243 format = '%s\n%s\n'
243 format = '%s\n%s\n'
244 else:
244 else:
245 format = '%s%s\n'
245 format = '%s%s\n'
246 banner = format % (self.banner,header)
246 banner = format % (self.banner,header)
247
247
248 # Call the embedding code with a stack depth of 1 so it can skip over
248 # Call the embedding code with a stack depth of 1 so it can skip over
249 # our call and get the original caller's namespaces.
249 # our call and get the original caller's namespaces.
250 self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1)
250 self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1)
251
251
252 if self.exit_msg:
252 if self.exit_msg:
253 print self.exit_msg
253 print self.exit_msg
254
254
255 # Restore global systems (display, completion)
255 # Restore global systems (display, completion)
256 sys.displayhook = self.sys_displayhook_ori
256 sys.displayhook = self.sys_displayhook_ori
257 self.restore_system_completer()
257 self.restore_system_completer()
258
258
259 def set_dummy_mode(self,dummy):
259 def set_dummy_mode(self,dummy):
260 """Sets the embeddable shell's dummy mode parameter.
260 """Sets the embeddable shell's dummy mode parameter.
261
261
262 set_dummy_mode(dummy): dummy = 0 or 1.
262 set_dummy_mode(dummy): dummy = 0 or 1.
263
263
264 This parameter is persistent and makes calls to the embeddable shell
264 This parameter is persistent and makes calls to the embeddable shell
265 silently return without performing any action. This allows you to
265 silently return without performing any action. This allows you to
266 globally activate or deactivate a shell you're using with a single call.
266 globally activate or deactivate a shell you're using with a single call.
267
267
268 If you need to manually"""
268 If you need to manually"""
269
269
270 if dummy not in [0,1,False,True]:
270 if dummy not in [0,1,False,True]:
271 raise ValueError,'dummy parameter must be boolean'
271 raise ValueError,'dummy parameter must be boolean'
272 self.__dummy_mode = dummy
272 self.__dummy_mode = dummy
273
273
274 def get_dummy_mode(self):
274 def get_dummy_mode(self):
275 """Return the current value of the dummy mode parameter.
275 """Return the current value of the dummy mode parameter.
276 """
276 """
277 return self.__dummy_mode
277 return self.__dummy_mode
278
278
279 def set_banner(self,banner):
279 def set_banner(self,banner):
280 """Sets the global banner.
280 """Sets the global banner.
281
281
282 This banner gets prepended to every header printed when the shell
282 This banner gets prepended to every header printed when the shell
283 instance is called."""
283 instance is called."""
284
284
285 self.banner = banner
285 self.banner = banner
286
286
287 def set_exit_msg(self,exit_msg):
287 def set_exit_msg(self,exit_msg):
288 """Sets the global exit_msg.
288 """Sets the global exit_msg.
289
289
290 This exit message gets printed upon exiting every time the embedded
290 This exit message gets printed upon exiting every time the embedded
291 shell is called. It is None by default. """
291 shell is called. It is None by default. """
292
292
293 self.exit_msg = exit_msg
293 self.exit_msg = exit_msg
294
294
295 #-----------------------------------------------------------------------------
295 #-----------------------------------------------------------------------------
296 if HAS_CTYPES:
296 if HAS_CTYPES:
297 # Add async exception support. Trick taken from:
297 # Add async exception support. Trick taken from:
298 # http://sebulba.wikispaces.com/recipe+thread2
298 # http://sebulba.wikispaces.com/recipe+thread2
299 def _async_raise(tid, exctype):
299 def _async_raise(tid, exctype):
300 """raises the exception, performs cleanup if needed"""
300 """raises the exception, performs cleanup if needed"""
301 if not inspect.isclass(exctype):
301 if not inspect.isclass(exctype):
302 raise TypeError("Only types can be raised (not instances)")
302 raise TypeError("Only types can be raised (not instances)")
303 res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid,
303 res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid,
304 ctypes.py_object(exctype))
304 ctypes.py_object(exctype))
305 if res == 0:
305 if res == 0:
306 raise ValueError("invalid thread id")
306 raise ValueError("invalid thread id")
307 elif res != 1:
307 elif res != 1:
308 # """if it returns a number greater than one, you're in trouble,
308 # """if it returns a number greater than one, you're in trouble,
309 # and you should call it again with exc=NULL to revert the effect"""
309 # and you should call it again with exc=NULL to revert the effect"""
310 ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
310 ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
311 raise SystemError("PyThreadState_SetAsyncExc failed")
311 raise SystemError("PyThreadState_SetAsyncExc failed")
312
312
313 def sigint_handler (signum,stack_frame):
313 def sigint_handler (signum,stack_frame):
314 """Sigint handler for threaded apps.
314 """Sigint handler for threaded apps.
315
315
316 This is a horrible hack to pass information about SIGINT _without_
316 This is a horrible hack to pass information about SIGINT _without_
317 using exceptions, since I haven't been able to properly manage
317 using exceptions, since I haven't been able to properly manage
318 cross-thread exceptions in GTK/WX. In fact, I don't think it can be
318 cross-thread exceptions in GTK/WX. In fact, I don't think it can be
319 done (or at least that's my understanding from a c.l.py thread where
319 done (or at least that's my understanding from a c.l.py thread where
320 this was discussed)."""
320 this was discussed)."""
321
321
322 global KBINT
322 global KBINT
323
323
324 if CODE_RUN:
324 if CODE_RUN:
325 _async_raise(MAIN_THREAD_ID,KeyboardInterrupt)
325 _async_raise(MAIN_THREAD_ID,KeyboardInterrupt)
326 else:
326 else:
327 KBINT = True
327 KBINT = True
328 print '\nKeyboardInterrupt - Press <Enter> to continue.',
328 print '\nKeyboardInterrupt - Press <Enter> to continue.',
329 Term.cout.flush()
329 Term.cout.flush()
330
330
331 else:
331 else:
332 def sigint_handler (signum,stack_frame):
332 def sigint_handler (signum,stack_frame):
333 """Sigint handler for threaded apps.
333 """Sigint handler for threaded apps.
334
334
335 This is a horrible hack to pass information about SIGINT _without_
335 This is a horrible hack to pass information about SIGINT _without_
336 using exceptions, since I haven't been able to properly manage
336 using exceptions, since I haven't been able to properly manage
337 cross-thread exceptions in GTK/WX. In fact, I don't think it can be
337 cross-thread exceptions in GTK/WX. In fact, I don't think it can be
338 done (or at least that's my understanding from a c.l.py thread where
338 done (or at least that's my understanding from a c.l.py thread where
339 this was discussed)."""
339 this was discussed)."""
340
340
341 global KBINT
341 global KBINT
342
342
343 print '\nKeyboardInterrupt - Press <Enter> to continue.',
343 print '\nKeyboardInterrupt - Press <Enter> to continue.',
344 Term.cout.flush()
344 Term.cout.flush()
345 # Set global flag so that runsource can know that Ctrl-C was hit
345 # Set global flag so that runsource can know that Ctrl-C was hit
346 KBINT = True
346 KBINT = True
347
347
348
348
349 class MTInteractiveShell(InteractiveShell):
349 class MTInteractiveShell(InteractiveShell):
350 """Simple multi-threaded shell."""
350 """Simple multi-threaded shell."""
351
351
352 # Threading strategy taken from:
352 # Threading strategy taken from:
353 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian
353 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian
354 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
354 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
355 # from the pygtk mailing list, to avoid lockups with system calls.
355 # from the pygtk mailing list, to avoid lockups with system calls.
356
356
357 # class attribute to indicate whether the class supports threads or not.
357 # class attribute to indicate whether the class supports threads or not.
358 # Subclasses with thread support should override this as needed.
358 # Subclasses with thread support should override this as needed.
359 isthreaded = True
359 isthreaded = True
360
360
361 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
361 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
362 user_ns=None,user_global_ns=None,banner2='',**kw):
362 user_ns=None,user_global_ns=None,banner2='',**kw):
363 """Similar to the normal InteractiveShell, but with threading control"""
363 """Similar to the normal InteractiveShell, but with threading control"""
364
364
365 InteractiveShell.__init__(self,name,usage,rc,user_ns,
365 InteractiveShell.__init__(self,name,usage,rc,user_ns,
366 user_global_ns,banner2)
366 user_global_ns,banner2)
367
367
368
368
369 # A queue to hold the code to be executed.
369 # A queue to hold the code to be executed.
370 self.code_queue = Queue.Queue()
370 self.code_queue = Queue.Queue()
371
371
372 # Stuff to do at closing time
372 # Stuff to do at closing time
373 self._kill = None
373 self._kill = None
374 on_kill = kw.get('on_kill', [])
374 on_kill = kw.get('on_kill', [])
375 # Check that all things to kill are callable:
375 # Check that all things to kill are callable:
376 for t in on_kill:
376 for t in on_kill:
377 if not callable(t):
377 if not callable(t):
378 raise TypeError,'on_kill must be a list of callables'
378 raise TypeError,'on_kill must be a list of callables'
379 self.on_kill = on_kill
379 self.on_kill = on_kill
380 # thread identity of the "worker thread" (that may execute code directly)
380 # thread identity of the "worker thread" (that may execute code directly)
381 self.worker_ident = None
381 self.worker_ident = None
382
382
383 def runsource(self, source, filename="<input>", symbol="single"):
383 def runsource(self, source, filename="<input>", symbol="single"):
384 """Compile and run some source in the interpreter.
384 """Compile and run some source in the interpreter.
385
385
386 Modified version of code.py's runsource(), to handle threading issues.
386 Modified version of code.py's runsource(), to handle threading issues.
387 See the original for full docstring details."""
387 See the original for full docstring details."""
388
388
389 global KBINT
389 global KBINT
390
390
391 # If Ctrl-C was typed, we reset the flag and return right away
391 # If Ctrl-C was typed, we reset the flag and return right away
392 if KBINT:
392 if KBINT:
393 KBINT = False
393 KBINT = False
394 return False
394 return False
395
395
396 if self._kill:
396 if self._kill:
397 # can't queue new code if we are being killed
397 # can't queue new code if we are being killed
398 return True
398 return True
399
399
400 try:
400 try:
401 code = self.compile(source, filename, symbol)
401 code = self.compile(source, filename, symbol)
402 except (OverflowError, SyntaxError, ValueError):
402 except (OverflowError, SyntaxError, ValueError):
403 # Case 1
403 # Case 1
404 self.showsyntaxerror(filename)
404 self.showsyntaxerror(filename)
405 return False
405 return False
406
406
407 if code is None:
407 if code is None:
408 # Case 2
408 # Case 2
409 return True
409 return True
410
410
411 # shortcut - if we are in worker thread, or the worker thread is not running,
411 # shortcut - if we are in worker thread, or the worker thread is not running,
412 # execute directly (to allow recursion and prevent deadlock if code is run early
412 # execute directly (to allow recursion and prevent deadlock if code is run early
413 # in IPython construction)
413 # in IPython construction)
414
414
415 if (self.worker_ident is None or self.worker_ident == thread.get_ident()):
415 if (self.worker_ident is None or self.worker_ident == thread.get_ident()):
416 InteractiveShell.runcode(self,code)
416 InteractiveShell.runcode(self,code)
417 return
417 return
418
418
419 # Case 3
419 # Case 3
420 # Store code in queue, so the execution thread can handle it.
420 # Store code in queue, so the execution thread can handle it.
421
421
422 completed_ev, received_ev = threading.Event(), threading.Event()
422 completed_ev, received_ev = threading.Event(), threading.Event()
423
423
424 self.code_queue.put((code,completed_ev, received_ev))
424 self.code_queue.put((code,completed_ev, received_ev))
425 # first make sure the message was received, with timeout
425 # first make sure the message was received, with timeout
426 received_ev.wait(5)
426 received_ev.wait(5)
427 if not received_ev.isSet():
427 if not received_ev.isSet():
428 # the mainloop is dead, start executing code directly
428 # the mainloop is dead, start executing code directly
429 print "Warning: Timeout for mainloop thread exceeded"
429 print "Warning: Timeout for mainloop thread exceeded"
430 print "switching to nonthreaded mode (until mainloop wakes up again)"
430 print "switching to nonthreaded mode (until mainloop wakes up again)"
431 self.worker_ident = None
431 self.worker_ident = None
432 else:
432 else:
433 completed_ev.wait()
433 completed_ev.wait()
434 return False
434 return False
435
435
436 def runcode(self):
436 def runcode(self):
437 """Execute a code object.
437 """Execute a code object.
438
438
439 Multithreaded wrapper around IPython's runcode()."""
439 Multithreaded wrapper around IPython's runcode()."""
440
440
441 global CODE_RUN
441 global CODE_RUN
442
442
443 # we are in worker thread, stash out the id for runsource()
443 # we are in worker thread, stash out the id for runsource()
444 self.worker_ident = thread.get_ident()
444 self.worker_ident = thread.get_ident()
445
445
446 if self._kill:
446 if self._kill:
447 print >>Term.cout, 'Closing threads...',
447 print >>Term.cout, 'Closing threads...',
448 Term.cout.flush()
448 Term.cout.flush()
449 for tokill in self.on_kill:
449 for tokill in self.on_kill:
450 tokill()
450 tokill()
451 print >>Term.cout, 'Done.'
451 print >>Term.cout, 'Done.'
452 # allow kill() to return
452 # allow kill() to return
453 self._kill.set()
453 self._kill.set()
454 return True
454 return True
455
455
456 # Install sigint handler. We do it every time to ensure that if user
456 # Install sigint handler. We do it every time to ensure that if user
457 # code modifies it, we restore our own handling.
457 # code modifies it, we restore our own handling.
458 try:
458 try:
459 signal(SIGINT,sigint_handler)
459 signal(SIGINT,sigint_handler)
460 except SystemError:
460 except SystemError:
461 # This happens under Windows, which seems to have all sorts
461 # This happens under Windows, which seems to have all sorts
462 # of problems with signal handling. Oh well...
462 # of problems with signal handling. Oh well...
463 pass
463 pass
464
464
465 # Flush queue of pending code by calling the run methood of the parent
465 # Flush queue of pending code by calling the run methood of the parent
466 # class with all items which may be in the queue.
466 # class with all items which may be in the queue.
467 code_to_run = None
467 code_to_run = None
468 while 1:
468 while 1:
469 try:
469 try:
470 code_to_run, completed_ev, received_ev = self.code_queue.get_nowait()
470 code_to_run, completed_ev, received_ev = self.code_queue.get_nowait()
471 except Queue.Empty:
471 except Queue.Empty:
472 break
472 break
473 received_ev.set()
473 received_ev.set()
474
474
475 # Exceptions need to be raised differently depending on which
475 # Exceptions need to be raised differently depending on which
476 # thread is active. This convoluted try/except is only there to
476 # thread is active. This convoluted try/except is only there to
477 # protect against asynchronous exceptions, to ensure that a KBINT
477 # protect against asynchronous exceptions, to ensure that a KBINT
478 # at the wrong time doesn't deadlock everything. The global
478 # at the wrong time doesn't deadlock everything. The global
479 # CODE_TO_RUN is set to true/false as close as possible to the
479 # CODE_TO_RUN is set to true/false as close as possible to the
480 # runcode() call, so that the KBINT handler is correctly informed.
480 # runcode() call, so that the KBINT handler is correctly informed.
481 try:
481 try:
482 try:
482 try:
483 CODE_RUN = True
483 CODE_RUN = True
484 InteractiveShell.runcode(self,code_to_run)
484 InteractiveShell.runcode(self,code_to_run)
485 except KeyboardInterrupt:
485 except KeyboardInterrupt:
486 print "Keyboard interrupted in mainloop"
486 print "Keyboard interrupted in mainloop"
487 while not self.code_queue.empty():
487 while not self.code_queue.empty():
488 code, ev1,ev2 = self.code_queue.get_nowait()
488 code, ev1,ev2 = self.code_queue.get_nowait()
489 ev1.set()
489 ev1.set()
490 ev2.set()
490 ev2.set()
491 break
491 break
492 finally:
492 finally:
493 CODE_RUN = False
493 CODE_RUN = False
494 # allow runsource() return from wait
494 # allow runsource() return from wait
495 completed_ev.set()
495 completed_ev.set()
496
496
497
497
498 # This MUST return true for gtk threading to work
498 # This MUST return true for gtk threading to work
499 return True
499 return True
500
500
501 def kill(self):
501 def kill(self):
502 """Kill the thread, returning when it has been shut down."""
502 """Kill the thread, returning when it has been shut down."""
503 self._kill = threading.Event()
503 self._kill = threading.Event()
504 self._kill.wait()
504 self._kill.wait()
505
505
506 class MatplotlibShellBase:
506 class MatplotlibShellBase:
507 """Mixin class to provide the necessary modifications to regular IPython
507 """Mixin class to provide the necessary modifications to regular IPython
508 shell classes for matplotlib support.
508 shell classes for matplotlib support.
509
509
510 Given Python's MRO, this should be used as the FIRST class in the
510 Given Python's MRO, this should be used as the FIRST class in the
511 inheritance hierarchy, so that it overrides the relevant methods."""
511 inheritance hierarchy, so that it overrides the relevant methods."""
512
512
513 def _matplotlib_config(self,name,user_ns,user_global_ns=None):
513 def _matplotlib_config(self,name,user_ns,user_global_ns=None):
514 """Return items needed to setup the user's shell with matplotlib"""
514 """Return items needed to setup the user's shell with matplotlib"""
515
515
516 # Initialize matplotlib to interactive mode always
516 # Initialize matplotlib to interactive mode always
517 import matplotlib
517 import matplotlib
518 from matplotlib import backends
518 from matplotlib import backends
519 matplotlib.interactive(True)
519 matplotlib.interactive(True)
520
520
521 def use(arg):
521 def use(arg):
522 """IPython wrapper for matplotlib's backend switcher.
522 """IPython wrapper for matplotlib's backend switcher.
523
523
524 In interactive use, we can not allow switching to a different
524 In interactive use, we can not allow switching to a different
525 interactive backend, since thread conflicts will most likely crash
525 interactive backend, since thread conflicts will most likely crash
526 the python interpreter. This routine does a safety check first,
526 the python interpreter. This routine does a safety check first,
527 and refuses to perform a dangerous switch. It still allows
527 and refuses to perform a dangerous switch. It still allows
528 switching to non-interactive backends."""
528 switching to non-interactive backends."""
529
529
530 if arg in backends.interactive_bk and arg != self.mpl_backend:
530 if arg in backends.interactive_bk and arg != self.mpl_backend:
531 m=('invalid matplotlib backend switch.\n'
531 m=('invalid matplotlib backend switch.\n'
532 'This script attempted to switch to the interactive '
532 'This script attempted to switch to the interactive '
533 'backend: `%s`\n'
533 'backend: `%s`\n'
534 'Your current choice of interactive backend is: `%s`\n\n'
534 'Your current choice of interactive backend is: `%s`\n\n'
535 'Switching interactive matplotlib backends at runtime\n'
535 'Switching interactive matplotlib backends at runtime\n'
536 'would crash the python interpreter, '
536 'would crash the python interpreter, '
537 'and IPython has blocked it.\n\n'
537 'and IPython has blocked it.\n\n'
538 'You need to either change your choice of matplotlib backend\n'
538 'You need to either change your choice of matplotlib backend\n'
539 'by editing your .matplotlibrc file, or run this script as a \n'
539 'by editing your .matplotlibrc file, or run this script as a \n'
540 'standalone file from the command line, not using IPython.\n' %
540 'standalone file from the command line, not using IPython.\n' %
541 (arg,self.mpl_backend) )
541 (arg,self.mpl_backend) )
542 raise RuntimeError, m
542 raise RuntimeError, m
543 else:
543 else:
544 self.mpl_use(arg)
544 self.mpl_use(arg)
545 self.mpl_use._called = True
545 self.mpl_use._called = True
546
546
547 self.matplotlib = matplotlib
547 self.matplotlib = matplotlib
548 self.mpl_backend = matplotlib.rcParams['backend']
548 self.mpl_backend = matplotlib.rcParams['backend']
549
549
550 # we also need to block switching of interactive backends by use()
550 # we also need to block switching of interactive backends by use()
551 self.mpl_use = matplotlib.use
551 self.mpl_use = matplotlib.use
552 self.mpl_use._called = False
552 self.mpl_use._called = False
553 # overwrite the original matplotlib.use with our wrapper
553 # overwrite the original matplotlib.use with our wrapper
554 matplotlib.use = use
554 matplotlib.use = use
555
555
556 # This must be imported last in the matplotlib series, after
556 # This must be imported last in the matplotlib series, after
557 # backend/interactivity choices have been made
557 # backend/interactivity choices have been made
558 import matplotlib.pylab as pylab
558 import matplotlib.pylab as pylab
559 self.pylab = pylab
559 self.pylab = pylab
560
560
561 self.pylab.show._needmain = False
561 self.pylab.show._needmain = False
562 # We need to detect at runtime whether show() is called by the user.
562 # We need to detect at runtime whether show() is called by the user.
563 # For this, we wrap it into a decorator which adds a 'called' flag.
563 # For this, we wrap it into a decorator which adds a 'called' flag.
564 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
564 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
565
565
566 # Build a user namespace initialized with matplotlib/matlab features.
566 # Build a user namespace initialized with matplotlib/matlab features.
567 user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns,
567 user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns,
568 user_global_ns)
568 user_global_ns)
569
569
570 # Import numpy as np/pyplot as plt are conventions we're trying to
570 # Import numpy as np/pyplot as plt are conventions we're trying to
571 # somewhat standardize on. Making them available to users by default
571 # somewhat standardize on. Making them available to users by default
572 # will greatly help this.
572 # will greatly help this.
573 exec ("import numpy\n"
573 exec ("import numpy\n"
574 "import numpy as np\n"
574 "import numpy as np\n"
575 "import matplotlib\n"
575 "import matplotlib\n"
576 "import matplotlib.pylab as pylab\n"
576 "import matplotlib.pylab as pylab\n"
577 "try:\n"
577 "try:\n"
578 " import matplotlib.pyplot as plt\n"
578 " import matplotlib.pyplot as plt\n"
579 "except ImportError:\n"
579 "except ImportError:\n"
580 " pass\n"
580 " pass\n"
581 ) in user_ns
581 ) in user_ns
582
582
583 # Build matplotlib info banner
583 # Build matplotlib info banner
584 b="""
584 b="""
585 Welcome to pylab, a matplotlib-based Python environment.
585 Welcome to pylab, a matplotlib-based Python environment.
586 For more information, type 'help(pylab)'.
586 For more information, type 'help(pylab)'.
587 """
587 """
588 return user_ns,,user_global_ns,b
588 return user_ns,user_global_ns,b
589
589
590 def mplot_exec(self,fname,*where,**kw):
590 def mplot_exec(self,fname,*where,**kw):
591 """Execute a matplotlib script.
591 """Execute a matplotlib script.
592
592
593 This is a call to execfile(), but wrapped in safeties to properly
593 This is a call to execfile(), but wrapped in safeties to properly
594 handle interactive rendering and backend switching."""
594 handle interactive rendering and backend switching."""
595
595
596 #print '*** Matplotlib runner ***' # dbg
596 #print '*** Matplotlib runner ***' # dbg
597 # turn off rendering until end of script
597 # turn off rendering until end of script
598 isInteractive = self.matplotlib.rcParams['interactive']
598 isInteractive = self.matplotlib.rcParams['interactive']
599 self.matplotlib.interactive(False)
599 self.matplotlib.interactive(False)
600 self.safe_execfile(fname,*where,**kw)
600 self.safe_execfile(fname,*where,**kw)
601 self.matplotlib.interactive(isInteractive)
601 self.matplotlib.interactive(isInteractive)
602 # make rendering call now, if the user tried to do it
602 # make rendering call now, if the user tried to do it
603 if self.pylab.draw_if_interactive.called:
603 if self.pylab.draw_if_interactive.called:
604 self.pylab.draw()
604 self.pylab.draw()
605 self.pylab.draw_if_interactive.called = False
605 self.pylab.draw_if_interactive.called = False
606
606
607 # if a backend switch was performed, reverse it now
607 # if a backend switch was performed, reverse it now
608 if self.mpl_use._called:
608 if self.mpl_use._called:
609 self.matplotlib.rcParams['backend'] = self.mpl_backend
609 self.matplotlib.rcParams['backend'] = self.mpl_backend
610
610
611 def magic_run(self,parameter_s=''):
611 def magic_run(self,parameter_s=''):
612 Magic.magic_run(self,parameter_s,runner=self.mplot_exec)
612 Magic.magic_run(self,parameter_s,runner=self.mplot_exec)
613
613
614 # Fix the docstring so users see the original as well
614 # Fix the docstring so users see the original as well
615 magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__,
615 magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__,
616 "\n *** Modified %run for Matplotlib,"
616 "\n *** Modified %run for Matplotlib,"
617 " with proper interactive handling ***")
617 " with proper interactive handling ***")
618
618
619 # Now we provide 2 versions of a matplotlib-aware IPython base shells, single
619 # Now we provide 2 versions of a matplotlib-aware IPython base shells, single
620 # and multithreaded. Note that these are meant for internal use, the IPShell*
620 # and multithreaded. Note that these are meant for internal use, the IPShell*
621 # classes below are the ones meant for public consumption.
621 # classes below are the ones meant for public consumption.
622
622
623 class MatplotlibShell(MatplotlibShellBase,InteractiveShell):
623 class MatplotlibShell(MatplotlibShellBase,InteractiveShell):
624 """Single-threaded shell with matplotlib support."""
624 """Single-threaded shell with matplotlib support."""
625
625
626 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
626 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
627 user_ns=None,user_global_ns=None,**kw):
627 user_ns=None,user_global_ns=None,**kw):
628 user_ns,user_global_ns,b2 = self._matplotlib_config(name,user_ns,user_global_ns)
628 user_ns,user_global_ns,b2 = self._matplotlib_config(name,user_ns,user_global_ns)
629 InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
629 InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
630 banner2=b2,**kw)
630 banner2=b2,**kw)
631
631
632 class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell):
632 class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell):
633 """Multi-threaded shell with matplotlib support."""
633 """Multi-threaded shell with matplotlib support."""
634
634
635 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
635 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
636 user_ns=None,user_global_ns=None, **kw):
636 user_ns=None,user_global_ns=None, **kw):
637 user_ns,b2 = self._matplotlib_config(name,user_ns)
637 user_ns,b2 = self._matplotlib_config(name,user_ns)
638 MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
638 MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
639 banner2=b2,**kw)
639 banner2=b2,**kw)
640
640
641 #-----------------------------------------------------------------------------
641 #-----------------------------------------------------------------------------
642 # Utility functions for the different GUI enabled IPShell* classes.
642 # Utility functions for the different GUI enabled IPShell* classes.
643
643
644 def get_tk():
644 def get_tk():
645 """Tries to import Tkinter and returns a withdrawn Tkinter root
645 """Tries to import Tkinter and returns a withdrawn Tkinter root
646 window. If Tkinter is already imported or not available, this
646 window. If Tkinter is already imported or not available, this
647 returns None. This function calls `hijack_tk` underneath.
647 returns None. This function calls `hijack_tk` underneath.
648 """
648 """
649 if not USE_TK or sys.modules.has_key('Tkinter'):
649 if not USE_TK or sys.modules.has_key('Tkinter'):
650 return None
650 return None
651 else:
651 else:
652 try:
652 try:
653 import Tkinter
653 import Tkinter
654 except ImportError:
654 except ImportError:
655 return None
655 return None
656 else:
656 else:
657 hijack_tk()
657 hijack_tk()
658 r = Tkinter.Tk()
658 r = Tkinter.Tk()
659 r.withdraw()
659 r.withdraw()
660 return r
660 return r
661
661
662 def hijack_tk():
662 def hijack_tk():
663 """Modifies Tkinter's mainloop with a dummy so when a module calls
663 """Modifies Tkinter's mainloop with a dummy so when a module calls
664 mainloop, it does not block.
664 mainloop, it does not block.
665
665
666 """
666 """
667 def misc_mainloop(self, n=0):
667 def misc_mainloop(self, n=0):
668 pass
668 pass
669 def tkinter_mainloop(n=0):
669 def tkinter_mainloop(n=0):
670 pass
670 pass
671
671
672 import Tkinter
672 import Tkinter
673 Tkinter.Misc.mainloop = misc_mainloop
673 Tkinter.Misc.mainloop = misc_mainloop
674 Tkinter.mainloop = tkinter_mainloop
674 Tkinter.mainloop = tkinter_mainloop
675
675
676 def update_tk(tk):
676 def update_tk(tk):
677 """Updates the Tkinter event loop. This is typically called from
677 """Updates the Tkinter event loop. This is typically called from
678 the respective WX or GTK mainloops.
678 the respective WX or GTK mainloops.
679 """
679 """
680 if tk:
680 if tk:
681 tk.update()
681 tk.update()
682
682
683 def hijack_wx():
683 def hijack_wx():
684 """Modifies wxPython's MainLoop with a dummy so user code does not
684 """Modifies wxPython's MainLoop with a dummy so user code does not
685 block IPython. The hijacked mainloop function is returned.
685 block IPython. The hijacked mainloop function is returned.
686 """
686 """
687 def dummy_mainloop(*args, **kw):
687 def dummy_mainloop(*args, **kw):
688 pass
688 pass
689
689
690 try:
690 try:
691 import wx
691 import wx
692 except ImportError:
692 except ImportError:
693 # For very old versions of WX
693 # For very old versions of WX
694 import wxPython as wx
694 import wxPython as wx
695
695
696 ver = wx.__version__
696 ver = wx.__version__
697 orig_mainloop = None
697 orig_mainloop = None
698 if ver[:3] >= '2.5':
698 if ver[:3] >= '2.5':
699 import wx
699 import wx
700 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
700 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
701 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
701 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
702 else: raise AttributeError('Could not find wx core module')
702 else: raise AttributeError('Could not find wx core module')
703 orig_mainloop = core.PyApp_MainLoop
703 orig_mainloop = core.PyApp_MainLoop
704 core.PyApp_MainLoop = dummy_mainloop
704 core.PyApp_MainLoop = dummy_mainloop
705 elif ver[:3] == '2.4':
705 elif ver[:3] == '2.4':
706 orig_mainloop = wx.wxc.wxPyApp_MainLoop
706 orig_mainloop = wx.wxc.wxPyApp_MainLoop
707 wx.wxc.wxPyApp_MainLoop = dummy_mainloop
707 wx.wxc.wxPyApp_MainLoop = dummy_mainloop
708 else:
708 else:
709 warn("Unable to find either wxPython version 2.4 or >= 2.5.")
709 warn("Unable to find either wxPython version 2.4 or >= 2.5.")
710 return orig_mainloop
710 return orig_mainloop
711
711
712 def hijack_gtk():
712 def hijack_gtk():
713 """Modifies pyGTK's mainloop with a dummy so user code does not
713 """Modifies pyGTK's mainloop with a dummy so user code does not
714 block IPython. This function returns the original `gtk.mainloop`
714 block IPython. This function returns the original `gtk.mainloop`
715 function that has been hijacked.
715 function that has been hijacked.
716 """
716 """
717 def dummy_mainloop(*args, **kw):
717 def dummy_mainloop(*args, **kw):
718 pass
718 pass
719 import gtk
719 import gtk
720 if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main
720 if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main
721 else: orig_mainloop = gtk.mainloop
721 else: orig_mainloop = gtk.mainloop
722 gtk.mainloop = dummy_mainloop
722 gtk.mainloop = dummy_mainloop
723 gtk.main = dummy_mainloop
723 gtk.main = dummy_mainloop
724 return orig_mainloop
724 return orig_mainloop
725
725
726 def hijack_qt():
726 def hijack_qt():
727 """Modifies PyQt's mainloop with a dummy so user code does not
727 """Modifies PyQt's mainloop with a dummy so user code does not
728 block IPython. This function returns the original
728 block IPython. This function returns the original
729 `qt.qApp.exec_loop` function that has been hijacked.
729 `qt.qApp.exec_loop` function that has been hijacked.
730 """
730 """
731 def dummy_mainloop(*args, **kw):
731 def dummy_mainloop(*args, **kw):
732 pass
732 pass
733 import qt
733 import qt
734 orig_mainloop = qt.qApp.exec_loop
734 orig_mainloop = qt.qApp.exec_loop
735 qt.qApp.exec_loop = dummy_mainloop
735 qt.qApp.exec_loop = dummy_mainloop
736 qt.QApplication.exec_loop = dummy_mainloop
736 qt.QApplication.exec_loop = dummy_mainloop
737 return orig_mainloop
737 return orig_mainloop
738
738
739 def hijack_qt4():
739 def hijack_qt4():
740 """Modifies PyQt4's mainloop with a dummy so user code does not
740 """Modifies PyQt4's mainloop with a dummy so user code does not
741 block IPython. This function returns the original
741 block IPython. This function returns the original
742 `QtGui.qApp.exec_` function that has been hijacked.
742 `QtGui.qApp.exec_` function that has been hijacked.
743 """
743 """
744 def dummy_mainloop(*args, **kw):
744 def dummy_mainloop(*args, **kw):
745 pass
745 pass
746 from PyQt4 import QtGui, QtCore
746 from PyQt4 import QtGui, QtCore
747 orig_mainloop = QtGui.qApp.exec_
747 orig_mainloop = QtGui.qApp.exec_
748 QtGui.qApp.exec_ = dummy_mainloop
748 QtGui.qApp.exec_ = dummy_mainloop
749 QtGui.QApplication.exec_ = dummy_mainloop
749 QtGui.QApplication.exec_ = dummy_mainloop
750 QtCore.QCoreApplication.exec_ = dummy_mainloop
750 QtCore.QCoreApplication.exec_ = dummy_mainloop
751 return orig_mainloop
751 return orig_mainloop
752
752
753 #-----------------------------------------------------------------------------
753 #-----------------------------------------------------------------------------
754 # The IPShell* classes below are the ones meant to be run by external code as
754 # The IPShell* classes below are the ones meant to be run by external code as
755 # IPython instances. Note that unless a specific threading strategy is
755 # IPython instances. Note that unless a specific threading strategy is
756 # desired, the factory function start() below should be used instead (it
756 # desired, the factory function start() below should be used instead (it
757 # selects the proper threaded class).
757 # selects the proper threaded class).
758
758
759 class IPThread(threading.Thread):
759 class IPThread(threading.Thread):
760 def run(self):
760 def run(self):
761 self.IP.mainloop(self._banner)
761 self.IP.mainloop(self._banner)
762 self.IP.kill()
762 self.IP.kill()
763
763
764 class IPShellGTK(IPThread):
764 class IPShellGTK(IPThread):
765 """Run a gtk mainloop() in a separate thread.
765 """Run a gtk mainloop() in a separate thread.
766
766
767 Python commands can be passed to the thread where they will be executed.
767 Python commands can be passed to the thread where they will be executed.
768 This is implemented by periodically checking for passed code using a
768 This is implemented by periodically checking for passed code using a
769 GTK timeout callback."""
769 GTK timeout callback."""
770
770
771 TIMEOUT = 100 # Millisecond interval between timeouts.
771 TIMEOUT = 100 # Millisecond interval between timeouts.
772
772
773 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
773 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
774 debug=1,shell_class=MTInteractiveShell):
774 debug=1,shell_class=MTInteractiveShell):
775
775
776 import gtk
776 import gtk
777
777
778 self.gtk = gtk
778 self.gtk = gtk
779 self.gtk_mainloop = hijack_gtk()
779 self.gtk_mainloop = hijack_gtk()
780
780
781 # Allows us to use both Tk and GTK.
781 # Allows us to use both Tk and GTK.
782 self.tk = get_tk()
782 self.tk = get_tk()
783
783
784 if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit
784 if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit
785 else: mainquit = self.gtk.mainquit
785 else: mainquit = self.gtk.mainquit
786
786
787 self.IP = make_IPython(argv,user_ns=user_ns,
787 self.IP = make_IPython(argv,user_ns=user_ns,
788 user_global_ns=user_global_ns,
788 user_global_ns=user_global_ns,
789 debug=debug,
789 debug=debug,
790 shell_class=shell_class,
790 shell_class=shell_class,
791 on_kill=[mainquit])
791 on_kill=[mainquit])
792
792
793 # HACK: slot for banner in self; it will be passed to the mainloop
793 # HACK: slot for banner in self; it will be passed to the mainloop
794 # method only and .run() needs it. The actual value will be set by
794 # method only and .run() needs it. The actual value will be set by
795 # .mainloop().
795 # .mainloop().
796 self._banner = None
796 self._banner = None
797
797
798 threading.Thread.__init__(self)
798 threading.Thread.__init__(self)
799
799
800 def mainloop(self,sys_exit=0,banner=None):
800 def mainloop(self,sys_exit=0,banner=None):
801
801
802 self._banner = banner
802 self._banner = banner
803
803
804 if self.gtk.pygtk_version >= (2,4,0):
804 if self.gtk.pygtk_version >= (2,4,0):
805 import gobject
805 import gobject
806 gobject.idle_add(self.on_timer)
806 gobject.idle_add(self.on_timer)
807 else:
807 else:
808 self.gtk.idle_add(self.on_timer)
808 self.gtk.idle_add(self.on_timer)
809
809
810 if sys.platform != 'win32':
810 if sys.platform != 'win32':
811 try:
811 try:
812 if self.gtk.gtk_version[0] >= 2:
812 if self.gtk.gtk_version[0] >= 2:
813 self.gtk.gdk.threads_init()
813 self.gtk.gdk.threads_init()
814 except AttributeError:
814 except AttributeError:
815 pass
815 pass
816 except RuntimeError:
816 except RuntimeError:
817 error('Your pyGTK likely has not been compiled with '
817 error('Your pyGTK likely has not been compiled with '
818 'threading support.\n'
818 'threading support.\n'
819 'The exception printout is below.\n'
819 'The exception printout is below.\n'
820 'You can either rebuild pyGTK with threads, or '
820 'You can either rebuild pyGTK with threads, or '
821 'try using \n'
821 'try using \n'
822 'matplotlib with a different backend (like Tk or WX).\n'
822 'matplotlib with a different backend (like Tk or WX).\n'
823 'Note that matplotlib will most likely not work in its '
823 'Note that matplotlib will most likely not work in its '
824 'current state!')
824 'current state!')
825 self.IP.InteractiveTB()
825 self.IP.InteractiveTB()
826
826
827 self.start()
827 self.start()
828 self.gtk.gdk.threads_enter()
828 self.gtk.gdk.threads_enter()
829 self.gtk_mainloop()
829 self.gtk_mainloop()
830 self.gtk.gdk.threads_leave()
830 self.gtk.gdk.threads_leave()
831 self.join()
831 self.join()
832
832
833 def on_timer(self):
833 def on_timer(self):
834 """Called when GTK is idle.
834 """Called when GTK is idle.
835
835
836 Must return True always, otherwise GTK stops calling it"""
836 Must return True always, otherwise GTK stops calling it"""
837
837
838 update_tk(self.tk)
838 update_tk(self.tk)
839 self.IP.runcode()
839 self.IP.runcode()
840 time.sleep(0.01)
840 time.sleep(0.01)
841 return True
841 return True
842
842
843
843
844 class IPShellWX(IPThread):
844 class IPShellWX(IPThread):
845 """Run a wx mainloop() in a separate thread.
845 """Run a wx mainloop() in a separate thread.
846
846
847 Python commands can be passed to the thread where they will be executed.
847 Python commands can be passed to the thread where they will be executed.
848 This is implemented by periodically checking for passed code using a
848 This is implemented by periodically checking for passed code using a
849 GTK timeout callback."""
849 GTK timeout callback."""
850
850
851 TIMEOUT = 100 # Millisecond interval between timeouts.
851 TIMEOUT = 100 # Millisecond interval between timeouts.
852
852
853 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
853 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
854 debug=1,shell_class=MTInteractiveShell):
854 debug=1,shell_class=MTInteractiveShell):
855
855
856 self.IP = make_IPython(argv,user_ns=user_ns,
856 self.IP = make_IPython(argv,user_ns=user_ns,
857 user_global_ns=user_global_ns,
857 user_global_ns=user_global_ns,
858 debug=debug,
858 debug=debug,
859 shell_class=shell_class,
859 shell_class=shell_class,
860 on_kill=[self.wxexit])
860 on_kill=[self.wxexit])
861
861
862 wantedwxversion=self.IP.rc.wxversion
862 wantedwxversion=self.IP.rc.wxversion
863 if wantedwxversion!="0":
863 if wantedwxversion!="0":
864 try:
864 try:
865 import wxversion
865 import wxversion
866 except ImportError:
866 except ImportError:
867 error('The wxversion module is needed for WX version selection')
867 error('The wxversion module is needed for WX version selection')
868 else:
868 else:
869 try:
869 try:
870 wxversion.select(wantedwxversion)
870 wxversion.select(wantedwxversion)
871 except:
871 except:
872 self.IP.InteractiveTB()
872 self.IP.InteractiveTB()
873 error('Requested wxPython version %s could not be loaded' %
873 error('Requested wxPython version %s could not be loaded' %
874 wantedwxversion)
874 wantedwxversion)
875
875
876 import wx
876 import wx
877
877
878 threading.Thread.__init__(self)
878 threading.Thread.__init__(self)
879 self.wx = wx
879 self.wx = wx
880 self.wx_mainloop = hijack_wx()
880 self.wx_mainloop = hijack_wx()
881
881
882 # Allows us to use both Tk and GTK.
882 # Allows us to use both Tk and GTK.
883 self.tk = get_tk()
883 self.tk = get_tk()
884
884
885 # HACK: slot for banner in self; it will be passed to the mainloop
885 # HACK: slot for banner in self; it will be passed to the mainloop
886 # method only and .run() needs it. The actual value will be set by
886 # method only and .run() needs it. The actual value will be set by
887 # .mainloop().
887 # .mainloop().
888 self._banner = None
888 self._banner = None
889
889
890 self.app = None
890 self.app = None
891
891
892 def wxexit(self, *args):
892 def wxexit(self, *args):
893 if self.app is not None:
893 if self.app is not None:
894 self.app.agent.timer.Stop()
894 self.app.agent.timer.Stop()
895 self.app.ExitMainLoop()
895 self.app.ExitMainLoop()
896
896
897 def mainloop(self,sys_exit=0,banner=None):
897 def mainloop(self,sys_exit=0,banner=None):
898
898
899 self._banner = banner
899 self._banner = banner
900
900
901 self.start()
901 self.start()
902
902
903 class TimerAgent(self.wx.MiniFrame):
903 class TimerAgent(self.wx.MiniFrame):
904 wx = self.wx
904 wx = self.wx
905 IP = self.IP
905 IP = self.IP
906 tk = self.tk
906 tk = self.tk
907 def __init__(self, parent, interval):
907 def __init__(self, parent, interval):
908 style = self.wx.DEFAULT_FRAME_STYLE | self.wx.TINY_CAPTION_HORIZ
908 style = self.wx.DEFAULT_FRAME_STYLE | self.wx.TINY_CAPTION_HORIZ
909 self.wx.MiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200),
909 self.wx.MiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200),
910 size=(100, 100),style=style)
910 size=(100, 100),style=style)
911 self.Show(False)
911 self.Show(False)
912 self.interval = interval
912 self.interval = interval
913 self.timerId = self.wx.NewId()
913 self.timerId = self.wx.NewId()
914
914
915 def StartWork(self):
915 def StartWork(self):
916 self.timer = self.wx.Timer(self, self.timerId)
916 self.timer = self.wx.Timer(self, self.timerId)
917 self.wx.EVT_TIMER(self, self.timerId, self.OnTimer)
917 self.wx.EVT_TIMER(self, self.timerId, self.OnTimer)
918 self.timer.Start(self.interval)
918 self.timer.Start(self.interval)
919
919
920 def OnTimer(self, event):
920 def OnTimer(self, event):
921 update_tk(self.tk)
921 update_tk(self.tk)
922 self.IP.runcode()
922 self.IP.runcode()
923
923
924 class App(self.wx.App):
924 class App(self.wx.App):
925 wx = self.wx
925 wx = self.wx
926 TIMEOUT = self.TIMEOUT
926 TIMEOUT = self.TIMEOUT
927 def OnInit(self):
927 def OnInit(self):
928 'Create the main window and insert the custom frame'
928 'Create the main window and insert the custom frame'
929 self.agent = TimerAgent(None, self.TIMEOUT)
929 self.agent = TimerAgent(None, self.TIMEOUT)
930 self.agent.Show(False)
930 self.agent.Show(False)
931 self.agent.StartWork()
931 self.agent.StartWork()
932 return True
932 return True
933
933
934 self.app = App(redirect=False)
934 self.app = App(redirect=False)
935 self.wx_mainloop(self.app)
935 self.wx_mainloop(self.app)
936 self.join()
936 self.join()
937
937
938
938
939 class IPShellQt(IPThread):
939 class IPShellQt(IPThread):
940 """Run a Qt event loop in a separate thread.
940 """Run a Qt event loop in a separate thread.
941
941
942 Python commands can be passed to the thread where they will be executed.
942 Python commands can be passed to the thread where they will be executed.
943 This is implemented by periodically checking for passed code using a
943 This is implemented by periodically checking for passed code using a
944 Qt timer / slot."""
944 Qt timer / slot."""
945
945
946 TIMEOUT = 100 # Millisecond interval between timeouts.
946 TIMEOUT = 100 # Millisecond interval between timeouts.
947
947
948 def __init__(self, argv=None, user_ns=None, user_global_ns=None,
948 def __init__(self, argv=None, user_ns=None, user_global_ns=None,
949 debug=0, shell_class=MTInteractiveShell):
949 debug=0, shell_class=MTInteractiveShell):
950
950
951 import qt
951 import qt
952
952
953 self.exec_loop = hijack_qt()
953 self.exec_loop = hijack_qt()
954
954
955 # Allows us to use both Tk and QT.
955 # Allows us to use both Tk and QT.
956 self.tk = get_tk()
956 self.tk = get_tk()
957
957
958 self.IP = make_IPython(argv,
958 self.IP = make_IPython(argv,
959 user_ns=user_ns,
959 user_ns=user_ns,
960 user_global_ns=user_global_ns,
960 user_global_ns=user_global_ns,
961 debug=debug,
961 debug=debug,
962 shell_class=shell_class,
962 shell_class=shell_class,
963 on_kill=[qt.qApp.exit])
963 on_kill=[qt.qApp.exit])
964
964
965 # HACK: slot for banner in self; it will be passed to the mainloop
965 # HACK: slot for banner in self; it will be passed to the mainloop
966 # method only and .run() needs it. The actual value will be set by
966 # method only and .run() needs it. The actual value will be set by
967 # .mainloop().
967 # .mainloop().
968 self._banner = None
968 self._banner = None
969
969
970 threading.Thread.__init__(self)
970 threading.Thread.__init__(self)
971
971
972 def mainloop(self, sys_exit=0, banner=None):
972 def mainloop(self, sys_exit=0, banner=None):
973
973
974 import qt
974 import qt
975
975
976 self._banner = banner
976 self._banner = banner
977
977
978 if qt.QApplication.startingUp():
978 if qt.QApplication.startingUp():
979 a = qt.QApplication(sys.argv)
979 a = qt.QApplication(sys.argv)
980
980
981 self.timer = qt.QTimer()
981 self.timer = qt.QTimer()
982 qt.QObject.connect(self.timer,
982 qt.QObject.connect(self.timer,
983 qt.SIGNAL('timeout()'),
983 qt.SIGNAL('timeout()'),
984 self.on_timer)
984 self.on_timer)
985
985
986 self.start()
986 self.start()
987 self.timer.start(self.TIMEOUT, True)
987 self.timer.start(self.TIMEOUT, True)
988 while True:
988 while True:
989 if self.IP._kill: break
989 if self.IP._kill: break
990 self.exec_loop()
990 self.exec_loop()
991 self.join()
991 self.join()
992
992
993 def on_timer(self):
993 def on_timer(self):
994 update_tk(self.tk)
994 update_tk(self.tk)
995 result = self.IP.runcode()
995 result = self.IP.runcode()
996 self.timer.start(self.TIMEOUT, True)
996 self.timer.start(self.TIMEOUT, True)
997 return result
997 return result
998
998
999
999
1000 class IPShellQt4(IPThread):
1000 class IPShellQt4(IPThread):
1001 """Run a Qt event loop in a separate thread.
1001 """Run a Qt event loop in a separate thread.
1002
1002
1003 Python commands can be passed to the thread where they will be executed.
1003 Python commands can be passed to the thread where they will be executed.
1004 This is implemented by periodically checking for passed code using a
1004 This is implemented by periodically checking for passed code using a
1005 Qt timer / slot."""
1005 Qt timer / slot."""
1006
1006
1007 TIMEOUT = 100 # Millisecond interval between timeouts.
1007 TIMEOUT = 100 # Millisecond interval between timeouts.
1008
1008
1009 def __init__(self, argv=None, user_ns=None, user_global_ns=None,
1009 def __init__(self, argv=None, user_ns=None, user_global_ns=None,
1010 debug=0, shell_class=MTInteractiveShell):
1010 debug=0, shell_class=MTInteractiveShell):
1011
1011
1012 from PyQt4 import QtCore, QtGui
1012 from PyQt4 import QtCore, QtGui
1013
1013
1014 try:
1014 try:
1015 # present in PyQt4-4.2.1 or later
1015 # present in PyQt4-4.2.1 or later
1016 QtCore.pyqtRemoveInputHook()
1016 QtCore.pyqtRemoveInputHook()
1017 except AttributeError:
1017 except AttributeError:
1018 pass
1018 pass
1019
1019
1020 if QtCore.PYQT_VERSION_STR == '4.3':
1020 if QtCore.PYQT_VERSION_STR == '4.3':
1021 warn('''PyQt4 version 4.3 detected.
1021 warn('''PyQt4 version 4.3 detected.
1022 If you experience repeated threading warnings, please update PyQt4.
1022 If you experience repeated threading warnings, please update PyQt4.
1023 ''')
1023 ''')
1024
1024
1025 self.exec_ = hijack_qt4()
1025 self.exec_ = hijack_qt4()
1026
1026
1027 # Allows us to use both Tk and QT.
1027 # Allows us to use both Tk and QT.
1028 self.tk = get_tk()
1028 self.tk = get_tk()
1029
1029
1030 self.IP = make_IPython(argv,
1030 self.IP = make_IPython(argv,
1031 user_ns=user_ns,
1031 user_ns=user_ns,
1032 user_global_ns=user_global_ns,
1032 user_global_ns=user_global_ns,
1033 debug=debug,
1033 debug=debug,
1034 shell_class=shell_class,
1034 shell_class=shell_class,
1035 on_kill=[QtGui.qApp.exit])
1035 on_kill=[QtGui.qApp.exit])
1036
1036
1037 # HACK: slot for banner in self; it will be passed to the mainloop
1037 # HACK: slot for banner in self; it will be passed to the mainloop
1038 # method only and .run() needs it. The actual value will be set by
1038 # method only and .run() needs it. The actual value will be set by
1039 # .mainloop().
1039 # .mainloop().
1040 self._banner = None
1040 self._banner = None
1041
1041
1042 threading.Thread.__init__(self)
1042 threading.Thread.__init__(self)
1043
1043
1044 def mainloop(self, sys_exit=0, banner=None):
1044 def mainloop(self, sys_exit=0, banner=None):
1045
1045
1046 from PyQt4 import QtCore, QtGui
1046 from PyQt4 import QtCore, QtGui
1047
1047
1048 self._banner = banner
1048 self._banner = banner
1049
1049
1050 if QtGui.QApplication.startingUp():
1050 if QtGui.QApplication.startingUp():
1051 a = QtGui.QApplication(sys.argv)
1051 a = QtGui.QApplication(sys.argv)
1052
1052
1053 self.timer = QtCore.QTimer()
1053 self.timer = QtCore.QTimer()
1054 QtCore.QObject.connect(self.timer,
1054 QtCore.QObject.connect(self.timer,
1055 QtCore.SIGNAL('timeout()'),
1055 QtCore.SIGNAL('timeout()'),
1056 self.on_timer)
1056 self.on_timer)
1057
1057
1058 self.start()
1058 self.start()
1059 self.timer.start(self.TIMEOUT)
1059 self.timer.start(self.TIMEOUT)
1060 while True:
1060 while True:
1061 if self.IP._kill: break
1061 if self.IP._kill: break
1062 self.exec_()
1062 self.exec_()
1063 self.join()
1063 self.join()
1064
1064
1065 def on_timer(self):
1065 def on_timer(self):
1066 update_tk(self.tk)
1066 update_tk(self.tk)
1067 result = self.IP.runcode()
1067 result = self.IP.runcode()
1068 self.timer.start(self.TIMEOUT)
1068 self.timer.start(self.TIMEOUT)
1069 return result
1069 return result
1070
1070
1071
1071
1072 # A set of matplotlib public IPython shell classes, for single-threaded (Tk*
1072 # A set of matplotlib public IPython shell classes, for single-threaded (Tk*
1073 # and FLTK*) and multithreaded (GTK*, WX* and Qt*) backends to use.
1073 # and FLTK*) and multithreaded (GTK*, WX* and Qt*) backends to use.
1074 def _load_pylab(user_ns):
1074 def _load_pylab(user_ns):
1075 """Allow users to disable pulling all of pylab into the top-level
1075 """Allow users to disable pulling all of pylab into the top-level
1076 namespace.
1076 namespace.
1077
1077
1078 This little utility must be called AFTER the actual ipython instance is
1078 This little utility must be called AFTER the actual ipython instance is
1079 running, since only then will the options file have been fully parsed."""
1079 running, since only then will the options file have been fully parsed."""
1080
1080
1081 ip = IPython.ipapi.get()
1081 ip = IPython.ipapi.get()
1082 if ip.options.pylab_import_all:
1082 if ip.options.pylab_import_all:
1083 ip.ex("from matplotlib.pylab import *")
1083 ip.ex("from matplotlib.pylab import *")
1084 ip.IP.user_config_ns.update(ip.user_ns)
1084 ip.IP.user_config_ns.update(ip.user_ns)
1085
1085
1086
1086
1087 class IPShellMatplotlib(IPShell):
1087 class IPShellMatplotlib(IPShell):
1088 """Subclass IPShell with MatplotlibShell as the internal shell.
1088 """Subclass IPShell with MatplotlibShell as the internal shell.
1089
1089
1090 Single-threaded class, meant for the Tk* and FLTK* backends.
1090 Single-threaded class, meant for the Tk* and FLTK* backends.
1091
1091
1092 Having this on a separate class simplifies the external driver code."""
1092 Having this on a separate class simplifies the external driver code."""
1093
1093
1094 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1094 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1095 IPShell.__init__(self,argv,user_ns,user_global_ns,debug,
1095 IPShell.__init__(self,argv,user_ns,user_global_ns,debug,
1096 shell_class=MatplotlibShell)
1096 shell_class=MatplotlibShell)
1097 _load_pylab(self.IP.user_ns)
1097 _load_pylab(self.IP.user_ns)
1098
1098
1099 class IPShellMatplotlibGTK(IPShellGTK):
1099 class IPShellMatplotlibGTK(IPShellGTK):
1100 """Subclass IPShellGTK with MatplotlibMTShell as the internal shell.
1100 """Subclass IPShellGTK with MatplotlibMTShell as the internal shell.
1101
1101
1102 Multi-threaded class, meant for the GTK* backends."""
1102 Multi-threaded class, meant for the GTK* backends."""
1103
1103
1104 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1104 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1105 IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug,
1105 IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug,
1106 shell_class=MatplotlibMTShell)
1106 shell_class=MatplotlibMTShell)
1107 _load_pylab(self.IP.user_ns)
1107 _load_pylab(self.IP.user_ns)
1108
1108
1109 class IPShellMatplotlibWX(IPShellWX):
1109 class IPShellMatplotlibWX(IPShellWX):
1110 """Subclass IPShellWX with MatplotlibMTShell as the internal shell.
1110 """Subclass IPShellWX with MatplotlibMTShell as the internal shell.
1111
1111
1112 Multi-threaded class, meant for the WX* backends."""
1112 Multi-threaded class, meant for the WX* backends."""
1113
1113
1114 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1114 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1115 IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug,
1115 IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug,
1116 shell_class=MatplotlibMTShell)
1116 shell_class=MatplotlibMTShell)
1117 _load_pylab(self.IP.user_ns)
1117 _load_pylab(self.IP.user_ns)
1118
1118
1119 class IPShellMatplotlibQt(IPShellQt):
1119 class IPShellMatplotlibQt(IPShellQt):
1120 """Subclass IPShellQt with MatplotlibMTShell as the internal shell.
1120 """Subclass IPShellQt with MatplotlibMTShell as the internal shell.
1121
1121
1122 Multi-threaded class, meant for the Qt* backends."""
1122 Multi-threaded class, meant for the Qt* backends."""
1123
1123
1124 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1124 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1125 IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug,
1125 IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug,
1126 shell_class=MatplotlibMTShell)
1126 shell_class=MatplotlibMTShell)
1127 _load_pylab(self.IP.user_ns)
1127 _load_pylab(self.IP.user_ns)
1128
1128
1129 class IPShellMatplotlibQt4(IPShellQt4):
1129 class IPShellMatplotlibQt4(IPShellQt4):
1130 """Subclass IPShellQt4 with MatplotlibMTShell as the internal shell.
1130 """Subclass IPShellQt4 with MatplotlibMTShell as the internal shell.
1131
1131
1132 Multi-threaded class, meant for the Qt4* backends."""
1132 Multi-threaded class, meant for the Qt4* backends."""
1133
1133
1134 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1134 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1135 IPShellQt4.__init__(self,argv,user_ns,user_global_ns,debug,
1135 IPShellQt4.__init__(self,argv,user_ns,user_global_ns,debug,
1136 shell_class=MatplotlibMTShell)
1136 shell_class=MatplotlibMTShell)
1137 _load_pylab(self.IP.user_ns)
1137 _load_pylab(self.IP.user_ns)
1138
1138
1139 #-----------------------------------------------------------------------------
1139 #-----------------------------------------------------------------------------
1140 # Factory functions to actually start the proper thread-aware shell
1140 # Factory functions to actually start the proper thread-aware shell
1141
1141
1142 def _select_shell(argv):
1142 def _select_shell(argv):
1143 """Select a shell from the given argv vector.
1143 """Select a shell from the given argv vector.
1144
1144
1145 This function implements the threading selection policy, allowing runtime
1145 This function implements the threading selection policy, allowing runtime
1146 control of the threading mode, both for general users and for matplotlib.
1146 control of the threading mode, both for general users and for matplotlib.
1147
1147
1148 Return:
1148 Return:
1149 Shell class to be instantiated for runtime operation.
1149 Shell class to be instantiated for runtime operation.
1150 """
1150 """
1151
1151
1152 global USE_TK
1152 global USE_TK
1153
1153
1154 mpl_shell = {'gthread' : IPShellMatplotlibGTK,
1154 mpl_shell = {'gthread' : IPShellMatplotlibGTK,
1155 'wthread' : IPShellMatplotlibWX,
1155 'wthread' : IPShellMatplotlibWX,
1156 'qthread' : IPShellMatplotlibQt,
1156 'qthread' : IPShellMatplotlibQt,
1157 'q4thread' : IPShellMatplotlibQt4,
1157 'q4thread' : IPShellMatplotlibQt4,
1158 'tkthread' : IPShellMatplotlib, # Tk is built-in
1158 'tkthread' : IPShellMatplotlib, # Tk is built-in
1159 }
1159 }
1160
1160
1161 th_shell = {'gthread' : IPShellGTK,
1161 th_shell = {'gthread' : IPShellGTK,
1162 'wthread' : IPShellWX,
1162 'wthread' : IPShellWX,
1163 'qthread' : IPShellQt,
1163 'qthread' : IPShellQt,
1164 'q4thread' : IPShellQt4,
1164 'q4thread' : IPShellQt4,
1165 'tkthread' : IPShell, # Tk is built-in
1165 'tkthread' : IPShell, # Tk is built-in
1166 }
1166 }
1167
1167
1168 backends = {'gthread' : 'GTKAgg',
1168 backends = {'gthread' : 'GTKAgg',
1169 'wthread' : 'WXAgg',
1169 'wthread' : 'WXAgg',
1170 'qthread' : 'QtAgg',
1170 'qthread' : 'QtAgg',
1171 'q4thread' :'Qt4Agg',
1171 'q4thread' :'Qt4Agg',
1172 'tkthread' :'TkAgg',
1172 'tkthread' :'TkAgg',
1173 }
1173 }
1174
1174
1175 all_opts = set(['tk','pylab','gthread','qthread','q4thread','wthread',
1175 all_opts = set(['tk','pylab','gthread','qthread','q4thread','wthread',
1176 'tkthread'])
1176 'tkthread'])
1177 user_opts = set([s.replace('-','') for s in argv[:3]])
1177 user_opts = set([s.replace('-','') for s in argv[:3]])
1178 special_opts = user_opts & all_opts
1178 special_opts = user_opts & all_opts
1179
1179
1180 if 'tk' in special_opts:
1180 if 'tk' in special_opts:
1181 USE_TK = True
1181 USE_TK = True
1182 special_opts.remove('tk')
1182 special_opts.remove('tk')
1183
1183
1184 if 'pylab' in special_opts:
1184 if 'pylab' in special_opts:
1185
1185
1186 try:
1186 try:
1187 import matplotlib
1187 import matplotlib
1188 except ImportError:
1188 except ImportError:
1189 error('matplotlib could NOT be imported! Starting normal IPython.')
1189 error('matplotlib could NOT be imported! Starting normal IPython.')
1190 return IPShell
1190 return IPShell
1191
1191
1192 special_opts.remove('pylab')
1192 special_opts.remove('pylab')
1193 # If there's any option left, it means the user wants to force the
1193 # If there's any option left, it means the user wants to force the
1194 # threading backend, else it's auto-selected from the rc file
1194 # threading backend, else it's auto-selected from the rc file
1195 if special_opts:
1195 if special_opts:
1196 th_mode = special_opts.pop()
1196 th_mode = special_opts.pop()
1197 matplotlib.rcParams['backend'] = backends[th_mode]
1197 matplotlib.rcParams['backend'] = backends[th_mode]
1198 else:
1198 else:
1199 backend = matplotlib.rcParams['backend']
1199 backend = matplotlib.rcParams['backend']
1200 if backend.startswith('GTK'):
1200 if backend.startswith('GTK'):
1201 th_mode = 'gthread'
1201 th_mode = 'gthread'
1202 elif backend.startswith('WX'):
1202 elif backend.startswith('WX'):
1203 th_mode = 'wthread'
1203 th_mode = 'wthread'
1204 elif backend.startswith('Qt4'):
1204 elif backend.startswith('Qt4'):
1205 th_mode = 'q4thread'
1205 th_mode = 'q4thread'
1206 elif backend.startswith('Qt'):
1206 elif backend.startswith('Qt'):
1207 th_mode = 'qthread'
1207 th_mode = 'qthread'
1208 else:
1208 else:
1209 # Any other backend, use plain Tk
1209 # Any other backend, use plain Tk
1210 th_mode = 'tkthread'
1210 th_mode = 'tkthread'
1211
1211
1212 return mpl_shell[th_mode]
1212 return mpl_shell[th_mode]
1213 else:
1213 else:
1214 # No pylab requested, just plain threads
1214 # No pylab requested, just plain threads
1215 try:
1215 try:
1216 th_mode = special_opts.pop()
1216 th_mode = special_opts.pop()
1217 except KeyError:
1217 except KeyError:
1218 th_mode = 'tkthread'
1218 th_mode = 'tkthread'
1219 return th_shell[th_mode]
1219 return th_shell[th_mode]
1220
1220
1221
1221
1222 # This is the one which should be called by external code.
1222 # This is the one which should be called by external code.
1223 def start(user_ns = None):
1223 def start(user_ns = None):
1224 """Return a running shell instance, dealing with threading options.
1224 """Return a running shell instance, dealing with threading options.
1225
1225
1226 This is a factory function which will instantiate the proper IPython shell
1226 This is a factory function which will instantiate the proper IPython shell
1227 based on the user's threading choice. Such a selector is needed because
1227 based on the user's threading choice. Such a selector is needed because
1228 different GUI toolkits require different thread handling details."""
1228 different GUI toolkits require different thread handling details."""
1229
1229
1230 shell = _select_shell(sys.argv)
1230 shell = _select_shell(sys.argv)
1231 return shell(user_ns = user_ns)
1231 return shell(user_ns = user_ns)
1232
1232
1233 # Some aliases for backwards compatibility
1233 # Some aliases for backwards compatibility
1234 IPythonShell = IPShell
1234 IPythonShell = IPShell
1235 IPythonShellEmbed = IPShellEmbed
1235 IPythonShellEmbed = IPShellEmbed
1236 #************************ End of file <Shell.py> ***************************
1236 #************************ End of file <Shell.py> ***************************
General Comments 0
You need to be logged in to leave comments. Login now