##// END OF EJS Templates
Restored major default completer functionality (cd, import, run)....
Fernando Perez -
Show More
@@ -0,0 +1,337 b''
1 """Implementations for various useful completers.
2
3 These are all loaded by default by IPython.
4 """
5 #-----------------------------------------------------------------------------
6 # Copyright (C) 2010 The IPython Development Team.
7 #
8 # Distributed under the terms of the BSD License.
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16 from __future__ import print_function
17
18 # Stdlib imports
19 import glob
20 import inspect
21 import os
22 import re
23 import shlex
24 import sys
25
26 # Third-party imports
27 from time import time
28 from zipimport import zipimporter
29
30 # Our own imports
31 from IPython.core.error import TryNext
32
33 # FIXME: this should be pulled in with the right call via the component system
34 from IPython.core.ipapi import get as get_ipython
35
36 #-----------------------------------------------------------------------------
37 # Globals and constants
38 #-----------------------------------------------------------------------------
39
40 # Time in seconds after which the rootmodules will be stored permanently in the
41 # ipython ip.db database (kept in the user's .ipython dir).
42 TIMEOUT_STORAGE = 2
43
44 # Time in seconds after which we give up
45 TIMEOUT_GIVEUP = 20
46
47 # Regular expression for the python import statement
48 import_re = re.compile(r'.*(\.so|\.py[cod]?)$')
49
50 # RE for the ipython %run command (python + ipython scripts)
51 magic_run_re = re.compile(r'.*(\.ipy|\.py[w]?)$')
52
53 #-----------------------------------------------------------------------------
54 # Local utilities
55 #-----------------------------------------------------------------------------
56
57 def shlex_split(x):
58 """Helper function to split lines into segments.
59 """
60 # shlex.split raises an exception if there is a syntax error in sh syntax
61 # for example if no closing " is found. This function keeps dropping the
62 # last character of the line until shlex.split does not raise
63 # an exception. It adds end of the line to the result of shlex.split
64 #
65 # Example:
66 # %run "c:/python -> ['%run','"c:/python']
67
68 endofline = []
69 while x != '':
70 try:
71 comps = shlex.split(x)
72 if len(endofline) >= 1:
73 comps.append(''.join(endofline))
74 return comps
75
76 except ValueError:
77 endofline = [x[-1:]]+endofline
78 x = x[:-1]
79
80 return [''.join(endofline)]
81
82 def module_list(path):
83 """
84 Return the list containing the names of the modules available in the given
85 folder.
86 """
87
88 if os.path.isdir(path):
89 folder_list = os.listdir(path)
90 elif path.endswith('.egg'):
91 try:
92 folder_list = [f for f in zipimporter(path)._files]
93 except:
94 folder_list = []
95 else:
96 folder_list = []
97
98 if not folder_list:
99 return []
100
101 # A few local constants to be used in loops below
102 isfile = os.path.isfile
103 pjoin = os.path.join
104 basename = os.path.basename
105
106 # Now find actual path matches for packages or modules
107 folder_list = [p for p in folder_list
108 if isfile(pjoin(path, p,'__init__.py'))
109 or import_re.match(p) ]
110
111 return [basename(p).split('.')[0] for p in folder_list]
112
113 def get_root_modules():
114 """
115 Returns a list containing the names of all the modules available in the
116 folders of the pythonpath.
117 """
118 ip = get_ipython()
119
120 if 'rootmodules' in ip.db:
121 return ip.db['rootmodules']
122
123 t = time()
124 store = False
125 modules = list(sys.builtin_module_names)
126 for path in sys.path:
127 modules += module_list(path)
128 if time() - t >= TIMEOUT_STORAGE and not store:
129 store = True
130 print("\nCaching the list of root modules, please wait!")
131 print("(This will only be done once - type '%rehashx' to "
132 "reset cache!)\n")
133 sys.stdout.flush()
134 if time() - t > TIMEOUT_GIVEUP:
135 print("This is taking too long, we give up.\n")
136 ip.db['rootmodules'] = []
137 return []
138
139 modules = set(modules)
140 if '__init__' in modules:
141 modules.remove('__init__')
142 modules = list(modules)
143 if store:
144 ip.db['rootmodules'] = modules
145 return modules
146
147
148 def is_importable(module, attr, only_modules):
149 if only_modules:
150 return inspect.ismodule(getattr(module, attr))
151 else:
152 return not(attr[:2] == '__' and attr[-2:] == '__')
153
154
155 def try_import(mod, only_modules=False):
156 try:
157 m = __import__(mod)
158 except:
159 return []
160 mods = mod.split('.')
161 for module in mods[1:]:
162 m = getattr(m, module)
163
164 m_is_init = hasattr(m, '__file__') and '__init__' in m.__file__
165
166 completions = []
167 if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init:
168 completions.extend( [attr for attr in dir(m) if
169 is_importable(m, attr, only_modules)])
170
171 completions.extend(getattr(m, '__all__', []))
172 if m_is_init:
173 completions.extend(module_list(os.path.dirname(m.__file__)))
174 completions = set(completions)
175 if '__init__' in completions:
176 completions.remove('__init__')
177 return list(completions)
178
179
180 #-----------------------------------------------------------------------------
181 # Completion-related functions.
182 #-----------------------------------------------------------------------------
183
184 def quick_completer(cmd, completions):
185 """ Easily create a trivial completer for a command.
186
187 Takes either a list of completions, or all completions in string (that will
188 be split on whitespace).
189
190 Example::
191
192 [d:\ipython]|1> import ipy_completers
193 [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz'])
194 [d:\ipython]|3> foo b<TAB>
195 bar baz
196 [d:\ipython]|3> foo ba
197 """
198
199 if isinstance(completions, basestring):
200 completions = completions.split()
201
202 def do_complete(self, event):
203 return completions
204
205 get_ipython().set_hook('complete_command',do_complete, str_key = cmd)
206
207
208 def module_completion(line):
209 """
210 Returns a list containing the completion possibilities for an import line.
211
212 The line looks like this :
213 'import xml.d'
214 'from xml.dom import'
215 """
216
217 words = line.split(' ')
218 nwords = len(words)
219
220 # from whatever <tab> -> 'import '
221 if nwords == 3 and words[0] == 'from':
222 return ['import ']
223
224 # 'from xy<tab>' or 'import xy<tab>'
225 if nwords < 3 and (words[0] in ['import','from']) :
226 if nwords == 1:
227 return get_root_modules()
228 mod = words[1].split('.')
229 if len(mod) < 2:
230 return get_root_modules()
231 completion_list = try_import('.'.join(mod[:-1]), True)
232 return ['.'.join(mod[:-1] + [el]) for el in completion_list]
233
234 # 'from xyz import abc<tab>'
235 if nwords >= 3 and words[0] == 'from':
236 mod = words[1]
237 return try_import(mod)
238
239 #-----------------------------------------------------------------------------
240 # Completers
241 #-----------------------------------------------------------------------------
242 # These all have the func(self, event) signature to be used as custom
243 # completers
244
245 def module_completer(self,event):
246 """Give completions after user has typed 'import ...' or 'from ...'"""
247
248 # This works in all versions of python. While 2.5 has
249 # pkgutil.walk_packages(), that particular routine is fairly dangerous,
250 # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full
251 # of possibly problematic side effects.
252 # This search the folders in the sys.path for available modules.
253
254 return module_completion(event.line)
255
256
257 def magic_run_completer(self, event):
258 """Complete files that end in .py or .ipy for the %run command.
259 """
260 comps = shlex_split(event.line)
261 relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"")
262
263 #print "\nev=",event # dbg
264 #print "rp=",relpath # dbg
265 #print 'comps=',comps # dbg
266
267 lglob = glob.glob
268 isdir = os.path.isdir
269 if relpath.startswith('~'):
270 relpath = os.path.expanduser(relpath)
271 dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)]
272
273 # Find if the user has already typed the first filename, after which we
274 # should complete on all files, since after the first one other files may
275 # be arguments to the input script.
276
277 if filter(magic_run_re.match, comps):
278 pys = [f.replace('\\','/') for f in lglob('*')]
279 else:
280 pys = [f.replace('\\','/')
281 for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') +
282 lglob(relpath + '*.pyw')]
283 return dirs + pys
284
285
286 def cd_completer(self, event):
287 """Completer function for cd, which only returns directories."""
288 ip = get_ipython()
289 relpath = event.symbol
290
291 #print(event) # dbg
292 if event.line.endswith('-b') or ' -b ' in event.line:
293 # return only bookmark completions
294 bkms = self.db.get('bookmarks', None)
295 if bkms:
296 return bkms.keys()
297 else:
298 return []
299
300 if event.symbol == '-':
301 width_dh = str(len(str(len(ip.user_ns['_dh']) + 1)))
302 # jump in directory history by number
303 fmt = '-%0' + width_dh +'d [%s]'
304 ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
305 if len(ents) > 1:
306 return ents
307 return []
308
309 if event.symbol.startswith('--'):
310 return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']]
311
312 if relpath.startswith('~'):
313 relpath = os.path.expanduser(relpath).replace('\\','/')
314
315 found = []
316 for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*')
317 if os.path.isdir(f)]:
318 if ' ' in d:
319 # we don't want to deal with any of that, complex code
320 # for this is elsewhere
321 raise TryNext
322
323 found.append( d )
324
325 if not found:
326 if os.path.isdir(relpath):
327 return [relpath]
328
329 # if no completions so far, try bookmarks
330 bks = self.db.get('bookmarks',{}).keys()
331 bkmatches = [s for s in bks if s.startswith(event.symbol)]
332 if bkmatches:
333 return bkmatches
334
335 raise TryNext
336
337 return found
@@ -1,784 +1,795 b''
1 1 """Word completion for IPython.
2 2
3 3 This module is a fork of the rlcompleter module in the Python standard
4 4 library. The original enhancements made to rlcompleter have been sent
5 5 upstream and were accepted as of Python 2.3, but we need a lot more
6 6 functionality specific to IPython, so this module will continue to live as an
7 7 IPython-specific utility.
8 8
9 9 Original rlcompleter documentation:
10 10
11 11 This requires the latest extension to the readline module (the
12 12 completes keywords, built-ins and globals in __main__; when completing
13 13 NAME.NAME..., it evaluates (!) the expression up to the last dot and
14 14 completes its attributes.
15 15
16 16 It's very cool to do "import string" type "string.", hit the
17 17 completion key (twice), and see the list of names defined by the
18 18 string module!
19 19
20 20 Tip: to use the tab key as the completion key, call
21 21
22 22 readline.parse_and_bind("tab: complete")
23 23
24 24 Notes:
25 25
26 26 - Exceptions raised by the completer function are *ignored* (and
27 27 generally cause the completion to fail). This is a feature -- since
28 28 readline sets the tty device in raw (or cbreak) mode, printing a
29 29 traceback wouldn't work well without some complicated hoopla to save,
30 30 reset and restore the tty state.
31 31
32 32 - The evaluation of the NAME.NAME... form may cause arbitrary
33 33 application defined code to be executed if an object with a
34 34 __getattr__ hook is found. Since it is the responsibility of the
35 35 application (or the user) to enable this feature, I consider this an
36 36 acceptable risk. More complicated expressions (e.g. function calls or
37 37 indexing operations) are *not* evaluated.
38 38
39 39 - GNU readline is also used by the built-in functions input() and
40 40 raw_input(), and thus these also benefit/suffer from the completer
41 41 features. Clearly an interactive application can benefit by
42 42 specifying its own completer function and using raw_input() for all
43 43 its input.
44 44
45 45 - When the original stdin is not a tty device, GNU readline is never
46 46 used, and this module (and the readline module) are silently inactive.
47 47 """
48 48
49 49 #*****************************************************************************
50 50 #
51 51 # Since this file is essentially a minimally modified copy of the rlcompleter
52 52 # module which is part of the standard Python distribution, I assume that the
53 53 # proper procedure is to maintain its copyright as belonging to the Python
54 54 # Software Foundation (in addition to my own, for all new code).
55 55 #
56 56 # Copyright (C) 2008-2010 IPython Development Team
57 57 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
58 58 # Copyright (C) 2001 Python Software Foundation, www.python.org
59 59 #
60 60 # Distributed under the terms of the BSD License. The full license is in
61 61 # the file COPYING, distributed as part of this software.
62 62 #
63 63 #*****************************************************************************
64 64 from __future__ import print_function
65 65
66 66 #-----------------------------------------------------------------------------
67 67 # Imports
68 68 #-----------------------------------------------------------------------------
69 69
70 70 import __builtin__
71 71 import __main__
72 72 import glob
73 73 import inspect
74 74 import itertools
75 75 import keyword
76 76 import os
77 77 import re
78 78 import shlex
79 79 import sys
80 80
81 81 from IPython.core.error import TryNext
82 82 from IPython.core.prefilter import ESC_MAGIC
83 from IPython.utils import generics, io
84 from IPython.utils.frame import debugx
83 from IPython.utils import generics
84 from IPython.utils import io
85 85 from IPython.utils.dir2 import dir2
86 86
87 87 #-----------------------------------------------------------------------------
88 88 # Globals
89 89 #-----------------------------------------------------------------------------
90 90
91 91 # Public API
92 92 __all__ = ['Completer','IPCompleter']
93 93
94 94 if sys.platform == 'win32':
95 95 PROTECTABLES = ' '
96 96 else:
97 97 PROTECTABLES = ' ()'
98 98
99 99 #-----------------------------------------------------------------------------
100 100 # Main functions and classes
101 101 #-----------------------------------------------------------------------------
102 102
103 103 def protect_filename(s):
104 104 """Escape a string to protect certain characters."""
105 105
106 106 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
107 107 for ch in s])
108 108
109 109
110 110 def mark_dirs(matches):
111 111 """Mark directories in input list by appending '/' to their names."""
112 112 out = []
113 113 isdir = os.path.isdir
114 114 for x in matches:
115 115 if isdir(x):
116 116 out.append(x+'/')
117 117 else:
118 118 out.append(x)
119 119 return out
120 120
121 121
122 122 def single_dir_expand(matches):
123 123 "Recursively expand match lists containing a single dir."
124 124
125 125 if len(matches) == 1 and os.path.isdir(matches[0]):
126 126 # Takes care of links to directories also. Use '/'
127 127 # explicitly, even under Windows, so that name completions
128 128 # don't end up escaped.
129 129 d = matches[0]
130 130 if d[-1] in ['/','\\']:
131 131 d = d[:-1]
132 132
133 133 subdirs = os.listdir(d)
134 134 if subdirs:
135 135 matches = [ (d + '/' + p) for p in subdirs]
136 136 return single_dir_expand(matches)
137 137 else:
138 138 return matches
139 139 else:
140 140 return matches
141 141
142 142
143 143 class Bunch(object): pass
144 144
145 145
146 146 class CompletionSplitter(object):
147 147 """An object to split an input line in a manner similar to readline.
148 148
149 149 By having our own implementation, we can expose readline-like completion in
150 150 a uniform manner to all frontends. This object only needs to be given the
151 151 line of text to be split and the cursor position on said line, and it
152 152 returns the 'word' to be completed on at the cursor after splitting the
153 153 entire line.
154 154
155 155 What characters are used as splitting delimiters can be controlled by
156 156 setting the `delims` attribute (this is a property that internally
157 157 automatically builds the necessary """
158 158
159 159 # Private interface
160 160
161 161 # A string of delimiter characters. The default value makes sense for
162 162 # IPython's most typical usage patterns.
163 163 _delims = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
164 164
165 165 # The expression (a normal string) to be compiled into a regular expression
166 166 # for actual splitting. We store it as an attribute mostly for ease of
167 167 # debugging, since this type of code can be so tricky to debug.
168 168 _delim_expr = None
169 169
170 170 # The regular expression that does the actual splitting
171 171 _delim_re = None
172 172
173 173 def __init__(self, delims=None):
174 174 delims = CompletionSplitter._delims if delims is None else delims
175 175 self.set_delims(delims)
176 176
177 177 def set_delims(self, delims):
178 178 """Set the delimiters for line splitting."""
179 179 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
180 180 self._delim_re = re.compile(expr)
181 181 self._delims = delims
182 182 self._delim_expr = expr
183 183
184 184 def get_delims(self):
185 185 """Return the string of delimiter characters."""
186 186 return self._delims
187 187
188 188 def split_line(self, line, cursor_pos=None):
189 189 """Split a line of text with a cursor at the given position.
190 190 """
191 191 l = line if cursor_pos is None else line[:cursor_pos]
192 192 return self._delim_re.split(l)[-1]
193 193
194 194
195 195 class Completer(object):
196 196 def __init__(self, namespace=None, global_namespace=None):
197 197 """Create a new completer for the command line.
198 198
199 199 Completer([namespace,global_namespace]) -> completer instance.
200 200
201 201 If unspecified, the default namespace where completions are performed
202 202 is __main__ (technically, __main__.__dict__). Namespaces should be
203 203 given as dictionaries.
204 204
205 205 An optional second namespace can be given. This allows the completer
206 206 to handle cases where both the local and global scopes need to be
207 207 distinguished.
208 208
209 209 Completer instances should be used as the completion mechanism of
210 210 readline via the set_completer() call:
211 211
212 212 readline.set_completer(Completer(my_namespace).complete)
213 213 """
214 214
215 215 # Don't bind to namespace quite yet, but flag whether the user wants a
216 216 # specific namespace or to use __main__.__dict__. This will allow us
217 217 # to bind to __main__.__dict__ at completion time, not now.
218 218 if namespace is None:
219 219 self.use_main_ns = 1
220 220 else:
221 221 self.use_main_ns = 0
222 222 self.namespace = namespace
223 223
224 224 # The global namespace, if given, can be bound directly
225 225 if global_namespace is None:
226 226 self.global_namespace = {}
227 227 else:
228 228 self.global_namespace = global_namespace
229 229
230 230 def complete(self, text, state):
231 231 """Return the next possible completion for 'text'.
232 232
233 233 This is called successively with state == 0, 1, 2, ... until it
234 234 returns None. The completion should begin with 'text'.
235 235
236 236 """
237 237 if self.use_main_ns:
238 238 self.namespace = __main__.__dict__
239 239
240 240 if state == 0:
241 241 if "." in text:
242 242 self.matches = self.attr_matches(text)
243 243 else:
244 244 self.matches = self.global_matches(text)
245 245 try:
246 246 return self.matches[state]
247 247 except IndexError:
248 248 return None
249 249
250 250 def global_matches(self, text):
251 251 """Compute matches when text is a simple name.
252 252
253 253 Return a list of all keywords, built-in functions and names currently
254 254 defined in self.namespace or self.global_namespace that match.
255 255
256 256 """
257 257 #print 'Completer->global_matches, txt=%r' % text # dbg
258 258 matches = []
259 259 match_append = matches.append
260 260 n = len(text)
261 261 for lst in [keyword.kwlist,
262 262 __builtin__.__dict__.keys(),
263 263 self.namespace.keys(),
264 264 self.global_namespace.keys()]:
265 265 for word in lst:
266 266 if word[:n] == text and word != "__builtins__":
267 267 match_append(word)
268 268 return matches
269 269
270 270 def attr_matches(self, text):
271 271 """Compute matches when text contains a dot.
272 272
273 273 Assuming the text is of the form NAME.NAME....[NAME], and is
274 274 evaluatable in self.namespace or self.global_namespace, it will be
275 275 evaluated and its attributes (as revealed by dir()) are used as
276 276 possible completions. (For class instances, class members are are
277 277 also considered.)
278 278
279 279 WARNING: this can still invoke arbitrary C code, if an object
280 280 with a __getattr__ hook is evaluated.
281 281
282 282 """
283 283
284 284 #print 'Completer->attr_matches, txt=%r' % text # dbg
285 285 # Another option, seems to work great. Catches things like ''.<tab>
286 286 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
287 287
288 288 if not m:
289 289 return []
290 290
291 291 expr, attr = m.group(1, 3)
292 292 try:
293 293 obj = eval(expr, self.namespace)
294 294 except:
295 295 try:
296 296 obj = eval(expr, self.global_namespace)
297 297 except:
298 298 return []
299 299
300 300 words = dir2(obj)
301 301
302 302 try:
303 303 words = generics.complete_object(obj, words)
304 304 except TryNext:
305 305 pass
306 306 # Build match list to return
307 307 n = len(attr)
308 308 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
309 309 return res
310 310
311 311
312 312 class IPCompleter(Completer):
313 313 """Extension of the completer class with IPython-specific features"""
314 314
315 315 def __init__(self, shell, namespace=None, global_namespace=None,
316 316 omit__names=0, alias_table=None, use_readline=True):
317 317 """IPCompleter() -> completer
318 318
319 319 Return a completer object suitable for use by the readline library
320 320 via readline.set_completer().
321 321
322 322 Inputs:
323 323
324 324 - shell: a pointer to the ipython shell itself. This is needed
325 325 because this completer knows about magic functions, and those can
326 326 only be accessed via the ipython instance.
327 327
328 328 - namespace: an optional dict where completions are performed.
329 329
330 330 - global_namespace: secondary optional dict for completions, to
331 331 handle cases (such as IPython embedded inside functions) where
332 332 both Python scopes are visible.
333 333
334 334 - The optional omit__names parameter sets the completer to omit the
335 335 'magic' names (__magicname__) for python objects unless the text
336 336 to be completed explicitly starts with one or more underscores.
337 337
338 338 - If alias_table is supplied, it should be a dictionary of aliases
339 339 to complete.
340 340
341 341 use_readline : bool, optional
342 342 If true, use the readline library. This completer can still function
343 343 without readline, though in that case callers must provide some extra
344 344 information on each call about the current line."""
345 345
346 346 Completer.__init__(self, namespace, global_namespace)
347 347
348 348 self.magic_escape = ESC_MAGIC
349 349 self.splitter = CompletionSplitter()
350 350
351 351 # Readline configuration, only used by the rlcompleter method.
352 352 if use_readline:
353 353 # We store the right version of readline so that later code
354 354 import IPython.utils.rlineimpl as readline
355 355 self.readline = readline
356 356 else:
357 357 self.readline = None
358 358
359 359 # List where completion matches will be stored
360 360 self.matches = []
361 361 self.omit__names = omit__names
362 362 self.merge_completions = shell.readline_merge_completions
363 363 self.shell = shell.shell
364 364 if alias_table is None:
365 365 alias_table = {}
366 366 self.alias_table = alias_table
367 367 # Regexp to split filenames with spaces in them
368 368 self.space_name_re = re.compile(r'([^\\] )')
369 369 # Hold a local ref. to glob.glob for speed
370 370 self.glob = glob.glob
371 371
372 372 # Determine if we are running on 'dumb' terminals, like (X)Emacs
373 373 # buffers, to avoid completion problems.
374 374 term = os.environ.get('TERM','xterm')
375 375 self.dumb_terminal = term in ['dumb','emacs']
376 376
377 377 # Special handling of backslashes needed in win32 platforms
378 378 if sys.platform == "win32":
379 379 self.clean_glob = self._clean_glob_win32
380 380 else:
381 381 self.clean_glob = self._clean_glob
382 382
383 383 # All active matcher routines for completion
384 384 self.matchers = [self.python_matches,
385 385 self.file_matches,
386 386 self.magic_matches,
387 387 self.alias_matches,
388 388 self.python_func_kw_matches,
389 389 ]
390 390
391 391 # Code contributed by Alex Schmolck, for ipython/emacs integration
392 392 def all_completions(self, text):
393 393 """Return all possible completions for the benefit of emacs."""
394 394
395 395 completions = []
396 396 comp_append = completions.append
397 397 try:
398 398 for i in xrange(sys.maxint):
399 399 res = self.complete(text, i, text)
400 400 if not res:
401 401 break
402 402 comp_append(res)
403 403 #XXX workaround for ``notDefined.<tab>``
404 404 except NameError:
405 405 pass
406 406 return completions
407 407 # /end Alex Schmolck code.
408 408
409 409 def _clean_glob(self,text):
410 410 return self.glob("%s*" % text)
411 411
412 412 def _clean_glob_win32(self,text):
413 413 return [f.replace("\\","/")
414 414 for f in self.glob("%s*" % text)]
415 415
416 416 def file_matches(self, text):
417 417 """Match filenames, expanding ~USER type strings.
418 418
419 419 Most of the seemingly convoluted logic in this completer is an
420 420 attempt to handle filenames with spaces in them. And yet it's not
421 421 quite perfect, because Python's readline doesn't expose all of the
422 422 GNU readline details needed for this to be done correctly.
423 423
424 424 For a filename with a space in it, the printed completions will be
425 425 only the parts after what's already been typed (instead of the
426 426 full completions, as is normally done). I don't think with the
427 427 current (as of Python 2.3) Python readline it's possible to do
428 428 better."""
429 429
430 430 #io.rprint('Completer->file_matches: <%s>' % text) # dbg
431 431
432 432 # chars that require escaping with backslash - i.e. chars
433 433 # that readline treats incorrectly as delimiters, but we
434 434 # don't want to treat as delimiters in filename matching
435 435 # when escaped with backslash
436 436 if text.startswith('!'):
437 437 text = text[1:]
438 438 text_prefix = '!'
439 439 else:
440 440 text_prefix = ''
441 441
442 442 text_until_cursor = self.text_until_cursor
443 443 open_quotes = 0 # track strings with open quotes
444 444 try:
445 445 lsplit = shlex.split(text_until_cursor)[-1]
446 446 except ValueError:
447 447 # typically an unmatched ", or backslash without escaped char.
448 448 if text_until_cursor.count('"')==1:
449 449 open_quotes = 1
450 450 lsplit = text_until_cursor.split('"')[-1]
451 451 elif text_until_cursor.count("'")==1:
452 452 open_quotes = 1
453 453 lsplit = text_until_cursor.split("'")[-1]
454 454 else:
455 455 return []
456 456 except IndexError:
457 457 # tab pressed on empty line
458 458 lsplit = ""
459 459
460 460 if not open_quotes and lsplit != protect_filename(lsplit):
461 461 # if protectables are found, do matching on the whole escaped
462 462 # name
463 463 has_protectables = 1
464 464 text0,text = text,lsplit
465 465 else:
466 466 has_protectables = 0
467 467 text = os.path.expanduser(text)
468 468
469 469 if text == "":
470 470 return [text_prefix + protect_filename(f) for f in self.glob("*")]
471 471
472 472 m0 = self.clean_glob(text.replace('\\',''))
473 473 if has_protectables:
474 474 # If we had protectables, we need to revert our changes to the
475 475 # beginning of filename so that we don't double-write the part
476 476 # of the filename we have so far
477 477 len_lsplit = len(lsplit)
478 478 matches = [text_prefix + text0 +
479 479 protect_filename(f[len_lsplit:]) for f in m0]
480 480 else:
481 481 if open_quotes:
482 482 # if we have a string with an open quote, we don't need to
483 483 # protect the names at all (and we _shouldn't_, as it
484 484 # would cause bugs when the filesystem call is made).
485 485 matches = m0
486 486 else:
487 487 matches = [text_prefix +
488 488 protect_filename(f) for f in m0]
489 489
490 490 #io.rprint('mm', matches) # dbg
491 491 return mark_dirs(matches)
492 492
493 493 def magic_matches(self, text):
494 494 """Match magics"""
495 495 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
496 496 # Get all shell magics now rather than statically, so magics loaded at
497 497 # runtime show up too
498 498 magics = self.shell.lsmagic()
499 499 pre = self.magic_escape
500 500 baretext = text.lstrip(pre)
501 501 return [ pre+m for m in magics if m.startswith(baretext)]
502 502
503 503 def alias_matches(self, text):
504 504 """Match internal system aliases"""
505 505 #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg
506 506
507 507 # if we are not in the first 'item', alias matching
508 508 # doesn't make sense - unless we are starting with 'sudo' command.
509 509 main_text = self.text_until_cursor.lstrip()
510 510 if ' ' in main_text and not main_text.startswith('sudo'):
511 511 return []
512 512 text = os.path.expanduser(text)
513 513 aliases = self.alias_table.keys()
514 514 if text == '':
515 515 return aliases
516 516 else:
517 517 return [a for a in aliases if a.startswith(text)]
518 518
519 519 def python_matches(self,text):
520 520 """Match attributes or global python names"""
521 521
522 522 #print 'Completer->python_matches, txt=%r' % text # dbg
523 523 if "." in text:
524 524 try:
525 525 matches = self.attr_matches(text)
526 526 if text.endswith('.') and self.omit__names:
527 527 if self.omit__names == 1:
528 528 # true if txt is _not_ a __ name, false otherwise:
529 529 no__name = (lambda txt:
530 530 re.match(r'.*\.__.*?__',txt) is None)
531 531 else:
532 532 # true if txt is _not_ a _ name, false otherwise:
533 533 no__name = (lambda txt:
534 534 re.match(r'.*\._.*?',txt) is None)
535 535 matches = filter(no__name, matches)
536 536 except NameError:
537 537 # catches <undefined attributes>.<tab>
538 538 matches = []
539 539 else:
540 540 matches = self.global_matches(text)
541 541
542 542 return matches
543 543
544 544 def _default_arguments(self, obj):
545 545 """Return the list of default arguments of obj if it is callable,
546 546 or empty list otherwise."""
547 547
548 548 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
549 549 # for classes, check for __init__,__new__
550 550 if inspect.isclass(obj):
551 551 obj = (getattr(obj,'__init__',None) or
552 552 getattr(obj,'__new__',None))
553 553 # for all others, check if they are __call__able
554 554 elif hasattr(obj, '__call__'):
555 555 obj = obj.__call__
556 556 # XXX: is there a way to handle the builtins ?
557 557 try:
558 558 args,_,_1,defaults = inspect.getargspec(obj)
559 559 if defaults:
560 560 return args[-len(defaults):]
561 561 except TypeError: pass
562 562 return []
563 563
564 564 def python_func_kw_matches(self,text):
565 565 """Match named parameters (kwargs) of the last open function"""
566 566
567 567 if "." in text: # a parameter cannot be dotted
568 568 return []
569 569 try: regexp = self.__funcParamsRegex
570 570 except AttributeError:
571 571 regexp = self.__funcParamsRegex = re.compile(r'''
572 572 '.*?' | # single quoted strings or
573 573 ".*?" | # double quoted strings or
574 574 \w+ | # identifier
575 575 \S # other characters
576 576 ''', re.VERBOSE | re.DOTALL)
577 577 # 1. find the nearest identifier that comes before an unclosed
578 578 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
579 579 tokens = regexp.findall(self.line_buffer)
580 580 tokens.reverse()
581 581 iterTokens = iter(tokens); openPar = 0
582 582 for token in iterTokens:
583 583 if token == ')':
584 584 openPar -= 1
585 585 elif token == '(':
586 586 openPar += 1
587 587 if openPar > 0:
588 588 # found the last unclosed parenthesis
589 589 break
590 590 else:
591 591 return []
592 592 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
593 593 ids = []
594 594 isId = re.compile(r'\w+$').match
595 595 while True:
596 596 try:
597 597 ids.append(iterTokens.next())
598 598 if not isId(ids[-1]):
599 599 ids.pop(); break
600 600 if not iterTokens.next() == '.':
601 601 break
602 602 except StopIteration:
603 603 break
604 604 # lookup the candidate callable matches either using global_matches
605 605 # or attr_matches for dotted names
606 606 if len(ids) == 1:
607 607 callableMatches = self.global_matches(ids[0])
608 608 else:
609 609 callableMatches = self.attr_matches('.'.join(ids[::-1]))
610 610 argMatches = []
611 611 for callableMatch in callableMatches:
612 612 try:
613 613 namedArgs = self._default_arguments(eval(callableMatch,
614 614 self.namespace))
615 615 except:
616 616 continue
617 617 for namedArg in namedArgs:
618 618 if namedArg.startswith(text):
619 619 argMatches.append("%s=" %namedArg)
620 620 return argMatches
621 621
622 def dispatch_custom_completer(self,text):
622 def dispatch_custom_completer(self, text):
623 623 #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg
624 624 line = self.line_buffer
625 625 if not line.strip():
626 626 return None
627
627
628 # Create a little structure to pass all the relevant information about
629 # the current completion to any custom completer.
628 630 event = Bunch()
629 631 event.line = line
630 632 event.symbol = text
631 633 cmd = line.split(None,1)[0]
632 634 event.command = cmd
635 event.text_until_cursor = self.text_until_cursor
636
633 637 #print "\ncustom:{%s]\n" % event # dbg
634 638
635 639 # for foo etc, try also to find completer for %foo
636 640 if not cmd.startswith(self.magic_escape):
637 641 try_magic = self.custom_completers.s_matches(
638 642 self.magic_escape + cmd)
639 643 else:
640 644 try_magic = []
641 645
642 646 for c in itertools.chain(self.custom_completers.s_matches(cmd),
643 647 try_magic,
644 648 self.custom_completers.flat_matches(self.text_until_cursor)):
645 649 #print "try",c # dbg
646 650 try:
647 651 res = c(event)
648 652 # first, try case sensitive match
649 653 withcase = [r for r in res if r.startswith(text)]
650 654 if withcase:
651 655 return withcase
652 656 # if none, then case insensitive ones are ok too
653 657 text_low = text.lower()
654 658 return [r for r in res if r.lower().startswith(text_low)]
655 659 except TryNext:
656 660 pass
657 661
658 662 return None
659 663
660 664 def complete(self, text=None, line_buffer=None, cursor_pos=None):
661 665 """Return the state-th possible completion for 'text'.
662 666
663 667 This is called successively with state == 0, 1, 2, ... until it
664 668 returns None. The completion should begin with 'text'.
665 669
666 670 Note that both the text and the line_buffer are optional, but at least
667 671 one of them must be given.
668 672
669 673 Parameters
670 674 ----------
671 675 text : string, optional
672 676 Text to perform the completion on. If not given, the line buffer
673 677 is split using the instance's CompletionSplitter object.
674 678
675 679 line_buffer : string, optional
676 680 If not given, the completer attempts to obtain the current line
677 681 buffer via readline. This keyword allows clients which are
678 682 requesting for text completions in non-readline contexts to inform
679 683 the completer of the entire text.
680 684
681 685 cursor_pos : int, optional
682 686 Index of the cursor in the full line buffer. Should be provided by
683 687 remote frontends where kernel has no access to frontend state.
684 688 """
685 689 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
686 690
687 691 # if the cursor position isn't given, the only sane assumption we can
688 692 # make is that it's at the end of the line (the common case)
689 693 if cursor_pos is None:
690 694 cursor_pos = len(line_buffer) if text is None else len(text)
691 695
692 696 # if text is either None or an empty string, rely on the line buffer
693 697 if not text:
694 698 text = self.splitter.split_line(line_buffer, cursor_pos)
695 699
696 700 # If no line buffer is given, assume the input text is all there was
697 701 if line_buffer is None:
698 702 line_buffer = text
699 703
700 magic_escape = self.magic_escape
701 704 self.line_buffer = line_buffer
702 705 self.text_until_cursor = self.line_buffer[:cursor_pos]
703 706 #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
704 707
705 708 # Start with a clean slate of completions
706 709 self.matches[:] = []
707 710 custom_res = self.dispatch_custom_completer(text)
708 711 if custom_res is not None:
709 712 # did custom completers produce something?
710 713 self.matches = custom_res
711 714 else:
712 715 # Extend the list of completions with the results of each
713 716 # matcher, so we return results to the user from all
714 717 # namespaces.
715 718 if self.merge_completions:
716 719 self.matches = []
717 720 for matcher in self.matchers:
718 721 self.matches.extend(matcher(text))
719 722 else:
720 723 for matcher in self.matchers:
721 724 self.matches = matcher(text)
722 725 if self.matches:
723 726 break
724 727 # FIXME: we should extend our api to return a dict with completions for
725 728 # different types of objects. The rlcomplete() method could then
726 729 # simply collapse the dict into a list for readline, but we'd have
727 730 # richer completion semantics in other evironments.
728 731 self.matches = sorted(set(self.matches))
729 732 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
730 733 return text, self.matches
731 734
732 735 def rlcomplete(self, text, state):
733 736 """Return the state-th possible completion for 'text'.
734 737
735 738 This is called successively with state == 0, 1, 2, ... until it
736 739 returns None. The completion should begin with 'text'.
737 740
738 741 Parameters
739 742 ----------
740 743 text : string
741 744 Text to perform the completion on.
742 745
743 746 state : int
744 747 Counter used by readline.
745 748 """
746 749 if state==0:
747 750
748 751 self.line_buffer = line_buffer = self.readline.get_line_buffer()
749 752 cursor_pos = self.readline.get_endidx()
750 753
751 754 #io.rprint("\nRLCOMPLETE: %r %r %r" %
752 755 # (text, line_buffer, cursor_pos) ) # dbg
753 756
754 757 # if there is only a tab on a line with only whitespace, instead of
755 758 # the mostly useless 'do you want to see all million completions'
756 759 # message, just do the right thing and give the user his tab!
757 760 # Incidentally, this enables pasting of tabbed text from an editor
758 761 # (as long as autoindent is off).
759 762
760 763 # It should be noted that at least pyreadline still shows file
761 764 # completions - is there a way around it?
762 765
763 766 # don't apply this on 'dumb' terminals, such as emacs buffers, so
764 767 # we don't interfere with their own tab-completion mechanism.
765 768 if not (self.dumb_terminal or line_buffer.strip()):
766 769 self.readline.insert_text('\t')
767 770 sys.stdout.flush()
768 771 return None
769 772
770 # This method computes the self.matches array
771 self.complete(text, line_buffer, cursor_pos)
772
773 # Debug version, since readline silences all exceptions making it
774 # impossible to debug any problem in the above code
775
776 ## try:
777 ## self.complete(text, line_buffer, cursor_pos)
778 ## except:
779 ## import traceback; traceback.print_exc()
773 # Note: debugging exceptions that may occur in completion is very
774 # tricky, because readline unconditionally silences them. So if
775 # during development you suspect a bug in the completion code, turn
776 # this flag on temporarily by uncommenting the second form (don't
777 # flip the value in the first line, as the '# dbg' marker can be
778 # automatically detected and is used elsewhere).
779 DEBUG = False
780 #DEBUG = True # dbg
781 if DEBUG:
782 try:
783 self.complete(text, line_buffer, cursor_pos)
784 except:
785 import traceback; traceback.print_exc()
786 else:
787 # The normal production version is here
788
789 # This method computes the self.matches array
790 self.complete(text, line_buffer, cursor_pos)
780 791
781 792 try:
782 793 return self.matches[state]
783 794 except IndexError:
784 795 return None
@@ -1,2390 +1,2403 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2010 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 from __future__ import with_statement
18 18 from __future__ import absolute_import
19 19
20 20 import __builtin__
21 21 import __future__
22 22 import abc
23 23 import atexit
24 24 import codeop
25 25 import exceptions
26 26 import new
27 27 import os
28 28 import re
29 29 import string
30 30 import sys
31 31 import tempfile
32 32 from contextlib import nested
33 33
34 34 from IPython.config.configurable import Configurable
35 35 from IPython.core import debugger, oinspect
36 36 from IPython.core import history as ipcorehist
37 37 from IPython.core import page
38 38 from IPython.core import prefilter
39 39 from IPython.core import shadowns
40 40 from IPython.core import ultratb
41 41 from IPython.core.alias import AliasManager
42 42 from IPython.core.builtin_trap import BuiltinTrap
43 43 from IPython.core.display_trap import DisplayTrap
44 44 from IPython.core.displayhook import DisplayHook
45 45 from IPython.core.error import TryNext, UsageError
46 46 from IPython.core.extensions import ExtensionManager
47 47 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
48 48 from IPython.core.inputlist import InputList
49 49 from IPython.core.logger import Logger
50 50 from IPython.core.magic import Magic
51 51 from IPython.core.payload import PayloadManager
52 52 from IPython.core.plugin import PluginManager
53 53 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
54 54 from IPython.external.Itpl import ItplNS
55 55 from IPython.utils import PyColorize
56 56 from IPython.utils import io
57 57 from IPython.utils import pickleshare
58 58 from IPython.utils.doctestreload import doctest_reload
59 59 from IPython.utils.io import ask_yes_no, rprint
60 60 from IPython.utils.ipstruct import Struct
61 61 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
62 62 from IPython.utils.process import system, getoutput
63 63 from IPython.utils.strdispatch import StrDispatch
64 64 from IPython.utils.syspathcontext import prepended_to_syspath
65 65 from IPython.utils.text import num_ini_spaces, format_screen
66 66 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
67 67 List, Unicode, Instance, Type)
68 68 from IPython.utils.warn import warn, error, fatal
69 69 import IPython.core.hooks
70 70
71 71 #-----------------------------------------------------------------------------
72 72 # Globals
73 73 #-----------------------------------------------------------------------------
74 74
75 75 # compiled regexps for autoindent management
76 76 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
77 77
78 78 #-----------------------------------------------------------------------------
79 79 # Utilities
80 80 #-----------------------------------------------------------------------------
81 81
82 82 # store the builtin raw_input globally, and use this always, in case user code
83 83 # overwrites it (like wx.py.PyShell does)
84 84 raw_input_original = raw_input
85 85
86 86 def softspace(file, newvalue):
87 87 """Copied from code.py, to remove the dependency"""
88 88
89 89 oldvalue = 0
90 90 try:
91 91 oldvalue = file.softspace
92 92 except AttributeError:
93 93 pass
94 94 try:
95 95 file.softspace = newvalue
96 96 except (AttributeError, TypeError):
97 97 # "attribute-less object" or "read-only attributes"
98 98 pass
99 99 return oldvalue
100 100
101 101
102 102 def no_op(*a, **kw): pass
103 103
104 104 class SpaceInInput(exceptions.Exception): pass
105 105
106 106 class Bunch: pass
107 107
108 108
109 109 def get_default_colors():
110 110 if sys.platform=='darwin':
111 111 return "LightBG"
112 112 elif os.name=='nt':
113 113 return 'Linux'
114 114 else:
115 115 return 'Linux'
116 116
117 117
118 118 class SeparateStr(Str):
119 119 """A Str subclass to validate separate_in, separate_out, etc.
120 120
121 121 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
122 122 """
123 123
124 124 def validate(self, obj, value):
125 125 if value == '0': value = ''
126 126 value = value.replace('\\n','\n')
127 127 return super(SeparateStr, self).validate(obj, value)
128 128
129 129 class MultipleInstanceError(Exception):
130 130 pass
131 131
132 132
133 133 #-----------------------------------------------------------------------------
134 134 # Main IPython class
135 135 #-----------------------------------------------------------------------------
136 136
137 137
138 138 class InteractiveShell(Configurable, Magic):
139 139 """An enhanced, interactive shell for Python."""
140 140
141 141 _instance = None
142 142 autocall = Enum((0,1,2), default_value=1, config=True)
143 143 # TODO: remove all autoindent logic and put into frontends.
144 144 # We can't do this yet because even runlines uses the autoindent.
145 145 autoindent = CBool(True, config=True)
146 146 automagic = CBool(True, config=True)
147 147 cache_size = Int(1000, config=True)
148 148 color_info = CBool(True, config=True)
149 149 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
150 150 default_value=get_default_colors(), config=True)
151 151 debug = CBool(False, config=True)
152 152 deep_reload = CBool(False, config=True)
153 153 displayhook_class = Type(DisplayHook)
154 154 exit_now = CBool(False)
155 155 filename = Str("<ipython console>")
156 156 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
157 157 logstart = CBool(False, config=True)
158 158 logfile = Str('', config=True)
159 159 logappend = Str('', config=True)
160 160 object_info_string_level = Enum((0,1,2), default_value=0,
161 161 config=True)
162 162 pdb = CBool(False, config=True)
163 163 pprint = CBool(True, config=True)
164 164 profile = Str('', config=True)
165 165 prompt_in1 = Str('In [\\#]: ', config=True)
166 166 prompt_in2 = Str(' .\\D.: ', config=True)
167 167 prompt_out = Str('Out[\\#]: ', config=True)
168 168 prompts_pad_left = CBool(True, config=True)
169 169 quiet = CBool(False, config=True)
170 170
171 171 # The readline stuff will eventually be moved to the terminal subclass
172 172 # but for now, we can't do that as readline is welded in everywhere.
173 173 readline_use = CBool(True, config=True)
174 174 readline_merge_completions = CBool(True, config=True)
175 175 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
176 176 readline_remove_delims = Str('-/~', config=True)
177 177 readline_parse_and_bind = List([
178 178 'tab: complete',
179 179 '"\C-l": clear-screen',
180 180 'set show-all-if-ambiguous on',
181 181 '"\C-o": tab-insert',
182 182 '"\M-i": " "',
183 183 '"\M-o": "\d\d\d\d"',
184 184 '"\M-I": "\d\d\d\d"',
185 185 '"\C-r": reverse-search-history',
186 186 '"\C-s": forward-search-history',
187 187 '"\C-p": history-search-backward',
188 188 '"\C-n": history-search-forward',
189 189 '"\e[A": history-search-backward',
190 190 '"\e[B": history-search-forward',
191 191 '"\C-k": kill-line',
192 192 '"\C-u": unix-line-discard',
193 193 ], allow_none=False, config=True)
194 194
195 195 # TODO: this part of prompt management should be moved to the frontends.
196 196 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
197 197 separate_in = SeparateStr('\n', config=True)
198 198 separate_out = SeparateStr('', config=True)
199 199 separate_out2 = SeparateStr('', config=True)
200 200 wildcards_case_sensitive = CBool(True, config=True)
201 201 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
202 202 default_value='Context', config=True)
203 203
204 204 # Subcomponents of InteractiveShell
205 205 alias_manager = Instance('IPython.core.alias.AliasManager')
206 206 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
207 207 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
208 208 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
209 209 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
210 210 plugin_manager = Instance('IPython.core.plugin.PluginManager')
211 211 payload_manager = Instance('IPython.core.payload.PayloadManager')
212 212
213 213 def __init__(self, config=None, ipython_dir=None,
214 214 user_ns=None, user_global_ns=None,
215 215 custom_exceptions=((),None)):
216 216
217 217 # This is where traits with a config_key argument are updated
218 218 # from the values on config.
219 219 super(InteractiveShell, self).__init__(config=config)
220 220
221 221 # These are relatively independent and stateless
222 222 self.init_ipython_dir(ipython_dir)
223 223 self.init_instance_attrs()
224 224
225 225 # Create namespaces (user_ns, user_global_ns, etc.)
226 226 self.init_create_namespaces(user_ns, user_global_ns)
227 227 # This has to be done after init_create_namespaces because it uses
228 228 # something in self.user_ns, but before init_sys_modules, which
229 229 # is the first thing to modify sys.
230 230 # TODO: When we override sys.stdout and sys.stderr before this class
231 231 # is created, we are saving the overridden ones here. Not sure if this
232 232 # is what we want to do.
233 233 self.save_sys_module_state()
234 234 self.init_sys_modules()
235 235
236 236 self.init_history()
237 237 self.init_encoding()
238 238 self.init_prefilter()
239 239
240 240 Magic.__init__(self, self)
241 241
242 242 self.init_syntax_highlighting()
243 243 self.init_hooks()
244 244 self.init_pushd_popd_magic()
245 245 # self.init_traceback_handlers use to be here, but we moved it below
246 246 # because it and init_io have to come after init_readline.
247 247 self.init_user_ns()
248 248 self.init_logger()
249 249 self.init_alias()
250 250 self.init_builtins()
251 251
252 252 # pre_config_initialization
253 253 self.init_shadow_hist()
254 254
255 255 # The next section should contain averything that was in ipmaker.
256 256 self.init_logstart()
257 257
258 258 # The following was in post_config_initialization
259 259 self.init_inspector()
260 260 # init_readline() must come before init_io(), because init_io uses
261 261 # readline related things.
262 262 self.init_readline()
263 263 # init_completer must come after init_readline, because it needs to
264 264 # know whether readline is present or not system-wide to configure the
265 265 # completers, since the completion machinery can now operate
266 266 # independently of readline (e.g. over the network)
267 267 self.init_completer()
268 268 # TODO: init_io() needs to happen before init_traceback handlers
269 269 # because the traceback handlers hardcode the stdout/stderr streams.
270 270 # This logic in in debugger.Pdb and should eventually be changed.
271 271 self.init_io()
272 272 self.init_traceback_handlers(custom_exceptions)
273 273 self.init_prompts()
274 274 self.init_displayhook()
275 275 self.init_reload_doctest()
276 276 self.init_magics()
277 277 self.init_pdb()
278 278 self.init_extension_manager()
279 279 self.init_plugin_manager()
280 280 self.init_payload()
281 281 self.hooks.late_startup_hook()
282 282 atexit.register(self.atexit_operations)
283 283
284 284 @classmethod
285 285 def instance(cls, *args, **kwargs):
286 286 """Returns a global InteractiveShell instance."""
287 287 if cls._instance is None:
288 288 inst = cls(*args, **kwargs)
289 289 # Now make sure that the instance will also be returned by
290 290 # the subclasses instance attribute.
291 291 for subclass in cls.mro():
292 292 if issubclass(cls, subclass) and \
293 293 issubclass(subclass, InteractiveShell):
294 294 subclass._instance = inst
295 295 else:
296 296 break
297 297 if isinstance(cls._instance, cls):
298 298 return cls._instance
299 299 else:
300 300 raise MultipleInstanceError(
301 301 'Multiple incompatible subclass instances of '
302 302 'InteractiveShell are being created.'
303 303 )
304 304
305 305 @classmethod
306 306 def initialized(cls):
307 307 return hasattr(cls, "_instance")
308 308
309 309 def get_ipython(self):
310 310 """Return the currently running IPython instance."""
311 311 return self
312 312
313 313 #-------------------------------------------------------------------------
314 314 # Trait changed handlers
315 315 #-------------------------------------------------------------------------
316 316
317 317 def _ipython_dir_changed(self, name, new):
318 318 if not os.path.isdir(new):
319 319 os.makedirs(new, mode = 0777)
320 320
321 321 def set_autoindent(self,value=None):
322 322 """Set the autoindent flag, checking for readline support.
323 323
324 324 If called with no arguments, it acts as a toggle."""
325 325
326 326 if not self.has_readline:
327 327 if os.name == 'posix':
328 328 warn("The auto-indent feature requires the readline library")
329 329 self.autoindent = 0
330 330 return
331 331 if value is None:
332 332 self.autoindent = not self.autoindent
333 333 else:
334 334 self.autoindent = value
335 335
336 336 #-------------------------------------------------------------------------
337 337 # init_* methods called by __init__
338 338 #-------------------------------------------------------------------------
339 339
340 340 def init_ipython_dir(self, ipython_dir):
341 341 if ipython_dir is not None:
342 342 self.ipython_dir = ipython_dir
343 343 self.config.Global.ipython_dir = self.ipython_dir
344 344 return
345 345
346 346 if hasattr(self.config.Global, 'ipython_dir'):
347 347 self.ipython_dir = self.config.Global.ipython_dir
348 348 else:
349 349 self.ipython_dir = get_ipython_dir()
350 350
351 351 # All children can just read this
352 352 self.config.Global.ipython_dir = self.ipython_dir
353 353
354 354 def init_instance_attrs(self):
355 355 self.more = False
356 356
357 357 # command compiler
358 358 self.compile = codeop.CommandCompiler()
359 359
360 360 # User input buffer
361 361 self.buffer = []
362 362
363 363 # Make an empty namespace, which extension writers can rely on both
364 364 # existing and NEVER being used by ipython itself. This gives them a
365 365 # convenient location for storing additional information and state
366 366 # their extensions may require, without fear of collisions with other
367 367 # ipython names that may develop later.
368 368 self.meta = Struct()
369 369
370 370 # Object variable to store code object waiting execution. This is
371 371 # used mainly by the multithreaded shells, but it can come in handy in
372 372 # other situations. No need to use a Queue here, since it's a single
373 373 # item which gets cleared once run.
374 374 self.code_to_run = None
375 375
376 376 # Temporary files used for various purposes. Deleted at exit.
377 377 self.tempfiles = []
378 378
379 379 # Keep track of readline usage (later set by init_readline)
380 380 self.has_readline = False
381 381
382 382 # keep track of where we started running (mainly for crash post-mortem)
383 383 # This is not being used anywhere currently.
384 384 self.starting_dir = os.getcwd()
385 385
386 386 # Indentation management
387 387 self.indent_current_nsp = 0
388 388
389 389 def init_encoding(self):
390 390 # Get system encoding at startup time. Certain terminals (like Emacs
391 391 # under Win32 have it set to None, and we need to have a known valid
392 392 # encoding to use in the raw_input() method
393 393 try:
394 394 self.stdin_encoding = sys.stdin.encoding or 'ascii'
395 395 except AttributeError:
396 396 self.stdin_encoding = 'ascii'
397 397
398 398 def init_syntax_highlighting(self):
399 399 # Python source parser/formatter for syntax highlighting
400 400 pyformat = PyColorize.Parser().format
401 401 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
402 402
403 403 def init_pushd_popd_magic(self):
404 404 # for pushd/popd management
405 405 try:
406 406 self.home_dir = get_home_dir()
407 407 except HomeDirError, msg:
408 408 fatal(msg)
409 409
410 410 self.dir_stack = []
411 411
412 412 def init_logger(self):
413 413 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
414 414 # local shortcut, this is used a LOT
415 415 self.log = self.logger.log
416 416
417 417 def init_logstart(self):
418 418 if self.logappend:
419 419 self.magic_logstart(self.logappend + ' append')
420 420 elif self.logfile:
421 421 self.magic_logstart(self.logfile)
422 422 elif self.logstart:
423 423 self.magic_logstart()
424 424
425 425 def init_builtins(self):
426 426 self.builtin_trap = BuiltinTrap(shell=self)
427 427
428 428 def init_inspector(self):
429 429 # Object inspector
430 430 self.inspector = oinspect.Inspector(oinspect.InspectColors,
431 431 PyColorize.ANSICodeColors,
432 432 'NoColor',
433 433 self.object_info_string_level)
434 434
435 435 def init_io(self):
436 436 import IPython.utils.io
437 437 if sys.platform == 'win32' and self.has_readline:
438 438 Term = io.IOTerm(
439 439 cout=self.readline._outputfile,cerr=self.readline._outputfile
440 440 )
441 441 else:
442 442 Term = io.IOTerm()
443 443 io.Term = Term
444 444
445 445 def init_prompts(self):
446 446 # TODO: This is a pass for now because the prompts are managed inside
447 447 # the DisplayHook. Once there is a separate prompt manager, this
448 448 # will initialize that object and all prompt related information.
449 449 pass
450 450
451 451 def init_displayhook(self):
452 452 # Initialize displayhook, set in/out prompts and printing system
453 453 self.displayhook = self.displayhook_class(
454 454 shell=self,
455 455 cache_size=self.cache_size,
456 456 input_sep = self.separate_in,
457 457 output_sep = self.separate_out,
458 458 output_sep2 = self.separate_out2,
459 459 ps1 = self.prompt_in1,
460 460 ps2 = self.prompt_in2,
461 461 ps_out = self.prompt_out,
462 462 pad_left = self.prompts_pad_left
463 463 )
464 464 # This is a context manager that installs/revmoes the displayhook at
465 465 # the appropriate time.
466 466 self.display_trap = DisplayTrap(hook=self.displayhook)
467 467
468 468 def init_reload_doctest(self):
469 469 # Do a proper resetting of doctest, including the necessary displayhook
470 470 # monkeypatching
471 471 try:
472 472 doctest_reload()
473 473 except ImportError:
474 474 warn("doctest module does not exist.")
475 475
476 476 #-------------------------------------------------------------------------
477 477 # Things related to injections into the sys module
478 478 #-------------------------------------------------------------------------
479 479
480 480 def save_sys_module_state(self):
481 481 """Save the state of hooks in the sys module.
482 482
483 483 This has to be called after self.user_ns is created.
484 484 """
485 485 self._orig_sys_module_state = {}
486 486 self._orig_sys_module_state['stdin'] = sys.stdin
487 487 self._orig_sys_module_state['stdout'] = sys.stdout
488 488 self._orig_sys_module_state['stderr'] = sys.stderr
489 489 self._orig_sys_module_state['excepthook'] = sys.excepthook
490 490 try:
491 491 self._orig_sys_modules_main_name = self.user_ns['__name__']
492 492 except KeyError:
493 493 pass
494 494
495 495 def restore_sys_module_state(self):
496 496 """Restore the state of the sys module."""
497 497 try:
498 498 for k, v in self._orig_sys_module_state.items():
499 499 setattr(sys, k, v)
500 500 except AttributeError:
501 501 pass
502 502 # Reset what what done in self.init_sys_modules
503 503 try:
504 504 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
505 505 except (AttributeError, KeyError):
506 506 pass
507 507
508 508 #-------------------------------------------------------------------------
509 509 # Things related to hooks
510 510 #-------------------------------------------------------------------------
511 511
512 512 def init_hooks(self):
513 513 # hooks holds pointers used for user-side customizations
514 514 self.hooks = Struct()
515 515
516 516 self.strdispatchers = {}
517 517
518 518 # Set all default hooks, defined in the IPython.hooks module.
519 519 hooks = IPython.core.hooks
520 520 for hook_name in hooks.__all__:
521 521 # default hooks have priority 100, i.e. low; user hooks should have
522 522 # 0-100 priority
523 523 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
524 524
525 525 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
526 526 """set_hook(name,hook) -> sets an internal IPython hook.
527 527
528 528 IPython exposes some of its internal API as user-modifiable hooks. By
529 529 adding your function to one of these hooks, you can modify IPython's
530 530 behavior to call at runtime your own routines."""
531 531
532 532 # At some point in the future, this should validate the hook before it
533 533 # accepts it. Probably at least check that the hook takes the number
534 534 # of args it's supposed to.
535 535
536 536 f = new.instancemethod(hook,self,self.__class__)
537 537
538 538 # check if the hook is for strdispatcher first
539 539 if str_key is not None:
540 540 sdp = self.strdispatchers.get(name, StrDispatch())
541 541 sdp.add_s(str_key, f, priority )
542 542 self.strdispatchers[name] = sdp
543 543 return
544 544 if re_key is not None:
545 545 sdp = self.strdispatchers.get(name, StrDispatch())
546 546 sdp.add_re(re.compile(re_key), f, priority )
547 547 self.strdispatchers[name] = sdp
548 548 return
549 549
550 550 dp = getattr(self.hooks, name, None)
551 551 if name not in IPython.core.hooks.__all__:
552 552 print "Warning! Hook '%s' is not one of %s" % \
553 553 (name, IPython.core.hooks.__all__ )
554 554 if not dp:
555 555 dp = IPython.core.hooks.CommandChainDispatcher()
556 556
557 557 try:
558 558 dp.add(f,priority)
559 559 except AttributeError:
560 560 # it was not commandchain, plain old func - replace
561 561 dp = f
562 562
563 563 setattr(self.hooks,name, dp)
564 564
565 565 #-------------------------------------------------------------------------
566 566 # Things related to the "main" module
567 567 #-------------------------------------------------------------------------
568 568
569 569 def new_main_mod(self,ns=None):
570 570 """Return a new 'main' module object for user code execution.
571 571 """
572 572 main_mod = self._user_main_module
573 573 init_fakemod_dict(main_mod,ns)
574 574 return main_mod
575 575
576 576 def cache_main_mod(self,ns,fname):
577 577 """Cache a main module's namespace.
578 578
579 579 When scripts are executed via %run, we must keep a reference to the
580 580 namespace of their __main__ module (a FakeModule instance) around so
581 581 that Python doesn't clear it, rendering objects defined therein
582 582 useless.
583 583
584 584 This method keeps said reference in a private dict, keyed by the
585 585 absolute path of the module object (which corresponds to the script
586 586 path). This way, for multiple executions of the same script we only
587 587 keep one copy of the namespace (the last one), thus preventing memory
588 588 leaks from old references while allowing the objects from the last
589 589 execution to be accessible.
590 590
591 591 Note: we can not allow the actual FakeModule instances to be deleted,
592 592 because of how Python tears down modules (it hard-sets all their
593 593 references to None without regard for reference counts). This method
594 594 must therefore make a *copy* of the given namespace, to allow the
595 595 original module's __dict__ to be cleared and reused.
596 596
597 597
598 598 Parameters
599 599 ----------
600 600 ns : a namespace (a dict, typically)
601 601
602 602 fname : str
603 603 Filename associated with the namespace.
604 604
605 605 Examples
606 606 --------
607 607
608 608 In [10]: import IPython
609 609
610 610 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
611 611
612 612 In [12]: IPython.__file__ in _ip._main_ns_cache
613 613 Out[12]: True
614 614 """
615 615 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
616 616
617 617 def clear_main_mod_cache(self):
618 618 """Clear the cache of main modules.
619 619
620 620 Mainly for use by utilities like %reset.
621 621
622 622 Examples
623 623 --------
624 624
625 625 In [15]: import IPython
626 626
627 627 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
628 628
629 629 In [17]: len(_ip._main_ns_cache) > 0
630 630 Out[17]: True
631 631
632 632 In [18]: _ip.clear_main_mod_cache()
633 633
634 634 In [19]: len(_ip._main_ns_cache) == 0
635 635 Out[19]: True
636 636 """
637 637 self._main_ns_cache.clear()
638 638
639 639 #-------------------------------------------------------------------------
640 640 # Things related to debugging
641 641 #-------------------------------------------------------------------------
642 642
643 643 def init_pdb(self):
644 644 # Set calling of pdb on exceptions
645 645 # self.call_pdb is a property
646 646 self.call_pdb = self.pdb
647 647
648 648 def _get_call_pdb(self):
649 649 return self._call_pdb
650 650
651 651 def _set_call_pdb(self,val):
652 652
653 653 if val not in (0,1,False,True):
654 654 raise ValueError,'new call_pdb value must be boolean'
655 655
656 656 # store value in instance
657 657 self._call_pdb = val
658 658
659 659 # notify the actual exception handlers
660 660 self.InteractiveTB.call_pdb = val
661 661
662 662 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
663 663 'Control auto-activation of pdb at exceptions')
664 664
665 665 def debugger(self,force=False):
666 666 """Call the pydb/pdb debugger.
667 667
668 668 Keywords:
669 669
670 670 - force(False): by default, this routine checks the instance call_pdb
671 671 flag and does not actually invoke the debugger if the flag is false.
672 672 The 'force' option forces the debugger to activate even if the flag
673 673 is false.
674 674 """
675 675
676 676 if not (force or self.call_pdb):
677 677 return
678 678
679 679 if not hasattr(sys,'last_traceback'):
680 680 error('No traceback has been produced, nothing to debug.')
681 681 return
682 682
683 683 # use pydb if available
684 684 if debugger.has_pydb:
685 685 from pydb import pm
686 686 else:
687 687 # fallback to our internal debugger
688 688 pm = lambda : self.InteractiveTB.debugger(force=True)
689 689 self.history_saving_wrapper(pm)()
690 690
691 691 #-------------------------------------------------------------------------
692 692 # Things related to IPython's various namespaces
693 693 #-------------------------------------------------------------------------
694 694
695 695 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
696 696 # Create the namespace where the user will operate. user_ns is
697 697 # normally the only one used, and it is passed to the exec calls as
698 698 # the locals argument. But we do carry a user_global_ns namespace
699 699 # given as the exec 'globals' argument, This is useful in embedding
700 700 # situations where the ipython shell opens in a context where the
701 701 # distinction between locals and globals is meaningful. For
702 702 # non-embedded contexts, it is just the same object as the user_ns dict.
703 703
704 704 # FIXME. For some strange reason, __builtins__ is showing up at user
705 705 # level as a dict instead of a module. This is a manual fix, but I
706 706 # should really track down where the problem is coming from. Alex
707 707 # Schmolck reported this problem first.
708 708
709 709 # A useful post by Alex Martelli on this topic:
710 710 # Re: inconsistent value from __builtins__
711 711 # Von: Alex Martelli <aleaxit@yahoo.com>
712 712 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
713 713 # Gruppen: comp.lang.python
714 714
715 715 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
716 716 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
717 717 # > <type 'dict'>
718 718 # > >>> print type(__builtins__)
719 719 # > <type 'module'>
720 720 # > Is this difference in return value intentional?
721 721
722 722 # Well, it's documented that '__builtins__' can be either a dictionary
723 723 # or a module, and it's been that way for a long time. Whether it's
724 724 # intentional (or sensible), I don't know. In any case, the idea is
725 725 # that if you need to access the built-in namespace directly, you
726 726 # should start with "import __builtin__" (note, no 's') which will
727 727 # definitely give you a module. Yeah, it's somewhat confusing:-(.
728 728
729 729 # These routines return properly built dicts as needed by the rest of
730 730 # the code, and can also be used by extension writers to generate
731 731 # properly initialized namespaces.
732 732 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
733 733 user_global_ns)
734 734
735 735 # Assign namespaces
736 736 # This is the namespace where all normal user variables live
737 737 self.user_ns = user_ns
738 738 self.user_global_ns = user_global_ns
739 739
740 740 # An auxiliary namespace that checks what parts of the user_ns were
741 741 # loaded at startup, so we can list later only variables defined in
742 742 # actual interactive use. Since it is always a subset of user_ns, it
743 743 # doesn't need to be separately tracked in the ns_table.
744 744 self.user_ns_hidden = {}
745 745
746 746 # A namespace to keep track of internal data structures to prevent
747 747 # them from cluttering user-visible stuff. Will be updated later
748 748 self.internal_ns = {}
749 749
750 750 # Now that FakeModule produces a real module, we've run into a nasty
751 751 # problem: after script execution (via %run), the module where the user
752 752 # code ran is deleted. Now that this object is a true module (needed
753 753 # so docetst and other tools work correctly), the Python module
754 754 # teardown mechanism runs over it, and sets to None every variable
755 755 # present in that module. Top-level references to objects from the
756 756 # script survive, because the user_ns is updated with them. However,
757 757 # calling functions defined in the script that use other things from
758 758 # the script will fail, because the function's closure had references
759 759 # to the original objects, which are now all None. So we must protect
760 760 # these modules from deletion by keeping a cache.
761 761 #
762 762 # To avoid keeping stale modules around (we only need the one from the
763 763 # last run), we use a dict keyed with the full path to the script, so
764 764 # only the last version of the module is held in the cache. Note,
765 765 # however, that we must cache the module *namespace contents* (their
766 766 # __dict__). Because if we try to cache the actual modules, old ones
767 767 # (uncached) could be destroyed while still holding references (such as
768 768 # those held by GUI objects that tend to be long-lived)>
769 769 #
770 770 # The %reset command will flush this cache. See the cache_main_mod()
771 771 # and clear_main_mod_cache() methods for details on use.
772 772
773 773 # This is the cache used for 'main' namespaces
774 774 self._main_ns_cache = {}
775 775 # And this is the single instance of FakeModule whose __dict__ we keep
776 776 # copying and clearing for reuse on each %run
777 777 self._user_main_module = FakeModule()
778 778
779 779 # A table holding all the namespaces IPython deals with, so that
780 780 # introspection facilities can search easily.
781 781 self.ns_table = {'user':user_ns,
782 782 'user_global':user_global_ns,
783 783 'internal':self.internal_ns,
784 784 'builtin':__builtin__.__dict__
785 785 }
786 786
787 787 # Similarly, track all namespaces where references can be held and that
788 788 # we can safely clear (so it can NOT include builtin). This one can be
789 789 # a simple list.
790 790 self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
791 791 self.internal_ns, self._main_ns_cache ]
792 792
793 793 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
794 794 """Return a valid local and global user interactive namespaces.
795 795
796 796 This builds a dict with the minimal information needed to operate as a
797 797 valid IPython user namespace, which you can pass to the various
798 798 embedding classes in ipython. The default implementation returns the
799 799 same dict for both the locals and the globals to allow functions to
800 800 refer to variables in the namespace. Customized implementations can
801 801 return different dicts. The locals dictionary can actually be anything
802 802 following the basic mapping protocol of a dict, but the globals dict
803 803 must be a true dict, not even a subclass. It is recommended that any
804 804 custom object for the locals namespace synchronize with the globals
805 805 dict somehow.
806 806
807 807 Raises TypeError if the provided globals namespace is not a true dict.
808 808
809 809 Parameters
810 810 ----------
811 811 user_ns : dict-like, optional
812 812 The current user namespace. The items in this namespace should
813 813 be included in the output. If None, an appropriate blank
814 814 namespace should be created.
815 815 user_global_ns : dict, optional
816 816 The current user global namespace. The items in this namespace
817 817 should be included in the output. If None, an appropriate
818 818 blank namespace should be created.
819 819
820 820 Returns
821 821 -------
822 822 A pair of dictionary-like object to be used as the local namespace
823 823 of the interpreter and a dict to be used as the global namespace.
824 824 """
825 825
826 826
827 827 # We must ensure that __builtin__ (without the final 's') is always
828 828 # available and pointing to the __builtin__ *module*. For more details:
829 829 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
830 830
831 831 if user_ns is None:
832 832 # Set __name__ to __main__ to better match the behavior of the
833 833 # normal interpreter.
834 834 user_ns = {'__name__' :'__main__',
835 835 '__builtin__' : __builtin__,
836 836 '__builtins__' : __builtin__,
837 837 }
838 838 else:
839 839 user_ns.setdefault('__name__','__main__')
840 840 user_ns.setdefault('__builtin__',__builtin__)
841 841 user_ns.setdefault('__builtins__',__builtin__)
842 842
843 843 if user_global_ns is None:
844 844 user_global_ns = user_ns
845 845 if type(user_global_ns) is not dict:
846 846 raise TypeError("user_global_ns must be a true dict; got %r"
847 847 % type(user_global_ns))
848 848
849 849 return user_ns, user_global_ns
850 850
851 851 def init_sys_modules(self):
852 852 # We need to insert into sys.modules something that looks like a
853 853 # module but which accesses the IPython namespace, for shelve and
854 854 # pickle to work interactively. Normally they rely on getting
855 855 # everything out of __main__, but for embedding purposes each IPython
856 856 # instance has its own private namespace, so we can't go shoving
857 857 # everything into __main__.
858 858
859 859 # note, however, that we should only do this for non-embedded
860 860 # ipythons, which really mimic the __main__.__dict__ with their own
861 861 # namespace. Embedded instances, on the other hand, should not do
862 862 # this because they need to manage the user local/global namespaces
863 863 # only, but they live within a 'normal' __main__ (meaning, they
864 864 # shouldn't overtake the execution environment of the script they're
865 865 # embedded in).
866 866
867 867 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
868 868
869 869 try:
870 870 main_name = self.user_ns['__name__']
871 871 except KeyError:
872 872 raise KeyError('user_ns dictionary MUST have a "__name__" key')
873 873 else:
874 874 sys.modules[main_name] = FakeModule(self.user_ns)
875 875
876 876 def init_user_ns(self):
877 877 """Initialize all user-visible namespaces to their minimum defaults.
878 878
879 879 Certain history lists are also initialized here, as they effectively
880 880 act as user namespaces.
881 881
882 882 Notes
883 883 -----
884 884 All data structures here are only filled in, they are NOT reset by this
885 885 method. If they were not empty before, data will simply be added to
886 886 therm.
887 887 """
888 888 # This function works in two parts: first we put a few things in
889 889 # user_ns, and we sync that contents into user_ns_hidden so that these
890 890 # initial variables aren't shown by %who. After the sync, we add the
891 891 # rest of what we *do* want the user to see with %who even on a new
892 892 # session (probably nothing, so theye really only see their own stuff)
893 893
894 894 # The user dict must *always* have a __builtin__ reference to the
895 895 # Python standard __builtin__ namespace, which must be imported.
896 896 # This is so that certain operations in prompt evaluation can be
897 897 # reliably executed with builtins. Note that we can NOT use
898 898 # __builtins__ (note the 's'), because that can either be a dict or a
899 899 # module, and can even mutate at runtime, depending on the context
900 900 # (Python makes no guarantees on it). In contrast, __builtin__ is
901 901 # always a module object, though it must be explicitly imported.
902 902
903 903 # For more details:
904 904 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
905 905 ns = dict(__builtin__ = __builtin__)
906 906
907 907 # Put 'help' in the user namespace
908 908 try:
909 909 from site import _Helper
910 910 ns['help'] = _Helper()
911 911 except ImportError:
912 912 warn('help() not available - check site.py')
913 913
914 914 # make global variables for user access to the histories
915 915 ns['_ih'] = self.input_hist
916 916 ns['_oh'] = self.output_hist
917 917 ns['_dh'] = self.dir_hist
918 918
919 919 ns['_sh'] = shadowns
920 920
921 921 # user aliases to input and output histories. These shouldn't show up
922 922 # in %who, as they can have very large reprs.
923 923 ns['In'] = self.input_hist
924 924 ns['Out'] = self.output_hist
925 925
926 926 # Store myself as the public api!!!
927 927 ns['get_ipython'] = self.get_ipython
928 928
929 929 # Sync what we've added so far to user_ns_hidden so these aren't seen
930 930 # by %who
931 931 self.user_ns_hidden.update(ns)
932 932
933 933 # Anything put into ns now would show up in %who. Think twice before
934 934 # putting anything here, as we really want %who to show the user their
935 935 # stuff, not our variables.
936 936
937 937 # Finally, update the real user's namespace
938 938 self.user_ns.update(ns)
939 939
940 940
941 941 def reset(self):
942 942 """Clear all internal namespaces.
943 943
944 944 Note that this is much more aggressive than %reset, since it clears
945 945 fully all namespaces, as well as all input/output lists.
946 946 """
947 947 for ns in self.ns_refs_table:
948 948 ns.clear()
949 949
950 950 self.alias_manager.clear_aliases()
951 951
952 952 # Clear input and output histories
953 953 self.input_hist[:] = []
954 954 self.input_hist_raw[:] = []
955 955 self.output_hist.clear()
956 956
957 957 # Restore the user namespaces to minimal usability
958 958 self.init_user_ns()
959 959
960 960 # Restore the default and user aliases
961 961 self.alias_manager.init_aliases()
962 962
963 963 def reset_selective(self, regex=None):
964 964 """Clear selective variables from internal namespaces based on a
965 965 specified regular expression.
966 966
967 967 Parameters
968 968 ----------
969 969 regex : string or compiled pattern, optional
970 970 A regular expression pattern that will be used in searching
971 971 variable names in the users namespaces.
972 972 """
973 973 if regex is not None:
974 974 try:
975 975 m = re.compile(regex)
976 976 except TypeError:
977 977 raise TypeError('regex must be a string or compiled pattern')
978 978 # Search for keys in each namespace that match the given regex
979 979 # If a match is found, delete the key/value pair.
980 980 for ns in self.ns_refs_table:
981 981 for var in ns:
982 982 if m.search(var):
983 983 del ns[var]
984 984
985 985 def push(self, variables, interactive=True):
986 986 """Inject a group of variables into the IPython user namespace.
987 987
988 988 Parameters
989 989 ----------
990 990 variables : dict, str or list/tuple of str
991 991 The variables to inject into the user's namespace. If a dict, a
992 992 simple update is done. If a str, the string is assumed to have
993 993 variable names separated by spaces. A list/tuple of str can also
994 994 be used to give the variable names. If just the variable names are
995 995 give (list/tuple/str) then the variable values looked up in the
996 996 callers frame.
997 997 interactive : bool
998 998 If True (default), the variables will be listed with the ``who``
999 999 magic.
1000 1000 """
1001 1001 vdict = None
1002 1002
1003 1003 # We need a dict of name/value pairs to do namespace updates.
1004 1004 if isinstance(variables, dict):
1005 1005 vdict = variables
1006 1006 elif isinstance(variables, (basestring, list, tuple)):
1007 1007 if isinstance(variables, basestring):
1008 1008 vlist = variables.split()
1009 1009 else:
1010 1010 vlist = variables
1011 1011 vdict = {}
1012 1012 cf = sys._getframe(1)
1013 1013 for name in vlist:
1014 1014 try:
1015 1015 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1016 1016 except:
1017 1017 print ('Could not get variable %s from %s' %
1018 1018 (name,cf.f_code.co_name))
1019 1019 else:
1020 1020 raise ValueError('variables must be a dict/str/list/tuple')
1021 1021
1022 1022 # Propagate variables to user namespace
1023 1023 self.user_ns.update(vdict)
1024 1024
1025 1025 # And configure interactive visibility
1026 1026 config_ns = self.user_ns_hidden
1027 1027 if interactive:
1028 1028 for name, val in vdict.iteritems():
1029 1029 config_ns.pop(name, None)
1030 1030 else:
1031 1031 for name,val in vdict.iteritems():
1032 1032 config_ns[name] = val
1033 1033
1034 1034 #-------------------------------------------------------------------------
1035 1035 # Things related to object introspection
1036 1036 #-------------------------------------------------------------------------
1037 1037 def _ofind(self, oname, namespaces=None):
1038 1038 """Find an object in the available namespaces.
1039 1039
1040 1040 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1041 1041
1042 1042 Has special code to detect magic functions.
1043 1043 """
1044 1044 #oname = oname.strip()
1045 1045 #print '1- oname: <%r>' % oname # dbg
1046 1046 try:
1047 1047 oname = oname.strip().encode('ascii')
1048 1048 #print '2- oname: <%r>' % oname # dbg
1049 1049 except UnicodeEncodeError:
1050 1050 print 'Python identifiers can only contain ascii characters.'
1051 1051 return dict(found=False)
1052 1052
1053 1053 alias_ns = None
1054 1054 if namespaces is None:
1055 1055 # Namespaces to search in:
1056 1056 # Put them in a list. The order is important so that we
1057 1057 # find things in the same order that Python finds them.
1058 1058 namespaces = [ ('Interactive', self.user_ns),
1059 1059 ('IPython internal', self.internal_ns),
1060 1060 ('Python builtin', __builtin__.__dict__),
1061 1061 ('Alias', self.alias_manager.alias_table),
1062 1062 ]
1063 1063 alias_ns = self.alias_manager.alias_table
1064 1064
1065 1065 # initialize results to 'null'
1066 1066 found = False; obj = None; ospace = None; ds = None;
1067 1067 ismagic = False; isalias = False; parent = None
1068 1068
1069 1069 # We need to special-case 'print', which as of python2.6 registers as a
1070 1070 # function but should only be treated as one if print_function was
1071 1071 # loaded with a future import. In this case, just bail.
1072 1072 if (oname == 'print' and not (self.compile.compiler.flags &
1073 1073 __future__.CO_FUTURE_PRINT_FUNCTION)):
1074 1074 return {'found':found, 'obj':obj, 'namespace':ospace,
1075 1075 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1076 1076
1077 1077 # Look for the given name by splitting it in parts. If the head is
1078 1078 # found, then we look for all the remaining parts as members, and only
1079 1079 # declare success if we can find them all.
1080 1080 oname_parts = oname.split('.')
1081 1081 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1082 1082 for nsname,ns in namespaces:
1083 1083 try:
1084 1084 obj = ns[oname_head]
1085 1085 except KeyError:
1086 1086 continue
1087 1087 else:
1088 1088 #print 'oname_rest:', oname_rest # dbg
1089 1089 for part in oname_rest:
1090 1090 try:
1091 1091 parent = obj
1092 1092 obj = getattr(obj,part)
1093 1093 except:
1094 1094 # Blanket except b/c some badly implemented objects
1095 1095 # allow __getattr__ to raise exceptions other than
1096 1096 # AttributeError, which then crashes IPython.
1097 1097 break
1098 1098 else:
1099 1099 # If we finish the for loop (no break), we got all members
1100 1100 found = True
1101 1101 ospace = nsname
1102 1102 if ns == alias_ns:
1103 1103 isalias = True
1104 1104 break # namespace loop
1105 1105
1106 1106 # Try to see if it's magic
1107 1107 if not found:
1108 1108 if oname.startswith(ESC_MAGIC):
1109 1109 oname = oname[1:]
1110 1110 obj = getattr(self,'magic_'+oname,None)
1111 1111 if obj is not None:
1112 1112 found = True
1113 1113 ospace = 'IPython internal'
1114 1114 ismagic = True
1115 1115
1116 1116 # Last try: special-case some literals like '', [], {}, etc:
1117 1117 if not found and oname_head in ["''",'""','[]','{}','()']:
1118 1118 obj = eval(oname_head)
1119 1119 found = True
1120 1120 ospace = 'Interactive'
1121 1121
1122 1122 return {'found':found, 'obj':obj, 'namespace':ospace,
1123 1123 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1124 1124
1125 1125 def _ofind_property(self, oname, info):
1126 1126 """Second part of object finding, to look for property details."""
1127 1127 if info.found:
1128 1128 # Get the docstring of the class property if it exists.
1129 1129 path = oname.split('.')
1130 1130 root = '.'.join(path[:-1])
1131 1131 if info.parent is not None:
1132 1132 try:
1133 1133 target = getattr(info.parent, '__class__')
1134 1134 # The object belongs to a class instance.
1135 1135 try:
1136 1136 target = getattr(target, path[-1])
1137 1137 # The class defines the object.
1138 1138 if isinstance(target, property):
1139 1139 oname = root + '.__class__.' + path[-1]
1140 1140 info = Struct(self._ofind(oname))
1141 1141 except AttributeError: pass
1142 1142 except AttributeError: pass
1143 1143
1144 1144 # We return either the new info or the unmodified input if the object
1145 1145 # hadn't been found
1146 1146 return info
1147 1147
1148 1148 def _object_find(self, oname, namespaces=None):
1149 1149 """Find an object and return a struct with info about it."""
1150 1150 inf = Struct(self._ofind(oname, namespaces))
1151 1151 return Struct(self._ofind_property(oname, inf))
1152 1152
1153 1153 def _inspect(self, meth, oname, namespaces=None, **kw):
1154 1154 """Generic interface to the inspector system.
1155 1155
1156 1156 This function is meant to be called by pdef, pdoc & friends."""
1157 1157 info = self._object_find(oname)
1158 1158 if info.found:
1159 1159 pmethod = getattr(self.inspector, meth)
1160 1160 formatter = format_screen if info.ismagic else None
1161 1161 if meth == 'pdoc':
1162 1162 pmethod(info.obj, oname, formatter)
1163 1163 elif meth == 'pinfo':
1164 1164 pmethod(info.obj, oname, formatter, info, **kw)
1165 1165 else:
1166 1166 pmethod(info.obj, oname)
1167 1167 else:
1168 1168 print 'Object `%s` not found.' % oname
1169 1169 return 'not found' # so callers can take other action
1170 1170
1171 1171 def object_inspect(self, oname):
1172 1172 info = self._object_find(oname)
1173 1173 if info.found:
1174 1174 return self.inspector.info(info.obj, info=info)
1175 1175 else:
1176 1176 return oinspect.mk_object_info({'found' : False})
1177 1177
1178 1178 #-------------------------------------------------------------------------
1179 1179 # Things related to history management
1180 1180 #-------------------------------------------------------------------------
1181 1181
1182 1182 def init_history(self):
1183 1183 # List of input with multi-line handling.
1184 1184 self.input_hist = InputList()
1185 1185 # This one will hold the 'raw' input history, without any
1186 1186 # pre-processing. This will allow users to retrieve the input just as
1187 1187 # it was exactly typed in by the user, with %hist -r.
1188 1188 self.input_hist_raw = InputList()
1189 1189
1190 1190 # list of visited directories
1191 1191 try:
1192 1192 self.dir_hist = [os.getcwd()]
1193 1193 except OSError:
1194 1194 self.dir_hist = []
1195 1195
1196 1196 # dict of output history
1197 1197 self.output_hist = {}
1198 1198
1199 1199 # Now the history file
1200 1200 if self.profile:
1201 1201 histfname = 'history-%s' % self.profile
1202 1202 else:
1203 1203 histfname = 'history'
1204 1204 self.histfile = os.path.join(self.ipython_dir, histfname)
1205 1205
1206 1206 # Fill the history zero entry, user counter starts at 1
1207 1207 self.input_hist.append('\n')
1208 1208 self.input_hist_raw.append('\n')
1209 1209
1210 1210 def init_shadow_hist(self):
1211 1211 try:
1212 1212 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1213 1213 except exceptions.UnicodeDecodeError:
1214 1214 print "Your ipython_dir can't be decoded to unicode!"
1215 1215 print "Please set HOME environment variable to something that"
1216 1216 print r"only has ASCII characters, e.g. c:\home"
1217 1217 print "Now it is", self.ipython_dir
1218 1218 sys.exit()
1219 1219 self.shadowhist = ipcorehist.ShadowHist(self.db)
1220 1220
1221 1221 def savehist(self):
1222 1222 """Save input history to a file (via readline library)."""
1223 1223
1224 1224 try:
1225 1225 self.readline.write_history_file(self.histfile)
1226 1226 except:
1227 1227 print 'Unable to save IPython command history to file: ' + \
1228 1228 `self.histfile`
1229 1229
1230 1230 def reloadhist(self):
1231 1231 """Reload the input history from disk file."""
1232 1232
1233 1233 try:
1234 1234 self.readline.clear_history()
1235 1235 self.readline.read_history_file(self.shell.histfile)
1236 1236 except AttributeError:
1237 1237 pass
1238 1238
1239 1239 def history_saving_wrapper(self, func):
1240 1240 """ Wrap func for readline history saving
1241 1241
1242 1242 Convert func into callable that saves & restores
1243 1243 history around the call """
1244 1244
1245 1245 if self.has_readline:
1246 1246 from IPython.utils import rlineimpl as readline
1247 1247 else:
1248 1248 return func
1249 1249
1250 1250 def wrapper():
1251 1251 self.savehist()
1252 1252 try:
1253 1253 func()
1254 1254 finally:
1255 1255 readline.read_history_file(self.histfile)
1256 1256 return wrapper
1257 1257
1258 1258 def get_history(self, index=None, raw=False, output=True):
1259 1259 """Get the history list.
1260 1260
1261 1261 Get the input and output history.
1262 1262
1263 1263 Parameters
1264 1264 ----------
1265 1265 index : n or (n1, n2) or None
1266 1266 If n, then the last entries. If a tuple, then all in
1267 1267 range(n1, n2). If None, then all entries. Raises IndexError if
1268 1268 the format of index is incorrect.
1269 1269 raw : bool
1270 1270 If True, return the raw input.
1271 1271 output : bool
1272 1272 If True, then return the output as well.
1273 1273
1274 1274 Returns
1275 1275 -------
1276 1276 If output is True, then return a dict of tuples, keyed by the prompt
1277 1277 numbers and with values of (input, output). If output is False, then
1278 1278 a dict, keyed by the prompt number with the values of input. Raises
1279 1279 IndexError if no history is found.
1280 1280 """
1281 1281 if raw:
1282 1282 input_hist = self.input_hist_raw
1283 1283 else:
1284 1284 input_hist = self.input_hist
1285 1285 if output:
1286 1286 output_hist = self.user_ns['Out']
1287 1287 n = len(input_hist)
1288 1288 if index is None:
1289 1289 start=0; stop=n
1290 1290 elif isinstance(index, int):
1291 1291 start=n-index; stop=n
1292 1292 elif isinstance(index, tuple) and len(index) == 2:
1293 1293 start=index[0]; stop=index[1]
1294 1294 else:
1295 1295 raise IndexError('Not a valid index for the input history: %r'
1296 1296 % index)
1297 1297 hist = {}
1298 1298 for i in range(start, stop):
1299 1299 if output:
1300 1300 hist[i] = (input_hist[i], output_hist.get(i))
1301 1301 else:
1302 1302 hist[i] = input_hist[i]
1303 1303 if len(hist)==0:
1304 1304 raise IndexError('No history for range of indices: %r' % index)
1305 1305 return hist
1306 1306
1307 1307 #-------------------------------------------------------------------------
1308 1308 # Things related to exception handling and tracebacks (not debugging)
1309 1309 #-------------------------------------------------------------------------
1310 1310
1311 1311 def init_traceback_handlers(self, custom_exceptions):
1312 1312 # Syntax error handler.
1313 1313 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1314 1314
1315 1315 # The interactive one is initialized with an offset, meaning we always
1316 1316 # want to remove the topmost item in the traceback, which is our own
1317 1317 # internal code. Valid modes: ['Plain','Context','Verbose']
1318 1318 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1319 1319 color_scheme='NoColor',
1320 1320 tb_offset = 1)
1321 1321
1322 1322 # The instance will store a pointer to the system-wide exception hook,
1323 1323 # so that runtime code (such as magics) can access it. This is because
1324 1324 # during the read-eval loop, it may get temporarily overwritten.
1325 1325 self.sys_excepthook = sys.excepthook
1326 1326
1327 1327 # and add any custom exception handlers the user may have specified
1328 1328 self.set_custom_exc(*custom_exceptions)
1329 1329
1330 1330 # Set the exception mode
1331 1331 self.InteractiveTB.set_mode(mode=self.xmode)
1332 1332
1333 1333 def set_custom_exc(self, exc_tuple, handler):
1334 1334 """set_custom_exc(exc_tuple,handler)
1335 1335
1336 1336 Set a custom exception handler, which will be called if any of the
1337 1337 exceptions in exc_tuple occur in the mainloop (specifically, in the
1338 1338 runcode() method.
1339 1339
1340 1340 Inputs:
1341 1341
1342 1342 - exc_tuple: a *tuple* of valid exceptions to call the defined
1343 1343 handler for. It is very important that you use a tuple, and NOT A
1344 1344 LIST here, because of the way Python's except statement works. If
1345 1345 you only want to trap a single exception, use a singleton tuple:
1346 1346
1347 1347 exc_tuple == (MyCustomException,)
1348 1348
1349 1349 - handler: this must be defined as a function with the following
1350 1350 basic interface::
1351 1351
1352 1352 def my_handler(self, etype, value, tb, tb_offset=None)
1353 1353 ...
1354 1354 # The return value must be
1355 1355 return structured_traceback
1356 1356
1357 1357 This will be made into an instance method (via new.instancemethod)
1358 1358 of IPython itself, and it will be called if any of the exceptions
1359 1359 listed in the exc_tuple are caught. If the handler is None, an
1360 1360 internal basic one is used, which just prints basic info.
1361 1361
1362 1362 WARNING: by putting in your own exception handler into IPython's main
1363 1363 execution loop, you run a very good chance of nasty crashes. This
1364 1364 facility should only be used if you really know what you are doing."""
1365 1365
1366 1366 assert type(exc_tuple)==type(()) , \
1367 1367 "The custom exceptions must be given AS A TUPLE."
1368 1368
1369 1369 def dummy_handler(self,etype,value,tb):
1370 1370 print '*** Simple custom exception handler ***'
1371 1371 print 'Exception type :',etype
1372 1372 print 'Exception value:',value
1373 1373 print 'Traceback :',tb
1374 1374 print 'Source code :','\n'.join(self.buffer)
1375 1375
1376 1376 if handler is None: handler = dummy_handler
1377 1377
1378 1378 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1379 1379 self.custom_exceptions = exc_tuple
1380 1380
1381 1381 def excepthook(self, etype, value, tb):
1382 1382 """One more defense for GUI apps that call sys.excepthook.
1383 1383
1384 1384 GUI frameworks like wxPython trap exceptions and call
1385 1385 sys.excepthook themselves. I guess this is a feature that
1386 1386 enables them to keep running after exceptions that would
1387 1387 otherwise kill their mainloop. This is a bother for IPython
1388 1388 which excepts to catch all of the program exceptions with a try:
1389 1389 except: statement.
1390 1390
1391 1391 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1392 1392 any app directly invokes sys.excepthook, it will look to the user like
1393 1393 IPython crashed. In order to work around this, we can disable the
1394 1394 CrashHandler and replace it with this excepthook instead, which prints a
1395 1395 regular traceback using our InteractiveTB. In this fashion, apps which
1396 1396 call sys.excepthook will generate a regular-looking exception from
1397 1397 IPython, and the CrashHandler will only be triggered by real IPython
1398 1398 crashes.
1399 1399
1400 1400 This hook should be used sparingly, only in places which are not likely
1401 1401 to be true IPython errors.
1402 1402 """
1403 1403 self.showtraceback((etype,value,tb),tb_offset=0)
1404 1404
1405 1405 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1406 1406 exception_only=False):
1407 1407 """Display the exception that just occurred.
1408 1408
1409 1409 If nothing is known about the exception, this is the method which
1410 1410 should be used throughout the code for presenting user tracebacks,
1411 1411 rather than directly invoking the InteractiveTB object.
1412 1412
1413 1413 A specific showsyntaxerror() also exists, but this method can take
1414 1414 care of calling it if needed, so unless you are explicitly catching a
1415 1415 SyntaxError exception, don't try to analyze the stack manually and
1416 1416 simply call this method."""
1417 1417
1418 1418 try:
1419 1419 if exc_tuple is None:
1420 1420 etype, value, tb = sys.exc_info()
1421 1421 else:
1422 1422 etype, value, tb = exc_tuple
1423 1423
1424 1424 if etype is None:
1425 1425 if hasattr(sys, 'last_type'):
1426 1426 etype, value, tb = sys.last_type, sys.last_value, \
1427 1427 sys.last_traceback
1428 1428 else:
1429 1429 self.write_err('No traceback available to show.\n')
1430 1430 return
1431 1431
1432 1432 if etype is SyntaxError:
1433 1433 # Though this won't be called by syntax errors in the input
1434 1434 # line, there may be SyntaxError cases whith imported code.
1435 1435 self.showsyntaxerror(filename)
1436 1436 elif etype is UsageError:
1437 1437 print "UsageError:", value
1438 1438 else:
1439 1439 # WARNING: these variables are somewhat deprecated and not
1440 1440 # necessarily safe to use in a threaded environment, but tools
1441 1441 # like pdb depend on their existence, so let's set them. If we
1442 1442 # find problems in the field, we'll need to revisit their use.
1443 1443 sys.last_type = etype
1444 1444 sys.last_value = value
1445 1445 sys.last_traceback = tb
1446 1446
1447 1447 if etype in self.custom_exceptions:
1448 1448 # FIXME: Old custom traceback objects may just return a
1449 1449 # string, in that case we just put it into a list
1450 1450 stb = self.CustomTB(etype, value, tb, tb_offset)
1451 1451 if isinstance(ctb, basestring):
1452 1452 stb = [stb]
1453 1453 else:
1454 1454 if exception_only:
1455 1455 stb = ['An exception has occurred, use %tb to see '
1456 1456 'the full traceback.\n']
1457 1457 stb.extend(self.InteractiveTB.get_exception_only(etype,
1458 1458 value))
1459 1459 else:
1460 1460 stb = self.InteractiveTB.structured_traceback(etype,
1461 1461 value, tb, tb_offset=tb_offset)
1462 1462 # FIXME: the pdb calling should be done by us, not by
1463 1463 # the code computing the traceback.
1464 1464 if self.InteractiveTB.call_pdb:
1465 1465 # pdb mucks up readline, fix it back
1466 1466 self.set_readline_completer()
1467 1467
1468 1468 # Actually show the traceback
1469 1469 self._showtraceback(etype, value, stb)
1470 1470
1471 1471 except KeyboardInterrupt:
1472 1472 self.write_err("\nKeyboardInterrupt\n")
1473 1473
1474 1474 def _showtraceback(self, etype, evalue, stb):
1475 1475 """Actually show a traceback.
1476 1476
1477 1477 Subclasses may override this method to put the traceback on a different
1478 1478 place, like a side channel.
1479 1479 """
1480 1480 # FIXME: this should use the proper write channels, but our test suite
1481 1481 # relies on it coming out of stdout...
1482 1482 print >> sys.stdout, self.InteractiveTB.stb2text(stb)
1483 1483
1484 1484 def showsyntaxerror(self, filename=None):
1485 1485 """Display the syntax error that just occurred.
1486 1486
1487 1487 This doesn't display a stack trace because there isn't one.
1488 1488
1489 1489 If a filename is given, it is stuffed in the exception instead
1490 1490 of what was there before (because Python's parser always uses
1491 1491 "<string>" when reading from a string).
1492 1492 """
1493 1493 etype, value, last_traceback = sys.exc_info()
1494 1494
1495 1495 # See note about these variables in showtraceback() above
1496 1496 sys.last_type = etype
1497 1497 sys.last_value = value
1498 1498 sys.last_traceback = last_traceback
1499 1499
1500 1500 if filename and etype is SyntaxError:
1501 1501 # Work hard to stuff the correct filename in the exception
1502 1502 try:
1503 1503 msg, (dummy_filename, lineno, offset, line) = value
1504 1504 except:
1505 1505 # Not the format we expect; leave it alone
1506 1506 pass
1507 1507 else:
1508 1508 # Stuff in the right filename
1509 1509 try:
1510 1510 # Assume SyntaxError is a class exception
1511 1511 value = SyntaxError(msg, (filename, lineno, offset, line))
1512 1512 except:
1513 1513 # If that failed, assume SyntaxError is a string
1514 1514 value = msg, (filename, lineno, offset, line)
1515 1515 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1516 1516 self._showtraceback(etype, value, stb)
1517 1517
1518 1518 #-------------------------------------------------------------------------
1519 1519 # Things related to readline
1520 1520 #-------------------------------------------------------------------------
1521 1521
1522 1522 def init_readline(self):
1523 1523 """Command history completion/saving/reloading."""
1524 1524
1525 1525 if self.readline_use:
1526 1526 import IPython.utils.rlineimpl as readline
1527 1527
1528 1528 self.rl_next_input = None
1529 1529 self.rl_do_indent = False
1530 1530
1531 1531 if not self.readline_use or not readline.have_readline:
1532 1532 self.has_readline = False
1533 1533 self.readline = None
1534 1534 # Set a number of methods that depend on readline to be no-op
1535 1535 self.savehist = no_op
1536 1536 self.reloadhist = no_op
1537 1537 self.set_readline_completer = no_op
1538 1538 self.set_custom_completer = no_op
1539 1539 self.set_completer_frame = no_op
1540 1540 warn('Readline services not available or not loaded.')
1541 1541 else:
1542 1542 self.has_readline = True
1543 1543 self.readline = readline
1544 1544 sys.modules['readline'] = readline
1545 1545
1546 1546 # Platform-specific configuration
1547 1547 if os.name == 'nt':
1548 1548 # FIXME - check with Frederick to see if we can harmonize
1549 1549 # naming conventions with pyreadline to avoid this
1550 1550 # platform-dependent check
1551 1551 self.readline_startup_hook = readline.set_pre_input_hook
1552 1552 else:
1553 1553 self.readline_startup_hook = readline.set_startup_hook
1554 1554
1555 1555 # Load user's initrc file (readline config)
1556 1556 # Or if libedit is used, load editrc.
1557 1557 inputrc_name = os.environ.get('INPUTRC')
1558 1558 if inputrc_name is None:
1559 1559 home_dir = get_home_dir()
1560 1560 if home_dir is not None:
1561 1561 inputrc_name = '.inputrc'
1562 1562 if readline.uses_libedit:
1563 1563 inputrc_name = '.editrc'
1564 1564 inputrc_name = os.path.join(home_dir, inputrc_name)
1565 1565 if os.path.isfile(inputrc_name):
1566 1566 try:
1567 1567 readline.read_init_file(inputrc_name)
1568 1568 except:
1569 1569 warn('Problems reading readline initialization file <%s>'
1570 1570 % inputrc_name)
1571 1571
1572 1572 # Configure readline according to user's prefs
1573 1573 # This is only done if GNU readline is being used. If libedit
1574 1574 # is being used (as on Leopard) the readline config is
1575 1575 # not run as the syntax for libedit is different.
1576 1576 if not readline.uses_libedit:
1577 1577 for rlcommand in self.readline_parse_and_bind:
1578 1578 #print "loading rl:",rlcommand # dbg
1579 1579 readline.parse_and_bind(rlcommand)
1580 1580
1581 1581 # Remove some chars from the delimiters list. If we encounter
1582 1582 # unicode chars, discard them.
1583 1583 delims = readline.get_completer_delims().encode("ascii", "ignore")
1584 1584 delims = delims.translate(string._idmap,
1585 1585 self.readline_remove_delims)
1586 1586 delims = delims.replace(ESC_MAGIC, '')
1587 1587 readline.set_completer_delims(delims)
1588 1588 # otherwise we end up with a monster history after a while:
1589 1589 readline.set_history_length(1000)
1590 1590 try:
1591 1591 #print '*** Reading readline history' # dbg
1592 1592 readline.read_history_file(self.histfile)
1593 1593 except IOError:
1594 1594 pass # It doesn't exist yet.
1595 1595
1596 1596 # If we have readline, we want our history saved upon ipython
1597 1597 # exiting.
1598 1598 atexit.register(self.savehist)
1599 1599
1600 1600 # Configure auto-indent for all platforms
1601 1601 self.set_autoindent(self.autoindent)
1602 1602
1603 1603 def set_next_input(self, s):
1604 1604 """ Sets the 'default' input string for the next command line.
1605 1605
1606 1606 Requires readline.
1607 1607
1608 1608 Example:
1609 1609
1610 1610 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1611 1611 [D:\ipython]|2> Hello Word_ # cursor is here
1612 1612 """
1613 1613
1614 1614 self.rl_next_input = s
1615 1615
1616 1616 # Maybe move this to the terminal subclass?
1617 1617 def pre_readline(self):
1618 1618 """readline hook to be used at the start of each line.
1619 1619
1620 1620 Currently it handles auto-indent only."""
1621 1621
1622 1622 if self.rl_do_indent:
1623 1623 self.readline.insert_text(self._indent_current_str())
1624 1624 if self.rl_next_input is not None:
1625 1625 self.readline.insert_text(self.rl_next_input)
1626 1626 self.rl_next_input = None
1627 1627
1628 1628 def _indent_current_str(self):
1629 1629 """return the current level of indentation as a string"""
1630 1630 return self.indent_current_nsp * ' '
1631 1631
1632 1632 #-------------------------------------------------------------------------
1633 1633 # Things related to text completion
1634 1634 #-------------------------------------------------------------------------
1635 1635
1636 1636 def init_completer(self):
1637 1637 """Initialize the completion machinery.
1638 1638
1639 1639 This creates completion machinery that can be used by client code,
1640 1640 either interactively in-process (typically triggered by the readline
1641 1641 library), programatically (such as in test suites) or out-of-prcess
1642 1642 (typically over the network by remote frontends).
1643 1643 """
1644 1644 from IPython.core.completer import IPCompleter
1645 from IPython.core.completerlib import (module_completer,
1646 magic_run_completer, cd_completer)
1647
1645 1648 self.Completer = IPCompleter(self,
1646 1649 self.user_ns,
1647 1650 self.user_global_ns,
1648 1651 self.readline_omit__names,
1649 1652 self.alias_manager.alias_table,
1650 1653 self.has_readline)
1654
1655 # Add custom completers to the basic ones built into IPCompleter
1651 1656 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1652 1657 self.strdispatchers['complete_command'] = sdisp
1653 1658 self.Completer.custom_completers = sdisp
1654 1659
1660 self.set_hook('complete_command', module_completer, str_key = 'import')
1661 self.set_hook('complete_command', module_completer, str_key = 'from')
1662 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1663 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1664
1665 # Only configure readline if we truly are using readline. IPython can
1666 # do tab-completion over the network, in GUIs, etc, where readline
1667 # itself may be absent
1655 1668 if self.has_readline:
1656 1669 self.set_readline_completer()
1657 1670
1658 1671 def complete(self, text, line=None, cursor_pos=None):
1659 1672 """Return the completed text and a list of completions.
1660 1673
1661 1674 Parameters
1662 1675 ----------
1663 1676
1664 1677 text : string
1665 1678 A string of text to be completed on. It can be given as empty and
1666 1679 instead a line/position pair are given. In this case, the
1667 1680 completer itself will split the line like readline does.
1668 1681
1669 1682 line : string, optional
1670 1683 The complete line that text is part of.
1671 1684
1672 1685 cursor_pos : int, optional
1673 1686 The position of the cursor on the input line.
1674 1687
1675 1688 Returns
1676 1689 -------
1677 1690 text : string
1678 1691 The actual text that was completed.
1679 1692
1680 1693 matches : list
1681 1694 A sorted list with all possible completions.
1682 1695
1683 1696 The optional arguments allow the completion to take more context into
1684 1697 account, and are part of the low-level completion API.
1685 1698
1686 1699 This is a wrapper around the completion mechanism, similar to what
1687 1700 readline does at the command line when the TAB key is hit. By
1688 1701 exposing it as a method, it can be used by other non-readline
1689 1702 environments (such as GUIs) for text completion.
1690 1703
1691 1704 Simple usage example:
1692 1705
1693 1706 In [1]: x = 'hello'
1694 1707
1695 1708 In [2]: _ip.complete('x.l')
1696 1709 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1697 1710 """
1698 1711
1699 1712 # Inject names into __builtin__ so we can complete on the added names.
1700 1713 with self.builtin_trap:
1701 1714 return self.Completer.complete(text, line, cursor_pos)
1702 1715
1703 1716 def set_custom_completer(self, completer, pos=0):
1704 1717 """Adds a new custom completer function.
1705 1718
1706 1719 The position argument (defaults to 0) is the index in the completers
1707 1720 list where you want the completer to be inserted."""
1708 1721
1709 1722 newcomp = new.instancemethod(completer,self.Completer,
1710 1723 self.Completer.__class__)
1711 1724 self.Completer.matchers.insert(pos,newcomp)
1712 1725
1713 1726 def set_readline_completer(self):
1714 1727 """Reset readline's completer to be our own."""
1715 1728 self.readline.set_completer(self.Completer.rlcomplete)
1716 1729
1717 1730 def set_completer_frame(self, frame=None):
1718 1731 """Set the frame of the completer."""
1719 1732 if frame:
1720 1733 self.Completer.namespace = frame.f_locals
1721 1734 self.Completer.global_namespace = frame.f_globals
1722 1735 else:
1723 1736 self.Completer.namespace = self.user_ns
1724 1737 self.Completer.global_namespace = self.user_global_ns
1725 1738
1726 1739 #-------------------------------------------------------------------------
1727 1740 # Things related to magics
1728 1741 #-------------------------------------------------------------------------
1729 1742
1730 1743 def init_magics(self):
1731 1744 # FIXME: Move the color initialization to the DisplayHook, which
1732 1745 # should be split into a prompt manager and displayhook. We probably
1733 1746 # even need a centralize colors management object.
1734 1747 self.magic_colors(self.colors)
1735 1748 # History was moved to a separate module
1736 1749 from . import history
1737 1750 history.init_ipython(self)
1738 1751
1739 1752 def magic(self,arg_s):
1740 1753 """Call a magic function by name.
1741 1754
1742 1755 Input: a string containing the name of the magic function to call and
1743 1756 any additional arguments to be passed to the magic.
1744 1757
1745 1758 magic('name -opt foo bar') is equivalent to typing at the ipython
1746 1759 prompt:
1747 1760
1748 1761 In[1]: %name -opt foo bar
1749 1762
1750 1763 To call a magic without arguments, simply use magic('name').
1751 1764
1752 1765 This provides a proper Python function to call IPython's magics in any
1753 1766 valid Python code you can type at the interpreter, including loops and
1754 1767 compound statements.
1755 1768 """
1756 1769 args = arg_s.split(' ',1)
1757 1770 magic_name = args[0]
1758 1771 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1759 1772
1760 1773 try:
1761 1774 magic_args = args[1]
1762 1775 except IndexError:
1763 1776 magic_args = ''
1764 1777 fn = getattr(self,'magic_'+magic_name,None)
1765 1778 if fn is None:
1766 1779 error("Magic function `%s` not found." % magic_name)
1767 1780 else:
1768 1781 magic_args = self.var_expand(magic_args,1)
1769 1782 with nested(self.builtin_trap,):
1770 1783 result = fn(magic_args)
1771 1784 return result
1772 1785
1773 1786 def define_magic(self, magicname, func):
1774 1787 """Expose own function as magic function for ipython
1775 1788
1776 1789 def foo_impl(self,parameter_s=''):
1777 1790 'My very own magic!. (Use docstrings, IPython reads them).'
1778 1791 print 'Magic function. Passed parameter is between < >:'
1779 1792 print '<%s>' % parameter_s
1780 1793 print 'The self object is:',self
1781 1794
1782 1795 self.define_magic('foo',foo_impl)
1783 1796 """
1784 1797
1785 1798 import new
1786 1799 im = new.instancemethod(func,self, self.__class__)
1787 1800 old = getattr(self, "magic_" + magicname, None)
1788 1801 setattr(self, "magic_" + magicname, im)
1789 1802 return old
1790 1803
1791 1804 #-------------------------------------------------------------------------
1792 1805 # Things related to macros
1793 1806 #-------------------------------------------------------------------------
1794 1807
1795 1808 def define_macro(self, name, themacro):
1796 1809 """Define a new macro
1797 1810
1798 1811 Parameters
1799 1812 ----------
1800 1813 name : str
1801 1814 The name of the macro.
1802 1815 themacro : str or Macro
1803 1816 The action to do upon invoking the macro. If a string, a new
1804 1817 Macro object is created by passing the string to it.
1805 1818 """
1806 1819
1807 1820 from IPython.core import macro
1808 1821
1809 1822 if isinstance(themacro, basestring):
1810 1823 themacro = macro.Macro(themacro)
1811 1824 if not isinstance(themacro, macro.Macro):
1812 1825 raise ValueError('A macro must be a string or a Macro instance.')
1813 1826 self.user_ns[name] = themacro
1814 1827
1815 1828 #-------------------------------------------------------------------------
1816 1829 # Things related to the running of system commands
1817 1830 #-------------------------------------------------------------------------
1818 1831
1819 1832 def system(self, cmd):
1820 1833 """Call the given cmd in a subprocess."""
1821 1834 # We do not support backgrounding processes because we either use
1822 1835 # pexpect or pipes to read from. Users can always just call
1823 1836 # os.system() if they really want a background process.
1824 1837 if cmd.endswith('&'):
1825 1838 raise OSError("Background processes not supported.")
1826 1839
1827 1840 return system(self.var_expand(cmd, depth=2))
1828 1841
1829 1842 def getoutput(self, cmd):
1830 1843 """Get output (possibly including stderr) from a subprocess."""
1831 1844 if cmd.endswith('&'):
1832 1845 raise OSError("Background processes not supported.")
1833 1846 return getoutput(self.var_expand(cmd, depth=2))
1834 1847
1835 1848 #-------------------------------------------------------------------------
1836 1849 # Things related to aliases
1837 1850 #-------------------------------------------------------------------------
1838 1851
1839 1852 def init_alias(self):
1840 1853 self.alias_manager = AliasManager(shell=self, config=self.config)
1841 1854 self.ns_table['alias'] = self.alias_manager.alias_table,
1842 1855
1843 1856 #-------------------------------------------------------------------------
1844 1857 # Things related to extensions and plugins
1845 1858 #-------------------------------------------------------------------------
1846 1859
1847 1860 def init_extension_manager(self):
1848 1861 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1849 1862
1850 1863 def init_plugin_manager(self):
1851 1864 self.plugin_manager = PluginManager(config=self.config)
1852 1865
1853 1866 #-------------------------------------------------------------------------
1854 1867 # Things related to payloads
1855 1868 #-------------------------------------------------------------------------
1856 1869
1857 1870 def init_payload(self):
1858 1871 self.payload_manager = PayloadManager(config=self.config)
1859 1872
1860 1873 #-------------------------------------------------------------------------
1861 1874 # Things related to the prefilter
1862 1875 #-------------------------------------------------------------------------
1863 1876
1864 1877 def init_prefilter(self):
1865 1878 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1866 1879 # Ultimately this will be refactored in the new interpreter code, but
1867 1880 # for now, we should expose the main prefilter method (there's legacy
1868 1881 # code out there that may rely on this).
1869 1882 self.prefilter = self.prefilter_manager.prefilter_lines
1870 1883
1871 1884
1872 1885 def auto_rewrite_input(self, cmd):
1873 1886 """Print to the screen the rewritten form of the user's command.
1874 1887
1875 1888 This shows visual feedback by rewriting input lines that cause
1876 1889 automatic calling to kick in, like::
1877 1890
1878 1891 /f x
1879 1892
1880 1893 into::
1881 1894
1882 1895 ------> f(x)
1883 1896
1884 1897 after the user's input prompt. This helps the user understand that the
1885 1898 input line was transformed automatically by IPython.
1886 1899 """
1887 1900 rw = self.displayhook.prompt1.auto_rewrite() + cmd
1888 1901
1889 1902 try:
1890 1903 # plain ascii works better w/ pyreadline, on some machines, so
1891 1904 # we use it and only print uncolored rewrite if we have unicode
1892 1905 rw = str(rw)
1893 1906 print >> IPython.utils.io.Term.cout, rw
1894 1907 except UnicodeEncodeError:
1895 1908 print "------> " + cmd
1896 1909
1897 1910 #-------------------------------------------------------------------------
1898 1911 # Things related to extracting values/expressions from kernel and user_ns
1899 1912 #-------------------------------------------------------------------------
1900 1913
1901 1914 def _simple_error(self):
1902 1915 etype, value = sys.exc_info()[:2]
1903 1916 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
1904 1917
1905 1918 def get_user_variables(self, names):
1906 1919 """Get a list of variable names from the user's namespace.
1907 1920
1908 1921 The return value is a dict with the repr() of each value.
1909 1922 """
1910 1923 out = {}
1911 1924 user_ns = self.user_ns
1912 1925 for varname in names:
1913 1926 try:
1914 1927 value = repr(user_ns[varname])
1915 1928 except:
1916 1929 value = self._simple_error()
1917 1930 out[varname] = value
1918 1931 return out
1919 1932
1920 1933 def eval_expressions(self, expressions):
1921 1934 """Evaluate a dict of expressions in the user's namespace.
1922 1935
1923 1936 The return value is a dict with the repr() of each value.
1924 1937 """
1925 1938 out = {}
1926 1939 user_ns = self.user_ns
1927 1940 global_ns = self.user_global_ns
1928 1941 for key, expr in expressions.iteritems():
1929 1942 try:
1930 1943 value = repr(eval(expr, global_ns, user_ns))
1931 1944 except:
1932 1945 value = self._simple_error()
1933 1946 out[key] = value
1934 1947 return out
1935 1948
1936 1949 #-------------------------------------------------------------------------
1937 1950 # Things related to the running of code
1938 1951 #-------------------------------------------------------------------------
1939 1952
1940 1953 def ex(self, cmd):
1941 1954 """Execute a normal python statement in user namespace."""
1942 1955 with nested(self.builtin_trap,):
1943 1956 exec cmd in self.user_global_ns, self.user_ns
1944 1957
1945 1958 def ev(self, expr):
1946 1959 """Evaluate python expression expr in user namespace.
1947 1960
1948 1961 Returns the result of evaluation
1949 1962 """
1950 1963 with nested(self.builtin_trap,):
1951 1964 return eval(expr, self.user_global_ns, self.user_ns)
1952 1965
1953 1966 def safe_execfile(self, fname, *where, **kw):
1954 1967 """A safe version of the builtin execfile().
1955 1968
1956 1969 This version will never throw an exception, but instead print
1957 1970 helpful error messages to the screen. This only works on pure
1958 1971 Python files with the .py extension.
1959 1972
1960 1973 Parameters
1961 1974 ----------
1962 1975 fname : string
1963 1976 The name of the file to be executed.
1964 1977 where : tuple
1965 1978 One or two namespaces, passed to execfile() as (globals,locals).
1966 1979 If only one is given, it is passed as both.
1967 1980 exit_ignore : bool (False)
1968 1981 If True, then silence SystemExit for non-zero status (it is always
1969 1982 silenced for zero status, as it is so common).
1970 1983 """
1971 1984 kw.setdefault('exit_ignore', False)
1972 1985
1973 1986 fname = os.path.abspath(os.path.expanduser(fname))
1974 1987
1975 1988 # Make sure we have a .py file
1976 1989 if not fname.endswith('.py'):
1977 1990 warn('File must end with .py to be run using execfile: <%s>' % fname)
1978 1991
1979 1992 # Make sure we can open the file
1980 1993 try:
1981 1994 with open(fname) as thefile:
1982 1995 pass
1983 1996 except:
1984 1997 warn('Could not open file <%s> for safe execution.' % fname)
1985 1998 return
1986 1999
1987 2000 # Find things also in current directory. This is needed to mimic the
1988 2001 # behavior of running a script from the system command line, where
1989 2002 # Python inserts the script's directory into sys.path
1990 2003 dname = os.path.dirname(fname)
1991 2004
1992 2005 with prepended_to_syspath(dname):
1993 2006 try:
1994 2007 execfile(fname,*where)
1995 2008 except SystemExit, status:
1996 2009 # If the call was made with 0 or None exit status (sys.exit(0)
1997 2010 # or sys.exit() ), don't bother showing a traceback, as both of
1998 2011 # these are considered normal by the OS:
1999 2012 # > python -c'import sys;sys.exit(0)'; echo $?
2000 2013 # 0
2001 2014 # > python -c'import sys;sys.exit()'; echo $?
2002 2015 # 0
2003 2016 # For other exit status, we show the exception unless
2004 2017 # explicitly silenced, but only in short form.
2005 2018 if status.code not in (0, None) and not kw['exit_ignore']:
2006 2019 self.showtraceback(exception_only=True)
2007 2020 except:
2008 2021 self.showtraceback()
2009 2022
2010 2023 def safe_execfile_ipy(self, fname):
2011 2024 """Like safe_execfile, but for .ipy files with IPython syntax.
2012 2025
2013 2026 Parameters
2014 2027 ----------
2015 2028 fname : str
2016 2029 The name of the file to execute. The filename must have a
2017 2030 .ipy extension.
2018 2031 """
2019 2032 fname = os.path.abspath(os.path.expanduser(fname))
2020 2033
2021 2034 # Make sure we have a .py file
2022 2035 if not fname.endswith('.ipy'):
2023 2036 warn('File must end with .py to be run using execfile: <%s>' % fname)
2024 2037
2025 2038 # Make sure we can open the file
2026 2039 try:
2027 2040 with open(fname) as thefile:
2028 2041 pass
2029 2042 except:
2030 2043 warn('Could not open file <%s> for safe execution.' % fname)
2031 2044 return
2032 2045
2033 2046 # Find things also in current directory. This is needed to mimic the
2034 2047 # behavior of running a script from the system command line, where
2035 2048 # Python inserts the script's directory into sys.path
2036 2049 dname = os.path.dirname(fname)
2037 2050
2038 2051 with prepended_to_syspath(dname):
2039 2052 try:
2040 2053 with open(fname) as thefile:
2041 2054 script = thefile.read()
2042 2055 # self.runlines currently captures all exceptions
2043 2056 # raise in user code. It would be nice if there were
2044 2057 # versions of runlines, execfile that did raise, so
2045 2058 # we could catch the errors.
2046 2059 self.runlines(script, clean=True)
2047 2060 except:
2048 2061 self.showtraceback()
2049 2062 warn('Unknown failure executing file: <%s>' % fname)
2050 2063
2051 2064 def runlines(self, lines, clean=False):
2052 2065 """Run a string of one or more lines of source.
2053 2066
2054 2067 This method is capable of running a string containing multiple source
2055 2068 lines, as if they had been entered at the IPython prompt. Since it
2056 2069 exposes IPython's processing machinery, the given strings can contain
2057 2070 magic calls (%magic), special shell access (!cmd), etc.
2058 2071 """
2059 2072
2060 2073 if isinstance(lines, (list, tuple)):
2061 2074 lines = '\n'.join(lines)
2062 2075
2063 2076 if clean:
2064 2077 lines = self._cleanup_ipy_script(lines)
2065 2078
2066 2079 # We must start with a clean buffer, in case this is run from an
2067 2080 # interactive IPython session (via a magic, for example).
2068 2081 self.resetbuffer()
2069 2082 lines = lines.splitlines()
2070 2083 more = 0
2071 2084 with nested(self.builtin_trap, self.display_trap):
2072 2085 for line in lines:
2073 2086 # skip blank lines so we don't mess up the prompt counter, but
2074 2087 # do NOT skip even a blank line if we are in a code block (more
2075 2088 # is true)
2076 2089
2077 2090 if line or more:
2078 2091 # push to raw history, so hist line numbers stay in sync
2079 2092 self.input_hist_raw.append(line + '\n')
2080 2093 prefiltered = self.prefilter_manager.prefilter_lines(line,
2081 2094 more)
2082 2095 more = self.push_line(prefiltered)
2083 2096 # IPython's runsource returns None if there was an error
2084 2097 # compiling the code. This allows us to stop processing
2085 2098 # right away, so the user gets the error message at the
2086 2099 # right place.
2087 2100 if more is None:
2088 2101 break
2089 2102 else:
2090 2103 self.input_hist_raw.append("\n")
2091 2104 # final newline in case the input didn't have it, so that the code
2092 2105 # actually does get executed
2093 2106 if more:
2094 2107 self.push_line('\n')
2095 2108
2096 2109 def runsource(self, source, filename='<input>', symbol='single'):
2097 2110 """Compile and run some source in the interpreter.
2098 2111
2099 2112 Arguments are as for compile_command().
2100 2113
2101 2114 One several things can happen:
2102 2115
2103 2116 1) The input is incorrect; compile_command() raised an
2104 2117 exception (SyntaxError or OverflowError). A syntax traceback
2105 2118 will be printed by calling the showsyntaxerror() method.
2106 2119
2107 2120 2) The input is incomplete, and more input is required;
2108 2121 compile_command() returned None. Nothing happens.
2109 2122
2110 2123 3) The input is complete; compile_command() returned a code
2111 2124 object. The code is executed by calling self.runcode() (which
2112 2125 also handles run-time exceptions, except for SystemExit).
2113 2126
2114 2127 The return value is:
2115 2128
2116 2129 - True in case 2
2117 2130
2118 2131 - False in the other cases, unless an exception is raised, where
2119 2132 None is returned instead. This can be used by external callers to
2120 2133 know whether to continue feeding input or not.
2121 2134
2122 2135 The return value can be used to decide whether to use sys.ps1 or
2123 2136 sys.ps2 to prompt the next line."""
2124 2137
2125 2138 # if the source code has leading blanks, add 'if 1:\n' to it
2126 2139 # this allows execution of indented pasted code. It is tempting
2127 2140 # to add '\n' at the end of source to run commands like ' a=1'
2128 2141 # directly, but this fails for more complicated scenarios
2129 2142 source=source.encode(self.stdin_encoding)
2130 2143 if source[:1] in [' ', '\t']:
2131 2144 source = 'if 1:\n%s' % source
2132 2145
2133 2146 try:
2134 2147 code = self.compile(source,filename,symbol)
2135 2148 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2136 2149 # Case 1
2137 2150 self.showsyntaxerror(filename)
2138 2151 return None
2139 2152
2140 2153 if code is None:
2141 2154 # Case 2
2142 2155 return True
2143 2156
2144 2157 # Case 3
2145 2158 # We store the code object so that threaded shells and
2146 2159 # custom exception handlers can access all this info if needed.
2147 2160 # The source corresponding to this can be obtained from the
2148 2161 # buffer attribute as '\n'.join(self.buffer).
2149 2162 self.code_to_run = code
2150 2163 # now actually execute the code object
2151 2164 if self.runcode(code) == 0:
2152 2165 return False
2153 2166 else:
2154 2167 return None
2155 2168
2156 2169 def runcode(self,code_obj):
2157 2170 """Execute a code object.
2158 2171
2159 2172 When an exception occurs, self.showtraceback() is called to display a
2160 2173 traceback.
2161 2174
2162 2175 Return value: a flag indicating whether the code to be run completed
2163 2176 successfully:
2164 2177
2165 2178 - 0: successful execution.
2166 2179 - 1: an error occurred.
2167 2180 """
2168 2181
2169 2182 # Set our own excepthook in case the user code tries to call it
2170 2183 # directly, so that the IPython crash handler doesn't get triggered
2171 2184 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2172 2185
2173 2186 # we save the original sys.excepthook in the instance, in case config
2174 2187 # code (such as magics) needs access to it.
2175 2188 self.sys_excepthook = old_excepthook
2176 2189 outflag = 1 # happens in more places, so it's easier as default
2177 2190 try:
2178 2191 try:
2179 2192 self.hooks.pre_runcode_hook()
2180 2193 #rprint('Running code') # dbg
2181 2194 exec code_obj in self.user_global_ns, self.user_ns
2182 2195 finally:
2183 2196 # Reset our crash handler in place
2184 2197 sys.excepthook = old_excepthook
2185 2198 except SystemExit:
2186 2199 self.resetbuffer()
2187 2200 self.showtraceback(exception_only=True)
2188 2201 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2189 2202 except self.custom_exceptions:
2190 2203 etype,value,tb = sys.exc_info()
2191 2204 self.CustomTB(etype,value,tb)
2192 2205 except:
2193 2206 self.showtraceback()
2194 2207 else:
2195 2208 outflag = 0
2196 2209 if softspace(sys.stdout, 0):
2197 2210 print
2198 2211 # Flush out code object which has been run (and source)
2199 2212 self.code_to_run = None
2200 2213 return outflag
2201 2214
2202 2215 def push_line(self, line):
2203 2216 """Push a line to the interpreter.
2204 2217
2205 2218 The line should not have a trailing newline; it may have
2206 2219 internal newlines. The line is appended to a buffer and the
2207 2220 interpreter's runsource() method is called with the
2208 2221 concatenated contents of the buffer as source. If this
2209 2222 indicates that the command was executed or invalid, the buffer
2210 2223 is reset; otherwise, the command is incomplete, and the buffer
2211 2224 is left as it was after the line was appended. The return
2212 2225 value is 1 if more input is required, 0 if the line was dealt
2213 2226 with in some way (this is the same as runsource()).
2214 2227 """
2215 2228
2216 2229 # autoindent management should be done here, and not in the
2217 2230 # interactive loop, since that one is only seen by keyboard input. We
2218 2231 # need this done correctly even for code run via runlines (which uses
2219 2232 # push).
2220 2233
2221 2234 #print 'push line: <%s>' % line # dbg
2222 2235 for subline in line.splitlines():
2223 2236 self._autoindent_update(subline)
2224 2237 self.buffer.append(line)
2225 2238 more = self.runsource('\n'.join(self.buffer), self.filename)
2226 2239 if not more:
2227 2240 self.resetbuffer()
2228 2241 return more
2229 2242
2230 2243 def resetbuffer(self):
2231 2244 """Reset the input buffer."""
2232 2245 self.buffer[:] = []
2233 2246
2234 2247 def _is_secondary_block_start(self, s):
2235 2248 if not s.endswith(':'):
2236 2249 return False
2237 2250 if (s.startswith('elif') or
2238 2251 s.startswith('else') or
2239 2252 s.startswith('except') or
2240 2253 s.startswith('finally')):
2241 2254 return True
2242 2255
2243 2256 def _cleanup_ipy_script(self, script):
2244 2257 """Make a script safe for self.runlines()
2245 2258
2246 2259 Currently, IPython is lines based, with blocks being detected by
2247 2260 empty lines. This is a problem for block based scripts that may
2248 2261 not have empty lines after blocks. This script adds those empty
2249 2262 lines to make scripts safe for running in the current line based
2250 2263 IPython.
2251 2264 """
2252 2265 res = []
2253 2266 lines = script.splitlines()
2254 2267 level = 0
2255 2268
2256 2269 for l in lines:
2257 2270 lstripped = l.lstrip()
2258 2271 stripped = l.strip()
2259 2272 if not stripped:
2260 2273 continue
2261 2274 newlevel = len(l) - len(lstripped)
2262 2275 if level > 0 and newlevel == 0 and \
2263 2276 not self._is_secondary_block_start(stripped):
2264 2277 # add empty line
2265 2278 res.append('')
2266 2279 res.append(l)
2267 2280 level = newlevel
2268 2281
2269 2282 return '\n'.join(res) + '\n'
2270 2283
2271 2284 def _autoindent_update(self,line):
2272 2285 """Keep track of the indent level."""
2273 2286
2274 2287 #debugx('line')
2275 2288 #debugx('self.indent_current_nsp')
2276 2289 if self.autoindent:
2277 2290 if line:
2278 2291 inisp = num_ini_spaces(line)
2279 2292 if inisp < self.indent_current_nsp:
2280 2293 self.indent_current_nsp = inisp
2281 2294
2282 2295 if line[-1] == ':':
2283 2296 self.indent_current_nsp += 4
2284 2297 elif dedent_re.match(line):
2285 2298 self.indent_current_nsp -= 4
2286 2299 else:
2287 2300 self.indent_current_nsp = 0
2288 2301
2289 2302 #-------------------------------------------------------------------------
2290 2303 # Things related to GUI support and pylab
2291 2304 #-------------------------------------------------------------------------
2292 2305
2293 2306 def enable_pylab(self, gui=None):
2294 2307 raise NotImplementedError('Implement enable_pylab in a subclass')
2295 2308
2296 2309 #-------------------------------------------------------------------------
2297 2310 # Utilities
2298 2311 #-------------------------------------------------------------------------
2299 2312
2300 2313 def var_expand(self,cmd,depth=0):
2301 2314 """Expand python variables in a string.
2302 2315
2303 2316 The depth argument indicates how many frames above the caller should
2304 2317 be walked to look for the local namespace where to expand variables.
2305 2318
2306 2319 The global namespace for expansion is always the user's interactive
2307 2320 namespace.
2308 2321 """
2309 2322
2310 2323 return str(ItplNS(cmd,
2311 2324 self.user_ns, # globals
2312 2325 # Skip our own frame in searching for locals:
2313 2326 sys._getframe(depth+1).f_locals # locals
2314 2327 ))
2315 2328
2316 2329 def mktempfile(self,data=None):
2317 2330 """Make a new tempfile and return its filename.
2318 2331
2319 2332 This makes a call to tempfile.mktemp, but it registers the created
2320 2333 filename internally so ipython cleans it up at exit time.
2321 2334
2322 2335 Optional inputs:
2323 2336
2324 2337 - data(None): if data is given, it gets written out to the temp file
2325 2338 immediately, and the file is closed again."""
2326 2339
2327 2340 filename = tempfile.mktemp('.py','ipython_edit_')
2328 2341 self.tempfiles.append(filename)
2329 2342
2330 2343 if data:
2331 2344 tmp_file = open(filename,'w')
2332 2345 tmp_file.write(data)
2333 2346 tmp_file.close()
2334 2347 return filename
2335 2348
2336 2349 # TODO: This should be removed when Term is refactored.
2337 2350 def write(self,data):
2338 2351 """Write a string to the default output"""
2339 2352 io.Term.cout.write(data)
2340 2353
2341 2354 # TODO: This should be removed when Term is refactored.
2342 2355 def write_err(self,data):
2343 2356 """Write a string to the default error output"""
2344 2357 io.Term.cerr.write(data)
2345 2358
2346 2359 def ask_yes_no(self,prompt,default=True):
2347 2360 if self.quiet:
2348 2361 return True
2349 2362 return ask_yes_no(prompt,default)
2350 2363
2351 2364 def show_usage(self):
2352 2365 """Show a usage message"""
2353 2366 page.page(IPython.core.usage.interactive_usage)
2354 2367
2355 2368 #-------------------------------------------------------------------------
2356 2369 # Things related to IPython exiting
2357 2370 #-------------------------------------------------------------------------
2358 2371 def atexit_operations(self):
2359 2372 """This will be executed at the time of exit.
2360 2373
2361 2374 Cleanup operations and saving of persistent data that is done
2362 2375 unconditionally by IPython should be performed here.
2363 2376
2364 2377 For things that may depend on startup flags or platform specifics (such
2365 2378 as having readline or not), register a separate atexit function in the
2366 2379 code that has the appropriate information, rather than trying to
2367 2380 clutter
2368 2381 """
2369 2382 # Cleanup all tempfiles left around
2370 2383 for tfile in self.tempfiles:
2371 2384 try:
2372 2385 os.unlink(tfile)
2373 2386 except OSError:
2374 2387 pass
2375 2388
2376 2389 # Clear all user namespaces to release all references cleanly.
2377 2390 self.reset()
2378 2391
2379 2392 # Run user hooks
2380 2393 self.hooks.shutdown_hook()
2381 2394
2382 2395 def cleanup(self):
2383 2396 self.restore_sys_module_state()
2384 2397
2385 2398
2386 2399 class InteractiveShellABC(object):
2387 2400 """An abstract base class for InteractiveShell."""
2388 2401 __metaclass__ = abc.ABCMeta
2389 2402
2390 2403 InteractiveShellABC.register(InteractiveShell)
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now