##// END OF EJS Templates
Namespace fixes for embedded instances, as well as tab-complete enhancements...
fperez -
Show More

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

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