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