Show More
@@ -1,956 +1,960 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 1 |
|
7 | $Id: Shell.py 1313 2006-05-19 17:48:41Z fperez $""" | |
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 | import __builtin__ |
|
21 | import __builtin__ | |
22 | import __main__ |
|
22 | import __main__ | |
23 | import Queue |
|
23 | import Queue | |
24 | import os |
|
24 | import os | |
25 | import signal |
|
25 | import signal | |
26 | import sys |
|
26 | import sys | |
27 | import threading |
|
27 | import threading | |
28 | import time |
|
28 | import time | |
29 |
|
29 | |||
30 | import IPython |
|
30 | import IPython | |
31 | from IPython import ultraTB |
|
31 | from IPython import ultraTB | |
32 | from IPython.genutils import Term,warn,error,flag_calls |
|
32 | from IPython.genutils import Term,warn,error,flag_calls | |
33 | from IPython.iplib import InteractiveShell |
|
33 | from IPython.iplib import InteractiveShell | |
34 | from IPython.ipmaker import make_IPython |
|
34 | from IPython.ipmaker import make_IPython | |
35 | from IPython.Magic import Magic |
|
35 | from IPython.Magic import Magic | |
36 | from IPython.ipstruct import Struct |
|
36 | from IPython.ipstruct import Struct | |
37 |
|
37 | |||
38 | # global flag to pass around information about Ctrl-C without exceptions |
|
38 | # global flag to pass around information about Ctrl-C without exceptions | |
39 | KBINT = False |
|
39 | KBINT = False | |
40 |
|
40 | |||
41 | # global flag to turn on/off Tk support. |
|
41 | # global flag to turn on/off Tk support. | |
42 | USE_TK = False |
|
42 | USE_TK = False | |
43 |
|
43 | |||
44 | #----------------------------------------------------------------------------- |
|
44 | #----------------------------------------------------------------------------- | |
45 | # This class is trivial now, but I want to have it in to publish a clean |
|
45 | # This class is trivial now, but I want to have it in to publish a clean | |
46 | # interface. Later when the internals are reorganized, code that uses this |
|
46 | # interface. Later when the internals are reorganized, code that uses this | |
47 | # shouldn't have to change. |
|
47 | # shouldn't have to change. | |
48 |
|
48 | |||
49 | class IPShell: |
|
49 | class IPShell: | |
50 | """Create an IPython instance.""" |
|
50 | """Create an IPython instance.""" | |
51 |
|
51 | |||
52 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
52 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, | |
53 | debug=1,shell_class=InteractiveShell): |
|
53 | debug=1,shell_class=InteractiveShell): | |
54 | self.IP = make_IPython(argv,user_ns=user_ns,user_global_ns=user_global_ns, |
|
54 | self.IP = make_IPython(argv,user_ns=user_ns,user_global_ns=user_global_ns, | |
55 | debug=debug,shell_class=shell_class) |
|
55 | debug=debug,shell_class=shell_class) | |
56 |
|
56 | |||
57 | def mainloop(self,sys_exit=0,banner=None): |
|
57 | def mainloop(self,sys_exit=0,banner=None): | |
58 | self.IP.mainloop(banner) |
|
58 | self.IP.mainloop(banner) | |
59 | if sys_exit: |
|
59 | if sys_exit: | |
60 | sys.exit() |
|
60 | sys.exit() | |
61 |
|
61 | |||
62 | #----------------------------------------------------------------------------- |
|
62 | #----------------------------------------------------------------------------- | |
63 | class IPShellEmbed: |
|
63 | class IPShellEmbed: | |
64 | """Allow embedding an IPython shell into a running program. |
|
64 | """Allow embedding an IPython shell into a running program. | |
65 |
|
65 | |||
66 | Instances of this class are callable, with the __call__ method being an |
|
66 | Instances of this class are callable, with the __call__ method being an | |
67 | alias to the embed() method of an InteractiveShell instance. |
|
67 | alias to the embed() method of an InteractiveShell instance. | |
68 |
|
68 | |||
69 | Usage (see also the example-embed.py file for a running example): |
|
69 | Usage (see also the example-embed.py file for a running example): | |
70 |
|
70 | |||
71 | ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override]) |
|
71 | ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override]) | |
72 |
|
72 | |||
73 | - argv: list containing valid command-line options for IPython, as they |
|
73 | - argv: list containing valid command-line options for IPython, as they | |
74 | would appear in sys.argv[1:]. |
|
74 | would appear in sys.argv[1:]. | |
75 |
|
75 | |||
76 | For example, the following command-line options: |
|
76 | For example, the following command-line options: | |
77 |
|
77 | |||
78 | $ ipython -prompt_in1 'Input <\\#>' -colors LightBG |
|
78 | $ ipython -prompt_in1 'Input <\\#>' -colors LightBG | |
79 |
|
79 | |||
80 | would be passed in the argv list as: |
|
80 | would be passed in the argv list as: | |
81 |
|
81 | |||
82 | ['-prompt_in1','Input <\\#>','-colors','LightBG'] |
|
82 | ['-prompt_in1','Input <\\#>','-colors','LightBG'] | |
83 |
|
83 | |||
84 | - banner: string which gets printed every time the interpreter starts. |
|
84 | - banner: string which gets printed every time the interpreter starts. | |
85 |
|
85 | |||
86 | - exit_msg: string which gets printed every time the interpreter exits. |
|
86 | - exit_msg: string which gets printed every time the interpreter exits. | |
87 |
|
87 | |||
88 | - rc_override: a dict or Struct of configuration options such as those |
|
88 | - rc_override: a dict or Struct of configuration options such as those | |
89 | used by IPython. These options are read from your ~/.ipython/ipythonrc |
|
89 | used by IPython. These options are read from your ~/.ipython/ipythonrc | |
90 | file when the Shell object is created. Passing an explicit rc_override |
|
90 | file when the Shell object is created. Passing an explicit rc_override | |
91 | dict with any options you want allows you to override those values at |
|
91 | dict with any options you want allows you to override those values at | |
92 | creation time without having to modify the file. This way you can create |
|
92 | creation time without having to modify the file. This way you can create | |
93 | embeddable instances configured in any way you want without editing any |
|
93 | embeddable instances configured in any way you want without editing any | |
94 | global files (thus keeping your interactive IPython configuration |
|
94 | global files (thus keeping your interactive IPython configuration | |
95 | unchanged). |
|
95 | unchanged). | |
96 |
|
96 | |||
97 | Then the ipshell instance can be called anywhere inside your code: |
|
97 | Then the ipshell instance can be called anywhere inside your code: | |
98 |
|
98 | |||
99 | ipshell(header='') -> Opens up an IPython shell. |
|
99 | ipshell(header='') -> Opens up an IPython shell. | |
100 |
|
100 | |||
101 | - header: string printed by the IPython shell upon startup. This can let |
|
101 | - header: string printed by the IPython shell upon startup. This can let | |
102 | you know where in your code you are when dropping into the shell. Note |
|
102 | you know where in your code you are when dropping into the shell. Note | |
103 | that 'banner' gets prepended to all calls, so header is used for |
|
103 | that 'banner' gets prepended to all calls, so header is used for | |
104 | location-specific information. |
|
104 | location-specific information. | |
105 |
|
105 | |||
106 | For more details, see the __call__ method below. |
|
106 | For more details, see the __call__ method below. | |
107 |
|
107 | |||
108 | When the IPython shell is exited with Ctrl-D, normal program execution |
|
108 | When the IPython shell is exited with Ctrl-D, normal program execution | |
109 | resumes. |
|
109 | resumes. | |
110 |
|
110 | |||
111 | This functionality was inspired by a posting on comp.lang.python by cmkl |
|
111 | This functionality was inspired by a posting on comp.lang.python by cmkl | |
112 | <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and |
|
112 | <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and | |
113 | by the IDL stop/continue commands.""" |
|
113 | by the IDL stop/continue commands.""" | |
114 |
|
114 | |||
115 | def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None): |
|
115 | def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None): | |
116 | """Note that argv here is a string, NOT a list.""" |
|
116 | """Note that argv here is a string, NOT a list.""" | |
117 | self.set_banner(banner) |
|
117 | self.set_banner(banner) | |
118 | self.set_exit_msg(exit_msg) |
|
118 | self.set_exit_msg(exit_msg) | |
119 | self.set_dummy_mode(0) |
|
119 | self.set_dummy_mode(0) | |
120 |
|
120 | |||
121 | # sys.displayhook is a global, we need to save the user's original |
|
121 | # sys.displayhook is a global, we need to save the user's original | |
122 | # Don't rely on __displayhook__, as the user may have changed that. |
|
122 | # Don't rely on __displayhook__, as the user may have changed that. | |
123 | self.sys_displayhook_ori = sys.displayhook |
|
123 | self.sys_displayhook_ori = sys.displayhook | |
124 |
|
124 | |||
125 | # save readline completer status |
|
125 | # save readline completer status | |
126 | try: |
|
126 | try: | |
127 | #print 'Save completer',sys.ipcompleter # dbg |
|
127 | #print 'Save completer',sys.ipcompleter # dbg | |
128 | self.sys_ipcompleter_ori = sys.ipcompleter |
|
128 | self.sys_ipcompleter_ori = sys.ipcompleter | |
129 | except: |
|
129 | except: | |
130 | pass # not nested with IPython |
|
130 | pass # not nested with IPython | |
131 |
|
131 | |||
132 | # FIXME. Passing user_ns breaks namespace handling. |
|
132 | # FIXME. Passing user_ns breaks namespace handling. | |
133 | #self.IP = make_IPython(argv,user_ns=__main__.__dict__) |
|
133 | #self.IP = make_IPython(argv,user_ns=__main__.__dict__) | |
134 | self.IP = make_IPython(argv,rc_override=rc_override,embedded=True) |
|
134 | self.IP = make_IPython(argv,rc_override=rc_override,embedded=True) | |
135 |
|
135 | |||
136 | # copy our own displayhook also |
|
136 | # copy our own displayhook also | |
137 | self.sys_displayhook_embed = sys.displayhook |
|
137 | self.sys_displayhook_embed = sys.displayhook | |
138 | # and leave the system's display hook clean |
|
138 | # and leave the system's display hook clean | |
139 | sys.displayhook = self.sys_displayhook_ori |
|
139 | sys.displayhook = self.sys_displayhook_ori | |
140 | # don't use the ipython crash handler so that user exceptions aren't |
|
140 | # don't use the ipython crash handler so that user exceptions aren't | |
141 | # trapped |
|
141 | # trapped | |
142 | sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors, |
|
142 | sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors, | |
143 | mode = self.IP.rc.xmode, |
|
143 | mode = self.IP.rc.xmode, | |
144 | call_pdb = self.IP.rc.pdb) |
|
144 | call_pdb = self.IP.rc.pdb) | |
145 | self.restore_system_completer() |
|
145 | self.restore_system_completer() | |
146 |
|
146 | |||
147 | def restore_system_completer(self): |
|
147 | def restore_system_completer(self): | |
148 | """Restores the readline completer which was in place. |
|
148 | """Restores the readline completer which was in place. | |
149 |
|
149 | |||
150 | This allows embedded IPython within IPython not to disrupt the |
|
150 | This allows embedded IPython within IPython not to disrupt the | |
151 | parent's completion. |
|
151 | parent's completion. | |
152 | """ |
|
152 | """ | |
153 |
|
153 | |||
154 | try: |
|
154 | try: | |
155 | self.IP.readline.set_completer(self.sys_ipcompleter_ori) |
|
155 | self.IP.readline.set_completer(self.sys_ipcompleter_ori) | |
156 | sys.ipcompleter = self.sys_ipcompleter_ori |
|
156 | sys.ipcompleter = self.sys_ipcompleter_ori | |
157 | except: |
|
157 | except: | |
158 | pass |
|
158 | pass | |
159 |
|
159 | |||
160 | def __call__(self,header='',local_ns=None,global_ns=None,dummy=None): |
|
160 | def __call__(self,header='',local_ns=None,global_ns=None,dummy=None): | |
161 | """Activate the interactive interpreter. |
|
161 | """Activate the interactive interpreter. | |
162 |
|
162 | |||
163 | __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start |
|
163 | __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start | |
164 | the interpreter shell with the given local and global namespaces, and |
|
164 | the interpreter shell with the given local and global namespaces, and | |
165 | optionally print a header string at startup. |
|
165 | optionally print a header string at startup. | |
166 |
|
166 | |||
167 | The shell can be globally activated/deactivated using the |
|
167 | The shell can be globally activated/deactivated using the | |
168 | set/get_dummy_mode methods. This allows you to turn off a shell used |
|
168 | set/get_dummy_mode methods. This allows you to turn off a shell used | |
169 | for debugging globally. |
|
169 | for debugging globally. | |
170 |
|
170 | |||
171 | However, *each* time you call the shell you can override the current |
|
171 | However, *each* time you call the shell you can override the current | |
172 | state of dummy_mode with the optional keyword parameter 'dummy'. For |
|
172 | state of dummy_mode with the optional keyword parameter 'dummy'. For | |
173 | example, if you set dummy mode on with IPShell.set_dummy_mode(1), you |
|
173 | example, if you set dummy mode on with IPShell.set_dummy_mode(1), you | |
174 | can still have a specific call work by making it as IPShell(dummy=0). |
|
174 | can still have a specific call work by making it as IPShell(dummy=0). | |
175 |
|
175 | |||
176 | The optional keyword parameter dummy controls whether the call |
|
176 | The optional keyword parameter dummy controls whether the call | |
177 | actually does anything. """ |
|
177 | actually does anything. """ | |
178 |
|
178 | |||
179 | # Allow the dummy parameter to override the global __dummy_mode |
|
179 | # Allow the dummy parameter to override the global __dummy_mode | |
180 | if dummy or (dummy != 0 and self.__dummy_mode): |
|
180 | if dummy or (dummy != 0 and self.__dummy_mode): | |
181 | return |
|
181 | return | |
182 |
|
182 | |||
183 | # Set global subsystems (display,completions) to our values |
|
183 | # Set global subsystems (display,completions) to our values | |
184 | sys.displayhook = self.sys_displayhook_embed |
|
184 | sys.displayhook = self.sys_displayhook_embed | |
185 | if self.IP.has_readline: |
|
185 | if self.IP.has_readline: | |
186 | self.IP.readline.set_completer(self.IP.Completer.complete) |
|
186 | self.IP.readline.set_completer(self.IP.Completer.complete) | |
187 |
|
187 | |||
188 | if self.banner and header: |
|
188 | if self.banner and header: | |
189 | format = '%s\n%s\n' |
|
189 | format = '%s\n%s\n' | |
190 | else: |
|
190 | else: | |
191 | format = '%s%s\n' |
|
191 | format = '%s%s\n' | |
192 | banner = format % (self.banner,header) |
|
192 | banner = format % (self.banner,header) | |
193 |
|
193 | |||
194 | # Call the embedding code with a stack depth of 1 so it can skip over |
|
194 | # Call the embedding code with a stack depth of 1 so it can skip over | |
195 | # our call and get the original caller's namespaces. |
|
195 | # our call and get the original caller's namespaces. | |
196 | self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1) |
|
196 | self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1) | |
197 |
|
197 | |||
198 | if self.exit_msg: |
|
198 | if self.exit_msg: | |
199 | print self.exit_msg |
|
199 | print self.exit_msg | |
200 |
|
200 | |||
201 | # Restore global systems (display, completion) |
|
201 | # Restore global systems (display, completion) | |
202 | sys.displayhook = self.sys_displayhook_ori |
|
202 | sys.displayhook = self.sys_displayhook_ori | |
203 | self.restore_system_completer() |
|
203 | self.restore_system_completer() | |
204 |
|
204 | |||
205 | def set_dummy_mode(self,dummy): |
|
205 | def set_dummy_mode(self,dummy): | |
206 | """Sets the embeddable shell's dummy mode parameter. |
|
206 | """Sets the embeddable shell's dummy mode parameter. | |
207 |
|
207 | |||
208 | set_dummy_mode(dummy): dummy = 0 or 1. |
|
208 | set_dummy_mode(dummy): dummy = 0 or 1. | |
209 |
|
209 | |||
210 | This parameter is persistent and makes calls to the embeddable shell |
|
210 | This parameter is persistent and makes calls to the embeddable shell | |
211 | silently return without performing any action. This allows you to |
|
211 | silently return without performing any action. This allows you to | |
212 | globally activate or deactivate a shell you're using with a single call. |
|
212 | globally activate or deactivate a shell you're using with a single call. | |
213 |
|
213 | |||
214 | If you need to manually""" |
|
214 | If you need to manually""" | |
215 |
|
215 | |||
216 | if dummy not in [0,1,False,True]: |
|
216 | if dummy not in [0,1,False,True]: | |
217 | raise ValueError,'dummy parameter must be boolean' |
|
217 | raise ValueError,'dummy parameter must be boolean' | |
218 | self.__dummy_mode = dummy |
|
218 | self.__dummy_mode = dummy | |
219 |
|
219 | |||
220 | def get_dummy_mode(self): |
|
220 | def get_dummy_mode(self): | |
221 | """Return the current value of the dummy mode parameter. |
|
221 | """Return the current value of the dummy mode parameter. | |
222 | """ |
|
222 | """ | |
223 | return self.__dummy_mode |
|
223 | return self.__dummy_mode | |
224 |
|
224 | |||
225 | def set_banner(self,banner): |
|
225 | def set_banner(self,banner): | |
226 | """Sets the global banner. |
|
226 | """Sets the global banner. | |
227 |
|
227 | |||
228 | This banner gets prepended to every header printed when the shell |
|
228 | This banner gets prepended to every header printed when the shell | |
229 | instance is called.""" |
|
229 | instance is called.""" | |
230 |
|
230 | |||
231 | self.banner = banner |
|
231 | self.banner = banner | |
232 |
|
232 | |||
233 | def set_exit_msg(self,exit_msg): |
|
233 | def set_exit_msg(self,exit_msg): | |
234 | """Sets the global exit_msg. |
|
234 | """Sets the global exit_msg. | |
235 |
|
235 | |||
236 | This exit message gets printed upon exiting every time the embedded |
|
236 | This exit message gets printed upon exiting every time the embedded | |
237 | shell is called. It is None by default. """ |
|
237 | shell is called. It is None by default. """ | |
238 |
|
238 | |||
239 | self.exit_msg = exit_msg |
|
239 | self.exit_msg = exit_msg | |
240 |
|
240 | |||
241 | #----------------------------------------------------------------------------- |
|
241 | #----------------------------------------------------------------------------- | |
242 | def sigint_handler (signum,stack_frame): |
|
242 | def sigint_handler (signum,stack_frame): | |
243 | """Sigint handler for threaded apps. |
|
243 | """Sigint handler for threaded apps. | |
244 |
|
244 | |||
245 | This is a horrible hack to pass information about SIGINT _without_ using |
|
245 | This is a horrible hack to pass information about SIGINT _without_ using | |
246 | exceptions, since I haven't been able to properly manage cross-thread |
|
246 | exceptions, since I haven't been able to properly manage cross-thread | |
247 | exceptions in GTK/WX. In fact, I don't think it can be done (or at least |
|
247 | exceptions in GTK/WX. In fact, I don't think it can be done (or at least | |
248 | that's my understanding from a c.l.py thread where this was discussed).""" |
|
248 | that's my understanding from a c.l.py thread where this was discussed).""" | |
249 |
|
249 | |||
250 | global KBINT |
|
250 | global KBINT | |
251 |
|
251 | |||
252 | print '\nKeyboardInterrupt - Press <Enter> to continue.', |
|
252 | print '\nKeyboardInterrupt - Press <Enter> to continue.', | |
253 | Term.cout.flush() |
|
253 | Term.cout.flush() | |
254 | # Set global flag so that runsource can know that Ctrl-C was hit |
|
254 | # Set global flag so that runsource can know that Ctrl-C was hit | |
255 | KBINT = True |
|
255 | KBINT = True | |
256 |
|
256 | |||
257 | class MTInteractiveShell(InteractiveShell): |
|
257 | class MTInteractiveShell(InteractiveShell): | |
258 | """Simple multi-threaded shell.""" |
|
258 | """Simple multi-threaded shell.""" | |
259 |
|
259 | |||
260 | # Threading strategy taken from: |
|
260 | # Threading strategy taken from: | |
261 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian |
|
261 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian | |
262 | # McErlean and John Finlay. Modified with corrections by Antoon Pardon, |
|
262 | # McErlean and John Finlay. Modified with corrections by Antoon Pardon, | |
263 | # from the pygtk mailing list, to avoid lockups with system calls. |
|
263 | # from the pygtk mailing list, to avoid lockups with system calls. | |
264 |
|
264 | |||
265 | # class attribute to indicate whether the class supports threads or not. |
|
265 | # class attribute to indicate whether the class supports threads or not. | |
266 | # Subclasses with thread support should override this as needed. |
|
266 | # Subclasses with thread support should override this as needed. | |
267 | isthreaded = True |
|
267 | isthreaded = True | |
268 |
|
268 | |||
269 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
269 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
270 | user_ns=None,user_global_ns=None,banner2='',**kw): |
|
270 | user_ns=None,user_global_ns=None,banner2='',**kw): | |
271 | """Similar to the normal InteractiveShell, but with threading control""" |
|
271 | """Similar to the normal InteractiveShell, but with threading control""" | |
272 |
|
272 | |||
273 | InteractiveShell.__init__(self,name,usage,rc,user_ns, |
|
273 | InteractiveShell.__init__(self,name,usage,rc,user_ns, | |
274 | user_global_ns,banner2) |
|
274 | user_global_ns,banner2) | |
275 |
|
275 | |||
276 | # Locking control variable. We need to use a norma lock, not an RLock |
|
276 | # Locking control variable. We need to use a norma lock, not an RLock | |
277 | # here. I'm not exactly sure why, it seems to me like it should be |
|
277 | # here. I'm not exactly sure why, it seems to me like it should be | |
278 | # the opposite, but we deadlock with an RLock. Puzzled... |
|
278 | # the opposite, but we deadlock with an RLock. Puzzled... | |
279 | self.thread_ready = threading.Condition(threading.Lock()) |
|
279 | self.thread_ready = threading.Condition(threading.Lock()) | |
280 |
|
280 | |||
281 | # A queue to hold the code to be executed. A scalar variable is NOT |
|
281 | # A queue to hold the code to be executed. A scalar variable is NOT | |
282 | # enough, because uses like macros cause reentrancy. |
|
282 | # enough, because uses like macros cause reentrancy. | |
283 | self.code_queue = Queue.Queue() |
|
283 | self.code_queue = Queue.Queue() | |
284 |
|
284 | |||
285 | # Stuff to do at closing time |
|
285 | # Stuff to do at closing time | |
286 | self._kill = False |
|
286 | self._kill = False | |
287 | on_kill = kw.get('on_kill') |
|
287 | on_kill = kw.get('on_kill') | |
288 | if on_kill is None: |
|
288 | if on_kill is None: | |
289 | on_kill = [] |
|
289 | on_kill = [] | |
290 | # Check that all things to kill are callable: |
|
290 | # Check that all things to kill are callable: | |
291 | for t in on_kill: |
|
291 | for t in on_kill: | |
292 | if not callable(t): |
|
292 | if not callable(t): | |
293 | raise TypeError,'on_kill must be a list of callables' |
|
293 | raise TypeError,'on_kill must be a list of callables' | |
294 | self.on_kill = on_kill |
|
294 | self.on_kill = on_kill | |
295 |
|
295 | |||
296 | def runsource(self, source, filename="<input>", symbol="single"): |
|
296 | def runsource(self, source, filename="<input>", symbol="single"): | |
297 | """Compile and run some source in the interpreter. |
|
297 | """Compile and run some source in the interpreter. | |
298 |
|
298 | |||
299 | Modified version of code.py's runsource(), to handle threading issues. |
|
299 | Modified version of code.py's runsource(), to handle threading issues. | |
300 | See the original for full docstring details.""" |
|
300 | See the original for full docstring details.""" | |
301 |
|
301 | |||
302 | global KBINT |
|
302 | global KBINT | |
303 |
|
303 | |||
304 | # If Ctrl-C was typed, we reset the flag and return right away |
|
304 | # If Ctrl-C was typed, we reset the flag and return right away | |
305 | if KBINT: |
|
305 | if KBINT: | |
306 | KBINT = False |
|
306 | KBINT = False | |
307 | return False |
|
307 | return False | |
308 |
|
308 | |||
309 | try: |
|
309 | try: | |
310 | code = self.compile(source, filename, symbol) |
|
310 | code = self.compile(source, filename, symbol) | |
311 | except (OverflowError, SyntaxError, ValueError): |
|
311 | except (OverflowError, SyntaxError, ValueError): | |
312 | # Case 1 |
|
312 | # Case 1 | |
313 | self.showsyntaxerror(filename) |
|
313 | self.showsyntaxerror(filename) | |
314 | return False |
|
314 | return False | |
315 |
|
315 | |||
316 | if code is None: |
|
316 | if code is None: | |
317 | # Case 2 |
|
317 | # Case 2 | |
318 | return True |
|
318 | return True | |
319 |
|
319 | |||
320 | # Case 3 |
|
320 | # Case 3 | |
321 | # Store code in queue, so the execution thread can handle it. |
|
321 | # Store code in queue, so the execution thread can handle it. | |
322 |
|
322 | |||
323 | # Note that with macros and other applications, we MAY re-enter this |
|
323 | # Note that with macros and other applications, we MAY re-enter this | |
324 | # section, so we have to acquire the lock with non-blocking semantics, |
|
324 | # section, so we have to acquire the lock with non-blocking semantics, | |
325 | # else we deadlock. |
|
325 | # else we deadlock. | |
326 | got_lock = self.thread_ready.acquire(False) |
|
326 | got_lock = self.thread_ready.acquire(False) | |
327 | self.code_queue.put(code) |
|
327 | self.code_queue.put(code) | |
328 | if got_lock: |
|
328 | if got_lock: | |
329 | self.thread_ready.wait() # Wait until processed in timeout interval |
|
329 | self.thread_ready.wait() # Wait until processed in timeout interval | |
330 | self.thread_ready.release() |
|
330 | self.thread_ready.release() | |
331 |
|
331 | |||
332 | return False |
|
332 | return False | |
333 |
|
333 | |||
334 | def runcode(self): |
|
334 | def runcode(self): | |
335 | """Execute a code object. |
|
335 | """Execute a code object. | |
336 |
|
336 | |||
337 | Multithreaded wrapper around IPython's runcode().""" |
|
337 | Multithreaded wrapper around IPython's runcode().""" | |
338 |
|
338 | |||
339 | # lock thread-protected stuff |
|
339 | # lock thread-protected stuff | |
340 | got_lock = self.thread_ready.acquire(False) |
|
340 | got_lock = self.thread_ready.acquire(False) | |
341 |
|
341 | |||
342 | # Install sigint handler |
|
342 | # Install sigint handler | |
343 | try: |
|
343 | try: | |
344 | signal.signal(signal.SIGINT, sigint_handler) |
|
344 | signal.signal(signal.SIGINT, sigint_handler) | |
345 | except SystemError: |
|
345 | except SystemError: | |
346 | # This happens under Windows, which seems to have all sorts |
|
346 | # This happens under Windows, which seems to have all sorts | |
347 | # of problems with signal handling. Oh well... |
|
347 | # of problems with signal handling. Oh well... | |
348 | pass |
|
348 | pass | |
349 |
|
349 | |||
350 | if self._kill: |
|
350 | if self._kill: | |
351 | print >>Term.cout, 'Closing threads...', |
|
351 | print >>Term.cout, 'Closing threads...', | |
352 | Term.cout.flush() |
|
352 | Term.cout.flush() | |
353 | for tokill in self.on_kill: |
|
353 | for tokill in self.on_kill: | |
354 | tokill() |
|
354 | tokill() | |
355 | print >>Term.cout, 'Done.' |
|
355 | print >>Term.cout, 'Done.' | |
356 |
|
356 | |||
357 | # Flush queue of pending code by calling the run methood of the parent |
|
357 | # Flush queue of pending code by calling the run methood of the parent | |
358 | # class with all items which may be in the queue. |
|
358 | # class with all items which may be in the queue. | |
359 | while 1: |
|
359 | while 1: | |
360 | try: |
|
360 | try: | |
361 | code_to_run = self.code_queue.get_nowait() |
|
361 | code_to_run = self.code_queue.get_nowait() | |
362 | except Queue.Empty: |
|
362 | except Queue.Empty: | |
363 | break |
|
363 | break | |
|
364 | if got_lock: | |||
364 | self.thread_ready.notify() |
|
365 | self.thread_ready.notify() | |
365 | InteractiveShell.runcode(self,code_to_run) |
|
366 | InteractiveShell.runcode(self,code_to_run) | |
|
367 | else: | |||
|
368 | break | |||
366 |
|
369 | |||
367 | # We're done with thread-protected variables |
|
370 | # We're done with thread-protected variables | |
368 | if got_lock: |
|
371 | if got_lock: | |
369 | self.thread_ready.release() |
|
372 | self.thread_ready.release() | |
370 | # This MUST return true for gtk threading to work |
|
373 | # This MUST return true for gtk threading to work | |
371 | return True |
|
374 | return True | |
372 |
|
375 | |||
373 |
def kill |
|
376 | def kill(self): | |
374 | """Kill the thread, returning when it has been shut down.""" |
|
377 | """Kill the thread, returning when it has been shut down.""" | |
375 | self.thread_ready.acquire(False) |
|
378 | got_lock = self.thread_ready.acquire(False) | |
376 | self._kill = True |
|
379 | self._kill = True | |
|
380 | if got_lock: | |||
377 | self.thread_ready.release() |
|
381 | self.thread_ready.release() | |
378 |
|
382 | |||
379 | class MatplotlibShellBase: |
|
383 | class MatplotlibShellBase: | |
380 | """Mixin class to provide the necessary modifications to regular IPython |
|
384 | """Mixin class to provide the necessary modifications to regular IPython | |
381 | shell classes for matplotlib support. |
|
385 | shell classes for matplotlib support. | |
382 |
|
386 | |||
383 | Given Python's MRO, this should be used as the FIRST class in the |
|
387 | Given Python's MRO, this should be used as the FIRST class in the | |
384 | inheritance hierarchy, so that it overrides the relevant methods.""" |
|
388 | inheritance hierarchy, so that it overrides the relevant methods.""" | |
385 |
|
389 | |||
386 | def _matplotlib_config(self,name): |
|
390 | def _matplotlib_config(self,name): | |
387 | """Return items needed to setup the user's shell with matplotlib""" |
|
391 | """Return items needed to setup the user's shell with matplotlib""" | |
388 |
|
392 | |||
389 | # Initialize matplotlib to interactive mode always |
|
393 | # Initialize matplotlib to interactive mode always | |
390 | import matplotlib |
|
394 | import matplotlib | |
391 | from matplotlib import backends |
|
395 | from matplotlib import backends | |
392 | matplotlib.interactive(True) |
|
396 | matplotlib.interactive(True) | |
393 |
|
397 | |||
394 | def use(arg): |
|
398 | def use(arg): | |
395 | """IPython wrapper for matplotlib's backend switcher. |
|
399 | """IPython wrapper for matplotlib's backend switcher. | |
396 |
|
400 | |||
397 | In interactive use, we can not allow switching to a different |
|
401 | In interactive use, we can not allow switching to a different | |
398 | interactive backend, since thread conflicts will most likely crash |
|
402 | interactive backend, since thread conflicts will most likely crash | |
399 | the python interpreter. This routine does a safety check first, |
|
403 | the python interpreter. This routine does a safety check first, | |
400 | and refuses to perform a dangerous switch. It still allows |
|
404 | and refuses to perform a dangerous switch. It still allows | |
401 | switching to non-interactive backends.""" |
|
405 | switching to non-interactive backends.""" | |
402 |
|
406 | |||
403 | if arg in backends.interactive_bk and arg != self.mpl_backend: |
|
407 | if arg in backends.interactive_bk and arg != self.mpl_backend: | |
404 | m=('invalid matplotlib backend switch.\n' |
|
408 | m=('invalid matplotlib backend switch.\n' | |
405 | 'This script attempted to switch to the interactive ' |
|
409 | 'This script attempted to switch to the interactive ' | |
406 | 'backend: `%s`\n' |
|
410 | 'backend: `%s`\n' | |
407 | 'Your current choice of interactive backend is: `%s`\n\n' |
|
411 | 'Your current choice of interactive backend is: `%s`\n\n' | |
408 | 'Switching interactive matplotlib backends at runtime\n' |
|
412 | 'Switching interactive matplotlib backends at runtime\n' | |
409 | 'would crash the python interpreter, ' |
|
413 | 'would crash the python interpreter, ' | |
410 | 'and IPython has blocked it.\n\n' |
|
414 | 'and IPython has blocked it.\n\n' | |
411 | 'You need to either change your choice of matplotlib backend\n' |
|
415 | 'You need to either change your choice of matplotlib backend\n' | |
412 | 'by editing your .matplotlibrc file, or run this script as a \n' |
|
416 | 'by editing your .matplotlibrc file, or run this script as a \n' | |
413 | 'standalone file from the command line, not using IPython.\n' % |
|
417 | 'standalone file from the command line, not using IPython.\n' % | |
414 | (arg,self.mpl_backend) ) |
|
418 | (arg,self.mpl_backend) ) | |
415 | raise RuntimeError, m |
|
419 | raise RuntimeError, m | |
416 | else: |
|
420 | else: | |
417 | self.mpl_use(arg) |
|
421 | self.mpl_use(arg) | |
418 | self.mpl_use._called = True |
|
422 | self.mpl_use._called = True | |
419 |
|
423 | |||
420 | self.matplotlib = matplotlib |
|
424 | self.matplotlib = matplotlib | |
421 | self.mpl_backend = matplotlib.rcParams['backend'] |
|
425 | self.mpl_backend = matplotlib.rcParams['backend'] | |
422 |
|
426 | |||
423 | # we also need to block switching of interactive backends by use() |
|
427 | # we also need to block switching of interactive backends by use() | |
424 | self.mpl_use = matplotlib.use |
|
428 | self.mpl_use = matplotlib.use | |
425 | self.mpl_use._called = False |
|
429 | self.mpl_use._called = False | |
426 | # overwrite the original matplotlib.use with our wrapper |
|
430 | # overwrite the original matplotlib.use with our wrapper | |
427 | matplotlib.use = use |
|
431 | matplotlib.use = use | |
428 |
|
432 | |||
429 | # This must be imported last in the matplotlib series, after |
|
433 | # This must be imported last in the matplotlib series, after | |
430 | # backend/interactivity choices have been made |
|
434 | # backend/interactivity choices have been made | |
431 | try: |
|
435 | try: | |
432 | import matplotlib.pylab as pylab |
|
436 | import matplotlib.pylab as pylab | |
433 | self.pylab = pylab |
|
437 | self.pylab = pylab | |
434 | self.pylab_name = 'pylab' |
|
438 | self.pylab_name = 'pylab' | |
435 | except ImportError: |
|
439 | except ImportError: | |
436 | import matplotlib.matlab as matlab |
|
440 | import matplotlib.matlab as matlab | |
437 | self.pylab = matlab |
|
441 | self.pylab = matlab | |
438 | self.pylab_name = 'matlab' |
|
442 | self.pylab_name = 'matlab' | |
439 |
|
443 | |||
440 | self.pylab.show._needmain = False |
|
444 | self.pylab.show._needmain = False | |
441 | # We need to detect at runtime whether show() is called by the user. |
|
445 | # We need to detect at runtime whether show() is called by the user. | |
442 | # For this, we wrap it into a decorator which adds a 'called' flag. |
|
446 | # For this, we wrap it into a decorator which adds a 'called' flag. | |
443 | self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive) |
|
447 | self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive) | |
444 |
|
448 | |||
445 | # Build a user namespace initialized with matplotlib/matlab features. |
|
449 | # Build a user namespace initialized with matplotlib/matlab features. | |
446 | user_ns = {'__name__':'__main__', |
|
450 | user_ns = {'__name__':'__main__', | |
447 | '__builtins__' : __builtin__ } |
|
451 | '__builtins__' : __builtin__ } | |
448 |
|
452 | |||
449 | # Be careful not to remove the final \n in the code string below, or |
|
453 | # Be careful not to remove the final \n in the code string below, or | |
450 | # things will break badly with py22 (I think it's a python bug, 2.3 is |
|
454 | # things will break badly with py22 (I think it's a python bug, 2.3 is | |
451 | # OK). |
|
455 | # OK). | |
452 | pname = self.pylab_name # Python can't interpolate dotted var names |
|
456 | pname = self.pylab_name # Python can't interpolate dotted var names | |
453 | exec ("import matplotlib\n" |
|
457 | exec ("import matplotlib\n" | |
454 | "import matplotlib.%(pname)s as %(pname)s\n" |
|
458 | "import matplotlib.%(pname)s as %(pname)s\n" | |
455 | "from matplotlib.%(pname)s import *\n" % locals()) in user_ns |
|
459 | "from matplotlib.%(pname)s import *\n" % locals()) in user_ns | |
456 |
|
460 | |||
457 | # Build matplotlib info banner |
|
461 | # Build matplotlib info banner | |
458 | b=""" |
|
462 | b=""" | |
459 | Welcome to pylab, a matplotlib-based Python environment. |
|
463 | Welcome to pylab, a matplotlib-based Python environment. | |
460 | For more information, type 'help(pylab)'. |
|
464 | For more information, type 'help(pylab)'. | |
461 | """ |
|
465 | """ | |
462 | return user_ns,b |
|
466 | return user_ns,b | |
463 |
|
467 | |||
464 | def mplot_exec(self,fname,*where,**kw): |
|
468 | def mplot_exec(self,fname,*where,**kw): | |
465 | """Execute a matplotlib script. |
|
469 | """Execute a matplotlib script. | |
466 |
|
470 | |||
467 | This is a call to execfile(), but wrapped in safeties to properly |
|
471 | This is a call to execfile(), but wrapped in safeties to properly | |
468 | handle interactive rendering and backend switching.""" |
|
472 | handle interactive rendering and backend switching.""" | |
469 |
|
473 | |||
470 | #print '*** Matplotlib runner ***' # dbg |
|
474 | #print '*** Matplotlib runner ***' # dbg | |
471 | # turn off rendering until end of script |
|
475 | # turn off rendering until end of script | |
472 | isInteractive = self.matplotlib.rcParams['interactive'] |
|
476 | isInteractive = self.matplotlib.rcParams['interactive'] | |
473 | self.matplotlib.interactive(False) |
|
477 | self.matplotlib.interactive(False) | |
474 | self.safe_execfile(fname,*where,**kw) |
|
478 | self.safe_execfile(fname,*where,**kw) | |
475 | self.matplotlib.interactive(isInteractive) |
|
479 | self.matplotlib.interactive(isInteractive) | |
476 | # make rendering call now, if the user tried to do it |
|
480 | # make rendering call now, if the user tried to do it | |
477 | if self.pylab.draw_if_interactive.called: |
|
481 | if self.pylab.draw_if_interactive.called: | |
478 | self.pylab.draw() |
|
482 | self.pylab.draw() | |
479 | self.pylab.draw_if_interactive.called = False |
|
483 | self.pylab.draw_if_interactive.called = False | |
480 |
|
484 | |||
481 | # if a backend switch was performed, reverse it now |
|
485 | # if a backend switch was performed, reverse it now | |
482 | if self.mpl_use._called: |
|
486 | if self.mpl_use._called: | |
483 | self.matplotlib.rcParams['backend'] = self.mpl_backend |
|
487 | self.matplotlib.rcParams['backend'] = self.mpl_backend | |
484 |
|
488 | |||
485 | def magic_run(self,parameter_s=''): |
|
489 | def magic_run(self,parameter_s=''): | |
486 | Magic.magic_run(self,parameter_s,runner=self.mplot_exec) |
|
490 | Magic.magic_run(self,parameter_s,runner=self.mplot_exec) | |
487 |
|
491 | |||
488 | # Fix the docstring so users see the original as well |
|
492 | # Fix the docstring so users see the original as well | |
489 | magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__, |
|
493 | magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__, | |
490 | "\n *** Modified %run for Matplotlib," |
|
494 | "\n *** Modified %run for Matplotlib," | |
491 | " with proper interactive handling ***") |
|
495 | " with proper interactive handling ***") | |
492 |
|
496 | |||
493 | # Now we provide 2 versions of a matplotlib-aware IPython base shells, single |
|
497 | # Now we provide 2 versions of a matplotlib-aware IPython base shells, single | |
494 | # and multithreaded. Note that these are meant for internal use, the IPShell* |
|
498 | # and multithreaded. Note that these are meant for internal use, the IPShell* | |
495 | # classes below are the ones meant for public consumption. |
|
499 | # classes below are the ones meant for public consumption. | |
496 |
|
500 | |||
497 | class MatplotlibShell(MatplotlibShellBase,InteractiveShell): |
|
501 | class MatplotlibShell(MatplotlibShellBase,InteractiveShell): | |
498 | """Single-threaded shell with matplotlib support.""" |
|
502 | """Single-threaded shell with matplotlib support.""" | |
499 |
|
503 | |||
500 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
504 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
501 | user_ns=None,user_global_ns=None,**kw): |
|
505 | user_ns=None,user_global_ns=None,**kw): | |
502 | user_ns,b2 = self._matplotlib_config(name) |
|
506 | user_ns,b2 = self._matplotlib_config(name) | |
503 | InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, |
|
507 | InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, | |
504 | banner2=b2,**kw) |
|
508 | banner2=b2,**kw) | |
505 |
|
509 | |||
506 | class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell): |
|
510 | class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell): | |
507 | """Multi-threaded shell with matplotlib support.""" |
|
511 | """Multi-threaded shell with matplotlib support.""" | |
508 |
|
512 | |||
509 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
513 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
510 | user_ns=None,user_global_ns=None, **kw): |
|
514 | user_ns=None,user_global_ns=None, **kw): | |
511 | user_ns,b2 = self._matplotlib_config(name) |
|
515 | user_ns,b2 = self._matplotlib_config(name) | |
512 | MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, |
|
516 | MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, | |
513 | banner2=b2,**kw) |
|
517 | banner2=b2,**kw) | |
514 |
|
518 | |||
515 | #----------------------------------------------------------------------------- |
|
519 | #----------------------------------------------------------------------------- | |
516 | # Utility functions for the different GUI enabled IPShell* classes. |
|
520 | # Utility functions for the different GUI enabled IPShell* classes. | |
517 |
|
521 | |||
518 | def get_tk(): |
|
522 | def get_tk(): | |
519 | """Tries to import Tkinter and returns a withdrawn Tkinter root |
|
523 | """Tries to import Tkinter and returns a withdrawn Tkinter root | |
520 | window. If Tkinter is already imported or not available, this |
|
524 | window. If Tkinter is already imported or not available, this | |
521 | returns None. This function calls `hijack_tk` underneath. |
|
525 | returns None. This function calls `hijack_tk` underneath. | |
522 | """ |
|
526 | """ | |
523 | if not USE_TK or sys.modules.has_key('Tkinter'): |
|
527 | if not USE_TK or sys.modules.has_key('Tkinter'): | |
524 | return None |
|
528 | return None | |
525 | else: |
|
529 | else: | |
526 | try: |
|
530 | try: | |
527 | import Tkinter |
|
531 | import Tkinter | |
528 | except ImportError: |
|
532 | except ImportError: | |
529 | return None |
|
533 | return None | |
530 | else: |
|
534 | else: | |
531 | hijack_tk() |
|
535 | hijack_tk() | |
532 | r = Tkinter.Tk() |
|
536 | r = Tkinter.Tk() | |
533 | r.withdraw() |
|
537 | r.withdraw() | |
534 | return r |
|
538 | return r | |
535 |
|
539 | |||
536 | def hijack_tk(): |
|
540 | def hijack_tk(): | |
537 | """Modifies Tkinter's mainloop with a dummy so when a module calls |
|
541 | """Modifies Tkinter's mainloop with a dummy so when a module calls | |
538 | mainloop, it does not block. |
|
542 | mainloop, it does not block. | |
539 |
|
543 | |||
540 | """ |
|
544 | """ | |
541 | def misc_mainloop(self, n=0): |
|
545 | def misc_mainloop(self, n=0): | |
542 | pass |
|
546 | pass | |
543 | def tkinter_mainloop(n=0): |
|
547 | def tkinter_mainloop(n=0): | |
544 | pass |
|
548 | pass | |
545 |
|
549 | |||
546 | import Tkinter |
|
550 | import Tkinter | |
547 | Tkinter.Misc.mainloop = misc_mainloop |
|
551 | Tkinter.Misc.mainloop = misc_mainloop | |
548 | Tkinter.mainloop = tkinter_mainloop |
|
552 | Tkinter.mainloop = tkinter_mainloop | |
549 |
|
553 | |||
550 | def update_tk(tk): |
|
554 | def update_tk(tk): | |
551 | """Updates the Tkinter event loop. This is typically called from |
|
555 | """Updates the Tkinter event loop. This is typically called from | |
552 | the respective WX or GTK mainloops. |
|
556 | the respective WX or GTK mainloops. | |
553 | """ |
|
557 | """ | |
554 | if tk: |
|
558 | if tk: | |
555 | tk.update() |
|
559 | tk.update() | |
556 |
|
560 | |||
557 | def hijack_wx(): |
|
561 | def hijack_wx(): | |
558 | """Modifies wxPython's MainLoop with a dummy so user code does not |
|
562 | """Modifies wxPython's MainLoop with a dummy so user code does not | |
559 | block IPython. The hijacked mainloop function is returned. |
|
563 | block IPython. The hijacked mainloop function is returned. | |
560 | """ |
|
564 | """ | |
561 | def dummy_mainloop(*args, **kw): |
|
565 | def dummy_mainloop(*args, **kw): | |
562 | pass |
|
566 | pass | |
563 | import wxPython |
|
567 | import wxPython | |
564 | ver = wxPython.__version__ |
|
568 | ver = wxPython.__version__ | |
565 | orig_mainloop = None |
|
569 | orig_mainloop = None | |
566 | if ver[:3] >= '2.5': |
|
570 | if ver[:3] >= '2.5': | |
567 | import wx |
|
571 | import wx | |
568 | if hasattr(wx, '_core_'): core = getattr(wx, '_core_') |
|
572 | if hasattr(wx, '_core_'): core = getattr(wx, '_core_') | |
569 | elif hasattr(wx, '_core'): core = getattr(wx, '_core') |
|
573 | elif hasattr(wx, '_core'): core = getattr(wx, '_core') | |
570 | else: raise AttributeError('Could not find wx core module') |
|
574 | else: raise AttributeError('Could not find wx core module') | |
571 | orig_mainloop = core.PyApp_MainLoop |
|
575 | orig_mainloop = core.PyApp_MainLoop | |
572 | core.PyApp_MainLoop = dummy_mainloop |
|
576 | core.PyApp_MainLoop = dummy_mainloop | |
573 | elif ver[:3] == '2.4': |
|
577 | elif ver[:3] == '2.4': | |
574 | orig_mainloop = wxPython.wxc.wxPyApp_MainLoop |
|
578 | orig_mainloop = wxPython.wxc.wxPyApp_MainLoop | |
575 | wxPython.wxc.wxPyApp_MainLoop = dummy_mainloop |
|
579 | wxPython.wxc.wxPyApp_MainLoop = dummy_mainloop | |
576 | else: |
|
580 | else: | |
577 | warn("Unable to find either wxPython version 2.4 or >= 2.5.") |
|
581 | warn("Unable to find either wxPython version 2.4 or >= 2.5.") | |
578 | return orig_mainloop |
|
582 | return orig_mainloop | |
579 |
|
583 | |||
580 | def hijack_gtk(): |
|
584 | def hijack_gtk(): | |
581 | """Modifies pyGTK's mainloop with a dummy so user code does not |
|
585 | """Modifies pyGTK's mainloop with a dummy so user code does not | |
582 | block IPython. This function returns the original `gtk.mainloop` |
|
586 | block IPython. This function returns the original `gtk.mainloop` | |
583 | function that has been hijacked. |
|
587 | function that has been hijacked. | |
584 | """ |
|
588 | """ | |
585 | def dummy_mainloop(*args, **kw): |
|
589 | def dummy_mainloop(*args, **kw): | |
586 | pass |
|
590 | pass | |
587 | import gtk |
|
591 | import gtk | |
588 | if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main |
|
592 | if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main | |
589 | else: orig_mainloop = gtk.mainloop |
|
593 | else: orig_mainloop = gtk.mainloop | |
590 | gtk.mainloop = dummy_mainloop |
|
594 | gtk.mainloop = dummy_mainloop | |
591 | gtk.main = dummy_mainloop |
|
595 | gtk.main = dummy_mainloop | |
592 | return orig_mainloop |
|
596 | return orig_mainloop | |
593 |
|
597 | |||
594 | #----------------------------------------------------------------------------- |
|
598 | #----------------------------------------------------------------------------- | |
595 | # The IPShell* classes below are the ones meant to be run by external code as |
|
599 | # The IPShell* classes below are the ones meant to be run by external code as | |
596 | # IPython instances. Note that unless a specific threading strategy is |
|
600 | # IPython instances. Note that unless a specific threading strategy is | |
597 | # desired, the factory function start() below should be used instead (it |
|
601 | # desired, the factory function start() below should be used instead (it | |
598 | # selects the proper threaded class). |
|
602 | # selects the proper threaded class). | |
599 |
|
603 | |||
600 | class IPShellGTK(threading.Thread): |
|
604 | class IPShellGTK(threading.Thread): | |
601 | """Run a gtk mainloop() in a separate thread. |
|
605 | """Run a gtk mainloop() in a separate thread. | |
602 |
|
606 | |||
603 | Python commands can be passed to the thread where they will be executed. |
|
607 | Python commands can be passed to the thread where they will be executed. | |
604 | This is implemented by periodically checking for passed code using a |
|
608 | This is implemented by periodically checking for passed code using a | |
605 | GTK timeout callback.""" |
|
609 | GTK timeout callback.""" | |
606 |
|
610 | |||
607 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
611 | TIMEOUT = 100 # Millisecond interval between timeouts. | |
608 |
|
612 | |||
609 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
613 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, | |
610 | debug=1,shell_class=MTInteractiveShell): |
|
614 | debug=1,shell_class=MTInteractiveShell): | |
611 |
|
615 | |||
612 | import gtk |
|
616 | import gtk | |
613 |
|
617 | |||
614 | self.gtk = gtk |
|
618 | self.gtk = gtk | |
615 | self.gtk_mainloop = hijack_gtk() |
|
619 | self.gtk_mainloop = hijack_gtk() | |
616 |
|
620 | |||
617 | # Allows us to use both Tk and GTK. |
|
621 | # Allows us to use both Tk and GTK. | |
618 | self.tk = get_tk() |
|
622 | self.tk = get_tk() | |
619 |
|
623 | |||
620 | if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit |
|
624 | if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit | |
621 | else: mainquit = self.gtk.mainquit |
|
625 | else: mainquit = self.gtk.mainquit | |
622 |
|
626 | |||
623 | self.IP = make_IPython(argv,user_ns=user_ns, |
|
627 | self.IP = make_IPython(argv,user_ns=user_ns, | |
624 | user_global_ns=user_global_ns, |
|
628 | user_global_ns=user_global_ns, | |
625 | debug=debug, |
|
629 | debug=debug, | |
626 | shell_class=shell_class, |
|
630 | shell_class=shell_class, | |
627 | on_kill=[mainquit]) |
|
631 | on_kill=[mainquit]) | |
628 |
|
632 | |||
629 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
633 | # HACK: slot for banner in self; it will be passed to the mainloop | |
630 | # method only and .run() needs it. The actual value will be set by |
|
634 | # method only and .run() needs it. The actual value will be set by | |
631 | # .mainloop(). |
|
635 | # .mainloop(). | |
632 | self._banner = None |
|
636 | self._banner = None | |
633 |
|
637 | |||
634 | threading.Thread.__init__(self) |
|
638 | threading.Thread.__init__(self) | |
635 |
|
639 | |||
636 | def run(self): |
|
640 | def run(self): | |
637 | self.IP.mainloop(self._banner) |
|
641 | self.IP.mainloop(self._banner) | |
638 | self.IP.kill() |
|
642 | self.IP.kill() | |
639 |
|
643 | |||
640 | def mainloop(self,sys_exit=0,banner=None): |
|
644 | def mainloop(self,sys_exit=0,banner=None): | |
641 |
|
645 | |||
642 | self._banner = banner |
|
646 | self._banner = banner | |
643 |
|
647 | |||
644 | if self.gtk.pygtk_version >= (2,4,0): |
|
648 | if self.gtk.pygtk_version >= (2,4,0): | |
645 | import gobject |
|
649 | import gobject | |
646 | gobject.idle_add(self.on_timer) |
|
650 | gobject.idle_add(self.on_timer) | |
647 | else: |
|
651 | else: | |
648 | self.gtk.idle_add(self.on_timer) |
|
652 | self.gtk.idle_add(self.on_timer) | |
649 |
|
653 | |||
650 | if sys.platform != 'win32': |
|
654 | if sys.platform != 'win32': | |
651 | try: |
|
655 | try: | |
652 | if self.gtk.gtk_version[0] >= 2: |
|
656 | if self.gtk.gtk_version[0] >= 2: | |
653 | self.gtk.threads_init() |
|
657 | self.gtk.threads_init() | |
654 | except AttributeError: |
|
658 | except AttributeError: | |
655 | pass |
|
659 | pass | |
656 | except RuntimeError: |
|
660 | except RuntimeError: | |
657 | error('Your pyGTK likely has not been compiled with ' |
|
661 | error('Your pyGTK likely has not been compiled with ' | |
658 | 'threading support.\n' |
|
662 | 'threading support.\n' | |
659 | 'The exception printout is below.\n' |
|
663 | 'The exception printout is below.\n' | |
660 | 'You can either rebuild pyGTK with threads, or ' |
|
664 | 'You can either rebuild pyGTK with threads, or ' | |
661 | 'try using \n' |
|
665 | 'try using \n' | |
662 | 'matplotlib with a different backend (like Tk or WX).\n' |
|
666 | 'matplotlib with a different backend (like Tk or WX).\n' | |
663 | 'Note that matplotlib will most likely not work in its ' |
|
667 | 'Note that matplotlib will most likely not work in its ' | |
664 | 'current state!') |
|
668 | 'current state!') | |
665 | self.IP.InteractiveTB() |
|
669 | self.IP.InteractiveTB() | |
666 | self.start() |
|
670 | self.start() | |
667 | self.gtk.threads_enter() |
|
671 | self.gtk.threads_enter() | |
668 | self.gtk_mainloop() |
|
672 | self.gtk_mainloop() | |
669 | self.gtk.threads_leave() |
|
673 | self.gtk.threads_leave() | |
670 | self.join() |
|
674 | self.join() | |
671 |
|
675 | |||
672 | def on_timer(self): |
|
676 | def on_timer(self): | |
673 | """Called when GTK is idle. |
|
677 | """Called when GTK is idle. | |
674 |
|
678 | |||
675 | Must return True always, otherwise GTK stops calling it""" |
|
679 | Must return True always, otherwise GTK stops calling it""" | |
676 |
|
680 | |||
677 | update_tk(self.tk) |
|
681 | update_tk(self.tk) | |
678 | self.IP.runcode() |
|
682 | self.IP.runcode() | |
679 | time.sleep(0.01) |
|
683 | time.sleep(0.01) | |
680 | return True |
|
684 | return True | |
681 |
|
685 | |||
682 | class IPShellWX(threading.Thread): |
|
686 | class IPShellWX(threading.Thread): | |
683 | """Run a wx mainloop() in a separate thread. |
|
687 | """Run a wx mainloop() in a separate thread. | |
684 |
|
688 | |||
685 | Python commands can be passed to the thread where they will be executed. |
|
689 | Python commands can be passed to the thread where they will be executed. | |
686 | This is implemented by periodically checking for passed code using a |
|
690 | This is implemented by periodically checking for passed code using a | |
687 | GTK timeout callback.""" |
|
691 | GTK timeout callback.""" | |
688 |
|
692 | |||
689 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
693 | TIMEOUT = 100 # Millisecond interval between timeouts. | |
690 |
|
694 | |||
691 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
695 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, | |
692 | debug=1,shell_class=MTInteractiveShell): |
|
696 | debug=1,shell_class=MTInteractiveShell): | |
693 |
|
697 | |||
694 | self.IP = make_IPython(argv,user_ns=user_ns, |
|
698 | self.IP = make_IPython(argv,user_ns=user_ns, | |
695 | user_global_ns=user_global_ns, |
|
699 | user_global_ns=user_global_ns, | |
696 | debug=debug, |
|
700 | debug=debug, | |
697 | shell_class=shell_class, |
|
701 | shell_class=shell_class, | |
698 | on_kill=[self.wxexit]) |
|
702 | on_kill=[self.wxexit]) | |
699 |
|
703 | |||
700 | wantedwxversion=self.IP.rc.wxversion |
|
704 | wantedwxversion=self.IP.rc.wxversion | |
701 | if wantedwxversion!="0": |
|
705 | if wantedwxversion!="0": | |
702 | try: |
|
706 | try: | |
703 | import wxversion |
|
707 | import wxversion | |
704 | except ImportError: |
|
708 | except ImportError: | |
705 | error('The wxversion module is needed for WX version selection') |
|
709 | error('The wxversion module is needed for WX version selection') | |
706 | else: |
|
710 | else: | |
707 | try: |
|
711 | try: | |
708 | wxversion.select(wantedwxversion) |
|
712 | wxversion.select(wantedwxversion) | |
709 | except: |
|
713 | except: | |
710 | self.IP.InteractiveTB() |
|
714 | self.IP.InteractiveTB() | |
711 | error('Requested wxPython version %s could not be loaded' % |
|
715 | error('Requested wxPython version %s could not be loaded' % | |
712 | wantedwxversion) |
|
716 | wantedwxversion) | |
713 |
|
717 | |||
714 | import wxPython.wx as wx |
|
718 | import wxPython.wx as wx | |
715 |
|
719 | |||
716 | threading.Thread.__init__(self) |
|
720 | threading.Thread.__init__(self) | |
717 | self.wx = wx |
|
721 | self.wx = wx | |
718 | self.wx_mainloop = hijack_wx() |
|
722 | self.wx_mainloop = hijack_wx() | |
719 |
|
723 | |||
720 | # Allows us to use both Tk and GTK. |
|
724 | # Allows us to use both Tk and GTK. | |
721 | self.tk = get_tk() |
|
725 | self.tk = get_tk() | |
722 |
|
726 | |||
723 |
|
727 | |||
724 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
728 | # HACK: slot for banner in self; it will be passed to the mainloop | |
725 | # method only and .run() needs it. The actual value will be set by |
|
729 | # method only and .run() needs it. The actual value will be set by | |
726 | # .mainloop(). |
|
730 | # .mainloop(). | |
727 | self._banner = None |
|
731 | self._banner = None | |
728 |
|
732 | |||
729 | self.app = None |
|
733 | self.app = None | |
730 |
|
734 | |||
731 | def wxexit(self, *args): |
|
735 | def wxexit(self, *args): | |
732 | if self.app is not None: |
|
736 | if self.app is not None: | |
733 | self.app.agent.timer.Stop() |
|
737 | self.app.agent.timer.Stop() | |
734 | self.app.ExitMainLoop() |
|
738 | self.app.ExitMainLoop() | |
735 |
|
739 | |||
736 | def run(self): |
|
740 | def run(self): | |
737 | self.IP.mainloop(self._banner) |
|
741 | self.IP.mainloop(self._banner) | |
738 | self.IP.kill() |
|
742 | self.IP.kill() | |
739 |
|
743 | |||
740 | def mainloop(self,sys_exit=0,banner=None): |
|
744 | def mainloop(self,sys_exit=0,banner=None): | |
741 |
|
745 | |||
742 | self._banner = banner |
|
746 | self._banner = banner | |
743 |
|
747 | |||
744 | self.start() |
|
748 | self.start() | |
745 |
|
749 | |||
746 | class TimerAgent(self.wx.wxMiniFrame): |
|
750 | class TimerAgent(self.wx.wxMiniFrame): | |
747 | wx = self.wx |
|
751 | wx = self.wx | |
748 | IP = self.IP |
|
752 | IP = self.IP | |
749 | tk = self.tk |
|
753 | tk = self.tk | |
750 | def __init__(self, parent, interval): |
|
754 | def __init__(self, parent, interval): | |
751 | style = self.wx.wxDEFAULT_FRAME_STYLE | self.wx.wxTINY_CAPTION_HORIZ |
|
755 | style = self.wx.wxDEFAULT_FRAME_STYLE | self.wx.wxTINY_CAPTION_HORIZ | |
752 | self.wx.wxMiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200), |
|
756 | self.wx.wxMiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200), | |
753 | size=(100, 100),style=style) |
|
757 | size=(100, 100),style=style) | |
754 | self.Show(False) |
|
758 | self.Show(False) | |
755 | self.interval = interval |
|
759 | self.interval = interval | |
756 | self.timerId = self.wx.wxNewId() |
|
760 | self.timerId = self.wx.wxNewId() | |
757 |
|
761 | |||
758 | def StartWork(self): |
|
762 | def StartWork(self): | |
759 | self.timer = self.wx.wxTimer(self, self.timerId) |
|
763 | self.timer = self.wx.wxTimer(self, self.timerId) | |
760 | self.wx.EVT_TIMER(self, self.timerId, self.OnTimer) |
|
764 | self.wx.EVT_TIMER(self, self.timerId, self.OnTimer) | |
761 | self.timer.Start(self.interval) |
|
765 | self.timer.Start(self.interval) | |
762 |
|
766 | |||
763 | def OnTimer(self, event): |
|
767 | def OnTimer(self, event): | |
764 | update_tk(self.tk) |
|
768 | update_tk(self.tk) | |
765 | self.IP.runcode() |
|
769 | self.IP.runcode() | |
766 |
|
770 | |||
767 | class App(self.wx.wxApp): |
|
771 | class App(self.wx.wxApp): | |
768 | wx = self.wx |
|
772 | wx = self.wx | |
769 | TIMEOUT = self.TIMEOUT |
|
773 | TIMEOUT = self.TIMEOUT | |
770 | def OnInit(self): |
|
774 | def OnInit(self): | |
771 | 'Create the main window and insert the custom frame' |
|
775 | 'Create the main window and insert the custom frame' | |
772 | self.agent = TimerAgent(None, self.TIMEOUT) |
|
776 | self.agent = TimerAgent(None, self.TIMEOUT) | |
773 | self.agent.Show(self.wx.false) |
|
777 | self.agent.Show(self.wx.false) | |
774 | self.agent.StartWork() |
|
778 | self.agent.StartWork() | |
775 | return self.wx.true |
|
779 | return self.wx.true | |
776 |
|
780 | |||
777 | self.app = App(redirect=False) |
|
781 | self.app = App(redirect=False) | |
778 | self.wx_mainloop(self.app) |
|
782 | self.wx_mainloop(self.app) | |
779 | self.join() |
|
783 | self.join() | |
780 |
|
784 | |||
781 |
|
785 | |||
782 | class IPShellQt(threading.Thread): |
|
786 | class IPShellQt(threading.Thread): | |
783 | """Run a Qt event loop in a separate thread. |
|
787 | """Run a Qt event loop in a separate thread. | |
784 |
|
788 | |||
785 | Python commands can be passed to the thread where they will be executed. |
|
789 | Python commands can be passed to the thread where they will be executed. | |
786 | This is implemented by periodically checking for passed code using a |
|
790 | This is implemented by periodically checking for passed code using a | |
787 | Qt timer / slot.""" |
|
791 | Qt timer / slot.""" | |
788 |
|
792 | |||
789 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
793 | TIMEOUT = 100 # Millisecond interval between timeouts. | |
790 |
|
794 | |||
791 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
795 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, | |
792 | debug=0,shell_class=MTInteractiveShell): |
|
796 | debug=0,shell_class=MTInteractiveShell): | |
793 |
|
797 | |||
794 | import qt |
|
798 | import qt | |
795 |
|
799 | |||
796 | class newQApplication: |
|
800 | class newQApplication: | |
797 | def __init__( self ): |
|
801 | def __init__( self ): | |
798 | self.QApplication = qt.QApplication |
|
802 | self.QApplication = qt.QApplication | |
799 |
|
803 | |||
800 | def __call__( *args, **kwargs ): |
|
804 | def __call__( *args, **kwargs ): | |
801 | return qt.qApp |
|
805 | return qt.qApp | |
802 |
|
806 | |||
803 | def exec_loop( *args, **kwargs ): |
|
807 | def exec_loop( *args, **kwargs ): | |
804 | pass |
|
808 | pass | |
805 |
|
809 | |||
806 | def __getattr__( self, name ): |
|
810 | def __getattr__( self, name ): | |
807 | return getattr( self.QApplication, name ) |
|
811 | return getattr( self.QApplication, name ) | |
808 |
|
812 | |||
809 | qt.QApplication = newQApplication() |
|
813 | qt.QApplication = newQApplication() | |
810 |
|
814 | |||
811 | # Allows us to use both Tk and QT. |
|
815 | # Allows us to use both Tk and QT. | |
812 | self.tk = get_tk() |
|
816 | self.tk = get_tk() | |
813 |
|
817 | |||
814 | self.IP = make_IPython(argv,user_ns=user_ns, |
|
818 | self.IP = make_IPython(argv,user_ns=user_ns, | |
815 | user_global_ns=user_global_ns, |
|
819 | user_global_ns=user_global_ns, | |
816 | debug=debug, |
|
820 | debug=debug, | |
817 | shell_class=shell_class, |
|
821 | shell_class=shell_class, | |
818 | on_kill=[qt.qApp.exit]) |
|
822 | on_kill=[qt.qApp.exit]) | |
819 |
|
823 | |||
820 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
824 | # HACK: slot for banner in self; it will be passed to the mainloop | |
821 | # method only and .run() needs it. The actual value will be set by |
|
825 | # method only and .run() needs it. The actual value will be set by | |
822 | # .mainloop(). |
|
826 | # .mainloop(). | |
823 | self._banner = None |
|
827 | self._banner = None | |
824 |
|
828 | |||
825 | threading.Thread.__init__(self) |
|
829 | threading.Thread.__init__(self) | |
826 |
|
830 | |||
827 | def run(self): |
|
831 | def run(self): | |
828 | self.IP.mainloop(self._banner) |
|
832 | self.IP.mainloop(self._banner) | |
829 | self.IP.kill() |
|
833 | self.IP.kill() | |
830 |
|
834 | |||
831 | def mainloop(self,sys_exit=0,banner=None): |
|
835 | def mainloop(self,sys_exit=0,banner=None): | |
832 |
|
836 | |||
833 | import qt |
|
837 | import qt | |
834 |
|
838 | |||
835 | self._banner = banner |
|
839 | self._banner = banner | |
836 |
|
840 | |||
837 | if qt.QApplication.startingUp(): |
|
841 | if qt.QApplication.startingUp(): | |
838 | a = qt.QApplication.QApplication(sys.argv) |
|
842 | a = qt.QApplication.QApplication(sys.argv) | |
839 | self.timer = qt.QTimer() |
|
843 | self.timer = qt.QTimer() | |
840 | qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer ) |
|
844 | qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer ) | |
841 |
|
845 | |||
842 | self.start() |
|
846 | self.start() | |
843 | self.timer.start( self.TIMEOUT, True ) |
|
847 | self.timer.start( self.TIMEOUT, True ) | |
844 | while True: |
|
848 | while True: | |
845 | if self.IP._kill: break |
|
849 | if self.IP._kill: break | |
846 | qt.qApp.exec_loop() |
|
850 | qt.qApp.exec_loop() | |
847 | self.join() |
|
851 | self.join() | |
848 |
|
852 | |||
849 | def on_timer(self): |
|
853 | def on_timer(self): | |
850 | update_tk(self.tk) |
|
854 | update_tk(self.tk) | |
851 | result = self.IP.runcode() |
|
855 | result = self.IP.runcode() | |
852 | self.timer.start( self.TIMEOUT, True ) |
|
856 | self.timer.start( self.TIMEOUT, True ) | |
853 | return result |
|
857 | return result | |
854 |
|
858 | |||
855 | # A set of matplotlib public IPython shell classes, for single-threaded |
|
859 | # A set of matplotlib public IPython shell classes, for single-threaded | |
856 | # (Tk* and FLTK* backends) and multithreaded (GTK* and WX* backends) use. |
|
860 | # (Tk* and FLTK* backends) and multithreaded (GTK* and WX* backends) use. | |
857 | class IPShellMatplotlib(IPShell): |
|
861 | class IPShellMatplotlib(IPShell): | |
858 | """Subclass IPShell with MatplotlibShell as the internal shell. |
|
862 | """Subclass IPShell with MatplotlibShell as the internal shell. | |
859 |
|
863 | |||
860 | Single-threaded class, meant for the Tk* and FLTK* backends. |
|
864 | Single-threaded class, meant for the Tk* and FLTK* backends. | |
861 |
|
865 | |||
862 | Having this on a separate class simplifies the external driver code.""" |
|
866 | Having this on a separate class simplifies the external driver code.""" | |
863 |
|
867 | |||
864 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
868 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): | |
865 | IPShell.__init__(self,argv,user_ns,user_global_ns,debug, |
|
869 | IPShell.__init__(self,argv,user_ns,user_global_ns,debug, | |
866 | shell_class=MatplotlibShell) |
|
870 | shell_class=MatplotlibShell) | |
867 |
|
871 | |||
868 | class IPShellMatplotlibGTK(IPShellGTK): |
|
872 | class IPShellMatplotlibGTK(IPShellGTK): | |
869 | """Subclass IPShellGTK with MatplotlibMTShell as the internal shell. |
|
873 | """Subclass IPShellGTK with MatplotlibMTShell as the internal shell. | |
870 |
|
874 | |||
871 | Multi-threaded class, meant for the GTK* backends.""" |
|
875 | Multi-threaded class, meant for the GTK* backends.""" | |
872 |
|
876 | |||
873 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
877 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): | |
874 | IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug, |
|
878 | IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug, | |
875 | shell_class=MatplotlibMTShell) |
|
879 | shell_class=MatplotlibMTShell) | |
876 |
|
880 | |||
877 | class IPShellMatplotlibWX(IPShellWX): |
|
881 | class IPShellMatplotlibWX(IPShellWX): | |
878 | """Subclass IPShellWX with MatplotlibMTShell as the internal shell. |
|
882 | """Subclass IPShellWX with MatplotlibMTShell as the internal shell. | |
879 |
|
883 | |||
880 | Multi-threaded class, meant for the WX* backends.""" |
|
884 | Multi-threaded class, meant for the WX* backends.""" | |
881 |
|
885 | |||
882 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
886 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): | |
883 | IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug, |
|
887 | IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug, | |
884 | shell_class=MatplotlibMTShell) |
|
888 | shell_class=MatplotlibMTShell) | |
885 |
|
889 | |||
886 | class IPShellMatplotlibQt(IPShellQt): |
|
890 | class IPShellMatplotlibQt(IPShellQt): | |
887 | """Subclass IPShellQt with MatplotlibMTShell as the internal shell. |
|
891 | """Subclass IPShellQt with MatplotlibMTShell as the internal shell. | |
888 |
|
892 | |||
889 | Multi-threaded class, meant for the Qt* backends.""" |
|
893 | Multi-threaded class, meant for the Qt* backends.""" | |
890 |
|
894 | |||
891 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
895 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): | |
892 | IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug, |
|
896 | IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug, | |
893 | shell_class=MatplotlibMTShell) |
|
897 | shell_class=MatplotlibMTShell) | |
894 |
|
898 | |||
895 | #----------------------------------------------------------------------------- |
|
899 | #----------------------------------------------------------------------------- | |
896 | # Factory functions to actually start the proper thread-aware shell |
|
900 | # Factory functions to actually start the proper thread-aware shell | |
897 |
|
901 | |||
898 | def _matplotlib_shell_class(): |
|
902 | def _matplotlib_shell_class(): | |
899 | """Factory function to handle shell class selection for matplotlib. |
|
903 | """Factory function to handle shell class selection for matplotlib. | |
900 |
|
904 | |||
901 | The proper shell class to use depends on the matplotlib backend, since |
|
905 | The proper shell class to use depends on the matplotlib backend, since | |
902 | each backend requires a different threading strategy.""" |
|
906 | each backend requires a different threading strategy.""" | |
903 |
|
907 | |||
904 | try: |
|
908 | try: | |
905 | import matplotlib |
|
909 | import matplotlib | |
906 | except ImportError: |
|
910 | except ImportError: | |
907 | error('matplotlib could NOT be imported! Starting normal IPython.') |
|
911 | error('matplotlib could NOT be imported! Starting normal IPython.') | |
908 | sh_class = IPShell |
|
912 | sh_class = IPShell | |
909 | else: |
|
913 | else: | |
910 | backend = matplotlib.rcParams['backend'] |
|
914 | backend = matplotlib.rcParams['backend'] | |
911 | if backend.startswith('GTK'): |
|
915 | if backend.startswith('GTK'): | |
912 | sh_class = IPShellMatplotlibGTK |
|
916 | sh_class = IPShellMatplotlibGTK | |
913 | elif backend.startswith('WX'): |
|
917 | elif backend.startswith('WX'): | |
914 | sh_class = IPShellMatplotlibWX |
|
918 | sh_class = IPShellMatplotlibWX | |
915 | elif backend.startswith('Qt'): |
|
919 | elif backend.startswith('Qt'): | |
916 | sh_class = IPShellMatplotlibQt |
|
920 | sh_class = IPShellMatplotlibQt | |
917 | else: |
|
921 | else: | |
918 | sh_class = IPShellMatplotlib |
|
922 | sh_class = IPShellMatplotlib | |
919 | #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg |
|
923 | #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg | |
920 | return sh_class |
|
924 | return sh_class | |
921 |
|
925 | |||
922 | # This is the one which should be called by external code. |
|
926 | # This is the one which should be called by external code. | |
923 | def start(user_ns = None): |
|
927 | def start(user_ns = None): | |
924 | """Return a running shell instance, dealing with threading options. |
|
928 | """Return a running shell instance, dealing with threading options. | |
925 |
|
929 | |||
926 | This is a factory function which will instantiate the proper IPython shell |
|
930 | This is a factory function which will instantiate the proper IPython shell | |
927 | based on the user's threading choice. Such a selector is needed because |
|
931 | based on the user's threading choice. Such a selector is needed because | |
928 | different GUI toolkits require different thread handling details.""" |
|
932 | different GUI toolkits require different thread handling details.""" | |
929 |
|
933 | |||
930 | global USE_TK |
|
934 | global USE_TK | |
931 | # Crude sys.argv hack to extract the threading options. |
|
935 | # Crude sys.argv hack to extract the threading options. | |
932 | argv = sys.argv |
|
936 | argv = sys.argv | |
933 | if len(argv) > 1: |
|
937 | if len(argv) > 1: | |
934 | if len(argv) > 2: |
|
938 | if len(argv) > 2: | |
935 | arg2 = argv[2] |
|
939 | arg2 = argv[2] | |
936 | if arg2.endswith('-tk'): |
|
940 | if arg2.endswith('-tk'): | |
937 | USE_TK = True |
|
941 | USE_TK = True | |
938 | arg1 = argv[1] |
|
942 | arg1 = argv[1] | |
939 | if arg1.endswith('-gthread'): |
|
943 | if arg1.endswith('-gthread'): | |
940 | shell = IPShellGTK |
|
944 | shell = IPShellGTK | |
941 | elif arg1.endswith( '-qthread' ): |
|
945 | elif arg1.endswith( '-qthread' ): | |
942 | shell = IPShellQt |
|
946 | shell = IPShellQt | |
943 | elif arg1.endswith('-wthread'): |
|
947 | elif arg1.endswith('-wthread'): | |
944 | shell = IPShellWX |
|
948 | shell = IPShellWX | |
945 | elif arg1.endswith('-pylab'): |
|
949 | elif arg1.endswith('-pylab'): | |
946 | shell = _matplotlib_shell_class() |
|
950 | shell = _matplotlib_shell_class() | |
947 | else: |
|
951 | else: | |
948 | shell = IPShell |
|
952 | shell = IPShell | |
949 | else: |
|
953 | else: | |
950 | shell = IPShell |
|
954 | shell = IPShell | |
951 | return shell(user_ns = user_ns) |
|
955 | return shell(user_ns = user_ns) | |
952 |
|
956 | |||
953 | # Some aliases for backwards compatibility |
|
957 | # Some aliases for backwards compatibility | |
954 | IPythonShell = IPShell |
|
958 | IPythonShell = IPShell | |
955 | IPythonShellEmbed = IPShellEmbed |
|
959 | IPythonShellEmbed = IPShellEmbed | |
956 | #************************ End of file <Shell.py> *************************** |
|
960 | #************************ End of file <Shell.py> *************************** |
General Comments 0
You need to be logged in to leave comments.
Login now